Debian Codegraph @

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