aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/navigator/choosefrompropertylistdialog.cpp
blob: b49cb8762a151f2790920a2fd4364c4a9f61f00c (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
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "choosefrompropertylistdialog.h"
#include "nodemetainfo.h"
#include "ui_choosefrompropertylistdialog.h"

namespace QmlDesigner {

// This will filter and return possible properties that the given type can be bound to
ChooseFromPropertyListFilter::ChooseFromPropertyListFilter(const NodeMetaInfo &insertInfo,
                                                           const NodeMetaInfo &parentInfo,
                                                           bool breakOnFirst)
{
    // TODO: Metainfo based matching system (QDS-6240)

    // Fall back to a hardcoded list of supported cases:
    // Texture
    //  -> DefaultMaterial
    //  -> PrincipledMaterial
    //  -> SpriteParticle3D
    //  -> TextureInput
    //  -> SceneEnvironment
    // Effect
    //  -> SceneEnvironment
    // Shader, Command, Buffer
    //  -> Pass
    // InstanceListEntry
    //  -> InstanceList
    // Pass
    //  -> Effect
    // Particle3D
    //  -> ParticleEmitter3D
    // ParticleAbstractShape3D
    //  -> ParticleEmitter3D
    //  -> Attractor3D
    // Material
    //  -> Model

    const TypeName textureType = "QtQuick3D.Texture";
    if (insertInfo.isSubclassOf(textureType)) {
        const TypeName textureTypeCpp = "<cpp>.QQuick3DTexture";
        if (parentInfo.isSubclassOf("QtQuick3D.DefaultMaterial")
            || parentInfo.isSubclassOf("QtQuick3D.PrincipledMaterial")) {
            // All texture properties are valid targets
            for (const auto &property : parentInfo.properties()) {
                const TypeName &propType = property.propertyType().typeName();
                if (propType == textureType || propType == textureTypeCpp) {
                    propertyList.append(QString::fromUtf8(property.name()));
                    if (breakOnFirst)
                        return;
                }
            }
        } else if (parentInfo.isSubclassOf("QtQuick3D.Particles3D.SpriteParticle3D")) {
            propertyList.append("sprite");
        } else if (parentInfo.isSubclassOf("QtQuick3D.TextureInput")) {
            propertyList.append("texture");
        } else if (parentInfo.isSubclassOf("QtQuick3D.SceneEnvironment")) {
            propertyList.append("lightProbe");
        }
    } else if (insertInfo.isSubclassOf("QtQuick3D.Effect")) {
        if (parentInfo.isSubclassOf("QtQuick3D.SceneEnvironment"))
            propertyList.append("effects");
    } else if (insertInfo.isSubclassOf("QtQuick3D.Shader")) {
        if (parentInfo.isSubclassOf("QtQuick3D.Pass"))
            propertyList.append("shaders");
    } else if (insertInfo.isSubclassOf("QtQuick3D.Command")) {
        if (parentInfo.isSubclassOf("QtQuick3D.Pass"))
            propertyList.append("commands");
    } else if (insertInfo.isSubclassOf("QtQuick3D.Buffer")) {
        if (parentInfo.isSubclassOf("QtQuick3D.Pass"))
            propertyList.append("output");
    } else if (insertInfo.isSubclassOf("QtQuick3D.InstanceListEntry")) {
        if (parentInfo.isSubclassOf("QtQuick3D.InstanceList"))
            propertyList.append("instances");
    } else if (insertInfo.isSubclassOf("QtQuick3D.Pass")) {
        if (parentInfo.isSubclassOf("QtQuick3D.Effect"))
            propertyList.append("passes");
    } else if (insertInfo.isSubclassOf("QtQuick3D.Particles3D.Particle3D")) {
        if (parentInfo.isSubclassOf("QtQuick3D.Particles3D.ParticleEmitter3D"))
            propertyList.append("particle");
    } else if (insertInfo.isSubclassOf("QQuick3DParticleAbstractShape")) {
        if (parentInfo.isSubclassOf("QtQuick3D.Particles3D.ParticleEmitter3D")
                || parentInfo.isSubclassOf("QtQuick3D.Particles3D.Attractor3D"))
            propertyList.append("shape");
    } else if (insertInfo.isSubclassOf("QtQuick3D.Material")) {
        if (parentInfo.isSubclassOf("QtQuick3D.Particles3D.Model"))
            propertyList.append("materials");
    }
}

// This dialog displays specified properties and allows the user to choose one
ChooseFromPropertyListDialog::ChooseFromPropertyListDialog(const QStringList &propNames,
                                                           QWidget *parent)
    : QDialog(parent)
    , m_ui(new Ui::ChooseFromPropertyListDialog)
{
    if (propNames.count() == 1) {
       m_selectedProperty = propNames.first().toLatin1();
       m_isSoloProperty = true;
       return;
    }
    m_ui->setupUi(this);
    setWindowTitle(tr("Select property"));
    m_ui->label->setText(tr("Bind to property:"));
    m_ui->label->setToolTip(tr("Binds this component to the parent's selected property."));
    setFixedSize(size());

    connect(m_ui->listProps, &QListWidget::itemClicked, this, [this](QListWidgetItem *item) {
        m_selectedProperty = item->isSelected() ? item->data(Qt::DisplayRole).toByteArray() : QByteArray();
    });

    connect(m_ui->listProps,
            &QListWidget::itemDoubleClicked,
            this,
            [this]([[maybe_unused]] QListWidgetItem *item) { QDialog::accept(); });

    fillList(propNames);
}

ChooseFromPropertyListDialog::~ChooseFromPropertyListDialog()
{
    delete m_ui;
}

TypeName ChooseFromPropertyListDialog::selectedProperty() const
{
    return m_selectedProperty;
}

// Create dialog for selecting any property matching newNode type
// Subclass type matches are also valid
ChooseFromPropertyListDialog *ChooseFromPropertyListDialog::createIfNeeded(
        const ModelNode &targetNode, const ModelNode &newNode, QWidget *parent)
{
    const NodeMetaInfo info = newNode.metaInfo();
    const NodeMetaInfo targetInfo = targetNode.metaInfo();
    ChooseFromPropertyListFilter *filter = new ChooseFromPropertyListFilter(info, targetInfo);

    if (!filter->propertyList.isEmpty())
        return new ChooseFromPropertyListDialog(filter->propertyList, parent);

    return nullptr;
}

// Create dialog for selecting writable properties of exact property type
ChooseFromPropertyListDialog *ChooseFromPropertyListDialog::createIfNeeded(
    const ModelNode &targetNode, const NodeMetaInfo &propertyType, QWidget *parent)
{
    const NodeMetaInfo metaInfo = targetNode.metaInfo();
    QStringList matchingNames;
    for (const auto &property : metaInfo.properties()) {
        if (property.propertyType() == propertyType && property.isWritable())
            matchingNames.append(QString::fromUtf8(property.name()));
    }

    if (!matchingNames.isEmpty())
        return new ChooseFromPropertyListDialog(matchingNames, parent);

    return nullptr;
}

void ChooseFromPropertyListDialog::fillList(const QStringList &propNames)
{
    if (propNames.isEmpty())
        return;

    QString defaultProp = propNames.first();
    QStringList sortedNames = propNames;
    sortedNames.sort();
    for (const auto &propName : qAsConst(sortedNames)) {
        QListWidgetItem *newItem = new QListWidgetItem(propName);
        m_ui->listProps->addItem(newItem);
    }

    // Select the default prop
    m_ui->listProps->setCurrentRow(sortedNames.indexOf(defaultProp));
    m_selectedProperty = defaultProp.toLatin1();
}

}