Scrollbar.qml 3.93 KB
Newer Older
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

30 31 32 33 34
import Qt 4.6

Item {
    id: bar

35 36
    // public
    
37
    property var flickable
38

39 40 41 42
    // internal

    ItemsViewStyle { id: style }
    
43 44 45 46 47 48 49 50
    property int scrollHeight: height - handle.height

    Rectangle {
        anchors.fill: parent;
        anchors.rightMargin: 1
        anchors.bottomMargin: 1
        color: "transparent"
        border.width: 1;
51
        border.color: style.scrollbarBorderColor;
52 53
    }

54 55
    function moveHandle(viewportPos, updateFlickable) {
	handle.updateFlickable = updateFlickable
56

57 58 59 60 61 62
	if (flickable)
	    handle.y = scrollHeight * Math.min(
		viewportPos / (flickable.viewportHeight - flickable.height),
                1);
	else
	    handle.y = 0;
63

64
	handle.updateFlickable = true
65 66
    }

67 68
    function updateHandle() {
	moveHandle(flickable.viewportY, false);
69
    }
70 71 72

    onFlickableChanged: moveHandle(0, true)

73
    Connection {
74
        sender: flickable
75
        signal: "heightChanged"
76
        script: moveHandle(0, true)
77 78
    }

79 80 81 82 83
    Connection {
        sender: flickable
        signal: "viewportHeightChanged"
        script: updateHandle()
    }
84

85 86 87 88
    Connection {
        sender: flickable
        signal: "positionYChanged"
        script: updateHandle()
89
    }
90 91 92

    onHeightChanged: updateHandle()
    
93 94 95 96 97
    MouseRegion {
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: parent.top
        anchors.bottom: handle.top
98
        onClicked: handle.y = Math.max(0, handle.y - style.scrollbarClickScrollAmount)
99 100 101 102 103 104
    }

    Item {
        id: handle

        anchors.left: parent.left
105
        anchors.leftMargin: 1
106
        anchors.right: parent.right
107
//        anchors.rightMargin: 1
108 109 110 111 112 113 114 115
	height: Math.max(width, bar.height * Math.min(1, flickable.height / flickable.viewportHeight))

	property bool updateFlickable: true
	
	onYChanged: {
	    if (updateFlickable)
		flickable.viewportY = Math.max(0, flickable.viewportHeight * y / bar.height)
	}
116 117

        Rectangle {
118
            width: parent.height - 1
119
            height: parent.width
120
            y: 1 - height
121

122 123
            rotation: 90
            transformOrigin: Item.BottomLeft
124 125

            gradient: Gradient {
126 127
                GradientStop { position: 0.0; color: style.scrollbarGradientStartColor }
                GradientStop { position: 1.0; color: style.scrollbarGradientEndColor }
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
            }
        }

        MouseRegion {
            anchors.fill: parent
            drag.target: parent
            drag.axis: "YAxis"
            drag.minimumY: 0
            drag.maximumY: scrollHeight
        }
    }

    MouseRegion {
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: handle.bottom
        anchors.bottom: parent.bottom
145
        onClicked: handle.y = Math.min(scrollHeight, handle.y + style.scrollbarClickScrollAmount)
146 147
    }
}