Cppcheck
codeeditstyledialog.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 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 "codeeditstyledialog.h"
20 
21 #include "codeeditor.h"
22 #include "codeeditstylecontrols.h"
23 
24 #include <QDialogButtonBox>
25 #include <QFlags>
26 #include <QFontMetrics>
27 #include <QFormLayout>
28 #include <QHBoxLayout>
29 #include <QPushButton>
30 #include <QStringList>
31 #include <QVBoxLayout>
32 #include <QtGlobal>
33 
34 class QWidget;
35 
37  "/*****\n"
38  "* Multiline Comment\n"
39  "*****/\n"
40  "#include <QApplication>\n"
41  "#include <iostream>\n"
42  "\n"
43  "class fwdClass;\n"
44  "\n"
45  "int main(int argc, char *argv[])\n"
46  "{\n"
47  " QApplication a(argc, argv);\n"
48  " int nLife = 42;\n"
49  " w.show();\n"
50  " // single line comment\n"
51  " // line below is highlighted\n"
52  " fwdClass( nLife );\n"
53  " return a.exec();\n"
54  "}\n"
55  "\n"
56  "void class fwdClass( double dValue ) {\n"
57  " std::cout << \"Ipsum Lorem: \"\n"
58  " << nValue\n"
59  " << std::endl;\n"
60  "}\n");
61 
62 const QStringList StyleEditDialog::mErrSymbolsList = (
63  QStringList(QStringList()
64  << "nLife"
65  << "dValue"
66  << "nValue"));
67 const int StyleEditDialog::mErrLineNum = 16;
68 
70  QWidget *parent /*= nullptr*/) :
71  QDialog(parent),
72  mStyleIncoming(newStyle),
73  mStyleOutgoing(newStyle)
74 {
75  auto *vboxMain = new QVBoxLayout(this);
76  auto *hboxEdit = new QHBoxLayout();
77  // Color/Weight controls
78  auto *flEditControls = new QFormLayout();
80  flEditControls->addRow(QObject::tr("Editor Foreground Color"),
83  flEditControls->addRow(QObject::tr("Editor Background Color"),
86  flEditControls->addRow(QObject::tr("Highlight Background Color"),
88  mBtnLineNumFG = new SelectColorButton(this);
89  flEditControls->addRow(QObject::tr("Line Number Foreground Color"),
91  mBtnLineNumBG = new SelectColorButton(this);
92  flEditControls->addRow(QObject::tr("Line Number Background Color"),
94  mBtnKeywordFG = new SelectColorButton(this);
95  flEditControls->addRow(QObject::tr("Keyword Foreground Color"),
98  flEditControls->addRow(QObject::tr("Keyword Font Weight"),
100  mBtnClassFG = new SelectColorButton(this);
101  flEditControls->addRow(QObject::tr("Class Foreground Color"),
102  mBtnClassFG);
104  flEditControls->addRow(QObject::tr("Class Font Weight"),
106  mBtnQuoteFG = new SelectColorButton(this);
107  flEditControls->addRow(QObject::tr("Quote Foreground Color"),
108  mBtnQuoteFG);
110  flEditControls->addRow(QObject::tr("Quote Font Weight"),
112  mBtnCommentFG = new SelectColorButton(this);
113  flEditControls->addRow(QObject::tr("Comment Foreground Color"),
114  mBtnCommentFG);
116  flEditControls->addRow(QObject::tr("Comment Font Weight"),
118  mBtnSymbolFG = new SelectColorButton(this);
119  flEditControls->addRow(QObject::tr("Symbol Foreground Color"),
120  mBtnSymbolFG);
121  mBtnSymbolBG = new SelectColorButton(this);
122  flEditControls->addRow(QObject::tr("Symbol Background Color"),
123  mBtnSymbolBG);
125  flEditControls->addRow(QObject::tr("Symbol Font Weight"),
127  hboxEdit->addLayout(flEditControls);
128  // CodeEditor to display Style
129  mSampleEditor = new CodeEditor(this);
130  QFont sampleFont("Monospace");
131  QFontMetrics fm(sampleFont);
132 #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
133  mSampleEditor->setMinimumWidth(fm.horizontalAdvance(QString(40, 'W')));
134 #else
135  mSampleEditor->setMinimumWidth(fm.width(QString(40, 'W')));
136 #endif
137  // designate highlight, errors, and symbols
139  // End Controls
140  hboxEdit->addWidget(mSampleEditor);
141  vboxMain->addLayout(hboxEdit);
142 
143  // Default Controls
144  auto *hboxDefaultControls = new QHBoxLayout();
145  mBtnDefaultLight = new QPushButton(QObject::tr("Set to Default Light"),
146  this);
147  mBtnDefaultDark = new QPushButton(QObject::tr("Set to Default Dark"),
148  this);
149  hboxDefaultControls->addStretch(1);
150  hboxDefaultControls->addWidget(mBtnDefaultLight);
151  hboxDefaultControls->addWidget(mBtnDefaultDark);
152  hboxDefaultControls->addStretch(1);
153  vboxMain->addLayout(hboxDefaultControls);
154  vboxMain->addStretch(2);
155  // dialog controls
156  auto *dBtnBox = new QDialogButtonBox(
157  QDialogButtonBox::Cancel |
158  QDialogButtonBox::Ok |
159  QDialogButtonBox::Reset);
160  vboxMain->addStretch(1);
161  vboxMain->addWidget(dBtnBox);
162 
163  // setup values for style controls
164  updateControls();
165  updateStyle();
166 
167  connect(dBtnBox, SIGNAL(accepted()), this, SLOT(accept()));
168  connect(dBtnBox, SIGNAL(rejected()), this, SLOT(reject()));
169  connect(dBtnBox->button(QDialogButtonBox::Reset), SIGNAL(clicked()),
170  this, SLOT(resetStyle()));
171  connect(mBtnDefaultLight, SIGNAL(clicked()),
172  this, SLOT(setStyleDefaultLight()));
173  connect(mBtnDefaultDark, SIGNAL(clicked()),
174  this, SLOT(setStyleDefaultDark()));
175  connect(mBtnWidgetColorFG, SIGNAL(colorChanged(QColor)),
176  this, SLOT(colorChangedWidgetFG(QColor)));
177  connect(mBtnWidgetColorBG, SIGNAL(colorChanged(QColor)),
178  this, SLOT(colorChangedWidgetBG(QColor)));
179  connect(mBtnHighlightBG, SIGNAL(colorChanged(QColor)),
180  this, SLOT(colorChangedHighlightBG(QColor)));
181  connect(mBtnLineNumFG, SIGNAL(colorChanged(QColor)),
182  this, SLOT(colorChangedLineNumFG(QColor)));
183  connect(mBtnLineNumBG, SIGNAL(colorChanged(QColor)),
184  this, SLOT(colorChangedLineNumBG(QColor)));
185  connect(mBtnKeywordFG, SIGNAL(colorChanged(QColor)),
186  this, SLOT(colorChangedKeywordFG(QColor)));
187  connect(mCBKeywordWeight, SIGNAL(weightChanged(QFont::Weight)),
188  this, SLOT(weightChangedKeyword(QFont::Weight)));
189  connect(mBtnClassFG, SIGNAL(colorChanged(QColor)),
190  this, SLOT(colorChangedClassFG(QColor)));
191  connect(mCBClassWeight, SIGNAL(weightChanged(QFont::Weight)),
192  this, SLOT(weightChangedClass(QFont::Weight)));
193  connect(mBtnQuoteFG, SIGNAL(colorChanged(QColor)),
194  this, SLOT(colorChangedQuoteFG(QColor)));
195  connect(mCBQuoteWeight, SIGNAL(weightChanged(QFont::Weight)),
196  this, SLOT(weightChangedQuote(QFont::Weight)));
197  connect(mBtnCommentFG, SIGNAL(colorChanged(QColor)),
198  this, SLOT(colorChangedCommentFG(QColor)));
199  connect(mCBCommentWeight, SIGNAL(weightChanged(QFont::Weight)),
200  this, SLOT(weightChangedComment(QFont::Weight)));
201  connect(mBtnSymbolFG, SIGNAL(colorChanged(QColor)),
202  this, SLOT(colorChangedSymbolFG(QColor)));
203  connect(mBtnSymbolBG, SIGNAL(colorChanged(QColor)),
204  this, SLOT(colorChangedSymbolBG(QColor)));
205  connect(mCBSymbolWeight, SIGNAL(weightChanged(QFont::Weight)),
206  this, SLOT(weightChangedSymbol(QFont::Weight)));
207 }
208 
210 {
227 }
228 
230 {
233  // set Editor Styling
235 }
236 
238 {
239  return mStyleOutgoing;
240 }
241 
243 {
245  updateControls();
246  updateStyle();
247 }
248 
250 {
252  updateControls();
253  updateStyle();
254 }
255 
257 {
259  updateControls();
260  updateStyle();
261 }
262 
263 void StyleEditDialog::colorChangedWidgetFG(const QColor& newColor)
264 {
265  mStyleOutgoing.widgetFGColor = newColor;
266  updateStyle();
267 }
268 
269 void StyleEditDialog::colorChangedWidgetBG(const QColor& newColor)
270 {
271  mStyleOutgoing.widgetBGColor = newColor;
272  updateStyle();
273 }
274 
275 void StyleEditDialog::colorChangedHighlightBG(const QColor& newColor)
276 {
277  mStyleOutgoing.highlightBGColor = newColor;
278  updateStyle();
279 }
280 
281 void StyleEditDialog::colorChangedLineNumFG(const QColor& newColor)
282 {
283  mStyleOutgoing.lineNumFGColor = newColor;
284  updateStyle();
285 }
286 
287 void StyleEditDialog::colorChangedLineNumBG(const QColor& newColor)
288 {
289  mStyleOutgoing.lineNumBGColor = newColor;
290  updateStyle();
291 }
292 
293 void StyleEditDialog::colorChangedKeywordFG(const QColor& newColor)
294 {
295  mStyleOutgoing.keywordColor = newColor;
296  updateStyle();
297 }
298 
299 void StyleEditDialog::weightChangedKeyword(QFont::Weight newWeight)
300 {
301  mStyleOutgoing.keywordWeight = newWeight;
302  updateStyle();
303 }
304 
305 void StyleEditDialog::colorChangedClassFG(const QColor& newColor)
306 {
307  mStyleOutgoing.classColor = newColor;
308  updateStyle();
309 }
310 
311 void StyleEditDialog::weightChangedClass(QFont::Weight newWeight)
312 {
313  mStyleOutgoing.classWeight = newWeight;
314  updateStyle();
315 }
316 
317 void StyleEditDialog::colorChangedQuoteFG(const QColor& newColor)
318 {
319  mStyleOutgoing.quoteColor = newColor;
320  updateStyle();
321 }
322 
323 void StyleEditDialog::weightChangedQuote(QFont::Weight newWeight)
324 {
325  mStyleOutgoing.quoteWeight = newWeight;
326  updateStyle();
327 }
328 
329 void StyleEditDialog::colorChangedCommentFG(const QColor& newColor)
330 {
331  mStyleOutgoing.commentColor = newColor;
332  updateStyle();
333 }
334 
335 void StyleEditDialog::weightChangedComment(QFont::Weight newWeight)
336 {
337  mStyleOutgoing.commentWeight = newWeight;
338  updateStyle();
339 }
340 
341 void StyleEditDialog::colorChangedSymbolFG(const QColor& newColor)
342 {
343  mStyleOutgoing.symbolFGColor = newColor;
344  updateStyle();
345 }
346 
347 void StyleEditDialog::colorChangedSymbolBG(const QColor& newColor)
348 {
349  mStyleOutgoing.symbolBGColor = newColor;
350  updateStyle();
351 }
352 
353 void StyleEditDialog::weightChangedSymbol(QFont::Weight newWeight)
354 {
355  mStyleOutgoing.symbolWeight = newWeight;
356  updateStyle();
357 }
QFont::Weight classWeight
QFont::Weight commentWeight
QFont::Weight quoteWeight
QFont::Weight symbolWeight
QFont::Weight keywordWeight
void setStyle(const CodeEditorStyle &newStyle)
Definition: codeeditor.cpp:325
void setError(const QString &code, int errorLine, const QStringList &symbols)
Set source code to show, goto error line and highlight that line.
Definition: codeeditor.cpp:335
void setColor(const QColor &color)
void setWeight(QFont::Weight weight)
CodeEditor * mSampleEditor
void colorChangedLineNumFG(const QColor &newColor)
SelectFontWeightCombo * mCBCommentWeight
SelectColorButton * mBtnLineNumBG
void colorChangedCommentFG(const QColor &newColor)
void colorChangedSymbolBG(const QColor &newColor)
static const QStringList mErrSymbolsList
SelectColorButton * mBtnQuoteFG
void colorChangedLineNumBG(const QColor &newColor)
void weightChangedClass(QFont::Weight newWeight)
void weightChangedComment(QFont::Weight newWeight)
QPushButton * mBtnDefaultLight
void colorChangedQuoteFG(const QColor &newColor)
SelectColorButton * mBtnKeywordFG
void weightChangedSymbol(QFont::Weight newWeight)
void colorChangedWidgetFG(const QColor &newColor)
SelectColorButton * mBtnLineNumFG
void colorChangedClassFG(const QColor &newColor)
void weightChangedKeyword(QFont::Weight newWeight)
CodeEditorStyle mStyleOutgoing
SelectColorButton * mBtnClassFG
static const int mErrLineNum
void weightChangedQuote(QFont::Weight newWeight)
SelectFontWeightCombo * mCBClassWeight
SelectColorButton * mBtnSymbolBG
void colorChangedWidgetBG(const QColor &newColor)
StyleEditDialog(const CodeEditorStyle &newStyle, QWidget *parent=nullptr)
SelectColorButton * mBtnSymbolFG
SelectColorButton * mBtnHighlightBG
void colorChangedHighlightBG(const QColor &newColor)
SelectFontWeightCombo * mCBQuoteWeight
SelectFontWeightCombo * mCBKeywordWeight
SelectColorButton * mBtnWidgetColorBG
QPushButton * mBtnDefaultDark
void colorChangedKeywordFG(const QColor &newColor)
SelectColorButton * mBtnCommentFG
SelectFontWeightCombo * mCBSymbolWeight
void colorChangedSymbolFG(const QColor &newColor)
static const QString mSampleDocument
SelectColorButton * mBtnWidgetColorFG
CodeEditorStyle mStyleIncoming
CodeEditorStyle getStyle()
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)
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)