From 0e0b8d102711e3e15f2750ae352387a9a0db2b35 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 17 Sep 2022 01:12:12 +0800 Subject: [PATCH 1/3] Revive dark theme detection The code was removed in 199d770e152275d01da6134b802f2e16fa4e96af and now revived. --- src/gui/utils.cpp | 9 +++++++++ src/gui/utils.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/gui/utils.cpp b/src/gui/utils.cpp index b40dcbbf6..99d31997c 100644 --- a/src/gui/utils.cpp +++ b/src/gui/utils.cpp @@ -34,8 +34,10 @@ #endif #include +#include #include #include +#include #include #include #include @@ -54,6 +56,13 @@ #include "base/utils/fs.h" #include "base/utils/version.h" +bool Utils::Gui::isDarkTheme() +{ + const QPalette palette = qApp->palette(); + const QColor &color = palette.color(QPalette::Active, QPalette::Base); + return (color.lightness() < 127); +} + QPixmap Utils::Gui::scaledPixmap(const QIcon &icon, const QWidget *widget, const int height) { Q_UNUSED(widget); // TODO: remove it diff --git a/src/gui/utils.h b/src/gui/utils.h index 58c9ac27f..ccf67f172 100644 --- a/src/gui/utils.h +++ b/src/gui/utils.h @@ -38,6 +38,8 @@ class QWidget; namespace Utils::Gui { + bool isDarkTheme(); + QPixmap scaledPixmap(const QIcon &icon, const QWidget *widget, int height); QPixmap scaledPixmap(const Path &path, const QWidget *widget, int height = 0); QPixmap scaledPixmapSvg(const Path &path, const QWidget *widget, int height); From 3aadb63d8020518985078258f65e5a9b9f456fc6 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 17 Sep 2022 01:38:56 +0800 Subject: [PATCH 2/3] Simplify code Don't use replacement placeholder and use string append which is faster. --- src/gui/log/loglistview.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/gui/log/loglistview.cpp b/src/gui/log/loglistview.cpp index c2494b6db..2fc41eec3 100644 --- a/src/gui/log/loglistview.cpp +++ b/src/gui/log/loglistview.cpp @@ -52,8 +52,9 @@ namespace QString logText(const QModelIndex &index) { - return u"%1%2%3"_qs.arg(index.data(BaseLogModel::TimeRole).toString(), SEPARATOR - , index.data(BaseLogModel::MessageRole).toString()); + return index.data(BaseLogModel::TimeRole).toString() + + SEPARATOR + + index.data(BaseLogModel::MessageRole).toString(); } class LogItemDelegate final : public QStyledItemDelegate From 37150520e15b81caabea6c4c1d56b5202bd7381d Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 17 Sep 2022 01:55:59 +0800 Subject: [PATCH 3/3] Use proper color for highlighted text in log widget The color is either from qbt theme pack or desktop environment. Note that Windows default theme do not use highlighted text color. Other themes (either OS default or qbt themes) are closer to native look when highlighted text color is in action. --- src/gui/log/loglistview.cpp | 53 ++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/src/gui/log/loglistview.cpp b/src/gui/log/loglistview.cpp index 2fc41eec3..ce841e3ad 100644 --- a/src/gui/log/loglistview.cpp +++ b/src/gui/log/loglistview.cpp @@ -29,6 +29,7 @@ #include "loglistview.h" +#include #include #include #include @@ -38,9 +39,12 @@ #include #include "base/global.h" -#include "gui/uithememanager.h" #include "logmodel.h" +#ifdef Q_OS_WIN +#include "base/preferences.h" +#endif + namespace { const QString SEPARATOR = u" - "_qs; @@ -60,7 +64,13 @@ namespace class LogItemDelegate final : public QStyledItemDelegate { public: - using QStyledItemDelegate::QStyledItemDelegate; + explicit LogItemDelegate(QObject *parent = nullptr) + : QStyledItemDelegate(parent) +#ifdef Q_OS_WIN + , m_useCustomUITheme(Preferences::instance()->useCustomUITheme()) +#endif + { + } private: void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override @@ -70,6 +80,16 @@ namespace const QStyle *style = option.widget ? option.widget->style() : QApplication::style(); const QRect textRect = option.rect.adjusted(1, 0, 0, 0); // shift 1 to avoid text being too close to focus rect + const bool isEnabled = option.state.testFlag(QStyle::State_Enabled); + +#ifdef Q_OS_WIN + // Windows default theme do not use highlighted text color + const QPalette::ColorRole textRole = m_useCustomUITheme + ? (option.state.testFlag(QStyle::State_Selected) ? QPalette::HighlightedText : QPalette::WindowText) + : QPalette::WindowText; +#else + const QPalette::ColorRole textRole = option.state.testFlag(QStyle::State_Selected) ? QPalette::HighlightedText : QPalette::WindowText; +#endif // for unknown reasons (fixme) painter won't accept some font properties // until they are set explicitly, and we have to manually set some font properties @@ -79,24 +99,23 @@ namespace font.setPointSizeF(option.font.pointSizeF()); painter->setFont(font); - const QPen originalPen = painter->pen(); - QPen coloredPen = originalPen; - coloredPen.setColor(index.data(BaseLogModel::TimeForegroundRole).value()); - painter->setPen(coloredPen); - const QString time = index.data(BaseLogModel::TimeRole).toString(); - style->drawItemText(painter, textRect, option.displayAlignment, option.palette, (option.state & QStyle::State_Enabled), time); + QPalette palette = option.palette; + + const QString time = index.data(BaseLogModel::TimeRole).toString(); + palette.setColor(QPalette::Active, QPalette::WindowText, index.data(BaseLogModel::TimeForegroundRole).value()); + style->drawItemText(painter, textRect, option.displayAlignment, palette, isEnabled, time, textRole); - painter->setPen(originalPen); const QFontMetrics fontMetrics = painter->fontMetrics(); // option.fontMetrics adds extra padding to QFontMetrics::width const int separatorCoordinateX = horizontalAdvance(fontMetrics, time); style->drawItemText(painter, textRect.adjusted(separatorCoordinateX, 0, 0, 0), option.displayAlignment, option.palette - , (option.state & QStyle::State_Enabled), SEPARATOR); + , isEnabled, SEPARATOR, textRole); - coloredPen.setColor(index.data(BaseLogModel::MessageForegroundRole).value()); - painter->setPen(coloredPen); const int messageCoordinateX = separatorCoordinateX + horizontalAdvance(fontMetrics, SEPARATOR); - style->drawItemText(painter, textRect.adjusted(messageCoordinateX, 0, 0, 0), option.displayAlignment, option.palette - , (option.state & QStyle::State_Enabled), index.data(BaseLogModel::MessageRole).toString()); + const QString message = index.data(BaseLogModel::MessageRole).toString(); + palette.setColor(QPalette::Active, QPalette::WindowText, index.data(BaseLogModel::MessageForegroundRole).value()); + style->drawItemText(painter, textRect.adjusted(messageCoordinateX, 0, 0, 0), option.displayAlignment, palette + , isEnabled, message, textRole); + painter->restore(); } @@ -108,6 +127,10 @@ namespace const QSize margins = (defaultSize - fontSize).expandedTo({0, 0}); return fontSize + margins; } + +#ifdef Q_OS_WIN + const bool m_useCustomUITheme = false; +#endif }; } @@ -117,7 +140,7 @@ LogListView::LogListView(QWidget *parent) setSelectionMode(QAbstractItemView::ExtendedSelection); setItemDelegate(new LogItemDelegate(this)); -#if defined(Q_OS_MAC) +#ifdef Q_OS_MAC setAttribute(Qt::WA_MacShowFocusRect, false); #endif }