Skip to content

Commit cc67969

Browse files
committed
Update preference colours when the application style is changed
In order to have matching colours in all the preferences, the individual colour settings in Data Browser and SQL tabs are reset to default values matching the corresponding style setting (dark stylesheet or follow desktop, which could be dark or light as always). Additionally, several problems with colour settings in QScintilla have been fixed: - We don't use indentation guides - both sets of lexer colour settings must be used, otherwise the result is inconsistant and unpredictable: * lexer->setDefaultColor|Paper and lexer->setColor|Paper
1 parent e84589b commit cc67969

File tree

6 files changed

+177
-75
lines changed

6 files changed

+177
-75
lines changed

src/ExtendedScintilla.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,18 @@ void ExtendedScintilla::setLexer(QsciLexer *lexer)
125125
{
126126
QsciScintilla::setLexer(lexer);
127127

128-
// Set margins to system window theme. setLexer seems to reset these colours.
129-
setMarginsBackgroundColor(QPalette().color(QPalette::Active, QPalette::Window));
130-
setMarginsForegroundColor(QPalette().color(QPalette::Active, QPalette::WindowText));
131-
setIndentationGuidesBackgroundColor(QPalette().color(QPalette::Active, QPalette::Window));
132-
setIndentationGuidesForegroundColor(QPalette().color(QPalette::Active, QPalette::WindowText));
128+
// Set margins according to settings. setLexer seems to reset these colours.
129+
// Use desktop default colors for margins when following desktop style, or the custom colors otherwise.
130+
switch (Settings::getValue("General", "appStyle").toInt()) {
131+
case Settings::FollowDesktopStyle :
132+
setMarginsBackgroundColor(QPalette().color(QPalette::Active, QPalette::Window));
133+
setMarginsForegroundColor(QPalette().color(QPalette::Active, QPalette::WindowText));
134+
break;
135+
case Settings::DarkStyle :
136+
setMarginsBackgroundColor(QColor(Settings::getValue("syntaxhighlighter","background_colour").toString()));
137+
setMarginsForegroundColor(QColor(Settings::getValue("syntaxhighlighter","foreground_colour").toString()));
138+
break;
139+
}
133140
}
134141

135142
void ExtendedScintilla::reloadKeywords()
@@ -145,33 +152,38 @@ void ExtendedScintilla::reloadSettings()
145152
}
146153
void ExtendedScintilla::reloadLexerSettings(QsciLexer *lexer)
147154
{
155+
QColor foreground (Settings::getValue("syntaxhighlighter", "foreground_colour").toString());
156+
QColor background (Settings::getValue("syntaxhighlighter", "background_colour").toString());
157+
158+
QFont defaultfont(Settings::getValue("editor", "font").toString());
159+
defaultfont.setStyleHint(QFont::TypeWriter);
160+
defaultfont.setPointSize(Settings::getValue("editor", "fontsize").toInt());
161+
148162
// Set syntax highlighting settings
149163
if(lexer)
150164
{
151-
QFont defaultfont(Settings::getValue("editor", "font").toString());
152-
defaultfont.setStyleHint(QFont::TypeWriter);
153-
defaultfont.setPointSize(Settings::getValue("editor", "fontsize").toInt());
154165
lexer->setFont(defaultfont);
155166

156-
lexer->setDefaultColor(QColor(Settings::getValue("syntaxhighlighter", "foreground_colour").toString()));
157-
lexer->setPaper(QColor(Settings::getValue("syntaxhighlighter", "background_colour").toString()));
167+
lexer->setDefaultPaper(background);
168+
lexer->setDefaultColor(foreground);
169+
170+
// This sets the base colors for all the styles
171+
lexer->setPaper(background);
172+
lexer->setColor(foreground);
158173
}
159174

160175
// Set font
161-
QFont font(Settings::getValue("editor", "font").toString());
162-
font.setStyleHint(QFont::TypeWriter);
163-
font.setPointSize(Settings::getValue("editor", "fontsize").toInt());
164-
setFont(font);
176+
setFont(defaultfont);
165177

166178
// Show line numbers
167-
setMarginsFont(font);
179+
setMarginsFont(defaultfont);
168180
setMarginLineNumbers(0, true);
169181
updateLineNumberAreaWidth();
170182

171183
// Highlight current line
172184
setCaretLineVisible(true);
173185
setCaretLineBackgroundColor(QColor(Settings::getValue("syntaxhighlighter", "currentline_colour").toString()));
174-
setCaretForegroundColor(QColor(Settings::getValue("syntaxhighlighter", "foreground_colour").toString()));
186+
setCaretForegroundColor(foreground);
175187

176188
// Set tab width
177189
setTabWidth(Settings::getValue("editor", "tabsize").toInt());

src/PreferencesDialog.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ PreferencesDialog::PreferencesDialog(QWidget* parent, Tabs tab)
4141

4242
loadSettings();
4343

44+
connect(ui->appStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(adjustColorsToStyle(int)));
45+
4446
// Avoid different heights due to having check boxes or not
4547
ui->treeSyntaxHighlighting->setUniformRowHeights(true);
4648

@@ -523,6 +525,26 @@ void PreferencesDialog::saveColorSetting(QFrame *frame, const QString & settingN
523525
frame->palette().color(frame->backgroundRole()));
524526
}
525527

528+
void PreferencesDialog::adjustColorsToStyle(int style)
529+
{
530+
Settings::AppStyle appStyle = static_cast<Settings::AppStyle>(style);
531+
setColorSetting(ui->fr_null_fg, Settings::getDefaultColorValue("databrowser", "null_fg_colour", appStyle));
532+
setColorSetting(ui->fr_null_bg, Settings::getDefaultColorValue("databrowser", "null_bg_colour", appStyle));
533+
setColorSetting(ui->fr_bin_fg, Settings::getDefaultColorValue("databrowser", "bin_fg_colour", appStyle));
534+
setColorSetting(ui->fr_bin_bg, Settings::getDefaultColorValue("databrowser", "bin_bg_colour", appStyle));
535+
setColorSetting(ui->fr_reg_fg, Settings::getDefaultColorValue("databrowser", "reg_fg_colour", appStyle));
536+
setColorSetting(ui->fr_reg_bg, Settings::getDefaultColorValue("databrowser", "reg_bg_colour", appStyle));
537+
538+
for(int i=0; i < ui->treeSyntaxHighlighting->topLevelItemCount(); ++i)
539+
{
540+
QString name = ui->treeSyntaxHighlighting->topLevelItem(i)->text(0);
541+
QColor color = Settings::getDefaultColorValue("syntaxhighlighter", name + "_colour", appStyle);
542+
ui->treeSyntaxHighlighting->topLevelItem(i)->setTextColor(2, color);
543+
ui->treeSyntaxHighlighting->topLevelItem(i)->setBackgroundColor(2, color);
544+
ui->treeSyntaxHighlighting->topLevelItem(i)->setText(2, color.name());
545+
}
546+
}
547+
526548
void PreferencesDialog::activateRemoteTab(bool active)
527549
{
528550
ui->tabWidget->setTabEnabled(ui->tabWidget->indexOf(ui->tabRemote), active);

src/PreferencesDialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ private slots:
4545
void removeClientCertificate();
4646
void chooseRemoteCloneDirectory();
4747
void updatePreviewFont();
48+
void adjustColorsToStyle(int style);
4849

4950
void on_buttonManageFileExtension_clicked();
5051
void on_buttonBox_clicked(QAbstractButton* button);

src/PreferencesDialog.ui

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@
509509
<verstretch>0</verstretch>
510510
</sizepolicy>
511511
</property>
512+
<property name="toolTip">
513+
<string>When this value is changed, all the other color preferences are also set to matching colors.</string>
514+
</property>
512515
<property name="currentIndex">
513516
<number>0</number>
514517
</property>
@@ -873,6 +876,9 @@ Can be set to 0 for disabling completion.</string>
873876
<property name="focusPolicy">
874877
<enum>Qt::StrongFocus</enum>
875878
</property>
879+
<property name="toolTip">
880+
<string>Click to set this color</string>
881+
</property>
876882
<property name="autoFillBackground">
877883
<bool>true</bool>
878884
</property>
@@ -899,6 +905,9 @@ Can be set to 0 for disabling completion.</string>
899905
<property name="focusPolicy">
900906
<enum>Qt::StrongFocus</enum>
901907
</property>
908+
<property name="toolTip">
909+
<string>Click to set this color</string>
910+
</property>
902911
<property name="autoFillBackground">
903912
<bool>true</bool>
904913
</property>
@@ -925,6 +934,9 @@ Can be set to 0 for disabling completion.</string>
925934
<property name="focusPolicy">
926935
<enum>Qt::StrongFocus</enum>
927936
</property>
937+
<property name="toolTip">
938+
<string>Click to set this color</string>
939+
</property>
928940
<property name="autoFillBackground">
929941
<bool>true</bool>
930942
</property>
@@ -941,6 +953,9 @@ Can be set to 0 for disabling completion.</string>
941953
<property name="focusPolicy">
942954
<enum>Qt::StrongFocus</enum>
943955
</property>
956+
<property name="toolTip">
957+
<string>Click to set this color</string>
958+
</property>
944959
<property name="autoFillBackground">
945960
<bool>true</bool>
946961
</property>
@@ -957,6 +972,9 @@ Can be set to 0 for disabling completion.</string>
957972
<property name="focusPolicy">
958973
<enum>Qt::StrongFocus</enum>
959974
</property>
975+
<property name="toolTip">
976+
<string>Click to set this color</string>
977+
</property>
960978
<property name="autoFillBackground">
961979
<bool>true</bool>
962980
</property>
@@ -973,6 +991,9 @@ Can be set to 0 for disabling completion.</string>
973991
<property name="focusPolicy">
974992
<enum>Qt::StrongFocus</enum>
975993
</property>
994+
<property name="toolTip">
995+
<string>Click to set this color</string>
996+
</property>
976997
<property name="autoFillBackground">
977998
<bool>true</bool>
978999
</property>

src/Settings.cpp

Lines changed: 100 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,8 @@ QVariant Settings::getDefaultValue(const QString& group, const QString& name)
215215
return "\\";
216216
if(name == "filter_delay")
217217
return 200;
218-
if(name == "null_fg_colour")
219-
return QColor(Qt::lightGray).name();
220-
if(name == "null_bg_colour")
221-
return QPalette().color(QPalette::Active, QPalette::Base).name();
222-
if(name == "reg_fg_colour")
223-
return QPalette().color(QPalette::Active, QPalette::Text).name();
224-
if(name == "reg_bg_colour")
225-
return QPalette().color(QPalette::Active, QPalette::Base).name();
226-
if(name == "bin_fg_colour")
227-
return QColor(Qt::lightGray).name();
228-
if(name == "bin_bg_colour")
229-
return QPalette().color(QPalette::Active, QPalette::Base).name();
218+
if(name.right(6) == "colour")
219+
return getDefaultColorValue(group, name, FollowDesktopStyle);
230220
}
231221

232222
// syntaxhighlighter?
@@ -246,52 +236,7 @@ QVariant Settings::getDefaultValue(const QString& group, const QString& name)
246236

247237
// Colour?
248238
if(name.right(6) == "colour")
249-
{
250-
QColor backgroundColour = QPalette().color(QPalette::Active, QPalette::Base);
251-
QColor foregroundColour = QPalette().color(QPalette::Active, QPalette::Text);
252-
253-
if(name == "foreground_colour")
254-
return foregroundColour.name();
255-
else if(name == "background_colour")
256-
return backgroundColour.name();
257-
258-
// Detect and provide sensible defaults for dark themes
259-
if (backgroundColour.value() < foregroundColour.value()) {
260-
if(name == "keyword_colour")
261-
return QColor(82, 148, 226).name();
262-
else if(name == "function_colour")
263-
return QColor(Qt::yellow).name();
264-
else if(name == "table_colour")
265-
return QColor(Qt::cyan).name();
266-
else if(name == "comment_colour")
267-
return QColor(Qt::green).name();
268-
else if(name == "identifier_colour")
269-
return QColor(Qt::magenta).name();
270-
else if(name == "string_colour")
271-
return QColor(Qt::lightGray).name();
272-
else if(name == "currentline_colour")
273-
return backgroundColour.lighter(150).name();
274-
else if(name == "background_colour")
275-
return backgroundColour.name();
276-
} else {
277-
if(name == "keyword_colour")
278-
return QColor(Qt::darkBlue).name();
279-
else if(name == "function_colour")
280-
return QColor(Qt::blue).name();
281-
else if(name == "table_colour")
282-
return QColor(Qt::darkCyan).name();
283-
else if(name == "comment_colour")
284-
return QColor(Qt::darkGreen).name();
285-
else if(name == "identifier_colour")
286-
return QColor(Qt::darkMagenta).name();
287-
else if(name == "string_colour")
288-
return QColor(Qt::red).name();
289-
else if(name == "currentline_colour")
290-
return QColor(236, 236, 245).name();
291-
else if(name == "background_colour")
292-
return backgroundColour.name();
293-
}
294-
}
239+
return getDefaultColorValue(group, name, FollowDesktopStyle);
295240
}
296241

297242
// editor/font?
@@ -398,6 +343,103 @@ QVariant Settings::getDefaultValue(const QString& group, const QString& name)
398343
return QVariant();
399344
}
400345

346+
QColor Settings::getDefaultColorValue(const QString& group, const QString& name, AppStyle style)
347+
{
348+
// Data Browser/NULL & Binary Fields
349+
if(group == "databrowser")
350+
{
351+
switch (style) {
352+
case FollowDesktopStyle :
353+
if(name == "null_fg_colour")
354+
return QColor(Qt::lightGray).name();
355+
if(name == "null_bg_colour")
356+
return QPalette().color(QPalette::Active, QPalette::Base).name();
357+
if(name == "reg_fg_colour")
358+
return QPalette().color(QPalette::Active, QPalette::Text).name();
359+
if(name == "reg_bg_colour")
360+
return QPalette().color(QPalette::Active, QPalette::Base).name();
361+
if(name == "bin_fg_colour")
362+
return QColor(Qt::lightGray).name();
363+
if(name == "bin_bg_colour")
364+
return QPalette().color(QPalette::Active, QPalette::Base).name();
365+
case DarkStyle :
366+
if(name == "null_fg_colour")
367+
return QColor("#787878");
368+
if(name == "null_bg_colour")
369+
return QColor("#19232D");
370+
if(name == "reg_fg_colour")
371+
return QColor("#F0F0F0");
372+
if(name == "reg_bg_colour")
373+
return QColor("#19232D");
374+
if(name == "bin_fg_colour")
375+
return QColor("#787878");
376+
if(name == "bin_bg_colour")
377+
return QColor("#19232D");
378+
}
379+
}
380+
381+
// syntaxhighlighter?
382+
if(group == "syntaxhighlighter")
383+
{
384+
// Colour?
385+
if(name.right(6) == "colour")
386+
{
387+
QColor backgroundColour;
388+
QColor foregroundColour;
389+
switch (style) {
390+
case FollowDesktopStyle :
391+
backgroundColour = QPalette().color(QPalette::Active, QPalette::Base);
392+
foregroundColour = QPalette().color(QPalette::Active, QPalette::Text);
393+
break;
394+
case DarkStyle :
395+
foregroundColour = QColor("#F0F0F0");
396+
backgroundColour = QColor("#19232D");
397+
break;
398+
}
399+
if(name == "foreground_colour")
400+
return foregroundColour;
401+
else if(name == "background_colour")
402+
return backgroundColour;
403+
404+
// Detect and provide sensible defaults for dark themes
405+
if (backgroundColour.value() < foregroundColour.value()) {
406+
if(name == "keyword_colour")
407+
return QColor(82, 148, 226);
408+
else if(name == "function_colour")
409+
return QColor(Qt::yellow);
410+
else if(name == "table_colour")
411+
return QColor(Qt::cyan);
412+
else if(name == "comment_colour")
413+
return QColor(Qt::green);
414+
else if(name == "identifier_colour")
415+
return QColor(Qt::magenta);
416+
else if(name == "string_colour")
417+
return QColor(Qt::lightGray);
418+
else if(name == "currentline_colour")
419+
return backgroundColour.lighter(150);
420+
} else {
421+
if(name == "keyword_colour")
422+
return QColor(Qt::darkBlue);
423+
else if(name == "function_colour")
424+
return QColor(Qt::blue);
425+
else if(name == "table_colour")
426+
return QColor(Qt::darkCyan);
427+
else if(name == "comment_colour")
428+
return QColor(Qt::darkGreen);
429+
else if(name == "identifier_colour")
430+
return QColor(Qt::darkMagenta);
431+
else if(name == "string_colour")
432+
return QColor(Qt::red);
433+
else if(name == "currentline_colour")
434+
return QColor(236, 236, 245);
435+
}
436+
}
437+
}
438+
439+
// Unknown combination of group and name? Return an invalid QColor!
440+
return QColor();
441+
}
442+
401443
void Settings::restoreDefaults ()
402444
{
403445
QSettings settings(QApplication::organizationName(), QApplication::organizationName());

src/Settings.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ class Settings
2121
private:
2222
Settings() { } // class is fully static
2323

24-
// This works similar to getSettingsValue but returns the default value instead of the value set by the user
24+
// This works similar to getValue but returns the default value instead of the value set by the user
2525
static QVariant getDefaultValue(const QString& group, const QString& name);
2626

27+
// This works similar to getDefaultValue but returns the default color value based on the passed application style
28+
// instead of the current palette.
29+
static QColor getDefaultColorValue(const QString& group, const QString& name, AppStyle style);
30+
2731
// Cache for storing the settings to avoid repeatedly reading the settings file all the time
2832
static QHash<QString, QVariant> m_hCache;
2933
};

0 commit comments

Comments
 (0)