Debian Codegraph @

src/imports/Components/Pickers/1.3/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 1.3
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 function disconnectPicker(index) {
67 var pickerItem = model.get(index).pickerModel.pickerItem;
68 if (pickerItem) {
69 pickerItem.onMovingChanged.disconnect(pickerMoving);
70 }
71 }
72
73 Connections {
74 target: row.model
75 function onPickerRemoved(index) { disconnectPicker(index) }
76 }
77
78 objectName: "PickerRow_Positioner";
79
80 Repeater {
81 id: rowRepeater
82 onModelChanged: row.pickerMoving(true)
83 Picker {
84 id: unitPicker
85 objectName: "PickerRow_" + pickerName
86 model: pickerModel
87 enabled: pickerModel.count > 1
88 circular: pickerModel.circular
89 live: false
90 width: pickerModel.pickerWidth
91 height: parent ? parent.height : 0
92
93 style: Rectangle {
94 anchors.fill: parent
95 color: (unitPicker.Positioner.index % 2) ? Qt.rgba(0, 0, 0, 0.03) : Qt.rgba(0, 0, 0, 0.07)
96 }
97 delegate: PickerDelegate {
98 Label {
99 objectName: "PickerRow_PickerLabel" + (pickerModel ? modelData : "")
100 text: pickerModel ? pickerModel.text(modelData) : ""
101 anchors.fill: parent
102 verticalAlignment: Text.AlignVCenter
103 horizontalAlignment: Text.AlignHCenter
104 }
105 Component.onCompleted: {
106 if (pickerModel && pickerModel.autoExtend && (index === (pickerModel.count - 1))) {
107 pickerModel.extend(modelData + 1);
108 }
109 }
110 }
111
112 onSelectedIndexChanged: {
113 if (pickerModel && !pickerModel.resetting) {
114 mainComponent.date = pickerModel.dateFromIndex(selectedIndex);
115 pickerModel.syncModels();
116 }
117 }
118
119 /*
120 Resets the Picker model and updates the new format limits.
121 */
122 function resetPicker() {
123 pickerModel.reset();
124 pickerModel.resetLimits(textSizer, margins);
125 pickerModel.resetCompleted();
126 positionViewAtIndex(pickerModel.indexOf());
127 }
128
129 onReadyChanged: {
130 // make sure positionner is updated when everything is ready
131 if (ready) {
132 positionViewAtIndex(pickerModel.indexOf());
133 }
134 }
135
136 Component.onCompleted: {
137 // update model with the item instance
138 pickerModel.pickerItem = unitPicker;
139 resetPicker();
140 unitPicker.onMovingChanged.connect(pickerMoving.bind(unitPicker));
141 row.pickerMoving(unitPicker.moving);
142 }
143 }
144 }
145 }