From d74bad1b65c84e9171daa4c375f233658b8ed41e Mon Sep 17 00:00:00 2001 From: zhaominyong Date: Tue, 8 Mar 2022 17:06:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A6=E4=BE=A7=E8=BE=B9=E6=A0=8F=E5=92=8C?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E9=9D=A2=E6=9D=BF=E7=BB=9F=E4=B8=80=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kybackup/component/imageutil.cpp | 44 +++++++------- kybackup/component/imageutil.h | 6 +- kybackup/component/myiconbutton.cpp | 91 +++++++++++++++++++++++++---- kybackup/component/myiconbutton.h | 6 ++ kybackup/kybackup.pro | 4 +- 5 files changed, 114 insertions(+), 37 deletions(-) diff --git a/kybackup/component/imageutil.cpp b/kybackup/component/imageutil.cpp index ab51698..717651b 100755 --- a/kybackup/component/imageutil.cpp +++ b/kybackup/component/imageutil.cpp @@ -4,29 +4,29 @@ #include #include -const QPixmap ImageUtil::loadSvg(const QString &path, const QString& color, int size) -{ - int origSize = size; - const auto ratio = qApp->devicePixelRatio(); - if ( 2 == ratio) { - size += origSize; - } else if (3 == ratio) { - size += origSize; - } - QPixmap pixmap(size, size); - QSvgRenderer renderer(path); - pixmap.fill(Qt::transparent); +//const QPixmap ImageUtil::loadSvg(const QString &path, const QString& color, int size) +//{ +// int origSize = size; +// const auto ratio = qApp->devicePixelRatio(); +// if ( 2 == ratio) { +// size += origSize; +// } else if (3 == ratio) { +// size += origSize; +// } +// QPixmap pixmap(size, size); +// QSvgRenderer renderer(path); +// pixmap.fill(Qt::transparent); - QPainter painter; - painter.begin(&pixmap); - renderer.render(&painter); - painter.end(); +// QPainter painter; +// painter.begin(&pixmap); +// renderer.render(&painter); +// painter.end(); - pixmap.setDevicePixelRatio(ratio); - return drawSymbolicColoredPixmap(pixmap, color); -} +// pixmap.setDevicePixelRatio(ratio); +// return drawSymbolicColoredPixmap(pixmap, color); +//} -const QPixmap ImageUtil::loadSvgTheme(const QString &theme, const QString& color, int size) +const QPixmap ImageUtil::loadTheme(const QString &theme, const QString &defaultTheme, const QString& color, int size) { int origSize = size; const auto ratio = qApp->devicePixelRatio(); @@ -36,8 +36,8 @@ const QPixmap ImageUtil::loadSvgTheme(const QString &theme, const QString& color size += origSize; } - QIcon icon = QIcon::fromTheme(theme); - QPixmap pixmap = icon.pixmap(QSize(size, size)); + QIcon icon = QIcon::fromTheme(theme, QIcon(defaultTheme)); + QPixmap pixmap = icon.pixmap(icon.actualSize(QSize(size, size))); pixmap.setDevicePixelRatio(ratio); return drawSymbolicColoredPixmap(pixmap, color); diff --git a/kybackup/component/imageutil.h b/kybackup/component/imageutil.h index e4521c3..638baee 100755 --- a/kybackup/component/imageutil.h +++ b/kybackup/component/imageutil.h @@ -2,14 +2,14 @@ #define IMAGEUTIL_H #include -#include +// #include #include class ImageUtil { public: - static const QPixmap loadSvg(const QString &path, const QString &color, int size = 16); - static const QPixmap loadSvgTheme(const QString &theme, const QString &color, int size = 16); + // 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, 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 e4a53ad..9debaf2 100755 --- a/kybackup/component/myiconbutton.cpp +++ b/kybackup/component/myiconbutton.cpp @@ -2,9 +2,9 @@ #include #include #include -#include #include -// #include "imageutil.h" +#include +#include "imageutil.h" #include "../gsettingswrapper.h" #include "../globalbackupinfo.h" @@ -12,19 +12,28 @@ MyIconButton::MyIconButton(QWidget *parent) : QPushButton(parent) { this->setCheckable(false); + this->setProperty("useButtonPalette", true); + m_hoverColor = pluginBtnHoverColor(true); + m_clickColor = pluginBtnHoverColor(false); + if (!this->isChecked()) + this->setStyleSheet(QString("QPushButton:hover{background-color:%1;border-radius: 6px;}" + "QPushButton:pressed{background-color:%2;border-radius: 6px;}").arg(m_hoverColor).arg(m_clickColor)); + m_pixSize = 16; m_iconButton = new QPushButton(this); m_iconButton->setCheckable(true); m_iconButton->setFixedSize(QSize(24, 24)); m_iconButton->setFocusPolicy(Qt::NoFocus); m_iconButton->setFlat(true); + QString iconBtnQss = QString("QPushButton:checked{border: none;}" + "QPushButton:!checked{border: none;}"); + m_iconButton->setStyleSheet(iconBtnQss); m_textLabel = new MyLabel(this); QSizePolicy textLabelPolicy = m_textLabel->sizePolicy(); textLabelPolicy.setHorizontalPolicy(QSizePolicy::Fixed); textLabelPolicy.setVerticalPolicy(QSizePolicy::Fixed); m_textLabel->setSizePolicy(textLabelPolicy); - // m_textLabel->setScaledContents(true); QHBoxLayout *hLayout = new QHBoxLayout(); hLayout->setContentsMargins(8, 0, 0, 0); @@ -33,16 +42,43 @@ MyIconButton::MyIconButton(QWidget *parent) : hLayout->addStretch(); setLayout(hLayout); - connect(m_iconButton, &QPushButton::clicked, this, [=](){ - m_iconButton->setChecked(this->isChecked()); + connect(m_iconButton, &QPushButton::clicked, this, [=]() { this->click(); + m_iconButton->setChecked(this->isChecked()); }); + + connect(m_iconButton, &QPushButton::toggled, this, [=] (bool checked) { + if (checked || 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)); + }); + connect(this, &MyIconButton::toggled, this, [=](bool checked) { - this->changePalette(checked); + m_iconButton->setChecked(this->isChecked()); + if (checked) { + this->setStyleSheet("QPushButton:checked{background-color: palette(highlight);border-radius: 6px;}"); + m_textLabel->setStyleSheet("color:white"); + } else { + this->setStyleSheet(QString("QPushButton:hover{background-color:%1;border-radius: 6px;}" + "QPushButton:pressed{background-color:%2;border-radius: 6px;}").arg(m_hoverColor).arg(m_clickColor)); + m_textLabel->setStyleSheet("color:palette(windowText)"); + } }); connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, [=](bool isDark) { - this->changePalette(this->isChecked()); + Q_UNUSED(isDark) + // 深浅主题切换时,因为调色板已经更换,高亮等颜色已经改变,所以要重新加载图标。 + if (this->isChecked() || isDark) + 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)); + + m_hoverColor = pluginBtnHoverColor(true); + m_clickColor = pluginBtnHoverColor(false); + if (!this->isChecked()) + this->setStyleSheet(QString("QPushButton:hover{background-color:%1;border-radius: 6px;}" + "QPushButton:pressed{background-color:%2;border-radius: 6px;}").arg(m_hoverColor).arg(m_clickColor)); }); } @@ -53,9 +89,9 @@ void MyIconButton::setThemeIcon(const QString &themeIconName, const QString &def { m_themeIconName = themeIconName; m_defaultIconName = defaultIconName; + m_pixSize = size; - QIcon icon = QIcon::fromTheme(themeIconName, QIcon(defaultIconName)); - m_iconButton->setIcon(icon.pixmap(icon.actualSize(QSize(size, size)))); + m_iconButton->setIcon(ImageUtil::loadTheme(m_themeIconName, m_defaultIconName, "default", m_pixSize)); } void MyIconButton::setDesplayText(const QString &text) @@ -64,6 +100,9 @@ void MyIconButton::setDesplayText(const QString &text) m_textLabel->setDeplayText(text); } +/** + * 废弃,改为使用qss了 + */ void MyIconButton::changePalette(bool checked) { m_iconButton->setChecked(checked); @@ -85,4 +124,36 @@ void MyIconButton::changePalette(bool checked) m_textLabel->setPalette(pal); } - +QString MyIconButton::pluginBtnHoverColor(bool hoverFlag) +{ + QColor color1, color2; + if (this->parent()) { + QWidget * parent = qobject_cast(this->parent()); + color1 = parent->palette().color(QPalette::Active, QPalette::Button); + color2 = parent->palette().color(QPalette::Active, QPalette::BrightText); + } else { + color1 = palette().color(QPalette::Active, QPalette::Button); + color2 = palette().color(QPalette::Active, QPalette::BrightText); + } + QColor color; + qreal r,g,b,a; + QString hoverColor; + if ((g_GSettingWrapper.isDarkTheme() && hoverFlag) || + (!g_GSettingWrapper.isDarkTheme() && !hoverFlag)) { + r = color1.redF() * 0.8 + color2.redF() * 0.2; + g = color1.greenF() * 0.8 + color2.greenF() * 0.2; + b = color1.blueF() * 0.8 + color2.blueF() * 0.2; + a = color1.alphaF() * 0.8 + color2.alphaF() * 0.2; + } else { + r = color1.redF() * 0.95 + color2.redF() * 0.05; + g = color1.greenF() * 0.95 + color2.greenF() * 0.05; + b = color1.blueF() * 0.95 + color2.blueF() * 0.05; + a = color1.alphaF() * 0.95 + color2.alphaF() * 0.05; + } + color = QColor::fromRgbF(r, g, b, a); + hoverColor = QString("rgba(%1, %2, %3, %4)").arg(color.red()) + .arg(color.green()) + .arg(color.blue()) + .arg(color.alpha()); + return hoverColor; +} diff --git a/kybackup/component/myiconbutton.h b/kybackup/component/myiconbutton.h index e8ab589..9917a41 100755 --- a/kybackup/component/myiconbutton.h +++ b/kybackup/component/myiconbutton.h @@ -17,6 +17,8 @@ public: void setDesplayText(const QString &text); + QString pluginBtnHoverColor(bool hoverFlag); + public slots: void changePalette(bool checked); @@ -26,6 +28,10 @@ private: QString m_themeIconName; QString m_defaultIconName; QString m_originalText; + + QString m_hoverColor; + QString m_clickColor; + int m_pixSize; }; #endif // MYICONBUTTON_H diff --git a/kybackup/kybackup.pro b/kybackup/kybackup.pro index ae3df02..953c62e 100755 --- a/kybackup/kybackup.pro +++ b/kybackup/kybackup.pro @@ -42,7 +42,7 @@ HEADERS += \ component/circlelabel.h \ component/clicklabel.h \ component/hoverwidget.h \ -# component/imageutil.h \ + component/imageutil.h \ component/linelabel.h \ component/mycheckbox.h \ component/myfileselect.h \ @@ -88,7 +88,7 @@ SOURCES += \ component/circlelabel.cpp \ component/clicklabel.cpp \ component/hoverwidget.cpp \ -# component/imageutil.cpp \ + component/imageutil.cpp \ component/linelabel.cpp \ component/mycheckbox.cpp \ component/myfileselect.cpp \