mainwindow.cpp 9.4 KB

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