Cppcheck
codeeditorstyle.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "codeeditorstyle.h"
20 
21 #include <QSettings>
22 #include <QStringList>
23 #include <QVariant>
24 
26  // cppcheck-suppress naming-varname - TODO: fix this
27  QColor CtrlFGColor, QColor CtrlBGColor,
28  // cppcheck-suppress naming-varname - TODO: fix this
29  QColor HiLiBGColor,
30  // cppcheck-suppress naming-varname - TODO: fix this
31  QColor LnNumFGColor, QColor LnNumBGColor,
32  // cppcheck-suppress naming-varname - TODO: fix this
33  QColor KeyWdFGColor, QFont::Weight KeyWdWeight,
34  // cppcheck-suppress naming-varname - TODO: fix this
35  QColor ClsFGColor, QFont::Weight ClsWeight,
36  // cppcheck-suppress naming-varname - TODO: fix this
37  QColor QteFGColor, QFont::Weight QteWeight,
38  // cppcheck-suppress naming-varname - TODO: fix this
39  QColor CmtFGColor, QFont::Weight CmtWeight,
40  // cppcheck-suppress naming-varname - TODO: fix this
41  QColor SymbFGColor, QColor SymbBGColor,
42  // cppcheck-suppress naming-varname - TODO: fix this
43  QFont::Weight SymbWeight) :
44  widgetFGColor(CtrlFGColor),
45  widgetBGColor(CtrlBGColor),
46  highlightBGColor(HiLiBGColor),
47  lineNumFGColor(LnNumFGColor),
48  lineNumBGColor(LnNumBGColor),
49  keywordColor(KeyWdFGColor),
50  keywordWeight(KeyWdWeight),
51  classColor(ClsFGColor),
52  classWeight(ClsWeight),
53  quoteColor(QteFGColor),
54  quoteWeight(QteWeight),
55  commentColor(CmtFGColor),
56  commentWeight(CmtWeight),
57  symbolFGColor(SymbFGColor),
58  symbolBGColor(SymbBGColor),
59  symbolWeight(SymbWeight)
60 {}
61 
63 {
64  if (mSystemTheme != rhs.mSystemTheme) return false;
65  if (widgetFGColor != rhs.widgetFGColor) return false;
66  if (widgetBGColor != rhs.widgetBGColor) return false;
67  if (highlightBGColor != rhs.highlightBGColor) return false;
68  if (lineNumFGColor != rhs.lineNumFGColor) return false;
69  if (lineNumBGColor != rhs.lineNumBGColor) return false;
70  if (keywordColor != rhs.keywordColor) return false;
71  if (keywordWeight != rhs.keywordWeight) return false;
72  if (classColor != rhs.classColor) return false;
73  if (classWeight != rhs.classWeight) return false;
74  if (quoteColor != rhs.quoteColor) return false;
75  if (quoteWeight != rhs.quoteWeight) return false;
76  if (commentColor != rhs.commentColor) return false;
77  if (commentWeight != rhs.commentWeight) return false;
78  if (symbolFGColor != rhs.symbolFGColor) return false;
79  if (symbolBGColor != rhs.symbolBGColor) return false;
80  if (symbolWeight != rhs.symbolWeight) return false;
81  return true;
82 }
83 
85 {
86  return !(*this == rhs);
87 }
88 
90 {
92  theStyle.mSystemTheme = true;
93  return theStyle;
94 }
95 
97 {
99  if (!settings)
100  return theStyle;
101 
102  if (!settings->childGroups().contains(SETTINGS_STYLE_GROUP))
103  return theStyle;
104 
105  // style section exists - load values
106  settings->beginGroup(SETTINGS_STYLE_GROUP);
107  QString type = settings->value(
109  QVariant(SETTINGS_STYLE_TYPE_LIGHT)
110  ).toString();
111  if (type == SETTINGS_STYLE_TYPE_LIGHT) {
112  settings->endGroup();
113  return theStyle;
114  }
115  if (type == SETTINGS_STYLE_TYPE_DARK) {
116  theStyle = defaultStyleDark;
117  settings->endGroup();
118  return theStyle;
119  }
120  if (type == SETTINGS_STYLE_TYPE_CUSTOM) {
121  theStyle.widgetFGColor = settings->value(
123  QVariant(defaultStyleLight.widgetFGColor)).value<QColor>();
124  theStyle.widgetBGColor = settings->value(
126  QVariant(defaultStyleLight.widgetBGColor)).value<QColor>();
127  theStyle.highlightBGColor = settings->value(
129  QVariant(defaultStyleLight.highlightBGColor)).value<QColor>();
130  theStyle.lineNumFGColor = settings->value(
132  QVariant(defaultStyleLight.lineNumFGColor)).value<QColor>();
133  theStyle.lineNumBGColor = settings->value(
135  QVariant(defaultStyleLight.lineNumBGColor)).value<QColor>();
136  theStyle.keywordColor = settings->value(
138  QVariant(defaultStyleLight.keywordColor)).value<QColor>();
139  QVariant defKeyWWt(static_cast<int>(defaultStyleLight.keywordWeight));
140  theStyle.keywordWeight = static_cast<QFont::Weight>(
141  settings->value(SETTINGS_STYLE_KEYWORDWT, defKeyWWt).toInt());
142  theStyle.classColor = settings->value(
144  QVariant(defaultStyleLight.classColor)).value<QColor>();
145  QVariant defClsWt(static_cast<int>(defaultStyleLight.classWeight));
146  theStyle.classWeight = static_cast<QFont::Weight>(
147  settings->value(SETTINGS_STYLE_CLASSWT, defClsWt).toInt());
148  theStyle.quoteColor = settings->value(
150  QVariant(defaultStyleLight.quoteColor)).value<QColor>();
151  QVariant defQteWt(static_cast<int>(defaultStyleLight.quoteWeight));
152  theStyle.quoteWeight = static_cast<QFont::Weight>(
153  settings->value(SETTINGS_STYLE_QUOTEWT, defQteWt).toInt());
154  theStyle.commentColor = settings->value(
156  QVariant(defaultStyleLight.commentColor)).value<QColor>();
157  QVariant defCmtWt(static_cast<int>(defaultStyleLight.commentWeight));
158  theStyle.commentWeight = static_cast<QFont::Weight>(
159  settings->value(SETTINGS_STYLE_COMMENTWT, defCmtWt).toInt());
160  theStyle.symbolFGColor = settings->value(
162  QVariant(defaultStyleLight.symbolFGColor)).value<QColor>();
163  theStyle.symbolBGColor = settings->value(
165  QVariant(defaultStyleLight.symbolBGColor)).value<QColor>();
166  QVariant defSymWt(static_cast<int>(defaultStyleLight.symbolWeight));
167  theStyle.symbolWeight = static_cast<QFont::Weight>(
168  settings->value(SETTINGS_STYLE_SYMBOLWT, defSymWt).toInt());
169  }
170  settings->endGroup();
171  return theStyle;
172 }
173 
174 void CodeEditorStyle::saveSettings(QSettings *settings,
175  const CodeEditorStyle& theStyle)
176 {
177  if (!settings)
178  return;
179 
180  if (settings->childGroups().contains(SETTINGS_STYLE_GROUP)) {
181  settings->remove(SETTINGS_STYLE_GROUP);
182  if (theStyle.isSystemTheme())
183  return;
184  }
185 
186  settings->beginGroup(SETTINGS_STYLE_GROUP);
187  const bool isDefaultLight = (defaultStyleLight == theStyle);
188  const bool isDefaultDark = (defaultStyleDark == theStyle);
189  if (isDefaultLight && !isDefaultDark) {
190  settings->setValue(SETTINGS_STYLE_TYPE,
192  } else if (!isDefaultLight && isDefaultDark) {
193  settings->setValue(SETTINGS_STYLE_TYPE,
195  } else {
196  settings->setValue(SETTINGS_STYLE_TYPE,
198  settings->setValue(SETTINGS_STYLE_WIDGETFG,
199  QVariant(theStyle.widgetFGColor));
200  settings->setValue(SETTINGS_STYLE_WIDGETBG,
201  QVariant(theStyle.widgetBGColor));
202  settings->setValue(SETTINGS_STYLE_HILIFG,
203  QVariant(theStyle.highlightBGColor));
204  settings->setValue(SETTINGS_STYLE_LINENUMFG,
205  QVariant(theStyle.lineNumFGColor));
206  settings->setValue(SETTINGS_STYLE_LINENUMBG,
207  QVariant(theStyle.lineNumBGColor));
208  settings->setValue(SETTINGS_STYLE_KEYWORDFG,
209  QVariant(theStyle.keywordColor));
210  settings->setValue(SETTINGS_STYLE_KEYWORDWT,
211  QVariant(static_cast<int>(theStyle.keywordWeight)));
212  settings->setValue(SETTINGS_STYLE_CLASSFG,
213  QVariant(theStyle.classColor));
214  settings->setValue(SETTINGS_STYLE_CLASSWT,
215  QVariant(static_cast<int>(theStyle.classWeight)));
216  settings->setValue(SETTINGS_STYLE_QUOTEFG,
217  QVariant(theStyle.quoteColor));
218  settings->setValue(SETTINGS_STYLE_QUOTEWT,
219  QVariant(static_cast<int>(theStyle.quoteWeight)));
220  settings->setValue(SETTINGS_STYLE_COMMENTFG,
221  QVariant(theStyle.commentColor));
222  settings->setValue(SETTINGS_STYLE_COMMENTWT,
223  QVariant(static_cast<int>(theStyle.commentWeight)));
224  settings->setValue(SETTINGS_STYLE_SYMBOLFG,
225  QVariant(theStyle.symbolFGColor));
226  settings->setValue(SETTINGS_STYLE_SYMBOLBG,
227  QVariant(theStyle.symbolBGColor));
228  settings->setValue(SETTINGS_STYLE_SYMBOLWT,
229  QVariant(static_cast<int>(theStyle.symbolWeight)));
230  }
231  settings->endGroup();
232 }
static void saveSettings(QSettings *settings, const CodeEditorStyle &theStyle)
QFont::Weight classWeight
QFont::Weight commentWeight
static CodeEditorStyle getSystemTheme()
QFont::Weight quoteWeight
static CodeEditorStyle loadSettings(QSettings *settings)
QFont::Weight symbolWeight
bool isSystemTheme() const
bool operator==(const CodeEditorStyle &rhs) const
bool operator!=(const CodeEditorStyle &rhs) const
CodeEditorStyle(QColor CtrlFGColor, QColor CtrlBGColor, QColor HiLiBGColor, QColor LnNumFGColor, QColor LnNumBGColor, QColor KeyWdFGColor, QFont::Weight KeyWdWeight, QColor ClsFGColor, QFont::Weight ClsWeight, QColor QteFGColor, QFont::Weight QteWeight, QColor CmtFGColor, QFont::Weight CmtWeight, QColor SymbFGColor, QColor SymbBGColor, QFont::Weight SymbWeight)
QFont::Weight keywordWeight
const QString SETTINGS_STYLE_SYMBOLFG("StyleSymbolFG")
const QString SETTINGS_STYLE_GROUP("EditorStyle")
const QString SETTINGS_STYLE_QUOTEFG("StyleQuoteFG")
const QString SETTINGS_STYLE_KEYWORDFG("StyleKeywordFG")
const QString SETTINGS_STYLE_TYPE_CUSTOM("Custom")
const QString SETTINGS_STYLE_LINENUMFG("StyleLineNumFG")
const QString SETTINGS_STYLE_QUOTEWT("StyleQuoteWeight")
const QString SETTINGS_STYLE_KEYWORDWT("StyleKeywordWeight")
const QString SETTINGS_STYLE_TYPE("StyleType")
const QString SETTINGS_STYLE_TYPE_DARK("DefaultDark")
const QString SETTINGS_STYLE_SYMBOLWT("StyleSymbolWeight")
const QString SETTINGS_STYLE_HILIFG("StyleHighlightFG")
const QString SETTINGS_STYLE_LINENUMBG("StyleLineNumBG")
const QString SETTINGS_STYLE_TYPE_LIGHT("DefaultLight")
const QString SETTINGS_STYLE_SYMBOLBG("StyleSymbolBG")
static const CodeEditorStyle defaultStyleLight(Qt::black, QColor(240, 240, 240), QColor(255, 220, 220), Qt::black, QColor(240, 240, 240), Qt::darkBlue, QFont::Bold, Qt::darkMagenta, QFont::Bold, Qt::darkGreen, QFont::Normal, Qt::gray, QFont::Normal, Qt::red, QColor(220, 220, 255), QFont::Normal)
const QString SETTINGS_STYLE_WIDGETBG("StyleWidgetBG")
const QString SETTINGS_STYLE_CLASSWT("StyleClassWeight")
const QString SETTINGS_STYLE_CLASSFG("StyleClassFG")
const QString SETTINGS_STYLE_COMMENTFG("StyleCommentFG")
const QString SETTINGS_STYLE_WIDGETFG("StyleWidgetFG")
const QString SETTINGS_STYLE_COMMENTWT("StyleCommentWeight")
static const CodeEditorStyle defaultStyleDark(QColor(218, 218, 218), QColor(16, 16, 32), QColor(64, 64, 64), QColor(43, 145, 175), QColor(16, 16, 32), QColor(0, 204, 204), QFont::Bold, QColor(218, 0, 218), QFont::Bold, QColor(0, 204, 0), QFont::Normal, QColor(180, 180, 180), QFont::Normal, QColor(218, 32, 32), QColor(32, 32, 108), QFont::Normal)