mainwindow.cpp 10 KB

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