forked from nuttyartist/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyleeditorwindow.cpp
More file actions
executable file
·407 lines (362 loc) · 16.4 KB
/
Copy pathstyleeditorwindow.cpp
File metadata and controls
executable file
·407 lines (362 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "styleeditorwindow.h"
#include "ui_styleeditorwindow.h"
#include <QDebug>
#include <QTimer>
#include <QShortcut>
/**
* Initializes the window components and configures the StyleEditorWindow
*/
StyleEditorWindow::StyleEditorWindow(QWidget *parent) :
QWidget(parent),
m_ui(new Ui::StyleEditorWindow),
m_currentlyClickedButton(Q_NULLPTR),
m_currentSelectedFontButton(Q_NULLPTR),
m_currentSelectedThemeButton(Q_NULLPTR),
m_isFullWidthClicked(false)
{
m_ui->setupUi(this);
this->setWindowTitle(tr("Editor Settings"));
this->setWindowFlags(Qt::Window | Qt::WindowStaysOnTopHint);
m_ui->serifButton->setToolTip("Change the text editor font to a serif font.\nClick again to try different fonts");
m_ui->sansSerifButton->setToolTip("Change the text editor font to a sans-serif font.\nClick again to try different fonts");
m_ui->monoButton->setToolTip("Change the text editor font to a monospace font.\nClick again to try different fonts");
m_ui->decreaseButton->setToolTip("Decrease font size");
m_ui->increaseButton->setToolTip("Increase font size");
m_ui->fullWidthButton->setToolTip("Make text stretch to the full width of the text editor");
m_ui->increaseWidthButton->setToolTip("Increase the text width by one character");
m_ui->decreaseWidthButton->setToolTip("Decrease the text width by one character");
m_ui->lightButton->setToolTip("Change app theme to Light");
m_ui->darkButton->setToolTip("Change app theme to Dark");
m_ui->sepiaButton->setToolTip("Chaneg app theme to Sepia");
m_ui->resetDefaultButton->setToolTip("Reset all to default settings");
connect(m_ui->serifButton, &QPushButton::clicked, this, [this]{emit StyleEditorWindow::changeFontType(FontTypeface::Serif); buttonClicked(m_ui->serifButton);});
connect(m_ui->sansSerifButton, &QPushButton::clicked, this, [this]{emit StyleEditorWindow::changeFontType(FontTypeface::SansSerif); buttonClicked(m_ui->sansSerifButton);});
connect(m_ui->monoButton, &QPushButton::clicked, this, [this]{emit StyleEditorWindow::changeFontType(FontTypeface::Mono); buttonClicked(m_ui->monoButton);});
connect(m_ui->decreaseButton, &QPushButton::clicked, this, [this]{emit StyleEditorWindow::changeFontSize(FontSizeAction::Decrease); buttonClicked(m_ui->decreaseButton);});
connect(m_ui->increaseButton, &QPushButton::clicked, this, [this]{emit StyleEditorWindow::changeFontSize(FontSizeAction::Increase); buttonClicked(m_ui->increaseButton);});
connect(m_ui->fullWidthButton, &QPushButton::clicked, this, [this]{emit StyleEditorWindow::changeEditorTextWidth(EditorTextWidth::FullWidth); buttonClicked(m_ui->fullWidthButton);});
connect(m_ui->increaseWidthButton, &QPushButton::clicked, this, [this]{emit StyleEditorWindow::changeEditorTextWidth(EditorTextWidth::Increase); buttonClicked(m_ui->increaseWidthButton);});
connect(m_ui->decreaseWidthButton, &QPushButton::clicked, this, [this]{emit StyleEditorWindow::changeEditorTextWidth(EditorTextWidth::Decrease); buttonClicked(m_ui->decreaseWidthButton);});
connect(m_ui->lightButton, &QPushButton::clicked, this, [this]{emit StyleEditorWindow::changeTheme(Theme::Light); buttonClicked(m_ui->lightButton);});
connect(m_ui->darkButton, &QPushButton::clicked, this, [this]{emit StyleEditorWindow::changeTheme(Theme::Dark); buttonClicked(m_ui->darkButton);});
connect(m_ui->sepiaButton, &QPushButton::clicked, this, [this]{emit StyleEditorWindow::changeTheme(Theme::Sepia); buttonClicked(m_ui->sepiaButton);});
connect(m_ui->resetDefaultButton, &QPushButton::clicked, this, [this]{emit StyleEditorWindow::resetEditorToDefaultSettings(); buttonClicked(m_ui->resetDefaultButton);});
QString ss = QString("QPushButton {background-color: white;} "
"QPushButton {border: none;}"
"QPushButton {padding: 0px;}"
);
QString fontDisplayName;
#ifdef __APPLE__
if(QFont(QStringLiteral("SF Pro Text")).exactMatch()) {
fontDisplayName = QStringLiteral("SF Pro Text");
} else if(QFont(QStringLiteral("Helvetica Neue")).exactMatch()) {
fontDisplayName = QStringLiteral("Helvetica Neue");
} else if(QFont(QStringLiteral("Helvetica")).exactMatch()) {
fontDisplayName = QStringLiteral("Helvetica");
} else {
fontDisplayName = QStringLiteral("Roboto");
}
#elif _WIN32
fontDisplayName = QFont(QStringLiteral("Segoe UI")).exactMatch() ? QStringLiteral("Segoe UI") : QStringLiteral("Roboto");
#else
fontDisplayName = QStringLiteral("Roboto");
#endif
this->setFont(QFont(fontDisplayName));
#ifdef __APPLE__
int fontDisplaySize = 13;
#else
int fontDisplaySize = 9;
#endif
m_ui->fullWidthButton->setFont(QFont(fontDisplayName, fontDisplaySize, QFont::Bold));
m_ui->increaseButton->setFont(QFont(fontDisplayName, fontDisplaySize, QFont::Bold));
m_ui->decreaseButton->setFont(QFont(fontDisplayName, fontDisplaySize, QFont::Bold));
m_ui->increaseWidthButton->setFont(QFont(fontDisplayName, fontDisplaySize, QFont::Bold));
m_ui->decreaseWidthButton->setFont(QFont(fontDisplayName, fontDisplaySize, QFont::Bold));
m_ui->lightButton->setFont(QFont(fontDisplayName, fontDisplaySize, QFont::Bold));
m_ui->darkButton->setFont(QFont(fontDisplayName, fontDisplaySize, QFont::Bold));
m_ui->sepiaButton->setFont(QFont(fontDisplayName, fontDisplaySize, QFont::Bold));
m_ui->resetDefaultButton->setFont(QFont(fontDisplayName, fontDisplaySize, QFont::Bold));
QList<QPushButton*> listChildrenButtons = findChildren<QPushButton*>();
foreach(QPushButton* childButton, listChildrenButtons) {
childButton->setStyleSheet(ss);
childButton->installEventFilter(this);
}
new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S), this, SLOT(toggleWindowVisibility()));
m_ui->decreaseButton->setIcon(QIcon(QStringLiteral(":images/minus.png")));
m_ui->increaseButton->setIcon(QIcon(QStringLiteral(":images/plus.png")));
QIcon decreaseWidthIcon(QStringLiteral(":images/decrease-width.png"));
QIcon increaseWidthIcon(QStringLiteral(":images/increase-width.png"));
m_ui->decreaseWidthButton->setIcon(decreaseWidthIcon);
m_ui->decreaseWidthButton->setIconSize(decreaseWidthIcon.actualSize(m_ui->decreaseWidthButton->size()));
m_ui->increaseWidthButton->setIcon(increaseWidthIcon);
m_ui->increaseWidthButton->setIconSize(increaseWidthIcon.actualSize(m_ui->increaseWidthButton->size()));
}
StyleEditorWindow::~StyleEditorWindow()
{
/* Delete UI controls */
delete m_ui;
}
/*!
* \brief StyleEditorWindow::buttonClicked
* Handles the styling of selected buttons
* (those who needs to keep show they are selected)
* \param button
*/
void StyleEditorWindow::buttonClicked(QPushButton* button)
{
m_currentlyClickedButton = button;
bool shouldBeClicked = true;
if(button == m_ui->serifButton ||
button == m_ui->sansSerifButton ||
button == m_ui->monoButton) {
if(m_currentSelectedFontButton != Q_NULLPTR ){
m_currentSelectedFontButton->setStyleSheet(getStyleSheetForButton(ButtonState::Normal));
}
m_currentSelectedFontButton = button;
}
if(button == m_ui->lightButton ||
button == m_ui->darkButton ||
button == m_ui->sepiaButton) {
if(m_currentSelectedThemeButton != Q_NULLPTR ){
m_currentSelectedThemeButton->setStyleSheet(getStyleSheetForButton(ButtonState::Normal));
}
m_currentSelectedThemeButton = button;
}
if(button == m_ui->fullWidthButton) {
if(m_isFullWidthClicked) {
m_ui->fullWidthButton->setStyleSheet(getStyleSheetForButton(ButtonState::Hovered));
m_isFullWidthClicked = false;
shouldBeClicked = false;
} else {
m_ui->fullWidthButton->setStyleSheet(getStyleSheetForButton(ButtonState::Clicked));
m_isFullWidthClicked = true;
}
}
if(button == m_ui->increaseWidthButton ||
button == m_ui->decreaseWidthButton) {
m_ui->fullWidthButton->setStyleSheet(getStyleSheetForButton(ButtonState::Normal));
m_isFullWidthClicked = false;
}
if(shouldBeClicked)
button->setStyleSheet(getStyleSheetForButton(ButtonState::Clicked));
if(button != m_currentSelectedFontButton && button != m_currentSelectedThemeButton && button != m_ui->fullWidthButton) {
QTimer::singleShot(200, this, [this]{m_currentlyClickedButton->setStyleSheet(getStyleSheetForButton(ButtonState::Normal));});
}
}
/*!
* \brief StyleEditorWindow::changeSelectedFont
* Called from MainWindow so we can let the style editor window
* know the font name (known only from the font lists in MainWindow)
* \param selectedFontType
* \param selectedFontName
*/
void StyleEditorWindow::changeSelectedFont(FontTypeface selectedFontType, QString selectedFontName)
{
switch(selectedFontType) {
case FontTypeface::Mono:
m_selectedMonoFontFamilyName = selectedFontName;
m_ui->monoButton->changeFont(selectedFontName, QStringLiteral("mono"), m_currentFontColor);
m_ui->monoButton->repaint();
break;
case FontTypeface::Serif:
m_selectedSerifFontFamilyName = selectedFontName;
m_ui->serifButton->changeFont(selectedFontName, QStringLiteral("serif"), m_currentFontColor);
m_ui->serifButton->repaint();
break;
case FontTypeface::SansSerif:
m_selectedSansSerifFontFamilyName = selectedFontName;
m_ui->sansSerifButton->changeFont(selectedFontName, QStringLiteral("sansSerif"), m_currentFontColor);
m_ui->sansSerifButton->repaint();
break;
}
}
/*!
* \brief StyleEditorWindow::getStyleSheetForButton
* Get the required stylesheet of a button based on the provided button's
* state: Normal, Hovered, Clicked.
* \param buttonState
*/
QString StyleEditorWindow::getStyleSheetForButton(ButtonState buttonState)
{
QString ss = QStringLiteral("QPushButton{ "
" background-color: %1; "
" border: none;"
" padding: 0px;");
// Font color
if(m_currentTheme == Theme::Dark) {
ss.append(QStringLiteral("color: rgb(204, 204, 204);"));
} else {
ss.append(QStringLiteral("color: rgb(26, 26, 26);"));
}
ss.append("}");
switch(buttonState) {
case ButtonState::Normal: {
ss = ss.arg(m_currentThemeColor.name());
break;
}
case ButtonState::Hovered: {
if(m_currentTheme == Theme::Dark) {
ss = ss.arg(QColor(43, 43,43).name());
} else {
ss = ss.arg(QColor(207, 207, 207).name());
}
break;
}
case ButtonState::Clicked: {
if(m_currentTheme == Theme::Dark) {
ss = ss.arg(QColor(0, 59, 148).name());
} else {
ss = ss.arg(QColor(218, 233, 239).name());
}
break;
}
}
return ss;
}
/*!
* \brief StyleEditorWindow::isSelectedButton
* Check if the provided button is currently selected
* (from those buttons who keep their selected state)
* \param button
*/
bool StyleEditorWindow::isSelectedButton(QPushButton *button)
{
if((button != m_currentSelectedFontButton &&
button != m_currentSelectedThemeButton) &&
!(button == m_ui->fullWidthButton && m_isFullWidthClicked)) {
return false;
}
return true;
}
/*!
* \brief StyleEditorWindow::setTheme
* Called from MainWindow to set the appropriate theme and suitable color
* Also textColor is provided as sometimes we want the font in style editor window
* to not be the same as the Mainwindow->m_textEdit's text color (as when Sepia is chosen)
* (from those buttons who keep their selected state)
* \param theme
* \param themeColor
* \param textColor
*/
void StyleEditorWindow::setTheme(Theme theme, QColor themeColor, QColor textColor)
{
m_currentTheme = theme;
m_currentThemeColor = themeColor;
m_currentFontColor = textColor;
if(theme == Theme::Dark) {
m_ui->decreaseButton->setIcon(QIcon(QStringLiteral(":images/minus-dark.png")));
m_ui->increaseButton->setIcon(QIcon(QStringLiteral(":images/plus-dark.png")));
QIcon decreaseWidthIcon(QStringLiteral(":images/decrease-width-dark.png"));
QIcon increaseWidthIcon(QStringLiteral(":images/increase-width-dark.png"));
m_ui->decreaseWidthButton->setIcon(decreaseWidthIcon);
m_ui->decreaseWidthButton->setIconSize(decreaseWidthIcon.actualSize(m_ui->decreaseWidthButton->size()));
m_ui->increaseWidthButton->setIcon(increaseWidthIcon);
m_ui->increaseWidthButton->setIconSize(increaseWidthIcon.actualSize(m_ui->increaseWidthButton->size()));
} else {
m_ui->decreaseButton->setIcon(QIcon(QStringLiteral(":images/minus.png")));
m_ui->increaseButton->setIcon(QIcon(QStringLiteral(":images/plus.png")));
QIcon decreaseWidthIcon(QStringLiteral(":images/decrease-width.png"));
QIcon increaseWidthIcon(QStringLiteral(":images/increase-width.png"));
m_ui->decreaseWidthButton->setIcon(decreaseWidthIcon);
m_ui->decreaseWidthButton->setIconSize(decreaseWidthIcon.actualSize(m_ui->decreaseWidthButton->size()));
m_ui->increaseWidthButton->setIcon(increaseWidthIcon);
m_ui->increaseWidthButton->setIconSize(increaseWidthIcon.actualSize(m_ui->increaseWidthButton->size()));
}
m_ui->monoButton->setTheme(theme);
m_ui->serifButton->setTheme(theme);
m_ui->sansSerifButton->setTheme(theme);
m_ui->monoButton->changeFont(m_selectedMonoFontFamilyName, QStringLiteral("mono"), m_currentFontColor);
m_ui->serifButton->changeFont(m_selectedSerifFontFamilyName, QStringLiteral("serif"), m_currentFontColor);
m_ui->sansSerifButton->changeFont(m_selectedSansSerifFontFamilyName, QStringLiteral("sansSerif"), m_currentFontColor);
m_ui->monoButton->repaint();
m_ui->serifButton->repaint();
m_ui->sansSerifButton->repaint();
QList<QPushButton*> listChildrenButtons = findChildren<QPushButton*>();
foreach(QPushButton* childButton, listChildrenButtons) {
if(!isSelectedButton(childButton)) {
childButton->setStyleSheet(getStyleSheetForButton(ButtonState::Normal));
} else {
childButton->setStyleSheet(getStyleSheetForButton(ButtonState::Clicked));
}
}
}
/*!
* \brief StyleEditorWindow::restoreSelectedOptions
* Called from MainWindow to restore the selected options
* on app start up, if they exist.
* \param isTextFullWidth
* \param selectedFontTypeface
* \param selectedTheme
*/
void StyleEditorWindow::restoreSelectedOptions(bool isTextFullWidth, FontTypeface selectedFontTypeface, Theme selectedTheme)
{
if(isTextFullWidth){
buttonClicked(m_ui->fullWidthButton);
}
switch(selectedFontTypeface) {
case FontTypeface::Mono:
buttonClicked(m_ui->monoButton);
break;
case FontTypeface::Serif:
buttonClicked(m_ui->serifButton);
break;
case FontTypeface::SansSerif:
buttonClicked(m_ui->sansSerifButton);
break;
}
switch(selectedTheme) {
case Theme::Light: {
buttonClicked(m_ui->lightButton);
break;
}
case Theme::Dark: {
buttonClicked(m_ui->darkButton);
break;
}
case Theme::Sepia: {
buttonClicked(m_ui->sepiaButton);
break;
}
}
}
void StyleEditorWindow::toggleWindowVisibility()
{
if(isVisible()) {
hide();
} else {
show();
}
}
/*!
* \brief StyleEditorWindow::eventFilter
* Handle buttons event when hovered upon/unhovered
* and set the appropriate stylesheet for them
* \param object
* \param event
*/
bool StyleEditorWindow::eventFilter(QObject *object, QEvent *event)
{
switch (event->type()) {
case QEvent::Enter:{
QList<QPushButton*> listChildrenButtons = findChildren<QPushButton*>();
foreach(QPushButton* childButton, listChildrenButtons) {
if((object == childButton && !isSelectedButton(childButton))) {
childButton->setStyleSheet(getStyleSheetForButton(ButtonState::Hovered));
}
}
break;
}
case QEvent::Leave:{
QList<QPushButton*> listChildrenButtons = findChildren<QPushButton*>();
foreach(QPushButton* childButton, listChildrenButtons) {
if(object == childButton && !isSelectedButton(childButton)) {
childButton->setStyleSheet(getStyleSheetForButton(ButtonState::Normal));
}
}
break;
}
default:
break;
}
return QObject::eventFilter(object, event);
}