mainwindow.cpp 11 KB

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