~chris.gagnon/ubuntu-calendar-app/fix_1293489

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