summaryrefslogtreecommitdiffstats
path: root/editorlib/qml/inputfields/FloatSliderInputField.qml
blob: 1dee997b3b930d8068dc4f0a3597ecfc1f25e7ce (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D Editor of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick 2.5
import QtQuick.Controls 2.0 as QQC2
import QtQuick.Layouts 1.2

Item {
    id: floatSliderInputField
    width: parent.width
    height: sliderLayout.height

    property alias lockProperty: lockButton.lockProperty
    property alias lockComponent: lockButton.lockComponent
    property alias locked: lockButton.locked
    property alias label: sliderLabel.text
    property alias stepSize: slider.stepSize
    property double minimum: 0.0
    property double maximum: 100.0
    property double value: 0.0
    property int roundDigits: 1 // TODO: Determine nice default rounding
    property int roundMultiplier: Math.pow(10, roundDigits) // Calculated from roundDigits, do not set directly
    property alias tooltip: sliderLabel.tooltip

    function roundNumber(number) {
        if (roundDigits >= 0)
            return Math.round(number * roundMultiplier) / roundMultiplier
        else
            return number
    }

    function tryCommitValue(desiredValue) {
        var newValue
        if (desiredValue !== "")
            newValue = roundNumber(desiredValue)
        else
            newValue = roundNumber(component[propertyName])
        newValue = Math.max(newValue, minimum)
        newValue = Math.min(newValue, maximum)
        value = newValue
        if (floatInput.text != newValue)
            floatInput.text = newValue
    }

    onValueChanged: {
        var roundedValue = roundNumber(value)
        floatInput.text = roundedValue
        slider.blockCommit = true
        slider.value = roundedValue
        slider.blockCommit = false
    }

    RowLayout {
        id: sliderLayout
        width: parent.width

        StyledLabel {
            id: sliderLabel
            text: qsTr("Float Slider") + editorScene.emptyString
            enabled: lockButton.buttonEnabled
            Layout.alignment: Qt.AlignLeft
        }

        DoubleValidator {
            id: doubleValidator
            locale: "C"
        }

        Rectangle {
            color: editorContent.paneBackgroundColor
            height: floatSliderInputField.height
            width: floatSliderInputField.width * 0.6 > editorContent.maximumControlWidth
                   ? editorContent.maximumControlWidth + editorContent.controlMargin
                   : floatSliderInputField.width * 0.62
            anchors.right: floatInput.right
        }

        QQC2.Slider {
            id: slider

            property bool blockCommit: false
            property bool pendingValue: false

            from: minimum
            to: maximum
            implicitWidth: floatSliderInputField.width * 0.6 > editorContent.maximumControlWidth
                           ? editorContent.maximumControlWidth * 0.65
                           : floatSliderInputField.width * 0.4 - 4
            enabled: lockButton.buttonEnabled
            anchors.right: floatInput.left
            anchors.rightMargin: 4
            handle: Rectangle {
                x: slider.leftPadding + (horizontal ? slider.visualPosition
                                                      * (slider.availableWidth - width)
                                                    : (slider.availableWidth - width) / 2)
                y: slider.topPadding + (horizontal ? (slider.availableHeight - height) / 2
                                                   : slider.visualPosition
                                                     * (slider.availableHeight - height))
                implicitWidth: 20
                implicitHeight: 20
                radius: width / 2
                border.color: enabled ? editorContent.listHighlightColor
                                      : editorContent.itemBackgroundColor
                color: enabled ? (slider.pressed ? editorContent.selectionColor
                                                 : editorContent.listHighlightColor)
                               : editorContent.itemBackgroundColor

                readonly property bool horizontal: slider.orientation === Qt.Horizontal
            }
            background: Rectangle {
                x: slider.leftPadding + (horizontal ? 0 : (slider.availableWidth - width) / 2)
                y: slider.topPadding + (horizontal ? (slider.availableHeight - height) / 2 : 0)
                implicitWidth: horizontal ? 200 : 6
                implicitHeight: horizontal ? 6 : 200
                width: horizontal ? slider.availableWidth : implicitWidth
                height: horizontal ? implicitHeight : slider.availableHeight
                radius: 3
                border.color: enabled ? editorContent.listHighlightColor
                                      : editorContent.itemBackgroundColor
                color: enabled ? editorContent.listHighlightColor : editorContent.itemBackgroundColor
                scale: horizontal && slider.mirrored ? -1 : 1

                readonly property bool horizontal: slider.orientation === Qt.Horizontal
            }

            onPositionChanged:  {
                var newValue = roundNumber((position * (to - from)) + from)
                floatInput.text = newValue
                if (!blockCommit) {
                    // Do not try to commit immediately, if we do not have focus.
                    // Instead, delay the commit until we get the focus. This should ensure
                    // any onEditingFinishes in other fields get executed before slider value
                    // creates its undo command into the stack, thus ensuring the undo stack
                    // is kept in correct order.
                    if (focus) {
                        pendingValue = false
                        tryCommitValue(newValue)
                    } else {
                        pendingValue = true
                    }
                }
            }

            onPressedChanged: {
                // Grab focus if user presses the slider with mouse.
                // Note that if this is changed, pendingValue logic will likely break.
                if (pressed)
                    forceActiveFocus(Qt.MouseFocusReason)
            }

            onFocusChanged: {
                if (focus && pendingValue) {
                    tryCommitValue(value)
                    pendingValue = false
                }
            }
        }

        StyledTextField {
            id: floatInput
            implicitWidth: floatSliderInputField.width * 0.6 > editorContent.maximumControlWidth
                           ? editorContent.maximumControlWidth * 0.34
                           : floatSliderInputField.width * 0.2
            anchors.right: lockButton.left
            anchors.rightMargin: 4
            validator: doubleValidator
            inputMethodHints: Qt.ImhFormattedNumbersOnly
            enabled: lockButton.buttonEnabled
            selectByMouse: true

            onEditingFinished: {
                tryCommitValue(floatInput.text)
            }

            Component.onCompleted: {
                text = roundNumber(slider.value)
            }
        }

        PropertyLockButton {
            id: lockButton
            Layout.alignment: Qt.AlignVCenter
            anchors.right: parent.right
            Layout.maximumWidth: 16
            label: floatSliderInputField.label
        }
    }
}