newpilotdialog.cpp 3.4 KB

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