firstrundialog.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "firstrundialog.h"
  2. #include "ui_firstrundialog.h"
  3. #include "src/testing/adebug.h"
  4. #include "src/database/adatabase.h"
  5. #include "src/database/adatabasesetup.h"
  6. #include "src/classes/apilotentry.h"
  7. #include "src/classes/adownload.h"
  8. #include "src/classes/asettings.h"
  9. #include "src/classes/astandardpaths.h"
  10. #include <QErrorMessage>
  11. FirstRunDialog::FirstRunDialog(QWidget *parent) :
  12. QDialog(parent),
  13. ui(new Ui::FirstRunDialog)
  14. {
  15. ui->setupUi(this);
  16. ui->stackedWidget->setCurrentIndex(0);
  17. ui->lastnameLineEdit->setFocus();
  18. ui->previousPushButton->setEnabled(false);
  19. ui->nightComboBox->setCurrentIndex(1);
  20. // auto *themeGroup = new QButtonGroup;
  21. // themeGroup->addButton(ui->systemThemeCheckBox, 0);
  22. // themeGroup->addButton(ui->lightThemeCheckBox, 1);
  23. // themeGroup->addButton(ui->darkThemeCheckBox, 2);
  24. // QObject::connect(themeGroup, QOverload<int>::of(&QButtonGroup::buttonClicked),
  25. // this, &FirstRunDialog::on_themeGroup_toggled);
  26. }
  27. FirstRunDialog::~FirstRunDialog()
  28. {
  29. delete ui;
  30. }
  31. void FirstRunDialog::on_previousPushButton_clicked()
  32. {
  33. auto current_idx = ui->stackedWidget->currentIndex();
  34. switch (current_idx) {
  35. case 0:
  36. return;
  37. case 1:
  38. ui->previousPushButton->setEnabled(false);
  39. break;
  40. case 2:
  41. ui->nextPushButton->setText(QStringLiteral("Next"));
  42. break;
  43. }
  44. ui->stackedWidget->setCurrentIndex(current_idx - 1);
  45. }
  46. void FirstRunDialog::on_nextPushButton_clicked()
  47. {
  48. auto current_idx = ui->stackedWidget->currentIndex();
  49. // [G]: per index do appropriate error handling
  50. switch (current_idx) {
  51. case 0:
  52. if(ui->firstnameLineEdit->text().isEmpty()
  53. || ui->lastnameLineEdit->text().isEmpty())
  54. {
  55. QMessageBox(QMessageBox::Warning, QStringLiteral("Error"),
  56. QStringLiteral("Please enter first and last name")
  57. ).exec();
  58. return;
  59. }
  60. ui->previousPushButton->setEnabled(true);
  61. break;
  62. case 1:
  63. ui->nextPushButton->setText(QStringLiteral("Done"));
  64. break;
  65. case 2:
  66. if(!finish())
  67. QDialog::reject();
  68. else
  69. QDialog::accept();
  70. return;
  71. }
  72. ui->stackedWidget->setCurrentIndex(current_idx + 1);
  73. }
  74. //void FirstRunDialog::on_themeGroup_toggled(int id)
  75. //{
  76. // ASettings::write(ASettings::Main::Theme, id);
  77. //}
  78. bool FirstRunDialog::finish()
  79. {
  80. ASettings::write(ASettings::FlightLogging::Function, ui->functionComboBox->currentText());
  81. ASettings::write(ASettings::FlightLogging::Approach, ui->approachComboBox->currentText());
  82. ASettings::write(ASettings::FlightLogging::NightLogging, ui->nightComboBox->currentIndex());
  83. ASettings::write(ASettings::FlightLogging::LogIFR, ui->rulesComboBox->currentIndex());
  84. ASettings::write(ASettings::FlightLogging::FlightNumberPrefix, ui->prefixLineEdit->text());
  85. ASettings::write(ASettings::FlightLogging::NumberTakeoffs, 1);
  86. ASettings::write(ASettings::FlightLogging::NumberLandings, 1);
  87. ASettings::write(ASettings::FlightLogging::PopupCalendar, true);
  88. ASettings::write(ASettings::FlightLogging::PilotFlying, true);
  89. QMap<QString, QVariant> data;
  90. ASettings::write(ASettings::UserData::DisplaySelfAs, ui->aliasComboBox->currentIndex());
  91. data.insert(DB_PILOTS_LASTNAME, ui->lastnameLineEdit->text());
  92. data.insert(DB_PILOTS_FIRSTNAME, ui->firstnameLineEdit->text());
  93. data.insert(DB_PILOTS_ALIAS, QStringLiteral("self"));
  94. data.insert(DB_PILOTS_EMPLOYEEID, ui->employeeidLineEdit->text());
  95. data.insert(DB_PILOTS_PHONE, ui->phoneLineEdit->text());
  96. data.insert(DB_PILOTS_EMAIL, ui->emailLineEdit->text());
  97. auto db_fail_msg_box = QMessageBox(QMessageBox::Critical, QStringLiteral("Database setup failed"),
  98. QStringLiteral("Errors have ocurred creating the database."
  99. "Without a working database The application will not be usable."));
  100. if (!setupDatabase()) {
  101. db_fail_msg_box.exec();
  102. return false;
  103. }
  104. aDB()->updateLayout();
  105. auto pilot = APilotEntry(1);
  106. pilot.setData(data);
  107. if(!aDB()->commit(pilot)){
  108. db_fail_msg_box.exec();
  109. return false;
  110. }
  111. return true;
  112. }
  113. bool FirstRunDialog::setupDatabase()
  114. {
  115. auto confirm = QMessageBox(QMessageBox::Question, QStringLiteral("Create Database"),
  116. QStringLiteral("We are now going to create the database.<br>" // [G]: Why both <br> and \n ?
  117. "Would you like to download the latest database information?"
  118. "<br>(Recommended, Internet connection required)"),
  119. QMessageBox::Yes | QMessageBox::No, this);
  120. confirm.setDefaultButton(QMessageBox::No);
  121. if (confirm.exec() == QMessageBox::Yes)
  122. ADataBaseSetup::downloadTemplates();
  123. aDB()->disconnect();
  124. ADataBaseSetup::backupOldData();
  125. aDB()->connect();
  126. // [F]: todo: handle unsuccessful steps
  127. if(!ADataBaseSetup::createDatabase())
  128. return false;
  129. aDB()->updateLayout();
  130. if(!ADataBaseSetup::importDefaultData())
  131. return false;
  132. return true;
  133. }
  134. void FirstRunDialog::reject()
  135. {
  136. auto confirm = QMessageBox(QMessageBox::Critical,
  137. QStringLiteral("Setup incomplete"),
  138. QStringLiteral("Without completing the initial setup"
  139. " you cannot use the application.<br><br>"
  140. "Quit anyway?"),
  141. QMessageBox::Yes | QMessageBox::No, this);
  142. confirm.setDefaultButton(QMessageBox::No);
  143. if (confirm.exec() == QMessageBox::Yes) {
  144. DEB << "rejected.";
  145. QDialog::reject();
  146. }
  147. }