2
0

mainwindow.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. if (aDB->dbRevision() < aDB->getMinimumDatabaseRevision()) {
  78. QString message = tr("Your database is out of date."
  79. "Minimum required revision: %1<br>"
  80. "You have revision: %2<br>")
  81. .arg(aDB->getMinimumDatabaseRevision(), aDB->dbRevision());
  82. WARN(message);
  83. }
  84. }
  85. MainWindow::~MainWindow()
  86. {
  87. delete ui;
  88. }
  89. void MainWindow::nope()
  90. {
  91. QMessageBox message_box(this); //error box
  92. message_box.setText(tr("This feature is not yet available!"));
  93. message_box.exec();
  94. }
  95. /*!
  96. * \brief Connect the widgets to signals that are emitted when an update of the widegts' contents,
  97. * is required, either because the database has changed (model and view need to be refreshed) or
  98. * because a setting that affects the widgets layout has changed.
  99. */
  100. void MainWindow::connectWidgets()
  101. {
  102. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  103. homeWidget, &HomeWidget::refresh);
  104. QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
  105. homeWidget, &HomeWidget::refresh);
  106. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  107. logbookWidget, &LogbookWidget::refresh);
  108. QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
  109. logbookWidget, &LogbookWidget::onLogbookWidget_viewSelectionChanged);
  110. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  111. aircraftWidget, &AircraftWidget::onAircraftWidget_dataBaseUpdated);
  112. QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
  113. aircraftWidget, &AircraftWidget::onAircraftWidget_settingChanged);
  114. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  115. pilotsWidget, &PilotsWidget::onPilotsWidget_databaseUpdated);
  116. QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
  117. pilotsWidget, &PilotsWidget::onPilotsWidget_settingChanged);
  118. QObject::connect(aDB, &ADatabase::connectionReset,
  119. logbookWidget, &LogbookWidget::repopulateModel);
  120. QObject::connect(aDB, &ADatabase::connectionReset,
  121. pilotsWidget, &PilotsWidget::repopulateModel);
  122. QObject::connect(aDB, &ADatabase::connectionReset,
  123. aircraftWidget, &AircraftWidget::repopulateModel);
  124. }
  125. /*
  126. * Slots
  127. */
  128. void MainWindow::on_actionHome_triggered()
  129. {
  130. ui->stackedWidget->setCurrentWidget(homeWidget);
  131. }
  132. void MainWindow::on_actionNewFlight_triggered()
  133. {
  134. NewFlightDialog nf(this);
  135. nf.exec();
  136. }
  137. void MainWindow::on_actionLogbook_triggered()
  138. {
  139. ui->stackedWidget->setCurrentWidget(logbookWidget);
  140. }
  141. void MainWindow::on_actionAircraft_triggered()
  142. {
  143. ui->stackedWidget->setCurrentWidget(aircraftWidget);
  144. }
  145. void MainWindow::on_actionPilots_triggered()
  146. {
  147. ui->stackedWidget->setCurrentWidget(pilotsWidget);
  148. }
  149. void MainWindow::on_actionBackup_triggered()
  150. {
  151. ui->stackedWidget->setCurrentWidget(backupWidget);
  152. }
  153. void MainWindow::on_actionSettings_triggered()
  154. {
  155. ui->stackedWidget->setCurrentWidget(settingsWidget);
  156. }
  157. void MainWindow::on_actionQuit_triggered()
  158. {
  159. QApplication::quit();
  160. }
  161. void MainWindow::on_actionDebug_triggered()
  162. {
  163. ui->stackedWidget->setCurrentWidget(debugWidget);
  164. }
  165. // Debug
  166. // not used at the moment
  167. /*void MainWindow::on_actionNewAircraft_triggered()
  168. {
  169. NewTailDialog nt(QString(), this);
  170. nt.exec();
  171. }
  172. void MainWindow::on_actionNewPilot_triggered()
  173. {
  174. NewPilotDialog np(this);
  175. np.exec();
  176. }*/