From 7fe61d1f970aae4ccb9d57c434eb457b8eb33402 Mon Sep 17 00:00:00 2001 From: Mahmoud Badri Date: Mon, 13 Sep 2021 20:38:32 +0300 Subject: [PATCH] QmlDesigner: Provide a descriptive message for invalid IDs Fixes: QDS-5044 Change-Id: I12f78851f5200e417fe6f323a0773587400cd90f Reviewed-by: Johanna Vanhatapio Reviewed-by: Leena Miettinen Reviewed-by: Thomas Hartmann --- .../propertyeditor/propertyeditorview.cpp | 5 ++-- .../designercore/include/modelnode.h | 2 ++ .../designercore/model/modelnode.cpp | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp index 6fa649e699e..a6030701721 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorview.cpp @@ -163,8 +163,9 @@ void PropertyEditorView::changeValue(const QString &name) m_locked = true; value->setValue(m_selectedNode.id()); m_locked = false; - if (!QmlDesigner::ModelNode::isValidId(newId)) - Core::AsynchronousMessageBox::warning(tr("Invalid ID"), tr("%1 is an invalid ID.").arg(newId)); + QString errMsg = QmlDesigner::ModelNode::getIdValidityErrorMessage(newId); + if (!errMsg.isEmpty()) + Core::AsynchronousMessageBox::warning(tr("Invalid ID"), errMsg.arg(newId)); else Core::AsynchronousMessageBox::warning(tr("Invalid ID"), tr("%1 already exists.").arg(newId)); } diff --git a/src/plugins/qmldesigner/designercore/include/modelnode.h b/src/plugins/qmldesigner/designercore/include/modelnode.h index 961d3914059..7fba10d3cd9 100644 --- a/src/plugins/qmldesigner/designercore/include/modelnode.h +++ b/src/plugins/qmldesigner/designercore/include/modelnode.h @@ -168,6 +168,8 @@ public: void setIdWithRefactoring(const QString &id); void setIdWithoutRefactoring(const QString &id); static bool isValidId(const QString &id); + static QString getIdValidityErrorMessage(const QString &id); + bool hasId() const; Model *model() const; diff --git a/src/plugins/qmldesigner/designercore/model/modelnode.cpp b/src/plugins/qmldesigner/designercore/model/modelnode.cpp index 64b51ff69fa..9e1e260bb09 100644 --- a/src/plugins/qmldesigner/designercore/model/modelnode.cpp +++ b/src/plugins/qmldesigner/designercore/model/modelnode.cpp @@ -212,6 +212,29 @@ bool ModelNode::isValidId(const QString &id) return id.isEmpty() || (!idContainsWrongLetter(id) && !idIsQmlKeyWord(id) && !isIdToAvoid(id)); } +QString ModelNode::getIdValidityErrorMessage(const QString &id) +{ + if (isValidId(id)) + return {}; // valid + + if (id.at(0).isUpper()) + return QObject::tr("ID cannot start with an uppercase character."); + + if (id.at(0).isDigit()) + return QObject::tr("ID cannot start with a number."); + + if (id.contains(' ')) + return QObject::tr("ID cannot include whitespace."); + + if (idIsQmlKeyWord(id)) + return QObject::tr("%1 is a reserved QML keyword."); + + if (isIdToAvoid(id)) + return QObject::tr("%1 is a reserved property keyword."); + + return QObject::tr("ID includes invalid characters."); +} + bool ModelNode::hasId() const { if (!isValid())