Commit 52e31f80 authored by Tobias Hunger's avatar Tobias Hunger
Browse files

JsonWizard: Enable validators for lineedits on Fields pages



Make use of a validator when entering C++ class names.

Change-Id: Id7f9c8c2e1fe036397a337595cbe7aa7fd9589d5
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@theqtcompany.com>
parent 50df8aa9
......@@ -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",
......
......@@ -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;
}
......
......@@ -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;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment