forked from qt-creator/qt-creator
QmlDesigner: Provide a descriptive message for invalid IDs
Fixes: QDS-5044 Change-Id: I12f78851f5200e417fe6f323a0773587400cd90f Reviewed-by: Johanna Vanhatapio <johanna.vanhatapio@qt.io> Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
@@ -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;
|
||||
|
@@ -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())
|
||||
|
Reference in New Issue
Block a user