newpilotdialog.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "debug.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(ADataBase::companies), ui->companyLineEdit);
  100. completer->setCompletionMode(QCompleter::InlineCompletion);
  101. completer->setCaseSensitivity(Qt::CaseSensitive);
  102. ui->companyLineEdit->setCompleter(completer);
  103. ///[F] moved connecting the slots here because
  104. /// - no need to declare the slots public as would be the case if connected in mainwindow
  105. /// - only one place where slots are connected vs. several places (mainwindow, pilotswidget),
  106. /// makes it easier to maintain.
  107. /// - these signals and slots are specific to this dialog, for communication with
  108. /// other widgets we have the QDialog::accepted() and QDialog::rejected signals.
  109. QObject::connect(aDB(), &ADataBase::sqlSuccessful,
  110. this, &NewPilotDialog::onCommitSuccessful);
  111. QObject::connect(aDB(), &ADataBase::sqlError,
  112. this, &NewPilotDialog::onCommitUnsuccessful);
  113. }
  114. void NewPilotDialog::on_buttonBox_accepted()
  115. {
  116. if (ui->piclastnameLineEdit->text().isEmpty() || ui->picfirstnameLineEdit->text().isEmpty()) {
  117. auto mb = QMessageBox(this);
  118. mb.setText("Last Name and First Name are required.");
  119. mb.show();
  120. } else {
  121. submitForm();
  122. }
  123. }
  124. void NewPilotDialog::onCommitSuccessful()
  125. {
  126. accept();
  127. }
  128. void NewPilotDialog::onCommitUnsuccessful(const QSqlError &sqlError, const QString &)
  129. {
  130. auto mb = QMessageBox(this);
  131. mb.setIcon(QMessageBox::Critical);
  132. mb.setText("The following error has ocurred.\n\n"
  133. + sqlError.text()
  134. + "\n\nYour entry has not been saved.");
  135. mb.exec();
  136. }
  137. void NewPilotDialog::formFiller()
  138. {
  139. DEB("Filling Form...");
  140. auto line_edits = this->findChildren<QLineEdit *>();
  141. for (const auto &le : line_edits) {
  142. QString key = le->objectName().remove("LineEdit");
  143. QString value = pilotEntry.getData().value(key);
  144. le->setText(value);
  145. }
  146. }
  147. void NewPilotDialog::submitForm()
  148. {
  149. DEB("Collecting User Input...");
  150. TableData new_data;
  151. auto line_edits = this->findChildren<QLineEdit *>();
  152. for(auto& le : line_edits) {
  153. auto key = le->objectName().remove("LineEdit");
  154. auto value = le->text();
  155. new_data.insert(key, value);
  156. }
  157. pilotEntry.setData(new_data);
  158. DEB("Pilot entry position: " << pilotEntry.getPosition());
  159. DEB("Pilot entry data: " << pilotEntry.getData());
  160. aDB()->commit(pilotEntry);
  161. }