~dpniel/ubuntu-calendar-app/ScrollView2

56.1.4 by Riccardo Padovani
Implemented DatePicker. Thanks to Michael Zanetti
1
/*
400.1.2 by mihirsoni-123
updated copyright year
2
 * Copyright (C) 2013-2014 Canonical Ltd
400.1.1 by mihirsoni-123
Added copyright header comments in all the pages
3
 *
4
 * This file is part of Ubuntu Calendar App
5
 *
6
 * Ubuntu Calendar App is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License version 3 as
8
 * published by the Free Software Foundation.
9
 *
10
 * Ubuntu Calendar App is distributed in the hope that it will be useful,
56.1.4 by Riccardo Padovani
Implemented DatePicker. Thanks to Michael Zanetti
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
463.1.1 by Akiva Avraham
Updated library imports to qtquick-2.3
19
import QtQuick 2.3
422.5.1 by Akiva Avraham
Upgraded component library imports
20
import Ubuntu.Components 1.1
21
import Ubuntu.Components.ListItems 1.0 as ListItems
56.1.4 by Riccardo Padovani
Implemented DatePicker. Thanks to Michael Zanetti
22
23
Item {
24
    id: root
25
    property int min: 0
26
    property int max: 10
27
    property variant value: model.get(listView.currentIndex) !== undefined ? model.get(listView.currentIndex).modelData : ""
28
    property alias model: listView.model
29
    property alias labelText: label.text
30
    property alias currentIndex: listView.currentIndex
31
32
    ListModel {
33
        id: defaultModel
34
    }
35
36
    Component.onCompleted: {
37
        var oldIndex = currentIndex
38
        for (var i = 0; i < (max+1)-min; ++i) {
39
            defaultModel.append({modelData: root.min + i})
40
        }
41
        listView.highlightMoveDuration = 0
42
        currentIndex = oldIndex
43
        listView.highlightMoveDuration = 300
44
    }
45
46
    onMinChanged: {
47
        if (defaultModel.get(0) === undefined) {
48
            return;
49
        }
50
51
        var oldMin = defaultModel.get(0).modelData
52
53
        while (oldMin > min) {
54
            defaultModel.insert(0, {modelData: --oldMin })
55
        }
56
        while (oldMin < min) {
57
            defaultModel.remove(0)
58
            ++oldMin
59
        }
60
    }
61
62
    onMaxChanged: {
63
        if (defaultModel.get(defaultModel.count - 1) === undefined) {
64
            return;
65
        }
66
67
        var oldMax = defaultModel.get(defaultModel.count - 1).modelData
68
69
        while (max < oldMax) {
70
            defaultModel.remove(defaultModel.count - 1);
71
            --oldMax;
72
        }
73
        while (max > oldMax) {
74
            defaultModel.insert(defaultModel.count, {modelData: ++oldMax})
75
        }
76
    }
77
78
    Item {
79
        id: labelRect
80
        anchors {
81
            left: parent.left
82
            top: parent.top
83
            right: parent.right
84
        }
85
        height: units.gu(5)
86
87
        Label {
88
            id: label
89
            anchors.centerIn: parent
90
        }
91
        ListItems.Divider {
92
            anchors {
93
                left: parent.left
94
                bottom: parent.bottom
95
                right: parent.right
96
            }
97
        }
98
    }
99
100
    PathView {
101
        id: listView
102
        model: defaultModel
103
        anchors.fill: parent
104
        anchors.topMargin: labelRect.height
105
        pathItemCount: listView.height / highlightItem.height + 1
106
        preferredHighlightBegin: 0.5
107
        preferredHighlightEnd: 0.5
108
        clip: true
109
110
        delegate: ListItems.Standard {
111
            width: parent.width
112
            highlightWhenPressed: false
113
            Label {
114
                anchors.centerIn: parent
115
                text: modelData
116
            }
117
            onClicked: listView.currentIndex = index
118
        }
119
        property int contentHeight: pathItemCount * highlightItem.height
120
        path: Path {
121
            startX: listView.width / 2; startY: -(listView.contentHeight - listView.height) / 2
122
            PathLine { x: listView.width / 2; y: listView.height + (listView.contentHeight - listView.height) / 2 }
123
        }
124
        highlight: Rectangle {
125
            width: parent.width
126
            height: units.gu(6)
127
            property color baseColor: "#dd4814"
128
            gradient: Gradient {
129
                GradientStop {
130
                    position: 0.00;
131
                    color: Qt.lighter(baseColor, 1.3);
132
                }
133
                GradientStop {
134
                    position: 1.0;
135
                    color: baseColor;
136
                }
137
            }
138
        }
139
        ListItems.Divider {
140
            anchors {
141
                left: parent.left
142
                bottom: parent.bottom
143
                right: parent.right
144
            }
145
        }
146
    }
147
}