mainwindow.cpp 9.7 KB

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