Browse Source

Added formatting to user input verification

Felix Turowsky 1 year ago
parent
commit
7608b53086

+ 2 - 3
src/classes/time.h

@@ -33,7 +33,7 @@ public:
     bool isValid() const;
 
     /**
-     * @brief toString returns the time as hh:mm
+     * @brief toString returns the time in the specified format
      */
     const QString toString() const;
 
@@ -86,9 +86,8 @@ public:
 
 private:
     static constexpr int MINUTES_PER_DAY = 24 * 60;
-    static constexpr QLatin1Char DECIMAL_SEPERATOR = QLatin1Char('.');
 
-    DateTimeFormat m_format;
+    const DateTimeFormat m_format;
     int32_t m_minutes;
 
 };

+ 4 - 36
src/gui/dialogues/newflightdialog.cpp

@@ -510,43 +510,11 @@ void NewFlightDialog::toUpper(const QString &text)
 
 void NewFlightDialog::onTimeLineEdit_editingFinished()
 {
-    auto line_edit = this->findChild<QLineEdit*>(sender()->objectName());
-
-    if(OPL::Time::fromString(line_edit->text(), m_format).isValidTimeOfDay()) {
-        onGoodInputReceived(line_edit);
-        return;
-    }
-
-    // try to fix up the input
-    QString text = line_edit->text();
-    // don't mess with decimal time formats
-    if(text.contains('.')) {
-        DEB << "Bad input received: " << text;
-        onBadInputReceived(line_edit);
-    }
-
-    DEB << "Trying to fix input: " << text;
-    // try inserting a ':' for hhmm inputs
-    QString fixed = text;
-    if (text.contains(':')) { // contains seperator
-        if(text.length() == 4)
-            fixed.prepend(QLatin1Char('0'));
-    } else { // does not contain seperator
-        if(text.length() == 4) {
-            fixed.insert(2, ':');
-        }
-        if(text.length() == 3) {
-            fixed.prepend(QLatin1Char('0'));
-            fixed.insert(2, ':');
-        }
-    }
+    const auto line_edit = this->findChild<QLineEdit*>(sender()->objectName());
 
-    if(OPL::Time::fromString(fixed, m_format).isValidTimeOfDay()) {
-        line_edit->setText(fixed);
-        onGoodInputReceived(line_edit);
-    } else {
-        DEB << "Bad input received: " << text;
-        onBadInputReceived(line_edit);
+    if(!verifyUserInput(line_edit, TimeInput(line_edit->text(), m_format))) {
+        if(!addNewDatabaseElement(line_edit, OPL::DbTable::Pilots))
+            onBadInputReceived(line_edit);
     }
 }
 

+ 2 - 2
src/gui/dialogues/newsimdialog.cpp

@@ -86,7 +86,7 @@ void NewSimDialog::on_dateLineEdit_editingFinished()
 
 void NewSimDialog::on_totalTimeLineEdit_editingFinished()
 {
-    const auto input = TimeInput(ui->totalTimeLineEdit->text());
+    const auto input = TimeInput(ui->totalTimeLineEdit->text(), m_format);
     if(input.isValid())
         return;
     else {
@@ -130,7 +130,7 @@ bool NewSimDialog::verifyInput(QString& error_msg)
         return false;
     }
     // Time
-    if(!TimeInput(ui->totalTimeLineEdit->text()).isValid())
+    if(!TimeInput(ui->totalTimeLineEdit->text(), m_format).isValid())
         return false;
 
     const OPL::Time time = OPL::Time::fromString(ui->totalTimeLineEdit->text(), m_format);

+ 37 - 21
src/gui/verification/timeinput.cpp

@@ -1,10 +1,11 @@
 #include "timeinput.h"
+#include "src/classes/time.h"
 #include "src/opl.h"
 #include <QTime>
 
 bool TimeInput::isValid() const
 {
-     return QTime::fromString(input, OPL::Format::TIME_FORMAT).isValid();
+    return OPL::Time::fromString(input, m_format).isValid();
 }
 
 /*!
@@ -12,28 +13,43 @@ bool TimeInput::isValid() const
  * \details If the user input is not a valid time, this function tries to fix it. Accepted Inputs are
  * hh:mm, h:mm, hhmm or hmm. Returns "hh:mm" or an empty string if the resulting time is invalid.
  */
-QT_DEPRECATED // time input verification depends on display format...fix this TODO
 QString TimeInput::fixup() const
 {
-     // don't mess with decimal time formats
-     if(input.contains('.'))
-         return input;
+    switch(m_format.timeFormat()) {
+    case OPL::DateTimeFormat::TimeFormat::Default:
+        return fixDefaultFormat();
+    case OPL::DateTimeFormat::TimeFormat::Decimal:
+        return fixDecimalFormat();
+    case OPL::DateTimeFormat::TimeFormat::Custom:
+        return input; // custom formats cannot be fixed
+        break;
+    }
+}
+
+const QString TimeInput::fixDefaultFormat() const
+{
+    // try inserting a ':' for hhmm inputs
+    QString fixed = input;
+    if (input.contains(':')) { // contains seperator
+        if(input.length() == 4)
+            fixed.prepend(QLatin1Char('0'));
+    } else { // does not contain seperator
+        if(input.length() == 4) {
+            fixed.insert(2, ':');
+        }
+        if(input.length() == 3) {
+            fixed.prepend(QLatin1Char('0'));
+            fixed.insert(2, ':');
+        }
+    }
 
-     // try inserting a ':' for hhmm inputs
-     QString fixed = input;
-     if (input.contains(':')) { // contains seperator
-         if(input.length() == 4)
-             fixed.prepend(QLatin1Char('0'));
-     } else { // does not contain seperator
-         if(input.length() == 4) {
-             fixed.insert(2, ':');
-         }
-         if(input.length() == 3) {
-             fixed.prepend(QLatin1Char('0'));
-             fixed.insert(2, ':');
-         }
-     }
+    return OPL::Time::fromString(fixed, m_format).toString();
+}
 
-     QTime time = QTime::fromString(fixed, OPL::Format::TIME_FORMAT);
-     return time.toString(OPL::Format::TIME_FORMAT);
+const QString TimeInput::fixDecimalFormat() const
+{
+    // try to replace an erroneus decimal seperator
+    QString fixed = input;
+    return OPL::Time::fromString(fixed.replace(QLatin1Char(','), OPL::DECIMAL_SEPERATOR), m_format).toString();
 }
+

+ 7 - 1
src/gui/verification/timeinput.h

@@ -1,13 +1,15 @@
 #ifndef TIMEINPUT_H
 #define TIMEINPUT_H
 
+#include "src/opl.h"
 #include "userinput.h"
 
 class TimeInput : public UserInput
 {
 public:
     TimeInput() = delete;
-    TimeInput(const QString &input) : UserInput(input) {}
+    TimeInput(const QString &input, const OPL::DateTimeFormat &format)
+        : UserInput(input), m_format(format) {}
 
     /*!
      * \brief Checks if a user entered String is a valid time input
@@ -25,6 +27,10 @@ public:
      */
     QString fixup() const override;
 private:
+    const OPL::DateTimeFormat &m_format;
+
+    const QString fixDefaultFormat() const;
+    const QString fixDecimalFormat() const;
 };
 
 #endif // TIMEINPUT_H

+ 12 - 7
src/opl.h

@@ -70,16 +70,22 @@ namespace OPL {
 /**
  * @brief Defines the row ID for non-user entries in the database;
  */
-const static int STUB_ROW_ID = -1;
+constexpr static int STUB_ROW_ID = -1;
 
 /**
  * @brief Defines a four-letter code for a non-extistent (dummy) airport: "XXXX"
  */
-const static char* STUB_AIRPORT_CODE = "XXXX";
+constexpr static auto STUB_AIRPORT_CODE = QLatin1String("XXXX");
 /**
  * @brief Defines a registration for a non-existent (dummy) aircraft: "XX-XXX"
  */
-const static char* STUB_AIRCRAFT_REG = "XX-XXX";
+constexpr static auto STUB_AIRCRAFT_REG = QLatin1String("XX-XXX");
+
+/*!
+ * \brief The decimal seperator used internally
+ */
+constexpr static char DECIMAL_SEPERATOR = '.';
+
 
 /*!
  * \brief The ANotificationHandler class handles displaying of user-directed messages. It displays
@@ -356,17 +362,16 @@ namespace CssStyles {
 const inline auto  RED_BORDER = QStringLiteral("border: 1px solid red");
 } // namespace Styles
 
-namespace Format {
+//namespace Format {
 
-const inline auto TIME_FORMAT = QStringLiteral("hh:mm");
+//const inline auto TIME_FORMAT = QStringLiteral("hh:mm");
 
-} // namespace Format
+//} // namespace Format
 
 namespace RegEx {
 
 const inline auto RX_PHONE_NUMBER  = QRegularExpression(QStringLiteral("^[+]{0,1}[0-9\\-\\s]+"));
 const inline auto RX_EMAIL_ADDRESS = QRegularExpression(QStringLiteral("\\A[a-z0-9!#$%&'*+/=?^_‘{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_‘{|}~-]+)*@"));
-//const inline auto RX_TIME_ENTRY    = QRegularExpression(QStringLiteral("([01]?[0-9]|2[0-3]):?[0-5][0-9]?"));
 const inline auto RX_TIME_ENTRY    = QRegularExpression(QStringLiteral("^(?:(?:([01]?\\d|2[0-3])(?::?)([0-5]\\d))|(?:([01]?\\d|2[0-3])([0-5]\\d))|(?:([1-9]|[1-9]\\d)\\:([0-5]\\d)?)|(?:([01]?\\d|2[0-3])\\.([0-5]?\\d)))$"));
 const inline auto RX_AIRPORT_CODE  = QRegularExpression(QStringLiteral("[a-zA-Z0-9]{1,4}"));