From 18b8b59af6d6be7e0c316ed093b637bfc2a4ccc0 Mon Sep 17 00:00:00 2001 From: James Turner Date: Thu, 18 May 2017 17:27:41 +0100 Subject: [PATCH] Validate MP callsign with a reg-exp --- src/GUI/CMakeLists.txt | 12 +++++++++++- src/GUI/MPSettings.qml | 6 +++++- src/GUI/SettingsWidgets.cxx | 26 ++++++++++++++++++++++++++ src/GUI/SettingsWidgets.hxx | 10 ++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/GUI/CMakeLists.txt b/src/GUI/CMakeLists.txt index 8e1a5878c..244031b64 100644 --- a/src/GUI/CMakeLists.txt +++ b/src/GUI/CMakeLists.txt @@ -81,6 +81,15 @@ if (HAVE_QT) ) qt5_add_resources(qrc_sources resources.qrc) + set(qml_sources + DownloadSettings.qml + GeneralSettings.qml + MPSettings.qml + RenderSettings.qml + TimeSettings.qml + ViewSettings.qml + Weather.qml + ) add_library(fglauncher QtLauncher.cxx QtLauncher.hxx @@ -141,7 +150,8 @@ if (HAVE_QT) MPServersModel.cpp MPServersModel.h ${uic_sources} - ${qrc_sources}) + ${qrc_sources} + ${qml_sources}) set_property(TARGET fglauncher PROPERTY AUTOMOC ON) target_link_libraries(fglauncher Qt5::Core Qt5::Widgets Qt5::Network Qt5::Qml SimGearCore) diff --git a/src/GUI/MPSettings.qml b/src/GUI/MPSettings.qml index afd2cb909..857e26e58 100644 --- a/src/GUI/MPSettings.qml +++ b/src/GUI/MPSettings.qml @@ -21,9 +21,13 @@ Section { label: "Callsign" description: "Enter a callsign you will use online. This is visible to all users and is " + "how ATC services and other pilots will refer to you. " + - "(Maximum of ten characters permitted)" + "(Maximum of seven characters permitted)" placeholder: "D-FGFS" keywords: ["callsign", "handle", "name"] + + // between one and seven alphanumerics, underscores and or hypens + // spaces not permitted + validation: "[\\w-]{1,7}" } Combo { diff --git a/src/GUI/SettingsWidgets.cxx b/src/GUI/SettingsWidgets.cxx index c4126cbf3..c3a9e93c8 100644 --- a/src/GUI/SettingsWidgets.cxx +++ b/src/GUI/SettingsWidgets.cxx @@ -15,6 +15,7 @@ #include #include #include +#include #include "LaunchConfig.hxx" #include "SettingsSection.hxx" @@ -504,6 +505,11 @@ QString SettingsText::placeholder() const #endif } +QString SettingsText::validation() const +{ + return m_validation; +} + void SettingsText::setPlaceholder(QString hold) { if (placeholder() == hold) @@ -516,6 +522,26 @@ void SettingsText::setPlaceholder(QString hold) emit placeholderChanged(hold); } +void SettingsText::setValidation(QString validation) +{ + if (m_validation == validation) + return; + + m_validation = validation; + + QRegularExpression re(m_validation); + if (!re.isValid()) { + qWarning() << "invalid validation expression:" << re.errorString(); + m_validation.clear(); + validation.clear(); + m_edit->setValidator(nullptr); + } else { + m_edit->setValidator(new QRegularExpressionValidator(re, this)); + } + + emit validationChanged(validation); +} + SettingsPath::SettingsPath(QWidget *pr) : SettingsControl(pr) { diff --git a/src/GUI/SettingsWidgets.hxx b/src/GUI/SettingsWidgets.hxx index 3731ae6ba..1519b406a 100644 --- a/src/GUI/SettingsWidgets.hxx +++ b/src/GUI/SettingsWidgets.hxx @@ -228,6 +228,9 @@ class SettingsText : public SettingsControl Q_PROPERTY(QString value READ value WRITE setValue NOTIFY valueChanged) Q_PROPERTY(QString placeholder READ placeholder WRITE setPlaceholder NOTIFY placeholderChanged) + + Q_PROPERTY(QString validation READ validation WRITE setValidation NOTIFY validationChanged) + public: SettingsText(QWidget *pr = nullptr); @@ -243,19 +246,26 @@ public: QString placeholder() const; + QString validation() const; + public slots: void setValue(QString value); void setPlaceholder(QString placeholder); + void setValidation(QString validation); + signals: void valueChanged(QString value); void placeholderChanged(QString placeholder); + void validationChanged(QString validation); + private: QLineEdit* m_edit; QLabel* m_label; + QString m_validation; }; class SettingsPath : public SettingsControl