-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPreferencesWindow.cpp
475 lines (441 loc) · 17.3 KB
/
PreferencesWindow.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include "PreferencesWindow.h"
PreferencesWindow::PreferencesWindow(QWidget *parent) : QDialog(parent)
{
// Variables load from the config.ini file
QFile configFile(QCoreApplication::applicationDirPath() + "/data/config/config.ini");
QString fileHomePage;
QString fileSearchEngine;
QString fileDownloadPath;
QString fileZoomTextOnly;
bool isZoomEnabled;
QString fileJs;
bool isJsEnabled;
QString fileFlash;
bool isFlashEnabled;
if (!configFile.exists())
{
QMessageBox::critical(NULL, tr("Error"), tr("<p>Configuration file missing ! Please try to reinstall.</p><p>The application will stop.</p>"));
qApp->quit();
}
else
{
if (configFile.open(QIODevice::ReadOnly))
{
QTextStream flux(&configFile);
flux >> fileHomePage;
flux >> fileSearchEngine;
flux >> fileDownloadPath;
flux >> fileZoomTextOnly;
flux >> fileJs;
flux >> fileFlash;
if (fileZoomTextOnly == "true")
{
isZoomEnabled = true;
}
else
{
isZoomEnabled = false;
}
if (fileJs == "true")
{
isJsEnabled = true;
}
else
{
isJsEnabled = false;
}
if (fileFlash == "true")
{
isFlashEnabled = true;
}
else
{
isFlashEnabled = false;
}
configFile.close();
}
}
// Tabs Widgets
// General widget
// Container widget
QWidget *generalWidget = new QWidget();
// Home page url label
QLabel *homePage = new QLabel;
homePage->setText(tr("Home page"));
homePage->setFixedSize(260, 35);
homePage->setStyleSheet("QLabel { font-family: \"Trebuchet MS\"; font-size: 13px; margin-left: 20px; } ");
QHBoxLayout *urlLabelLayout = new QHBoxLayout;
urlLabelLayout->addWidget(homePage);
urlLabelLayout->setAlignment(Qt::AlignLeft);
// Home page url field
urlHomePage = new QLineEdit;
urlHomePage->setFixedSize(220, 35);
urlHomePage->setStyleSheet("QLineEdit { margin-top: 10px; margin-left: 0px; } ");
urlHomePage->setText(fileHomePage);
// Home icon
QLabel *iconHome = new QLabel;
iconHome->setPixmap(QPixmap::fromImage(QImage(QCoreApplication::applicationDirPath() + "/data/homePage.png")));
QHBoxLayout *urlFieldLayout = new QHBoxLayout;
urlFieldLayout->addWidget(urlHomePage);
urlFieldLayout->addWidget(iconHome);
urlFieldLayout->setAlignment(Qt::AlignCenter);
// Home page layout
QHBoxLayout *homePageLayout = new QHBoxLayout;
homePageLayout->addLayout(urlLabelLayout);
homePageLayout->addLayout(urlFieldLayout);
homePageLayout->setMargin(15);
// Default search engine label
QLabel *searchEngineLabel = new QLabel;
searchEngineLabel->setText(tr("Default search engine"));
searchEngineLabel->setFixedSize(260, 35);
searchEngineLabel->setStyleSheet("QLabel { font-family: \"Trebuchet MS\"; font-size: 13px; margin-left: 20px; } ");
QHBoxLayout *searchLabelLayout = new QHBoxLayout;
searchLabelLayout->addWidget(searchEngineLabel);
searchLabelLayout->setAlignment(Qt::AlignLeft);
// Default search engine field
searchEngineComboBox = new QComboBox;
searchEngineComboBox->setFixedSize(220, 35);
searchEngineComboBox->setStyleSheet("QComboBox { margin-top: 10px; margin-left: 0px; } ");
searchEngineComboBox->addItem(QIcon(QCoreApplication::applicationDirPath() + "/data/iconGoogle.png"), "Google");
searchEngineComboBox->addItem(QIcon(QCoreApplication::applicationDirPath() + "/data/iconBing.png"), "Bing");
searchEngineComboBox->addItem(QIcon(QCoreApplication::applicationDirPath() + "/data/iconYahoo.png"), "Yahoo");
searchEngineComboBox->addItem(QIcon(QCoreApplication::applicationDirPath() + "/data/iconWiki.png"), "Wikipedia");
for (int i = 0; i < searchEngineComboBox->count(); i++)
{
if (fileSearchEngine == searchEngineComboBox->itemText(i))
{
searchEngineComboBox->setCurrentIndex(i);
}
}
// Search icon
QLabel *iconSearch = new QLabel;
iconSearch->setPixmap(QPixmap::fromImage(QImage(QCoreApplication::applicationDirPath() + "/data/iconSearch.png")));
QHBoxLayout *searchEngineComboBoxLayout = new QHBoxLayout;
searchEngineComboBoxLayout->addWidget(searchEngineComboBox);
searchEngineComboBoxLayout->addWidget(iconSearch);
searchEngineComboBoxLayout->setAlignment(Qt::AlignCenter);
// Search engine layout
QHBoxLayout *searchEngineLayout = new QHBoxLayout;
searchEngineLayout->addLayout(searchLabelLayout);
searchEngineLayout->addLayout(searchEngineComboBoxLayout);
searchEngineLayout->setMargin(15);
// Download path
QLabel *downloadPath = new QLabel;
downloadPath->setText(tr("Download path"));
downloadPath->setFixedSize(260, 35);
downloadPath->setStyleSheet("QLabel { font-family: \"Trebuchet MS\"; font-size: 13px; margin-left: 20px; } ");
QHBoxLayout *pathLabelLayout = new QHBoxLayout;
pathLabelLayout->addWidget(downloadPath);
pathLabelLayout->setAlignment(Qt::AlignLeft);
// Download path field
urlDownloadPath = new QLineEdit;
urlDownloadPath->setFixedSize(220, 35);
urlDownloadPath->setStyleSheet("QLineEdit { margin-top: 10px; margin-left: 0px; } ");
urlDownloadPath->setText(fileDownloadPath); // Windows
// Choose directory
QPushButton *buttonOpenDir = new QPushButton;
buttonOpenDir->setFixedSize(39, 39);
buttonOpenDir->setIcon(QIcon(QCoreApplication::applicationDirPath() + "/data/openDir.png"));
buttonOpenDir->setIconSize(QSize(30, 30));
QHBoxLayout *urlDownloadPathLayout = new QHBoxLayout;
urlDownloadPathLayout->addWidget(urlDownloadPath);
urlDownloadPathLayout->addWidget(buttonOpenDir);
urlDownloadPathLayout->setAlignment(Qt::AlignCenter);
// Download path layout
QHBoxLayout *downloadPathLayout = new QHBoxLayout;
downloadPathLayout->addLayout(pathLabelLayout);
downloadPathLayout->addLayout(urlDownloadPathLayout);
downloadPathLayout->setMargin(15);
// General layout
QVBoxLayout *generalLayout = new QVBoxLayout;
generalLayout->addLayout(homePageLayout);
generalLayout->addLayout(searchEngineLayout);
generalLayout->addLayout(downloadPathLayout);
generalLayout->setAlignment(Qt::AlignTop);
generalWidget->setLayout(generalLayout);
// Appearance widget
QWidget *appearanceWidget = new QWidget();
// Zoom text only
QLabel *zoomTextOnly = new QLabel;
zoomTextOnly->setText(tr("Zoom : "));
zoomTextOnly->setFixedSize(260, 35);
zoomTextOnly->setStyleSheet("QLabel { font-family: \"Trebuchet MS\"; font-size: 13px; margin-left: 20px; } ");
QHBoxLayout *zoomLabelLayout = new QHBoxLayout;
zoomLabelLayout->addWidget(zoomTextOnly);
zoomLabelLayout->setAlignment(Qt::AlignLeft);
// Zoom text only checkbox
zoomCheckBox = new QCheckBox;
zoomCheckBox->setFixedSize(220, 35);
zoomCheckBox->setStyleSheet("QCheckBox { margin-top: 10px; margin-left: 0px; } ");
zoomCheckBox->setText(tr("Text only"));
zoomCheckBox->setChecked(isZoomEnabled);
// Zoom icon
QLabel *iconZoom = new QLabel;
iconZoom->setPixmap(QPixmap::fromImage(QImage(QCoreApplication::applicationDirPath() + "/data/magnifyingGlass.png")));
QHBoxLayout *zoomCheckBoxLayout = new QHBoxLayout;
zoomCheckBoxLayout->addWidget(zoomCheckBox);
zoomCheckBoxLayout->addWidget(iconZoom);
zoomCheckBoxLayout->setAlignment(Qt::AlignCenter);
// Zoom text only layout
QHBoxLayout *zoomTextOnlyLayout = new QHBoxLayout;
zoomTextOnlyLayout->addLayout(zoomLabelLayout);
zoomTextOnlyLayout->addLayout(zoomCheckBoxLayout);
zoomTextOnlyLayout->setMargin(15);
// Appearance layout
QVBoxLayout *appearanceLayout = new QVBoxLayout;
appearanceLayout->addLayout(zoomTextOnlyLayout);
appearanceLayout->setAlignment(Qt::AlignTop);
appearanceWidget->setLayout(appearanceLayout);
// Privacy widget
QWidget *privacyWidget = new QWidget();
// Disable Javascript
QLabel *disableJs = new QLabel;
disableJs->setText(tr("Javascript : "));
disableJs->setFixedSize(260, 35);
disableJs->setStyleSheet("QLabel { font-family: \"Trebuchet MS\"; font-size: 13px; margin-left: 20px; } ");
QHBoxLayout *disableJsLabelLayout = new QHBoxLayout;
disableJsLabelLayout->addWidget(disableJs);
disableJsLabelLayout->setAlignment(Qt::AlignLeft);
// Disable JS Checkbox
disableJsCheckBox = new QCheckBox;
disableJsCheckBox->setFixedSize(220, 35);
disableJsCheckBox->setStyleSheet("QCheckBox { margin-top: 10px; margin-left: 0px; } ");
disableJsCheckBox->setText(tr("Enable / Disable"));
disableJsCheckBox->setChecked(isJsEnabled);
// JS icon
QLabel *iconJs = new QLabel;
iconJs->setPixmap(QPixmap::fromImage(QImage(QCoreApplication::applicationDirPath() + "/data/iconJs.png")));
QHBoxLayout *disableJsCheckBoxLayout = new QHBoxLayout;
disableJsCheckBoxLayout->addWidget(disableJsCheckBox);
disableJsCheckBoxLayout->addWidget(iconJs);
disableJsCheckBoxLayout->setAlignment(Qt::AlignCenter);
// Disable JS Layout
QHBoxLayout *disableJsLayout = new QHBoxLayout;
disableJsLayout->addLayout(disableJsLabelLayout);
disableJsLayout->addLayout(disableJsCheckBoxLayout);
disableJsLayout->setMargin(15);
// Disable Flash
QLabel *disableFlash = new QLabel;
disableFlash->setText(tr("Flash : "));
disableFlash->setFixedSize(260, 35);
disableFlash->setStyleSheet("QLabel { font-family: \"Trebuchet MS\"; font-size: 13px; margin-left: 20px; } ");
QHBoxLayout *disableFlashLabelLayout = new QHBoxLayout;
disableFlashLabelLayout->addWidget(disableFlash);
disableFlashLabelLayout->setAlignment(Qt::AlignLeft);
// Disable Flash Checkbox
disableFlashCheckBox = new QCheckBox;
disableFlashCheckBox->setFixedSize(220, 35);
disableFlashCheckBox->setStyleSheet("QCheckBox { margin-top: 10px; margin-left: 0px; } ");
disableFlashCheckBox->setText(tr("Enable / Disable"));
disableFlashCheckBox->setChecked(isFlashEnabled);
// Flash icon
QLabel *iconFlash = new QLabel;
iconFlash->setPixmap(QPixmap::fromImage(QImage(QCoreApplication::applicationDirPath() + "/data/iconFlash.png")));
QHBoxLayout *disableFlashCheckBoxLayout = new QHBoxLayout;
disableFlashCheckBoxLayout->addWidget(disableFlashCheckBox);
disableFlashCheckBoxLayout->addWidget(iconFlash);
disableFlashCheckBoxLayout->setAlignment(Qt::AlignCenter);
// Disable Flash Layout
QHBoxLayout *disableFlashLayout = new QHBoxLayout;
disableFlashLayout->addLayout(disableFlashLabelLayout);
disableFlashLayout->addLayout(disableFlashCheckBoxLayout);
disableFlashLayout->setMargin(15);
// Privacy layout
QVBoxLayout *privacyLayout = new QVBoxLayout;
privacyLayout->addLayout(disableJsLayout);
privacyLayout->addLayout(disableFlashLayout);
privacyLayout->setAlignment(Qt::AlignTop);
privacyWidget->setLayout(privacyLayout);
// Buttons widgets
QPushButton *submitButton = new QPushButton(tr("OK"));
QPushButton *cancelButton = new QPushButton(tr("Cancel"));
QHBoxLayout *buttonsLayout = new QHBoxLayout;
buttonsLayout->addWidget(submitButton);
buttonsLayout->addWidget(cancelButton);
buttonsLayout->setAlignment(Qt::AlignRight);
// Tab
QTabWidget *tabs = new QTabWidget();
tabs->setStyle(new QWindowsStyle);
tabs->setMovable(true);
tabs->setElideMode(Qt::ElideRight);
tabs->addTab(generalWidget, tr("General"));
tabs->addTab(appearanceWidget, tr("Appearance"));
tabs->addTab(privacyWidget, tr("Privacy"));
// Main layout
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(tabs);
mainLayout->addLayout(buttonsLayout);
// Tab style
QString style = "QTabBar::tab:selected { margin-left: -2px; margin-right: -2px; } QTabBar::tab:first:selected { margin-left: 0; } QTabBar::tab:last:selected { margin-right: 0; } ";
style += "QTabBar::tab:only-one { margin: 0; } QTabWidget::pane { border-top: 2px solid #C2C7CB; } QTabWidget::tab-bar { left: 5px; } ";
style += "QTabBar::tab { font-family: Calibri, Arial; font-size: 15px; background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #E1E1E1, stop: 0.4 #DDDDDD, stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3); border: 1px solid #C4C4C3; border-bottom-color: #C2C7CB; border-top-left-radius: 4px; border-top-right-radius: 4px; min-width: 80px; padding: 8px; }";
style += "QTabBar::tab:selected, QTabBar::tab:hover { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #fafafa, stop: 0.4 #f4f4f4, stop: 0.5 #e7e7e7, stop: 1.0 #fafafa); } QTabBar::tab:selected { border-color: #9B9B9B; border-bottom-color: #C2C7CB; } QTabBar::tab:!selected { margin-top: 2px; }";
tabs->setStyleSheet(style);
// Properties
setWindowTitle(tr("Preferences - Mango"));
setFixedSize(600, 400);
setLayout(mainLayout);
submitButton->setFocus(Qt::OtherFocusReason);
// Connections
connect(buttonOpenDir, SIGNAL(clicked()), this, SLOT(chooseDirDownload()));
connect(submitButton, SIGNAL(clicked()), this, SLOT(submitPreferences()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
}
// Methods
QString PreferencesWindow::checkHomePage()
{
QString urlHome;
QString url = urlHomePage->text();
bool isAValidUrl = false;
if (!url.isEmpty())
{
isAValidUrl = true;
if (url.left(7) != "http://" && url.left(8) != "https://")
{
url = "http://" + url;
}
}
if (!isAValidUrl)
{
urlHome = "http://www.google." + tr("us");
}
else
{
urlHome = url;
}
return urlHome;
}
QString PreferencesWindow::checkDefaultSearchEngine()
{
QString searchEngines[4];
QString nameDefaultSearchEngine;
searchEngines[0] = "Google";
searchEngines[1] = "Bing";
searchEngines[2] = "Yahoo";
searchEngines[3] = "Wikipedia";
bool isInArray = false;
for (int i=0; i < 4; i++)
{
if (searchEngines[i] == searchEngineComboBox->currentText())
{
isInArray = true;
}
}
if (searchEngineComboBox->currentText().isEmpty() || !isInArray)
{
nameDefaultSearchEngine = "Google";
}
else
{
nameDefaultSearchEngine = searchEngineComboBox->currentText();
}
return nameDefaultSearchEngine;
}
QString PreferencesWindow::checkDownloadPath()
{
QString downloadDirPath;
if (urlDownloadPath->text().isEmpty())
{
downloadDirPath = QDir::homePath().replace("/", "\\"); // Windows
}
else
{
downloadDirPath = urlDownloadPath->text();
}
return downloadDirPath;
}
bool PreferencesWindow::isZoomTextOnlyEnabled()
{
bool zoomTextOnlyEnabled;
if (zoomCheckBox->checkState() == Qt::Checked)
{
zoomTextOnlyEnabled = true;
}
else
{
zoomTextOnlyEnabled = false;
}
return zoomTextOnlyEnabled;
}
// Privacy slots
bool PreferencesWindow::isJsEnabled()
{
bool jsEnabled;
if (disableJsCheckBox->checkState() == Qt::Checked)
{
jsEnabled = true;
}
else
{
jsEnabled = false;
}
return jsEnabled;
}
bool PreferencesWindow::isFlashEnabled()
{
bool flashEnabled;
if (disableFlashCheckBox->checkState() == Qt::Checked)
{
flashEnabled = true;
}
else
{
flashEnabled = false;
}
return flashEnabled;
}
// Slots
void PreferencesWindow::chooseDirDownload()
{
QString downloadDirPath = QFileDialog::getExistingDirectory(this, tr("Choose a download path"), QDir::homePath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::HideNameFilterDetails);
if (downloadDirPath.isEmpty())
{
//downloadDirPath = QDir::homePath(); // Linux
downloadDirPath = QDir::homePath().replace("/", "\\"); // Windows
}
//urlDownloadPath->setText(downloadDirPath); // Linux
urlDownloadPath->setText(downloadDirPath.replace("/", "\\")); // Windows
}
void PreferencesWindow::submitPreferences()
{
QFile configFile(QCoreApplication::applicationDirPath() + "/data/config/config.ini");
if(!configFile.exists())
{
QMessageBox::critical(NULL, tr("Error"), tr("Configuration file missing ! Please try to reinstall."));
}
else
{
QString zoomTextEnabledString;
QString flashEnabledString;
QString jsEnabledString;
if (isZoomTextOnlyEnabled())
{
zoomTextEnabledString = "true";
}
else
{
zoomTextEnabledString = "false";
}
if (isFlashEnabled())
{
flashEnabledString = "true";
}
else
{
flashEnabledString = "false";
}
if (isJsEnabled())
{
jsEnabledString = "true";
}
else
{
jsEnabledString = "false";
}
configFile.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream flux(&configFile);
flux << checkHomePage() << endl << checkDefaultSearchEngine() << endl << checkDownloadPath() << endl << zoomTextEnabledString << endl << jsEnabledString << endl << flashEnabledString;
configFile.close();
}
this->close();
}