-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
249 lines (213 loc) · 7.07 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow( Settings* s, QWidget *parent) :
QDialog(parent),
ui(new Ui::MainWindow),
settings(s)
{
ui->setupUi(this);
this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);
// Only hexadecimal in color input.
QString mask = "\\#HHHHHH";
m_maskLength = mask.length();
loadSettingsToGui();
enableRelevantGuiOnly();
ui->btnApply->setEnabled(false);
#if defined(Q_OS_WIN)
ui->checkBoxAvailableMemory->setEnabled( false);
#endif
ui->editColorText1->setInputMask(mask);
ui->editColorText2->setInputMask(mask);
ui->editColorBack1->setInputMask(mask);
ui->editColorBack2->setInputMask(mask);
// Resize editBoxes to contents.
QFontMetrics fontMetrics( ui->editColorText1->font());
int width = fontMetrics.width(mask) + 10;
ui->editColorText1->setMinimumWidth(width);
ui->editColorText2->setMinimumWidth(width);
ui->editColorBack1->setMinimumWidth(width);
ui->editColorBack2->setMinimumWidth(width);
ui->editColorText1->setMaximumWidth(width);
ui->editColorText2->setMaximumWidth(width);
ui->editColorBack1->setMaximumWidth(width);
ui->editColorBack2->setMaximumWidth(width);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeEvent(QCloseEvent *event) // on close: just do cancel.
{
event->ignore();
on_btnCancel_clicked();
}
void MainWindow::openColorDialog(QLineEdit* lineEdit)
{
QColorDialog dialog;
QColor currentColor;
currentColor.setNamedColor( lineEdit->text());
QColor color = dialog.getColor(currentColor);
if (color.isValid())
lineEdit->setText( color.name());
}
void MainWindow::loadSettingsToGui()
{
m_loadingSettings = true;
ui->spinBoxInterval->setValue( settings->value("refreshInterval").toInt());
ui->checkBoxShowValue->setChecked( settings->value("showValue").toBool());
ui->radioShowSolidBackground->setChecked( settings->value("showBackground").toBool());
ui->radioShowProgress->setChecked( settings->value("showBackgroundProgress").toBool());
ui->editColorText1->setText( settings->value("colorText1").toString());
ui->editColorText2->setText( settings->value("colorText2").toString());
ui->editColorBack1->setText( settings->value("colorBack1").toString());
ui->editColorBack2->setText( settings->value("colorBack2").toString());
ui->checkBoxAvailableMemory->setChecked( settings->value("availableMemory").toBool());
m_loadingSettings = false;
}
void MainWindow::saveSettings()
{
settings->insert("refreshInterval", ui->spinBoxInterval->value());
settings->insert("showValue", ui->checkBoxShowValue->isChecked());
settings->insert("showBackground", ui->radioShowSolidBackground->isChecked());
settings->insert("showBackgroundProgress", ui->radioShowProgress->isChecked());
settings->insert("colorText1", ui->editColorText1->text());
settings->insert("colorText2", ui->editColorText2->text());
settings->insert("colorBack1", ui->editColorBack1->text());
settings->insert("colorBack2", ui->editColorBack2->text());
settings->insert("availableMemory", ui->checkBoxAvailableMemory->isChecked());
}
void MainWindow::enableRelevantGuiOnly()
{
// Disable text color settings if there is no text.
if (!ui->checkBoxShowValue->isChecked())
{
ui->editColorText1->setEnabled(false);
ui->editColorText2->setEnabled(false);
ui->labelColor1->setEnabled(false);
ui->labelColor2->setEnabled(false);
ui->btnColor1->setEnabled(false);
ui->btnColor2->setEnabled(false);
} else {
ui->editColorText1->setEnabled(true);
ui->editColorText2->setEnabled(true);
ui->labelColor1->setEnabled(true);
ui->labelColor2->setEnabled(true);
ui->btnColor1->setEnabled(true);
ui->btnColor2->setEnabled(true);
}
// Disable second background color if background is only solid.
if (ui->radioShowSolidBackground->isChecked())
{
ui->editColorBack2->setEnabled(false);
ui->labelColor4->setEnabled(false);
ui->btnColor4->setEnabled(false);
ui->editColorText2->setEnabled(false); // deactivate second text color, if background is solid.
ui->labelColor2->setEnabled(false);
ui->btnColor2->setEnabled(false);
} else {
ui->editColorBack2->setEnabled(true);
ui->labelColor4->setEnabled(true);
ui->btnColor4->setEnabled(true);
}
}
void MainWindow::on_btnColor1_clicked()
{
openColorDialog( ui->editColorText1);
}
void MainWindow::on_btnColor2_clicked()
{
openColorDialog( ui->editColorText2);
}
void MainWindow::on_btnColor3_clicked()
{
openColorDialog( ui->editColorBack1);
}
void MainWindow::on_btnColor4_clicked()
{
openColorDialog( ui->editColorBack2);
}
void MainWindow::on_btnCancel_clicked()
{
settings->undoChanges();
loadSettingsToGui();
ui->btnApply->setEnabled(false);
emit sCloseWindow();
}
void MainWindow::on_btnOK_clicked()
{
saveSettings();
settings->applyChangesAndSave();
emit sCloseWindow();
}
void MainWindow::on_btnApply_clicked()
{
saveSettings();
settings->applyChangesAndSave();
ui->btnApply->setEnabled(false);
}
void MainWindow::somethingChanged()
{
ui->btnApply->setEnabled(true);
}
void MainWindow::on_spinBoxInterval_valueChanged(int arg1)
{
Q_UNUSED(arg1);
somethingChanged();
}
void MainWindow::on_radioShowProgress_clicked()
{
somethingChanged();
enableRelevantGuiOnly();
}
void MainWindow::on_radioShowSolidBackground_clicked()
{
somethingChanged();
enableRelevantGuiOnly();
}
void MainWindow::on_checkBoxShowValue_clicked()
{
somethingChanged();
enableRelevantGuiOnly();
}
void MainWindow::colorValueChanged(QLineEdit* lineEdit, QLabel* colorExampleLabel, const QString &arg)
{
QString argEdit = arg.mid( 0, m_maskLength - 1);
int pos = lineEdit->cursorPosition();
#if (QT_VERSION >= 0x050600) // Later than 5.51. Maybe even later.
argEdit.resize( m_maskLength - 1, '0');
#else
argEdit += QString( m_maskLength - argEdit.size(), '0');
#endif
lineEdit->setText(argEdit);
lineEdit->setCursorPosition(pos);
colorExampleLabel->setStyleSheet("background-color:" + argEdit + ";");
}
void MainWindow::on_editColorText1_textChanged(const QString &arg1)
{
colorValueChanged( ui->editColorText1, ui->colorExample1, arg1);
somethingChanged();
}
void MainWindow::on_editColorText2_textChanged(const QString &arg1)
{
colorValueChanged(ui->editColorText2, ui->colorExample2, arg1);
somethingChanged();
}
void MainWindow::on_editColorBack1_textChanged(const QString &arg1)
{
colorValueChanged(ui->editColorBack1, ui->colorExample3, arg1);
somethingChanged();
}
void MainWindow::on_editColorBack2_textChanged(const QString &arg1)
{
colorValueChanged(ui->editColorBack2, ui->colorExample4, arg1);
somethingChanged();
}
void MainWindow::on_checkBoxAvailableMemory_toggled(bool checked)
{
Q_UNUSED(checked);
somethingChanged();
}
void MainWindow::on_toolButton_clicked()
{
aboutDialog.exec();
}