aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/edit3d/bakelights.cpp
blob: 35d59f07ef47c11d41b6e77cafe8add1900f7fca (plain)
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "bakelights.h"

#include <abstractview.h>
#include <auxiliarydataproperties.h>
#include <bakelightsconnectionmanager.h>
#include <bakelightsdatamodel.h>
#include <bindingproperty.h>
#include <documentmanager.h>
#include <model/modelutils.h>
#include <modelnode.h>
#include <nodeabstractproperty.h>
#include <nodeinstanceview.h>
#include <nodemetainfo.h>
#include <plaintexteditmodifier.h>
#include <rewriterview.h>
#include <variantproperty.h>

#include <coreplugin/icore.h>

#include <qmljs/qmljsmodelmanagerinterface.h>

#include <utils/algorithm.h>
#include <utils/environment.h>
#include <utils/filepath.h>
#include <utils/qtcassert.h>

#include <QEvent>
#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickView>
#include <QSaveFile>
#include <QTextCursor>
#include <QTextDocument>
#include <QTimer>
#include <QVariant>

namespace QmlDesigner {

static QString propertyEditorResourcesPath()
{
#ifdef SHARE_QML_PATH
    if (Utils::qtcEnvironmentVariableIsSet("LOAD_QML_FROM_SOURCE"))
        return QLatin1String(SHARE_QML_PATH) + "/propertyEditorQmlSources";
#endif
    return Core::ICore::resourcePath("qmldesigner/propertyEditorQmlSources").toString();
}

static QString qmlSourcesPath()
{
#ifdef SHARE_QML_PATH
    if (Utils::qtcEnvironmentVariableIsSet("LOAD_QML_FROM_SOURCE"))
        return QLatin1String(SHARE_QML_PATH) + "/edit3dQmlSource";
#endif
    return Core::ICore::resourcePath("qmldesigner/edit3dQmlSource").toString();
}

BakeLights::BakeLights(AbstractView *view)
    : QObject(view)
    , m_view(view)
{
    m_view3dId = resolveView3dId(view);

    if (m_view3dId.isEmpty()) {
        // It should never get here, baking controls should be disabled in this case
        qWarning() << __FUNCTION__ << "Active scene is not View3D";
        deleteLater();
        return;
    }

    showSetupDialog();
}

BakeLights::~BakeLights()
{
    cleanup();
}

ModelNode BakeLights::resolveView3dNode(AbstractView *view)
{
    if (!view || !view->model())
        return {};

    ModelNode activeView3D;
    ModelNode activeScene = view->active3DSceneNode();

    if (activeScene.isValid()) {
        if (activeScene.metaInfo().isQtQuick3DView3D()) {
            activeView3D = activeScene;
        } else {
            ModelNode sceneParent = activeScene.parentProperty().parentModelNode();
            if (sceneParent.metaInfo().isQtQuick3DView3D())
                activeView3D = sceneParent;
        }
        return activeView3D;
    }

    return {};
}

QString BakeLights::resolveView3dId(AbstractView *view)
{
    ModelNode activeView3D = resolveView3dNode(view);

    if (activeView3D.isValid())
        return activeView3D.id();

    return {};
}

void BakeLights::raiseDialog()
{
    if (m_progressDialog)
        m_progressDialog->raise();
}

bool BakeLights::manualMode() const
{
    return m_manualMode;
}

void BakeLights::setManualMode(bool enabled)
{
    if (m_manualMode != enabled) {
        m_manualMode = enabled;
        emit manualModeChanged();
    }
}

void BakeLights::bakeLights()
{
    if (!m_view || !m_view->model())
        return;

    m_setupDialog->hide();
    showProgressDialog();

    // Start baking process
    m_connectionManager = new BakeLightsConnectionManager;
    m_rewriterView = new RewriterView{m_view->externalDependencies(), RewriterView::Amend};
    m_nodeInstanceView = new NodeInstanceView{*m_connectionManager, m_view->externalDependencies()};

    m_model = QmlDesigner::Model::create("QtQuick/Item", 2, 1);
    m_model->setFileUrl(m_view->model()->fileUrl());

    // Take the current unsaved state of the main model and apply it to our copy
    auto textDocument = std::make_unique<QTextDocument>(
                m_view->model()->rewriterView()->textModifier()->textDocument()->toRawText());

    auto modifier = std::make_unique<NotIndentingTextEditModifier>(textDocument.get(),
                                                                   QTextCursor{textDocument.get()});

    m_rewriterView->setTextModifier(modifier.get());
    m_model->setRewriterView(m_rewriterView);

    auto rootModelNodeMetaInfo = m_rewriterView->rootModelNode().metaInfo();
    bool is3DRoot = m_rewriterView->errors().isEmpty()
                    && (rootModelNodeMetaInfo.isQtQuick3DNode()
                        || rootModelNodeMetaInfo.isQtQuick3DMaterial());

    if (!m_rewriterView->errors().isEmpty()
            || (!m_rewriterView->rootModelNode().metaInfo().isGraphicalItem() && !is3DRoot)) {
        emit progress(tr("Invalid root node, baking aborted."));
        emit finished();
        m_progressDialog->raise();
        return;
    }

    m_nodeInstanceView->setTarget(m_view->nodeInstanceView()->target());

    auto progressCallback = [this](const QString &msg) {
        emit progress(msg);
    };

    auto finishedCallback = [this](const QString &msg) {
        m_progressDialog->raise();
        emit progress(msg);
        emit finished();

        // Puppet reset is needed to update baking results to current views
        m_view->resetPuppet();
    };

    auto crashCallback = [this] {
        m_progressDialog->raise();
        emit progress(tr("Baking process crashed, baking aborted."));
        emit finished();
    };

    m_connectionManager->setProgressCallback(std::move(progressCallback));
    m_connectionManager->setFinishedCallback(std::move(finishedCallback));
    m_nodeInstanceView->setCrashCallback(std::move(crashCallback));

    m_model->setNodeInstanceView(m_nodeInstanceView);

    // InternalIds are not guaranteed to match between normal model and our copy of it, so
    // we identify the View3D by its qml id.
    m_nodeInstanceView->view3DAction(View3DActionType::SetBakeLightsView3D, m_view3dId);
}

void BakeLights::apply()
{
    // Uninitialized QVariant stored instead of false to remove the aux property in that case
    m_dataModel->view3dNode().setAuxiliaryData(bakeLightsManualProperty,
                                               m_manualMode ? QVariant{true} : QVariant{});

    if (!m_manualMode)
        m_dataModel->apply();

    // Create folders for lightmaps if they do not exist
    PropertyName loadPrefixPropName{"loadPrefix"};
    const QList<ModelNode> bakedLightmapNodes = m_view->allModelNodesOfType(
        m_view->model()->qtQuick3DBakedLightmapMetaInfo());
    Utils::FilePath currentPath = DocumentManager::currentFilePath().absolutePath();
    QSet<Utils::FilePath> pathSet;
    for (const ModelNode &node : bakedLightmapNodes) {
        if (node.hasVariantProperty(loadPrefixPropName)) {
            QString prefix = node.variantProperty(loadPrefixPropName).value().toString();
            Utils::FilePath fp = Utils::FilePath::fromString(prefix);
            if (fp.isRelativePath()) {
                fp = currentPath.pathAppended(prefix);
                if (!fp.exists())
                    pathSet.insert(fp);
            }
        }
    }
    for (const Utils::FilePath &fp : std::as_const(pathSet))
        fp.createDir();
}

void BakeLights::rebake()
{
    QTimer::singleShot(0, this, [this] {
        cleanup();
        showSetupDialog();
    });
}

void BakeLights::exposeModelsAndLights(const QString &nodeId)
{
    ModelNode compNode = m_view->modelNodeForId(nodeId);
    if (!compNode.isValid() || !compNode.isComponent()) {
        return;
    }

    auto componentFilePath = ModelUtils::componentFilePath(compNode);
    if (componentFilePath.isEmpty()) {
        return;
    }

    RewriterView rewriter{m_view->externalDependencies(), RewriterView::Amend};
    ModelPointer compModel = QmlDesigner::Model::create("QtQuick/Item", 2, 1);
    const Utils::FilePath compFilePath = Utils::FilePath::fromString(componentFilePath);
    QByteArray src = compFilePath.fileContents().value();

    compModel->setFileUrl(QUrl::fromLocalFile(componentFilePath));

    auto textDocument = std::make_unique<QTextDocument>(QString::fromUtf8(src));
    auto modifier = std::make_unique<IndentingTextEditModifier>(
        textDocument.get(), QTextCursor{textDocument.get()});

    rewriter.setTextModifier(modifier.get());
    compModel->setRewriterView(&rewriter);

    if (!rewriter.rootModelNode().isValid() || !rewriter.errors().isEmpty())
        return;

    QString originalText = modifier->text();
    QStringList idList;

    rewriter.executeInTransaction(__FUNCTION__, [&]() {
        QList<ModelNode> nodes = rewriter.rootModelNode().allSubModelNodes();
        for (ModelNode &node : nodes) {
            if (node.metaInfo().isQtQuick3DModel() || node.metaInfo().isQtQuick3DLight()) {
                QString idStr = node.id();
                if (idStr.isEmpty()) {
                    const QString type = node.metaInfo().isQtQuick3DModel() ? "model" : "light";
                    idStr = compModel->generateNewId(type);
                    node.setIdWithoutRefactoring(idStr);
                }
                idList.append(idStr);
            }
        }
    });

    rewriter.executeInTransaction(__FUNCTION__, [&]() {
        for (const QString &id : std::as_const(idList)) {
            ModelNode node = rewriter.modelNodeForId(id);
            if (!node.isValid())
                continue;
            rewriter.rootModelNode().bindingProperty(id.toUtf8())
                .setDynamicTypeNameAndExpression("alias", id);
        }
    });

    rewriter.forceAmend();

    QString newText = modifier->text();
    if (newText != originalText) {
        QSaveFile saveFile(componentFilePath);
        if (saveFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
            saveFile.write(newText.toUtf8());
            saveFile.commit();
        } else {
            qWarning() << __FUNCTION__ << "Failed to save changes to:" << componentFilePath;
        }
    }

    QmlJS::ModelManagerInterface *modelManager = QmlJS::ModelManagerInterface::instance();
    QmlJS::Document::Ptr doc = rewriter.document()->ptr();
    modelManager->updateDocument(doc);

    m_view->model()->rewriterView()->forceAmend();

    compModel->setRewriterView({});

    // Rebake to relaunch setup dialog with updated properties
    rebake();
}

void BakeLights::showSetupDialog()
{
    if (!m_dataModel)
        m_dataModel = new BakeLightsDataModel(m_view);

    if (!m_dataModel->reset()) {
        m_manualMode = true;
    } else {
        auto data = m_dataModel->view3dNode().auxiliaryData(bakeLightsManualProperty);
        if (data)
            m_manualMode = data->toBool();
    }

    if (!m_setupDialog) {
        // Show non-modal progress dialog with cancel button
        QString path = qmlSourcesPath() + "/BakeLightsSetupDialog.qml";

        m_setupDialog = new QQuickView;
        m_setupDialog->setTitle(tr("Lights Baking Setup"));
        m_setupDialog->setResizeMode(QQuickView::SizeRootObjectToView);
        m_setupDialog->setMinimumSize({550, 200});
        m_setupDialog->setWidth(550);
        m_setupDialog->setHeight(400);
        m_setupDialog->setFlags(Qt::Dialog);
        m_setupDialog->setModality(Qt::ApplicationModal);
        m_setupDialog->engine()->addImportPath(propertyEditorResourcesPath() + "/imports");

        m_setupDialog->rootContext()->setContextProperties({
            {"rootView", QVariant::fromValue(this)},
            {"sceneId", QVariant::fromValue(m_view3dId)},
            {"bakeModel", QVariant::fromValue(m_dataModel.data())}
        });
        m_setupDialog->setSource(QUrl::fromLocalFile(path));
        m_setupDialog->installEventFilter(this);
    }
    m_setupDialog->show();
}

void BakeLights::showProgressDialog()
{
    if (!m_progressDialog) {
        // Show non-modal progress dialog with cancel button
        QString path = qmlSourcesPath() + "/BakeLightsProgressDialog.qml";

        m_progressDialog = new QQuickView;
        m_progressDialog->setTitle(tr("Lights Baking Progress"));
        m_progressDialog->setResizeMode(QQuickView::SizeRootObjectToView);
        m_progressDialog->setMinimumSize({150, 100});
        m_progressDialog->setWidth(800);
        m_progressDialog->setHeight(400);
        m_progressDialog->setFlags(Qt::Dialog);
        m_progressDialog->setModality(Qt::NonModal);
        m_progressDialog->engine()->addImportPath(propertyEditorResourcesPath() + "/imports");

        m_progressDialog->rootContext()->setContextProperties({
            {"rootView", QVariant::fromValue(this)},
            {"sceneId", QVariant::fromValue(m_view3dId)}
        });
        m_progressDialog->setSource(QUrl::fromLocalFile(path));
        m_progressDialog->installEventFilter(this);
    }
    m_progressDialog->show();
}

void BakeLights::cleanup()
{
    if (m_connectionManager) {
        m_connectionManager->setProgressCallback({});
        m_connectionManager->setFinishedCallback({});
        m_nodeInstanceView->setCrashCallback({});
    }

    if (m_model) {
        m_model->setNodeInstanceView({});
        m_model->setRewriterView({});
        m_model.reset();
    }

    delete m_setupDialog;
    delete m_progressDialog;
    delete m_rewriterView;
    delete m_nodeInstanceView;
    delete m_connectionManager;
    delete m_dataModel;

    m_manualMode = false;
}

void BakeLights::cancel()
{
    if (!m_setupDialog.isNull() && m_setupDialog->isVisible())
        m_setupDialog->close();
    if (!m_progressDialog.isNull() && m_progressDialog->isVisible())
        m_progressDialog->close();

    deleteLater();
}

bool BakeLights::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == m_progressDialog || obj == m_setupDialog) {
        if (event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Escape)
                cancel();
        } else if (event->type() == QEvent::Close) {
            cancel();
        }
    }

    return QObject::eventFilter(obj, event);
}

} // namespace QmlDesigner