2
0

mainwindow.cpp 11 KB

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