mainwindow.cpp 11 KB

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