左侧边栏和控制面板统一样式

This commit is contained in:
zhaominyong 2022-03-08 17:06:02 +08:00
parent 2e1dc931b3
commit d74bad1b65
5 changed files with 114 additions and 37 deletions

View File

@ -4,29 +4,29 @@
#include <QApplication>
#include <QIcon>
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);

View File

@ -2,14 +2,14 @@
#define IMAGEUTIL_H
#include <QPixmap>
#include <QSvgRenderer>
// #include <QSvgRenderer>
#include <QIcon>
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);
};

View File

@ -2,9 +2,9 @@
#include <QFontMetrics>
#include <QHBoxLayout>
#include <QStyleOptionButton>
#include <QPainter>
#include <QApplication>
// #include "imageutil.h"
#include <QPainter>
#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<QWidget *>(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;
}

View File

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

View File

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