newpilotdialog.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. *openPilot Log - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020 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/database/adatabase.h"
  21. #include "src/classes/aentry.h"
  22. #include "src/testing/adebug.h"
  23. /* Examples for names around the world:
  24. * José Eduardo Santos Tavares Melo Silva
  25. * María José Carreño Quiñones
  26. * 毛泽东先生 (Mao Ze Dong xiān shēng)
  27. * Борис Николаевич Ельцин (Boris Nikolayevich Yeltsin)
  28. * John Q. Public
  29. * Abu Karim Muhammad al-Jamil ibn Nidal ibn Abdulaziz al-Filistini
  30. * Nguyễn Tấn Dũng
  31. * 東海林賢蔵
  32. * Chris van de Hoopen
  33. * Karl-Gustav von Meiershausen
  34. * Mathias d'Arras
  35. * Martin Luther King, Jr.
  36. */
  37. static const auto NAME_RX = QLatin1String("(\\p{L}+(\\s|'|\\-)?\\s?(\\p{L}+)?\\s?)");
  38. static const auto FIRSTNAME_VALID = QPair<QString, QRegularExpression> {
  39. "firstnameLineEdit", QRegularExpression(NAME_RX + NAME_RX + NAME_RX)};
  40. static const auto LASTNAME_VALID = QPair<QString, QRegularExpression> {
  41. "lastnameLineEdit", QRegularExpression(NAME_RX + NAME_RX + NAME_RX)};
  42. static const auto PHONE_VALID = QPair<QString, QRegularExpression> {
  43. "phoneLineEdit", QRegularExpression("^[+]{0,1}[0-9\\-\\s]+")};
  44. static const auto EMAIL_VALID = QPair<QString, QRegularExpression> {
  45. "emailLineEdit", QRegularExpression(
  46. "\\A[a-z0-9!#$%&'*+/=?^_‘{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_‘{|}~-]+)*@"
  47. "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\z")};
  48. static const auto COMPANY_VALID = QPair<QString, QRegularExpression> {
  49. "companyLineEdit", QRegularExpression("\\w+(\\s|\\-)?(\\w+(\\s|\\-)?)?(\\w+(\\s|\\-)?)?")};
  50. static const auto EMPLOYEENR_VALID = QPair<QString, QRegularExpression> {
  51. "employeeidLineEdit", QRegularExpression("\\w+")};
  52. static const auto LINE_EDIT_VALIDATORS = QVector{
  53. FIRSTNAME_VALID,
  54. LASTNAME_VALID,
  55. PHONE_VALID,
  56. EMAIL_VALID,
  57. COMPANY_VALID,
  58. EMPLOYEENR_VALID
  59. };
  60. // For creating a new entry
  61. NewPilotDialog::NewPilotDialog(QWidget *parent) :
  62. QDialog(parent),
  63. ui(new Ui::NewPilot)
  64. {
  65. DEB << "New NewPilotDialog (newEntry)";
  66. setup();
  67. pilotEntry = APilotEntry();
  68. ui->lastnameLineEdit->setFocus();
  69. }
  70. NewPilotDialog::NewPilotDialog(int rowId, QWidget *parent) :
  71. QDialog(parent),
  72. ui(new Ui::NewPilot)
  73. {
  74. DEB << "New NewPilotDialog (editEntry)";
  75. setup();
  76. pilotEntry = aDB->getPilotEntry(rowId);
  77. DEB << "Pilot Entry position: " << pilotEntry.getPosition().tableName << pilotEntry.getPosition().rowId;
  78. formFiller();
  79. ui->lastnameLineEdit->setFocus();
  80. }
  81. NewPilotDialog::~NewPilotDialog()
  82. {
  83. DEB << "Deleting New NewPilotDialog";
  84. delete ui;
  85. }
  86. void NewPilotDialog::setup()
  87. {
  88. ui->setupUi(this);
  89. DEB << "Setting up Validators...";
  90. for (const auto& pair : LINE_EDIT_VALIDATORS) {
  91. auto line_edit = parent()->findChild<QLineEdit*>(pair.first);
  92. if (line_edit != nullptr) {
  93. auto validator = new QRegularExpressionValidator(pair.second,line_edit);
  94. line_edit->setValidator(validator);
  95. } else {
  96. DEB << "Error: Line Edit not found: "<< pair.first << " - skipping.";
  97. }
  98. }
  99. DEB << "Setting up completer...";
  100. auto completer = new QCompleter(aDB->getCompletionList(ADatabaseTarget::companies), ui->companyLineEdit);
  101. completer->setCompletionMode(QCompleter::InlineCompletion);
  102. completer->setCaseSensitivity(Qt::CaseSensitive);
  103. ui->companyLineEdit->setCompleter(completer);
  104. }
  105. void NewPilotDialog::on_buttonBox_accepted()
  106. {
  107. if (ui->lastnameLineEdit->text().isEmpty() || ui->firstnameLineEdit->text().isEmpty()) {
  108. QMessageBox message_box(this);
  109. message_box.setText("Last Name and First Name are required.");
  110. message_box.show();
  111. } else {
  112. submitForm();
  113. }
  114. }
  115. void NewPilotDialog::formFiller()
  116. {
  117. DEB << "Filling Form...";
  118. auto line_edits = this->findChildren<QLineEdit *>();
  119. for (const auto &le : line_edits) {
  120. auto key = le->objectName().remove("LineEdit");
  121. le->setText(pilotEntry.getData().value(key).toString());
  122. }
  123. }
  124. void NewPilotDialog::submitForm()
  125. {
  126. DEB << "Collecting User Input...";
  127. RowData new_data;
  128. auto line_edits = this->findChildren<QLineEdit *>();
  129. for(auto& le : line_edits) {
  130. auto key = le->objectName().remove("LineEdit");
  131. auto value = le->text();
  132. new_data.insert(key, value);
  133. }
  134. pilotEntry.setData(new_data);
  135. DEB << "Pilot entry position: " << pilotEntry.getPosition().tableName << pilotEntry.getPosition().rowId;
  136. DEB << "Pilot entry data: " << pilotEntry.getData();
  137. if (!aDB->commit(pilotEntry)) {
  138. auto message_box = QMessageBox(this);
  139. message_box.setText("The following error has ocurred:\n\n"
  140. + aDB->lastError.text()
  141. + "\n\nThe entry has not been saved.");
  142. message_box.exec();
  143. return;
  144. } else {
  145. QDialog::accept();
  146. }
  147. }