图标主题切换优化

This commit is contained in:
zhaominyong 2022-03-30 16:43:48 +08:00
parent a2f0d5007a
commit 7b7ad2d13c
5 changed files with 45 additions and 9 deletions

View File

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

View File

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

View File

@ -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));
}
/**

View File

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

View File

@ -1,5 +1,6 @@
#include "pixmaplabel.h"
#include <QIcon>
#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));
}
}