forked from qt-creator/qt-creator
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>
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user