aboutsummaryrefslogtreecommitdiffstats
path: root/share/qtcreator/qmldesigner/effectComposerQmlSources/EffectComposerPreview.qml
blob: c0c47f736617408aa227ff413b8ee0c8fe9d8ad6 (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
// 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 HelperWidgets as HelperWidgets
import StudioControls as StudioControls
import StudioTheme as StudioTheme
import EffectComposerBackend

Column {
    id: root

    property real animatedTime: previewFrameTimer.elapsedTime
    property int animatedFrame: previewFrameTimer.currentFrame
    property bool timeRunning: previewAnimationRunning

    required property Item mainRoot
    property var effectComposerModel: EffectComposerBackend.effectComposerModel
    property alias source: source
    // The delay in ms to wait until updating the effect
    readonly property int updateDelay: 100

    readonly property int previewMargin: 5
    readonly property int extraMargin: 200

    property real previewScale: 1

    // Create a dummy parent to host the effect qml object
    function createNewComponent() {
        // If we have a working effect, do not show preview image as it shows through
        // transparent parts of the final image
        placeHolder.visible = false;

        var oldComponent = componentParent.children[0];
        if (oldComponent)
            oldComponent.destroy();
        try {
            const newObject = Qt.createQmlObject(
                effectComposerModel.qmlComponentString(),
                componentParent,
                ""
            );
            effectComposerModel.resetEffectError(0);
        } catch (error) {
            let errorString = "QML: ERROR: ";
            let errorLine = -1;
            if (error.qmlErrors.length > 0) {
                // Show the first QML error
                let e = error.qmlErrors[0];
                errorString += e.lineNumber + ": " + e.message;
                errorLine = e.lineNumber;
            }
            effectComposerModel.setEffectError(errorString, 0, errorLine);
            placeHolder.visible = true;
        }
    }

    Rectangle { // toolbar
        width: parent.width
        height: StudioTheme.Values.toolbarHeight
        color: StudioTheme.Values.themeToolbarBackground

        Row {
            spacing: 5
            anchors.leftMargin: 5
            anchors.left: parent.left
            anchors.verticalCenter: parent.verticalCenter

            PreviewImagesComboBox {
                id: imagesComboBox

                mainRoot: root.mainRoot
            }

            StudioControls.ColorEditor {
                id: colorEditor

                actionIndicatorVisible: false
                showHexTextField: false
                color: "#dddddd"
            }
        }

        Row {
            spacing: 5
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter

            HelperWidgets.AbstractButton {
                enabled: root.previewScale < 3
                style: StudioTheme.Values.viewBarButtonStyle
                buttonIcon: StudioTheme.Constants.zoomIn_medium
                tooltip: qsTr("Zoom In")

                onClicked: {
                    imageScaler.enableAnim(true)
                    root.previewScale += .2
                    imageScaler.enableAnim(false)
                    zoomIndicator.show()
                }
            }

            HelperWidgets.AbstractButton {
                enabled: root.previewScale > .4
                style: StudioTheme.Values.viewBarButtonStyle
                buttonIcon: StudioTheme.Constants.zoomOut_medium
                tooltip: qsTr("Zoom out")

                onClicked: {
                    imageScaler.enableAnim(true)
                    root.previewScale -= .2
                    imageScaler.enableAnim(false)
                    zoomIndicator.show()
                }
            }

            HelperWidgets.AbstractButton {
                enabled: root.previewScale !== 1 || imageScaler.x !== root.previewMargin
                                                 || imageScaler.y !== root.previewMargin
                style: StudioTheme.Values.viewBarButtonStyle
                buttonIcon: StudioTheme.Constants.fitAll_medium
                tooltip: qsTr("Reset View")

                onClicked: {
                    imageScaler.resetTransforms()
                }
            }
        }

        Row {
            spacing: 5
            anchors.rightMargin: 5
            anchors.right: parent.right
            anchors.verticalCenter: parent.verticalCenter

            Column {
                Text {
                    text: animatedTime >= 100
                          ? animatedTime.toFixed(1) + " s" : animatedTime.toFixed(3) + " s"
                    color: StudioTheme.Values.themeTextColor
                    font.pixelSize: 10
                }

                Text {
                    text: (animatedFrame).toString().padStart(6, '0')
                    color: StudioTheme.Values.themeTextColor
                    font.pixelSize: 10
                }
            }

            HelperWidgets.AbstractButton {
                style: StudioTheme.Values.viewBarButtonStyle
                buttonIcon: StudioTheme.Constants.toStartFrame_medium
                tooltip: qsTr("Restart Animation")

                onClicked: {
                    previewFrameTimer.reset()
                }
            }

            HelperWidgets.AbstractButton {
                style: StudioTheme.Values.viewBarButtonStyle
                buttonIcon: previewAnimationRunning ? StudioTheme.Constants.pause_medium
                                                    : StudioTheme.Constants.playOutline_medium
                tooltip: qsTr("Play Animation")

                onClicked: {
                    previewAnimationRunning = !previewAnimationRunning
                }
            }
        }
    }

    Rectangle { // preview image
        id: preview

        color: colorEditor.color
        width: parent.width
        height: root.height - y
        clip: true

        MouseArea {
            id: mouseArea

            anchors.fill: parent
            acceptedButtons: Qt.LeftButton

            property real pressX: 0
            property real pressY: 0
            property bool panning: false

            onPressed:  {
                pressX = mouseX - imageScaler.x
                pressY = mouseY - imageScaler.y
                panning = true
            }

            onReleased: {
                panning = false
            }

            onWheel: (wheel) => {
                let oldPoint = imageScaler.mapFromItem(mouseArea, Qt.point(wheel.x, wheel.y))

                if (wheel.angleDelta.y > 0) {
                    if (root.previewScale < 3)
                        root.previewScale += .2
                } else {
                    if (root.previewScale > .4)
                        root.previewScale -= .2
                }

                let newPoint = imageScaler.mapFromItem(mouseArea, Qt.point(wheel.x, wheel.y))
                imageScaler.x -= (oldPoint.x - newPoint.x) * imageScaler.scale
                imageScaler.y -= (oldPoint.y - newPoint.y) * imageScaler.scale

                imageScaler.checkBounds()
                zoomIndicator.show()
            }

            Timer { // pan timer
                running: parent.panning
                interval: 16
                repeat: true

                onTriggered: {
                    imageScaler.x = mouseArea.mouseX - mouseArea.pressX
                    imageScaler.y = mouseArea.mouseY - mouseArea.pressY
                    imageScaler.checkBounds()
                }
            }
        }

        Image {
            id: placeHolder
            anchors.fill: parent
            anchors.margins: root.previewMargin
            fillMode: Image.PreserveAspectFit
            source: imagesComboBox.selectedImage
            smooth: true
        }

        Item { // Source item as a canvas (render target) for effect
            id: source
            width: sourceImage.sourceSize.width
            height: sourceImage.sourceSize.height
            layer.enabled: true
            layer.mipmap: true
            layer.smooth: true
            layer.sourceRect: Qt.rect(-root.extraMargin, -root.extraMargin,
                                      width + root.extraMargin * 2, height + root.extraMargin * 2)
            visible: false

            Image {
                id: sourceImage

                onSourceChanged: imageScaler.resetTransforms()

                fillMode: Image.Pad

                source: imagesComboBox.selectedImage
                smooth: true
            }
        }

        BlurHelper {
            id: blurHelper
            source: source
            property int blurMax: g_propertyData.blur_helper_max_level ? g_propertyData.blur_helper_max_level : 64
            property real blurMultiplier: g_propertyData.blurMultiplier ? g_propertyData.blurMultiplier : 0
        }

        Item {
            id: imageScaler
            x: root.previewMargin
            y: root.previewMargin
            width: parent.width - root.previewMargin * 2
            height: parent.height - root.previewMargin * 2

            scale: root.previewScale * (width > height ? height / sourceImage.sourceSize.height
                                                       : width / sourceImage.sourceSize.width)

            Behavior on x {
                id: xBehavior

                enabled: false
                NumberAnimation {
                    duration: 200
                    easing.type: Easing.OutQuad
                }
            }

            Behavior on y {
                id: yBehavior

                enabled: false
                NumberAnimation {
                    duration: 200
                    easing.type: Easing.OutQuad
                }
            }

            Behavior on scale {
                id: scaleBehavior

                enabled: false
                NumberAnimation {
                    duration: 200
                    easing.type: Easing.OutQuad
                }
            }

            function checkBounds() {
                let edgeMargin = 10
                // correction factor to account for an observation that edgeMargin decreases
                // with increased zoom
                let corrFactor = 10 * imageScaler.scale
                let imgW2 = sourceImage.paintedWidth * imageScaler.scale * .5
                let imgH2 = sourceImage.paintedHeight * imageScaler.scale * .5
                let srcW2 = width * .5
                let srcH2 = height * .5

                if (imageScaler.x < -srcW2 - imgW2 + edgeMargin + corrFactor)
                    imageScaler.x = -srcW2 - imgW2 + edgeMargin + corrFactor
                else if (x > srcW2 + imgW2 - edgeMargin - corrFactor)
                    imageScaler.x = srcW2 + imgW2 - edgeMargin - corrFactor

                if (imageScaler.y < -srcH2 - imgH2 + edgeMargin + corrFactor)
                    imageScaler.y = -srcH2 - imgH2 + edgeMargin + corrFactor
                else if (y > srcH2 + imgH2 - edgeMargin - corrFactor)
                    imageScaler.y = srcH2 + imgH2 - edgeMargin - corrFactor
            }

            function resetTransforms() {
                imageScaler.enableAnim(true)
                root.previewScale = 1
                imageScaler.x = root.previewMargin
                imageScaler.y = root.previewMargin
                imageScaler.enableAnim(false)
            }

            function enableAnim(flag) {
                xBehavior.enabled = flag
                yBehavior.enabled = flag
                scaleBehavior.enabled = flag
            }

            Item {
                id: componentParent
                width: source.width
                height: source.height
                anchors.centerIn: parent
            }
        }

        Rectangle {
            id: zoomIndicator

            width: 40
            height: 20
            color: StudioTheme.Values.themeDialogBackground
            visible: false

            function show() {
                zoomIndicator.visible = true
                zoomIndicatorTimer.start()
            }

            Text {
                text: Math.round(root.previewScale * 100) + "%"
                color: StudioTheme.Values.themeTextColor
                anchors.centerIn: parent
            }

            Timer {
                id: zoomIndicatorTimer
                interval: 1000
                onTriggered: zoomIndicator.visible = false
            }
        }

        Connections {
            target: effectComposerModel
            function onShadersBaked() {
                updateTimer.restart()
            }
        }

        Timer {
            id: updateTimer
            interval: updateDelay
            onTriggered: {
                effectComposerModel.updateQmlComponent()
                createNewComponent()
            }
        }
    }
}