newpilotdialog.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "newpilotdialog.h"
  19. #include "src/gui/verification/completerprovider.h"
  20. #include "ui_newpilot.h"
  21. #include "src/opl.h"
  22. #include "src/database/database.h"
  23. #include "src/database/row.h"
  24. /*!
  25. * \brief NewPilotDialog::NewPilotDialog - creates a new pilot dialog which can be used to add a new entry to the database
  26. */
  27. NewPilotDialog::NewPilotDialog(QWidget *parent) :
  28. QDialog(parent),
  29. ui(new Ui::NewPilot)
  30. {
  31. setup();
  32. ui->lastnameLineEdit->setFocus();
  33. }
  34. /*!
  35. * \brief NewPilotDialog::NewPilotDialog - creates a new pilot dialog which can be used to edit an existing entry in the database
  36. * \param rowId - the rowid of the entry to be edited in the database
  37. */
  38. NewPilotDialog::NewPilotDialog(int rowId, QWidget *parent) :
  39. QDialog(parent),
  40. ui(new Ui::NewPilot)
  41. {
  42. setup();
  43. pilotEntry = DB->getPilotEntry(rowId);
  44. DEB << "Editing Pilot: " << pilotEntry;
  45. formFiller();
  46. ui->lastnameLineEdit->setFocus();
  47. }
  48. NewPilotDialog::~NewPilotDialog()
  49. {
  50. delete ui;
  51. }
  52. void NewPilotDialog::setup()
  53. {
  54. ui->setupUi(this);
  55. ui->companyLineEdit->setCompleter(QCompleterProvider.getCompleter(CompleterProvider::Companies));
  56. }
  57. void NewPilotDialog::on_buttonBox_accepted()
  58. {
  59. if (ui->lastnameLineEdit->text().isEmpty() || ui->firstnameLineEdit->text().isEmpty()) {
  60. QMessageBox message_box(this);
  61. message_box.setText(tr("Last Name and First Name are required."));
  62. message_box.exec();
  63. } else {
  64. submitForm();
  65. }
  66. }
  67. void NewPilotDialog::formFiller()
  68. {
  69. auto line_edits = this->findChildren<QLineEdit *>();
  70. for (const auto &le : line_edits) {
  71. auto key = le->objectName().remove(QStringLiteral("LineEdit"));
  72. le->setText(pilotEntry.getData().value(key).toString());
  73. }
  74. }
  75. void NewPilotDialog::submitForm()
  76. {
  77. OPL::RowData_T new_data;
  78. const auto line_edits = this->findChildren<QLineEdit *>();
  79. for(auto& le : line_edits) {
  80. auto key = le->objectName().remove(QStringLiteral("LineEdit"));
  81. auto value = le->text();
  82. new_data.insert(key, value);
  83. }
  84. pilotEntry.setData(new_data);
  85. DEB << "Submitting Pilot:";
  86. DEB << pilotEntry;
  87. if (!DB->commit(pilotEntry)) {
  88. QMessageBox message_box(this);
  89. message_box.setText(tr("The following error has ocurred:"
  90. "<br><br>%1<br><br>"
  91. "The entry has not been saved."
  92. ).arg(DB->lastError.text()));
  93. message_box.exec();
  94. return;
  95. } else {
  96. QDialog::accept();
  97. }
  98. }