Debian Codegraph @

src/qml/CustomPickers/1.0/PickerPanel.qml

raw
1 /*
2 * Copyright 2015 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 pragma Singleton
18 import QtQuick 2.4
19 import QtQuick.Window 2.0
20 import Lomiri.Components 1.3
21 import Lomiri.Components.ListItems 1.3
22 import Lomiri.Components.Popups 1.3
23
24 /*!
25 \qmltype PickerPanel
26 \inqmlmodule Lomiri.Components
27 \ingroup ubuntu-pickers
28 \brief Provides a panel for opening a DatePicker in place of the input panel or
29 as Popover, depending on the form factor.
30
31 PickerPanel is a singleton component designed to open a DatePicker in the input panel
32 area or in a Popover, depending on the form factor, following the design guides
33 on date pickers.
34 \qml
35 import QtQuick 2.4
36 import Lomiri.Components 1.3
37
38 MainWindow {
39 width: units.gu(40)
40 height: units.gu(71)
41
42 Page {
43 title: "PickerPanel"
44 Button {
45 id: dateButton
46 property date date: new Date()
47 text: Qt.formatDateTime(date, "yyyy/MMMM")
48 onClicked: PickerPanel.openDatePicker(dateButton, "date", "Years|Months")
49 }
50 }
51 }
52 \endqml
53
54 The opened panel is closed automatically when the user taps/presses outside
55 of the panel or Popover area.
56 */
57
58 Item {
59
60 /*!
61 The function opens a DatePicker component in the input method area or in a
62 popover, depending on the availability of an input method provider in the
63 system and whether the size of the main screen width/height defines a phone
64 form factor. The picked date will be read from and reported to the \a property
65 of the \a caller as date type. This implies that the caller must have defined
66 the given property of type date.
67
68 On failure, the function returns null. On success, the returned object has the
69 following properties:
70 \code
71 Item {
72 property DatePicker picker
73 property string pickerMode
74 property date date
75 property Item caller
76 property string callerProperty
77
78 signal closed()
79 }
80 \endcode
81
82 \table
83 \header
84 \li Property
85 \li Description
86 \row
87 \li \b picker
88 \li instance of the DatePicker component shown in the panel/popup
89 \row
90 \li \b pickerMode
91 \li represents the \l DatePicker::mode to be used. This is an optional
92 parameter and if not defined, the default mode will be used.
93 \row
94 \li \b date
95 \li represents the date selected
96 \row
97 \li \b caller
98 \li the instance of the component opening the panel
99 \row
100 \li \b callerProperty
101 \li the property of the caller holding the date value which will be
102 updated by the picker.
103 \header
104 \li Signal
105 \li Description
106 \row
107 \li closed()
108 \li the signal is emitted when the panel or popover gets closed. The
109 signal is handy when actions are performed upon panel close.
110 \endtable
111 */
112 function openDatePicker(caller, property, mode) {
113 if (mode === undefined) {
114 mode = "Years|Months|Days";
115 }
116 var params = {
117 "date": caller[property],
118 "pickerMode": mode,
119 "callerProperty": property
120 }
121
122 // hide OSK if eventually open
123 if (Qt.inputMethod.visible) {
124 Qt.inputMethod.hide();
125 }
126
127 if (!internal.isPhone) {
128 // we have no input panel defined, or the therefore we show the picker in a Popover
129 return internal.openPopover(caller, params);
130 }
131 // OSK panel
132 return internal.openPanel(caller, params);
133 }
134
135 QtObject {
136 id: internal
137 objectName: "PickerPanel_Internals"
138
139 property bool formFactorPhone: Screen.width <= units.gu(40) && Screen.height <= units.gu(71)
140 // present in a property so we can test it in
141 property bool isPhone: false
142 Component.onCompleted: isPhone = formFactorPhone && (QuickUtils.inputMethodProvider !== "")
143
144 function openPopover(caller, params) {
145 var popover = PopupUtils.open(datePickerPopover, caller, params);
146 // Avoid confusing TypeError if popover couldn't be created
147 if (popover)
148 popover.parent = QuickUtils.rootItem(caller);
149 return popover;
150 }
151 function openPanel(caller, params) {
152 params["caller"] = caller;
153 var panel = datePickerPanel.createObject(QuickUtils.rootItem(caller), params);
154 return panel;
155 }
156 }
157
158 // popover
159 Component {
160 id: datePickerPopover
161 Popover {
162 property alias picker: picker
163 property alias date: picker.date
164 property alias pickerMode: picker.mode
165 property string callerProperty
166
167 signal closed()
168
169 contentWidth: frame.width
170 contentHeight: frame.height
171
172 Item {
173 id: frame
174 width: picker.width + units.gu(4)
175 height: picker.height + units.gu(4)
176 DatePicker {
177 id: picker
178 StyleHints {
179 highlightBackgroundColor: theme.palette.normal.raised
180 highlightColor: LomiriColors.jet
181 }
182 anchors.centerIn: parent
183 Binding {
184 target: caller
185 property: callerProperty
186 when: callerProperty != undefined
187 value: picker.date
188 }
189 }
190 }
191 Component.onDestruction: closed()
192 }
193 }
194
195 // OSK panel
196 Component {
197 id: datePickerPanel
198 Rectangle {
199 property alias picker: picker
200 property alias date: picker.date
201 property alias pickerMode: picker.mode
202 property string callerProperty
203 property Item caller
204
205 signal closed()
206
207 id: panel
208 // no additional styling is needed
209 color: theme.palette.normal.overlaySecondaryText
210 width: parent.width
211 height: Qt.inputMethod.keyboardRectangle.height > 0 ? Qt.inputMethod.keyboardRectangle.height : units.gu(26)
212 y: parent.height
213
214 ThinDivider { anchors.bottom: parent.top }
215 DatePicker {
216 id: picker
217 StyleHints {
218 highlightBackgroundColor: theme.palette.normal.raised
219 highlightColor: LomiriColors.jet
220 }
221 anchors {
222 fill: panel
223 margins: units.gu(2)
224 }
225 Binding {
226 target: caller
227 property: callerProperty
228 when: callerProperty != undefined
229 value: picker.date
230 }
231 }
232
233 InverseMouseArea {
234 anchors.fill: parent
235 onPressed: panel.state = ''
236 }
237
238 Component.onCompleted: state = 'opened'
239
240 states: [
241 State {
242 name: 'opened'
243 PropertyChanges {
244 target: panel
245 y: parent.height - height
246 }
247 }
248 ]
249 transitions: [
250 Transition {
251 from: ''
252 to: 'opened'
253 LomiriNumberAnimation {
254 target: panel
255 property: 'y'
256 }
257 },
258 Transition {
259 from: 'opened'
260 to: ''
261 SequentialAnimation {
262 LomiriNumberAnimation {
263 target: panel
264 property: 'y'
265 }
266 ScriptAction {
267 script: {
268 closed();
269 panel.destroy();
270 }
271 }
272 }
273 }
274 ]
275 }
276 }
277 }