diff --git a/CMakeLists.txt b/CMakeLists.txt index 21b5f69..e035e62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,7 @@ set(SOURCE_FILES src/uiconfig/theme-palette.cpp src/uiconfig/theme-palette.h src/ukui-menu-application.cpp src/ukui-menu-application.h src/model/model-manager.cpp src/model/model-manager.h + src/appdata/app-icon-provider.cpp src/appdata/app-icon-provider.h ) # qrc文件 diff --git a/res/icon.ico b/res/icon.ico deleted file mode 100644 index e69de29..0000000 diff --git a/res/icon/application-x-desktop.png b/res/icon/application-x-desktop.png new file mode 100644 index 0000000..2095ab0 Binary files /dev/null and b/res/icon/application-x-desktop.png differ diff --git a/res/res.qrc b/res/res.qrc index 77380b8..72f745e 100644 --- a/res/res.qrc +++ b/res/res.qrc @@ -1,5 +1,5 @@ - icon.ico + icon/application-x-desktop.png diff --git a/src/appdata/app-icon-provider.cpp b/src/appdata/app-icon-provider.cpp new file mode 100644 index 0000000..a2b068a --- /dev/null +++ b/src/appdata/app-icon-provider.cpp @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2022, KylinSoft Co., Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "app-icon-provider.h" + +#include +#include +#include + +namespace UkuiMenu { + +QSize AppIconProvider::s_defaultSize = QSize(128, 128); + +AppIconProvider::AppIconProvider() : QQuickImageProvider(QQmlImageProviderBase::Pixmap) +{ + +} + +QPixmap AppIconProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) +{ + // 不处理sourceSize请求 + Q_UNUSED(requestedSize); + QPixmap pixmap; + loadPixmap(id, pixmap); + + if (size) { + QSize pixmapSize = pixmap.size(); + size->setWidth(pixmapSize.width()); + size->setHeight(pixmapSize.height()); + } + + return pixmap; +} + +void AppIconProvider::loadPixmap(const QString &id, QPixmap &pixmap) +{ + if (id.isEmpty()) { + loadDefault(pixmap); + return; + } + + bool isOk; + QUrl url(id); + if (!url.scheme().isEmpty()) { + isOk = loadPixmapFromUrl(url, pixmap); + + } else if (id.startsWith(QLatin1String("/")) || id.startsWith(QLatin1String(":/"))) { + //qrc path: :/xxx/xxx.png + isOk = loadPixmapFromPath(id, pixmap); + + } else { + isOk = loadPixmapFromTheme(id, pixmap); + } + + if (!isOk) { + loadDefault(pixmap); + } +} + +// see: https://doc.qt.io/archives/qt-5.12/qurl.html#details +bool AppIconProvider::loadPixmapFromUrl(const QUrl &url, QPixmap &pixmap) +{ + QString path = url.path(); + if (path.isEmpty()) { + qWarning() << "Error: loadPixmapFromUrl, path is empty."; + return false; + } + + // qrc example: the Path ":/images/cut.png" or the URL "qrc:///images/cut.png" + // see: https://doc.qt.io/archives/qt-5.12/resources.html + if (url.scheme() == QLatin1String("qrc")) { + path.prepend(QLatin1String(":")); + return loadPixmapFromPath(path, pixmap); + } + + // net? + return false; +} + +bool AppIconProvider::loadPixmapFromPath(const QString &path, QPixmap &pixmap) +{ + if (!QFile::exists(path)) { + qWarning() << "Error: loadPixmapFromPath, File dose not exists." << path; + return false; + } + + QPixmap filePixmap; + bool isOk = filePixmap.load(path); + if (!isOk) { + return false; + } + + pixmap.swap(filePixmap); + + return true; +} + +bool AppIconProvider::loadPixmapFromTheme(const QString &name, QPixmap &pixmap) +{ + if (!QIcon::hasThemeIcon(name)) { + qWarning() << "Error: loadPixmapFromTheme, icon dose not exists. name:" << name; + return false; + } + QIcon icon = QIcon::fromTheme(name); + pixmap = icon.pixmap(AppIconProvider::s_defaultSize); + return true; +} + +void AppIconProvider::loadDefault(QPixmap &pixmap) +{ + if (!loadPixmapFromTheme("application-x-desktop", pixmap)) { + loadPixmapFromPath(":/res/icon/application-x-desktop.png", pixmap); + } +} + +} // UkuiMenu diff --git a/src/appdata/app-icon-provider.h b/src/appdata/app-icon-provider.h new file mode 100644 index 0000000..fa0c676 --- /dev/null +++ b/src/appdata/app-icon-provider.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2022, KylinSoft Co., Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef UKUI_MENU_APP_ICON_PROVIDER_H +#define UKUI_MENU_APP_ICON_PROVIDER_H + +#include +#include + +namespace UkuiMenu { + +// see: https://doc.qt.io/archives/qt-5.12/qquickimageprovider.html#details +class AppIconProvider : public QQuickImageProvider +{ +public: + AppIconProvider(); + QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override; + +private: + static void loadPixmap(const QString &id, QPixmap &pixmap); + static void loadDefault(QPixmap &pixmap); + static bool loadPixmapFromUrl(const QUrl &url, QPixmap &pixmap); + static bool loadPixmapFromPath(const QString &path, QPixmap &pixmap); + static bool loadPixmapFromTheme(const QString &name, QPixmap &pixmap); + +private: + static QSize s_defaultSize; +}; + +} // UkuiMenu + +#endif //UKUI_MENU_APP_ICON_PROVIDER_H diff --git a/src/ukui-menu-application.cpp b/src/ukui-menu-application.cpp index 0daac25..b6437b4 100644 --- a/src/ukui-menu-application.cpp +++ b/src/ukui-menu-application.cpp @@ -22,6 +22,7 @@ #include "model-manager.h" #include "color-helper.h" #include "theme-palette.h" +#include "app-icon-provider.h" #include #include @@ -67,6 +68,9 @@ void UkuiMenuApplication::initQmlEngine() m_applicationEngine->addImportPath("qrc:/qml"); + // icon provider + m_applicationEngine->addImageProvider("appicon", new AppIconProvider()); + m_applicationEngine->rootContext()->setContextProperty("colorHelper", ColorHelper::instance()); m_applicationEngine->rootContext()->setContextProperty("themePalette", ThemePalette::getInstance()); m_applicationEngine->rootContext()->setContextProperty("menuSetting", MenuSetting::instance());