1/*
2 * Copyright (c) 2019-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 threshold: 'midpoint'
25    property double thresholdDefault: 128
26
27    keyframableParameters: [threshold]
28    startValues: [thresholdDefault]
29    middleValues: [thresholdDefault]
30    endValues: [thresholdDefault]
31
32    width: 350
33    height: 150
34
35    Component.onCompleted: {
36        if (filter.isNew) {
37            filter.set(threshold, thresholdDefault)
38            filter.savePreset(preset.parameters)
39        }
40        setControls()
41    }
42
43    function setControls() {
44        setKeyframableControls()
45        invertCheckbox.checked = filter.get('invert') === '1'
46        useAlphaCheckbox.checked = filter.get('use_alpha') === '1'
47    }
48
49    function setKeyframableControls() {
50        var position = getPosition()
51        blockUpdate = true
52        thresholdSlider.value = filter.getDouble(threshold, position) / 255  * thresholdSlider.maximumValue
53        thresholdKeyframesButton.checked = filter.animateIn <= 0 && filter.animateOut <= 0 && filter.keyframeCount(threshold) > 0
54        blockUpdate = false
55        enableControls(isSimpleKeyframesActive())
56    }
57
58    function enableControls(enabled) {
59        thresholdSlider.enabled = enabled
60    }
61
62    function updateSimpleKeyframes() {
63        updateFilter(threshold, thresholdSlider.value / thresholdSlider.maximumValue * 255, thresholdKeyframesButton)
64    }
65
66    GridLayout {
67        anchors.fill: parent
68        anchors.margins: 8
69        columns: 4
70
71        Label {
72            text: qsTr('Preset')
73            Layout.alignment: Qt.AlignRight
74        }
75        Shotcut.Preset {
76            id: preset
77            parameters: [threshold, 'invert', 'use_alpha']
78            Layout.columnSpan: 3
79            onBeforePresetLoaded: {
80                filter.resetProperty(threshold)
81            }
82            onPresetSelected: {
83                setControls()
84                initializeSimpleKeyframes()
85            }
86        }
87
88        Label {
89            text: qsTr('Level')
90            Layout.alignment: Qt.AlignRight
91        }
92        Shotcut.SliderSpinner {
93            id: thresholdSlider
94            minimumValue: 0
95            maximumValue: 100
96            stepSize: 1
97            decimals: 1
98            suffix: ' %'
99            onValueChanged: updateFilter(threshold, thresholdSlider.value / thresholdSlider.maximumValue * 255, thresholdKeyframesButton, getPosition())
100        }
101        Shotcut.UndoButton {
102            onClicked: thresholdSlider.value = thresholdDefault / 255  * thresholdSlider.maximumValue
103        }
104        Shotcut.KeyframesButton {
105            id: thresholdKeyframesButton
106            onToggled: {
107                enableControls(true)
108                toggleKeyframes(checked, threshold, thresholdSlider.value / thresholdSlider.maximumValue * 255)
109            }
110        }
111
112        Item { width: 1 }
113        CheckBox {
114            id: invertCheckbox
115            text: qsTr('Invert')
116            onCheckedChanged: filter.set('invert', checked)
117        }
118        Shotcut.UndoButton {
119            onClicked: invertCheckbox.checked = false
120        }
121        Item { width: 1 }
122
123        Label {}
124        CheckBox {
125            id: useAlphaCheckbox
126            text: qsTr('Compare with alpha channel')
127            onCheckedChanged: filter.set('use_alpha', checked)
128        }
129        Shotcut.UndoButton {
130            onClicked: useAlphaCheckbox.checked = false
131        }
132        Item { width: 1 }
133
134        Item {
135            Layout.fillHeight: true
136        }
137    }
138
139    Connections {
140        target: filter
141        onInChanged: updateSimpleKeyframes()
142        onOutChanged: updateSimpleKeyframes()
143        onAnimateInChanged: updateSimpleKeyframes()
144        onAnimateOutChanged: updateSimpleKeyframes()
145    }
146
147    Connections {
148        target: producer
149        onPositionChanged: setKeyframableControls()
150    }
151}
152