1/*
2 * Copyright (c) 2014-2021 Meltytech, LLC
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18import QtQuick 2.12
19import QtQuick.Controls 2.12
20import QtQuick.Layouts 1.12
21import Shotcut.Controls 1.0 as Shotcut
22
23Item {
24    property string paramBlur: '0'
25    property var defaultParameters: [paramBlur]
26    property bool blockUpdate: true
27    property double startValue: 0.0
28    property double middleValue: 0.5
29    property double endValue: 0.0
30    width: 350
31    height: 50
32    Component.onCompleted: {
33        if (filter.isNew) {
34            // Set default parameter values
35            filter.set(paramBlur, 50.0 / 100.0)
36            filter.savePreset(defaultParameters)
37        } else {
38            middleValue = filter.getDouble(paramBlur, filter.animateIn)
39            if (filter.animateIn > 0)
40                startValue = filter.getDouble(paramBlur, 0)
41            if (filter.animateOut > 0)
42                endValue = filter.getDouble(paramBlur, filter.duration - 1)
43        }
44        setControls()
45    }
46
47    function getPosition() {
48        return Math.max(producer.position - (filter.in - producer.in), 0)
49    }
50
51    function setControls() {
52        var position = getPosition()
53        blockUpdate = true
54        bslider.value = filter.getDouble(paramBlur, position) * 100.0
55        blurKeyframesButton.checked = filter.keyframeCount(paramBlur) > 0 && filter.animateIn <= 0 && filter.animateOut <= 0
56        blockUpdate = false
57        bslider.enabled = position <= 0 || (position >= (filter.animateIn - 1) && position <= (filter.duration - filter.animateOut)) || position >= (filter.duration - 1)
58    }
59
60    function updateFilter(position) {
61        if (blockUpdate) return
62        var value = bslider.value / 100.0
63
64        if (position !== null) {
65            if (position <= 0 && filter.animateIn > 0)
66                startValue = value
67            else if (position >= filter.duration - 1 && filter.animateOut > 0)
68                endValue = value
69            else
70                middleValue = value
71        }
72
73        if (filter.animateIn > 0 || filter.animateOut > 0) {
74            filter.resetProperty(paramBlur)
75            blurKeyframesButton.checked = false
76            if (filter.animateIn > 0) {
77                filter.set(paramBlur, startValue, 0)
78                filter.set(paramBlur, middleValue, filter.animateIn - 1)
79            }
80            if (filter.animateOut > 0) {
81                filter.set(paramBlur, middleValue, filter.duration - filter.animateOut)
82                filter.set(paramBlur, endValue, filter.duration - 1)
83            }
84        } else if (!blurKeyframesButton.checked) {
85            filter.resetProperty(paramBlur)
86            filter.set(paramBlur, middleValue)
87        } else if (position !== null) {
88            filter.set(paramBlur, value, position)
89        }
90    }
91
92    GridLayout {
93        columns: 4
94        anchors.fill: parent
95        anchors.margins: 8
96
97        Label {
98            text: qsTr('Preset')
99            Layout.alignment: Qt.AlignRight
100        }
101        Shotcut.Preset {
102            Layout.columnSpan: 3
103            parameters: defaultParameters
104            onBeforePresetLoaded: filter.resetProperty(paramBlur)
105            onPresetSelected: {
106                setControls()
107                middleValue = filter.getDouble(paramBlur, filter.animateIn)
108                if (filter.animateIn > 0)
109                    startValue = filter.getDouble(paramBlur, 0)
110                if (filter.animateOut > 0)
111                    endValue = filter.getDouble(paramBlur, filter.duration - 1)
112            }
113        }
114
115        Label {
116            text: qsTr('Blur')
117            Layout.alignment: Qt.AlignRight
118        }
119        Shotcut.SliderSpinner {
120            id: bslider
121            minimumValue: 0
122            maximumValue: 100
123            suffix: ' %'
124            onValueChanged: updateFilter(getPosition())
125        }
126        Shotcut.UndoButton {
127            onClicked: bslider.value = 50
128        }
129        Shotcut.KeyframesButton {
130            id: blurKeyframesButton
131            onToggled: {
132                var value = bslider.value / 100.0
133                if (checked) {
134                    blockUpdate = true
135                    filter.clearSimpleAnimation(paramBlur)
136                    blockUpdate = false
137                    filter.set(paramBlur, value, getPosition())
138                } else {
139                    filter.resetProperty(paramBlur)
140                    filter.set(paramBlur, value)
141                }
142            }
143        }
144
145        Item {
146            Layout.fillHeight: true
147        }
148    }
149
150    Connections {
151        target: filter
152        onInChanged: updateFilter(null)
153        onOutChanged: updateFilter(null)
154        onAnimateInChanged: updateFilter(null)
155        onAnimateOutChanged: updateFilter(null)
156    }
157
158    Connections {
159        target: producer
160        onPositionChanged: setControls()
161    }
162}
163