Debian Codegraph @

src/imports/Components/Popups/1.2/Popover.qml

raw
1 /*
2 * Copyright 2012 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.2
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.2
36 import Lomiri.Components.ListItems 1.0 as ListItem
37 import Lomiri.Components.Popups 1.0
38
39 Rectangle {
40 color: Theme.palette.normal.background
41 width: units.gu(80)
42 height: units.gu(80)
43 Component {
44 id: popoverComponent
45
46 Popover {
47 id: popover
48 Column {
49 id: containerLayout
50 anchors {
51 left: parent.left
52 top: parent.top
53 right: parent.right
54 }
55 ListItem.Header { text: "Standard list items" }
56 ListItem.Standard { text: "Do something" }
57 ListItem.Standard { text: "Do something else" }
58 ListItem.Header { text: "Buttons" }
59 ListItem.SingleControl {
60 highlightWhenPressed: false
61 control: Button {
62 text: "Do nothing"
63 anchors {
64 fill: parent
65 margins: units.gu(1)
66 }
67 }
68 }
69 ListItem.SingleControl {
70 highlightWhenPressed: false
71 control: Button {
72 text: "Close"
73 anchors {
74 fill: parent
75 margins: units.gu(1)
76 }
77 onClicked: PopupUtils.close(popover)
78 }
79 }
80 }
81 }
82 }
83 Button {
84 id: popoverButton
85 anchors.centerIn: parent
86 text: "open"
87 onClicked: PopupUtils.open(popoverComponent, popoverButton)
88 }
89 }
90 \endqml
91 */
92 PopupBase {
93 id: popover
94
95 /*!
96 \qmlproperty list<Object> container
97 Content will be put inside the foreround of the Popover.
98 */
99 default property alias container: containerItem.data
100
101 /*!
102 \qmlproperty real contentWidth
103 Use this property to override the default content width.
104 */
105 property alias contentWidth: foreground.width
106 /*!
107 \qmlproperty real contentHeight
108 Use this property to override the default content height.
109 */
110 property alias contentHeight: foreground.height
111
112 /*!
113 The Item such as a \l Button that the user interacted with to open the Dialog.
114 This property will be used for the automatic positioning of the Dialog next to
115 the caller, if possible.
116 */
117 property Item caller
118
119 /*!
120 The property holds the item to which the pointer should be anchored to.
121 This can be same as the caller or any child of the caller. By default the
122 property is set to caller.
123 */
124 property Item pointerTarget: caller
125
126 /*!
127 The property holds the margins from the popover's dismissArea. The property
128 is themed.
129 */
130 property real edgeMargins: foreground.square ? 0 : units.gu(2)
131
132 /*!
133 The property holds the margin from the popover's caller. The property
134 is themed.
135 */
136 property real callerMargin: 0
137
138 /*!
139 The property drives the automatic closing of the Popover when user taps
140 on the dismissArea. The default behavior is to close the Popover, therefore
141 set to true.
142
143 When set to false, closing the Popover is the responsibility of the caller.
144 Also, the mouse and touch events are not blocked from the dismissArea.
145 */
146 property bool autoClose: true
147
148 /*!
149 \qmlproperty Component foregroundStyle
150 Exposes the style property of the \l StyledItem contained in the Popover.
151 Refer to \l StyledItem how to use it.
152 */
153 property alias foregroundStyle: foreground.style
154
155 /*!
156 Make the popover visible. Reparent to the background area object first if needed.
157 Only use this function if you handle memory management. Otherwise use
158 PopupUtils.open() to do it automatically.
159 */
160 function show() {
161 /* Cannot call parent's show() however PopupBase::show()
162 does not do anything useful to us.
163
164 https://bugreports.qt-project.org/browse/QTBUG-25942
165 http://qt-project.org/forums/viewthread/19577
166 */
167 visible = true;
168 foreground.show();
169 }
170
171 /*!
172 Hide the popover.
173 Only use this function if you handle memory management. Otherwise use
174 PopupUtils.close() to do it automatically.
175 */
176 function hide() {
177 foreground.hide();
178 }
179
180 Component.onCompleted: foreground.hideCompleted.connect(popover.__makeInvisible)
181 /*!
182 \internal
183 */
184 function __makeInvisible() {
185 visible = false;
186 }
187
188 QtObject {
189 id: internal
190 property bool portrait: width < height
191
192 // private
193 function updatePosition() {
194 if (pointerTarget && pointerTarget.parent)
195 popover.y = (popover.parent.height - popover.height) / 2;
196 var pos = new InternalPopupUtils.CallerPositioning(foreground, pointer, dismissArea, caller, pointerTarget, edgeMargins, callerMargin);
197 pos.auto();
198 }
199 }
200
201 __foreground: foreground
202 __eventGrabber.enabled: autoClose
203 __closeOnDismissAreaPress: true
204
205 y: parent ? (parent.height - height) / 2 : 0
206
207 StyledItem {
208 id: foreground
209 activeFocusOnPress: true
210 objectName: "popover_foreground"
211
212 //styling properties
213 property real minimumWidth: units.gu(40)
214
215 property real maxWidth: dismissArea ? (internal.portrait ? dismissArea.width : dismissArea.width * 3/4) : 0.0
216 property real maxHeight: dismissArea ? (internal.portrait ? dismissArea.height * 3/4 : dismissArea.height) : 0.0
217 width: Math.min(minimumWidth, maxWidth)
218 height: containerItem.height
219
220 Item {
221 id: containerItem
222 parent: foreground.__styleInstance.contentItem
223 anchors {
224 left: parent.left
225 top: parent.top
226 right: parent.right
227 }
228 height: childrenRect.height
229 }
230
231 onWidthChanged: internal.updatePosition()
232 onHeightChanged: internal.updatePosition()
233
234 property point target: Qt.point(pointer.x - x, pointer.y - y)
235 property string direction: pointer.direction
236 property bool clipContent: true
237 property bool square: popover.hasOwnProperty("square") ? popover.square : false
238
239 signal show()
240 signal hide()
241 signal showCompleted()
242 signal hideCompleted()
243
244 style: Theme.createStyleComponent("PopoverForegroundStyle.qml", foreground)
245 }
246
247 QtObject {
248 id: pointer
249
250 /* Input variables for InternalPopupUtils are the properties:
251 - horizontalMargin
252 - verticalMargin
253 - size
254
255 Output variables of InternalPopupUtils are the properties:
256 - x
257 - y
258 - direction
259 */
260
261 property real arrowSize: units.dp(15)
262 property real cornerSize: units.dp(11)
263
264 /* Minimum distance between the top or bottom of the popup and
265 the tip of the pointer when the direction is left or right.
266 */
267 property real horizontalMargin: arrowSize/2.0 + cornerSize
268 /* Minimum distance between the left or right of the popup and
269 the tip of the pointer when the direction is up or down.
270 */
271 property real verticalMargin: arrowSize/2.0 + cornerSize
272 /* Either:
273 - distance between the left or right of the popup and the tip
274 of the pointer when the direction is left or right.
275 - distance between the top or bottom of the popup and the tip
276 of the pointer when the direction is up or down.
277 */
278 property real size: units.dp(6)
279
280 property real x
281 property real y
282 property string direction
283 }
284
285
286 /*! \internal */
287 onCallerChanged: internal.updatePosition()
288 /*! \internal */
289 onPointerTargetChanged: internal.updatePosition()
290 /*! \internal */
291 onWidthChanged: internal.updatePosition()
292 /*! \internal */
293 onHeightChanged: internal.updatePosition()
294 /*! \internal */
295 onRotatingChanged: hide()
296
297 Connections {
298 target: pointerTarget
299 function onXChanged() { internal.updatePosition() }
300 function onYChanged() { internal.updatePosition() }
301 function onWidthChanged() { internal.updatePosition() }
302 function onHeightChanged() { internal.updatePosition() }
303 }
304 }