mainwindow.cpp 11 KB

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