From 379bbb81a2fd3849980788bcf8aaa63141f87a15 Mon Sep 17 00:00:00 2001 From: mgrojo Date: Sun, 25 Feb 2018 19:13:28 +0100 Subject: [PATCH] Support for dark themes in default settings and restore defaults button All the colour configurations have been reviewed under an operating-system theme dark theme. Some hard-coded colours in QScintilla editors and Data Browser have been replaced by queries of the system palette or reuse of our settings. New settings for foreground and background colours for QScintilla editors defaulting to system colours. This is mainly needed because if the user saves colour settings for a dark theme and then he changes the system theme back to a light theme, then the foreground colours are preserved while the background would fall back to the system theme leading to an incompatible combination. This also gives more freedom to the user in defining his own colour combinations. Since only the default colour settings adapt to the system theme, and once settings are saved this can no longer happen, a 'Restore Defaults' button is added so the default adapted colour settings can be restored. This is also useful for restoring default behaviour when wanted. Other fixes and improvements: waiting cursor while saving; check boxes in SQL tab for bold, italic and underline when applicable; avoid translation of hidden colour setting names used in code. See related issue #1324. --- src/ExtendedScintilla.cpp | 19 ++++++++-- src/ExtendedScintilla.h | 2 ++ src/PreferencesDialog.cpp | 23 ++++++++++-- src/PreferencesDialog.h | 2 ++ src/PreferencesDialog.ui | 70 ++++++++++++++++++------------------ src/Settings.cpp | 74 +++++++++++++++++++++++++++++---------- src/Settings.h | 1 + src/docktextedit.cpp | 5 +++ 8 files changed, 139 insertions(+), 57 deletions(-) diff --git a/src/ExtendedScintilla.cpp b/src/ExtendedScintilla.cpp index 4ca3de4b..b86048e6 100644 --- a/src/ExtendedScintilla.cpp +++ b/src/ExtendedScintilla.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include @@ -99,6 +100,17 @@ void ExtendedScintilla::setupSyntaxHighlightingFormat(QsciLexer *lexer, const QS lexer->setFont(font, style); } +void ExtendedScintilla::setLexer(QsciLexer *lexer) +{ + QsciScintilla::setLexer(lexer); + + // Set margins to system window theme. setLexer seems to reset these colours. + setMarginsBackgroundColor(QPalette().color(QPalette::Active, QPalette::Window)); + setMarginsForegroundColor(QPalette().color(QPalette::Active, QPalette::WindowText)); + setIndentationGuidesBackgroundColor(QPalette().color(QPalette::Active, QPalette::Window)); + setIndentationGuidesForegroundColor(QPalette().color(QPalette::Active, QPalette::WindowText)); +} + void ExtendedScintilla::reloadKeywords() { // Set lexer again to reload the updated keywords list @@ -115,9 +127,11 @@ void ExtendedScintilla::reloadLexerSettings(QsciLexer *lexer) QFont defaultfont(Settings::getValue("editor", "font").toString()); defaultfont.setStyleHint(QFont::TypeWriter); defaultfont.setPointSize(Settings::getValue("editor", "fontsize").toInt()); - lexer->setDefaultColor(Qt::black); lexer->setFont(defaultfont); + lexer->setDefaultColor(QColor(Settings::getValue("syntaxhighlighter", "foreground_colour").toString())); + lexer->setPaper(QColor(Settings::getValue("syntaxhighlighter", "background_colour").toString())); + // Set font QFont font(Settings::getValue("editor", "font").toString()); font.setStyleHint(QFont::TypeWriter); @@ -129,12 +143,13 @@ void ExtendedScintilla::reloadLexerSettings(QsciLexer *lexer) marginsfont.setPointSize(font.pointSize()); setMarginsFont(marginsfont); setMarginLineNumbers(0, true); - setMarginsBackgroundColor(Qt::lightGray); + updateLineNumberAreaWidth(); // Highlight current line setCaretLineVisible(true); setCaretLineBackgroundColor(QColor(Settings::getValue("syntaxhighlighter", "currentline_colour").toString())); + setCaretForegroundColor(QColor(Settings::getValue("syntaxhighlighter", "foreground_colour").toString())); // Set tab width setTabWidth(Settings::getValue("editor", "tabsize").toInt()); diff --git a/src/ExtendedScintilla.h b/src/ExtendedScintilla.h index 9a7a1b37..99260450 100644 --- a/src/ExtendedScintilla.h +++ b/src/ExtendedScintilla.h @@ -19,6 +19,8 @@ public: bool findText(QString text, bool regexp, bool caseSensitive, bool words, bool wrap, bool forward); void clearSelection(); + // Override parent setLexer + void setLexer(QsciLexer *lexer); public slots: void reloadKeywords(); diff --git a/src/PreferencesDialog.cpp b/src/PreferencesDialog.cpp index 41b9c59d..cddb6fe4 100644 --- a/src/PreferencesDialog.cpp +++ b/src/PreferencesDialog.cpp @@ -110,7 +110,7 @@ void PreferencesDialog::loadSettings() ui->treeSyntaxHighlighting->topLevelItem(i)->setTextColor(2, color); ui->treeSyntaxHighlighting->topLevelItem(i)->setBackgroundColor(2, color); ui->treeSyntaxHighlighting->topLevelItem(i)->setText(2, colorname); - if (name != "null") { + if (name != "null" && name != "currentline" && name != "background" && name != "foreground") { ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(3, Settings::getValue("syntaxhighlighter", name + "_bold").toBool() ? Qt::Checked : Qt::Unchecked); ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(4, Settings::getValue("syntaxhighlighter", name + "_italic").toBool() ? Qt::Checked : Qt::Unchecked); ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(5, Settings::getValue("syntaxhighlighter", name + "_underline").toBool() ? Qt::Checked : Qt::Unchecked); @@ -181,6 +181,8 @@ void PreferencesDialog::loadSettings() void PreferencesDialog::saveSettings() { + QApplication::setOverrideCursor(Qt::WaitCursor); + Settings::setValue("db", "defaultencoding", ui->encodingComboBox->currentText()); Settings::setValue("db", "defaultlocation", ui->locationEdit->text()); Settings::setValue("db", "savedefaultlocation", ui->comboDefaultLocation->currentIndex()); @@ -281,10 +283,11 @@ void PreferencesDialog::saveSettings() Settings::setValue("General", "language", newLanguage); Settings::setValue("General", "toolbarStyle", ui->toolbarStyleComboBox->currentIndex()); - Settings::setValue("General", "DBFileExtensions", m_dbFileExtensions.join(";;") ); accept(); + + QApplication::restoreOverrideCursor(); } void PreferencesDialog::showColourDialog(QTreeWidgetItem* item, int column) @@ -578,3 +581,19 @@ void PreferencesDialog::on_buttonManageFileExtension_clicked() m_dbFileExtensions = manager->getDBFileExtensions(); } } + +void PreferencesDialog::on_buttonBox_clicked(QAbstractButton* button) +{ + if (button == ui->buttonBox->button(QDialogButtonBox::Cancel)) + reject(); + else if (button == ui->buttonBox->button(QDialogButtonBox::Save)) + saveSettings(); + else if (button == ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)) { + if (QMessageBox::warning(this, QApplication::applicationName(), tr("Are you sure you want to clear all the saved settings?\nAll your preferences will be lost and default values will be used."), + QMessageBox::RestoreDefaults | QMessageBox::Cancel, QMessageBox::Cancel) == QMessageBox::RestoreDefaults) + { + Settings::restoreDefaults(); + accept(); + } + } +} diff --git a/src/PreferencesDialog.h b/src/PreferencesDialog.h index eba8cbc3..e7cfd87f 100644 --- a/src/PreferencesDialog.h +++ b/src/PreferencesDialog.h @@ -9,6 +9,7 @@ class QTreeWidgetItem; class QFrame; class QTableWidget; class QSslCertificate; +class QAbstractButton; namespace Ui { class PreferencesDialog; @@ -37,6 +38,7 @@ private slots: void updatePreviewFont(); void on_buttonManageFileExtension_clicked(); + void on_buttonBox_clicked(QAbstractButton* button); private: Ui::PreferencesDialog *ui; diff --git a/src/PreferencesDialog.ui b/src/PreferencesDialog.ui index 1029df6f..e5030d15 100644 --- a/src/PreferencesDialog.ui +++ b/src/PreferencesDialog.ui @@ -806,7 +806,7 @@ - function + function Function @@ -894,12 +894,28 @@ - currentline + currentline Current line + + + background + + + Background + + + + + foreground + + + Foreground + + @@ -1349,7 +1365,7 @@ Qt::Horizontal - QDialogButtonBox::Cancel|QDialogButtonBox::Save + QDialogButtonBox::Cancel|QDialogButtonBox::RestoreDefaults|QDialogButtonBox::Save @@ -1415,38 +1431,6 @@ - - buttonBox - accepted() - PreferencesDialog - saveSettings() - - - 287 - 607 - - - 155 - 143 - - - - - buttonBox - rejected() - PreferencesDialog - reject() - - - 355 - 607 - - - 286 - 144 - - - buttonAddExtension clicked() @@ -1623,6 +1607,22 @@ + + buttonBox + clicked(QAbstractButton*) + PreferencesDialog + on_buttonBox_clicked(QAbstractButton*) + + + 385 + 591 + + + 385 + 306 + + + saveSettings() diff --git a/src/Settings.cpp b/src/Settings.cpp index 6aaeb35d..28da7c17 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -6,6 +6,7 @@ #include #include #include +#include QHash Settings::m_hCache; @@ -188,15 +189,15 @@ QVariant Settings::getDefaultValue(const QString& group, const QString& name) if(name == "null_fg_colour") return QColor(Qt::lightGray).name(); if(name == "null_bg_colour") - return QColor(Qt::white).name(); + return QPalette().color(QPalette::Active, QPalette::Base).name(); if(name == "reg_fg_colour") - return QColor(Qt::black).name(); + return QPalette().color(QPalette::Active, QPalette::Text).name(); if(name == "reg_bg_colour") - return QColor(Qt::white).name(); + return QPalette().color(QPalette::Active, QPalette::Base).name(); if(name == "bin_fg_colour") return QColor(Qt::lightGray).name(); if(name == "bin_bg_colour") - return QColor(Qt::white).name(); + return QPalette().color(QPalette::Active, QPalette::Base).name(); } // syntaxhighlighter? @@ -217,20 +218,50 @@ QVariant Settings::getDefaultValue(const QString& group, const QString& name) // Colour? if(name.right(6) == "colour") { - if(name == "keyword_colour") - return QColor(Qt::darkBlue).name(); - else if(name == "function_colour") - return QColor(Qt::blue).name(); - else if(name == "table_colour") - return QColor(Qt::darkCyan).name(); - else if(name == "comment_colour") - return QColor(Qt::darkGreen).name(); - else if(name == "identifier_colour") - return QColor(Qt::darkMagenta).name(); - else if(name == "string_colour") - return QColor(Qt::red).name(); - else if(name == "currentline_colour") - return QColor(236, 236, 245).name(); + QColor backgroundColour = QPalette().color(QPalette::Active, QPalette::Base); + QColor foregroundColour = QPalette().color(QPalette::Active, QPalette::Text); + + if(name == "foreground_colour") + return foregroundColour.name(); + else if(name == "background_colour") + return backgroundColour.name(); + + // Detect and provide sensible defaults for dark themes + if (backgroundColour.value() < foregroundColour.value()) { + if(name == "keyword_colour") + return QColor(82, 148, 226).name(); + else if(name == "function_colour") + return QColor(Qt::yellow).name(); + else if(name == "table_colour") + return QColor(Qt::cyan).name(); + else if(name == "comment_colour") + return QColor(Qt::green).name(); + else if(name == "identifier_colour") + return QColor(Qt::magenta).name(); + else if(name == "string_colour") + return QColor(Qt::lightGray).name(); + else if(name == "currentline_colour") + return backgroundColour.lighter(150).name(); + else if(name == "background_colour") + return backgroundColour.name(); + } else { + if(name == "keyword_colour") + return QColor(Qt::darkBlue).name(); + else if(name == "function_colour") + return QColor(Qt::blue).name(); + else if(name == "table_colour") + return QColor(Qt::darkCyan).name(); + else if(name == "comment_colour") + return QColor(Qt::darkGreen).name(); + else if(name == "identifier_colour") + return QColor(Qt::darkMagenta).name(); + else if(name == "string_colour") + return QColor(Qt::red).name(); + else if(name == "currentline_colour") + return QColor(236, 236, 245).name(); + else if(name == "background_colour") + return backgroundColour.name(); + } } } @@ -310,3 +341,10 @@ QVariant Settings::getDefaultValue(const QString& group, const QString& name) // Unknown combination of group and name? Return an invalid QVariant! return QVariant(); } + +void Settings::restoreDefaults () +{ + QSettings settings(QApplication::organizationName(), QApplication::organizationName()); + settings.clear(); + m_hCache.clear(); +} diff --git a/src/Settings.h b/src/Settings.h index 983a81f4..f80a1aed 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -12,6 +12,7 @@ class Settings public: static QVariant getValue(const QString& group, const QString& name); static void setValue(const QString& group, const QString& name, const QVariant& value, bool dont_save_to_disk = false); + static void restoreDefaults(); private: Settings() { } // class is fully static diff --git a/src/docktextedit.cpp b/src/docktextedit.cpp index ae913134..e5d99fb0 100644 --- a/src/docktextedit.cpp +++ b/src/docktextedit.cpp @@ -37,6 +37,7 @@ void DockTextEdit::reloadSettings() setupSyntaxHighlightingFormat(jsonLexer, "comment", QsciLexerJSON::CommentBlock); setupSyntaxHighlightingFormat(jsonLexer, "keyword", QsciLexerJSON::Keyword); setupSyntaxHighlightingFormat(jsonLexer, "keyword", QsciLexerJSON::KeywordLD); + setupSyntaxHighlightingFormat(jsonLexer, "function", QsciLexerJSON::Operator); setupSyntaxHighlightingFormat(jsonLexer, "string", QsciLexerJSON::String); setupSyntaxHighlightingFormat(jsonLexer, "table", QsciLexerJSON::Number); setupSyntaxHighlightingFormat(jsonLexer, "identifier", QsciLexerJSON::Property); @@ -57,8 +58,12 @@ void DockTextEdit::reloadSettings() jsonLexer->setPaper(jsonLexer->defaultPaper(QsciLexerJSON::String), QsciLexerJSON::Error); jsonLexer->setPaper(jsonLexer->defaultPaper(QsciLexerJSON::String), QsciLexerJSON::UnclosedString); + xmlLexer->setColor(QColor(Settings::getValue("syntaxhighlighter", "foreground_colour").toString())); setupSyntaxHighlightingFormat(xmlLexer, "comment", QsciLexerHTML::HTMLComment); setupSyntaxHighlightingFormat(xmlLexer, "keyword", QsciLexerHTML::Tag); + setupSyntaxHighlightingFormat(xmlLexer, "keyword", QsciLexerHTML::XMLTagEnd); + setupSyntaxHighlightingFormat(xmlLexer, "keyword", QsciLexerHTML::XMLStart); + setupSyntaxHighlightingFormat(xmlLexer, "keyword", QsciLexerHTML::XMLEnd); setupSyntaxHighlightingFormat(xmlLexer, "string", QsciLexerHTML::HTMLDoubleQuotedString); setupSyntaxHighlightingFormat(xmlLexer, "string", QsciLexerHTML::HTMLSingleQuotedString); setupSyntaxHighlightingFormat(xmlLexer, "table", QsciLexerHTML::HTMLNumber);