Debian Codegraph @

src/imports/Components/Popups/1.3/Popover.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 import QtQuick 2.4
18 import "internalPopupUtils.js" as InternalPopupUtils
19 import Lomiri.Components 1.3
20
21 /*!
22 \qmltype Popover
23 \inherits PopupBase
24 \inqmlmodule Lomiri.Components.Popups 1.0
25 \ingroup lomiri-popups
26 \brief A popover allows an application to present additional content without changing the view.
27 A popover has a fixed width and automatic height, depending on is contents.
28 It can be closed by clicking anywhere outside of the popover area.
29
30 \l {https://design.lomiri.com/apps/building-blocks/dialog#popover}{See also the Design Guidelines on Popovers}.
31
32 Example:
33 \qml
34 import QtQuick 2.4
35 import Lomiri.Components 1.3
36 import Lomiri.Components.Popups 1.3
37 import Lomiri.Components.ListItems 1.3 as Old_ListItem
38 Rectangle {
39 color: theme.palette.normal.background
40 width: units.gu(80)
41 height: units.gu(80)
42 Component {
43 id: popoverComponent
44
45 Popover {
46 id: popover
47 Column {
48 id: containerLayout
49 anchors {
50 left: parent.left
51 top: parent.top
52 right: parent.right
53 }
54
55 // there is no equivalent yet to ListItem.Header
56 Old_ListItem.Header { text: "Standard list items" }
57
58 ListItem {
59 // shall specify the height when Using ListItemLayout inside ListItem
60 height: somethingLayout.height + (divider.visible ? divider.height : 0)
61 ListItemLayout {
62 id: somethingLayout
63 title.text: "Do somethings"
64 }
65 onClicked: console.log("clicked on ListItem with onClicked implemented")
66 }
67 ListItem {
68 // shall specify the height when Using ListItemLayout inside ListItem
69 height: somethingElseLayout.height + (divider.visible ? divider.height : 0)
70 ListItemLayout {
71 id: somethingElseLayout
72 title.text: "Do somethings"
73 subtitle.text: "else"
74 }
75 }
76 ListItem {
77 // shall specify the height when Using ListItemLayout inside ListItem
78 height: closeBtn.height + (divider.visible ? divider.height : 0)
79 Button {
80 id: closeBtn
81 text: "Close button"
82 onClicked: PopupUtils.close(popover);
83 }
84 }
85 }
86 }
87 }
88 Button {
89 id: popoverButton
90 anchors.centerIn: parent
91 text: "open"
92 onClicked: PopupUtils.open(popoverComponent, popoverButton)
93 }
94 }
95 \endqml
96 */
97 PopupBase {
98 id: popover
99
100 /*!
101 \qmlproperty list<Object> container
102 Content will be put inside the foreround of the Popover.
103 */
104 default property alias container: containerItem.data
105
106 /*!
107 \qmlproperty real contentWidth
108 Use this property to override the default content width.
109 */
110 property alias contentWidth: foreground.width
111 /*!
112 \qmlproperty real contentHeight
113 Use this property to override the default content height.
114 */
115 property alias contentHeight: foreground.height
116
117 /*!
118 The Item such as a \l Button that the user interacted with to open the Dialog.
119 This property will be used for the automatic positioning of the Dialog next to
120 the caller, if possible.
121 */
122 property Item caller
123
124 /*!
125 The property holds the item to which the pointer should be anchored to.
126 This can be same as the caller or any child of the caller. By default the
127 property is set to caller.
128 */
129 property Item pointerTarget: caller
130
131 /*!
132 The property holds the margins from the popover's dismissArea. The property
133 is themed.
134 */
135 property real edgeMargins: foreground.square ? 0 : units.gu(2)
136
137 /*!
138 The property holds the margin from the popover's caller. The property
139 is themed.
140 */
141 property real callerMargin: 0
142
143 /*!
144 The property drives the automatic closing of the Popover when user taps
145 on the dismissArea. The default behavior is to close the Popover, therefore
146 set to true.
147
148 When set to false, closing the Popover is the responsibility of the caller.
149 Also, the mouse and touch events are not blocked from the dismissArea.
150 */
151 property bool autoClose: true
152
153 /*!
154 \qmlproperty Component foregroundStyle
155 Exposes the style property of the \l StyledItem contained in the Popover.
156 Refer to \l StyledItem how to use it.
157 */
158 property alias foregroundStyle: foreground.style
159
160 /*!
161 Make the popover visible. Reparent to the background area object first if needed.
162 Only use this function if you handle memory management. Otherwise use
163 PopupUtils.open() to do it automatically.
164 */
165 function show() {
166 /* Cannot call parent's show() however PopupBase::show()
167 does not do anything useful to us.
168
169 https://bugreports.qt-project.org/browse/QTBUG-25942
170 http://qt-project.org/forums/viewthread/19577
171 */
172 visible = true;
173 foreground.show();
174 foreground.forceActiveFocus();
175 }
176
177 /*!
178 Hide the popover.
179 Only use this function if you handle memory management. Otherwise use
180 PopupUtils.close() to do it automatically.
181 */
182 function hide() {
183 foreground.hide();
184 }
185
186 Component.onCompleted: foreground.hideCompleted.connect(popover.__makeInvisible)
187 /*!
188 \internal
189 */
190 function __makeInvisible() {
191 visible = false;
192 }
193
194 QtObject {
195 id: internal
196 property bool portrait: width < height
197
198 // private
199 function updatePosition() {
200 if (pointerTarget && pointerTarget.parent && popover.parent)
201 popover.y = (popover.parent.height - popover.height) / 2;
202 var pos = new InternalPopupUtils.CallerPositioning(foreground, pointer, dismissArea, caller, pointerTarget, edgeMargins, callerMargin);
203 pos.auto();
204 }
205 }
206
207 __foreground: foreground
208 __eventGrabber.enabled: autoClose
209 __closeOnDismissAreaPress: true
210
211 y: parent ? (parent.height - height) / 2 : 0
212
213 /*!
214 \qmlproperty ThemeSettings theme
215 \since Lomiri.Components 1.3
216 Configure the theme of the Popover foreground and all its subcomponents.
217 \sa StyledItem::theme, Dialog::theme
218 */
219 property alias theme: foreground.theme
220
221 /*!
222 \qmlproperty string styleName
223 The style name of the foreground of the Popover.
224 \since Lomiri.Components 1.3
225 \sa StyledItem::styleName
226 */
227 property alias styleName: foreground.styleName
228
229 /*!
230 \qmlproperty Component style
231 The style of the foreground of the Popover.
232 This property takes precedence over \l styleName
233 \since Lomiri.Components 1.3
234 \sa StyledItem::style
235 */
236 property alias style: foreground.style
237 StyledItem {
238 id: foreground
239 objectName: "popover_foreground"
240 Keys.onEscapePressed: hideCompleted()
241
242 //styling properties
243 property real minimumWidth: units.gu(40)
244
245 property real maxWidth: dismissArea ? (internal.portrait ? dismissArea.width : dismissArea.width * 3/4) : 0.0
246 property real maxHeight: dismissArea ? (internal.portrait ? dismissArea.height * 3/4 : dismissArea.height) : 0.0
247 width: Math.min(minimumWidth, maxWidth)
248 height: containerItem.height
249
250 Item {
251 id: containerItem
252 parent: foreground.__styleInstance.contentItem
253 anchors {
254 left: parent.left
255 top: parent.top
256 right: parent.right
257 }
258 height: childrenRect.height
259
260 // put the PopupContext inside the container to save one step
261 // in the context lookup
262 PopupContext {
263 id: popupContext
264 objectName: popover.objectName + "PopupContext"
265 active: foreground.visible
266 }
267 }
268
269 onWidthChanged: internal.updatePosition()
270 onHeightChanged: internal.updatePosition()
271
272 property point target: Qt.point(pointer.x - x, pointer.y - y)
273 property string direction: pointer.direction
274 property bool clipContent: true
275 property bool square: popover.hasOwnProperty("square") ? popover.square : false
276
277 signal show()
278 signal hide()
279 signal showCompleted()
280 signal hideCompleted()
281
282 styleName: "PopoverForegroundStyle"
283 }
284
285 QtObject {
286 id: pointer
287
288 /* Input variables for InternalPopupUtils are the properties:
289 - horizontalMargin
290 - verticalMargin
291 - size
292
293 Output variables of InternalPopupUtils are the properties:
294 - x
295 - y
296 - direction
297 */
298
299 property real arrowSize: units.dp(15)
300 property real cornerSize: units.dp(11)
301
302 /* Minimum distance between the top or bottom of the popup and
303 the tip of the pointer when the direction is left or right.
304 */
305 property real horizontalMargin: arrowSize/2.0 + cornerSize
306 /* Minimum distance between the left or right of the popup and
307 the tip of the pointer when the direction is up or down.
308 */
309 property real verticalMargin: arrowSize/2.0 + cornerSize
310 /* Either:
311 - distance between the left or right of the popup and the tip
312 of the pointer when the direction is left or right.
313 - distance between the top or bottom of the popup and the tip
314 of the pointer when the direction is up or down.
315 */
316 property real size: units.dp(6)
317
318 property real x
319 property real y
320 property string direction
321 }
322
323
324 /*! \internal */
325 onCallerChanged: internal.updatePosition()
326 /*! \internal */
327 onPointerTargetChanged: internal.updatePosition()
328 /*! \internal */
329 onWidthChanged: internal.updatePosition()
330 /*! \internal */
331 onHeightChanged: internal.updatePosition()
332 /*! \internal */
333 onRotatingChanged: hide()
334
335 Connections {
336 target: pointerTarget
337 function onXChanged() { internal.updatePosition() }
338 function onYChanged() { internal.updatePosition() }
339 function onWidthChanged() { internal.updatePosition() }
340 function onHeightChanged() { internal.updatePosition() }
341 }
342 }