forked from qt-creator/qt-creator
QmlDesigner: Confirm adding existing material to content library
Also remove methods not used anymore and make addLibMaterial() as close as possible to addLib3DItem() in preparation to unifying them. Change-Id: I4b0bbf3667c8203fd71a5440aee02d62a67673ca Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
This commit is contained in:
@@ -14,7 +14,6 @@
|
|||||||
#include <imageutils.h>
|
#include <imageutils.h>
|
||||||
#include <qmldesignerconstants.h>
|
#include <qmldesignerconstants.h>
|
||||||
#include <qmldesignerplugin.h>
|
#include <qmldesignerplugin.h>
|
||||||
#include <uniquename.h>
|
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
@@ -218,64 +217,6 @@ ContentLibraryUserModel::SectionIndex ContentLibraryUserModel::bundleIdToSection
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Gets unique Qml component and icon file material names from a given name
|
|
||||||
* @param defaultName input name
|
|
||||||
* @return <Qml, icon> file names
|
|
||||||
*/
|
|
||||||
QPair<QString, QString> ContentLibraryUserModel::getUniqueLibMaterialNames(const QString &defaultName) const
|
|
||||||
{
|
|
||||||
const QJsonObject bundleObj = qobject_cast<UserItemCategory *>(
|
|
||||||
m_userCategories.at(Items3DSectionIdx))->bundleObjRef();
|
|
||||||
return getUniqueLibItemNames(defaultName, bundleObj);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Gets unique Qml component and icon file 3d item names from a given name
|
|
||||||
* @param defaultName input name
|
|
||||||
* @return <Qml, icon> file names
|
|
||||||
*/
|
|
||||||
QPair<QString, QString> ContentLibraryUserModel::getUniqueLib3DNames(const QString &defaultName) const
|
|
||||||
{
|
|
||||||
const QJsonObject bundleObj = qobject_cast<UserItemCategory *>(
|
|
||||||
m_userCategories.at(Items3DSectionIdx))->bundleObjRef();
|
|
||||||
return getUniqueLibItemNames(defaultName, bundleObj);
|
|
||||||
}
|
|
||||||
|
|
||||||
QPair<QString, QString> ContentLibraryUserModel::getUniqueLibItemNames(const QString &defaultName,
|
|
||||||
const QJsonObject &bundleObj) const
|
|
||||||
{
|
|
||||||
QString uniqueQml = UniqueName::generateId(defaultName);
|
|
||||||
if (uniqueQml.isEmpty())
|
|
||||||
uniqueQml = "Component";
|
|
||||||
else
|
|
||||||
uniqueQml[0] = uniqueQml.at(0).toUpper();
|
|
||||||
uniqueQml.prepend("My");
|
|
||||||
|
|
||||||
QString uniqueIcon = defaultName;
|
|
||||||
|
|
||||||
if (!bundleObj.isEmpty()) {
|
|
||||||
const QJsonArray itemsArr = bundleObj.value("items").toArray();
|
|
||||||
|
|
||||||
QStringList itemQmls, itemIcons;
|
|
||||||
for (const QJsonValueConstRef &itemRef : itemsArr) {
|
|
||||||
const QJsonObject &obj = itemRef.toObject();
|
|
||||||
itemQmls.append(obj.value("qml").toString().chopped(4)); // remove .qml
|
|
||||||
itemIcons.append(QFileInfo(obj.value("icon").toString()).baseName());
|
|
||||||
}
|
|
||||||
|
|
||||||
uniqueQml = UniqueName::generate(uniqueQml, [&] (const QString &name) {
|
|
||||||
return itemQmls.contains(name);
|
|
||||||
});
|
|
||||||
|
|
||||||
uniqueIcon = UniqueName::generate(uniqueIcon, [&] (const QString &name) {
|
|
||||||
return itemIcons.contains(name);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return {uniqueQml + ".qml", uniqueIcon + ".png"};
|
|
||||||
}
|
|
||||||
|
|
||||||
QHash<int, QByteArray> ContentLibraryUserModel::roleNames() const
|
QHash<int, QByteArray> ContentLibraryUserModel::roleNames() const
|
||||||
{
|
{
|
||||||
static const QHash<int, QByteArray> roles {
|
static const QHash<int, QByteArray> roles {
|
||||||
|
@@ -38,10 +38,6 @@ public:
|
|||||||
|
|
||||||
bool jsonPropertyExists(const QString &propName, const QString &propValue,
|
bool jsonPropertyExists(const QString &propName, const QString &propValue,
|
||||||
const QString &bundleId) const;
|
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",
|
|
||||||
const QJsonObject &bundleObj = {}) const;
|
|
||||||
|
|
||||||
void setQuick3DImportVersion(int major, int minor);
|
void setQuick3DImportVersion(int major, int minor);
|
||||||
|
|
||||||
|
@@ -521,34 +521,57 @@ void ContentLibraryView::applyBundleMaterialToDropTarget(const ModelNode &bundle
|
|||||||
// Add a project material to Content Library's user tab
|
// Add a project material to Content Library's user tab
|
||||||
void ContentLibraryView::addLibMaterial(const ModelNode &node, const QPixmap &iconPixmap)
|
void ContentLibraryView::addLibMaterial(const ModelNode &node, const QPixmap &iconPixmap)
|
||||||
{
|
{
|
||||||
|
auto compUtils = QmlDesignerPlugin::instance()->documentManager().generatedComponentUtils();
|
||||||
auto bundlePath = Utils::FilePath::fromString(Paths::bundlesPathSetting() + "/User/materials/");
|
auto bundlePath = Utils::FilePath::fromString(Paths::bundlesPathSetting() + "/User/materials/");
|
||||||
|
QString bundleId = compUtils.userMaterialsBundleId();
|
||||||
|
|
||||||
QString name = node.variantProperty("objectName").value().toString();
|
QString name = node.variantProperty("objectName").value().toString();
|
||||||
if (name.isEmpty())
|
if (name.isEmpty())
|
||||||
name = node.displayName();
|
name = node.displayName();
|
||||||
|
|
||||||
auto [qml, icon] = m_widget->userModel()->getUniqueLibMaterialNames(name);
|
QJsonObject &jsonRef = m_widget->userModel()->bundleObjectRef(bundleId);
|
||||||
|
QJsonArray itemsArr = jsonRef.value("items").toArray();
|
||||||
|
|
||||||
QString iconPath = QLatin1String("icons/%1").arg(icon);
|
QString qml = nodeNameToComponentFileName(name);
|
||||||
QString fullIconPath = bundlePath.pathAppended(iconPath).toFSPathString();
|
|
||||||
|
|
||||||
// save icon
|
// confirm overwrite if an item with same name exists
|
||||||
bool iconSaved = iconPixmap.save(fullIconPath);
|
if (m_widget->userModel()->jsonPropertyExists("qml", qml, bundleId)) {
|
||||||
if (!iconSaved)
|
QMessageBox::StandardButton reply = QMessageBox::question(m_widget, tr("Material Exists"),
|
||||||
qWarning() << __FUNCTION__ << "icon save failed";
|
tr("A material 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;
|
||||||
|
|
||||||
// generate and save material Qml file
|
// 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);
|
auto [qmlString, depAssets] = modelNodeToQmlString(node);
|
||||||
const QStringList depAssetsList = depAssets.values();
|
const QStringList depAssetsList = depAssets.values();
|
||||||
|
|
||||||
auto result = bundlePath.pathAppended(qml).writeFileContents(qmlString.toUtf8());
|
auto result = bundlePath.pathAppended(qml).writeFileContents(qmlString.toUtf8());
|
||||||
QTC_ASSERT_EXPECTED(result,);
|
QTC_ASSERT_EXPECTED(result,);
|
||||||
|
|
||||||
|
// save 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();
|
||||||
|
|
||||||
|
bool iconSaved = iconPixmap.save(m_iconSavePath.toFSPathString());
|
||||||
|
if (!iconSaved)
|
||||||
|
qWarning() << __FUNCTION__ << "icon save failed";
|
||||||
|
|
||||||
// add the material to the bundle json
|
// add the material to the bundle json
|
||||||
auto compUtils = QmlDesignerPlugin::instance()->documentManager().generatedComponentUtils();
|
itemsArr = jsonRef.value("items").toArray();
|
||||||
QString bundleId = compUtils.userMaterialsBundleId();
|
|
||||||
QJsonObject &jsonRef = m_widget->userModel()->bundleObjectRef(bundleId);
|
|
||||||
QJsonArray itemsArr = jsonRef.value("items").toArray();
|
|
||||||
itemsArr.append(QJsonObject {
|
itemsArr.append(QJsonObject {
|
||||||
{"name", name},
|
{"name", name},
|
||||||
{"qml", qml},
|
{"qml", qml},
|
||||||
@@ -572,7 +595,7 @@ void ContentLibraryView::addLibMaterial(const ModelNode &node, const QPixmap &ic
|
|||||||
QTC_ASSERT_EXPECTED(result,);
|
QTC_ASSERT_EXPECTED(result,);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_widget->userModel()->addItem(bundleId, name, qml, QUrl::fromLocalFile(fullIconPath), depAssetsList);
|
m_widget->userModel()->addItem(bundleId, name, qml, m_iconSavePath.toUrl(), depAssetsList);
|
||||||
m_widget->userModel()->refreshSection(bundleId);
|
m_widget->userModel()->refreshSection(bundleId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user