新增appicon provider类,提供查询应用图标的功能

This commit is contained in:
hewenfei 2022-12-14 16:40:48 +08:00
parent c6b038a168
commit 23df5a944f
7 changed files with 184 additions and 1 deletions

View File

@ -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

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

@ -1,5 +1,5 @@
<RCC>
<qresource prefix="/res">
<file>icon.ico</file>
<file>icon/application-x-desktop.png</file>
</qresource>
</RCC>

View File

@ -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 <https://www.gnu.org/licenses/>.
*
*/
#include "app-icon-provider.h"
#include <QDebug>
#include <QIcon>
#include <QFile>
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

View File

@ -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 <https://www.gnu.org/licenses/>.
*
*/
#ifndef UKUI_MENU_APP_ICON_PROVIDER_H
#define UKUI_MENU_APP_ICON_PROVIDER_H
#include <QSize>
#include <QQuickImageProvider>
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

View File

@ -22,6 +22,7 @@
#include "model-manager.h"
#include "color-helper.h"
#include "theme-palette.h"
#include "app-icon-provider.h"
#include <QCoreApplication>
#include <QCommandLineParser>
@ -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());