diff --git a/src/libs/utils/projectintropage.cpp b/src/libs/utils/projectintropage.cpp index 55553f62255..f218a61c99f 100644 --- a/src/libs/utils/projectintropage.cpp +++ b/src/libs/utils/projectintropage.cpp @@ -60,11 +60,13 @@ namespace Utils { -struct ProjectIntroPagePrivate +class ProjectIntroPagePrivate { +public: ProjectIntroPagePrivate(); Ui::ProjectIntroPage m_ui; bool m_complete; + QRegularExpressionValidator m_projectNameValidator; // Status label style sheets const QString m_errorStyleSheet; const QString m_warningStyleSheet; @@ -91,7 +93,7 @@ ProjectIntroPage::ProjectIntroPage(QWidget *parent) : d->m_ui.nameLineEdit->setInitialText(tr("")); d->m_ui.nameLineEdit->setFocus(); d->m_ui.nameLineEdit->setValidationFunction([this](FancyLineEdit *edit, QString *errorString) { - return ProjectIntroPage::validateProjectName(edit->text(), errorString); + return validateProjectName(edit->text(), errorString); }); d->m_ui.projectLabel->setVisible(d->m_forceSubProject); d->m_ui.projectComboBox->setVisible(d->m_forceSubProject); @@ -140,6 +142,12 @@ void ProjectIntroPage::setPath(const QString &path) d->m_ui.pathChooser->setPath(path); } +void ProjectIntroPage::setProjectNameRegularExpression(const QRegularExpression ®Ex) +{ + Q_ASSERT_X(regEx.isValid(), Q_FUNC_INFO, qPrintable(regEx.errorString())); + d->m_projectNameValidator.setRegularExpression(regEx); +} + void ProjectIntroPage::setProjectName(const QString &name) { d->m_ui.nameLineEdit->setText(name); @@ -250,23 +258,45 @@ int ProjectIntroPage::projectIndex() const return d->m_ui.projectComboBox->currentIndex(); } -bool ProjectIntroPage::validateProjectName(const QString &name, QString *errorMessage /* = 0*/) +bool ProjectIntroPage::validateProjectName(const QString &name, QString *errorMessage) { - // Validation is file name + checking for dots - if (!FileNameValidatingLineEdit::validateFileName(name, false, errorMessage)) - return false; + int pos = -1; + // if we have a pattern it was set + if (!d->m_projectNameValidator.regularExpression().pattern().isEmpty()) { + if (name.isEmpty()) { + if (errorMessage) + *errorMessage = tr("Name is empty."); + return false; + } + // pos is set by reference + QString tmp = name; + QValidator::State validatorState = d->m_projectNameValidator.validate(tmp, pos); - int pos = FileUtils::indexOfQmakeUnfriendly(name); + // if pos is set by validate it is cought at the bottom where it shows + // a more detailed error message + if (validatorState != QValidator::Acceptable && (pos == -1 || pos >= name.count())) { + if (errorMessage) { + *errorMessage = tr("Name does not match \"%1\".").arg( + d->m_projectNameValidator.regularExpression().pattern()); + } + return false; + } + } else { // no validator means usually a qmake project + // Validation is file name + checking for dots + if (!FileNameValidatingLineEdit::validateFileName(name, false, errorMessage)) + return false; + if (name.contains(QLatin1Char('.'))) { + if (errorMessage) + *errorMessage = tr("Invalid character \".\"."); + return false; + } + pos = FileUtils::indexOfQmakeUnfriendly(name); + } if (pos >= 0) { if (errorMessage) *errorMessage = tr("Invalid character \"%1\" found.").arg(name.at(pos)); return false; } - if (name.contains(QLatin1Char('.'))) { - if (errorMessage) - *errorMessage = tr("Invalid character \".\"."); - return false; - } return true; } diff --git a/src/libs/utils/projectintropage.h b/src/libs/utils/projectintropage.h index 87cc3944877..c672f83c109 100644 --- a/src/libs/utils/projectintropage.h +++ b/src/libs/utils/projectintropage.h @@ -36,7 +36,7 @@ namespace Utils { -struct ProjectIntroPagePrivate; +class ProjectIntroPagePrivate; class QTCREATOR_UTILS_EXPORT ProjectIntroPage : public WizardPage { @@ -67,7 +67,7 @@ public: void setProjectDirectories(const QStringList &directoryList); int projectIndex() const; - static bool validateProjectName(const QString &name, QString *errorMessage /* = 0*/); + bool validateProjectName(const QString &name, QString *errorMessage); signals: void activated(); @@ -77,6 +77,7 @@ public slots: void setProjectName(const QString &name); void setDescription(const QString &description); void setUseAsDefaultPath(bool u); + void setProjectNameRegularExpression(const QRegularExpression ®Ex); private slots: void slotChanged(); diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory_p.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory_p.cpp index 963ae3c4bec..211214c527a 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory_p.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardpagefactory_p.cpp @@ -199,6 +199,8 @@ bool KitsPageFactory::validateData(Core::Id typeId, const QVariant &data, QStrin // ProjectPageFactory: // -------------------------------------------------------------------- +static const char KEY_PROJECT_NAME_VALIDATOR[] = "projectNameValidator"; + ProjectPageFactory::ProjectPageFactory() { setTypeIdsSuffix(QLatin1String("Project")); @@ -216,6 +218,13 @@ Utils::WizardPage *ProjectPageFactory::create(JsonWizard *wizard, Core::Id typeI QString description = tmp.value(QLatin1String("trDescription"), QLatin1String("%{trDescription}")).toString(); page->setDescription(wizard->expander()->expand(description)); + QString projectNameValidator + = tmp.value(QLatin1String(KEY_PROJECT_NAME_VALIDATOR)).toString(); + if (!projectNameValidator.isEmpty()) { + QRegularExpression regularExpression(projectNameValidator); + if (regularExpression.isValid()) + page->setProjectNameRegularExpression(regularExpression); + } return page; } @@ -230,6 +239,19 @@ bool ProjectPageFactory::validateData(Core::Id typeId, const QVariant &data, QSt "\"data\" must be empty or a JSON object for \"Project\" pages."); return false; } + QVariantMap tmp = data.toMap(); + QString projectNameValidator + = tmp.value(QLatin1String(KEY_PROJECT_NAME_VALIDATOR)).toString(); + if (!projectNameValidator.isNull()) { + QRegularExpression regularExpression(projectNameValidator); + if (!regularExpression.isValid()) { + *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", + "Invalid regular expression \"%1\" in \"%2\". %3").arg( + projectNameValidator, QLatin1String(KEY_PROJECT_NAME_VALIDATOR), regularExpression.errorString()); + return false; + } + } + return true; }