File: main.qml

package info (click to toggle)
qt6-quick3d 6.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 143,588 kB
  • sloc: cpp: 395,989; ansic: 41,469; xml: 288; sh: 242; makefile: 32
file content (319 lines) | stat: -rw-r--r-- 10,349 bytes parent folder | download | duplicates (2)
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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import QtQuick
import QtQuick.Window
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Dialogs
import QtCore

import QtQuick3D.MaterialEditor

ApplicationWindow {
    id: window
    height: 720
    width: 1024
    visible: true
    title: qsTr("Custom Material Editor")

    // Context property (see main.cpp)
    property url projectFolder: _qtProjectDir // qmllint disable unqualified

    Settings {
        id: settings
        property alias windowX: window.x
        property alias windowY: window.y
        property alias windowWidth: window.width
        property alias windowHeight: window.height
        property alias windowVisibility: window.visibility
    }

    Component.onCompleted: {
        mainSplitView.restoreState(settings.value("ui/mainSplitView"))
        editorView.restoreState(settings.value("ui/editorView"))
    }
    Component.onDestruction: {
        settings.setValue("ui/mainSplitView", mainSplitView.saveState())
        settings.setValue("ui/editorView", editorView.saveState())
    }

    QtObject {
        id: resourceStore
        objectName: "QtQuick3DResourceStorePrivate"
    }

    FileDialog {
        id: openMaterialDialog
        title: "Open a Material Project File"
        nameFilters: [ "Material Editor Project (*.qmp)"]
        currentFolder: window.projectFolder
        onAccepted: {
            if (openMaterialDialog.selectedFile !== null)
                materialAdapter.loadMaterial(openMaterialDialog.selectedFile);
        }
    }

    FileDialog {
        id: saveAsDialog
        fileMode: FileDialog.SaveFile
        currentFolder: window.projectFolder
        nameFilters: [ "Material Editor Project (*.qmp)"]
        onAccepted: materialAdapter.saveMaterial(selectedFile)

    }


    FileDialog {
        id: fragmentShaderImportDialog
        title: "Fragment Shader to import"
        nameFilters: [ "Fragment Shader (*.frag *.fs *.glsl)" ]
        currentFolder: window.projectFolder
        onAccepted: {
            if (fragmentShaderImportDialog.selectedFile !== null) {
                materialAdapter.importFragmentShader(fragmentShaderImportDialog.selectedFile)
            }
        }
    }

    FileDialog {
        id: vertexShaderImportDialog
        title: "Vertex Shader to import"
        nameFilters: [ "Vertex Shader (*.vert *.vs *.glsl)" ]
        currentFolder: window.projectFolder
        onAccepted: {
            if (vertexShaderImportDialog.selectedFile !== null) {
                materialAdapter.importVertexShader(vertexShaderImportDialog.selectedFile)
            }
        }
    }

    FileDialog {
        id: saveCompFileDialog
        title: "Choose file"
        nameFilters: [ "QML Componen (*.qml)" ]
        fileMode: FileDialog.SaveFile
        currentFolder: window.projectFolder
        onAccepted: {
            if (selectedFile !== null)
                componentFilePath.text = selectedFile
        }
    }

    RegularExpressionValidator {
        id: nameValidator
        regularExpression: /[a-zA-Z0-9_-]*/
    }

    Dialog {
        id: exportMaterialDialog
        title: "Export material"
        anchors.centerIn: parent

        ColumnLayout {
            id: exportFiles
            anchors.fill: parent
            spacing: 1
            RowLayout {
                Text {
                    text: qsTr("Component")
                    color: palette.text
                }
                TextField {
                    id: componentFilePath
                    readOnly: true
                }
                Button {
                    text: qsTr("Choose...")
                    onClicked: {
                        saveCompFileDialog.open()
                        exportMaterialDialog.aboutToHide()
                    }
                }
            }
            RowLayout {
                Text {
                    text: qsTr("Vertex:")
                    color: palette.text
                }
                TextField {
                    id: vertexFilename
                    enabled: (editorView.vertexEditor.text !== "")
                    validator: nameValidator
                }
            }
            RowLayout {
                Text {
                    text: qsTr("Fragment:")
                    color: palette.text
                }
                TextField {
                    id: fragmentFilename
                    enabled: (editorView.fragmentEditor.text !== "")
                    validator: nameValidator
                }
            }

            DialogButtonBox {
                Button {
                    text: qsTr("Export")
                    enabled: (componentFilePath.text !== "" && (!vertexFilename.enabled || (vertexFilename.enabled && vertexFilename.text !== "")) && (!fragmentFilename.enabled || (fragmentFilename.enabled && fragmentFilename.text !== "")))
                    DialogButtonBox.buttonRole: DialogButtonBox.AcceptRole
                    onClicked: exportMaterialDialog.accept()
                }
                Button {
                    text: qsTr("Cancel")
                    DialogButtonBox.buttonRole: DialogButtonBox.DestructiveRole
                    onClicked: exportMaterialDialog.reject()
                }
            }
        }

        onAccepted: {
            materialAdapter.exportQmlComponent(componentFilePath.text, vertexFilename.text, fragmentFilename.text)
        }
    }

    SaveChangesDialog {
        id: saveChangesDialog
        materialAdapter: materialAdapter
        saveAsDialog: saveAsDialog
        anchors.centerIn: parent
    }

    AboutDialog {
        id: aboutDialog
        parent: Overlay.overlay
        anchors.centerIn: parent
    }

    function saveAction() {
        // 1. No file name(s) given (call saveAs)
        let materialSaveFileUrl = new URL(materialAdapter.materialSaveFile)
        if (materialSaveFileUrl.toString().length > 0)
            materialAdapter.save()
        else
            saveAsAction()
    }
    function openAction() {
        openMaterialDialog.open()
    }
    function newAction() {
        saveChangesDialog.doIfChangesSavedOrDiscarded(() => { materialAdapter.reset() });
        materialAdapter.reset()
    }
    function saveAsAction() {
        saveAsDialog.open()
    }
    function quitAction() {
        Qt.quit()
    }
    function aboutAction() {
        aboutDialog.open()
    }

    function importFragmentShader() {
        fragmentShaderImportDialog.open()
    }

    function importVertexShader() {
        vertexShaderImportDialog.open()
    }

    function exportMaterial() {
        exportMaterialDialog.open()
    }

    menuBar: MenuBar {
        Menu {
            title: qsTr("&File")
            Action { text: qsTr("&New..."); onTriggered: window.newAction(); }
            Action { text: qsTr("&Open..."); onTriggered: window.openAction(); }
            Action { text: qsTr("&Save"); onTriggered: window.saveAction(); }
            Action { text: qsTr("Save &As..."); onTriggered: window.saveAsAction(); }
            MenuSeparator { }
            Menu {
                title: qsTr("Import")
                Action { text: qsTr("Fragment Shader"); onTriggered: window.importFragmentShader(); }
                Action { text: qsTr("Vertex Shader"); onTriggered: window.importVertexShader(); }
            }
            Action { text: qsTr("Export"); onTriggered: window.exportMaterial(); }

            MenuSeparator { }
            Action { text: qsTr("&Quit"); onTriggered: window.quitAction(); }
        }
        Menu {
            title: qsTr("&Help")
            Action { text: qsTr("&About"); onTriggered: window.aboutAction(); }
        }
    }

    SplitView {
        id: mainSplitView
        anchors.fill: parent
        orientation: Qt.Horizontal
        EditorView {
            id: editorView
            vertexTabText: "Vertex Shader"
            fragmentTabText: "Fragment Shader"
            SplitView.preferredWidth: window.width * 0.5
            SplitView.fillWidth: true
            materialAdapter: materialAdapter
            instanceEntry: preview.instanceEntry
            targetModel: preview.modelInstance
        }
        Preview {
            id: preview
            implicitWidth: parent.width * 0.5
            currentMaterial: materialAdapter.material
        }
    }

    function outputLine(lineText) {
        // Prepend
        editorView.outputTextItem.text = lineText + "\n" + editorView.outputTextItem.text;
    }

    function printShaderStatusError(stage, msg) {
        let outputString = ""
        outputString += msg.filename + " => " + msg.message
        if (msg.identifier !== null && msg.identifier !== "")
            outputString += " '" + msg.identifier + "'";
        if (msg.line >= 0)
            outputString += ", on line: " + msg.line
        outputLine(outputString)
    }

    MaterialAdapter {
        id: materialAdapter
        vertexShader: editorView.vertexEditor.text
        fragmentShader: editorView.fragmentEditor.text
        rootNode: preview.rootNode
        uniformModel: editorView.uniformModel
        onVertexStatusChanged: {
            if (vertexStatus.status !== ShaderConstants.Success) {
                editorView.tabBarInfoView.currentIndex = 1
                window.printShaderStatusError(ShaderConstants.Vertex, vertexStatus)
            } else if (fragmentStatus.status === ShaderConstants.Success){
                // both work, clear
                editorView.outputTextItem.text = "";
            }
        }
        onFragmentStatusChanged: {
            if (fragmentStatus.status !== ShaderConstants.Success) {
                editorView.tabBarInfoView.currentIndex = 1
                window.printShaderStatusError(ShaderConstants.Fragment, fragmentStatus)
            } else if (vertexStatus.status === ShaderConstants.Success) {
                // both work, clear
                editorView.outputTextItem.text = "";
            }
        }

        onVertexShaderChanged: {
            editorView.vertexEditor.text = materialAdapter.vertexShader
        }
        onFragmentShaderChanged: {
            editorView.fragmentEditor.text = materialAdapter.fragmentShader
        }
    }
}