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

View file

@ -120,6 +120,7 @@ ConnectionConfigurationWidget::ConnectionConfigurationWidget(
cmbbxSsl->setModelColumn(0);
auto ssl_model = new SslModeModel(this);
cmbbxSsl->setModel(ssl_model);
cmbbxSsl->setEditable(true);
lblSsl->setBuddy(cmbbxSsl);
lblCert = new QLabel;
@ -206,7 +207,8 @@ void ConnectionConfigurationWidget::setData(const ConnectionConfig &cfg)
edtPassword->setText("");
cmbDbname->setCurrentText(cfg.dbname());
cmbbxSsl->setCurrentIndex(static_cast<int>(cfg.sslMode()));
//cmbbxSsl->setCurrentIndex(static_cast<int>(cfg.sslMode()));
cmbbxSsl->setCurrentText(cfg.getParameter("sslmode"));
edtCert->setText(cfg.sslCert());
edtKey->setText(cfg.sslKey());
edtRootCert->setText(cfg.sslRootCert());

View file

@ -1,5 +1,4 @@
#include "ResultTableModelUtil.h"
#include "Pgsql_oids.h"
#include <QTableView>
#include <QHeaderView>
@ -28,29 +27,7 @@ Qt::Alignment GetDefaultAlignmentForType(Oid o)
return r;
}
QColor GetDefaultColorForType(Oid o)
{
QColor c;
switch (o) {
case int2_oid:
case int4_oid:
case int8_oid:
c = GetDefaultIntegerColor();
break;
case float4_oid:
case float8_oid:
c = GetDefaultFloatColor();
break;
case numeric_oid:
c = GetDefaultNumericColor();
break;
case oid_oid:
case bool_oid:
default:
c = Qt::black;
}
return c;
}
QString FormatBoolForDisplay(bool v)
{

View file

@ -1,10 +1,36 @@
#pragma once
#include "Pgsql_declare.h"
#include "Pgsql_oids.h"
#include <QAbstractTableModel>
#include <QColor>
#include <util/Colors.h>
Qt::Alignment GetDefaultAlignmentForType(Oid oid);
QColor GetDefaultColorForType(Oid oid);
// inline QColor GetDefaultColorForType(Oid oid)
// {
// using namespace Pgsql;
// const ColorTheme &theme = GetColorTheme();
// QColor c;
// switch (oid) {
// case int2_oid:
// case int4_oid:
// case int8_oid:
// c = theme.integerColor;
// break;
// case float4_oid:
// case float8_oid:
// c = theme.floatColor;
// break;
// case numeric_oid:
// c = theme.numericColor;
// break;
// case oid_oid:
// case bool_oid:
// default:
// c = Qt::black;
// }
// return c;
// }
inline Qt::Alignment GetDefaultAlignment() { return Qt::AlignLeft | Qt::AlignVCenter; }
inline Qt::Alignment GetDefaultBoolAlignment() { return Qt::AlignCenter | Qt::AlignVCenter; }
@ -12,13 +38,14 @@ inline Qt::Alignment GetDefaultNumberAlignment() { return Qt::AlignRight | Qt::A
inline QColor GetDefaultBoolColor(bool v)
{
return v ? Qt::darkGreen : Qt::darkRed;
const ColorTheme& colorTheme = GetColorTheme();
return v ? colorTheme.booleanTrue : colorTheme.booleanFalse;
}
inline QColor GetDefaultIntegerColor() { return Qt::darkBlue; }
inline QColor GetDefaultFloatColor() { return Qt::darkCyan; }
inline QColor GetDefaultNumericColor() { return Qt::darkGreen; }
inline QColor GetDefaultNullColor() { return Qt::gray; }
// inline QColor GetDefaultIntegerColor() { return Qt::darkBlue; }
// inline QColor GetDefaultFloatColor() { return Qt::darkCyan; }
// inline QColor GetDefaultNumericColor() { return Qt::darkGreen; }
// inline QColor GetDefaultNullColor() { return Qt::gray; }
QString FormatBoolForDisplay(bool v);

View file

@ -30,6 +30,13 @@ namespace {
}
ColumnTableModel::ColumnTableModel(QObject *parent)
: BaseTableModel(parent)
, theme(GetColorTheme())
{
}
void ColumnTableModel::setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table)
{
if (cat != m_catalog) {
@ -285,22 +292,19 @@ QVariant ColumnTableModel::data(const QModelIndex &index, int role) const
QVariant v;
const auto &t = m_columns[index.row()];
if (t.typid == InvalidOid)
v = QBrush(Qt::black);
v = QBrush(theme.defaultTextColor);
else {
auto c = m_catalog->types()->getByKey(t.typid);
switch (c->category) {
case TypCategory::Boolean:
v = QBrush(Qt::darkGreen);
break;
case TypCategory::Numeric:
v = QBrush(Qt::darkBlue);
v = QBrush(theme.numericTypeColor);
break;
case TypCategory::DateTime:
case TypCategory::Timespan:
v = QBrush(Qt::darkMagenta);
v = QBrush(theme.dateTimeTypeColor);
break;
case TypCategory::String:
v = QBrush(Qt::darkYellow);
v = QBrush(theme.stringTypeColor);
break;
case TypCategory::Array:
case TypCategory::Composite:
@ -313,7 +317,8 @@ QVariant ColumnTableModel::data(const QModelIndex &index, int role) const
case TypCategory::BitString:
case TypCategory::Unknown:
default:
v = QBrush(Qt::black);
v = QBrush(theme.defaultTextColor);
break;
}
}
return v;

View file

@ -1,6 +1,7 @@
#ifndef COLUMNTABLEMODEL_H
#define COLUMNTABLEMODEL_H
#include "util/Colors.h"
#include "catalog/models/BaseTableModel.h"
#include "catalog/PgAttribute.h"
#include "catalog/PgDatabaseCatalog.h"
@ -29,7 +30,8 @@ public:
colCount };
using BaseTableModel::BaseTableModel;
explicit ColumnTableModel(QObject *parent = nullptr);
void setData(std::shared_ptr<const PgDatabaseCatalog> cat, const std::optional<PgClass> &table);
// Header:
@ -57,6 +59,9 @@ protected:
QString getFKey(const PgAttribute &column) const;
QString getDefaultString(const PgAttribute &column) const;
private:
const ColorTheme &theme;
private slots:
/// Retrieves required data from catalog, called everytime it might have changed
void refresh();

View file

@ -14,6 +14,7 @@
CodeEditor::CodeEditor(QWidget *parent)
: QPlainTextEdit(parent)
, gutterArea(new EditorGutter(this))
, colorTheme(GetColorTheme())
{
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateGutterAreaWidth(int)));
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateGutterArea(QRect,int)));
@ -69,8 +70,7 @@ void CodeEditor::resizeEvent(QResizeEvent *e)
void CodeEditor::highlightCurrentLine()
{
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setBackground(colorTheme.currentLine);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
@ -87,8 +87,7 @@ void CodeEditor::onTextChanged()
void CodeEditor::addErrorMarker(int position, int length)
{
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::red).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setBackground(colorTheme.errorLine);
selection.format.setFontItalic(true);
selection.cursor = textCursor();
selection.cursor.setPosition(position);

View file

@ -3,6 +3,7 @@
#include <QPlainTextEdit>
#include <set>
#include "util/Colors.h"
/** This class adds some capabilities to QPlainTextEdit that are useful
* in code editor scenarios.
@ -39,6 +40,7 @@ private:
QTextEdit::ExtraSelection currentLine;
QList<QTextEdit::ExtraSelection> errorMarkers;
const ColorTheme &colorTheme;
std::set<int> errorLines;

View file

@ -7,11 +7,14 @@ GutterPainter::GutterPainter(CodeEditor *editor, QPaintEvent *event)
, painter(editor->gutterArea)
, event(event)
, fontMetrics(editor->fontMetrics())
{}
, colorTheme(GetColorTheme())
{
}
void GutterPainter::Paint()
{
painter.fillRect(event->rect(), Qt::lightGray);
painter.fillRect(event->rect(), colorTheme.gutterBackground);
LoopState loopState(editor);
// We will now loop through all visible lines and paint the line numbers in the
@ -37,7 +40,7 @@ void GutterPainter::Paint()
void GutterPainter::drawLineNumber(const LoopState &loopState)
{
QString number = QString::number(loopState.blockNumber() + 1);
painter.setPen(Qt::black);
painter.setPen(colorTheme.lineNumber);
painter.drawText(0, loopState.top, editor->gutterArea->width(), fontMetrics.height(),
Qt::AlignRight, number);
}

View file

@ -1,5 +1,6 @@
#include <QPainter>
#include <QTextBlock>
#include "util/Colors.h"
#pragma once
@ -13,10 +14,13 @@ public:
void Paint();
private:
CodeEditor *editor;
QPainter painter;
QPaintEvent *event;
QFontMetrics fontMetrics;
const ColorTheme &colorTheme;
struct LoopState {
CodeEditor *editor;

View file

@ -234,7 +234,7 @@ void CrudModel::loadIntoModel(std::shared_ptr<Pgsql::Result> data)
initializeColumnList();
lastRowKey = data->rows() - 1;
initRowMapping();
appendNewRow();
appendNewRowInternal();
endResetModel();
}
@ -549,10 +549,15 @@ void CrudModel::appendNewRow()
{
int row = static_cast<int>(m_rowMapping.size());
beginInsertRows(QModelIndex(), row, row);
m_rowMapping.emplace_back(allocNewRowKey(), std::vector<Value>(m_roData->cols()));
appendNewRowInternal();
endInsertRows();
}
void CrudModel::appendNewRowInternal()
{
m_rowMapping.emplace_back(allocNewRowKey(), std::vector<Value>(m_roData->cols()));
}
std::tuple<bool, QString> CrudModel::removeRows(const std::set<IntegerRange<int>> &row_ranges)
{
if (row_ranges.empty())

View file

@ -246,6 +246,7 @@ private:
std::tuple<bool, std::vector<Value>> saveRow(const PendingRow &pending_row);
void appendNewRow();
void appendNewRowInternal();
int lastRowKey = -1;
int allocNewRowKey() { return ++lastRowKey; }

View file

@ -1,8 +1,10 @@
#include "MasterController.h"
#include "util/Colors.h"
#include <QApplication>
#ifdef _WIN32
# include <winsock2.h>
#endif
#include <QPalette>
#include <memory>
int main(int argc, char *argv[])
@ -28,6 +30,8 @@ int main(int argc, char *argv[])
QCoreApplication::setOrganizationDomain("eelkeklein.nl");
QCoreApplication::setApplicationName("pglab");
InitApplicationPalette();
int result = -1;
{
// make sure the io_service is stopped before we wait on the future

View file

@ -58,6 +58,7 @@ SOURCES += main.cpp\
serverinspector/DatabasesPage.cpp \
serverinspector/RolesPage.cpp \
serverinspector/ServerInspector.cpp \
util/Colors.cpp \
util/PgLabItemDelegate.cpp \
util/PgLabTableView.cpp \
util/SqlSyntaxHighlighter.cpp \
@ -130,6 +131,7 @@ HEADERS += \
serverinspector/DatabasesPage.h \
serverinspector/RolesPage.h \
serverinspector/ServerInspector.h \
util/Colors.h \
util/PgLabItemDelegate.h \
util/PgLabTableView.h \
util/PgLabTableViewHelper.h \

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);
}