From 2151e763e4921fdd1e2dbfc8278679dcfd87e22b Mon Sep 17 00:00:00 2001 From: Sergey Shambir <sergey.shambir.auto@gmail.com> Date: Tue, 15 Jan 2013 01:09:45 +0400 Subject: [PATCH] Utils: refactored ClassNameValidatingLineEdit Now this class can accept '.' namespaces delimiter, which is used in python. Changed behavior a bit: if namespaces disabled, error message for 'Namespace:Widget' now is 'The class name contains invalid characters' instead of 'The class name must not contain namespace delimiters'. Error message for 'Namespace::Widget' not changed. Change-Id: I45283ca2002957552375419f1cf3137632dcacdb Reviewed-by: hjk <qthjk@ovi.com> --- .../utils/classnamevalidatinglineedit.cpp | 37 ++++++++++++++++--- src/libs/utils/classnamevalidatinglineedit.h | 5 +++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/libs/utils/classnamevalidatinglineedit.cpp b/src/libs/utils/classnamevalidatinglineedit.cpp index 49aeff34ded..5f130f4533a 100644 --- a/src/libs/utils/classnamevalidatinglineedit.cpp +++ b/src/libs/utils/classnamevalidatinglineedit.cpp @@ -46,8 +46,8 @@ namespace Utils { struct ClassNameValidatingLineEditPrivate { ClassNameValidatingLineEditPrivate(); - const QRegExp m_nameRegexp; - const QString m_namespaceDelimiter; + QRegExp m_nameRegexp; + QString m_namespaceDelimiter; bool m_namespacesEnabled; bool m_lowerCaseFileName; bool m_forceFirstCapitalLetter; @@ -67,6 +67,7 @@ ClassNameValidatingLineEdit::ClassNameValidatingLineEdit(QWidget *parent) : Utils::BaseValidatingLineEdit(parent), d(new ClassNameValidatingLineEditPrivate) { + updateRegExp(); } ClassNameValidatingLineEdit::~ClassNameValidatingLineEdit() @@ -84,11 +85,28 @@ void ClassNameValidatingLineEdit::setNamespacesEnabled(bool b) d->m_namespacesEnabled = b; } +/** + * @return Language-specific namespace delimiter, e.g. '::' or '.' + */ +QString ClassNameValidatingLineEdit::namespaceDelimiter() +{ + return d->m_namespaceDelimiter; +} + +/** + * @brief Sets language-specific namespace delimiter, e.g. '::' or '.' + * Do not use identifier characters in delimiter + */ +void ClassNameValidatingLineEdit::setNamespaceDelimiter(const QString &delimiter) +{ + d->m_namespaceDelimiter = delimiter; +} + bool ClassNameValidatingLineEdit::validate(const QString &value, QString *errorMessage) const { - static QRegExp nameRegexp(QLatin1String("[a-zA-Z_][a-zA-Z0-9_]*(::[a-zA-Z_][a-zA-Z0-9_]*)*")); - QTC_ASSERT(nameRegexp.isValid(), return false); - if (!d->m_namespacesEnabled && value.contains(QLatin1Char(':'))) { + QTC_ASSERT(d->m_nameRegexp.isValid(), return false); + + if (!d->m_namespacesEnabled && value.contains(d->m_namespaceDelimiter)) { if (errorMessage) *errorMessage = tr("The class name must not contain namespace delimiters."); return false; @@ -96,7 +114,7 @@ bool ClassNameValidatingLineEdit::validate(const QString &value, QString *errorM if (errorMessage) *errorMessage = tr("Please enter a class name."); return false; - } else if (!nameRegexp.exactMatch(value)) { + } else if (!d->m_nameRegexp.exactMatch(value)) { if (errorMessage) *errorMessage = tr("The class name contains invalid characters."); return false; @@ -131,6 +149,13 @@ QString ClassNameValidatingLineEdit::fixInputString(const QString &string) return fixedString; } +void ClassNameValidatingLineEdit::updateRegExp() const +{ + QString identifierPatter(QLatin1String("[a-zA-Z_][a-zA-Z0-9_]*")); + QString pattern(QLatin1String("%1(%2%1)*")); + d->m_nameRegexp.setPattern(pattern.arg(identifierPatter).arg(d->m_namespaceDelimiter)); +} + QString ClassNameValidatingLineEdit::createClassName(const QString &name) { // Remove spaces and convert the adjacent characters to uppercase diff --git a/src/libs/utils/classnamevalidatinglineedit.h b/src/libs/utils/classnamevalidatinglineedit.h index 05af35170fe..abf1a52523d 100644 --- a/src/libs/utils/classnamevalidatinglineedit.h +++ b/src/libs/utils/classnamevalidatinglineedit.h @@ -51,6 +51,9 @@ public: bool namespacesEnabled() const; void setNamespacesEnabled(bool b); + QString namespaceDelimiter(); + void setNamespaceDelimiter(const QString &delimiter); + bool lowerCaseFileName() const; void setLowerCaseFileName(bool v); @@ -71,6 +74,8 @@ protected: virtual QString fixInputString(const QString &string); private: + void updateRegExp() const; + ClassNameValidatingLineEditPrivate *d; }; -- GitLab