增加使用xdgicon查找图标功能

This commit is contained in:
hewenfei 2023-02-22 10:54:44 +08:00
parent cad6456a44
commit d80deb9e9f
3 changed files with 19 additions and 1 deletions

View File

@ -19,6 +19,7 @@ find_package(Qt5 COMPONENTS
# find kde framework components # find kde framework components
# see: https://api.kde.org/frameworks/index.html > KWindowSystem # see: https://api.kde.org/frameworks/index.html > KWindowSystem
find_package(KF5WindowSystem REQUIRED) find_package(KF5WindowSystem REQUIRED)
find_package(Qt5Xdg REQUIRED)
# #
# see: https://cmake.org/cmake/help/v3.16/module/FindPkgConfig.html # see: https://cmake.org/cmake/help/v3.16/module/FindPkgConfig.html
@ -134,6 +135,7 @@ target_link_libraries(${PROJECT_NAME}
Qt5::DBus Qt5::DBus
Qt5::X11Extras Qt5::X11Extras
KF5::WindowSystem KF5::WindowSystem
Qt5Xdg
${SingleApplication} ${SingleApplication}
${UKUI_MENU_EXTERNAL_LIBS} ${UKUI_MENU_EXTERNAL_LIBS}
) )

View File

@ -21,6 +21,7 @@
#include <QDebug> #include <QDebug>
#include <QIcon> #include <QIcon>
#include <QFile> #include <QFile>
#include <qt5xdg/XdgIcon>
namespace UkuiMenu { namespace UkuiMenu {
@ -65,6 +66,9 @@ void AppIconProvider::loadPixmap(const QString &id, QPixmap &pixmap)
} else { } else {
isOk = loadPixmapFromTheme(id, pixmap); isOk = loadPixmapFromTheme(id, pixmap);
if (!isOk) {
isOk = loadPixmapFromXdg(id, pixmap);
}
} }
if (!isOk) { if (!isOk) {
@ -113,7 +117,6 @@ bool AppIconProvider::loadPixmapFromPath(const QString &path, QPixmap &pixmap)
bool AppIconProvider::loadPixmapFromTheme(const QString &name, QPixmap &pixmap) bool AppIconProvider::loadPixmapFromTheme(const QString &name, QPixmap &pixmap)
{ {
if (!QIcon::hasThemeIcon(name)) { if (!QIcon::hasThemeIcon(name)) {
qWarning() << "Error: loadPixmapFromTheme, icon dose not exists. name:" << name;
return false; return false;
} }
QIcon icon = QIcon::fromTheme(name); QIcon icon = QIcon::fromTheme(name);
@ -128,4 +131,16 @@ void AppIconProvider::loadDefault(QPixmap &pixmap)
} }
} }
bool AppIconProvider::loadPixmapFromXdg(const QString &name, QPixmap &pixmap)
{
QIcon icon = XdgIcon::fromTheme(name);
if (icon.isNull()) {
qWarning() << "Error: loadPixmapFromXdg, icon dose not exists. name:" << name;
return false;
}
pixmap = icon.pixmap(AppIconProvider::s_defaultSize);
return true;
}
} // UkuiMenu } // UkuiMenu

View File

@ -37,6 +37,7 @@ private:
static bool loadPixmapFromUrl(const QUrl &url, QPixmap &pixmap); static bool loadPixmapFromUrl(const QUrl &url, QPixmap &pixmap);
static bool loadPixmapFromPath(const QString &path, QPixmap &pixmap); static bool loadPixmapFromPath(const QString &path, QPixmap &pixmap);
static bool loadPixmapFromTheme(const QString &name, QPixmap &pixmap); static bool loadPixmapFromTheme(const QString &name, QPixmap &pixmap);
static bool loadPixmapFromXdg(const QString &name, QPixmap &pixmap);
private: private:
static QSize s_defaultSize; static QSize s_defaultSize;