Readability integration - first draft
[quicknewsreader] / qml / QuickNewsReader / content / view / ScrollBar.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** Copyright (C) 2012 Christophe CHAPUIS <chris.chapuis _at_ gmail _dot_ com>
8 **
9 ** This package is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation; either version 2 of the License, or
12 ** (at your option) any later version.
13 **
14 ** This package is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this package; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22 **
23 ****************************************************************************/
24
25 import QtQuick 1.0
26
27 Item {
28     id: container
29
30     property variant scrollArea
31     property variant orientation: Qt.Vertical
32
33     opacity: 0
34
35     function position()
36     {
37         var ny = 0;
38         if (container.orientation == Qt.Vertical)
39             ny = scrollArea.visibleArea.yPosition * container.height;
40         else
41             ny = scrollArea.visibleArea.xPosition * container.width;
42         if (ny > 2) return ny; else return 2;
43     }
44
45     function size()
46     {
47         var nh, ny;
48
49         if (container.orientation == Qt.Vertical)
50             nh = scrollArea.visibleArea.heightRatio * container.height;
51         else
52             nh = scrollArea.visibleArea.widthRatio * container.width;
53
54         if (container.orientation == Qt.Vertical)
55             ny = scrollArea.visibleArea.yPosition * container.height;
56         else
57             ny = scrollArea.visibleArea.xPosition * container.width;
58
59         if (ny > 3) {
60             var t;
61             if (container.orientation == Qt.Vertical)
62                 t = Math.ceil(container.height - 3 - ny);
63             else
64                 t = Math.ceil(container.width - 3 - ny);
65             if (nh > t) return t; else return nh;
66         } else return nh + ny;
67     }
68
69     Rectangle { anchors.fill: parent; color: "Black"; opacity: 0.3 }
70
71     BorderImage {
72         source: "../images/scrollbar.png"
73         border { left: 1; right: 1; top: 1; bottom: 1 }
74         x: container.orientation == Qt.Vertical ? 2 : position()
75         width: container.orientation == Qt.Vertical ? container.width - 4 : size()
76         y: container.orientation == Qt.Vertical ? position() : 2
77         height: container.orientation == Qt.Vertical ? size() : container.height - 4
78     }
79
80     states: State {
81         name: "visible"
82         when: container.orientation == Qt.Vertical ? scrollArea.movingVertically : scrollArea.movingHorizontally
83         PropertyChanges { target: container; opacity: 1.0 }
84     }
85
86     transitions: Transition {
87         from: "visible"; to: ""
88         NumberAnimation { properties: "opacity"; duration: 600 }
89     }
90 }