mainwindow.cpp 11 KB

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