mainwindow.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2021 Felix Turowsky
  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 <https://www.gnu.org/licenses/>.
  17. */
  18. #include "mainwindow.h"
  19. #include "ui_mainwindow.h"
  20. #include "src/testing/adebug.h"
  21. #include "src/database/adatabase.h"
  22. #include "src/classes/astyle.h"
  23. MainWindow::MainWindow(QWidget *parent)
  24. : QMainWindow(parent)
  25. , ui(new Ui::MainWindow)
  26. {
  27. ui->setupUi(this);
  28. readSettings();
  29. // Set up Toolbar
  30. ui->actionHome->setIcon(QIcon(":/icons/ionicon-icons/home-outline.png"));
  31. ui->actionNewFlight->setIcon(QIcon(":/icons/ionicon-icons/airplane-outline.png"));
  32. ui->actionLogbook->setIcon(QIcon(":/icons/ionicon-icons/book-outline.png"));
  33. ui->actionAircraft->setIcon(QIcon(":/icons/ionicon-icons/airplane-outline.png"));
  34. ui->actionPilots->setIcon(QIcon(":/icons/ionicon-icons/pilot.png"));
  35. ui->actionSettings->setIcon(QIcon(":/icons/ionicon-icons/settings-outline.png"));
  36. ui->actionQuit->setIcon(QIcon(":/icons/ionicon-icons/power-outline.png"));
  37. ui->toolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
  38. const auto buttons = ui->toolBar->findChildren<QWidget *>();
  39. ui->toolBar->setIconSize(QSize(64, 64));
  40. for (const auto &button : buttons) {
  41. button->setMinimumWidth(128);
  42. }
  43. // Add spacer in toolbar to separate left and right parts
  44. auto *spacer = new QWidget();
  45. spacer->setMinimumWidth(1);
  46. spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
  47. ui->toolBar->insertWidget(ui->actionSettings, spacer);
  48. // Construct Widgets
  49. homeWidget = new HomeWidget(this);
  50. ui->stackedWidget->addWidget(homeWidget);
  51. pilotsWidget = new PilotsWidget(this);
  52. ui->stackedWidget->addWidget(pilotsWidget);
  53. logbookWidget = new LogbookWidget(this);
  54. ui->stackedWidget->addWidget(logbookWidget);
  55. settingsWidget = new SettingsWidget(this);
  56. ui->stackedWidget->addWidget(settingsWidget);
  57. aircraftWidget = new AircraftWidget(this);
  58. ui->stackedWidget->addWidget(aircraftWidget);
  59. debugWidget = new DebugWidget(this);
  60. ui->stackedWidget->addWidget(debugWidget);
  61. connectWidgets();
  62. // Startup Screen (Home Screen)
  63. ui->stackedWidget->setCurrentWidget(homeWidget);
  64. // check database version (Debug)
  65. int db_ver = checkDbVersion();
  66. if (db_ver != DATABASE_REVISION) {
  67. DEB << "############## WARNING ####################";
  68. DEB << "Your database is out of date.";
  69. DEB << "Current Revision:\t" << DATABASE_REVISION;
  70. DEB << "You have revision:\t" << db_ver;
  71. DEB << "############## WARNING ###################";
  72. QMessageBox message_box(this); //error box
  73. message_box.setText(tr("Database revision out of date!"));
  74. message_box.exec();
  75. } else {
  76. DEB << "Your database is up to date with the latest revision:" << db_ver;
  77. }
  78. }
  79. MainWindow::~MainWindow()
  80. {
  81. delete ui;
  82. }
  83. void MainWindow::nope()
  84. {
  85. QMessageBox message_box(this); //error box
  86. message_box.setText(tr("This feature is not yet available!"));
  87. message_box.exec();
  88. }
  89. /*
  90. * Slots
  91. */
  92. void MainWindow::on_actionQuit_triggered()
  93. {
  94. QApplication::quit();
  95. }
  96. void MainWindow::on_actionHome_triggered()
  97. {
  98. ui->stackedWidget->setCurrentWidget(homeWidget);
  99. }
  100. void MainWindow::on_actionLogbook_triggered()
  101. {
  102. ui->stackedWidget->setCurrentWidget(logbookWidget);
  103. }
  104. void MainWindow::on_actionDebug_triggered()
  105. {
  106. ui->stackedWidget->setCurrentWidget(debugWidget);
  107. }
  108. void MainWindow::connectWidgets()
  109. {
  110. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  111. logbookWidget, &LogbookWidget::onDisplayModel_dataBaseUpdated);
  112. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  113. pilotsWidget, &PilotsWidget::onDisplayModel_dataBaseUpdated);
  114. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  115. aircraftWidget, &AircraftWidget::onDisplayModel_dataBaseUpdated);
  116. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  117. homeWidget, &HomeWidget::onHomeWidget_dataBaseUpdated);
  118. QObject::connect(settingsWidget, &SettingsWidget::viewSelectionChanged,
  119. logbookWidget, &LogbookWidget::onLogbookWidget_viewSelectionChanged);
  120. }
  121. void MainWindow::readSettings()
  122. {
  123. // Set application style
  124. auto style_setting = ASettings::read(ASettings::Main::Style).toString();
  125. if (style_setting == QLatin1String("Dark-Palette")) {
  126. AStyle::setStyle(AStyle::darkPalette());
  127. return;
  128. }
  129. for (const auto &style_name : AStyle::styles) {
  130. if (style_setting == style_name) {
  131. AStyle::setStyle(style_name);
  132. return;
  133. }
  134. }
  135. for (const auto &style_sheet : AStyle::styleSheets) {
  136. if (style_setting == style_sheet.styleSheetName) {
  137. AStyle::setStyle(style_sheet);
  138. return;
  139. }
  140. }
  141. if (!ASettings::read(ASettings::Main::UseSystemFont).toBool()) {
  142. QFont font(ASettings::read(ASettings::Main::Font).toString());
  143. font.setPointSize(ASettings::read(ASettings::Main::FontSize).toUInt());
  144. qApp->setFont(font);
  145. DEB << "Font set:" << font;
  146. }
  147. }
  148. void MainWindow::on_actionSettings_triggered()
  149. {
  150. ui->stackedWidget->setCurrentWidget(settingsWidget);
  151. }
  152. void MainWindow::on_actionPilots_triggered()
  153. {
  154. ui->stackedWidget->setCurrentWidget(pilotsWidget);
  155. }
  156. void MainWindow::on_actionAircraft_triggered()
  157. {
  158. ui->stackedWidget->setCurrentWidget(aircraftWidget);
  159. }
  160. void MainWindow::on_actionNewFlight_triggered()
  161. {
  162. NewFlightDialog nf(this);
  163. nf.exec();
  164. }
  165. void MainWindow::on_actionNewAircraft_triggered()
  166. {
  167. NewTailDialog nt(QString(), this);
  168. nt.exec();
  169. }
  170. void MainWindow::on_actionNewPilot_triggered()
  171. {
  172. NewPilotDialog np(this);
  173. np.exec();
  174. }
  175. // Debug
  176. int MainWindow::checkDbVersion()
  177. {
  178. QSqlQuery query("SELECT COUNT(*) FROM changelog");
  179. query.next();
  180. return query.value(0).toInt();
  181. }