Mercurial > hg > syntilista
comparison src/main.cpp @ 135:45e17cdde93a
Move application global settings to a struct.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 23 Aug 2017 21:30:34 +0300 |
parents | 478ce4c94f6b |
children | 32c8408eb0cb |
comparison
equal
deleted
inserted
replaced
134:478ce4c94f6b | 135:45e17cdde93a |
---|---|
17 #include "ui_mainwindow.h" | 17 #include "ui_mainwindow.h" |
18 #include "ui_editperson.h" | 18 #include "ui_editperson.h" |
19 #include "ui_aboutwindow.h" | 19 #include "ui_aboutwindow.h" |
20 | 20 |
21 | 21 |
22 double setScale; // Global UI scale factor | 22 // |
23 QString appDataPath; // Application data path/directory | 23 // Application settings struct |
24 // | |
25 struct | |
26 { | |
27 double uiScale; // Global UI scale factor | |
28 QString dataPath; // Application data path/directory | |
29 } settings; | |
30 | |
24 | 31 |
25 | 32 |
26 // | 33 // |
27 // Convert QString to a double value, replacing comma | 34 // Convert QString to a double value, replacing comma |
28 // | 35 // |
76 // | 83 // |
77 // Error logging | 84 // Error logging |
78 // | 85 // |
79 void slLog(const QString &mtype, const QString &msg) | 86 void slLog(const QString &mtype, const QString &msg) |
80 { | 87 { |
81 QString filename = appDataPath + QDir::separator() + APP_LOG_FILE; | 88 QString filename = settings.dataPath + QDir::separator() + APP_LOG_FILE; |
82 QFile fh(filename); | 89 QFile fh(filename); |
83 if (fh.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) | 90 if (fh.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) |
84 { | 91 { |
85 QTextStream out(&fh); | 92 QTextStream out(&fh); |
86 out << | 93 out << |
190 | 197 |
191 | 198 |
192 void slSetCommonStyleSheet(QWidget *widget) | 199 void slSetCommonStyleSheet(QWidget *widget) |
193 { | 200 { |
194 // Clamp scale value | 201 // Clamp scale value |
195 if (setScale < 0.5f) | 202 if (settings.uiScale < 0.5f) |
196 setScale = 0.5f; | 203 settings.uiScale = 0.5f; |
197 | 204 |
198 if (setScale > 3.0f) | 205 if (settings.uiScale > 3.0f) |
199 setScale = 3.0f; | 206 settings.uiScale = 3.0f; |
200 | 207 |
201 // Set the stylesheet | 208 // Set the stylesheet |
202 widget->setStyleSheet( | 209 widget->setStyleSheet( |
203 QStringLiteral( | 210 QStringLiteral( |
204 "* { font-size: %1pt; }" | 211 "* { font-size: %1pt; }" |
214 "#label_PersonName { font-size: %5pt; font-weight: bold; }" | 221 "#label_PersonName { font-size: %5pt; font-weight: bold; }" |
215 "#label_BalanceValue { font-size: %4pt; font-weight: bold; }" | 222 "#label_BalanceValue { font-size: %4pt; font-weight: bold; }" |
216 "#label_EUR { font-size: %4pt; font-weight: bold; }" | 223 "#label_EUR { font-size: %4pt; font-weight: bold; }" |
217 "#edit_Amount { font-size: %4pt; margin: 0.5em; padding: 0.5em; }" | 224 "#edit_Amount { font-size: %4pt; margin: 0.5em; padding: 0.5em; }" |
218 ). | 225 ). |
219 arg(12 * setScale). | 226 arg(12 * settings.uiScale). |
220 arg(14 * setScale). | 227 arg(14 * settings.uiScale). |
221 arg(16 * setScale). | 228 arg(16 * settings.uiScale). |
222 arg(18 * setScale). | 229 arg(18 * settings.uiScale). |
223 arg(20 * setScale) | 230 arg(20 * settings.uiScale) |
224 ); | 231 ); |
225 } | 232 } |
226 | 233 |
227 | 234 |
228 int main(int argc, char *argv[]) | 235 int main(int argc, char *argv[]) |
230 QApplication sapp(argc, argv); | 237 QApplication sapp(argc, argv); |
231 | 238 |
232 // | 239 // |
233 // Create logfile and data directory | 240 // Create logfile and data directory |
234 // | 241 // |
235 appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); | 242 settings.dataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); |
236 QDir path(appDataPath); | 243 QDir path(settings.dataPath); |
237 if (!path.exists(appDataPath)) | 244 if (!path.exists(settings.dataPath)) |
238 path.mkpath(appDataPath); | 245 path.mkpath(settings.dataPath); |
239 | 246 |
240 // | 247 // |
241 // Initialize / open SQL database connection | 248 // Initialize / open SQL database connection |
242 // | 249 // |
243 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); | 250 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); |
244 db.setDatabaseName(appDataPath + QDir::separator() + APP_SQLITE_FILE); | 251 db.setDatabaseName(settings.dataPath + QDir::separator() + APP_SQLITE_FILE); |
245 | 252 |
246 if (!db.open()) | 253 if (!db.open()) |
247 { | 254 { |
248 slErrorMsg( | 255 slErrorMsg( |
249 QObject::tr("Tietokantaa ei voitu avata"), | 256 QObject::tr("Tietokantaa ei voitu avata"), |
416 // | 423 // |
417 // Restoring and saving of current settings | 424 // Restoring and saving of current settings |
418 // | 425 // |
419 void SyntilistaMainWindow::readSettings() | 426 void SyntilistaMainWindow::readSettings() |
420 { | 427 { |
421 QSettings settings(APP_VENDOR, APP_ID); | 428 QSettings tmpst(APP_VENDOR, APP_ID); |
422 | 429 |
423 // Restore window size and position | 430 // Restore window size and position |
424 move(settings.value("pos", QPoint(100, 100)).toPoint()); | 431 move(tmpst.value("pos", QPoint(100, 100)).toPoint()); |
425 resize(settings.value("size", QSize(1000, 600)).toSize()); | 432 resize(tmpst.value("size", QSize(1000, 600)).toSize()); |
426 | 433 |
427 // Other settings | 434 // Other settings |
428 setScale = settings.value("scale", 1.0f).toDouble(); | 435 settings.uiScale = tmpst.value("scale", 1.0f).toDouble(); |
429 } | 436 } |
430 | 437 |
431 | 438 |
432 void SyntilistaMainWindow::saveSettings() | 439 void SyntilistaMainWindow::saveSettings() |
433 { | 440 { |
434 QSettings settings(APP_VENDOR, APP_ID); | 441 QSettings tmpst(APP_VENDOR, APP_ID); |
435 | 442 |
436 // Save window size and position | 443 // Save window size and position |
437 settings.setValue("pos", pos()); | 444 tmpst.setValue("pos", pos()); |
438 settings.setValue("size", size()); | 445 tmpst.setValue("size", size()); |
439 settings.setValue("scale", setScale); | 446 tmpst.setValue("scale", settings.uiScale); |
440 } | 447 } |
441 | 448 |
442 | 449 |
443 // | 450 // |
444 // Window scale / zoom changing | 451 // Window scale / zoom changing |
445 // | 452 // |
446 void SyntilistaMainWindow::changeUIZoomIn() | 453 void SyntilistaMainWindow::changeUIZoomIn() |
447 { | 454 { |
448 setScale += 0.1f; | 455 settings.uiScale += 0.1f; |
449 slSetCommonStyleSheet(this); | 456 slSetCommonStyleSheet(this); |
450 } | 457 } |
451 | 458 |
452 | 459 |
453 void SyntilistaMainWindow::changeUIZoomOut() | 460 void SyntilistaMainWindow::changeUIZoomOut() |
454 { | 461 { |
455 setScale -= 0.1f; | 462 settings.uiScale -= 0.1f; |
456 slSetCommonStyleSheet(this); | 463 slSetCommonStyleSheet(this); |
457 } | 464 } |
458 | 465 |
459 | 466 |
460 void SyntilistaMainWindow::changeUIZoomReset() | 467 void SyntilistaMainWindow::changeUIZoomReset() |
461 { | 468 { |
462 setScale = 1.0f; | 469 settings.uiScale = 1.0f; |
463 slSetCommonStyleSheet(this); | 470 slSetCommonStyleSheet(this); |
464 } | 471 } |
465 | 472 |
466 | 473 |
467 // | 474 // |