aboutsummaryrefslogtreecommitdiffstats
path: root/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/LuminanceSlider.qml
blob: f121e91f754f3d2a6685dbaa7dd4a278b8566ce5 (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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0 WITH Qt-GPL-exception-1.0

import QtQuick 2.15
import StudioTheme 1.0 as StudioTheme

Item {
    id: root

    property real value: 1
    property real minimum: 0
    property real maximum: 1
    property bool pressed: mouseArea.pressed
    property bool integer: false
    property color color

    signal clicked
    signal moved

    height: StudioTheme.Values.hueSliderHeight

    function updatePos() {
        if (root.maximum > root.minimum) {
            var pos = track.width * (root.value - root.minimum) / (root.maximum - root.minimum)
            return Math.min(Math.max(pos, 0), track.width)
        } else {
            return 0
        }
    }

    Item {
        id: track
        width: parent.width
        height: parent.height

        Rectangle {
            anchors.fill: parent
            border.color: StudioTheme.Values.themeControlOutline
            border.width: StudioTheme.Values.border

            gradient: Gradient {
                orientation: Gradient.Horizontal
                GradientStop { position: 0.000; color: Qt.hsva(root.color.hsvHue, root.color.hsvSaturation, 1, 1) }
                GradientStop { position: 1.000; color: "black" }
            }
        }

        Rectangle {
            id: handle
            width: StudioTheme.Values.hueSliderHandleWidth
            height: track.height + 4
            anchors.verticalCenter: parent.verticalCenter
            color: "transparent"
            radius: 2
            border.color: "black"
            border.width: 1
            x: root.updatePos() - handle.width * 0.5
            y: 2
            z: 1

            Rectangle {
                anchors.fill: parent
                anchors.margins: 1
                color: Qt.hsva(root.color.hsvHue, root.color.hsvSaturation, root.color.hsvValue, 1)
                radius: 1
                border.color: "white"
                border.width: 1
            }
        }

        MouseArea {
            id: mouseArea
            anchors.fill: parent
            anchors.margins: -StudioTheme.Values.hueSliderHandleWidth * 0.5
            preventStealing: true

            function calculateValue() {
                var halfHandle = StudioTheme.Values.hueSliderHandleWidth * 0.5
                var handleX = Math.max(0, Math.min(mouseArea.mouseX - halfHandle, parent.width))
                var realValue = (root.maximum - root.minimum) * handleX / parent.width + root.minimum
                root.value = root.integer ? Math.round(realValue) : realValue
                root.moved()
            }

            onPressed: mouseArea.calculateValue()
            onReleased: root.clicked()
            onPositionChanged: {
                if (mouseArea.pressed)
                    mouseArea.calculateValue()
            }
        }

    }
}