| 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 Lomiri.Components 1.3 |
| 19 |
|
| 20 |
/*! |
| 21 |
\qmltype PopupBase |
| 22 |
\inqmlmodule Lomiri.Components.Popups 1.0 |
| 23 |
\ingroup lomiri-popups |
| 24 |
\brief The base class for all dialogs, sheets and popovers. Do not use directly. |
| 25 |
|
| 26 |
Examples: See subclasses. |
| 27 |
*/ |
| 28 |
OrientationHelper { |
| 29 |
id: popupBase |
| 30 |
|
| 31 |
/*! |
| 32 |
The property holds the area used to dismiss the popups, the area from where |
| 33 |
mouse and touch events will be grabbed. By default this area is the Popup |
| 34 |
itself. |
| 35 |
*/ |
| 36 |
property Item dismissArea: popupBase |
| 37 |
|
| 38 |
/*! |
| 39 |
The property specifies whether to forward or not the mouse and touch events |
| 40 |
happening outside of the popover. By default all events are grabbed. |
| 41 |
*/ |
| 42 |
property bool grabDismissAreaEvents: true |
| 43 |
|
| 44 |
/*! |
| 45 |
\internal |
| 46 |
FIXME: publish this property once agreed |
| 47 |
*/ |
| 48 |
property PropertyAnimation fadingAnimation: PropertyAnimation{duration: 0} |
| 49 |
|
| 50 |
// without specifying width and height below, some width calculations go wrong in Sheet. |
| 51 |
// I guess popupBase.width is not correctly set initially |
| 52 |
width: parent ? parent.width : undefined |
| 53 |
height: parent ? parent.height : undefined |
| 54 |
|
| 55 |
// copy value of automaticOrientation from root object (typically a MainView) |
| 56 |
automaticOrientation: stateWrapper.rootItem && stateWrapper.rootItem.automaticOrientation ? |
| 57 |
stateWrapper.rootItem.automaticOrientation : false |
| 58 |
anchorToKeyboard: true |
| 59 |
|
| 60 |
LayoutMirroring.enabled: Qt.application.layoutDirection == Qt.RightToLeft |
| 61 |
LayoutMirroring.childrenInherit: true |
| 62 |
|
| 63 |
/*! |
| 64 |
Make the popup visible. Reparent to the background area object first if needed. |
| 65 |
Only use this function if you handle memory management. Otherwise use |
| 66 |
PopupUtils.open() to do it automatically. |
| 67 |
*/ |
| 68 |
function show() { |
| 69 |
|
| 70 |
if ((typeof popupBase["reparentToRootItem"]) === "boolean") { |
| 71 |
// So the property exists. Let's use it then. |
| 72 |
if (reparentToRootItem) { |
| 73 |
parent = stateWrapper.rootItem; |
| 74 |
} |
| 75 |
} else { |
| 76 |
// The property does not exist. Default is to reparent |
| 77 |
parent = stateWrapper.rootItem; |
| 78 |
} |
| 79 |
|
| 80 |
if (!dismissArea) { |
| 81 |
dismissArea = parent |
| 82 |
} |
| 83 |
|
| 84 |
// Without setting the parent, mapFromItem() breaks in internalPopupUtils. |
| 85 |
stateWrapper.state = 'opened'; |
| 86 |
} |
| 87 |
|
| 88 |
/*! |
| 89 |
Hide the popup. |
| 90 |
Only use this function if you handle memory management. Otherwise use |
| 91 |
PopupUtils.close() to do it automatically. |
| 92 |
*/ |
| 93 |
function hide() { |
| 94 |
stateWrapper.state = 'closed'; |
| 95 |
} |
| 96 |
|
| 97 |
/*! |
| 98 |
\internal |
| 99 |
When the popup is created by calling PopupUtils.open(), |
| 100 |
onVisibleChanged is connected to __closeIfHidden(). |
| 101 |
*/ |
| 102 |
function __closeIfHidden() { |
| 103 |
if (!visible) __closePopup(); |
| 104 |
} |
| 105 |
|
| 106 |
/*! |
| 107 |
\internal |
| 108 |
The function closes the popup. This is called when popup's caller is no |
| 109 |
longer valid. |
| 110 |
*/ |
| 111 |
function __closePopup() { |
| 112 |
if (popupBase) { |
| 113 |
stateWrapper.restoreActiveFocus(); |
| 114 |
popupBase.destroy(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/*! |
| 119 |
\internal |
| 120 |
The function saves the active focus for later. |
| 121 |
*/ |
| 122 |
function __setPreviousActiveFocusItem(item) { |
| 123 |
stateWrapper.prevFocus = item; |
| 124 |
} |
| 125 |
|
| 126 |
/*! |
| 127 |
\internal |
| 128 |
Foreground component excluded from InverseMouseArea |
| 129 |
*/ |
| 130 |
property Item __foreground |
| 131 |
|
| 132 |
/*! |
| 133 |
\internal |
| 134 |
Set to true if the InverseMouseArea should dismiss the area |
| 135 |
*/ |
| 136 |
property bool __closeOnDismissAreaPress: false |
| 137 |
|
| 138 |
/*! |
| 139 |
\internal |
| 140 |
Property driving dimming the popup's background. The default is the same as |
| 141 |
defined in the style |
| 142 |
*/ |
| 143 |
property alias __dimBackground: background.dim |
| 144 |
|
| 145 |
/*! |
| 146 |
\internal |
| 147 |
Property driving the dim color popup's background. The default is the same as |
| 148 |
defined in the style |
| 149 |
*/ |
| 150 |
property alias __dimBackgroundColor: background.color |
| 151 |
|
| 152 |
|
| 153 |
/*! |
| 154 |
\internal |
| 155 |
Property to control dismissArea event capture. |
| 156 |
*/ |
| 157 |
property alias __eventGrabber: eventGrabber |
| 158 |
|
| 159 |
// dimmer |
| 160 |
Rectangle { |
| 161 |
id: background |
| 162 |
// styling properties |
| 163 |
property bool dim: false |
| 164 |
anchors.fill: parent |
| 165 |
visible: dim |
| 166 |
property color dimC: __foreground.theme.name == "Lomiri.Components.Themes.Ambiance" ? LomiriColors.jet : LomiriColors.inkstone |
| 167 |
color: popupBase.width > units.gu(60) ? Qt.rgba(dimC.r, dimC.g, dimC.b, 0.6) : Qt.rgba(dimC.r, dimC.g, dimC.b, 0.9) |
| 168 |
} |
| 169 |
|
| 170 |
InverseMouseArea { |
| 171 |
id: eventGrabber |
| 172 |
enabled: true |
| 173 |
acceptedButtons: Qt.LeftButton | Qt.MiddleButton | Qt.RightButton |
| 174 |
anchors.fill: __foreground |
| 175 |
sensingArea: dismissArea |
| 176 |
propagateComposedEvents: !grabDismissAreaEvents |
| 177 |
onPressed: if (__closeOnDismissAreaPress) popupBase.hide() |
| 178 |
onWheel: wheel.accepted = true |
| 179 |
} |
| 180 |
|
| 181 |
MouseArea { |
| 182 |
acceptedButtons: Qt.LeftButton | Qt.MiddleButton | Qt.RightButton |
| 183 |
anchors.fill: __foreground |
| 184 |
onWheel: wheel.accepted = true |
| 185 |
} |
| 186 |
|
| 187 |
// set visible as false by default |
| 188 |
visible: false |
| 189 |
opacity: 0.0 |
| 190 |
/*! \internal */ |
| 191 |
onVisibleChanged: stateWrapper.state = (visible) ? 'opened' : 'closed' |
| 192 |
/*! \internal */ |
| 193 |
onParentChanged: stateWrapper.rootItem = QuickUtils.rootItem(popupBase) |
| 194 |
Component.onCompleted: { |
| 195 |
stateWrapper.rootItem = QuickUtils.rootItem(popupBase); |
| 196 |
} |
| 197 |
|
| 198 |
Item { |
| 199 |
id: stateWrapper |
| 200 |
property Item rootItem: QuickUtils.rootItem(popupBase) |
| 201 |
property Item prevFocus |
| 202 |
|
| 203 |
function restoreActiveFocus() { |
| 204 |
if (prevFocus) { |
| 205 |
if (prevFocus.hasOwnProperty("requestFocus")) { |
| 206 |
prevFocus.requestFocus(Qt.OtherFocusReason); |
| 207 |
} else { |
| 208 |
prevFocus.forceActiveFocus(Qt.OtherFocusReason); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
states: [ |
| 214 |
State { |
| 215 |
name: 'closed' |
| 216 |
extend: '' |
| 217 |
}, |
| 218 |
State { |
| 219 |
name: 'opened' |
| 220 |
} |
| 221 |
] |
| 222 |
transitions: [ |
| 223 |
Transition { |
| 224 |
from: "*" |
| 225 |
to: "opened" |
| 226 |
SequentialAnimation { |
| 227 |
ScriptAction { |
| 228 |
script: popupBase.visible = true |
| 229 |
} |
| 230 |
NumberAnimation { |
| 231 |
target: popupBase |
| 232 |
property: "opacity" |
| 233 |
from: 0.0 |
| 234 |
to: 1.0 |
| 235 |
duration: fadingAnimation.duration |
| 236 |
easing: fadingAnimation.easing |
| 237 |
} |
| 238 |
} |
| 239 |
}, |
| 240 |
Transition { |
| 241 |
from: "opened" |
| 242 |
to: "closed" |
| 243 |
SequentialAnimation { |
| 244 |
NumberAnimation { |
| 245 |
target: popupBase |
| 246 |
property: "opacity" |
| 247 |
from: 1.0 |
| 248 |
to: 0.0 |
| 249 |
duration: fadingAnimation.duration |
| 250 |
easing: fadingAnimation.easing |
| 251 |
} |
| 252 |
ScriptAction { |
| 253 |
script: { |
| 254 |
popupBase.visible = false; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
] |
| 260 |
} |
| 261 |
} |