summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/tab-widgets
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2020-11-04 13:09:44 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2020-11-04 14:47:10 -0800
commit6201ac34b412eb57e0ca03401da22ed9ae683c38 (patch)
tree5d41d1f3b69021918e1c74cef27f0cdd55e27c0f /desktop-widgets/tab-widgets
parent4c6b00f812757508118deee286886243485b3142 (diff)
downloadsubsurface-6201ac34b412eb57e0ca03401da22ed9ae683c38.tar.gz
desktop: automatically pick a good theme title color
The preference setting seemed far too strange to do this. And not very user friendly. So instead we figure out if this is a dark theme or not by looking at text and background colors in the palette, and make sure we get notified if that changes. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'desktop-widgets/tab-widgets')
-rw-r--r--desktop-widgets/tab-widgets/TabBase.cpp3
-rw-r--r--desktop-widgets/tab-widgets/TabBase.h2
-rw-r--r--desktop-widgets/tab-widgets/TabDiveInformation.cpp8
-rw-r--r--desktop-widgets/tab-widgets/TabDiveInformation.h2
-rw-r--r--desktop-widgets/tab-widgets/maintab.cpp30
-rw-r--r--desktop-widgets/tab-widgets/maintab.h2
6 files changed, 33 insertions, 14 deletions
diff --git a/desktop-widgets/tab-widgets/TabBase.cpp b/desktop-widgets/tab-widgets/TabBase.cpp
index 602480253..fa0a88f06 100644
--- a/desktop-widgets/tab-widgets/TabBase.cpp
+++ b/desktop-widgets/tab-widgets/TabBase.cpp
@@ -5,6 +5,7 @@ TabBase::TabBase(QWidget *parent) : QWidget(parent)
{
}
-void TabBase::updateUi()
+void TabBase::updateUi(QString titleColor)
{
+ Q_UNUSED(titleColor)
}
diff --git a/desktop-widgets/tab-widgets/TabBase.h b/desktop-widgets/tab-widgets/TabBase.h
index 3f8d9bf85..928de633f 100644
--- a/desktop-widgets/tab-widgets/TabBase.h
+++ b/desktop-widgets/tab-widgets/TabBase.h
@@ -13,7 +13,7 @@ public:
TabBase(QWidget *parent = 0);
virtual void updateData() = 0;
virtual void clear() = 0;
- virtual void updateUi();
+ virtual void updateUi(QString titleColor);
};
#endif
diff --git a/desktop-widgets/tab-widgets/TabDiveInformation.cpp b/desktop-widgets/tab-widgets/TabDiveInformation.cpp
index b9b504403..63d7fdd55 100644
--- a/desktop-widgets/tab-widgets/TabDiveInformation.cpp
+++ b/desktop-widgets/tab-widgets/TabDiveInformation.cpp
@@ -243,14 +243,10 @@ void TabDiveInformation::updateData()
showCurrentWidget(false, 0); // Show current star widget at lefthand side
}
-void TabDiveInformation::updateUi()
+void TabDiveInformation::updateUi(QString titleColor)
{
- // Put together appropriate CSS stylesheets: NB: colors below in same order as the enum in prefs.h
- QStringList colors = { "mediumblue", "lightblue", "black" }; // If using dark theme, set color appropriately
- QString colorText = colors[prefs.headerstyle_color];
-
QString CSSSetSmallLabel = "QLabel:enabled { color: ";
- CSSSetSmallLabel.append(colorText + "; font-size: ");
+ CSSSetSmallLabel.append(titleColor + "; font-size: ");
CSSSetSmallLabel.append(QString::number((int)(0.5 + ui->diveHeadingLabel->geometry().height() * 0.66)) + "px;}");
ui->groupBox_visibility->setStyleSheet(ui->groupBox_visibility->styleSheet() + CSSSetSmallLabel);
ui->groupBox_current->setStyleSheet(ui->groupBox_current->styleSheet() + CSSSetSmallLabel);
diff --git a/desktop-widgets/tab-widgets/TabDiveInformation.h b/desktop-widgets/tab-widgets/TabDiveInformation.h
index 2beaa98ac..d8dda7c07 100644
--- a/desktop-widgets/tab-widgets/TabDiveInformation.h
+++ b/desktop-widgets/tab-widgets/TabDiveInformation.h
@@ -16,7 +16,7 @@ public:
~TabDiveInformation();
void updateData() override;
void clear() override;
- void updateUi() override;
+ void updateUi(QString titleColor) override;
private slots:
void divesChanged(const QVector<dive *> &dives, DiveField field);
void cylinderChanged(dive *d);
diff --git a/desktop-widgets/tab-widgets/maintab.cpp b/desktop-widgets/tab-widgets/maintab.cpp
index ab4b4f28b..6e794f07d 100644
--- a/desktop-widgets/tab-widgets/maintab.cpp
+++ b/desktop-widgets/tab-widgets/maintab.cpp
@@ -47,6 +47,12 @@ struct Completers {
QCompleter *tags;
};
+static bool paletteIsDark(const QPalette &p)
+{
+ // we consider a palette dark if the text color is lighter than the windows background
+ return p.window().color().valueF() < p.windowText().color().valueF();
+}
+
MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
editMode(false),
ignoreInput(false),
@@ -72,6 +78,9 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
extraWidgets << new TabDiveComputer(this);
ui.tabWidget->addTab(extraWidgets.last(), tr("Device names"));
+ // make sure we know if this is a light or dark mode
+ isDark = paletteIsDark(palette());
+
// call colorsChanged() for the initial setup now that the extraWidgets are loaded
colorsChanged();
@@ -704,13 +713,24 @@ void MainTab::clearTabs()
widget->clear();
}
+void MainTab::changeEvent(QEvent *ev)
+{
+ if (ev->type() == QEvent::PaletteChange) {
+ // check if this is a light or dark mode
+ bool dark = paletteIsDark(palette());
+ if (dark != isDark) {
+ // things have changed, so setup the colors correctly
+ isDark = dark;
+ colorsChanged();
+ }
+ }
+ QTabWidget::changeEvent(ev);
+}
+
// setup the colors of 'header' elements in the tab widget
void MainTab::colorsChanged()
{
- // Put together appropriate CSS stylesheets: NB: colors below in same order as the enum in prefs.h
- QStringList colors = { "mediumblue", "lightblue", "black" }; // If using dark theme, set color appropriately
- QString colorText = colors[prefs.headerstyle_color];
-
+ QString colorText = isDark ? QStringLiteral("lightblue") : QStringLiteral("mediumblue");
QString lastpart = colorText + " ;}";
// only set the color if the widget is enabled
@@ -731,5 +751,5 @@ void MainTab::colorsChanged()
// finally call the individual updateUi() functions so they can overwrite these style sheets
for (TabBase *widget: extraWidgets)
- widget->updateUi();
+ widget->updateUi(colorText);
}
diff --git a/desktop-widgets/tab-widgets/maintab.h b/desktop-widgets/tab-widgets/maintab.h
index ea7240d08..5ec4e6096 100644
--- a/desktop-widgets/tab-widgets/maintab.h
+++ b/desktop-widgets/tab-widgets/maintab.h
@@ -78,6 +78,8 @@ private:
dive_trip *currentTrip;
QList<TabBase*> extraWidgets;
void divesEdited(int num); // Opens a warning window if more than one dive was edited
+ void changeEvent(QEvent *ev) override;
+ bool isDark;
};
#endif // MAINTAB_H