summaryrefslogtreecommitdiffstats
path: root/editorlib/qml/EditorContent.qml
blob: 35b620cbf73907fd0ca9e755f5eed811c8bcd8d5 (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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/****************************************************************************
**
** 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.2
import QtQuick.Dialogs 1.2
import QtQml.Models 2.2
import QtQml 2.2
import Qt3D.Core 2.0
import Qt.labs.settings 1.0
import com.theqtcompany.SceneEditor3D 1.0

Item {
    id: editorContent
    anchors.fill: parent

    property var selectedEntity: null
    property string selectedEntityName: ""
    property url saveFileUrl: ""
    property url defaultFolder: "file:///"
    property url saveFolder: "file:///"
    property url textureFolder: "file:///"
    property url importFolder: "file:///"
    property string saveFileTitleAddition: {
        if (saveFileUrl != "")
            " - " + saveFileUrl.toString().substring(saveFileUrl.toString().lastIndexOf("/") + 1)
        else
            saveFileUrl.toString()
    }

    property int currentHelperPlane: 1
    property alias selectedEntityType: generalPropertyView.entityType

    property int mousePosY: -1
    property int mousePosX: -1

    property color textColor: "#ffffff"
    property color disabledTextColor: "#a0a1a2"
    property color selectionColor: "#43adee"
    property color listHighlightColor: "#585a5c"
    property color paneBackgroundColor: "#2e2f30"
    property color paneColor: "#373839"
    property color viewBorderColor: "#000000"
    property color itemBackgroundColor: "#46484a"
    property color itemColor: "#cccccc"
    property color iconHighlightColor: "#26282a"
    property string labelFontFamily: "Open Sans"
    property int labelFontWeight: Font.Normal
    property int labelFontPixelSize: 12
    property int maximumControlWidth: 200
    property int controlMargin: 4

    property bool transformViewVisible: true
    property bool materialViewVisible: true
    property bool meshViewVisible: true
    property bool lightViewVisible: true
    property bool cameraViewVisible: true

    property real qlcControlHeight: 28

    property string systemLanguage: editorScene.language
    property alias editorScene: editorScene

    function checkUnsavedChanges() {
        if (!editorScene.undoHandler.isClean()) {
            closingDialog.open()
            return false
        }
        return true
    }

    function fileLoad() {
        if (!editorScene.undoHandler.isClean()) {
            saveUnsavedDialog.newFile = false
            saveUnsavedDialog.open()
        } else {
            loadFileDialog.folder = saveFolder
            loadFileDialog.open()
        }
    }

    function loadScene(fileUrl, folder) {
        if (editorScene.loadScene(fileUrl)) {
            entityTree.selectSceneRoot()
            editorContent.saveFolder = folder
            editorContent.saveFileUrl = fileUrl
        }
    }

    function fileSave() {
        if (saveFileUrl == "") {
            saveFileDialog.folder = saveFolder
            saveFileDialog.open()
            // No previous autosave file, no need to delete anything
        } else {
            editorScene.saveScene(saveFileUrl)
            editorScene.deleteScene(saveFileUrl, true)
        }
    }

    function fileSaveAs() {
        if (editorContent.saveFileUrl != "")
            editorScene.deleteScene(editorContent.saveFileUrl, true)
        saveFileDialog.folder = saveFolder
        saveFileDialog.open()
    }

    function undo() {
        editorScene.undoHandler.undo()
    }

    function redo() {
        editorScene.undoHandler.redo()
    }

    function fileNew() {
        if (!editorScene.undoHandler.isClean()) {
            saveUnsavedDialog.newFile = true
            saveUnsavedDialog.open()
        } else {
            editorScene.resetScene()
            showNormalYPlane()
            saveFileUrl = ""
        }
    }

    function showNormalXPlane() {
        editorScene.helperPlane.enabled = true
        editorScene.helperPlaneTransform.rotation =
                editorScene.helperPlaneTransform.fromAxisAndAngle(0, 1, 0, 90)
        currentHelperPlane = 0
    }

    function showNormalYPlane() {
        editorScene.helperPlane.enabled = true
        editorScene.helperPlaneTransform.rotation =
                editorScene.helperPlaneTransform.fromAxisAndAngle(1, 0, 0, 90)
        currentHelperPlane = 1
    }

    function showNormalZPlane() {
        editorScene.helperPlane.enabled = true
        editorScene.helperPlaneTransform.rotation =
                editorScene.helperPlaneTransform.fromAxisAndAngle(0, 0, 1, 90)
        currentHelperPlane = 2
    }

    function hideHelperPlane() {
        editorScene.helperPlane.enabled = false
        currentHelperPlane = 3
    }

    function copyEntity(entityName) {
        if (entityName !== editorScene.sceneRootName()) {
            // Disable possible previous cut operation
            if (editorScene.clipboardOperation === EditorScene.ClipboardCut) {
                var index = editorScene.sceneModel.getModelIndexByName(
                            editorScene.clipboardContent)
                var sceneItem = editorScene.sceneModel.editorSceneItemFromIndex(index)
                sceneItem.entity().enabled = true
            }
            editorScene.clipboardContent = entityName
            editorScene.clipboardOperation = EditorScene.ClipboardCopy
            // TODO: When to stop reading mouse movements when copy-pasting?
            // TODO: When any other operation is done?
        }
    }

    function cutEntity(entityName, entity) {
        if (entityName !== editorScene.sceneRootName()) {
            editorScene.clipboardContent = entityName
            editorScene.clipboardOperation = EditorScene.ClipboardCut
            entity.entity().enabled = false
        }
    }

    function pasteEntity() {
        if (editorScene.clipboardContent.length) {
            var parentName = ""
            if (entityTree.treeviewPasting) {
                parentName = selectedEntityName
                // Prevent pasting an entity under itself in tree
                if (!editorScene.sceneModel.canReparent(
                            editorScene.sceneModel.editorSceneItemFromIndex(
                                editorScene.sceneModel.getModelIndexByName(
                                    selectedEntityName)),
                            editorScene.sceneModel.editorSceneItemFromIndex(
                                editorScene.sceneModel.getModelIndexByName(
                                    editorScene.clipboardContent)), true)) {
                    return
                }
            }
            // When pasting to tree, world position is not used, and parent entity name is passed
            editorScene.undoHandler.createPasteEntityCommand(editorScene.getWorldPosition(
                                                                 mousePosX, mousePosY), parentName)
            if (editorScene.clipboardOperation === EditorScene.ClipboardCut)
                editorScene.clipboardOperation = EditorScene.ClipboardNone
        }
    }

    function resetCameraToDefault() {
        editorScene.freeView = true
        editorScene.resetFreeViewCamera()
    }

    function changeCameraPosition() {
        cameraPositionMenu.popup()
    }

    CameraPositionMenu {
        id: cameraPositionMenu
    }

    Settings {
        // Save view panel sizes
        // Use detailed category name, as plugin saves settings under QtCreator application
        category: "Qt 3D SceneEditor Content Geometry"
        property alias leftPaneWidth: entityLibrary.width
        property alias rightPaneWidth: propertyPane.width
        property alias entityTreeHeight: entityTree.height
    }

    Settings {
        category: "Qt 3D SceneEditor Folders"
        property alias saveFolder: editorContent.saveFolder
        property alias textureFolder: editorContent.textureFolder
        property alias importFolder: editorContent.importFolder
        property alias defaultFolder: editorContent.defaultFolder
    }

    EditorToolbar {
        id: editorToolbar
    }
    Item {
        anchors.top: editorToolbar.bottom
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.right: parent.right

        Item {
            // This item is used to map global mouse position
            id: applicationArea
            anchors.fill: parent
        }

        DragEntity {
            id: dragEntityItem
        }


        FileDialog {
            id: loadFileDialog
            selectMultiple: false
            selectExisting: true
            title: qsTr("Load Scene") + editorScene.emptyString
            nameFilters: [qsTr("Qt3D Scenes (*.qt3d.qrc)") + editorScene.emptyString]
            onAccepted: {
                editorContent.loadScene(fileUrl, folder)
            }
        }

        FileDialog {
            id: saveFileDialog
            selectMultiple: false
            selectExisting: false
            property bool exiting: false
            title: qsTr("Save Scene") + editorScene.emptyString
            nameFilters: [qsTr("Qt3D Scenes (*.qt3d.qrc)") + editorScene.emptyString]
            onAccepted: {
                editorScene.saveScene(fileUrl)
                editorContent.saveFolder = folder
                editorContent.saveFileUrl = fileUrl
                if (exiting)
                    Qt.quit()
            }
        }

        FileDialog {
            id: importEntityDialog
            selectMultiple: false
            selectExisting: true
            title: qsTr("Import Entity") + editorScene.emptyString
            nameFilters: [qsTr("All files (*)") + editorScene.emptyString]
            onAccepted: {
                editorScene.undoHandler.createImportEntityCommand(fileUrl)
                editorContent.importFolder = folder
            }
        }


        Shortcut {
            id: copyShortcut
            sequence: StandardKey.Copy
            onActivated: {
                // Prevent copying multiselection (for now, at least)
                if (!editorScene.multiSelection)
                    editorContent.copyEntity(editorContent.selectedEntityName)
            }
        }

        Shortcut {
            id: cutShortcut
            sequence: StandardKey.Cut
            onActivated: {
                // Prevent cutting multiselection (for now, at least)
                if (!editorScene.multiSelection)
                    editorContent.cutEntity(editorContent.selectedEntityName, editorContent.selectedEntity)
            }
        }

        Shortcut {
            id: pasteShortcut
            sequence: StandardKey.Paste
            onActivated: {
                editorContent.pasteEntity()
            }
        }

        EditorScene {
            id: editorScene
            viewport: editorViewport
            freeView: true

            onSelectionChanged: {
                restoreSelection(selection)
            }

            onMultiSelectionListChanged: {
                restoreMultiSelection(editorScene.multiSelectionList)
            }

            onErrorChanged: {
                notification.title = qsTr("Error") + editorScene.emptyString
                notification.text = error
                notification.open()
            }

            onMouseRightButtonReleasedWithoutDragging: {
                entityTree.menu.popup()
            }

            onWorldPositionLabelUpdate: {
                editorToolbar.coordDisplayText =
                        editorToolbar.coordDisplayTemplate.arg(wpX).arg(wpY).arg(wpZ)
            }

            function restoreSelection(entity) {
                var index = editorScene.sceneModel.getModelIndex(entity)
                selectIndex(index)
            }

            function restoreMultiSelection(selectionList) {
                // Deselect old ones
                entityTree.view.selection.clear()
                // Dig indexes of all selected entities and pass the selections to entitytree
                for (var i = 0; i < selectionList.length; ++i) {
                    var index = editorScene.sceneModel.getModelIndexByName(multiSelectionList[i])
                    entityTree.view.selection.select(index, ItemSelectionModel.Select)
                    expandTo(index)
                }
            }

            function selectIndex(index) {
                expandTo(index)
                entityTree.focusTree()
                entityTree.view.selection.setCurrentIndex(index, ItemSelectionModel.ClearAndSelect)
            }

            function expandTo(index) {
                var target = index
                do {
                    entityTree.view.expand(target)
                    target = target.parent
                } while (target.valid)
            }
        }

        MessageDialog {
            id: notification
            icon: StandardIcon.Warning
        }

        SettingsDialog {
            id: settingsDialog
        }

        MessageDialog {
            id: closingDialog
            icon: StandardIcon.Warning
            standardButtons: StandardButton.Yes | StandardButton.No | StandardButton.Cancel
            title: qsTr("Exit Qt 3D Scene Editor") + editorScene.emptyString
            text: qsTr("There are unsaved changes.\nSave changes before exiting?")
                  + editorScene.emptyString

            onYes: {
                if (editorContent.saveFileUrl == "") {
                    saveFileDialog.exiting = true
                    saveFileDialog.open()
                    // No previous autosave file, no need to delete anything
                    saveFileDialog.exiting = false
                } else {
                    editorScene.saveScene(editorContent.saveFileUrl)
                    editorScene.deleteScene(editorContent.saveFileUrl, true)
                    Qt.quit()
                }
            }

            onNo: {
                Qt.quit()
            }

            // Cancel doesn't need to do anything
        }

        MessageDialog {
            id: saveUnsavedDialog
            property bool newFile: false
            icon: StandardIcon.Warning
            standardButtons: StandardButton.Yes | StandardButton.No | StandardButton.Cancel
            title: qsTr("Save changes?") + editorScene.emptyString
            text: qsTr("There are unsaved changes.\nSave changes?")
                  + editorScene.emptyString

            onYes: {
                if (editorContent.saveFileUrl == "") {
                    saveFileDialog.folder = editorContent.saveFolder
                    saveFileDialog.open()
                    // No previous autosave file, no need to delete anything
                } else {
                    editorScene.saveScene(editorContent.saveFileUrl)
                    editorScene.deleteScene(editorContent.saveFileUrl, true)
                }
            }

            onNo: {
                if (newFile) {
                    editorScene.resetScene()
                    showNormalYPlane()
                    editorContent.saveFileUrl = ""
                } else {
                    loadFileDialog.folder = editorContent.saveFolder
                    loadFileDialog.open()
                }
            }

            // Cancel doesn't need to do anything
        }


        SplitView {
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.left: parent.left
            orientation: Qt.Horizontal
            width: parent.width - propertyPane.visibleWidth

            // Entity library
            EntityLibrary {
                id: entityLibrary
                z: 4
                Layout.minimumWidth: 100
                Layout.maximumWidth: editorContent.width - 10
                onCreateNewEntity: {
                    entityTree.selectSceneRoot()
                    entityTree.addNewEntity(entityType, xPos, yPos)
                }
            }

            Item {
                Layout.fillWidth: true
                Layout.minimumWidth: 100

                EditorViewport {
                    id: editorViewport
                    anchors.fill: parent
                    scene: editorScene

                    MouseArea {
                        id: viewportMouseArea
                        anchors.fill: parent
                        onPressed: {
                            entityTree.focusTree()
                            mouse.accepted = false
                        }
                        onEntered: entityTree.treeviewPasting = false
                        hoverEnabled: true
                        onPositionChanged: {
                            editorContent.mousePosY = mouseY
                            editorContent.mousePosX = mouseX
                            editorScene.updateWorldPositionLabel(mouseX, mouseY)
                        }
                        onExited: editorScene.updateWorldPositionLabel(-1, -1)
                        onCanceled: editorScene.updateWorldPositionLabel(-1, -1)
                    }

                    DropArea {
                        anchors.fill: parent
                        keys: [ "insertEntity" ]
                        onContainsDragChanged: {
                            dragEntityItem.visible = !containsDrag
                        }
                    }
                    DragHandle {
                        // This is the translate handle at the center of selection box
                        handleType: EditorScene.DragTranslate
                        baseZ: 3 // Make sure translate handle is on top of scale handles
                        color: "#c22555"
                        radius: height / 2
                        handleIndex: EditorScene.TranslateHandleBoxCenter
                    }
                    DragHandle {
                        // This is the group/mesh center indicator handle
                        handleType: EditorScene.DragTranslate
                        baseZ: 3 // Make sure center handle is on top of scale handles
                        color: "blue"
                        radius: height / 2
                        handleIndex: EditorScene.TranslateHandleMeshCenter
                    }
                    DragHandle {
                        handleType: EditorScene.DragRotate
                        baseZ: 3 // Make sure rotate handle is on top of scale handles
                        Image {
                            anchors.fill: parent
                            source: "images/rotate_handle.png"
                        }
                        color: "transparent"
                    }
                    // Each corner will have a separate drag handle
                    DragHandle {
                        handleType: EditorScene.DragScale
                        handleIndex: 0
                    }
                    DragHandle {
                        handleType: EditorScene.DragScale
                        handleIndex: 1
                    }
                    DragHandle {
                        handleType: EditorScene.DragScale
                        handleIndex: 2
                    }
                    DragHandle {
                        handleType: EditorScene.DragScale
                        handleIndex: 3
                    }
                    DragHandle {
                        handleType: EditorScene.DragScale
                        handleIndex: 4
                    }
                    DragHandle {
                        handleType: EditorScene.DragScale
                        handleIndex: 5
                    }
                    DragHandle {
                        handleType: EditorScene.DragScale
                        handleIndex: 6
                    }
                    DragHandle {
                        handleType: EditorScene.DragScale
                        handleIndex: 7
                    }

                    // Debug handle for visualizing world positions, uncomment to use.
                    // You can show debug handles with EditorScene::showDebugHandle().
    //                DragHandle {
    //                    handleType: EditorScene.DragDebug
    //                    color: "green"
    //                    baseZ: 4
    //                }
                }
            }
        }

        SplitView {
            id: propertyPane
            orientation: Qt.Vertical
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            width: editorContent.width / 4.5
            x: editorContent.width - visibleWidth

            property int visibleWidth: width

            // Entity list
            EntityTree {
                id: entityTree
            }

            GeneralPropertyView {
                id: generalPropertyView
                entityName: editorContent.selectedEntityName
                entityType: editorScene.sceneModel.editorSceneItemFromIndex(entityTree.view.selection.currentIndex).itemType()
                propertiesButtonVisible: {
                    (entityTree.view.selection.currentIndex
                     !== editorScene.sceneModel.sceneEntityIndex())
                            ? true : false
                }
            }

            // Property (transform, material, etc.) list
            ListView {
                id: componentPropertiesView
                Layout.fillHeight: true
                delegate: ComponentPropertiesDelegate {}
                flickableDirection: Flickable.VerticalFlick
                boundsBehavior: Flickable.StopAtBounds
                clip: true
                visible: generalPropertyView.viewTitleVisible
            }
        }

        onWidthChanged: {
            var maximumWidth = editorContent.width - entityLibrary.width - 10
            if (propertyPane.visibleWidth > maximumWidth) {
                propertyPane.visibleWidth = maximumWidth
                propertyPane.width = propertyPane.visibleWidth
            } else if (propertyPane.visibleWidth > resizeRectangle.paneMinimumWidth) {
                propertyPane.width = propertyPane.visibleWidth
            }
            propertyPane.x = editorContent.width - propertyPane.visibleWidth
        }

        Rectangle {
            id: resizeRectangle
            width: 2
            height: propertyPane.height
            anchors.top: parent.top
            anchors.right: propertyPane.left
            color: editorContent.viewBorderColor

            property int paneMinimumWidth: 250
            property int paneMaximumWidth: editorContent.width - entityLibrary.width - 10

            MouseArea {
                anchors.fill: parent
                hoverEnabled: true

                drag {
                    target: parent;
                    axis: Drag.XAxis
                }

                onEntered: {
                    cursorShape = Qt.SplitHCursor
                }

                onExited: {
                    cursorShape = Qt.ArrowCursor
                }

                onMouseXChanged: {
                    if (drag.active) {
                        var newPaneWidth = propertyPane.width - mouseX
                        if (propertyPane.visibleWidth < resizeRectangle.paneMinimumWidth
                                || newPaneWidth < resizeRectangle.paneMinimumWidth) {
                            var newXPos = propertyPane.x + mouseX
                            if (newXPos < editorContent.width) {
                                propertyPane.visibleWidth = editorContent.width - newXPos
                                propertyPane.x = newXPos
                            }
                        }
                        else if (newPaneWidth > resizeRectangle.paneMinimumWidth
                                 && newPaneWidth < resizeRectangle.paneMaximumWidth) {
                            propertyPane.x = editorContent.width - newPaneWidth
                            propertyPane.width = newPaneWidth
                            propertyPane.visibleWidth = propertyPane.width
                        }
                    }
                }
            }
        }

        Timer {
            id: autoSaveTimer
            running: false
            interval: 600000 // 10 minutes
            repeat: true
            onTriggered: {
                editorScene.saveScene(editorContent.saveFileUrl, true)
            }
        }
    }
}