From 52e31f80ad5704e31e9469c2e8a5766bd3b8db4c Mon Sep 17 00:00:00 2001
From: Tobias Hunger <tobias.hunger@theqtcompany.com>
Date: Wed, 15 Oct 2014 13:19:05 +0200
Subject: [PATCH] JsonWizard: Enable validators for lineedits on Fields pages
Make use of a validator when entering C++ class names.
Change-Id: Id7f9c8c2e1fe036397a337595cbe7aa7fd9589d5
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@theqtcompany.com>
---
 .../templates/wizards/classes/cpp/wizard.json |  3 ++-
 .../jsonwizard/jsonfieldpage.cpp              | 24 ++++++++++++++++++-
 .../jsonwizard/jsonfieldpage.h                |  3 +++
 3 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/share/qtcreator/templates/wizards/classes/cpp/wizard.json b/share/qtcreator/templates/wizards/classes/cpp/wizard.json
index 57d8252f1ab..dd2915cbfe3 100644
--- a/share/qtcreator/templates/wizards/classes/cpp/wizard.json
+++ b/share/qtcreator/templates/wizards/classes/cpp/wizard.json
@@ -33,7 +33,8 @@
                     "name": "Class",
                     "trDisplayName": "Class name:",
                     "mandatory": true,
-                    "type": "LineEdit"
+                    "type": "LineEdit",
+                    "data": { "validator": "(?:(?:[a-zA-Z_][a-zA-Z_0-9]*::)+[a-zA-Z_][a-zA-Z_0-9]*|)" }
                 },
                 {
                     "name": "BaseCB",
diff --git a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp
index 744b64f1fa9..fd9d6128d87 100644
--- a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp
+++ b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp
@@ -46,6 +46,8 @@
 #include <QFormLayout>
 #include <QLabel>
 #include <QLineEdit>
+#include <QRegularExpression>
+#include <QRegularExpressionValidator>
 #include <QTextEdit>
 #include <QVariant>
 #include <QVariantMap>
@@ -263,9 +265,14 @@ QWidget *JsonFieldPage::SpacerField::widget(const QString &displayName)
 // --------------------------------------------------------------------
 
 JsonFieldPage::LineEditField::LineEditField() :
-    m_isModified(false)
+    m_validatorRegExp(0), m_isModified(false)
 { }
 
+JsonFieldPage::LineEditField::~LineEditField()
+{
+    delete m_validatorRegExp;
+}
+
 bool JsonFieldPage::LineEditField::parseData(const QVariant &data, QString *errorMessage)
 {
     if (data.isNull())
@@ -282,6 +289,18 @@ bool JsonFieldPage::LineEditField::parseData(const QVariant &data, QString *erro
     m_defaultText = JsonWizardFactory::localizedString(tmp.value(QLatin1String("trText")).toString());
     m_disabledText = JsonWizardFactory::localizedString(tmp.value(QLatin1String("trDisabledText")).toString());
     m_placeholderText = JsonWizardFactory::localizedString(tmp.value(QLatin1String("trPlaceholder")).toString());
+    QString pattern = tmp.value(QLatin1String("validator")).toString();
+    if (!pattern.isEmpty()) {
+        m_validatorRegExp = new QRegularExpression(pattern);
+        if (!m_validatorRegExp->isValid()) {
+            *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
+                                                        "Invalid regular expression \"%1\" in \"validator\".")
+                    .arg(pattern);
+            delete m_validatorRegExp;
+            m_validatorRegExp = 0;
+            return false;
+        }
+    }
 
     return true;
 }
@@ -293,6 +312,9 @@ QWidget *JsonFieldPage::LineEditField::widget(const QString &displayName)
     QLineEdit *w = new QLineEdit;
     connect(w, &QLineEdit::textEdited, [this](){ m_isModified = true; });
 
+    if (m_validatorRegExp)
+        w->setValidator(new QRegularExpressionValidator(*m_validatorRegExp, w));
+
     m_widget = w;
     return m_widget;
 }
diff --git a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.h b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.h
index 770c34e925b..591f9b09410 100644
--- a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.h
+++ b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.h
@@ -40,6 +40,7 @@ QT_BEGIN_NAMESPACE
 class QFormLayout;
 class QLabel;
 class QLineEdit;
+class QRegularExpression;
 class QTextEdit;
 QT_END_NAMESPACE
 
@@ -127,6 +128,7 @@ public:
     {
     public:
         LineEditField();
+        ~LineEditField();
 
     private:
         bool parseData(const QVariant &data, QString *errorMessage);
@@ -140,6 +142,7 @@ public:
         QString m_placeholderText;
         QString m_defaultText;
         QString m_disabledText;
+        QRegularExpression *m_validatorRegExp;
 
         bool m_isModified;
         mutable QString m_currentText;
-- 
GitLab