summaryrefslogtreecommitdiff
path: root/SDL_Core/src/components/qt_hmi/References/Look/Widgets/ScrollBar.qml
blob: b1f67d043c71ce234cf02cdce456a51dc01fcd45 (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
// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 2.0

Item {
    id: scrollbar
    property variant target

    clip: true

    //anchors {top: target.top; bottom: target.bottom; right: target.right }
    visible: (track.height == slider.height) ? false : true //TODO: !visible -> width: 0 (but creates a binding loop)

    Timer {
        property int scrollAmount

        id: timer
        repeat: true
        interval: 20
        onTriggered: {
            target.contentY = Math.max(
                0, Math.min(
                    target.contentY + scrollAmount,
                    target.contentHeight - target.height));
        }
    }

    MouseArea {
        anchors.fill: scrollbar
        onPressed: {
            timer.scrollAmount = target.height * (mouseY < slider.y ? -1 : 1)       // scroll by a page
            timer.running = true;
        }
        onReleased: {
            timer.running = false;
        }
    }

    Rectangle {
        id: track

        color: "red"
        opacity: 0.3
        radius: 2
        smooth: true

        anchors.top: scrollbar.top
        anchors.bottom: scrollbar.bottom
        anchors.horizontalCenter: scrollbar.horizontalCenter
        width: 3
    }

    Rectangle {
        id:slider

        width: scrollbar.width
        color: "red"
        opacity: 0.7
        radius: 4
        smooth: true

        height: Math.min(target.height / target.contentHeight * track.height, track.height)
        y: target.visibleArea.yPosition * track.height

        MouseArea {
            anchors.fill: parent
            drag.target: parent
            drag.axis: Drag.YAxis
            drag.minimumY: 0
            drag.maximumY: track.height - height

            onPositionChanged: {
                if (pressedButtons == Qt.LeftButton) {
                    target.contentY = slider.y * target.contentHeight / track.height
                }
            }
        }
    }
}