1/*
2 * Copyright (c) 2020-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
23Shotcut.KeyframableFilter {
24    property string amount: 'amount'
25    property int amountDefault: 5
26
27    keyframableParameters: [amount]
28    startValues: [0]
29    middleValues: [amountDefault]
30    endValues: [0]
31
32    width : 350
33    height: 100
34
35    Component.onCompleted: {
36        if (filter.isNew) {
37            filter.set(amount, amountDefault)
38            filter.savePreset(preset.parameters)
39        }
40        setControls();
41    }
42
43    function setControls() {
44        blockUpdate = true
45        amountSlider.value = filter.getDouble(amount, getPosition())
46        amountKeyframesButton.checked = filter.animateIn <= 0 && filter.animateOut <= 0 && filter.keyframeCount(amount) > 0
47        blockUpdate = false
48        enableControls(isSimpleKeyframesActive())
49    }
50
51    function enableControls(enabled) {
52        amountSlider.enabled = enabled
53    }
54
55    function updateSimpleKeyframes() {
56        updateFilter(amount, amountSlider.value, amountKeyframesButton)
57    }
58
59    GridLayout {
60        anchors.fill: parent
61        anchors.margins: 8
62        columns: 4
63
64        Label {
65            text: qsTr('Preset')
66            Layout.alignment: Qt.AlignRight
67        }
68
69        Shotcut.Preset {
70            id: preset
71            Layout.columnSpan: parent.columns - 1
72            parameters: [amount]
73            onBeforePresetLoaded: {
74                filter.resetProperty(amount)
75            }
76            onPresetSelected: {
77                setControls()
78                initializeSimpleKeyframes()
79            }
80        }
81
82        Label {
83            text: qsTr('Repeat')
84            Layout.alignment: Qt.AlignRight
85        }
86        Shotcut.SliderSpinner {
87            id: amountSlider
88            minimumValue: 0
89            maximumValue: Math.round(profile.fps)
90            stepSize: 1
91            suffix: qsTr(' frames')
92            spinnerWidth: 110
93            onValueChanged: updateFilter(amount, amountSlider.value, amountKeyframesButton, getPosition())
94        }
95        Shotcut.UndoButton {
96            onClicked: amountSlider.value = amountDefault
97        }
98        Shotcut.KeyframesButton {
99            id: amountKeyframesButton
100            onToggled: {
101                enableControls(true)
102                toggleKeyframes(checked, amount, amountSlider.value)
103            }
104        }
105
106        Item { Layout.fillHeight: true }
107    }
108
109    Connections {
110        target: filter
111        onInChanged: updateSimpleKeyframes()
112        onOutChanged: updateSimpleKeyframes()
113        onAnimateInChanged: updateSimpleKeyframes()
114        onAnimateOutChanged: updateSimpleKeyframes()
115    }
116
117    Connections {
118        target: producer
119        onPositionChanged: setControls()
120    }
121}
122