2
0

mainwindow.cpp 9.4 KB

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