aboutsummaryrefslogtreecommitdiffstats
path: root/src/labs/stylekit/RangeSlider.qml
blob: 19944ad4aacb447ea080c0c32e9ece557c8ffcf3 (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
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
// Qt-Security score:significant reason:default

import QtQuick
import QtQuick.Templates as T
import Qt.labs.StyleKit
import Qt.labs.StyleKit.impl

T.RangeSlider {
    id: control

    implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
                            first.implicitHandleWidth + leftPadding + rightPadding,
                            second.implicitHandleWidth + leftPadding + rightPadding,
                            implicitContentWidth + leftPadding + rightPadding)
    implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
                             first.implicitHandleHeight + topPadding + bottomPadding,
                             second.implicitHandleHeight + topPadding + bottomPadding,
                             implicitContentHeight + topPadding + bottomPadding)

    leftPadding: styleReaderFirst.leftPadding
    topPadding: styleReaderFirst.topPadding
    rightPadding: styleReaderFirst.rightPadding
    bottomPadding: styleReaderFirst.bottomPadding
    spacing: styleReaderFirst.spacing

    states: [
        /* The delegate logic is moved out of the delegate, to relieve the style
         * developer from having to re-invent it if he changes the delegate. To
         * disable it, 'states' can be set to an empty array from the outside. */
        State {
            when: horizontal
            PropertyChanges  {
                // The width of the handle is fixed. But its margins are used
                // to describe the area within the content area that the handle
                // is allowed to move.
                control.first.handle.x: leftPadding + styleReaderFirst.handle.leftMargin
                   + (first.visualPosition * (availableWidth - styleReaderFirst.handle.leftMargin
                         - styleReaderFirst.handle.rightMargin - first.handle.width))
                // The height of the handle is fixed. But the margins are used to
                // shift its position up or down from the center of the content area.
                control.first.handle.y: topPadding
                    + styleReaderFirst.handle.topMargin - styleReaderFirst.handle.bottomMargin
                    + (availableHeight - first.handle.height) / 2

                control.second.handle.x: leftPadding + styleReaderSecond.handle.leftMargin
                   + (second.visualPosition * (availableWidth - styleReaderSecond.handle.leftMargin
                         - styleReaderSecond.handle.rightMargin - second.handle.width))

                control.second.handle.y: topPadding
                    + styleReaderSecond.handle.topMargin - styleReaderSecond.handle.bottomMargin
                    + (availableHeight - second.handle.height) / 2
            }
        },
        State {
            // Note: we deliberatly flip margins (but not padding) in vertical
            // mode since a vertical slider is logically a flipped horizontal slider.
            when: control.vertical
            PropertyChanges  {
                control.first.handle.x: leftPadding
                   + styleReaderFirst.handle.topMargin - styleReaderFirst.handle.bottomMargin
                   + (availableWidth + first.handle.height) / 2
                control.first.handle.y: topPadding + styleReaderFirst.handle.leftMargin
                   + (first.visualPosition * (availableHeight - styleReaderFirst.handle.leftMargin
                         - styleReaderFirst.handle.rightMargin - first.handle.width))
                control.first.handle.rotation: 90
                control.first.handle.transformOrigin: Item.TopLeft

                control.second.handle.x: leftPadding
                   + styleReaderSecond.handle.topMargin - styleReaderSecond.handle.bottomMargin
                   + (availableWidth + second.handle.height) / 2
                control.second.handle.y: topPadding + styleReaderSecond.handle.leftMargin
                   + (second.visualPosition * (availableHeight - styleReaderSecond.handle.leftMargin
                         - styleReaderSecond.handle.rightMargin - second.handle.width))
                control.second.handle.rotation: 90
                control.second.handle.transformOrigin: Item.TopLeft
            }
        }
    ]

    readonly property StyleKitReader styleReader: styleReaderFirst

    StyleKitControl.controlType: styleReaderFirst.type
    StyleKitReader {
        id: styleReaderFirst
        type: StyleKitReader.Slider
        enabled: control.enabled
        focused: control.activeFocus
        hovered: control.first.hovered
        pressed: control.first.pressed
        palette: control.palette
        vertical: !control.horizontal
    }

    StyleKitReader {
        id: styleReaderSecond
        type: StyleKitReader.Slider
        enabled: control.enabled
        focused: control.activeFocus
        hovered: control.second.hovered
        pressed: control.second.pressed
        palette: control.palette
        vertical: !control.horizontal
    }

    StyleKitReader {
        id: styleReaderIndicator
        type: StyleKitReader.Slider
        enabled: control.enabled
        focused: control.activeFocus
        hovered: control.hovered || control.first.hovered || control.second.hovered
        pressed: control.first.pressed || control.second.pressed
        palette: control.palette
        vertical: !control.horizontal
    }

    first.handle: HandleDelegate {
        parentControl: control
        handleProperties: styleReaderFirst.handle.first
    }

    second.handle: HandleDelegate {
        parentControl: control
        handleProperties: styleReaderSecond.handle.second
    }

    background: BackgroundAndIndicatorDelegate {
        parentControl: control
        indicatorProperties: styleReaderIndicator.indicator
        backgroundProperties: styleReaderIndicator.background
        indicator.firstProgress: control.first.position
        indicator.secondProgress: control.second.position
        vertical: control.vertical
    }
}