cabana: fix dark theme detection to work with system theme (#35690)

fix dark theme detection to work with system theme
pull/35675/head^2
Dean Lee 5 days ago committed by GitHub
parent bd5586da55
commit be4e995d9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      tools/cabana/chart/chart.cc
  2. 2
      tools/cabana/chart/chartswidget.cc
  3. 3
      tools/cabana/chart/tiplabel.cc
  4. 2
      tools/cabana/historylog.cc
  5. 9
      tools/cabana/utils/util.cc
  6. 2
      tools/cabana/utils/util.h

@ -45,7 +45,7 @@ ChartView::ChartView(const std::pair<double, double> &x_range, ChartsWidget *par
createToolButtons(); createToolButtons();
setRubberBand(QChartView::HorizontalRubberBand); setRubberBand(QChartView::HorizontalRubberBand);
setMouseTracking(true); setMouseTracking(true);
setTheme(settings.theme == DARK_THEME ? QChart::QChart::ChartThemeDark : QChart::ChartThemeLight); setTheme(utils::isDarkTheme() ? QChart::QChart::ChartThemeDark : QChart::ChartThemeLight);
signal_value_font.setPointSize(9); signal_value_font.setPointSize(9);
QObject::connect(axis_y, &QValueAxis::rangeChanged, this, &ChartView::resetChartCache); QObject::connect(axis_y, &QValueAxis::rangeChanged, this, &ChartView::resetChartCache);
@ -747,7 +747,7 @@ void ChartView::drawTimeline(QPainter *painter) {
QRectF time_str_rect(QPointF(x - time_str_size.width() / 2.0, plot_area.bottom() + AXIS_X_TOP_MARGIN), time_str_size); QRectF time_str_rect(QPointF(x - time_str_size.width() / 2.0, plot_area.bottom() + AXIS_X_TOP_MARGIN), time_str_size);
QPainterPath path; QPainterPath path;
path.addRoundedRect(time_str_rect, 3, 3); path.addRoundedRect(time_str_rect, 3, 3);
painter->fillPath(path, settings.theme == DARK_THEME ? Qt::darkGray : Qt::gray); painter->fillPath(path, utils::isDarkTheme() ? Qt::darkGray : Qt::gray);
painter->setPen(palette().color(QPalette::BrightText)); painter->setPen(palette().color(QPalette::BrightText));
painter->setFont(axis_x->labelsFont()); painter->setFont(axis_x->labelsFont());
painter->drawText(time_str_rect, Qt::AlignCenter, time_str); painter->drawText(time_str_rect, Qt::AlignCenter, time_str);

@ -254,7 +254,7 @@ void ChartsWidget::settingChanged() {
if (std::exchange(current_theme, settings.theme) != current_theme) { if (std::exchange(current_theme, settings.theme) != current_theme) {
undo_zoom_action->setIcon(utils::icon("arrow-counterclockwise")); undo_zoom_action->setIcon(utils::icon("arrow-counterclockwise"));
redo_zoom_action->setIcon(utils::icon("arrow-clockwise")); redo_zoom_action->setIcon(utils::icon("arrow-clockwise"));
auto theme = settings.theme == DARK_THEME ? QChart::QChart::ChartThemeDark : QChart::ChartThemeLight; auto theme = utils::isDarkTheme() ? QChart::QChart::ChartThemeDark : QChart::ChartThemeLight;
for (auto c : charts) { for (auto c : charts) {
c->setTheme(theme); c->setTheme(theme);
} }

@ -7,6 +7,7 @@
#include <QToolTip> #include <QToolTip>
#include "tools/cabana/settings.h" #include "tools/cabana/settings.h"
#include "tools/cabana/utils/util.h"
TipLabel::TipLabel(QWidget *parent) : QLabel(parent, Qt::ToolTip | Qt::FramelessWindowHint) { TipLabel::TipLabel(QWidget *parent) : QLabel(parent, Qt::ToolTip | Qt::FramelessWindowHint) {
setAttribute(Qt::WA_ShowWithoutActivating); setAttribute(Qt::WA_ShowWithoutActivating);
@ -19,7 +20,7 @@ TipLabel::TipLabel(QWidget *parent) : QLabel(parent, Qt::ToolTip | Qt::Frameless
font.setPointSizeF(8.34563465); font.setPointSizeF(8.34563465);
setFont(font); setFont(font);
auto palette = QToolTip::palette(); auto palette = QToolTip::palette();
if (settings.theme != DARK_THEME) { if (!utils::isDarkTheme()) {
palette.setColor(QPalette::ToolTipBase, QApplication::palette().color(QPalette::Base)); palette.setColor(QPalette::ToolTipBase, QApplication::palette().color(QPalette::Base));
palette.setColor(QPalette::ToolTipText, QRgb(0x404044)); // same color as chart label brush palette.setColor(QPalette::ToolTipText, QRgb(0x404044)); // same color as chart label brush
} }

@ -153,7 +153,7 @@ void HeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalI
painter->fillRect(rect, bg_role.value<QBrush>()); painter->fillRect(rect, bg_role.value<QBrush>());
} }
QString text = model()->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole).toString(); QString text = model()->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole).toString();
painter->setPen(palette().color(settings.theme == DARK_THEME ? QPalette::BrightText : QPalette::Text)); painter->setPen(palette().color(utils::isDarkTheme() ? QPalette::BrightText : QPalette::Text));
painter->drawText(rect.adjusted(5, 3, -5, -3), defaultAlignment(), text.replace(QChar('_'), ' ')); painter->drawText(rect.adjusted(5, 3, -5, -3), defaultAlignment(), text.replace(QChar('_'), ' '));
} }

@ -191,8 +191,15 @@ DoubleValidator::DoubleValidator(QObject *parent) : QDoubleValidator(parent) {
} }
namespace utils { namespace utils {
bool isDarkTheme() {
QColor windowColor = QApplication::palette().color(QPalette::Window);
return windowColor.lightness() < 128;
}
QPixmap icon(const QString &id) { QPixmap icon(const QString &id) {
bool dark_theme = settings.theme == DARK_THEME; bool dark_theme = isDarkTheme();
QPixmap pm; QPixmap pm;
QString key = "bootstrap_" % id % (dark_theme ? "1" : "0"); QString key = "bootstrap_" % id % (dark_theme ? "1" : "0");
if (!QPixmapCache::find(key, &pm)) { if (!QPixmapCache::find(key, &pm)) {

@ -98,7 +98,9 @@ public:
}; };
namespace utils { namespace utils {
QPixmap icon(const QString &id); QPixmap icon(const QString &id);
bool isDarkTheme();
void setTheme(int theme); void setTheme(int theme);
QString formatSeconds(double sec, bool include_milliseconds = false, bool absolute_time = false); QString formatSeconds(double sec, bool include_milliseconds = false, bool absolute_time = false);
inline void drawStaticText(QPainter *p, const QRect &r, const QStaticText &text) { inline void drawStaticText(QPainter *p, const QRect &r, const QStaticText &text) {

Loading…
Cancel
Save