1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "contentlibraryview.h"
#include "contentlibrarybundleimporter.h"
#include "contentlibrarymaterial.h"
#include "contentlibrarymaterialsmodel.h"
#include "contentlibrarytexture.h"
#include "contentlibrarytexturesmodel.h"
#include "contentlibrarywidget.h"
#include "nodelistproperty.h"
#include "qmldesignerconstants.h"
#include "qmlobjectnode.h"
#include "variantproperty.h"
#include <coreplugin/messagebox.h>
#include <enumeration.h>
#include <utils/algorithm.h>
#ifndef QMLDESIGNER_TEST
#include <projectexplorer/kit.h>
#include <projectexplorer/projectmanager.h>
#include <projectexplorer/target.h>
#include <qtsupport/baseqtversion.h>
#include <qtsupport/qtkitinformation.h>
#endif
namespace QmlDesigner {
ContentLibraryView::ContentLibraryView(ExternalDependenciesInterface &externalDependencies)
: AbstractView(externalDependencies)
, m_createTexture(this)
{}
ContentLibraryView::~ContentLibraryView()
{}
bool ContentLibraryView::hasWidget() const
{
return true;
}
WidgetInfo ContentLibraryView::widgetInfo()
{
if (m_widget.isNull()) {
m_widget = new ContentLibraryWidget();
connect(m_widget, &ContentLibraryWidget::bundleMaterialDragStarted, this,
[&] (QmlDesigner::ContentLibraryMaterial *mat) {
m_draggedBundleMaterial = mat;
});
connect(m_widget, &ContentLibraryWidget::bundleTextureDragStarted, this,
[&] (QmlDesigner::ContentLibraryTexture *tex) {
m_draggedBundleTexture = tex;
});
connect(m_widget, &ContentLibraryWidget::addTextureRequested, this,
[&] (const QString &texPath, AddTextureMode mode) {
executeInTransaction("ContentLibraryView::widgetInfo", [&]() {
m_createTexture.execute(texPath, mode, m_sceneId);
});
});
connect(m_widget, &ContentLibraryWidget::updateSceneEnvStateRequested, this, [&]() {
ModelNode activeSceneEnv = m_createTexture.resolveSceneEnv(m_sceneId);
const bool sceneEnvExists = activeSceneEnv.isValid();
m_widget->texturesModel()->setHasSceneEnv(sceneEnvExists);
m_widget->environmentsModel()->setHasSceneEnv(sceneEnvExists);
});
ContentLibraryMaterialsModel *materialsModel = m_widget->materialsModel().data();
connect(materialsModel, &ContentLibraryMaterialsModel::applyToSelectedTriggered, this,
[&] (ContentLibraryMaterial *bundleMat, bool add) {
if (m_selectedModels.isEmpty())
return;
m_bundleMaterialTargets = m_selectedModels;
m_bundleMaterialAddToSelected = add;
ModelNode defaultMat = getBundleMaterialDefaultInstance(bundleMat->type());
if (defaultMat.isValid())
applyBundleMaterialToDropTarget(defaultMat);
else
m_widget->materialsModel()->addToProject(bundleMat);
});
connect(materialsModel, &ContentLibraryMaterialsModel::bundleMaterialImported, this,
[&] (const QmlDesigner::NodeMetaInfo &metaInfo) {
applyBundleMaterialToDropTarget({}, metaInfo);
updateBundleMaterialsImportedState();
});
connect(materialsModel, &ContentLibraryMaterialsModel::bundleMaterialAboutToUnimport, this,
[&] (const QmlDesigner::TypeName &type) {
// delete instances of the bundle material that is about to be unimported
executeInTransaction("ContentLibraryView::widgetInfo", [&] {
ModelNode matLib = materialLibraryNode();
if (!matLib.isValid())
return;
Utils::reverseForeach(matLib.directSubModelNodes(), [&](const ModelNode &mat) {
if (mat.isValid() && mat.type() == type)
QmlObjectNode(mat).destroy();
});
});
});
connect(materialsModel, &ContentLibraryMaterialsModel::bundleMaterialUnimported, this,
&ContentLibraryView::updateBundleMaterialsImportedState);
}
return createWidgetInfo(m_widget.data(),
"ContentLibrary",
WidgetInfo::LeftPane,
0,
tr("Content Library"));
}
void ContentLibraryView::modelAttached(Model *model)
{
AbstractView::modelAttached(model);
m_hasQuick3DImport = model->hasImport("QtQuick3D");
updateBundleMaterialsQuick3DVersion();
updateBundleMaterialsImportedState();
const bool hasLibrary = materialLibraryNode().isValid();
m_widget->setHasMaterialLibrary(hasLibrary);
m_widget->setHasQuick3DImport(m_hasQuick3DImport);
m_sceneId = model->active3DSceneId();
m_widget->clearSearchFilter();
}
void ContentLibraryView::modelAboutToBeDetached(Model *model)
{
m_widget->setHasMaterialLibrary(false);
m_widget->setHasQuick3DImport(false);
AbstractView::modelAboutToBeDetached(model);
}
void ContentLibraryView::importsChanged(const QList<Import> &addedImports, const QList<Import> &removedImports)
{
Q_UNUSED(addedImports)
Q_UNUSED(removedImports)
updateBundleMaterialsQuick3DVersion();
bool hasQuick3DImport = model()->hasImport("QtQuick3D");
if (hasQuick3DImport == m_hasQuick3DImport)
return;
m_hasQuick3DImport = hasQuick3DImport;
m_widget->setHasQuick3DImport(m_hasQuick3DImport);
}
void ContentLibraryView::active3DSceneChanged(qint32 sceneId)
{
m_sceneId = sceneId;
}
void ContentLibraryView::selectedNodesChanged(const QList<ModelNode> &selectedNodeList,
const QList<ModelNode> &lastSelectedNodeList)
{
Q_UNUSED(lastSelectedNodeList)
m_selectedModels = Utils::filtered(selectedNodeList, [](const ModelNode &node) {
return node.metaInfo().isQtQuick3DModel();
});
m_widget->materialsModel()->setHasModelSelection(!m_selectedModels.isEmpty());
}
void ContentLibraryView::customNotification(const AbstractView *view, const QString &identifier,
const QList<ModelNode> &nodeList, const QList<QVariant> &data)
{
Q_UNUSED(data)
if (view == this)
return;
if (identifier == "drop_bundle_material") {
ModelNode matLib = materialLibraryNode();
if (!matLib.isValid())
return;
m_bundleMaterialTargets = nodeList;
ModelNode defaultMat = getBundleMaterialDefaultInstance(m_draggedBundleMaterial->type());
if (defaultMat.isValid()) {
if (m_bundleMaterialTargets.isEmpty()) // if no drop target, create a duplicate material
createMaterial(defaultMat.metaInfo());
else
applyBundleMaterialToDropTarget(defaultMat);
} else {
m_widget->materialsModel()->addToProject(m_draggedBundleMaterial);
}
m_draggedBundleMaterial = nullptr;
} else if (identifier == "drop_bundle_texture") {
ModelNode matLib = materialLibraryNode();
if (!matLib.isValid())
return;
m_widget->addTexture(m_draggedBundleTexture);
m_draggedBundleTexture = nullptr;
}
}
void ContentLibraryView::nodeReparented(const ModelNode &node,
[[maybe_unused]] const NodeAbstractProperty &newPropertyParent,
[[maybe_unused]] const NodeAbstractProperty &oldPropertyParent,
[[maybe_unused]] PropertyChangeFlags propertyChange)
{
if (node.id() == Constants::MATERIAL_LIB_ID)
m_widget->setHasMaterialLibrary(true);
}
void ContentLibraryView::nodeAboutToBeRemoved(const ModelNode &removedNode)
{
if (removedNode.id() == Constants::MATERIAL_LIB_ID)
m_widget->setHasMaterialLibrary(false);
}
void ContentLibraryView::applyBundleMaterialToDropTarget(const ModelNode &bundleMat,
const NodeMetaInfo &metaInfo)
{
if (!bundleMat.isValid() && !metaInfo.isValid())
return;
executeInTransaction("ContentLibraryView::applyBundleMaterialToDropTarget", [&] {
ModelNode newMatNode = metaInfo.isValid() ? createMaterial(metaInfo) : bundleMat;
// TODO: unify this logic as it exist elsewhere also
auto expToList = [](const QString &exp) {
QString copy = exp;
copy = copy.remove("[").remove("]");
QStringList tmp = copy.split(',', Qt::SkipEmptyParts);
for (QString &str : tmp)
str = str.trimmed();
return tmp;
};
auto listToExp = [](QStringList &stringList) {
if (stringList.size() > 1)
return QString("[" + stringList.join(",") + "]");
if (stringList.size() == 1)
return stringList.first();
return QString();
};
for (const ModelNode &target : std::as_const(m_bundleMaterialTargets)) {
if (target.isValid() && target.metaInfo().isQtQuick3DModel()) {
QmlObjectNode qmlObjNode(target);
if (m_bundleMaterialAddToSelected) {
QStringList matList = expToList(qmlObjNode.expression("materials"));
matList.append(newMatNode.id());
QString updatedExp = listToExp(matList);
qmlObjNode.setBindingProperty("materials", updatedExp);
} else {
qmlObjNode.setBindingProperty("materials", newMatNode.id());
}
}
m_bundleMaterialTargets = {};
m_bundleMaterialAddToSelected = false;
}
});
}
ModelNode ContentLibraryView::getBundleMaterialDefaultInstance(const TypeName &type)
{
ModelNode matLib = materialLibraryNode();
if (!matLib.isValid())
return {};
const QList <ModelNode> matLibNodes = matLib.directSubModelNodes();
for (const ModelNode &mat : matLibNodes) {
if (mat.isValid() && mat.type() == type) {
bool isDefault = true;
const QList<AbstractProperty> props = mat.properties();
for (const AbstractProperty &prop : props) {
if (prop.name() != "objectName") {
isDefault = false;
break;
}
}
if (isDefault)
return mat;
}
}
return {};
}
ModelNode ContentLibraryView::createMaterial(const NodeMetaInfo &metaInfo)
{
ModelNode matLib = materialLibraryNode();
if (!matLib.isValid() || !metaInfo.isValid())
return {};
ModelNode newMatNode = createModelNode(metaInfo.typeName(), metaInfo.majorVersion(),
metaInfo.minorVersion());
matLib.defaultNodeListProperty().reparentHere(newMatNode);
static QRegularExpression rgx("([A-Z])([a-z]*)");
QString newName = QString::fromLatin1(metaInfo.simplifiedTypeName()).replace(rgx, " \\1\\2").trimmed();
if (newName.endsWith(" Material"))
newName.chop(9); // remove trailing " Material"
QString newId = model()->generateIdFromName(newName, "material");
newMatNode.setIdWithRefactoring(newId);
VariantProperty objNameProp = newMatNode.variantProperty("objectName");
objNameProp.setValue(newName);
emitCustomNotification("focus_material_section", {});
return newMatNode;
}
void ContentLibraryView::updateBundleMaterialsImportedState()
{
using namespace Utils;
if (!m_widget->materialsModel()->bundleImporter())
return;
QStringList importedBundleMats;
FilePath materialBundlePath = m_widget->materialsModel()->bundleImporter()->resolveBundleImportPath();
if (materialBundlePath.exists()) {
importedBundleMats = transform(materialBundlePath.dirEntries({{"*.qml"}, QDir::Files}),
[](const FilePath &f) { return f.fileName().chopped(4); });
}
m_widget->materialsModel()->updateImportedState(importedBundleMats);
}
void ContentLibraryView::updateBundleMaterialsQuick3DVersion()
{
bool hasImport = false;
int major = -1;
int minor = -1;
const QString url {"QtQuick3D"};
const auto imports = model()->imports();
for (const auto &import : imports) {
if (import.url() == url) {
hasImport = true;
const int importMajor = import.majorVersion();
if (major < importMajor) {
minor = -1;
major = importMajor;
}
if (major == importMajor)
minor = qMax(minor, import.minorVersion());
}
}
#ifndef QMLDESIGNER_TEST
if (hasImport && major == -1) {
// Import without specifying version, so we take the kit version
auto target = ProjectExplorer::ProjectManager::startupTarget();
if (target) {
QtSupport::QtVersion *qtVersion = QtSupport::QtKitAspect::qtVersion(target->kit());
if (qtVersion) {
major = qtVersion->qtVersion().majorVersion();
minor = qtVersion->qtVersion().minorVersion();
}
}
}
#endif
m_widget->materialsModel()->setQuick3DImportVersion(major, minor);
}
} // namespace QmlDesigner
|