ソースを参照

added Validators

fiffty-50 4 年 前
コミット
d3170a9412

+ 2 - 1
main.cpp

@@ -60,6 +60,7 @@ int main(int argc, char *argv[])
     }
 
     MainWindow w;
-    w.showMaximized();
+    //w.showMaximized();
+    w.show();
     return openPilotLog.exec();
 }

+ 1 - 0
src/classes/strictrxvalidator.h

@@ -26,6 +26,7 @@
  */
 class StrictRxValidator : public QRegularExpressionValidator
 {
+    using QRegularExpressionValidator::QRegularExpressionValidator;
 public:
     QValidator::State validate(QString &txt, int &pos) const;
 };

+ 33 - 1
src/gui/dialogues/newpilot.cpp

@@ -21,12 +21,33 @@
 #define DEB(expr) \
     qDebug() << __PRETTY_FUNCTION__ << "\t" << expr
 
+const auto FIRSTNAME_VALID = QPair<QString, QRegularExpression> {
+    "picfirstnameLineEdit", QRegularExpression("[a-zA-Z]+")};
+const auto LASTNAME_VALID = QPair<QString, QRegularExpression> {
+    "piclastnameLineEdit", QRegularExpression("\\w+")};
+const auto PHONE_VALID = QPair<QString, QRegularExpression> {
+    "phoneLineEdit", QRegularExpression("^[+]{0,1}[0-9\\-\\s]+")};
+const auto EMAIL_VALID = QPair<QString, QRegularExpression> {
+    "emailLineEdit", QRegularExpression("\\A[a-z0-9!#$%&'*+/=?^_‘{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_‘{|}~-]+)*@"
+                                        "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\z")};
+const auto ALIAS_VALID = QPair<QString, QRegularExpression> {
+    "aliasLineEdit", QRegularExpression("\\w+")};
+const auto EMPLOYEENR_VALID = QPair<QString, QRegularExpression> {
+    "employeeidLineEdit", QRegularExpression("\\w+")};
+
+
+
+const auto LINE_EDIT_VALIDATORS = QVector({FIRSTNAME_VALID, LASTNAME_VALID,
+                                           PHONE_VALID,     EMAIL_VALID,
+                                           ALIAS_VALID,     EMPLOYEENR_VALID});
+
 NewPilot::NewPilot(Db::editRole edRole, QWidget *parent) :
     QDialog(parent),
     ui(new Ui::NewPilot)
 {
     role = edRole;
     ui->setupUi(this);
+    setupValidators();
 }
 
 NewPilot::NewPilot(Pilot existingEntry, Db::editRole edRole, QWidget *parent) :
@@ -36,6 +57,7 @@ NewPilot::NewPilot(Pilot existingEntry, Db::editRole edRole, QWidget *parent) :
     oldEntry = existingEntry;
     role = edRole;
     ui->setupUi(this);
+    setupValidators();
     formFiller();
     ui->piclastnameLineEdit->setFocus();
 }
@@ -47,15 +69,25 @@ NewPilot::~NewPilot()
 
 void NewPilot::on_buttonBox_accepted()
 {
-    DEB("aseontuh");
+    DEB("accepted.");
     if (ui->piclastnameLineEdit->text().isEmpty()) {
         auto mb = new QMessageBox(this);
         mb->setText("Last Name is required.");
+        mb->show();
     } else {
         submitForm();
     }
 }
 
+void NewPilot::setupValidators()
+{
+    for(const auto& pair : LINE_EDIT_VALIDATORS){
+        auto line_edit = parent()->findChild<QLineEdit*>(pair.first);
+        auto validator = new QRegularExpressionValidator(pair.second,line_edit);
+        line_edit->setValidator(validator);
+    }
+}
+
 void NewPilot::formFiller()
 {
     DEB("Filling Form...");

+ 4 - 0
src/gui/dialogues/newpilot.h

@@ -20,6 +20,8 @@
 
 #include <QDialog>
 #include <QMessageBox>
+#include <QRegularExpression>
+#include <QRegularExpressionValidator>
 #include "src/classes/pilot.h"
 
 namespace Ui {
@@ -45,6 +47,8 @@ private:
 
     Pilot oldEntry;
 
+    void setupValidators();
+
     void formFiller();
 
     void submitForm();

+ 36 - 5
src/gui/dialogues/newpilot.ui

@@ -15,7 +15,11 @@
   </property>
   <layout class="QGridLayout" name="gridLayout">
    <item row="4" column="1">
-    <widget class="QLineEdit" name="phoneLineEdit"/>
+    <widget class="QLineEdit" name="phoneLineEdit">
+     <property name="maxLength">
+      <number>40</number>
+     </property>
+    </widget>
    </item>
    <item row="0" column="0">
     <widget class="QLabel" name="picfirstnameLabel">
@@ -32,7 +36,11 @@
     </widget>
    </item>
    <item row="5" column="1">
-    <widget class="QLineEdit" name="emailLineEdit"/>
+    <widget class="QLineEdit" name="emailLineEdit">
+     <property name="maxLength">
+      <number>40</number>
+     </property>
+    </widget>
    </item>
    <item row="2" column="0">
     <widget class="QLabel" name="aliasLabel">
@@ -56,10 +64,18 @@
     </widget>
    </item>
    <item row="1" column="1">
-    <widget class="QLineEdit" name="piclastnameLineEdit"/>
+    <widget class="QLineEdit" name="piclastnameLineEdit">
+     <property name="maxLength">
+      <number>40</number>
+     </property>
+    </widget>
    </item>
    <item row="0" column="1">
-    <widget class="QLineEdit" name="picfirstnameLineEdit"/>
+    <widget class="QLineEdit" name="picfirstnameLineEdit">
+     <property name="maxLength">
+      <number>40</number>
+     </property>
+    </widget>
    </item>
    <item row="1" column="0">
     <widget class="QLabel" name="piclastnameLabel">
@@ -69,13 +85,20 @@
     </widget>
    </item>
    <item row="2" column="1">
-    <widget class="QLineEdit" name="aliasLineEdit"/>
+    <widget class="QLineEdit" name="aliasLineEdit">
+     <property name="maxLength">
+      <number>40</number>
+     </property>
+    </widget>
    </item>
    <item row="3" column="1">
     <widget class="QLineEdit" name="employeeidLineEdit">
      <property name="text">
       <string/>
      </property>
+     <property name="maxLength">
+      <number>40</number>
+     </property>
     </widget>
    </item>
    <item row="6" column="0" colspan="2">
@@ -90,6 +113,14 @@
    </item>
   </layout>
  </widget>
+ <tabstops>
+  <tabstop>picfirstnameLineEdit</tabstop>
+  <tabstop>piclastnameLineEdit</tabstop>
+  <tabstop>aliasLineEdit</tabstop>
+  <tabstop>employeeidLineEdit</tabstop>
+  <tabstop>phoneLineEdit</tabstop>
+  <tabstop>emailLineEdit</tabstop>
+ </tabstops>
  <resources/>
  <connections>
   <connection>

+ 58 - 36
src/gui/dialogues/newtail.cpp

@@ -22,6 +22,14 @@
 #define DEB(expr) \
     qDebug() << __PRETTY_FUNCTION__<< "\t" << expr
 
+const auto MAKE_VALID = QPair<QString, QRegularExpression> {
+    "makeLineEdit", QRegularExpression("[a-zA-Z]+")};
+const auto MODEL_VALID = QPair<QString, QRegularExpression> {
+    "modelLineEdit", QRegularExpression("\\w+")};
+const auto VARIANT_VALID = QPair<QString, QRegularExpression> {
+    "variantLineEdit", QRegularExpression("\\w+")};
+const auto LINE_EDIT_VALIDATORS = QVector({MAKE_VALID, MODEL_VALID, VARIANT_VALID});
+
 
 //Dialog to be used to create a new tail
 NewTail::NewTail(QString newreg, Db::editRole edRole, QWidget *parent) :
@@ -31,6 +39,7 @@ NewTail::NewTail(QString newreg, Db::editRole edRole, QWidget *parent) :
     ui->setupUi(this);
     role = edRole;
     setupCompleter();
+    setupValidators();
 
     ui->editLabel->hide();
     ui->registrationLineEdit->setText(newreg);
@@ -44,19 +53,58 @@ NewTail::NewTail(Aircraft dbentry, Db::editRole edRole, QWidget *parent) :
     ui(new Ui::NewTail)
 {
     oldEntry = dbentry;
-    ui->setupUi(this);
     role = edRole;
+    ui->setupUi(this);
     ui->searchLabel->hide();
     ui->searchLineEdit->hide();
     ui->line->hide();
-    formFiller(dbentry);
+
+    setupValidators();
+    formFiller(oldEntry);
 }
 
 NewTail::~NewTail()
 {
     delete ui;
 }
+/// Functions
 
+/*!
+ * \brief NewTail::setupCompleter creates a QMap<aircaft_id,searchstring> for auto completion,
+ * obtains a QStringList for QCompleter and sets up search line edit accordingly
+ */
+void NewTail::setupCompleter()
+{
+    auto query = QLatin1String("SELECT make||' '||model||'-'||variant, aircraft_id FROM aircraft");
+    auto vector = Db::customQuery(query, 2);
+    QMap<QString, int> map;
+    for (int i = 0; i < vector.length() - 2 ; i += 2) {
+        if (vector[i] != QLatin1String("")) {
+            map.insert(vector[i], vector[i + 1].toInt());
+        }
+    }
+    //creating QStringlist for QCompleter. This list is identical to a list of map<key>,
+    //but creating it like this is faster.
+    auto cl = new CompletionList(CompleterTarget::aircraft);
+
+    aircraftlist = cl->list;
+    idMap = map;
+
+    QCompleter *completer = new QCompleter(aircraftlist, ui->searchLineEdit);
+    completer->setCaseSensitivity(Qt::CaseInsensitive);
+    completer->setCompletionMode(QCompleter::PopupCompletion);
+    completer->setFilterMode(Qt::MatchContains);
+    ui->searchLineEdit->setCompleter(completer);
+}
+
+void NewTail::setupValidators()
+{
+    for(const auto& pair : LINE_EDIT_VALIDATORS){
+        auto line_edit = parent()->findChild<QLineEdit*>(pair.first);
+        auto validator = new QRegularExpressionValidator(pair.second,line_edit);
+        line_edit->setValidator(validator);
+    }
+}
 /*!
  * \brief NewTail::formFiller populates the Dialog with the
  * information contained in an aircraft object.
@@ -91,35 +139,6 @@ void NewTail::formFiller(Aircraft entry)
     ui->ppTypeComboBox->setCurrentIndex(ppType.indexOf("1") + 1);
     ui->weightComboBox->setCurrentIndex(weight.indexOf("1") + 1);
 }
-
-/// Functions
-
-/*!
- * \brief NewTail::setupCompleter creates a QMap<aircaft_id,searchstring> for auto completion,
- * obtains a QStringList for QCompleter and sets up search line edit accordingly
- */
-void NewTail::setupCompleter()
-{
-    auto query = QLatin1String("SELECT make||' '||model||'-'||variant, aircraft_id FROM aircraft");
-    auto vector = Db::customQuery(query, 2);
-    QMap<QString, int> map;
-    for (int i = 0; i < vector.length() - 2 ; i += 2) {
-        if (vector[i] != QLatin1String("")) {
-            map.insert(vector[i], vector[i + 1].toInt());
-        }
-    }
-    //creating QStringlist for QCompleter
-    auto cl = new CompletionList(CompleterTarget::aircraft);
-
-    aircraftlist = cl->list;
-    idMap = map;
-
-    QCompleter *completer = new QCompleter(aircraftlist, ui->searchLineEdit);
-    completer->setCaseSensitivity(Qt::CaseInsensitive);
-    completer->setCompletionMode(QCompleter::PopupCompletion);
-    completer->setFilterMode(Qt::MatchContains);
-    ui->searchLineEdit->setCompleter(completer);
-}
 /*!
  * \brief NewTail::verify A simple check for empty recommended fields in the form
  * \return true if all reconmmended fields are populated
@@ -161,7 +180,11 @@ bool NewTail::verify()
         return false;
     }
 }
-
+/*!
+ * \brief NewTail::submitForm collects input from Line Edits and creates
+ * or updates a database entry and commits or updates the database
+ * \param edRole editExisting or createNew
+ */
 void NewTail::submitForm(Db::editRole edRole)
 {
     DEB("Creating Database Object...");
@@ -176,7 +199,6 @@ void NewTail::submitForm(Db::editRole edRole)
             newData.insert(name, le->text());
         }
     }
-
     //prepare comboboxes
     QVector<QString> operation = {"singlepilot", "multipilot"};
     QVector<QString> ppNumber  = {"singleengine", "multiengine"};
@@ -202,8 +224,8 @@ void NewTail::submitForm(Db::editRole edRole)
     //create db object
     switch (edRole) {
     case Db::createNew: {
-        auto newEntry = new Aircraft("tails", newData);;
-        newEntry->commit();
+        auto newEntry = Aircraft("tails", newData);;
+        newEntry.commit();
         break;
     }
     case Db::editExisting:
@@ -275,7 +297,7 @@ void NewTail::on_buttonBox_accepted()
             if (!setting.value("userdata/acAllowIncomplete").toInt()) {
                 auto nope = new QMessageBox(this);
                 nope->setText("Some or all fields are empty.\nPlease go back and "
-                              "complete.\n\nYou can allow logging incomplete entries on the settings page.");
+                              "complete the form.\n\nYou can allow logging incomplete entries on the settings page.");
                 nope->show();
             } else {
                 QMessageBox::StandardButton reply;

+ 7 - 12
src/gui/dialogues/newtail.h

@@ -22,8 +22,10 @@
 #include <QCompleter>
 #include <QMessageBox>
 #include <QSettings>
+#include <QRegularExpression>
 #include "src/classes/completionlist.h"
 #include "src/classes/aircraft.h"
+#include "src/classes/strictrxvalidator.h"
 
 namespace Ui {
 class NewTail;
@@ -40,16 +42,12 @@ class NewTail : public QDialog
     Q_OBJECT
 
 public:
-
-    //explicit NewTail(aircraft acft, QWidget *parent = nullptr);
     //to create new tail from scratch
     explicit NewTail(QString reg, Db::editRole edRole, QWidget *parent = nullptr);
     //to edit existing tail
     explicit NewTail(Aircraft dbentry, Db::editRole edRole, QWidget *parent = nullptr);
 
     ~NewTail();
-
-
 private slots:
 
     void on_searchLineEdit_textChanged(const QString &arg1);
@@ -72,22 +70,19 @@ private:
 
     Aircraft oldEntry;
 
+    QStringList aircraftlist;
+
+    QMap<QString, int> idMap;
+
     void submitForm(Db::editRole edRole);
 
     void setupCompleter();
 
-    //void formFiller(aircraft);
+    void setupValidators();
 
     void formFiller(Aircraft);
 
     bool verify();
-
-    //aircraft createAircraftFromSelection();
-
-    QStringList aircraftlist;
-
-    QMap<QString, int> idMap;
-
 };
 
 #endif // NEWTAIL_H