From 23df5a944fc357bb42e5fdd22ebca337768cac89 Mon Sep 17 00:00:00 2001 From: hewenfei Date: Wed, 14 Dec 2022 16:40:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eappicon=20provider=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E4=BE=9B=E6=9F=A5=E8=AF=A2=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=9B=BE=E6=A0=87=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 1 + res/icon.ico | 0 res/icon/application-x-desktop.png | Bin 0 -> 5072 bytes res/res.qrc | 2 +- src/appdata/app-icon-provider.cpp | 131 +++++++++++++++++++++++++++++ src/appdata/app-icon-provider.h | 47 +++++++++++ src/ukui-menu-application.cpp | 4 + 7 files changed, 184 insertions(+), 1 deletion(-) delete mode 100644 res/icon.ico create mode 100644 res/icon/application-x-desktop.png create mode 100644 src/appdata/app-icon-provider.cpp create mode 100644 src/appdata/app-icon-provider.h 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 0000000000000000000000000000000000000000..2095ab014a13939ca218d105bac3176ed619aee3 GIT binary patch literal 5072 zcmV;>6EEzEP)o23wZq_ zi$akg2(c2l0s*htEM9p82v`@wTX_Ymz$+?GK_F4yTtFU{fJ6u&FHznkDkQhQ&h{`n zJFnfD-JRW;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MqlKzso_ zlh{zA1~fX1QhxkzG$N`OgD_rCo;;cT4J#iGBW%#9?LCK?H&CGCIu5Hxfff)87`%Js zyMXd_9g7?d(DVbu`gwA(4(WM$QaH=OHishsHishsHiwfZ(B^P7fX(3ufX(3ufX(69 z05*pn4Y1fbB_wLGSc`C7Cx-Oz-~W_zK2X%5XxXx530g$!J~PPWa^diSG`n#zO7T%% zUY_4!*>VW@nT700000 - 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());