newpilotdialog.cpp 3.5 KB

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