Debian Codegraph @

src/imports/Components/Pickers/1.2/PickerPanel.qml

raw
1 /*
2 * Copyright 2014 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.2
21 import Lomiri.Components.ListItems 1.0
22 import Lomiri.Components.Popups 1.0
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 1.2
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 popover.parent = QuickUtils.rootItem(null);
147 return popover;
148 }
149 function openPanel(caller, params) {
150 params["caller"] = caller;
151 var panel = datePickerPanel.createObject(QuickUtils.rootItem(null), params);
152 return panel;
153 }
154 }
155
156 // popover
157 Component {
158 id: datePickerPopover
159 Popover {
160 property alias picker: picker
161 property alias date: picker.date
162 property alias pickerMode: picker.mode
163 property string callerProperty
164
165 signal closed()
166
167 contentWidth: frame.width
168 contentHeight: frame.height
169
170 Item {
171 id: frame
172 width: picker.width + units.gu(4)
173 height: picker.height + units.gu(4)
174 DatePicker {
175 id: picker
176 anchors.centerIn: parent
177 Binding {
178 target: caller
179 property: callerProperty
180 when: callerProperty != undefined
181 value: picker.date
182 }
183 }
184 }
185 Component.onDestruction: closed()
186 }
187 }
188
189 // OSK panel
190 Component {
191 id: datePickerPanel
192 Rectangle {
193 property alias picker: picker
194 property alias date: picker.date
195 property alias pickerMode: picker.mode
196 property string callerProperty
197 property Item caller
198
199 signal closed()
200
201 id: panel
202 // no additional styling is needed
203 color: Theme.palette.normal.overlay
204 width: parent.width
205 height: Qt.inputMethod.keyboardRectangle.height > 0 ? Qt.inputMethod.keyboardRectangle.height : units.gu(26)
206 y: parent.height
207
208 ThinDivider { anchors.bottom: parent.top }
209 DatePicker {
210 id: picker
211 anchors {
212 fill: panel
213 margins: units.gu(2)
214 }
215 Binding {
216 target: caller
217 property: callerProperty
218 when: callerProperty != undefined
219 value: picker.date
220 }
221 }
222
223 InverseMouseArea {
224 anchors.fill: parent
225 onPressed: panel.state = ''
226 }
227
228 Component.onCompleted: state = 'opened'
229
230 states: [
231 State {
232 name: 'opened'
233 PropertyChanges {
234 target: panel
235 y: parent.height - height
236 }
237 }
238 ]
239 transitions: [
240 Transition {
241 from: ''
242 to: 'opened'
243 LomiriNumberAnimation {
244 target: panel
245 property: 'y'
246 }
247 },
248 Transition {
249 from: 'opened'
250 to: ''
251 SequentialAnimation {
252 LomiriNumberAnimation {
253 target: panel
254 property: 'y'
255 }
256 ScriptAction {
257 script: {
258 closed();
259 panel.destroy();
260 }
261 }
262 }
263 }
264 ]
265 }
266 }
267 }