aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/connectioneditor/dynamicpropertiesmodel.cpp
blob: 5cf90ca6fd8bbde34429eb4cf6418a2e4c204de5 (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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "dynamicpropertiesmodel.h"
#include "dynamicpropertiesitem.h"
#include "connectioneditorutils.h"

#include <abstractproperty.h>
#include <bindingproperty.h>
#include <modelfwd.h>
#include <rewritertransaction.h>
#include <rewritingexception.h>
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <variantproperty.h>
#include <qmlchangeset.h>
#include <qmldesignerconstants.h>
#include <qmldesignerplugin.h>
#include <qmlobjectnode.h>
#include <qmltimeline.h>

#include <optional>

namespace QmlDesigner {

DynamicPropertiesModel::DynamicPropertiesModel(bool exSelection, AbstractView *parent)
    : QStandardItemModel(parent)
    , m_view(parent)
    , m_delegate(new DynamicPropertiesModelBackendDelegate(this))
    , m_explicitSelection(exSelection)
{
    setHorizontalHeaderLabels(DynamicPropertiesItem::headerLabels());
}

AbstractView *DynamicPropertiesModel::view() const
{
    return m_view;
}

DynamicPropertiesModelBackendDelegate *DynamicPropertiesModel::delegate() const
{
    return m_delegate;
}

int DynamicPropertiesModel::currentIndex() const
{
    return m_currentIndex;
}

AbstractProperty DynamicPropertiesModel::currentProperty() const
{
    return propertyForRow(m_currentIndex);
}

void DynamicPropertiesModel::add()
{
    QmlDesignerPlugin::emitUsageStatistics(Constants::EVENT_PROPERTY_ADDED);

    if (const QList<ModelNode> nodes = selectedNodes(); nodes.size() == 1) {
        const ModelNode modelNode = nodes.constFirst();
        if (!modelNode.isValid())
            return;

        try {
            PropertyName newName = uniquePropertyName("property", modelNode);
            VariantProperty newProperty = modelNode.variantProperty(newName);
            newProperty.setDynamicTypeNameAndValue("string", "This is a string");
        } catch (RewritingException &e) {
            showErrorMessage(e.description());
        }
    } else {
        qWarning() << "DynamicPropertiesModel::add not one node selected";
    }
}

void DynamicPropertiesModel::remove(int row)
{
    m_view->executeInTransaction(__FUNCTION__, [this, row]() {
        if (DynamicPropertiesItem *item = itemForRow(row)) {
            PropertyName name = item->propertyName();
            if (ModelNode node = modelNodeForItem(item); node.isValid()) {
                node.removeProperty(name);

                QmlObjectNode objectNode = QmlObjectNode(node);
                const auto stateOperations = objectNode.allAffectingStatesOperations();
                for (const QmlModelStateOperation &stateOperation : stateOperations) {
                    if (stateOperation.modelNode().hasProperty(name))
                        stateOperation.modelNode().removeProperty(name);
                }
                for (auto &timelineNode : objectNode.allTimelines()) {
                    QmlTimeline timeline(timelineNode);
                    timeline.removeKeyframesForTargetAndProperty(node, name);
                }
            }
        }
    });
    reset();
}

void DynamicPropertiesModel::reset(const QList<ModelNode> &modelNodes)
{
    AbstractProperty current = currentProperty();

    clear();

    if (!modelNodes.isEmpty()) {
        for (const ModelNode &modelNode : modelNodes)
            addModelNode(modelNode);
        return;
    }

    if (m_view->isAttached()) {
        const QList<ModelNode> selected = selectedNodes();
        for (const ModelNode &modelNode : selected)
            addModelNode(modelNode);
    }

    setCurrentProperty(current);
}

void DynamicPropertiesModel::setCurrentIndex(int i)
{
    if (m_currentIndex != i) {
        m_currentIndex = i;
        emit currentIndexChanged();
    }
    // Property properties may have changed.
    m_delegate->update(currentProperty());
}

void DynamicPropertiesModel::setCurrentProperty(const AbstractProperty &property)
{
    if (!property.isValid())
        return;

    if (auto index = findRow(property.parentModelNode().internalId(), property.name()))
        setCurrentIndex(*index);
}

void DynamicPropertiesModel::setCurrent(int internalId, const PropertyName &name)
{
    if (internalId < 0)
        return;

    if (auto index = findRow(internalId, name))
        setCurrentIndex(*index);
}

void DynamicPropertiesModel::updateItem(const AbstractProperty &property)
{
    if (!property.isDynamic())
        return;

    if (auto *item = itemForProperty(property)) {
        item->updateProperty(property);
    } else {
        ModelNode node = property.parentModelNode();
        if (selectedNodes().contains(node)) {
            addProperty(property);
            setCurrentProperty(property);
        }
    }
}

void DynamicPropertiesModel::removeItem(const AbstractProperty &property)
{
    if (!property.isValid())
        return;

    AbstractProperty current = currentProperty();

    if (auto index = findRow(property.parentModelNode().internalId(), property.name()))
        static_cast<void>(removeRow(*index));

    setCurrentProperty(current);
}

QHash<int, QByteArray> DynamicPropertiesModel::roleNames() const
{
    return DynamicPropertiesItem::roleNames();
}

AbstractProperty DynamicPropertiesModel::propertyForRow(int row) const
{
    if (!m_view)
        return {};

    if (!m_view->isAttached())
        return {};

    if (auto *item = itemForRow(row)) {
        int internalId = item->internalId();
        if (ModelNode node = m_view->modelNodeForInternalId(internalId); node.isValid())
            return node.property(item->propertyName());
    }
    return {};
}

std::optional<int> DynamicPropertiesModel::findRow(int nodeId, const PropertyName &name) const
{
    for (int i = 0; i < rowCount(); ++i) {
        if (auto *item = itemForRow(i)) {
            if (item->propertyName() == name && item->internalId() == nodeId)
                return i;
        }
    }
    return std::nullopt;
}

DynamicPropertiesItem *DynamicPropertiesModel::itemForRow(int row) const
{
    if (QModelIndex idx = index(row, 0); idx.isValid())
        return dynamic_cast<DynamicPropertiesItem *>(itemFromIndex(idx));
    return nullptr;
}

DynamicPropertiesItem *DynamicPropertiesModel::itemForProperty(const AbstractProperty &property) const
{
    if (!property.isValid())
        return nullptr;

    if (auto row = findRow(property.parentModelNode().internalId(), property.name()))
        return itemForRow(*row);

    return nullptr;
}

ModelNode DynamicPropertiesModel::modelNodeForItem(DynamicPropertiesItem *item)
{
    if (!m_view->isAttached())
        return {};

    return m_view->modelNodeForInternalId(item->internalId());
}

void DynamicPropertiesModel::addModelNode(const ModelNode &node)
{
    if (!node.isValid())
        return;

    for (const AbstractProperty &property : dynamicPropertiesFromNode(node))
        addProperty(property);
}

void DynamicPropertiesModel::addProperty(const AbstractProperty &property)
{
    const PropertyName name = property.name();
    for (int i = 0; i < rowCount(); ++i) {
        if (auto *item = itemForRow(i)) {
            if (item->propertyName() > name) {
                insertRow(i, new DynamicPropertiesItem(property));
                return;
            }
        }
    }
    appendRow(new DynamicPropertiesItem(property));
}

void DynamicPropertiesModel::commitPropertyType(int row, const TypeName &type)
{
    AbstractProperty property = propertyForRow(row);
    if (!property.isValid())
        return;

    ModelNode node = property.parentModelNode();
    RewriterTransaction transaction = m_view->beginRewriterTransaction(__FUNCTION__);
    try {
        if (property.isBindingProperty()) {
            BindingProperty binding = property.toBindingProperty();
            const QString expression = binding.expression();
            binding.parentModelNode().removeProperty(binding.name());
            binding.setDynamicTypeNameAndExpression(type, expression);
        } else if (property.isVariantProperty()) {
            VariantProperty variant = property.toVariantProperty();
            QVariant val = typeConvertVariant(variant.value(), type);
            variant.parentModelNode().removeProperty(variant.name());
            variant.setDynamicTypeNameAndValue(type, val);
        }
        transaction.commit();

    } catch (Exception &e) {
        showErrorMessage(e.description());
    }
}

void DynamicPropertiesModel::commitPropertyName(int row, const PropertyName &name)
{
    AbstractProperty property = propertyForRow(row);
    if (!property.isValid())
        return;

    ModelNode node = property.parentModelNode();
    if (property.isBindingProperty()) {
        BindingProperty binding = property.toBindingProperty();
        m_view->executeInTransaction(__FUNCTION__, [binding, name, &node]() {
            const QString expression = binding.expression();
            const TypeName type = binding.dynamicTypeName();
            node.removeProperty(binding.name());
            node.bindingProperty(name).setDynamicTypeNameAndExpression(type, expression);
        });

    } else if (property.isVariantProperty()) {
        VariantProperty variant = property.toVariantProperty();
        m_view->executeInTransaction(__FUNCTION__, [variant, name, &node]() {
            const QVariant value = variant.value();
            const TypeName type = variant.dynamicTypeName();
            node.removeProperty(variant.name());
            node.variantProperty(name).setDynamicTypeNameAndValue(type, value);
        });
    }
}

void DynamicPropertiesModel::commitPropertyValue(int row, const QVariant &value)
{
    AbstractProperty property = propertyForRow(row);
    if (!property.isValid())
        return;

    RewriterTransaction transaction = m_view->beginRewriterTransaction(__FUNCTION__);
    try {
        bool isBindingValue = isBindingExpression(value);
        if (property.isBindingProperty()) {
            BindingProperty binding = property.toBindingProperty();
            if (!isBindingValue) {
                convertBindingToVariantProperty(binding, value);
            } else {
                const QString expression = value.toString();
                const TypeName typeName = binding.dynamicTypeName();
                binding.setDynamicTypeNameAndExpression(typeName, expression);
            }
        } else if (property.isVariantProperty()) {
            VariantProperty variant = property.toVariantProperty();
            if (isBindingValue)
                convertVariantToBindingProperty(variant, value);
            else
                variant.setDynamicTypeNameAndValue(variant.dynamicTypeName(), value);
        }
        transaction.commit();
    } catch (Exception &e) {
        showErrorMessage(e.description());
    }
}

void DynamicPropertiesModel::dispatchPropertyChanges(const AbstractProperty &abstractProperty)
{
    if (abstractProperty.parentModelNode().simplifiedTypeName() == "PropertyChanges") {
        QmlPropertyChanges changes(abstractProperty.parentModelNode());
        if (changes.target().isValid()) {
            const ModelNode target = changes.target();
            const PropertyName propertyName = abstractProperty.name();
            const AbstractProperty targetProperty = target.variantProperty(propertyName);
            if (target.hasProperty(propertyName) && targetProperty.isDynamic())
                updateItem(targetProperty);
        }
    }
}

const QList<ModelNode> DynamicPropertiesModel::selectedNodes() const
{
    if (m_explicitSelection)
        return m_selectedNodes;

    return m_view->selectedModelNodes();
}

const ModelNode DynamicPropertiesModel::singleSelectedNode() const
{
    if (m_explicitSelection)
        return m_selectedNodes.first();

    return m_view->singleSelectedModelNode();
}

void DynamicPropertiesModel::setSelectedNode(const ModelNode &node)
{
    QTC_ASSERT(m_explicitSelection, return);

    if (!node.isValid())
        return;

    m_selectedNodes.clear();
    m_selectedNodes.append(node);
    reset();
}

DynamicPropertiesModelBackendDelegate::DynamicPropertiesModelBackendDelegate(DynamicPropertiesModel *parent)
    : QObject(parent)
    , m_internalNodeId(std::nullopt)
{
    m_type.setModel({"int", "bool", "var", "real", "string", "url", "color"});
    connect(&m_type, &StudioQmlComboBoxBackend::activated, this, [this] { handleTypeChanged(); });
    connect(&m_name, &StudioQmlTextBackend::activated, this, [this] { handleNameChanged(); });
    connect(&m_value, &StudioQmlTextBackend::activated, this, [this] { handleValueChanged(); });
}

void DynamicPropertiesModelBackendDelegate::update(const AbstractProperty &property)
{
    if (!property.isValid())
        return;

    m_internalNodeId = property.parentModelNode().internalId();
    m_type.setCurrentText(QString::fromUtf8(property.dynamicTypeName()));
    m_name.setText(QString::fromUtf8(property.name()));

    if (property.isVariantProperty())
        m_value.setText(property.toVariantProperty().value().toString());
    else if (property.isBindingProperty())
        m_value.setText(property.toBindingProperty().expression());

    m_targetNode = property.parentModelNode().id();
    emit targetNodeChanged();
}

void DynamicPropertiesModelBackendDelegate::handleTypeChanged()
{
    DynamicPropertiesModel *model = qobject_cast<DynamicPropertiesModel *>(parent());
    QTC_ASSERT(model, return);

    const PropertyName name = m_name.text().toUtf8();

    int current = model->currentIndex();
    const TypeName type = m_type.currentText().toUtf8();
    model->commitPropertyType(current, type);

    // The order might have changed!
    model->setCurrent(m_internalNodeId.value_or(-1), name);
}

void DynamicPropertiesModelBackendDelegate::handleNameChanged()
{
    DynamicPropertiesModel *model = qobject_cast<DynamicPropertiesModel *>(parent());
    QTC_ASSERT(model, return);

    const PropertyName name = m_name.text().toUtf8();
    QTC_ASSERT(!name.isEmpty(), return);

    int current = model->currentIndex();
    model->commitPropertyName(current, name);

    // The order might have changed!
    model->setCurrent(m_internalNodeId.value_or(-1), name);
}

// TODO: Maybe replace with utils typeConvertVariant?
QVariant valueFromText(const QString &value, const QString &type)
{
    if (isBindingExpression(value))
        return value;

    if (type == "real" || type == "int")
        return value.toFloat();

    if (type == "bool")
        return value == "true";

    return value;
}

void DynamicPropertiesModelBackendDelegate::handleValueChanged()
{
    DynamicPropertiesModel *model = qobject_cast<DynamicPropertiesModel *>(parent());
    QTC_ASSERT(model, return);

    int current = model->currentIndex();
    QVariant value = valueFromText(m_value.text(), m_type.currentText());
    model->commitPropertyValue(current, value);
}

QString DynamicPropertiesModelBackendDelegate::targetNode() const
{
    return m_targetNode;
}

StudioQmlComboBoxBackend *DynamicPropertiesModelBackendDelegate::type()
{
    return &m_type;
}

StudioQmlTextBackend *DynamicPropertiesModelBackendDelegate::name()
{
    return &m_name;
}

StudioQmlTextBackend *DynamicPropertiesModelBackendDelegate::value()
{
    return &m_value;
}

} // namespace QmlDesigner