mainwindow.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. // Quick and dirty Debug area
  24. #include "src/gui/dialogues/firstrundialog.h"
  25. void MainWindow::doDebugStuff()
  26. {
  27. auto fr = new FirstRunDialog(this);
  28. fr->exec();
  29. }
  30. MainWindow::MainWindow(QWidget *parent)
  31. : QMainWindow(parent)
  32. , ui(new Ui::MainWindow)
  33. {
  34. ui->setupUi(this);
  35. // connect to the Database
  36. QFileInfo database_file(AStandardPaths::directory(AStandardPaths::Database).
  37. absoluteFilePath(QStringLiteral("logbook.db")));
  38. bool db_invalid = false;
  39. if (!database_file.exists()) {
  40. WARN(tr("Error: Database file not found."));
  41. db_invalid = true;
  42. }
  43. if (database_file.size() == 0) { // To Do: Check for database errors instead of just checking for empty
  44. WARN(tr("Database file invalid."));
  45. db_invalid = true;
  46. }
  47. if (db_invalid)
  48. onDatabaseInvalid();
  49. if(!aDB->connect()){
  50. WARN(tr("Error establishing database connection."));
  51. }
  52. // retreive completion lists and maps
  53. completionData.init();
  54. // Create a spacer for the toolbar to separate left and right parts
  55. auto *spacer = new QWidget();
  56. spacer->setMinimumWidth(1);
  57. spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
  58. // Set up Toolbar
  59. ui->actionHome->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_HOME));
  60. ui->actionNewFlight->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_NEW_FLIGHT));
  61. ui->actionLogbook->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_LOGBOOK));
  62. ui->actionAircraft->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_AIRCRAFT));
  63. ui->actionPilots->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_PILOT));
  64. ui->toolBar->insertWidget(ui->actionSettings, spacer); // spacer goes here
  65. ui->actionBackup->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_BACKUP));
  66. ui->actionSettings->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_SETTINGS));
  67. ui->actionQuit->setIcon(QIcon(Opl::Assets::ICON_TOOLBAR_QUIT));
  68. ui->toolBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
  69. const auto buttons = ui->toolBar->findChildren<QWidget *>();
  70. ui->toolBar->setIconSize(QSize(64, 64));
  71. for (const auto &button : buttons) {
  72. button->setMinimumWidth(128);
  73. }
  74. // Construct Widgets
  75. homeWidget = new HomeWidget(this);
  76. ui->stackedWidget->addWidget(homeWidget);
  77. logbookWidget = new LogbookWidget(completionData, this);
  78. ui->stackedWidget->addWidget(logbookWidget);
  79. aircraftWidget = new AircraftWidget(this);
  80. ui->stackedWidget->addWidget(aircraftWidget);
  81. pilotsWidget = new PilotsWidget(this);
  82. ui->stackedWidget->addWidget(pilotsWidget);
  83. backupWidget = new BackupWidget(this);
  84. ui->stackedWidget->addWidget(backupWidget);
  85. settingsWidget = new SettingsWidget(this);
  86. ui->stackedWidget->addWidget(settingsWidget);
  87. debugWidget = new DebugWidget(this);
  88. ui->stackedWidget->addWidget(debugWidget);
  89. connectWidgets();
  90. // Startup Screen (Home Screen)
  91. ui->stackedWidget->setCurrentWidget(homeWidget);
  92. // check database version (Debug)
  93. if (aDB->dbRevision() < aDB->getMinimumDatabaseRevision()) {
  94. QString message = tr("Your database is out of date."
  95. "Minimum required revision: %1<br>"
  96. "You have revision: %2<br>")
  97. .arg(aDB->getMinimumDatabaseRevision(), aDB->dbRevision());
  98. WARN(message);
  99. }
  100. }
  101. MainWindow::~MainWindow()
  102. {
  103. delete ui;
  104. }
  105. void MainWindow::nope()
  106. {
  107. QMessageBox message_box(this); //error box
  108. message_box.setText(tr("This feature is not yet available!"));
  109. message_box.exec();
  110. }
  111. /*!
  112. * \brief Connect the widgets to signals that are emitted when an update of the widegts' contents,
  113. * is required, either because the database has changed (model and view need to be refreshed) or
  114. * because a setting that affects the widgets layout has changed.
  115. */
  116. void MainWindow::connectWidgets()
  117. {
  118. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  119. homeWidget, &HomeWidget::refresh);
  120. QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
  121. homeWidget, &HomeWidget::refresh);
  122. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  123. logbookWidget, &LogbookWidget::refresh);
  124. QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
  125. logbookWidget, &LogbookWidget::onLogbookWidget_viewSelectionChanged);
  126. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  127. aircraftWidget, &AircraftWidget::onAircraftWidget_dataBaseUpdated);
  128. QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
  129. aircraftWidget, &AircraftWidget::onAircraftWidget_settingChanged);
  130. QObject::connect(aDB, &ADatabase::dataBaseUpdated,
  131. pilotsWidget, &PilotsWidget::onPilotsWidget_databaseUpdated);
  132. QObject::connect(settingsWidget, &SettingsWidget::settingChanged,
  133. pilotsWidget, &PilotsWidget::onPilotsWidget_settingChanged);
  134. QObject::connect(aDB, &ADatabase::connectionReset,
  135. logbookWidget, &LogbookWidget::repopulateModel);
  136. QObject::connect(aDB, &ADatabase::connectionReset,
  137. pilotsWidget, &PilotsWidget::repopulateModel);
  138. QObject::connect(aDB, &ADatabase::connectionReset,
  139. aircraftWidget, &AircraftWidget::repopulateModel);
  140. }
  141. void MainWindow::onDatabaseInvalid()
  142. {
  143. QMessageBox db_error(this);
  144. //db_error.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  145. db_error.addButton(tr("Restore Backup"), QMessageBox::ButtonRole::AcceptRole);
  146. db_error.addButton(tr("Create New Database"), QMessageBox::ButtonRole::RejectRole);
  147. db_error.addButton(tr("Abort"), QMessageBox::ButtonRole::DestructiveRole);
  148. db_error.setIcon(QMessageBox::Warning);
  149. db_error.setWindowTitle(tr("No valid database found"));
  150. db_error.setText(tr("No valid database has been found.<br>"
  151. "Would you like to create a new database or import a backup?"));
  152. int ret = db_error.exec();
  153. if (ret == QMessageBox::DestructiveRole) {
  154. DEB << "No valid database found. Exiting.";
  155. on_actionQuit_triggered();
  156. } else if (ret == QMessageBox::ButtonRole::AcceptRole) {
  157. DEB << "Yes(Import Backup)";
  158. QString db_path = QFileDialog::getOpenFileName(this,
  159. tr("Select Database"),
  160. AStandardPaths::directory(AStandardPaths::Backup).canonicalPath(),
  161. tr("Database file (*.db)"));
  162. if (!db_path.isEmpty()) {
  163. if(!aDB->restoreBackup(db_path)) {
  164. WARN(tr("Unable to restore Backup file: %1").arg(db_path));
  165. on_actionQuit_triggered();
  166. } else {
  167. INFO(tr("Backup successfully restored."));
  168. }
  169. }
  170. } else if (ret == QMessageBox::ButtonRole::RejectRole){
  171. DEB << "No(Create New)";
  172. if(FirstRunDialog().exec() == QDialog::Rejected){
  173. LOG << "Initial setup incomplete or unsuccessfull.";
  174. on_actionQuit_triggered();
  175. }
  176. ASettings::write(ASettings::Main::SetupComplete, true);
  177. LOG << "Initial Setup Completed successfully";
  178. }
  179. }
  180. /*
  181. * Slots
  182. */
  183. void MainWindow::on_actionHome_triggered()
  184. {
  185. ui->stackedWidget->setCurrentWidget(homeWidget);
  186. }
  187. void MainWindow::on_actionNewFlight_triggered()
  188. {
  189. // Make sure latest completion data is used, make this trigger only when needed
  190. TODO << "Trigger update only when needed.";
  191. completionData.update();
  192. NewFlightDialog nf(completionData,
  193. this);
  194. nf.exec();
  195. }
  196. void MainWindow::on_actionLogbook_triggered()
  197. {
  198. ui->stackedWidget->setCurrentWidget(logbookWidget);
  199. }
  200. void MainWindow::on_actionAircraft_triggered()
  201. {
  202. ui->stackedWidget->setCurrentWidget(aircraftWidget);
  203. }
  204. void MainWindow::on_actionPilots_triggered()
  205. {
  206. ui->stackedWidget->setCurrentWidget(pilotsWidget);
  207. }
  208. void MainWindow::on_actionBackup_triggered()
  209. {
  210. ui->stackedWidget->setCurrentWidget(backupWidget);
  211. }
  212. void MainWindow::on_actionSettings_triggered()
  213. {
  214. ui->stackedWidget->setCurrentWidget(settingsWidget);
  215. }
  216. void MainWindow::on_actionQuit_triggered()
  217. {
  218. QApplication::quit();
  219. }
  220. void MainWindow::on_actionDebug_triggered()
  221. {
  222. ui->stackedWidget->setCurrentWidget(debugWidget);
  223. }
  224. // Debug
  225. // not used at the moment
  226. /*void MainWindow::on_actionNewAircraft_triggered()
  227. {
  228. NewTailDialog nt(QString(), this);
  229. nt.exec();
  230. }
  231. void MainWindow::on_actionNewPilot_triggered()
  232. {
  233. NewPilotDialog np(this);
  234. np.exec();
  235. }*/