mainwindow.cpp 11 KB

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