forked from qt-creator/qt-creator
QmlDesigner: Confirm adding an existing item to content lib
Change-Id: Ic3c007444a1c061161268548e0f180ca5335b8bc Reviewed-by: Ali Kianian <ali.kianian@qt.io> Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
This commit is contained in:
@@ -352,6 +352,25 @@ void ContentLibraryUserModel::updateImportedState(const QStringList &importedIte
|
||||
emit dataChanged(index(secIdx), index(secIdx), {ItemsRole});
|
||||
}
|
||||
|
||||
bool ContentLibraryUserModel::jsonPropertyExists(const QString &propName, const QString &propValue,
|
||||
const QString &bundleId) const
|
||||
{
|
||||
SectionIndex secIdx = bundleIdToSectionIndex(bundleId);
|
||||
UserItemCategory *cat = qobject_cast<UserItemCategory *>(m_userCategories.at(secIdx));
|
||||
QTC_ASSERT(cat, return false);
|
||||
|
||||
QJsonObject &bundleObj = cat->bundleObjRef();
|
||||
const QJsonArray itemsArr = bundleObj.value("items").toArray();
|
||||
|
||||
for (const QJsonValueConstRef &itemRef : itemsArr) {
|
||||
const QJsonObject &obj = itemRef.toObject();
|
||||
if (obj.value(propName).toString() == propValue)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ContentLibraryUserModel::setQuick3DImportVersion(int major, int minor)
|
||||
{
|
||||
bool oldRequiredImport = hasRequiredQuick3DImport();
|
||||
|
@@ -36,6 +36,8 @@ public:
|
||||
void setSearchText(const QString &searchText);
|
||||
void updateImportedState(const QStringList &importedItems, const QString &bundleId);
|
||||
|
||||
bool jsonPropertyExists(const QString &propName, const QString &propValue,
|
||||
const QString &bundleId) const;
|
||||
QPair<QString, QString> getUniqueLibMaterialNames(const QString &defaultName = "Material") const;
|
||||
QPair<QString, QString> getUniqueLib3DNames(const QString &defaultName = "Item") const;
|
||||
QPair<QString, QString> getUniqueLibItemNames(const QString &defaultName = "Item",
|
||||
|
@@ -833,6 +833,18 @@ void ContentLibraryView::exportLib3DComponent(const ModelNode &node)
|
||||
});
|
||||
}
|
||||
|
||||
QString ContentLibraryView::nodeNameToComponentFileName(const QString &name) const
|
||||
{
|
||||
QString fileName = UniqueName::generateId(name);
|
||||
if (fileName.isEmpty())
|
||||
fileName = "Component";
|
||||
else
|
||||
fileName[0] = fileName.at(0).toUpper();
|
||||
fileName.prepend("My");
|
||||
|
||||
return fileName + ".qml";
|
||||
}
|
||||
|
||||
void ContentLibraryView::addLib3DItem(const ModelNode &node)
|
||||
{
|
||||
auto compUtils = QmlDesignerPlugin::instance()->documentManager().generatedComponentUtils();
|
||||
@@ -844,8 +856,23 @@ void ContentLibraryView::addLib3DItem(const ModelNode &node)
|
||||
name = node.displayName();
|
||||
|
||||
QJsonObject &jsonRef = m_widget->userModel()->bundleObjectRef(bundleId);
|
||||
QJsonArray itemsArr = jsonRef.value("items").toArray();
|
||||
|
||||
auto [qml, icon] = m_widget->userModel()->getUniqueLibItemNames(name, jsonRef);
|
||||
QString qml = nodeNameToComponentFileName(name);
|
||||
|
||||
// confirm overwrite if an item with same name exists
|
||||
if (m_widget->userModel()->jsonPropertyExists("qml", qml, bundleId)) {
|
||||
QMessageBox::StandardButton reply = QMessageBox::question(m_widget, tr("Component Exists"),
|
||||
tr("A component with the same name '%1' already "
|
||||
"exists in the Content Library, are you sure "
|
||||
"you want to overwrite it?")
|
||||
.arg(qml), QMessageBox::Yes | QMessageBox::No);
|
||||
if (reply == QMessageBox::No)
|
||||
return;
|
||||
|
||||
// before overwriting remove old item (to avoid partial items and dangling assets)
|
||||
m_widget->userModel()->removeItemByName(qml, bundleId);
|
||||
}
|
||||
|
||||
// generate and save Qml file
|
||||
auto [qmlString, depAssets] = modelNodeToQmlString(node);
|
||||
@@ -855,7 +882,13 @@ void ContentLibraryView::addLib3DItem(const ModelNode &node)
|
||||
QTC_ASSERT_EXPECTED(result,);
|
||||
|
||||
// generate and save icon
|
||||
QString iconPath = QLatin1String("icons/%1").arg(icon);
|
||||
QString iconPathTemplate = QLatin1String("icons/%1.png");
|
||||
QString iconBaseName = UniqueName::generateId(name, [&] (const QString &currName) {
|
||||
return m_widget->userModel()->jsonPropertyExists("icon", iconPathTemplate.arg(currName),
|
||||
bundleId);
|
||||
});
|
||||
|
||||
QString iconPath = iconPathTemplate.arg(iconBaseName);
|
||||
m_iconSavePath = bundlePath.pathAppended(iconPath);
|
||||
m_iconSavePath.parentDir().ensureWritableDir();
|
||||
|
||||
@@ -876,7 +909,7 @@ void ContentLibraryView::addLib3DItem(const ModelNode &node)
|
||||
});
|
||||
|
||||
// add the item to the bundle json
|
||||
QJsonArray itemsArr = jsonRef.value("items").toArray();
|
||||
itemsArr = jsonRef.value("items").toArray();
|
||||
itemsArr.append(QJsonObject {
|
||||
{"name", name},
|
||||
{"qml", qml},
|
||||
@@ -950,7 +983,8 @@ void ContentLibraryView::exportLib3DItem(const ModelNode &node, const QPixmap &i
|
||||
if (name.isEmpty())
|
||||
name = node.displayName();
|
||||
|
||||
auto [qml, icon] = m_widget->userModel()->getUniqueLibItemNames(name);
|
||||
QString qml = nodeNameToComponentFileName(name);
|
||||
QString iconBaseName = UniqueName::generateId(name);
|
||||
|
||||
// generate and save Qml file
|
||||
auto [qmlString, depAssets] = modelNodeToQmlString(node);
|
||||
@@ -961,7 +995,7 @@ void ContentLibraryView::exportLib3DItem(const ModelNode &node, const QPixmap &i
|
||||
QTC_ASSERT_EXPECTED(result, return);
|
||||
m_zipWriter->addFile(qmlFilePath.fileName(), qmlString.toUtf8());
|
||||
|
||||
QString iconPath = QLatin1String("icons/%1").arg(icon);
|
||||
QString iconPath = QLatin1String("icons/%1.png").arg(iconBaseName);
|
||||
|
||||
// add the item to the bundle json
|
||||
QJsonObject jsonObj;
|
||||
|
@@ -72,6 +72,7 @@ private:
|
||||
std::function<void(const QImage &image)> successCallback);
|
||||
QString getExportPath(const ModelNode &node) const;
|
||||
QString getImportPath() const;
|
||||
QString nodeNameToComponentFileName(const QString &name) const;
|
||||
QPair<QString, QSet<QString>> modelNodeToQmlString(const ModelNode &node, int depth = 0);
|
||||
|
||||
#ifdef QDS_USE_PROJECTSTORAGE
|
||||
|
Reference in New Issue
Block a user