mainwindow.cpp 12 KB

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