Skip to content

Commit f4c1580

Browse files
author
ThePBone
committed
Rewrite style helpers
1 parent 52783aa commit f4c1580

File tree

2 files changed

+83
-128
lines changed

2 files changed

+83
-128
lines changed

misc/stylehelper.cpp

Lines changed: 37 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -39,120 +39,30 @@ void StyleHelper::SetStyle(){
3939
f.open(QFile::ReadOnly | QFile::Text);
4040
QTextStream ts(&f);
4141
qApp->setStyleSheet(ts.readAll());
42-
if(style_sheet=="amoled" || style_sheet=="vsdark"){
43-
m_host->settings_dlg->updateButtonStyle(true);
44-
QPixmap pix(":/icons/settings-white.svg");
45-
QIcon icon(pix);
46-
QPixmap pix2(":/icons/queue-white.svg");
47-
QIcon icon2(pix2);
48-
QPixmap pix3(":/icons/menu-white.svg");
49-
QIcon icon3(pix3);
50-
m_host->ui->set->setIcon(icon);
51-
m_host->ui->cpreset->setIcon(icon2);
52-
m_host->ui->toolButton->setIcon(icon3);
53-
}else{
54-
m_host->settings_dlg->updateButtonStyle(false);
55-
QPixmap pix(":/icons/settings.svg");
56-
QIcon icon(pix);
57-
QPixmap pix2(":/icons/queue.svg");
58-
QIcon icon2(pix2);
59-
QPixmap pix3(":/icons/menu.svg");
60-
QIcon icon3(pix3);
61-
m_host->ui->set->setIcon(icon);
62-
m_host->ui->cpreset->setIcon(icon2);
63-
m_host->ui->toolButton->setIcon(icon3);
64-
}
42+
loadIcons(style_sheet=="amoled" || style_sheet=="vsdark");
6543
}
6644
}else{
67-
loadIcons(false);
68-
if(color_palette=="dark"){
69-
loadIcons(true);
70-
QColor background = QColor(53,53,53);
71-
QColor foreground = Qt::white;
72-
QColor base = QColor(25,25,25);
73-
QColor selection = QColor(42, 130, 218);
74-
setPalette(base,background,foreground,selection,Qt::black);
75-
}else if(color_palette=="gray"){
76-
loadIcons(true);
77-
QColor background = QColor(49,49,74);
78-
QColor foreground = Qt::white;
79-
QColor base = QColor(83,83,125);
80-
QColor selection = QColor(85,85,127);
81-
setPalette(base,background,foreground,selection,Qt::black,QColor(144,144,179));
82-
}else if(color_palette=="white"){
83-
QColor background = Qt::white;
84-
QColor foreground = Qt::black;
85-
QColor base = Qt::white;
86-
QColor selection = QColor(56,161,227);
87-
setPalette(base,background,foreground,selection,Qt::black);
88-
}
89-
else if(color_palette=="blue"){
90-
loadIcons(true);
91-
QColor background = QColor(0,0,50);
92-
QColor foreground = Qt::white;
93-
QColor base = QColor(0,0,38);
94-
QColor selection = QColor(85,0,255);
95-
setPalette(base,background,foreground,selection,Qt::black);
96-
}
97-
else if(color_palette=="darkblue"){
98-
loadIcons(true);
99-
QColor background = QColor(19,25,38);
100-
QColor foreground = Qt::white;
101-
QColor base = QColor(14,19,29);
102-
QColor selection = QColor(70,79,89);
103-
setPalette(base,background,foreground,selection,Qt::black);
104-
}
105-
else if(color_palette=="honeycomb"){
106-
QColor background = QColor(212,215,208);
107-
QColor foreground = Qt::black;
108-
QColor base = QColor(185,188,182);
109-
QColor selection = QColor(243,193,41);
110-
setPalette(base,background,foreground,selection,Qt::white);
111-
}
112-
else if(color_palette=="black"){
113-
loadIcons(true);
114-
QColor background = QColor(16,16,16);
115-
QColor foreground = QColor(222,222,222);
116-
QColor base = Qt::black;
117-
QColor selection = QColor(132,132,132);
118-
setPalette(base,background,foreground,selection,Qt::black);
119-
}
120-
else if(color_palette=="darkgreen"){
121-
loadIcons(true);
122-
QColor background = QColor(27,34,36);
123-
QColor foreground = QColor(197,209,217);
124-
QColor base = QColor(30,30,30);
125-
QColor selection = QColor(21,67,58);
126-
setPalette(base,background,foreground,selection,Qt::black);
127-
}
128-
else if(color_palette=="green"){
129-
loadIcons(true);
130-
QColor background = QColor(0,12,0);
131-
QColor foreground = Qt::white;
132-
QColor base = QColor(6,29,12);
133-
QColor selection = QColor(86,191,121);
134-
setPalette(base,background,foreground,selection,Qt::black);
135-
}
136-
else if(color_palette=="stone"){
137-
loadIcons(true);
138-
QColor background = QColor(34,45,50);
139-
QColor foreground = Qt::white;
140-
QColor base = QColor(27,36,40);
141-
QColor selection = QColor(165,206,255);
142-
setPalette(base,background,foreground,selection,Qt::black);
143-
}
144-
else if(color_palette=="custom"){
45+
auto palettes = ColorStyleProvider::TABLE();
46+
if(color_palette=="custom"){
14547
QColor base = QColor(loadColor(0,0),loadColor(0,1),loadColor(0,2));
14648
QColor background = QColor(loadColor(1,0),loadColor(1,1),loadColor(1,2));
14749
QColor foreground = QColor(loadColor(2,0),loadColor(2,1),loadColor(2,2));
14850
QColor selection = QColor(loadColor(3,0),loadColor(3,1),loadColor(3,2));
14951
QColor disabled = QColor(loadColor(4,0),loadColor(4,1),loadColor(4,2));
15052
QColor selectiontext = QColor(255-loadColor(3,0),255-loadColor(3,1),255-loadColor(3,2));
53+
ColorStyle cs = ColorStyle(m_appconf->getWhiteIcons(),
54+
base,background,foreground,selection,selectiontext,disabled);
15155

152-
setPalette(base,background,foreground,selection,selectiontext,disabled);
56+
setPalette(cs);
15357
loadIcons(m_appconf->getWhiteIcons());
15458
}
59+
else if(palettes.contains(color_palette)){
60+
ColorStyle currentColorStyle = palettes[color_palette];
61+
setPalette(currentColorStyle);
62+
loadIcons(currentColorStyle.useWhiteIcons);
63+
}
15564
else{
65+
loadIcons(false);
15666
QApplication::setPalette(m_host->style()->standardPalette());
15767
QFile f(":/default.qss");
15868
if (!f.exists())printf("Unable to set stylesheet, file not found\n");
@@ -167,24 +77,35 @@ void StyleHelper::SetStyle(){
16777
}
16878
emit styleChanged();
16979
}
170-
void StyleHelper::setPalette(const QColor& base,const QColor& background,const QColor& foreground,const QColor& selection,const QColor& selectiontext,const QColor& disabled){
80+
void StyleHelper::setPalette(const ColorStyle& s){
17181
QPalette *palette = new QPalette();
172-
palette->setColor(QPalette::Window, background);
173-
palette->setColor(QPalette::WindowText, foreground);
174-
palette->setColor(QPalette::Base, base);
175-
palette->setColor(QPalette::AlternateBase, background);
176-
palette->setColor(QPalette::ToolTipBase, background);
177-
palette->setColor(QPalette::ToolTipText, foreground);
178-
palette->setColor(QPalette::Text, foreground);
179-
palette->setColor(QPalette::Button, background);
180-
palette->setColor(QPalette::ButtonText, foreground);
82+
palette->setColor(QPalette::Window, s.background);
83+
palette->setColor(QPalette::WindowText, s.foreground);
84+
palette->setColor(QPalette::Base, s.base);
85+
palette->setColor(QPalette::AlternateBase, s.background);
86+
palette->setColor(QPalette::ToolTipBase, s.background);
87+
palette->setColor(QPalette::ToolTipText, s.foreground);
88+
palette->setColor(QPalette::Text, s.foreground);
89+
palette->setColor(QPalette::Button, s.background);
90+
palette->setColor(QPalette::ButtonText, s.foreground);
18191
palette->setColor(QPalette::BrightText, Qt::red);
18292
palette->setColor(QPalette::Link, QColor(42, 130, 218));
183-
palette->setColor(QPalette::Highlight, selection);
184-
palette->setColor(QPalette::HighlightedText, selectiontext);
93+
palette->setColor(QPalette::Highlight, s.selection);
94+
palette->setColor(QPalette::HighlightedText, s.selectiontext);
18595
qApp->setPalette(*palette);
186-
QString rgbdisabled = disabled.name();
187-
qApp->setStyleSheet(R"(QFrame[frameShape="4"], QFrame[frameShape="5"]{ color: gray; }*::disabled { color: )" + rgbdisabled +";}QToolButton::disabled { color: " + rgbdisabled +";}QComboBox::disabled { color: " + rgbdisabled +";}");
96+
qApp->setStyleSheet(QString(R"(QFrame[frameShape="4"], QFrame[frameShape="5"]{
97+
color: gray;
98+
}
99+
*::disabled {
100+
color: %1;
101+
}
102+
QToolButton::disabled{
103+
color: %1;
104+
105+
}
106+
QComboBox::disabled{
107+
color: %1;
108+
})").arg(s.disabled.name()));
188109
}
189110
void StyleHelper::loadIcons(bool white){
190111
MainWindow* m_host = qobject_cast<MainWindow*>(m_objhost);
@@ -230,11 +151,3 @@ int StyleHelper::loadColor(int index,int rgb_index){
230151
QStringList rgb = elements[index].split(',');
231152
return rgb[rgb_index].toInt();
232153
}
233-
void StyleHelper::switchPalette(const QPalette& palette)
234-
{
235-
MainWindow* m_host = qobject_cast<MainWindow*>(m_objhost);
236-
qApp->setPalette(palette);
237-
QList<QWidget*> widgets = m_host->findChildren<QWidget*>();
238-
foreach (QWidget* w, widgets)
239-
w->setPalette(palette);
240-
}

misc/stylehelper.h

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,65 @@
1616
#define STYLEHELPER_H
1717
#include <QObject>
1818
#include <QColor>
19+
#include "misc/initializableqmap.h"
20+
#define CS_UNIT QString,ColorStyle
1921

22+
class ColorStyle;
2023
class StyleHelper : public QObject
2124
{
2225
Q_OBJECT
2326
public:
27+
2428
StyleHelper(QObject* host);
2529
void SetStyle();
2630
void loadIcons(bool white);
2731
int loadColor(int index,int rgb_index);
28-
void switchPalette(const QPalette& palette);
29-
void setPalette(const QColor& base,const QColor& background,
30-
const QColor& foreground,const QColor& selection = QColor(42,130,218),
31-
const QColor& selectiontext = Qt::black,const QColor& disabled = QColor(85,85,85));
32+
void setPalette(const ColorStyle& s);
3233
private:
3334
QObject* m_objhost;
3435
signals:
3536
void styleChanged();
3637
};
3738

39+
class ColorStyle {
40+
public:
41+
bool useWhiteIcons;
42+
QColor background;
43+
QColor foreground;
44+
QColor base;
45+
QColor selection;
46+
QColor selectiontext;
47+
QColor disabled;
48+
49+
ColorStyle(){}
50+
ColorStyle(const bool _whiteIcons, const QColor& _base,const QColor& _background,
51+
const QColor& _foreground,const QColor& _selection = QColor(42,130,218),
52+
const QColor& _selectiontext = Qt::black,const QColor& _disabled = QColor(85,85,85)){
53+
useWhiteIcons = _whiteIcons;
54+
base = _base;
55+
background = _background;
56+
foreground = _foreground;
57+
selection = _selection;
58+
selectiontext = _selectiontext;
59+
disabled = _disabled;
60+
}
61+
};
62+
63+
64+
namespace ColorStyleProvider {
65+
static QMap<CS_UNIT> TABLE(){
66+
InitializableQMap<CS_UNIT> map;
67+
map << QPair<CS_UNIT>("dark",ColorStyle(true,QColor(25,25,25),QColor(53,53,53),Qt::white,QColor(42, 130, 218)))
68+
<< QPair<CS_UNIT>("gray",ColorStyle(true,QColor(83,83,125),QColor(49,49,74),Qt::white,QColor(85,85,127),QColor(144,144,179)))
69+
<< QPair<CS_UNIT>("white",ColorStyle(false,Qt::white,Qt::white,Qt::black,QColor(56,161,227)))
70+
<< QPair<CS_UNIT>("blue",ColorStyle(true,QColor(0,0,38),QColor(0,0,50),Qt::white,QColor(85,0,255)))
71+
<< QPair<CS_UNIT>("darkblue",ColorStyle(true,QColor(14,19,29),QColor(19,25,38),Qt::white,QColor(70,79,89)))
72+
<< QPair<CS_UNIT>("honeycomb",ColorStyle(false,QColor(185,188,182),QColor(212,215,208),Qt::black,QColor(243,193,41),Qt::white))
73+
<< QPair<CS_UNIT>("black",ColorStyle(true,Qt::black,QColor(16,16,16),QColor(222,222,222),QColor(132,132,132)))
74+
<< QPair<CS_UNIT>("darkgreen",ColorStyle(true,QColor(30,30,30),QColor(27,34,36),QColor(197,209,217),QColor(21,67,58)))
75+
<< QPair<CS_UNIT>("green",ColorStyle(true,QColor(6,29,12),QColor(0,12,0),Qt::white,QColor(86,191,121)))
76+
<< QPair<CS_UNIT>("stone",ColorStyle(true,QColor(27,36,40),QColor(34,45,50),Qt::white,QColor(165,206,255)));
77+
return std::move(map);
78+
}
79+
}
3880
#endif // STYLEHELPER_H

0 commit comments

Comments
 (0)