forked from qt-creator/qt-creator
QmlDesigner: Move MaterialEditorView::generateIdFromName() to the model
Change-Id: I78aa373f7b8d70d5494866b2e2855435c9e04b04 Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
@@ -990,7 +990,7 @@ void MaterialEditorView::renameMaterial(ModelNode &material, const QString &newN
|
||||
QTC_ASSERT(material.isValid(), return);
|
||||
|
||||
executeInTransaction("MaterialEditorView:renameMaterial", [&] {
|
||||
material.setIdWithRefactoring(generateIdFromName(newName));
|
||||
material.setIdWithRefactoring(model()->generateIdFromName(newName, "material"));
|
||||
|
||||
VariantProperty objNameProp = material.variantProperty("objectName");
|
||||
objNameProp.setValue(newName);
|
||||
@@ -1019,7 +1019,7 @@ void MaterialEditorView::duplicateMaterial(const ModelNode &material)
|
||||
// set name and id
|
||||
QString newName = sourceMat.modelNode().variantProperty("objectName").value().toString() + " copy";
|
||||
duplicateMat.modelNode().variantProperty("objectName").setValue(newName);
|
||||
duplicateMat.modelNode().setIdWithoutRefactoring(generateIdFromName(newName));
|
||||
duplicateMat.modelNode().setIdWithoutRefactoring(model()->generateIdFromName(newName, "material"));
|
||||
|
||||
// sync properties
|
||||
const QList<AbstractProperty> props = material.properties();
|
||||
@@ -1127,38 +1127,4 @@ void MaterialEditorView::reloadQml()
|
||||
resetView();
|
||||
}
|
||||
|
||||
// generate a unique camelCase id from a name
|
||||
QString MaterialEditorView::generateIdFromName(const QString &name)
|
||||
{
|
||||
QString newId;
|
||||
if (name.isEmpty()) {
|
||||
newId = "material";
|
||||
} else {
|
||||
// convert to camel case
|
||||
QStringList nameWords = name.split(" ");
|
||||
nameWords[0] = nameWords[0].at(0).toLower() + nameWords[0].mid(1);
|
||||
for (int i = 1; i < nameWords.size(); ++i)
|
||||
nameWords[i] = nameWords[i].at(0).toUpper() + nameWords[i].mid(1);
|
||||
newId = nameWords.join("");
|
||||
|
||||
// if id starts with a number prepend an underscore
|
||||
if (newId.at(0).isDigit())
|
||||
newId.prepend('_');
|
||||
}
|
||||
|
||||
QRegularExpression rgx("\\d+$"); // matches a number at the end of a string
|
||||
while (hasId(newId)) { // id exists
|
||||
QRegularExpressionMatch match = rgx.match(newId);
|
||||
if (match.hasMatch()) { // ends with a number, increment it
|
||||
QString numStr = match.captured();
|
||||
int num = numStr.toInt() + 1;
|
||||
newId = newId.mid(0, match.capturedStart()) + QString::number(num);
|
||||
} else {
|
||||
newId.append('1');
|
||||
}
|
||||
}
|
||||
|
||||
return newId;
|
||||
}
|
||||
|
||||
} // namespace QmlDesigner
|
||||
|
@@ -114,7 +114,6 @@ private:
|
||||
|
||||
void reloadQml();
|
||||
void highlightSupportedProperties(bool highlight = true);
|
||||
QString generateIdFromName(const QString &name);
|
||||
|
||||
void requestPreviewRender();
|
||||
void applyMaterialToSelectedModels(const ModelNode &material, bool add = false);
|
||||
|
@@ -128,8 +128,8 @@ public:
|
||||
bool hasId(const QString &id) const;
|
||||
bool hasImport(const QString &importUrl) const;
|
||||
|
||||
QString generateNewId(const QString &prefixName) const;
|
||||
QString generateNewId(const QString &prefixName, const QString &fallbackPrefix) const;
|
||||
QString generateNewId(const QString &prefixName, const QString &fallbackPrefix = "element") const;
|
||||
QString generateIdFromName(const QString &name, const QString &fallbackId = "element") const;
|
||||
|
||||
void startDrag(QMimeData *mimeData, const QPixmap &icon);
|
||||
void endDrag();
|
||||
|
@@ -1557,6 +1557,41 @@ QString Model::generateNewId(const QString &prefixName, const QString &fallbackP
|
||||
return newId;
|
||||
}
|
||||
|
||||
// Generate a unique camelCase id from a name
|
||||
// note: this methods does the same as generateNewId(). The 2 methods should be merged into one
|
||||
QString Model::generateIdFromName(const QString &name, const QString &fallbackId) const
|
||||
{
|
||||
QString newId;
|
||||
if (name.isEmpty()) {
|
||||
newId = fallbackId;
|
||||
} else {
|
||||
// convert to camel case
|
||||
QStringList nameWords = name.split(" ");
|
||||
nameWords[0] = nameWords[0].at(0).toLower() + nameWords[0].mid(1);
|
||||
for (int i = 1; i < nameWords.size(); ++i)
|
||||
nameWords[i] = nameWords[i].at(0).toUpper() + nameWords[i].mid(1);
|
||||
newId = nameWords.join("");
|
||||
|
||||
// if id starts with a number prepend an underscore
|
||||
if (newId.at(0).isDigit())
|
||||
newId.prepend('_');
|
||||
}
|
||||
|
||||
QRegularExpression rgx("\\d+$"); // matches a number at the end of a string
|
||||
while (hasId(newId)) { // id exists
|
||||
QRegularExpressionMatch match = rgx.match(newId);
|
||||
if (match.hasMatch()) { // ends with a number, increment it
|
||||
QString numStr = match.captured();
|
||||
int num = numStr.toInt() + 1;
|
||||
newId = newId.mid(0, match.capturedStart()) + QString::number(num);
|
||||
} else {
|
||||
newId.append('1');
|
||||
}
|
||||
}
|
||||
|
||||
return newId;
|
||||
}
|
||||
|
||||
void Model::startDrag(QMimeData *mimeData, const QPixmap &icon)
|
||||
{
|
||||
d->notifyDragStarted(mimeData);
|
||||
@@ -1575,11 +1610,6 @@ void Model::endDrag()
|
||||
d->notifyDragEnded();
|
||||
}
|
||||
|
||||
QString Model::generateNewId(const QString &prefixName) const
|
||||
{
|
||||
return generateNewId(prefixName, QStringLiteral("element"));
|
||||
}
|
||||
|
||||
bool Model::isImportPossible(const Import &import, bool ignoreAlias, bool allowHigherVersion) const
|
||||
{
|
||||
if (imports().contains(import))
|
||||
|
Reference in New Issue
Block a user