Browse Source

Merge pull request #17737 from Chocobo1/logview

Use proper color for highlighted text in log widget
adaptive-webui-19844
Chocobo1 2 years ago committed by GitHub
parent
commit
2a3a4bff70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 56
      src/gui/log/loglistview.cpp
  2. 9
      src/gui/utils.cpp
  3. 2
      src/gui/utils.h

56
src/gui/log/loglistview.cpp

@ -29,6 +29,7 @@
#include "loglistview.h" #include "loglistview.h"
#include <QtGlobal>
#include <QApplication> #include <QApplication>
#include <QClipboard> #include <QClipboard>
#include <QFontMetrics> #include <QFontMetrics>
@ -38,9 +39,12 @@
#include <QStyledItemDelegate> #include <QStyledItemDelegate>
#include "base/global.h" #include "base/global.h"
#include "gui/uithememanager.h"
#include "logmodel.h" #include "logmodel.h"
#ifdef Q_OS_WIN
#include "base/preferences.h"
#endif
namespace namespace
{ {
const QString SEPARATOR = u" - "_qs; const QString SEPARATOR = u" - "_qs;
@ -52,14 +56,21 @@ namespace
QString logText(const QModelIndex &index) QString logText(const QModelIndex &index)
{ {
return u"%1%2%3"_qs.arg(index.data(BaseLogModel::TimeRole).toString(), SEPARATOR return index.data(BaseLogModel::TimeRole).toString()
, index.data(BaseLogModel::MessageRole).toString()); + SEPARATOR
+ index.data(BaseLogModel::MessageRole).toString();
} }
class LogItemDelegate final : public QStyledItemDelegate class LogItemDelegate final : public QStyledItemDelegate
{ {
public: public:
using QStyledItemDelegate::QStyledItemDelegate; explicit LogItemDelegate(QObject *parent = nullptr)
: QStyledItemDelegate(parent)
#ifdef Q_OS_WIN
, m_useCustomUITheme(Preferences::instance()->useCustomUITheme())
#endif
{
}
private: private:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
@ -69,6 +80,16 @@ namespace
const QStyle *style = option.widget ? option.widget->style() : QApplication::style(); 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 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 // 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 // until they are set explicitly, and we have to manually set some font properties
@ -78,24 +99,23 @@ namespace
font.setPointSizeF(option.font.pointSizeF()); font.setPointSizeF(option.font.pointSizeF());
painter->setFont(font); painter->setFont(font);
const QPen originalPen = painter->pen(); QPalette palette = option.palette;
QPen coloredPen = originalPen;
coloredPen.setColor(index.data(BaseLogModel::TimeForegroundRole).value<QColor>());
painter->setPen(coloredPen);
const QString time = index.data(BaseLogModel::TimeRole).toString(); const QString time = index.data(BaseLogModel::TimeRole).toString();
style->drawItemText(painter, textRect, option.displayAlignment, option.palette, (option.state & QStyle::State_Enabled), time); palette.setColor(QPalette::Active, QPalette::WindowText, index.data(BaseLogModel::TimeForegroundRole).value<QColor>());
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 QFontMetrics fontMetrics = painter->fontMetrics(); // option.fontMetrics adds extra padding to QFontMetrics::width
const int separatorCoordinateX = horizontalAdvance(fontMetrics, time); const int separatorCoordinateX = horizontalAdvance(fontMetrics, time);
style->drawItemText(painter, textRect.adjusted(separatorCoordinateX, 0, 0, 0), option.displayAlignment, option.palette 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<QColor>());
painter->setPen(coloredPen);
const int messageCoordinateX = separatorCoordinateX + horizontalAdvance(fontMetrics, SEPARATOR); const int messageCoordinateX = separatorCoordinateX + horizontalAdvance(fontMetrics, SEPARATOR);
style->drawItemText(painter, textRect.adjusted(messageCoordinateX, 0, 0, 0), option.displayAlignment, option.palette const QString message = index.data(BaseLogModel::MessageRole).toString();
, (option.state & QStyle::State_Enabled), index.data(BaseLogModel::MessageRole).toString()); palette.setColor(QPalette::Active, QPalette::WindowText, index.data(BaseLogModel::MessageForegroundRole).value<QColor>());
style->drawItemText(painter, textRect.adjusted(messageCoordinateX, 0, 0, 0), option.displayAlignment, palette
, isEnabled, message, textRole);
painter->restore(); painter->restore();
} }
@ -107,6 +127,10 @@ namespace
const QSize margins = (defaultSize - fontSize).expandedTo({0, 0}); const QSize margins = (defaultSize - fontSize).expandedTo({0, 0});
return fontSize + margins; return fontSize + margins;
} }
#ifdef Q_OS_WIN
const bool m_useCustomUITheme = false;
#endif
}; };
} }
@ -116,7 +140,7 @@ LogListView::LogListView(QWidget *parent)
setSelectionMode(QAbstractItemView::ExtendedSelection); setSelectionMode(QAbstractItemView::ExtendedSelection);
setItemDelegate(new LogItemDelegate(this)); setItemDelegate(new LogItemDelegate(this));
#if defined(Q_OS_MAC) #ifdef Q_OS_MAC
setAttribute(Qt::WA_MacShowFocusRect, false); setAttribute(Qt::WA_MacShowFocusRect, false);
#endif #endif
} }

9
src/gui/utils.cpp

@ -35,8 +35,10 @@
#endif #endif
#include <QApplication> #include <QApplication>
#include <QColor>
#include <QDesktopServices> #include <QDesktopServices>
#include <QIcon> #include <QIcon>
#include <QPalette>
#include <QPixmap> #include <QPixmap>
#include <QPixmapCache> #include <QPixmapCache>
#include <QPoint> #include <QPoint>
@ -55,6 +57,13 @@
#include "base/utils/fs.h" #include "base/utils/fs.h"
#include "base/utils/version.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) QPixmap Utils::Gui::scaledPixmap(const QIcon &icon, const QWidget *widget, const int height)
{ {
Q_UNUSED(widget); // TODO: remove it Q_UNUSED(widget); // TODO: remove it

2
src/gui/utils.h

@ -38,6 +38,8 @@ class QWidget;
namespace Utils::Gui namespace Utils::Gui
{ {
bool isDarkTheme();
QPixmap scaledPixmap(const QIcon &icon, const QWidget *widget, int height); QPixmap scaledPixmap(const QIcon &icon, const QWidget *widget, int height);
QPixmap scaledPixmap(const Path &path, const QWidget *widget, int height = 0); QPixmap scaledPixmap(const Path &path, const QWidget *widget, int height = 0);
QPixmap scaledPixmapSvg(const Path &path, const QWidget *widget, int height); QPixmap scaledPixmapSvg(const Path &path, const QWidget *widget, int height);

Loading…
Cancel
Save