newpilotdialog.cpp 5.5 KB

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