mainwindow.cpp 9.6 KB

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