Commit e6d03836 authored by Tim Jenssen's avatar Tim Jenssen
Browse files

add projectNameValidator to JsonProjectPage



Change-Id: Ie9851a8f16d494ef2a6fafdcc83b5b47bcd0e021
Reviewed-by: default avatarTobias Hunger <tobias.hunger@theqtcompany.com>
parent 59ac67c0
......@@ -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("<Enter_Name>"));
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 &regEx)
{
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 = FileUtils::indexOfQmakeUnfriendly(name);
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);
// 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;
}
......
......@@ -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 &regEx);
private slots:
void slotChanged();
......
......@@ -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;
}
......
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