Remove duplicate theme color code in Pages.cpp (#4503)

Refactored into GColor::getThemeColor for easier manteinance.
This commit is contained in:
Paul Johnson
2024-05-27 22:53:39 +01:00
committed by GitHub
parent 9256358756
commit 7684b4a39b
3 changed files with 118 additions and 210 deletions

View File

@@ -937,8 +937,121 @@ Themes::Themes()
}
// NOTE: this is duplicated in Pages.cpp:1407:ColorsPage::applyThemeClicked()
// you need to change there too. Sorry.
QColor GCColor::getThemeColor(const ColorTheme& theme, int colorIdx)
{
QColor color;
// apply theme to color
switch (colorIdx) {
case CPLOTBACKGROUND:
case CRIDEPLOTBACKGROUND:
case CTRENDPLOTBACKGROUND:
// background color
color = theme.colors[0];
break;
case CTRAINPLOTBACKGROUND:
// always, and I mean always default to a black background
color = QColor(Qt::black);
break;
case CCARDBACKGROUND:
// set back to light black for dark themes
// and gray for light themes
color = theme.colors[11];
break;
case CCARDBACKGROUND2:
// set back to light black for dark themes
// and gray for light themes
color = theme.colors[12];
break;
case CCARDBACKGROUND3:
// set back to light black for dark themes
// and gray for light themes
color = theme.colors[13];
break;
case COVERVIEWBACKGROUND:
// set back to light black for dark themes
// and gray for light themes
color = theme.colors[10];
break;
case CCHROME:
case CCHARTBAR:
case CTOOLBAR:
// we always keep them the same, but user can make different
// set to black for dark themes and grey for light themes
color = theme.colors[1];
break;
case CHOVER:
// stealthy themes use overview card background for hover color since they are close
// all other themes get a boring default
color = theme.stealth ? ColorList[96].color : (theme.dark ? QColor(50, 50, 50) : QColor(200, 200, 200));
break;
case CPLOTSYMBOL:
case CRIDEPLOTXAXIS:
case CRIDEPLOTYAXIS:
case CPLOTMARKER:
color = theme.colors[2]; // accent color
break;
case CPLOTSELECT:
case CPLOTTRACKER:
case CINTERVALHIGHLIGHTER:
color = theme.colors[3]; // select color
break;
case CPLOTGRID:
// grid doesn't have a theme color
// we make it barely distinguishable from background
{
QColor bg = theme.colors[0];
if (bg == QColor(Qt::black)) color = QColor(30, 30, 30);
else color = bg.darker(110);
}
break;
case CCP:
case CWBAL:
case CRIDECP:
color = theme.colors[4];
break;
case CHEARTRATE:
color = theme.colors[5];
break;
case CSPEED:
color = theme.colors[6];
break;
case CPOWER:
color = theme.colors[7];
break;
case CCADENCE:
color = theme.colors[8];
break;
case CTORQUE:
color = theme.colors[9];
break;
default:
if (theme.dark) color = DarkDefaultColorList[colorIdx].color;
else color = LightDefaultColorList[colorIdx].color;
}
return color;
}
void
GCColor::applyTheme(int index)
{
@@ -947,111 +1060,7 @@ GCColor::applyTheme(int index)
for (int i=0; ColorList[i].name != ""; i++) {
QColor color;
// apply theme to color
switch(i) {
case CPLOTBACKGROUND:
case CRIDEPLOTBACKGROUND:
case CTRENDPLOTBACKGROUND:
color = theme.colors[0]; // background color
break;
case CTRAINPLOTBACKGROUND:
// always, and I mean always default to a black background
color = QColor(Qt::black);
break;
case CCARDBACKGROUND:
// set back to light black for dark themes
// and gray for light themes
color = theme.colors[11];
break;
case CCARDBACKGROUND2:
// set back to light black for dark themes
// and gray for light themes
color = theme.colors[12];
break;
case CCARDBACKGROUND3:
// set back to light black for dark themes
// and gray for light themes
color = theme.colors[13];
break;
case COVERVIEWBACKGROUND:
color = theme.colors[10];
break;
case CCHROME:
case CCHARTBAR:
case CTOOLBAR: // we always keep them the same, but user can make different
color = theme.colors[1];
break;
case CHOVER:
// stealthy themes use overview card background for hover color since they are close
// all other themes get a boring default
color = theme.stealth ? ColorList[96].color : (theme.dark ? QColor(50,50,50) : QColor(200,200,200));
break;
case CPLOTSYMBOL:
case CRIDEPLOTXAXIS:
case CRIDEPLOTYAXIS:
case CPLOTMARKER:
color = theme.colors[2]; // accent color
break;
case CPLOTSELECT:
case CPLOTTRACKER:
case CINTERVALHIGHLIGHTER:
color = theme.colors[3]; // select color
break;
case CPLOTGRID: // grid doesn't have a theme color
// we make it barely distinguishable from background
{
QColor bg = theme.colors[0];
if(bg == QColor(Qt::black)) color = QColor(30,30,30);
else color = bg.darker(110);
}
break;
case CCP:
case CWBAL:
case CRIDECP:
color = theme.colors[4];
break;
case CHEARTRATE:
color = theme.colors[5];
break;
case CSPEED:
color = theme.colors[6];
break;
case CPOWER:
color = theme.colors[7];
break;
case CCADENCE:
color = theme.colors[8];
break;
case CTORQUE:
color = theme.colors[9];
break;
default:
if (theme.dark) color = DarkDefaultColorList[i].color;
else color = LightDefaultColorList[i].color;
}
QColor color = getThemeColor(theme, i);
// theme applied !
ColorList[i].color = color;

View File

@@ -132,6 +132,7 @@ class GCColor : public QObject
static QColor htmlCode(QColor x) { return x.name(); } // return the alternate background
static Themes &themes();
static void applyTheme(int index);
static QColor getThemeColor(const ColorTheme& theme, int colorIdx);
// for styling things with current preferences
static QLinearGradient linearGradient(int size, bool active, bool alternate=false);

View File

@@ -1633,110 +1633,8 @@ ColorsPage::applyThemeIndex(int index)
for (int i=0; colorSet[i].name != ""; i++) {
QColor color;
// apply theme to color
switch(i) {
case CPLOTBACKGROUND:
case CRIDEPLOTBACKGROUND:
case CTRENDPLOTBACKGROUND:
color = theme.colors[0]; // background color
break;
case CTRAINPLOTBACKGROUND:
// always, and I mean always default to a black background
color = QColor(Qt::black);
break;
case COVERVIEWBACKGROUND:
// set back to light black for dark themes
// and gray for light themes
color = theme.colors[10];
break;
case CCARDBACKGROUND:
// set back to light black for dark themes
// and gray for light themes
color = theme.colors[11];
break;
case CCARDBACKGROUND2:
// set back to light black for dark themes
// and gray for light themes
color = theme.colors[12];
break;
case CCARDBACKGROUND3:
// set back to light black for dark themes
// and gray for light themes
color = theme.colors[13];
break;
case CCHROME:
case CCHARTBAR:
case CTOOLBAR: // we always keep them the same, but user can make different
// set to black for dark themese and grey for light themes
color = theme.colors[1];
break;
case CHOVER:
color = theme.stealth ? theme.colors[11] : (theme.dark ? QColor(50,50,50) : QColor(200,200,200));
break;
case CPLOTSYMBOL:
case CRIDEPLOTXAXIS:
case CRIDEPLOTYAXIS:
case CPLOTMARKER:
color = theme.colors[2]; // accent color
break;
case CPLOTSELECT:
case CPLOTTRACKER:
case CINTERVALHIGHLIGHTER:
color = theme.colors[3]; // select color
break;
case CPLOTGRID: // grid doesn't have a theme color
// we make it barely distinguishable from background
{
QColor bg = theme.colors[0];
if(bg == QColor(Qt::black)) color = QColor(30,30,30);
else color = bg.darker(110);
}
break;
case CCP:
case CWBAL:
case CRIDECP:
color = theme.colors[4];
break;
case CHEARTRATE:
color = theme.colors[5];
break;
case CSPEED:
color = theme.colors[6];
break;
case CPOWER:
color = theme.colors[7];
break;
case CCADENCE:
color = theme.colors[8];
break;
case CTORQUE:
color = theme.colors[9];
break;
default:
color = colorSet[i].color;
}
QColor color = GCColor::getThemeColor(theme, i);
QTreeWidgetItem *add;
ColorButton *colorButton = new ColorButton(this, colorSet[i].name, color);