diff --git a/qml/AppControls2/IconLabel.qml b/qml/AppControls2/IconLabel.qml index 9c2b42b..f3ffe53 100644 --- a/qml/AppControls2/IconLabel.qml +++ b/qml/AppControls2/IconLabel.qml @@ -60,6 +60,8 @@ Item { id: iconImage height: root.iconHeight width: root.iconWidth + sourceSize.height: root.iconHeight + sourceSize.width: root.iconWidth source: root.appIcon cache: false } diff --git a/src/appdata/app-icon-provider.cpp b/src/appdata/app-icon-provider.cpp index 12c1f69..e270f21 100644 --- a/src/appdata/app-icon-provider.cpp +++ b/src/appdata/app-icon-provider.cpp @@ -29,7 +29,6 @@ QSize AppIconProvider::s_defaultSize = QSize(128, 128); AppIconProvider::AppIconProvider() : QQuickImageProvider(QQmlImageProviderBase::Pixmap) { - } QPixmap AppIconProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) @@ -39,10 +38,10 @@ QPixmap AppIconProvider::requestPixmap(const QString &id, QSize *size, const QSi QPixmap AppIconProvider::getPixmap(const QString &id, QSize *size, const QSize &requestedSize) { - // 不处理sourceSize请求 - Q_UNUSED(requestedSize); - QPixmap pixmap; - loadPixmap(id, pixmap); + QIcon icon; + loadIcon(id, icon); + + QPixmap pixmap = icon.pixmap(requestedSize.isEmpty() ? s_defaultSize : requestedSize); if (size) { QSize pixmapSize = pixmap.size(); @@ -53,40 +52,40 @@ QPixmap AppIconProvider::getPixmap(const QString &id, QSize *size, const QSize & return pixmap; } -void AppIconProvider::loadPixmap(const QString &id, QPixmap &pixmap) +void AppIconProvider::loadIcon(const QString &id, QIcon &icon) { if (id.isEmpty()) { - loadDefault(pixmap); + loadDefault(icon); return; } bool isOk; QUrl url(id); if (!url.scheme().isEmpty()) { - isOk = loadPixmapFromUrl(url, pixmap); + isOk = loadIconFromUrl(url, icon); } else if (id.startsWith(QLatin1String("/")) || id.startsWith(QLatin1String(":/"))) { //qrc path: :/xxx/xxx.png - isOk = loadPixmapFromPath(id, pixmap); + isOk = loadIconFromPath(id, icon); } else { - isOk = loadPixmapFromTheme(id, pixmap); + isOk = loadIconFromTheme(id, icon); if (!isOk) { - isOk = loadPixmapFromXdg(id, pixmap); + isOk = loadIconFromXdg(id, icon); } } if (!isOk) { - loadDefault(pixmap); + loadDefault(icon); } } // see: https://doc.qt.io/archives/qt-5.12/qurl.html#details -bool AppIconProvider::loadPixmapFromUrl(const QUrl &url, QPixmap &pixmap) +bool AppIconProvider::loadIconFromUrl(const QUrl &url, QIcon &icon) { QString path = url.path(); if (path.isEmpty()) { - qWarning() << "Error: loadPixmapFromUrl, path is empty."; + qWarning() << "Error: loadIconFromUrl, path is empty."; return false; } @@ -94,57 +93,52 @@ bool AppIconProvider::loadPixmapFromUrl(const QUrl &url, QPixmap &pixmap) // see: https://doc.qt.io/archives/qt-5.12/resources.html if (url.scheme() == QLatin1String("qrc")) { path.prepend(QLatin1String(":")); - return loadPixmapFromPath(path, pixmap); + return loadIconFromPath(path, icon); } // net? return false; } -bool AppIconProvider::loadPixmapFromPath(const QString &path, QPixmap &pixmap) +bool AppIconProvider::loadIconFromPath(const QString &path, QIcon &icon) { if (!QFile::exists(path)) { - qWarning() << "Error: loadPixmapFromPath, File dose not exists." << path; + qWarning() << "Error: loadIconFromPath, File dose not exists." << path; return false; } - QPixmap filePixmap; bool isOk = filePixmap.load(path); if (!isOk) { return false; } - - pixmap.swap(filePixmap); + icon.addPixmap(filePixmap); return true; } -bool AppIconProvider::loadPixmapFromTheme(const QString &name, QPixmap &pixmap) +bool AppIconProvider::loadIconFromTheme(const QString &name, QIcon &icon) { if (!QIcon::hasThemeIcon(name)) { return false; } - QIcon icon = QIcon::fromTheme(name); - pixmap = icon.pixmap(AppIconProvider::s_defaultSize); + icon = QIcon::fromTheme(name); return true; } -void AppIconProvider::loadDefault(QPixmap &pixmap) +void AppIconProvider::loadDefault(QIcon &icon) { - if (!loadPixmapFromTheme("application-x-desktop", pixmap)) { - loadPixmapFromPath(":/res/icon/application-x-desktop.png", pixmap); + if (!loadIconFromTheme("application-x-desktop", icon)) { + loadIconFromPath(":/res/icon/application-x-desktop.png", icon); } } -bool AppIconProvider::loadPixmapFromXdg(const QString &name, QPixmap &pixmap) +bool AppIconProvider::loadIconFromXdg(const QString &name, QIcon &icon) { - QIcon icon = XdgIcon::fromTheme(name); + icon = XdgIcon::fromTheme(name); if (icon.isNull()) { - qWarning() << "Error: loadPixmapFromXdg, icon dose not exists. name:" << name; + qWarning() << "Error: loadIconFromXdg, icon dose not exists. name:" << name; return false; } - - pixmap = icon.pixmap(AppIconProvider::s_defaultSize); return true; } diff --git a/src/appdata/app-icon-provider.h b/src/appdata/app-icon-provider.h index dafd587..0576c29 100644 --- a/src/appdata/app-icon-provider.h +++ b/src/appdata/app-icon-provider.h @@ -21,6 +21,7 @@ #include #include +#include namespace UkuiMenu { @@ -33,12 +34,12 @@ public: static QPixmap getPixmap(const QString &id, QSize *size, const QSize &requestedSize); 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); - static bool loadPixmapFromXdg(const QString &name, QPixmap &pixmap); + static void loadIcon(const QString &id, QIcon &icon); + static void loadDefault(QIcon &icon); + static bool loadIconFromUrl(const QUrl &url, QIcon &icon); + static bool loadIconFromPath(const QString &path, QIcon &icon); + static bool loadIconFromTheme(const QString &name, QIcon &icon); + static bool loadIconFromXdg(const QString &name, QIcon &icon); private: static QSize s_defaultSize; diff --git a/src/items/theme-icon.cpp b/src/items/theme-icon.cpp index 0de8703..91acf09 100644 --- a/src/items/theme-icon.cpp +++ b/src/items/theme-icon.cpp @@ -44,6 +44,8 @@ ThemeIcon::ThemeIcon(QQuickItem *parent) : QQuickPaintedItem(parent) checkThemeName(); } }); + connect(this, &ThemeIcon::widthChanged, this, &ThemeIcon::onSizeChanged); + connect(this, &ThemeIcon::heightChanged, this, &ThemeIcon::onSizeChanged); } void ThemeIcon::checkThemeName() @@ -55,6 +57,16 @@ void ThemeIcon::checkThemeName() update(); } +void ThemeIcon::loadPixmapBySource(const QString &source) +{ + QSize size; + m_pixmap = UkuiMenu::AppIconProvider::getPixmap(source, &size, QSize(width(), height())); + if (m_pixmap.isNull()) { + return; + } + m_isPureColor = isPixmapPureColor(m_pixmap); +} + void ThemeIcon::paint(QPainter *painter) { //默认居中绘制 @@ -107,13 +119,7 @@ void ThemeIcon::setSource(const QString &source) if (m_source.startsWith(prefix)) { m_source = m_source.mid(prefix.size()); } - - QSize size; - m_pixmap = UkuiMenu::AppIconProvider::getPixmap(m_source, &size, size); - if (m_pixmap.isNull()) { - return; - } - m_isPureColor = isPixmapPureColor(m_pixmap); + loadPixmapBySource(m_source); checkThemeName(); update(); } @@ -223,6 +229,14 @@ bool ThemeIcon::isPixmapPureColor(const QPixmap &pixmap) return arithmeticSquareRoot_red < 2.0 && arithmeticSquareRoot_green < 2.0 && arithmeticSquareRoot_blue < 2.0; } +void ThemeIcon::onSizeChanged() +{ + if ((width() != 0) && (height() != 0) && (!m_source.isEmpty())) { + loadPixmapBySource(m_source); + } + update(); +} + int ThemeIcon::radius() { return m_radius; diff --git a/src/items/theme-icon.h b/src/items/theme-icon.h index e1d68ea..42ca4d0 100644 --- a/src/items/theme-icon.h +++ b/src/items/theme-icon.h @@ -64,8 +64,11 @@ public: private: void checkThemeName(); + void loadPixmapBySource(const QString &source); static bool isPixmapPureColor(const QPixmap &pixmap); +private Q_SLOTS: + void onSizeChanged(); private: bool m_disabled = false; bool m_highLight = false;