Dark mode support

Centralized all colors, tweaked application paletter in darkmode to make it darker.
This commit is contained in:
eelke 2025-02-23 08:32:15 +01:00
parent aac55b0ed1
commit 86a9a0d709
19 changed files with 335 additions and 73 deletions

147
pglab/util/Colors.cpp Normal file
View file

@ -0,0 +1,147 @@
#include "Colors.h"
#include <QGuiApplication>
#include <QPalette>
#include <QColor>
bool isDarkModeInternal()
{
QPalette palette = QGuiApplication::palette();
QColor backgroundColor = palette.color(QPalette::Window);
return backgroundColor.lightness() < 128; // Check if the background color is dark
}
bool isDarkMode()
{
static bool darkMode = isDarkModeInternal();
return darkMode;
}
ColorTheme GetLightMode()
{
ColorTheme t;
t.defaultTextColor = Qt::black;
t.gutterBackground = Qt::lightGray;
t.lineNumber = Qt::black;
t.currentLine = QColor(Qt::yellow).lighter(160);
t.errorLine = QColor(Qt::red).lighter(160);
t.keyword = QColor(32, 32, 192);
t.comment = QColor(128, 128, 128);
t.quotedString = QColor(192, 32, 192);
t.quotedIdentifier = QColor(192, 128, 32);
t.type = QColor(32, 192, 32);
t.parameter = QColor(192, 32, 32);
t.gigaBytes = QColorConstants::Svg::darkorange;
t.megaBytes = QColorConstants::Svg::darkgoldenrod;
t.kiloBytes = QColorConstants::Svg::darkgreen;
t.bytes = QColorConstants::Svg::darkblue;
t.booleanTrue = Qt::darkGreen;
t.booleanFalse = Qt::darkRed;
t.integerColor = Qt::darkBlue;
t.floatColor = Qt::darkCyan;
t.numericColor = Qt::darkGreen;
t.nullColor = Qt::gray;
t.numericTypeColor = Qt::darkBlue;
t.dateTimeTypeColor = Qt::darkMagenta;
t.stringTypeColor = Qt::darkYellow;
return t;
}
ColorTheme GetDarkMode()
{
ColorTheme t;
t.defaultTextColor = Qt::lightGray;
t.gutterBackground = QColor(Qt::darkGray).darker(400);
t.lineNumber = Qt::lightGray;
t.currentLine = QColor(Qt::darkGray).darker(400);;
t.errorLine = QColor(Qt::red).darker(180);
t.keyword = QColor(160, 160, 255);
t.comment = QColor(160, 160, 160);
t.quotedString = QColor(255, 160, 255);
t.quotedIdentifier = QColor(255, 192, 128);
t.type = QColor(160, 255, 160);
t.parameter = QColor(255, 160, 160);
t.gigaBytes = QColorConstants::Svg::lightcoral;
t.megaBytes = QColorConstants::Svg::lightgoldenrodyellow;
t.kiloBytes = QColorConstants::Svg::lightgreen;
t.bytes = QColorConstants::Svg::lightblue;
t.booleanTrue = QColorConstants::Svg::lightgreen;
t.booleanFalse = QColorConstants::Svg::lightcoral;
t.integerColor = QColorConstants::Svg::deepskyblue;
t.floatColor = QColorConstants::Svg::cyan;
t.numericColor = QColorConstants::Svg::lime;
t.nullColor = Qt::darkGray;
t.numericTypeColor = QColorConstants::Svg::lightblue;
t.dateTimeTypeColor = QColor(Qt::magenta);
t.stringTypeColor = QColor(Qt::yellow);
return t;
}
const ColorTheme& GetColorTheme()
{
static ColorTheme lightMode = GetLightMode();
static ColorTheme darkMode = GetDarkMode();
return isDarkMode() ? darkMode : lightMode;
}
void InitApplicationPalette()
{
if (isDarkMode())
{
QPalette pal = QGuiApplication::palette();
pal.setColor(QPalette::Active, QPalette::Base, QColor(20, 20, 20));
pal.setColor(QPalette::Active, QPalette::AlternateBase, Qt::black);
pal.setColor(QPalette::Inactive, QPalette::Base, QColor(20, 20, 20));
pal.setColor(QPalette::Inactive, QPalette::AlternateBase, Qt::black);
QGuiApplication::setPalette(pal);
}
}
QColor ColorTheme::GetColorForType(Oid oid) const
{
using namespace Pgsql;
QColor c;
switch (oid) {
case int2_oid:
case int4_oid:
case int8_oid:
c = integerColor;
break;
case float4_oid:
case float8_oid:
c = floatColor;
break;
case numeric_oid:
c = numericColor;
break;
case varchar_oid:
case text_oid:
case oid_oid:
case bool_oid:
default:
c = defaultTextColor;
}
return c;
}

48
pglab/util/Colors.h Normal file
View file

@ -0,0 +1,48 @@
#include <QColor>
#include "Pgsql_oids.h"
#pragma once
bool isDarkMode();
struct ColorTheme
{
QColor defaultTextColor;
QColor gutterBackground;
QColor lineNumber;
QColor currentLine;
QColor errorLine;
QColor keyword;
QColor comment;
QColor quotedString;
QColor quotedIdentifier;
QColor type;
QColor parameter;
QColor gigaBytes;
QColor megaBytes;
QColor kiloBytes;
QColor bytes;
QColor booleanTrue;
QColor booleanFalse;
QColor integerColor;
QColor floatColor;
QColor numericColor;
QColor nullColor;
QColor numericTypeColor;
QColor dateTimeTypeColor;
QColor stringTypeColor;
QColor GetColorForType(Oid oid) const;
};
const ColorTheme& GetColorTheme();
void InitApplicationPalette();

View file

@ -1,11 +1,14 @@
#include "util/PgLabItemDelegate.h"
#include <QApplication>
#include <QPainter>
#include "Pgsql_oids.h"
#include "ResultTableModelUtil.h"
#include "CustomDataRole.h"
#include "AbstractEditorFactory.h"
#include "Colors.h"
#include "utils/HumanReadableBytes.h"
#include "qstyleoption.h"
PgLabItemDelegate::PgLabItemDelegate(QObject *parent)
: QStyledItemDelegate(parent)
@ -83,6 +86,8 @@ void PgLabItemDelegate::initStyleOption(QStyleOptionViewItem *option,
// }
// }
const ColorTheme &theme = GetColorTheme();
Oid oid = InvalidOid;
value = index.data(CustomDataTypeRole); // get OID
if (value.isValid())
@ -99,9 +104,7 @@ void PgLabItemDelegate::initStyleOption(QStyleOptionViewItem *option,
if (value.isValid() && ! value.isNull()) {
QColor forground_color = oid == Pgsql::bool_oid
? GetDefaultBoolColor(value.toBool())
: GetDefaultColorForType(oid);
QColor forground_color;
option->features |= QStyleOptionViewItem::HasDisplay;
if (oid == Pgsql::bool_oid) {
@ -110,7 +113,7 @@ void PgLabItemDelegate::initStyleOption(QStyleOptionViewItem *option,
option->text = FormatBoolForDisplay(b);
}
else {
forground_color = GetDefaultColorForType(oid);
forground_color = theme.GetColorForType(oid);
if (meaning == DataMeaning::Bytes) {
option->text = HandleBytes(value.toLongLong(), forground_color);
}
@ -123,13 +126,22 @@ void PgLabItemDelegate::initStyleOption(QStyleOptionViewItem *option,
option->palette.setBrush(QPalette::Text, QBrush(forground_color));
}
else {
option->palette.setBrush(QPalette::Text, QBrush(GetDefaultNullColor()));
option->palette.setBrush(QPalette::Text, QBrush(theme.nullColor));
option->features |= QStyleOptionViewItem::HasDisplay;
option->text = "null";
}
// if (option->state & QStyle::State_HasFocus) {
// QColor color = Qt::darkYellow;
// option->palette.setColor(QPalette::Active, QPalette::Highlight, color);
// // option->palette.setColor(QPalette::Active, QPalette::AlternateBase, color);
// option->backgroundBrush = QBrush(color);
// option->font.setWeight(QFont::ExtraBold);
// }
// option->backgroundBrush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole));
// disable style animations for checkboxes etc. within itemviews (QTBUG-30146)
@ -138,22 +150,25 @@ void PgLabItemDelegate::initStyleOption(QStyleOptionViewItem *option,
QString PgLabItemDelegate::HandleBytes(qlonglong s, QColor &forground_color) const
{
const ColorTheme& theme = GetColorTheme();
auto str = HumanReadableBytes(s);
if (s > 1024 * 1024 * 1024) {
forground_color = QColorConstants::Svg::darkorange;
forground_color = theme.gigaBytes;
}
else if (s > 1024 * 1024) {
forground_color = QColorConstants::Svg::darkgoldenrod;
forground_color = theme.megaBytes;
}
else if (s > 1024) {
forground_color = QColorConstants::Svg::darkgreen;
forground_color = theme.kiloBytes;
}
else {
forground_color = QColorConstants::Svg::darkblue;
forground_color = theme.bytes;
}
return QString::fromStdString(str);
}
void PgLabItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
@ -165,6 +180,13 @@ void PgLabItemDelegate::paint(QPainter *painter,
const QWidget *widget = option.widget; // QStyledItemDelegatePrivate::widget(option);
QStyle *style = widget ? widget->style() : QApplication::style();
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
if (option.state & QStyle::State_HasFocus) {
QColor color = Qt::red;
painter->setPen(QPen(QBrush(color), 2.0));
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
}
}
QWidget *PgLabItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const

View file

@ -6,14 +6,19 @@
PgLabTableView::PgLabTableView(QWidget *parent)
: QTableView(parent)
{
setAlternatingRowColors(true);
setItemDelegate(new PgLabItemDelegate(this));
setWordWrap(false);
setAlternatingRowColors(true);
auto pal = palette();
pal.setColor(QPalette::Inactive, QPalette::Highlight, pal.color(QPalette::Active, QPalette::Highlight));
pal.setColor(QPalette::Inactive, QPalette::HighlightedText, pal.color(QPalette::Active, QPalette::HighlightedText));
setPalette(pal);
setItemDelegate(new PgLabItemDelegate(this));
setWordWrap(false);
QPalette pal = palette();
//pal.setColor(QPalette::Inactive, QPalette::Highlight, pal.color(QPalette::Active, QPalette::Highlight));
//pal.setColor(QPalette::Inactive, QPalette::HighlightedText, pal.color(QPalette::Active, QPalette::HighlightedText));
// pal.setColor(QPalette::Active, QPalette::Base, QColor(20, 20, 20));
// pal.setColor(QPalette::Active, QPalette::AlternateBase, Qt::black);
// setPalette(pal);
auto vertical_header = verticalHeader();
vertical_header->setMinimumSectionSize(16);

View file

@ -47,6 +47,7 @@ public:
m_itemView->setModel(m_sortFilter);
m_itemView->setSortingEnabled(true);
m_sortFilter->sort(0, Qt::AscendingOrder);
}
PgLabTableViewHelper(QWidget * parent)

View file

@ -2,25 +2,28 @@
#include "catalog/PgTypeContainer.h"
#include "SqlLexer.h"
#include "util/Colors.h"
SqlSyntaxHighlighter::SqlSyntaxHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
m_keywordFormat.setForeground(QColor(32, 32, 192));
m_keywordFormat.setFontWeight(QFont::Bold);
const ColorTheme& theme = GetColorTheme();
m_commentFormat.setForeground(QColor(128, 128, 128));
m_keywordFormat.setForeground(theme.keyword);
m_keywordFormat.setFontWeight(QFont::Bold);
m_quotedStringFormat.setForeground(QColor(192, 32, 192));
m_commentFormat.setForeground(theme.comment);
m_quotedIdentifierFormat.setForeground(QColor(192, 128, 32));
m_quotedStringFormat.setForeground(theme.quotedString);
m_typeFormat.setForeground(QColor(32, 192, 32));
m_quotedIdentifierFormat.setForeground(theme.quotedIdentifier);
m_typeFormat.setForeground(theme.type);
m_typeFormat.setFontWeight(QFont::Bold);
m_parameterFormat.setForeground(QColor(192, 32, 32));
m_parameterFormat.setForeground(theme.parameter);
m_parameterFormat.setFontWeight(QFont::Bold);
}