修改开始菜单图标模糊问题

This commit is contained in:
youdiansaodongxi 2023-07-19 11:46:05 +08:00
parent 57a1085466
commit 808d3d5813
5 changed files with 58 additions and 44 deletions

View File

@ -60,6 +60,8 @@ Item {
id: iconImage id: iconImage
height: root.iconHeight height: root.iconHeight
width: root.iconWidth width: root.iconWidth
sourceSize.height: root.iconHeight
sourceSize.width: root.iconWidth
source: root.appIcon source: root.appIcon
cache: false cache: false
} }

View File

@ -29,7 +29,6 @@ QSize AppIconProvider::s_defaultSize = QSize(128, 128);
AppIconProvider::AppIconProvider() : QQuickImageProvider(QQmlImageProviderBase::Pixmap) AppIconProvider::AppIconProvider() : QQuickImageProvider(QQmlImageProviderBase::Pixmap)
{ {
} }
QPixmap AppIconProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) 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) QPixmap AppIconProvider::getPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{ {
// 不处理sourceSize请求 QIcon icon;
Q_UNUSED(requestedSize); loadIcon(id, icon);
QPixmap pixmap;
loadPixmap(id, pixmap); QPixmap pixmap = icon.pixmap(requestedSize.isEmpty() ? s_defaultSize : requestedSize);
if (size) { if (size) {
QSize pixmapSize = pixmap.size(); QSize pixmapSize = pixmap.size();
@ -53,40 +52,40 @@ QPixmap AppIconProvider::getPixmap(const QString &id, QSize *size, const QSize &
return pixmap; return pixmap;
} }
void AppIconProvider::loadPixmap(const QString &id, QPixmap &pixmap) void AppIconProvider::loadIcon(const QString &id, QIcon &icon)
{ {
if (id.isEmpty()) { if (id.isEmpty()) {
loadDefault(pixmap); loadDefault(icon);
return; return;
} }
bool isOk; bool isOk;
QUrl url(id); QUrl url(id);
if (!url.scheme().isEmpty()) { if (!url.scheme().isEmpty()) {
isOk = loadPixmapFromUrl(url, pixmap); isOk = loadIconFromUrl(url, icon);
} else if (id.startsWith(QLatin1String("/")) || id.startsWith(QLatin1String(":/"))) { } else if (id.startsWith(QLatin1String("/")) || id.startsWith(QLatin1String(":/"))) {
//qrc path: :/xxx/xxx.png //qrc path: :/xxx/xxx.png
isOk = loadPixmapFromPath(id, pixmap); isOk = loadIconFromPath(id, icon);
} else { } else {
isOk = loadPixmapFromTheme(id, pixmap); isOk = loadIconFromTheme(id, icon);
if (!isOk) { if (!isOk) {
isOk = loadPixmapFromXdg(id, pixmap); isOk = loadIconFromXdg(id, icon);
} }
} }
if (!isOk) { if (!isOk) {
loadDefault(pixmap); loadDefault(icon);
} }
} }
// see: https://doc.qt.io/archives/qt-5.12/qurl.html#details // 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(); QString path = url.path();
if (path.isEmpty()) { if (path.isEmpty()) {
qWarning() << "Error: loadPixmapFromUrl, path is empty."; qWarning() << "Error: loadIconFromUrl, path is empty.";
return false; 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 // see: https://doc.qt.io/archives/qt-5.12/resources.html
if (url.scheme() == QLatin1String("qrc")) { if (url.scheme() == QLatin1String("qrc")) {
path.prepend(QLatin1String(":")); path.prepend(QLatin1String(":"));
return loadPixmapFromPath(path, pixmap); return loadIconFromPath(path, icon);
} }
// net? // net?
return false; return false;
} }
bool AppIconProvider::loadPixmapFromPath(const QString &path, QPixmap &pixmap) bool AppIconProvider::loadIconFromPath(const QString &path, QIcon &icon)
{ {
if (!QFile::exists(path)) { if (!QFile::exists(path)) {
qWarning() << "Error: loadPixmapFromPath, File dose not exists." << path; qWarning() << "Error: loadIconFromPath, File dose not exists." << path;
return false; return false;
} }
QPixmap filePixmap; QPixmap filePixmap;
bool isOk = filePixmap.load(path); bool isOk = filePixmap.load(path);
if (!isOk) { if (!isOk) {
return false; return false;
} }
icon.addPixmap(filePixmap);
pixmap.swap(filePixmap);
return true; return true;
} }
bool AppIconProvider::loadPixmapFromTheme(const QString &name, QPixmap &pixmap) bool AppIconProvider::loadIconFromTheme(const QString &name, QIcon &icon)
{ {
if (!QIcon::hasThemeIcon(name)) { if (!QIcon::hasThemeIcon(name)) {
return false; return false;
} }
QIcon icon = QIcon::fromTheme(name); icon = QIcon::fromTheme(name);
pixmap = icon.pixmap(AppIconProvider::s_defaultSize);
return true; return true;
} }
void AppIconProvider::loadDefault(QPixmap &pixmap) void AppIconProvider::loadDefault(QIcon &icon)
{ {
if (!loadPixmapFromTheme("application-x-desktop", pixmap)) { if (!loadIconFromTheme("application-x-desktop", icon)) {
loadPixmapFromPath(":/res/icon/application-x-desktop.png", pixmap); 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()) { if (icon.isNull()) {
qWarning() << "Error: loadPixmapFromXdg, icon dose not exists. name:" << name; qWarning() << "Error: loadIconFromXdg, icon dose not exists. name:" << name;
return false; return false;
} }
pixmap = icon.pixmap(AppIconProvider::s_defaultSize);
return true; return true;
} }

View File

@ -21,6 +21,7 @@
#include <QSize> #include <QSize>
#include <QQuickImageProvider> #include <QQuickImageProvider>
#include <QIcon>
namespace UkuiMenu { namespace UkuiMenu {
@ -33,12 +34,12 @@ public:
static QPixmap getPixmap(const QString &id, QSize *size, const QSize &requestedSize); static QPixmap getPixmap(const QString &id, QSize *size, const QSize &requestedSize);
private: private:
static void loadPixmap(const QString &id, QPixmap &pixmap); static void loadIcon(const QString &id, QIcon &icon);
static void loadDefault(QPixmap &pixmap); static void loadDefault(QIcon &icon);
static bool loadPixmapFromUrl(const QUrl &url, QPixmap &pixmap); static bool loadIconFromUrl(const QUrl &url, QIcon &icon);
static bool loadPixmapFromPath(const QString &path, QPixmap &pixmap); static bool loadIconFromPath(const QString &path, QIcon &icon);
static bool loadPixmapFromTheme(const QString &name, QPixmap &pixmap); static bool loadIconFromTheme(const QString &name, QIcon &icon);
static bool loadPixmapFromXdg(const QString &name, QPixmap &pixmap); static bool loadIconFromXdg(const QString &name, QIcon &icon);
private: private:
static QSize s_defaultSize; static QSize s_defaultSize;

View File

@ -44,6 +44,8 @@ ThemeIcon::ThemeIcon(QQuickItem *parent) : QQuickPaintedItem(parent)
checkThemeName(); checkThemeName();
} }
}); });
connect(this, &ThemeIcon::widthChanged, this, &ThemeIcon::onSizeChanged);
connect(this, &ThemeIcon::heightChanged, this, &ThemeIcon::onSizeChanged);
} }
void ThemeIcon::checkThemeName() void ThemeIcon::checkThemeName()
@ -55,6 +57,16 @@ void ThemeIcon::checkThemeName()
update(); 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) void ThemeIcon::paint(QPainter *painter)
{ {
//默认居中绘制 //默认居中绘制
@ -107,13 +119,7 @@ void ThemeIcon::setSource(const QString &source)
if (m_source.startsWith(prefix)) { if (m_source.startsWith(prefix)) {
m_source = m_source.mid(prefix.size()); m_source = m_source.mid(prefix.size());
} }
loadPixmapBySource(m_source);
QSize size;
m_pixmap = UkuiMenu::AppIconProvider::getPixmap(m_source, &size, size);
if (m_pixmap.isNull()) {
return;
}
m_isPureColor = isPixmapPureColor(m_pixmap);
checkThemeName(); checkThemeName();
update(); update();
} }
@ -223,6 +229,14 @@ bool ThemeIcon::isPixmapPureColor(const QPixmap &pixmap)
return arithmeticSquareRoot_red < 2.0 && arithmeticSquareRoot_green < 2.0 && arithmeticSquareRoot_blue < 2.0; 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() int ThemeIcon::radius()
{ {
return m_radius; return m_radius;

View File

@ -64,8 +64,11 @@ public:
private: private:
void checkThemeName(); void checkThemeName();
void loadPixmapBySource(const QString &source);
static bool isPixmapPureColor(const QPixmap &pixmap); static bool isPixmapPureColor(const QPixmap &pixmap);
private Q_SLOTS:
void onSizeChanged();
private: private:
bool m_disabled = false; bool m_disabled = false;
bool m_highLight = false; bool m_highLight = false;