mainwindow.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/functions/alog.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. // connect to the Database
  29. TODO << "Create more verbose warning about DB and offer instructions how to fix it.";
  30. QFileInfo database_file(AStandardPaths::directory(AStandardPaths::Database).
  31. absoluteFilePath(QStringLiteral("logbook.db")));
  32. if (!database_file.exists()) {
  33. WARN(tr("Error: Database file not found."));
  34. }
  35. if(!aDB->connect()){
  36. WARN(tr("Error establishing database connection."));
  37. }
  38. // Create a spacer for the toolbar to separate left and right parts
  39. auto *spacer = new QWidget();
  40. spacer->setMinimumWidth(1);
  41. spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
  42. // Set up Toolbar
  43. ui->actionHome->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_HOME));
  44. ui->actionNewFlight->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_NEW_FLIGHT));
  45. ui->actionLogbook->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_LOGBOOK));
  46. ui->actionAircraft->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_AIRCRAFT));
  47. ui->actionPilots->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_PILOT));
  48. ui->toolBar->insertWidget(ui->actionSettings, spacer); // spacer goes here
  49. ui->actionBackup->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_BACKUP));
  50. ui->actionSettings->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_SETTINGS));
  51. ui->actionQuit->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_QUIT));
  52. ui->toolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
  53. const auto buttons = ui->toolBar->findChildren<QWidget *>();
  54. ui->toolBar->setIconSize(QSize(64, 64));
  55. for (const auto &button : buttons) {
  56. button->setMinimumWidth(128);
  57. }
  58. // Construct Widgets
  59. homeWidget = new HomeWidget(this);
  60. ui->stackedWidget->addWidget(homeWidget);
  61. logbookWidget = new LogbookWidget(this);
  62. ui->stackedWidget->addWidget(logbookWidget);
  63. aircraftWidget = new AircraftWidget(this);
  64. ui->stackedWidget->addWidget(aircraftWidget);
  65. pilotsWidget = new PilotsWidget(this);
  66. ui->stackedWidget->addWidget(pilotsWidget);
  67. backupWidget = new BackupWidget(this);
  68. ui->stackedWidget->addWidget(backupWidget);
  69. settingsWidget = new SettingsWidget(this);
  70. ui->stackedWidget->addWidget(settingsWidget);
  71. debugWidget = new DebugWidget(this);
  72. ui->stackedWidget->addWidget(debugWidget);
  73. connectWidgets();
  74. // Startup Screen (Home Screen)
  75. ui->stackedWidget->setCurrentWidget(homeWidget);
  76. // check database version (Debug)
  77. int db_ver = aDB->dbVersion();
  78. if (db_ver != DATABASE_REVISION) {
  79. DEB << "############## WARNING ##############";
  80. DEB << "Your database is out of date.";
  81. DEB << "Current Revision:\t" << DATABASE_REVISION;
  82. DEB << "You have revision:\t" << db_ver;
  83. DEB << "############## WARNING ##############";
  84. QMessageBox message_box(this); //error box
  85. message_box.setText(tr("Database revision out of date!"));
  86. message_box.exec();
  87. } else {
  88. DEB << "Your database is up to date with the latest revision:" << db_ver;
  89. }
  90. }
  91. MainWindow::~MainWindow()
  92. {
  93. delete ui;
  94. }
  95. void MainWindow::nope()
  96. {
  97. QMessageBox message_box(this); //error box
  98. message_box.setText(tr("This feature is not yet available!"));
  99. message_box.exec();
  100. }
  101. /*!
  102. * \brief Connect the widgets to signals that are emitted when an update of the widegts' contents,
  103. * is required, either because the database has changed (model and view need to be refreshed) or
  104. * because a setting that affects the widgets layout has changed.
  105. */
  106. void MainWindow::connectWidgets()
  107. {
  108. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  109. homeWidget, &HomeWidget::refresh);
  110. QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
  111. homeWidget, &HomeWidget::refresh);
  112. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  113. logbookWidget, &LogbookWidget::refresh);
  114. QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
  115. logbookWidget, &LogbookWidget::onLogbookWidget_viewSelectionChanged);
  116. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  117. aircraftWidget, &AircraftWidget::onAircraftWidget_dataBaseUpdated);
  118. QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
  119. aircraftWidget, &AircraftWidget::onAircraftWidget_settingChanged);
  120. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  121. pilotsWidget, &PilotsWidget::onPilotsWidget_databaseUpdated);
  122. QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
  123. pilotsWidget, &PilotsWidget::onPilotsWidget_settingChanged);
  124. QObject::connect(aDB, &ADatabase::connectionReset,
  125. logbookWidget, &LogbookWidget::repopulateModel);
  126. QObject::connect(aDB, &ADatabase::connectionReset,
  127. pilotsWidget, &PilotsWidget::repopulateModel);
  128. QObject::connect(aDB, &ADatabase::connectionReset,
  129. aircraftWidget, &AircraftWidget::repopulateModel);
  130. }
  131. /*
  132. * Slots
  133. */
  134. void MainWindow::on_actionHome_triggered()
  135. {
  136. ui->stackedWidget->setCurrentWidget(homeWidget);
  137. }
  138. void MainWindow::on_actionNewFlight_triggered()
  139. {
  140. NewFlightDialog nf(this);
  141. nf.exec();
  142. }
  143. void MainWindow::on_actionLogbook_triggered()
  144. {
  145. ui->stackedWidget->setCurrentWidget(logbookWidget);
  146. }
  147. void MainWindow::on_actionAircraft_triggered()
  148. {
  149. ui->stackedWidget->setCurrentWidget(aircraftWidget);
  150. }
  151. void MainWindow::on_actionPilots_triggered()
  152. {
  153. ui->stackedWidget->setCurrentWidget(pilotsWidget);
  154. }
  155. void MainWindow::on_actionBackup_triggered()
  156. {
  157. ui->stackedWidget->setCurrentWidget(backupWidget);
  158. }
  159. void MainWindow::on_actionSettings_triggered()
  160. {
  161. ui->stackedWidget->setCurrentWidget(settingsWidget);
  162. }
  163. void MainWindow::on_actionQuit_triggered()
  164. {
  165. QApplication::quit();
  166. }
  167. void MainWindow::on_actionDebug_triggered()
  168. {
  169. ui->stackedWidget->setCurrentWidget(debugWidget);
  170. }
  171. // Debug
  172. // not used at the moment
  173. /*void MainWindow::on_actionNewAircraft_triggered()
  174. {
  175. NewTailDialog nt(QString(), this);
  176. nt.exec();
  177. }
  178. void MainWindow::on_actionNewPilot_triggered()
  179. {
  180. NewPilotDialog np(this);
  181. np.exec();
  182. }*/