summaryrefslogtreecommitdiff
path: root/metawatch/qml/com/javispedro/sowatch/metawatch/MWListView.qml
blob: e16869fb2c6c7811905913a8c3bc93f516bafd12 (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
import Qt 4.7

ListView {
	id: list

	property bool selectable: true
	property bool indicator: true

	interactive: false
	highlightFollowsCurrentItem: false
	keyNavigationWraps: false
	boundsBehavior: Flickable.StopAtBounds
	flickableDirection: Flickable.VerticalFlick

	property real currentItemTop: currentItem !== null ? currentItem.y - contentY : 0
	property real currentItemBottom: currentItem !== null ? currentItemTop + currentItem.height : 0

	function scrollDown() {
		if (count == 0) {
			return; // No items
		}
		if (selectable) {
			if (currentIndex === -1) {
				// If no item is selected, select the first one.
				currentItem = 0;
				return;
			}
			if (currentIndex < count && currentItemBottom < height) {
				// If the next item is visible, highlight it
				incrementCurrentIndex();
			}
			if (currentItemBottom >= height) {
				// If the next item now is still not visible, scroll
				contentY += 96/3;
			}
		}
	}

	function scrollUp() {
		if (count == 0) {
			return; // No items
		}
		if (selectable) {
			if (currentIndex === -1) {
				// If no item is selected, select the last one.
				currentIndex = count - 1;
				return;
			}
			if (currentIndex >= 0 && currentItemTop > 0) {
				var prevContentY = contentY;
				// If the previous item is visible, highlight it
				decrementCurrentIndex();
				// ListView will "smoothtly scroll the list" even if hightlightFollowsCurrentItem is false,
				// so we have to add the following ugly workaround:
				contentY = prevContentY;
			}
			if (currentItemTop <= 0) {
				// If the previous item now is still not visible, scroll
				var newContentY = contentY - 96/3;

				if (newContentY < 0) {
					contentY = 0; // Never overscroll.
				} else {
					contentY = newContentY;
				}
			}
		}
	}

	function scrollTop() {
		if (count == 0) {
			return;
		}
		if (selectable) {
			currentIndex = 0;
		}
		positionViewAtIndex(0, ListView.Beginning);

	}

	Rectangle {
		id: indicatorCont
		visible: list.indicator && (list.contentHeight > list.height)
		anchors.top: parent.top
		anchors.right: parent.right
		anchors.bottom: parent.bottom
		width: 4

		color: "white"

		Rectangle {
			id: indicatorRect

			property int minHeight: 10

			anchors.right: parent.right
			anchors.left: parent.left
			anchors.leftMargin: 1

			y: list.visibleArea.yPosition * indicatorCont.height
			height: Math.max(minHeight, list.visibleArea.heightRatio * indicatorCont.height)

			color: "black"
		}
	}
}