firstrundialog.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2021 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 "firstrundialog.h"
  19. #include "ui_firstrundialog.h"
  20. #include "src/functions/alog.h"
  21. #include "src/database/adatabase.h"
  22. #include "src/database/adatabasesetup.h"
  23. #include "src/classes/apilotentry.h"
  24. #include "src/classes/adownload.h"
  25. #include "src/classes/asettings.h"
  26. #include "src/opl.h"
  27. #include <QErrorMessage>
  28. #include "src/classes/astyle.h"
  29. FirstRunDialog::FirstRunDialog(QWidget *parent) :
  30. QDialog(parent),
  31. ui(new Ui::FirstRunDialog)
  32. {
  33. ui->setupUi(this);
  34. ui->stackedWidget->setCurrentIndex(0);
  35. ui->lastnameLineEdit->setFocus();
  36. ui->previousPushButton->setEnabled(false);
  37. ui->logoLabel->setPixmap(QPixmap(Opl::Assets::LOGO));
  38. // approach Combo Box
  39. for (const auto &approach : Opl::ApproachTypes){
  40. ui->approachComboBox->addItem(approach);
  41. }
  42. // Style combo box
  43. const QSignalBlocker blocker_style(ui->styleComboBox);
  44. ui->styleComboBox->addItems(AStyle::styles);
  45. for (const auto &style_sheet : AStyle::styleSheets) {
  46. ui->styleComboBox->addItem(style_sheet.styleSheetName);
  47. }
  48. ui->styleComboBox->addItem(QStringLiteral("Dark-Palette"));
  49. ui->styleComboBox->model()->sort(0);
  50. ui->styleComboBox->setCurrentText(AStyle::defaultStyle);
  51. // Set Date Edits for currencies
  52. for (const auto date_edit : this->findChildren<QDateEdit *>())
  53. date_edit->setDate(QDate::currentDate());
  54. }
  55. FirstRunDialog::~FirstRunDialog()
  56. {
  57. delete ui;
  58. }
  59. void FirstRunDialog::on_previousPushButton_clicked()
  60. {
  61. auto current_index = ui->stackedWidget->currentIndex();
  62. switch (current_index) {
  63. case 0:
  64. return;
  65. case 1:
  66. ui->previousPushButton->setEnabled(false);
  67. break;
  68. case 2:
  69. ui->nextPushButton->setText(tr("Next"));
  70. break;
  71. }
  72. ui->stackedWidget->setCurrentIndex(current_index - 1);
  73. }
  74. void FirstRunDialog::on_nextPushButton_clicked()
  75. {
  76. auto current_index = ui->stackedWidget->currentIndex();
  77. switch (current_index) {
  78. case 0:
  79. if(ui->firstnameLineEdit->text().isEmpty()
  80. || ui->lastnameLineEdit->text().isEmpty())
  81. {
  82. QMessageBox(QMessageBox::Warning, tr("Error"),
  83. tr("Please enter first and last name")
  84. ).exec();
  85. return;
  86. }
  87. ui->previousPushButton->setEnabled(true);
  88. break;
  89. case 3:
  90. ui->nextPushButton->setText(tr("Done"));
  91. break;
  92. case 4:
  93. if(!finishSetup())
  94. QDialog::reject();
  95. else
  96. QDialog::accept();
  97. return;
  98. }
  99. ui->stackedWidget->setCurrentIndex(current_index + 1);
  100. }
  101. bool FirstRunDialog::finishSetup()
  102. {
  103. writeSettings();
  104. QFileInfo database_file(AStandardPaths::directory(AStandardPaths::Database).
  105. absoluteFilePath(QStringLiteral("logbook.db")));
  106. if (database_file.exists()) {
  107. QMessageBox message_box(QMessageBox::Critical, tr("Database found"),
  108. tr("Warning."
  109. "An existing database file has been detected on your system.<br>"
  110. "A backup copy of the existing database will be created at this location:<br>"
  111. "%1").arg(
  112. QDir::cleanPath(AStandardPaths::directory(AStandardPaths::Backup).canonicalPath())));
  113. message_box.exec();
  114. ADataBaseSetup::backupOldData();
  115. }
  116. if (!aDB->connect()) {
  117. QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
  118. tr("Errors have ocurred creating the database."
  119. "Without a working database The application will not be usable.<br>"
  120. "The following error has ocurred:<br>"
  121. "Database: Unable to connect"));
  122. message_box.exec();
  123. return false;
  124. }
  125. if (!setupDatabase()) {
  126. QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
  127. tr("Errors have ocurred creating the database."
  128. "Without a working database The application will not be usable.<br>"
  129. "The following error has ocurred:<br>%1"
  130. ).arg(aDB->lastError.text()));
  131. message_box.exec();
  132. return false;
  133. }
  134. if (!createUserEntry()) {
  135. QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
  136. tr("Unable to execute database query<br>"
  137. "The following error has occured:<br>%1"
  138. ).arg(aDB->lastError.text()));
  139. message_box.exec();
  140. return false;
  141. }
  142. if (!writeCurrencies()) {
  143. QMessageBox message_box(QMessageBox::Critical, tr("Database setup failed"),
  144. tr("Unable to execute database query<br>"
  145. "The following error has occured:<br>%1"
  146. ).arg(aDB->lastError.text()));
  147. message_box.exec();
  148. return false;
  149. }
  150. aDB->disconnect(); // connection will be re-established by main()
  151. return true;
  152. }
  153. void FirstRunDialog::writeSettings()
  154. {
  155. ASettings::resetToDefaults();
  156. ASettings::write(ASettings::FlightLogging::Function, ui->functionComboBox->currentText());
  157. ASettings::write(ASettings::FlightLogging::Approach, ui->approachComboBox->currentIndex());
  158. switch (ui->nightComboBox->currentIndex()) {
  159. case 0:
  160. ASettings::write(ASettings::FlightLogging::NightLoggingEnabled, true);
  161. break;
  162. case 1:
  163. ASettings::write(ASettings::FlightLogging::NightLoggingEnabled, false);
  164. break;
  165. default:
  166. ASettings::write(ASettings::FlightLogging::NightLoggingEnabled, true);
  167. break;
  168. }
  169. switch (ui->nightRulesComboBox->currentIndex()) {
  170. case 0:
  171. ASettings::write(ASettings::FlightLogging::NightAngle, 6);
  172. break;
  173. case 1:
  174. ASettings::write(ASettings::FlightLogging::NightAngle, 0);
  175. break;
  176. }
  177. ASettings::write(ASettings::FlightLogging::LogIFR, ui->rulesComboBox->currentIndex());
  178. ASettings::write(ASettings::FlightLogging::FlightNumberPrefix, ui->prefixLineEdit->text());
  179. ASettings::write(ASettings::FlightLogging::FlightTimeFormat, Opl::Time::Default);
  180. ASettings::write(ASettings::UserData::DisplaySelfAs, ui->aliasComboBox->currentIndex());
  181. ASettings::write(ASettings::Main::LogbookView, ui->logbookViewComboBox->currentIndex());
  182. switch (ui->currWarningCheckBox->checkState()) {
  183. case Qt::CheckState::Checked:
  184. ASettings::write(ASettings::UserData::CurrWarningEnabled, true);
  185. break;
  186. case Qt::CheckState::Unchecked:
  187. ASettings::write(ASettings::UserData::CurrWarningEnabled, false);
  188. break;
  189. default:
  190. break;
  191. }
  192. ASettings::write(ASettings::UserData::CurrWarningThreshold, ui->currWarningThresholdSpinBox->value());
  193. ASettings::write(ASettings::Main::Style, ui->styleComboBox->currentText());
  194. QSettings settings;
  195. settings.sync();
  196. }
  197. bool FirstRunDialog::setupDatabase()
  198. {
  199. QMessageBox confirm(QMessageBox::Question, tr("Create Database"),
  200. tr("We are now going to create the database.<br>"
  201. "Would you like to download the latest database information?"
  202. "<br>(Recommended, Internet connection required)"),
  203. QMessageBox::Yes | QMessageBox::No, this);
  204. confirm.setDefaultButton(QMessageBox::No);
  205. if (confirm.exec() == QMessageBox::Yes) {
  206. useRessourceData = false;
  207. if (!ADataBaseSetup::downloadTemplates()) {
  208. QMessageBox message_box(this);
  209. message_box.setText(tr("Downloading latest data has failed.<br><br>Using local data instead."));
  210. message_box.exec();
  211. useRessourceData = true; // fall back
  212. }
  213. } else {
  214. useRessourceData = true;
  215. }
  216. if(!ADataBaseSetup::createDatabase())
  217. return false;
  218. aDB->updateLayout();
  219. if(!ADataBaseSetup::importDefaultData(useRessourceData))
  220. return false;
  221. aDB->updateLayout();
  222. return true;
  223. }
  224. bool FirstRunDialog::createUserEntry()
  225. {
  226. QMap<QString, QVariant> data;
  227. data.insert(Opl::Db::PILOTS_LASTNAME, ui->lastnameLineEdit->text());
  228. data.insert(Opl::Db::PILOTS_FIRSTNAME, ui->firstnameLineEdit->text());
  229. data.insert(Opl::Db::PILOTS_ALIAS, QStringLiteral("self"));
  230. data.insert(Opl::Db::PILOTS_EMPLOYEEID, ui->employeeidLineEdit->text());
  231. data.insert(Opl::Db::PILOTS_PHONE, ui->phoneLineEdit->text());
  232. data.insert(Opl::Db::PILOTS_EMAIL, ui->emailLineEdit->text());
  233. auto pilot = APilotEntry(1);
  234. pilot.setData(data);
  235. return aDB->commit(pilot);
  236. }
  237. bool FirstRunDialog::writeCurrencies()
  238. {
  239. const QList<QPair<ACurrencyEntry::CurrencyName, QDateEdit*>> currencies_list = {
  240. {ACurrencyEntry::CurrencyName::Licence, ui->currLicDateEdit},
  241. {ACurrencyEntry::CurrencyName::TypeRating, ui->currTrDateEdit},
  242. {ACurrencyEntry::CurrencyName::LineCheck, ui->currLckDateEdit},
  243. {ACurrencyEntry::CurrencyName::Medical, ui->currMedDateEdit},
  244. {ACurrencyEntry::CurrencyName::Custom1, ui->currCustom1DateEdit},
  245. {ACurrencyEntry::CurrencyName::Custom2, ui->currCustom1DateEdit},
  246. };
  247. QDate today = QDate::currentDate();
  248. for (const auto &pair : currencies_list) {
  249. // only write dates that have been edited
  250. if (pair.second->date() != today) {
  251. ACurrencyEntry entry(pair.first, pair.second->date());
  252. if (!aDB->commit(entry))
  253. return false;
  254. }
  255. }
  256. return true;
  257. }
  258. void FirstRunDialog::reject()
  259. {
  260. QMessageBox confirm(QMessageBox::Critical,
  261. tr("Setup incomplete"),
  262. tr("Without completing the initial setup"
  263. " you cannot use the application.<br><br>"
  264. "Quit anyway?"),
  265. QMessageBox::Yes | QMessageBox::No, this);
  266. confirm.setDefaultButton(QMessageBox::No);
  267. if (confirm.exec() == QMessageBox::Yes) {
  268. DEB << "rejected.";
  269. QDialog::reject();
  270. }
  271. }
  272. void FirstRunDialog::on_styleComboBox_currentTextChanged(const QString &new_style_setting)
  273. {
  274. DEB << "style selected:"<<new_style_setting;
  275. if (new_style_setting == QLatin1String("Dark-Palette")) {
  276. //DEB << "Palette";
  277. AStyle::setStyle(AStyle::darkPalette());
  278. return;
  279. }
  280. for (const auto &style_name : AStyle::styles) {
  281. if (new_style_setting == style_name) {
  282. //DEB << "style";
  283. AStyle::setStyle(style_name);
  284. return;
  285. }
  286. }
  287. for (const auto &style_sheet : AStyle::styleSheets) {
  288. if (new_style_setting == style_sheet.styleSheetName) {
  289. //DEB << "stylesheet";
  290. AStyle::setStyle(style_sheet);
  291. return;
  292. }
  293. }
  294. }
  295. void FirstRunDialog::on_currWarningCheckBox_stateChanged(int arg1)
  296. {
  297. switch (arg1) {
  298. case Qt::CheckState::Checked:
  299. ASettings::write(ASettings::UserData::CurrWarningEnabled, true);
  300. break;
  301. case Qt::CheckState::Unchecked:
  302. ASettings::write(ASettings::UserData::CurrWarningEnabled, false);
  303. break;
  304. default:
  305. break;
  306. }
  307. ASettings::write(ASettings::UserData::CurrWarningThreshold, arg1);
  308. }
  309. void FirstRunDialog::on_currWarningThresholdSpinBox_valueChanged(int arg1)
  310. {
  311. ASettings::write(ASettings::UserData::CurrWarningThreshold, arg1);
  312. }
  313. void FirstRunDialog::on_currCustom1LineEdit_editingFinished()
  314. {
  315. ASettings::write(ASettings::UserData::Custom1CurrencyName, ui->currCustom1LineEdit->text());
  316. }
  317. void FirstRunDialog::on_currCustom2LineEdit_editingFinished()
  318. {
  319. ASettings::write(ASettings::UserData::Custom2CurrencyName, ui->currCustom2LineEdit->text());
  320. }