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
23RowLayout {
24    id: root
25    property int minimumValue: 0
26    property int maximumValue: 99
27    property int value: 0
28    property alias undoButtonVisible: undoButton.visible
29    property alias saveButtonVisible: saveButton.visible
30
31    signal setDefaultClicked()
32    signal saveDefaultClicked()
33
34    spacing: 0
35
36    TextField {
37        id: timeField
38        text: filter.timeFromFrames(clamp(value, minimumValue, maximumValue))
39        horizontalAlignment: TextInput.AlignRight
40        selectByMouse: true
41        validator: RegExpValidator {regExp: /^\s*(\d*:){0,2}(\d*[.;:])?\d*\s*$/}
42        onEditingFinished: value = filter.framesFromTime(text)
43        Keys.onDownPressed: decrementAction.trigger()
44        Keys.onUpPressed: incrementAction.trigger()
45        onFocusChanged: if (focus) selectAll()
46    }
47    Shotcut.Button {
48        id: decrementButton
49        icon.name: 'list-remove'
50        icon.source: 'qrc:///icons/oxygen/32x32/actions/list-remove.png'
51        Shotcut.HoverTip { text: qsTr('Decrement') }
52        implicitWidth: 20
53        implicitHeight: 20
54        MouseArea {
55            anchors.fill: parent
56            onPressed: decrementAction.trigger()
57            onPressAndHold: decrementTimer.start()
58            onReleased: decrementTimer.stop()
59        }
60        Timer {
61            id: decrementTimer
62            repeat: true
63            interval: 200
64            triggeredOnStart: true
65            onTriggered: decrementAction.trigger()
66        }
67    }
68    Shotcut.Button {
69        id: incrementButton
70        icon.name: 'list-add'
71        icon.source: 'qrc:///icons/oxygen/32x32/actions/list-add.png'
72        Shotcut.HoverTip { text: qsTr('Increment') }
73        implicitWidth: 20
74        implicitHeight: 20
75        MouseArea {
76            anchors.fill: parent
77            onPressed: incrementAction.trigger()
78            onPressAndHold: incrementTimer.start()
79            onReleased: incrementTimer.stop()
80        }
81        Timer {
82            id: incrementTimer
83            repeat: true
84            interval: 200
85            triggeredOnStart: true
86            onTriggered: incrementAction.trigger()
87        }
88    }
89    Shotcut.UndoButton {
90        id: undoButton
91        onClicked: root.setDefaultClicked()
92    }
93    Shotcut.SaveDefaultButton {
94        id: saveButton
95        onClicked: root.saveDefaultClicked()
96    }
97    Action {
98        id: decrementAction
99        onTriggered: value = Math.max(value - 1, minimumValue)
100    }
101    Action {
102        id: incrementAction
103        onTriggered: value = Math.min(value + 1, maximumValue)
104    }
105
106    function clamp(x, min, max) {
107        return Math.max(min, Math.min(max, x))
108    }
109}
110