summaryrefslogtreecommitdiffstats
path: root/editorlib/qml/EntityTree.qml
blob: b220bad355cab076a1993bdb0fdfecb60f92d417 (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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D Editor of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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.
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import QtQml.Models 2.2
import com.theqtcompany.SceneEditor3D 1.0
import QtQuick.Controls.Styles 1.4

Item {
    id: treeViewSplit

    property bool entityTreeViewEditing: entityTreeView.editing

    property alias view: entityTreeView
    property alias menu: addComponentMenu
    property bool treeviewPasting: false

    Keys.onDeletePressed: {
        if (editorScene.multiSelection) {
            // Handle multiselection removal
            editorScene.undoHandler.beginMacro("Remove selected")
            var removeList = editorScene.sceneModel.parentList(editorScene.multiSelectionList)
            for (var i = 0; i < removeList.length; ++i)
                editorScene.undoHandler.createRemoveEntityCommand(removeList[i])
            editorScene.undoHandler.endMacro()
            selectSceneRoot()
        } else {
            // Doublecheck that we don't try to remove the scene root
            if (entityTreeView.selection.currentIndex !== editorScene.sceneModel.sceneEntityIndex())
                editorScene.undoHandler.createRemoveEntityCommand(selectedEntityName)
        }
    }

    function focusTree() {
        entityTreeView.forceActiveFocus(Qt.MouseFocusReason)
    }

    function selectSceneRoot() {
        editorScene.clearMultiSelection()
        entityTreeView.selection.setCurrentIndex(
                    editorScene.sceneModel.sceneEntityIndex(),
                    ItemSelectionModel.SelectCurrent)
        entityTreeView.expand(entityTreeView.selection.currentIndex)
    }

    function addNewEntity(entityType, xPos, yPos) {
        var x = xPos ? xPos : -1
        var y = yPos ? yPos : -1
        entityTreeView.editing = false

        editorScene.clearMultiSelection()

        // Never allow inserting to root
        if (entityTreeView.selection.currentIndex.row === -1)
            selectSceneRoot()
        editorScene.undoHandler.createInsertEntityCommand(entityType, selectedEntityName,
                                                          editorScene.getWorldPosition(x, y))
        var newItemIndex = editorScene.sceneModel.lastInsertedIndex()

        entityTreeView.expand(entityTreeView.selection.currentIndex)
        entityTreeView.selection.setCurrentIndex(newItemIndex,
                                                 ItemSelectionModel.SelectCurrent)
        // Remove focus so activating editing will always force it on
        entityTreeView.focus = true
    }

    Component.onCompleted: selectSceneRoot()

    property int splitHeight: treeViewHeader.height + 130

    Layout.minimumHeight: treeViewHeader.height
    height: splitHeight

    ButtonViewHeader {
        id: treeViewHeader
        anchors.top: treeViewSplit.top
        headerText: qsTr("Scene") + editorScene.emptyString
        tooltip: qsTr("Show/Hide Scene View") + editorScene.emptyString

        onShowViewButtonPressed: {
            treeViewSplit.height = splitHeight
        }
        onHideViewButtonPressed: {
            splitHeight = treeViewSplit.height
            treeViewSplit.height = minimumHeaderHeight
        }
    }

    TreeView {
        id: entityTreeView
        anchors.top: treeViewHeader.bottom
        anchors.bottom: treeViewSplit.bottom
        width: parent.width
        visible: treeViewHeader.viewVisible
        model: editorScene.sceneModel
        selectionMode: SelectionMode.ExtendedSelection
        selection: ItemSelectionModel {
            model: editorScene.sceneModel
        }
        headerVisible: false
        alternatingRowColors: false
        backgroundVisible: false
        style: TreeViewStyle {
            textColor: mainwindow.textColor
            highlightedTextColor: mainwindow.textColor
            backgroundColor: mainwindow.paneBackgroundColor
            alternateBackgroundColor: mainwindow.paneBackgroundColor
            branchDelegate: Item {
                width: 10
                height: 10
                Image {
                    visible: styleData.column === 0 && styleData.hasChildren
                    anchors.fill: parent
                    anchors.verticalCenterOffset: 2
                    source: "images/arrow.png"
                    transform: Rotation {
                        origin.x: width / 2
                        origin.y: height / 2
                        angle: styleData.isExpanded ? 0 : -90
                    }
                }
            }
            handle: Rectangle {
                    implicitWidth: 20
                    implicitHeight: 30
                    color: "transparent"
                    Rectangle {
                        anchors.horizontalCenter: parent.horizontalCenter
                        implicitWidth: 14
                        anchors.bottom: parent.bottom
                        anchors.top: parent.top
                        color: mainwindow.selectionColor
                    }
            }
            scrollBarBackground: Rectangle {
                implicitWidth: 20
                implicitHeight: 30
                color: mainwindow.paneBackgroundColor
            }
            decrementControl: Image {
                width: 20
                source: "images/arrow.png"
                transform: Rotation {
                    origin.x: width / 2
                    origin.y: height / 2
                    angle: 180
                }
            }
            incrementControl: Image {
                width: 20
                source: "images/arrow.png"
            }
        }

        property bool editing: false
        property bool sceneRootSelected: true
        property bool cameraSelected: true
        property bool groupSelected: true

        onExpanded: {
            model.addExpandedItem(index)
        }
        onCollapsed: {
            model.removeExpandedItem(index)
        }

        Connections {
            target: editorScene.sceneModel
            onExpandItems: {
                for (var i = 0; i < items.length; i++)
                    entityTreeView.expand(items[i])
            }
            onSelectIndex: {
                entityTreeView.selection.setCurrentIndex(selectIndex,
                                                         ItemSelectionModel.SelectCurrent)
            }
        }

        itemDelegate: FocusScope {
            MouseArea {
                id: treeItemMouseArea
                anchors.fill: parent
                acceptedButtons: Qt.LeftButton

                property int dragPositionX
                property int dragPositionY
                drag.target: dragEntityItem

                function startDrag() {
                    var globalPos = mapToItem(applicationArea, mouseX, mouseY)
                    dragPositionX = globalPos.x
                    dragPositionY = globalPos.y
                    var meshType
                    var meshDragImage
                    var itemType = editorScene.sceneModel.editorSceneItemFromIndex(
                                styleData.index).itemType();
                    if (styleData.index === editorScene.sceneModel.sceneEntityIndex()
                            || itemType === EditorSceneItem.Camera
                            || itemType === EditorSceneItem.SceneLoader) {
                        // TODO: SceneLoader blocked because it currently crashes, and also
                        // TODO: because it cannot be safely deleted, which is needed when
                        // TODO: reparenting, which does duplicate+delete (QTBUG-52723).
                        meshType = EditorUtils.InvalidEntity
                        // TODO: make a proper "invalid drag" icon
                        meshDragImage = "images/cross.png"
                    } else if (itemType === EditorSceneItem.Light) {
                        meshType = EditorUtils.LightEntity
                        meshDragImage = "images/light_large.png"
                    } else if (itemType === EditorSceneItem.Group) {
                        meshType = EditorUtils.GroupEntity
                        meshDragImage = "images/group_large.png"
                    } else {
                        // Use cuboid type to indicate mesh
                        meshType = EditorUtils.CuboidEntity
                        meshDragImage = "images/mesh_large.png"
                    }

                    dragEntityItem.startDrag(treeItemMouseArea, meshDragImage,
                                             "changeParent", dragPositionX, dragPositionY,
                                             meshType, 0.3,
                                             editorScene.sceneModel.entityName(styleData.index))
                }

                onPressed: {
                    if (mouse.modifiers & Qt.ControlModifier) {
                        // Handle multiselection
                        // Prevent multiselecting scene root and toggling when singly selected
                        // entity is clicked again
                        if ((editorScene.multiSelection
                                || styleData.index !== entityTreeView.selection.currentIndex)
                                && styleData.index !== editorScene.sceneModel.sceneEntityIndex()) {
                            editorScene.toggleEntityMultiSelection(editorScene.sceneModel.entityName(
                                                                       styleData.index))
                        }
                    } else {
                        editorScene.clearMultiSelection()
                        entityTreeView.selection.setCurrentIndex(styleData.index,
                                                                 ItemSelectionModel.SelectCurrent)
                    }
                    entityTreeView.expand(styleData.index)
                }

                onPositionChanged: {
                    if (!dragEntityItem.Drag.active) {
                        startDrag()
                    } else {
                        var globalPos = mapToItem(applicationArea, mouseX, mouseY)
                        dragPositionX = globalPos.x
                        dragPositionY = globalPos.y
                        dragEntityItem.setPosition(dragPositionX, dragPositionY)
                        entityTreeView.expand(styleData.index)
                    }
                }

                onReleased: {
                    var dropResult = dragEntityItem.endDrag(true)
                }

                onCanceled: {
                    dragEntityItem.endDrag(false)
                }

                onDoubleClicked: {
                    entityTreeView.editing = true
                }
            }
            DropArea {
                anchors.fill: parent
                keys: [ "insertEntity", "changeParent" ]

                function isValidDropTarget(dropSource) {
                    // Dropping into camera is always invalid
                    // Camera can only be dropped into scene root
                    // Light can only be dropped to light or transform entity
                    // Group entities cannot be dropped under non-group entities
                    var itemType = editorScene.sceneModel.editorSceneItemFromIndex(
                                styleData.index).itemType();
                    var dropValid =
                            dropSource.drag.target.entityType !== EditorUtils.InvalidEntity
                            && itemType !== EditorSceneItem.Camera
                            && (styleData.index === editorScene.sceneModel.sceneEntityIndex()
                                || dropSource.drag.target.entityType !== EditorUtils.CameraEntity)
                            && (dropSource.drag.target.entityType !== EditorUtils.GroupEntity
                                || itemType === EditorSceneItem.Group
                                || styleData.index === editorScene.sceneModel.sceneEntityIndex())
                    if (dropValid && dropSource.drag.target.dragKey === "changeParent") {
                        dropValid = editorScene.sceneModel.canReparent(
                                    editorScene.sceneModel.editorSceneItemFromIndex(styleData.index),
                                    editorScene.sceneModel.editorSceneItemFromIndex(
                                        editorScene.sceneModel.getModelIndexByName(dropSource.drag.target.entityName)))
                    }

                    return dropValid
                }

                onDropped: {
                    if (isValidDropTarget(drop.source)) {
                        dragHighlight.visible = false
                        editorScene.clearMultiSelection()
                        entityTreeView.selection.setCurrentIndex(styleData.index,
                                                                 ItemSelectionModel.SelectCurrent)
                        if (drop.source.drag.target.dragKey === "changeParent") {
                            var entityName = editorScene.sceneModel.entityName(styleData.index)
                            editorScene.undoHandler.createReparentEntityCommand(
                                        entityName,
                                        drag.source.drag.target.entityName)
                            drop.action = Qt.MoveAction
                            entityTreeView.expand(
                                        editorScene.sceneModel.getModelIndexByName(entityName))
                        } else {
                            entityTree.addNewEntity(drag.source.drag.target.entityType)
                            drop.action = Qt.CopyAction
                        }
                        drop.accept()
                    }
                }
                onEntered: {
                    if (isValidDropTarget(drag.source)) {
                        dragHighlight.visible = true
                        if (drag.source.drag.target.dragKey === "changeParent")
                            dragEntityItem.opacity = 1
                    }
                }
                onExited: {
                    dragHighlight.visible = false
                    if (drag.source.drag.target.dragKey === "changeParent")
                        dragEntityItem.opacity = 0.3
                }

                Rectangle {
                    id: dragHighlight
                    anchors.fill: parent
                    color: mainwindow.selectionColor
                    visible: false
                }
            }

            Text {
                id: valueField
                anchors.verticalCenter: parent.verticalCenter
                color: mainwindow.textColor
                elide: styleData.elideMode
                text: styleData.value
                visible: !entityTreeView.editing || !styleData.selected
                anchors.fill: parent
                clip: true
            }
            Rectangle {
                id: renameField
                anchors.fill: parent
                color: mainwindow.paneBackgroundColor
                border.color: mainwindow.listHighlightColor
                visible: !valueField.visible
                TextInput {
                    id: renameTextiInput
                    anchors.fill: parent
                    clip: true
                    visible: !valueField.visible
                    selectByMouse: true
                    focus: true
                    color: mainwindow.textColor
                    selectionColor: mainwindow.selectionColor
                    selectedTextColor: mainwindow.textColor
                    font.family: mainwindow.labelFontFamily
                    font.weight: mainwindow.labelFontWeight
                    font.pixelSize: mainwindow.labelFontPixelSize
                    validator: RegExpValidator {
                        regExp: /^[A-Za-z_][A-Za-z0-9_ ]*$/
                    }

                    onVisibleChanged: {
                        if (visible) {
                            text = styleData.value
                            selectAll()
                            forceActiveFocus(Qt.MouseFocusReason)
                        }
                    }

                    onCursorVisibleChanged: {
                        if (!cursorVisible)
                            entityTreeView.editing = false
                    }

                    Keys.onReturnPressed: {
                        entityTreeView.editing = false
                        if (text !== model.name) {
                            editorScene.undoHandler.createRenameEntityCommand(selectedEntityName,
                                                                              text)
                        }
                        selectedEntityName = editorScene.sceneModel.entityName(
                                    entityTreeView.selection.currentIndex)
                    }
                }
            }
        }
        onDoubleClicked: {
            entityTreeView.editing = true
        }

        TableViewColumn {
            title: qsTr("Entities") + editorScene.emptyString
            role: "name"
            width: parent.width - 50
        }
        TableViewColumn {
            title: qsTr("Visibility") + editorScene.emptyString
            role: "visibility"
            width: 18
            delegate: VisiblePropertyInputField {
                id: visibleProperty
                component: editorScene.sceneModel.editorSceneItemFromIndex(styleData.index).entity()
                propertyName: "enabled"
                visibleOnImage: "images/visible_on.png"
                visibleOffImage: "images/visible_off.png"
                // The component is not shown for root item
                visible: (styleData.index !== editorScene.sceneModel.sceneEntityIndex()) ? true
                                                                                         : false

                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        entityName = editorScene.sceneModel.entityName(styleData.index)
                        if (entityEnabled)
                            entityEnabled = false
                        else
                            entityEnabled = true
                        visibleProperty.visibleImageClicked()
                    }
                }
            }
        }

        Connections {
            target: entityTreeView.selection
            onCurrentIndexChanged: {
                entityTreeView.editing = false
                // If there is no current item selected for some reason, fall back to scene root,
                // except when dealing with multiselection, during which currentIndex can become -1 temporarily.
                if (entityTreeView.selection.currentIndex.row === -1) {
                    selectedEntityName = ""
                    if (!editorScene.multiSelection)
                        selectSceneRoot()
                } else {
                    entityTreeView.sceneRootSelected =
                            (editorScene.sceneModel.sceneEntityIndex() === entityTreeView.selection.currentIndex)
                    selectedEntity = editorScene.sceneModel.editorSceneItemFromIndex(entityTreeView.selection.currentIndex)
                    if (selectedEntity) {
                        componentPropertiesView.model = selectedEntity.componentsModel
                        entityTreeView.cameraSelected =
                                selectedEntity.itemType() === EditorSceneItem.Camera
                        entityTreeView.groupSelected =
                                selectedEntity.itemType() === EditorSceneItem.Group
                        selectedEntityName = editorScene.sceneModel.entityName(
                                    entityTreeView.selection.currentIndex)
                    } else {
                        // Should never get here
                        selectedEntityName = ""
                        selectSceneRoot()
                    }
                    if (!editorScene.multiSelection)
                        editorScene.selection = selectedEntity.entity()
                }
            }
        }

        ComponentMenu {
            id: addComponentMenu
            onAboutToHide: {
                treeMouseArea.hoverEnabled = true
            }
        }

        MouseArea {
            id: treeMouseArea
            anchors.fill: parent
            acceptedButtons: Qt.RightButton
            hoverEnabled: true
            onClicked: {
                // Prevent menu popup if no entity is selected
                if (componentPropertiesView.model !== undefined) {
                    hoverEnabled = false
                    addComponentMenu.popup()
                }
            }
            onContainsMouseChanged: {
                if (treeMouseArea.hoverEnabled)
                    treeviewPasting = containsMouse
            }
        }
    }
}