From 7b7ad2d13ca566acff43fe4fac16c494607faab0 Mon Sep 17 00:00:00 2001 From: zhaominyong Date: Wed, 30 Mar 2022 16:43:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E6=A0=87=E4=B8=BB=E9=A2=98=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kybackup/component/imageutil.cpp | 35 +++++++++++++++++++++++++---- kybackup/component/imageutil.h | 2 +- kybackup/component/myiconbutton.cpp | 5 ++++- kybackup/component/pixmapbutton.cpp | 5 ++++- kybackup/component/pixmaplabel.cpp | 7 ++++-- 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/kybackup/component/imageutil.cpp b/kybackup/component/imageutil.cpp index 717651b..b1fdf51 100755 --- a/kybackup/component/imageutil.cpp +++ b/kybackup/component/imageutil.cpp @@ -36,11 +36,38 @@ const QPixmap ImageUtil::loadTheme(const QString &theme, const QString &defaultT size += origSize; } - QIcon icon = QIcon::fromTheme(theme, QIcon(defaultTheme)); - QPixmap pixmap = icon.pixmap(icon.actualSize(QSize(size, size))); + QIcon icon = QIcon::fromTheme(theme); + // 在主题中未找到,则使用默认路径的图标 + if (icon.isNull()) { + QIcon iconDefault(defaultTheme); + QPixmap pixmap = iconDefault.pixmap(iconDefault.actualSize(QSize(size, size))); - pixmap.setDevicePixelRatio(ratio); - return drawSymbolicColoredPixmap(pixmap, color); + pixmap.setDevicePixelRatio(ratio); + return drawSymbolicColoredPixmap(pixmap, color); + } + + return icon.pixmap(icon.actualSize(QSize(size, size))); +} + +const QPixmap ImageUtil::loadTheme(const QString &theme, const QString &defaultTheme, const QString& color, QSize size) +{ + int origWidth = size.width(); + int origHeight = size.height(); + const auto ratio = qApp->devicePixelRatio(); + size.setWidth(origWidth * ratio); + size.setHeight(origHeight * ratio); + + QIcon icon = QIcon::fromTheme(theme); + // 在主题中未找到,则使用默认路径的图标 + if (icon.isNull()) { + QIcon iconDefault(defaultTheme); + QPixmap pixmap = iconDefault.pixmap(iconDefault.actualSize(size)); + + pixmap.setDevicePixelRatio(ratio); + return drawSymbolicColoredPixmap(pixmap, color); + } + + return icon.pixmap(icon.actualSize(size)); } const QPixmap ImageUtil::loadPixmap(QIcon &icon, const QString &color, int size) diff --git a/kybackup/component/imageutil.h b/kybackup/component/imageutil.h index 638baee..bd754ea 100755 --- a/kybackup/component/imageutil.h +++ b/kybackup/component/imageutil.h @@ -8,7 +8,7 @@ class ImageUtil { public: - // static const QPixmap loadSvg(const QString &path, const QString &color, int size = 16); + static const QPixmap loadTheme(const QString &theme, const QString &defaultTheme, const QString& color, QSize size); static const QPixmap loadTheme(const QString &theme, const QString &defaultTheme, const QString &color, int size = 16); static const QPixmap loadPixmap(QIcon &icon, const QString &color, int size = 16); static QPixmap drawSymbolicColoredPixmap(const QPixmap &source, const QString &cgColor); diff --git a/kybackup/component/myiconbutton.cpp b/kybackup/component/myiconbutton.cpp index 90f3709..13058ad 100755 --- a/kybackup/component/myiconbutton.cpp +++ b/kybackup/component/myiconbutton.cpp @@ -100,7 +100,10 @@ void MyIconButton::setThemeIcon(const QString &themeIconName, const QString &def m_defaultIconName = defaultIconName; m_pixSize = size; - m_iconButton->setIcon(ImageUtil::loadTheme(m_themeIconName, m_defaultIconName, "default", m_pixSize)); + if (g_GSettingWrapper.isDarkTheme()) + m_iconButton->setIcon(ImageUtil::loadTheme(m_themeIconName, m_defaultIconName, "white", m_pixSize)); + else + m_iconButton->setIcon(ImageUtil::loadTheme(m_themeIconName, m_defaultIconName, "default", m_pixSize)); } /** diff --git a/kybackup/component/pixmapbutton.cpp b/kybackup/component/pixmapbutton.cpp index 53e6c1a..9717501 100644 --- a/kybackup/component/pixmapbutton.cpp +++ b/kybackup/component/pixmapbutton.cpp @@ -37,7 +37,10 @@ void PixmapButton::setThemeIcon(const QString &themeIconName, const QString &def m_defaultIconName = defaultIconName; m_iconSize = size; - this->setIcon(ImageUtil::loadTheme(m_iconTheme, m_defaultIconName, "default", m_iconSize)); + if (g_GSettingWrapper.isDarkTheme()) + this->setIcon(ImageUtil::loadTheme(m_iconTheme, m_defaultIconName, "white", m_iconSize)); + else + this->setIcon(ImageUtil::loadTheme(m_iconTheme, m_defaultIconName, "default", m_iconSize)); } PixmapButton::~PixmapButton() diff --git a/kybackup/component/pixmaplabel.cpp b/kybackup/component/pixmaplabel.cpp index 2db0c1a..6d9576f 100644 --- a/kybackup/component/pixmaplabel.cpp +++ b/kybackup/component/pixmaplabel.cpp @@ -1,5 +1,6 @@ #include "pixmaplabel.h" #include +#include "imageutil.h" #include "../globalsignals.h" #include "../globalbackupinfo.h" #include "../gsettingswrapper.h" @@ -66,8 +67,10 @@ void PixmapLabel::on_themeIconChanged() QIcon titleIcon = QIcon::fromTheme(m_iconTheme); setPixmap(titleIcon.pixmap(titleIcon.actualSize(m_iconSize))); } else { - QIcon titleIcon = QIcon::fromTheme(m_iconTheme, QIcon(m_iconThemeDefault)); - setPixmap(titleIcon.pixmap(titleIcon.actualSize(m_iconSize))); + if (g_GSettingWrapper.isDarkTheme()) + setPixmap(ImageUtil::loadTheme(m_iconTheme, m_iconThemeDefault, "white", m_iconSize)); + else + setPixmap(ImageUtil::loadTheme(m_iconTheme, m_iconThemeDefault, "default", m_iconSize)); } }