Debian Codegraph @

src/imports/Components/Pickers/2.0/PickerRow.qml

raw
1 /*
2 * Copyright 2013 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components
19
20 Row {
21 id: row
22
23 /*
24 Reference to the main composit component holding this row.
25 */
26 property Item mainComponent
27
28 /*
29 The model populating the row.
30 */
31 property alias model: rowRepeater.model
32
33 /*
34 Picker label margins
35 */
36 property real margins: units.gu(1.5)
37
38 /*
39 Reports whether either of the pickers is moving
40 */
41 property bool moving
42
43 // the following functions/properties should be kept private in case the
44 // component is ever decided to be published
45
46 function pickerMoving(isMoving) {
47 if (isMoving === undefined) {
48 isMoving = this.moving;
49 }
50
51 if (isMoving) {
52 row.moving = true;
53 } else {
54 for (var i = 0; i < row.model.count; i++) {
55 var pickerItem = model.get(i).pickerModel.pickerItem;
56 if (!pickerItem) return;
57 if (pickerItem.moving) {
58 row.moving = true;
59 return;
60 }
61 }
62 row.moving = false;
63 }
64 }
65
66 objectName: "PickerRow_Positioner";
67
68 Repeater {
69 id: rowRepeater
70 Picker {
71 id: unitPicker
72 objectName: "PickerRow_" + pickerName
73 model: pickerModel
74 enabled: pickerModel.count > 1
75 circular: pickerModel.circular
76 live: false
77 width: pickerModel.pickerWidth
78 height: parent ? parent.height : 0
79
80 style: Rectangle {
81 anchors.fill: parent
82 color: (unitPicker.Positioner.index % 2) ? Qt.rgba(0, 0, 0, 0.03) : Qt.rgba(0, 0, 0, 0.07)
83 }
84 delegate: PickerDelegate {
85 Label {
86 objectName: "PickerRow_PickerLabel" + (pickerModel ? modelData : "")
87 text: pickerModel ? pickerModel.text(modelData) : ""
88 anchors.fill: parent
89 verticalAlignment: Text.AlignVCenter
90 horizontalAlignment: Text.AlignHCenter
91 }
92 Component.onCompleted: {
93 if (pickerModel && pickerModel.autoExtend && (index === (pickerModel.count - 1))) {
94 pickerModel.extend(modelData + 1);
95 }
96 }
97 }
98
99 onSelectedIndexChanged: {
100 if (pickerModel && !pickerModel.resetting) {
101 mainComponent.date = pickerModel.dateFromIndex(selectedIndex);
102 pickerModel.syncModels();
103 }
104 }
105
106 /*
107 Resets the Picker model and updates the new format limits.
108 */
109 function resetPicker() {
110 pickerModel.reset();
111 pickerModel.resetLimits(textSizer, margins);
112 pickerModel.resetCompleted();
113 positionViewAtIndex(pickerModel.indexOf());
114 }
115
116 onReadyChanged: {
117 // make sure positionner is updated when everything is ready
118 if (ready) {
119 positionViewAtIndex(pickerModel.indexOf());
120 }
121 }
122
123 Component.onCompleted: {
124 // update model with the item instance
125 pickerModel.pickerItem = unitPicker;
126 resetPicker();
127 unitPicker.onMovingChanged.connect(pickerMoving.bind(unitPicker));
128 row.pickerMoving(unitPicker.moving);
129 }
130 }
131 }
132 }