Debian Codegraph @

src/qml/CustomPickers/1.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 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 onPickerRemoved: 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: theme.palette.normal.overlayText
96 opacity: (unitPicker.Positioner.index % 2) ? 0.03 : 0.07
97 }
98 delegate: PickerDelegate {
99 Label {
100 objectName: "PickerRow_PickerLabel" + (pickerModel ? modelData : "")
101 text: pickerModel ? pickerModel.text(modelData) : ""
102 anchors.fill: parent
103 verticalAlignment: Text.AlignVCenter
104 horizontalAlignment: Text.AlignHCenter
105 }
106 Component.onCompleted: {
107 if (pickerModel && pickerModel.autoExtend && (index === (pickerModel.count - 1))) {
108 pickerModel.extend(modelData + 1);
109 }
110 }
111 }
112
113 onSelectedIndexChanged: {
114 if (pickerModel && !pickerModel.resetting) {
115 mainComponent.date = pickerModel.dateFromIndex(selectedIndex);
116 pickerModel.syncModels();
117 }
118 }
119
120 /*
121 Resets the Picker model and updates the new format limits.
122 */
123 function resetPicker() {
124 pickerModel.reset();
125 pickerModel.resetLimits(textSizer, margins);
126 pickerModel.resetCompleted();
127 positionViewAtIndex(pickerModel.indexOf());
128 }
129
130 Component.onCompleted: {
131 // update model with the item instance
132 pickerModel.pickerItem = unitPicker;
133 unitPicker.onMovingChanged.connect(pickerMoving.bind(unitPicker));
134 row.pickerMoving(unitPicker.moving);
135 }
136 }
137 }
138 }