QmlDesigner: Add suffux to invalid ids

Instead of replacing them with "element" add a suffix.

Change-Id: Idc72ba9a8a7b4f48d4e325378b975cafeab88bd4
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Thomas Hartmann
2020-08-21 16:44:04 +02:00
parent 151373396f
commit acdb971048

View File

@@ -508,9 +508,13 @@ QString AbstractView::generateNewId(const QString &prefixName) const
{
QString fixedPrefix = firstCharToLower(prefixName);
fixedPrefix.remove(' ');
bool forceSuffix = false;
if (!ModelNode::isValidId(fixedPrefix))
return generateNewId("element");
int counter = 1;
forceSuffix = true;
int counter = 0;
/* First try just the prefixName without number as postfix, then continue with 2 and further as postfix
* until id does not already exist.
@@ -520,11 +524,14 @@ QString AbstractView::generateNewId(const QString &prefixName) const
*/
QString newId = QString(QStringLiteral("%1")).arg(firstCharToLower(prefixName));
if (forceSuffix)
QString(QStringLiteral("%1%2")).arg(firstCharToLower(prefixName)).arg(1);
newId.remove(QRegExp(QStringLiteral("[^a-zA-Z0-9_]")));
while (!ModelNode::isValidId(newId) || hasId(newId) || rootModelNode().hasProperty(newId.toUtf8()) || newId == "item") {
counter += 1;
newId = QString(QStringLiteral("%1%2")).arg(firstCharToLower(prefixName)).arg(counter - 1);
newId = QString(QStringLiteral("%1%2")).arg(firstCharToLower(prefixName)).arg(counter);
newId.remove(QRegExp(QStringLiteral("[^a-zA-Z0-9_]")));
}