Debian Codegraph @

src/imports/Components/Popups/2.0/Dialog.qml

raw
1 /*
2 * Copyright 2016 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 QtQuick.Window 2.2
19 import Lomiri.Components
20 import "internalPopupUtils.js" as InternalPopupUtils
21
22 /*!
23 \qmltype Dialog
24 \inherits PopupBase
25 \inqmlmodule Lomiri.Components.Popups 1.0
26 \ingroup lomiri-popups
27 \brief The Dialog caters for cases in which the application requires the user to determine
28 between optional actions. The Dialog will interrupt the user flow and lock the view
29 for further interaction before the user has selected a desired action.
30 It can only be closed by selecting an optional action confirming or cancelling the operation.
31
32 \l {http://design.lomiri.com/apps/building-blocks/dialog}{See also the Design Guidelines on Dialogs}.
33
34 Example:
35 \qml
36 import QtQuick 2.4
37 import Lomiri.Components
38 import Lomiri.Components.Popups
39
40 Item {
41 width: units.gu(80)
42 height: units.gu(80)
43 Component {
44 id: dialog
45 Dialog {
46 id: dialogue
47 title: "Save file"
48 text: "Are you sure that you want to save this file?"
49 Button {
50 text: "cancel"
51 onClicked: PopupUtils.close(dialogue)
52 }
53 Button {
54 text: "overwrite previous version"
55 color: LomiriColors.orange
56 onClicked: PopupUtils.close(dialogue)
57 }
58 Button {
59 text: "save a copy"
60 color: LomiriColors.orange
61 onClicked: PopupUtils.close(dialogue)
62 }
63 }
64 }
65 Button {
66 anchors.centerIn: parent
67 id: saveButton
68 text: "save"
69 onClicked: PopupUtils.open(dialog)
70 }
71 }
72 \endqml
73 */
74
75 PopupBase {
76 id: dialog
77
78 /*!
79 \qmlproperty list<Object> contents
80 Content will be put inside a column in the foreround of the Dialog.
81 */
82 default property alias contents: contentsColumn.data
83
84 /*!
85 The title of the question to ask the user.
86 \qmlproperty string title
87 */
88 property alias title: foreground.title
89
90 /*!
91 The question to the user.
92 \qmlproperty string text
93 */
94 property alias text: foreground.text
95
96 /*!
97 \deprecated
98 The Item such as a \l Button that the user interacted with to open the Dialog.
99 This property will be used for the automatic positioning of the Dialog next to
100 the caller, if possible.
101 */
102 property Item caller
103 onCallerChanged: console.warn("'caller' is DEPRECATED. It has no effect.")
104
105 /*!
106 The property holds the margins from the dialog's dismissArea. The property
107 is themed.
108 */
109 property real edgeMargins: units.gu(2)
110
111 /*!
112 The property holds the margin from the dialog's caller. The property
113 is themed.
114 */
115 property real callerMargin: units.gu(1)
116
117 /*!
118 The property controls whether the dialog is modal or not. Modal dialogs block
119 event propagation to items under dismissArea, when non-modal ones let these
120 events passed to these items. In addition, non-modal dialogs do not dim the
121 dismissArea.
122
123 The default value is true.
124 */
125 property bool modal: true
126
127 /*!
128 \qmlproperty real contentWidth
129 \since Lomiri.Components 1.3
130 Specifies the width of the dialog content.
131 */
132 property alias contentWidth: foreground.width
133
134 /*!
135 \qmlproperty real contentHeight
136 \since Lomiri.Components 1.3
137 Specifies the height of the dialog content.
138 */
139 property alias contentHeight: foreground.height
140
141 /*!
142 \qmlproperty ThemeSettings theme
143 \since Lomiri.Components 1.3
144 Configure the theme of the Dialog foreground and all its subcomponents.
145 Example:
146 \qml
147 import QtQuick 2.4
148 import Lomiri.Components
149 import Lomiri.Components.Popups
150 MainView {
151 width: units.gu(40)
152 height: units.gu(71)
153
154 // make sure the main theme is Ambiance
155 theme.name: "Lomiri.Components.Themes.Ambiance"
156
157 Component {
158 id: dialogComponent
159 Dialog {
160 id: dialog
161 title: "Input dialog"
162 // the dialog and its children will use SuruDark
163 theme: ThemeSettings {
164 name: "Lomiri.Components.Themes.SuruDark"
165 }
166 TextField {
167 placeholderText: "enter text"
168 }
169 Button {
170 text: "Close"
171 onClicked: PopupUtils.close(dialog)
172 }
173 }
174 }
175
176 Button {
177 text: "Open dialog"
178 onClicked: PopupUtils.open(dialogComponent)
179 }
180 }
181 \endqml
182 \sa StyledItem::theme
183 */
184 property alias theme: foreground.theme
185
186 /*!
187 \qmlproperty string styleName
188 The style name of the foreground of the Dialog.
189 \since Lomiri.Components 1.3
190 \sa StyledItem::styleName
191 */
192 property alias styleName: foreground.styleName
193
194 /*!
195 \qmlproperty Component style
196 The style of the foreground of the Dialog.
197 This property takes precedence over \l styleName
198 \since Lomiri.Components 1.3
199 \sa StyledItem::style
200 */
201 property alias style: foreground.style
202
203 __foreground: foreground
204 __eventGrabber.enabled: modal
205 __dimBackground: modal
206 fadingAnimation: LomiriNumberAnimation { duration: LomiriAnimation.SnapDuration }
207
208 StyledItem {
209 id: foreground
210 // Grab focus when Dialog is shown
211 focus: visible
212 width: Math.min(minimumWidth, dialog.width)
213 anchors.centerIn: parent
214 clip: true
215 objectName: 'dialogForeground'
216
217 // used in the style
218 property string title
219 property string text
220 property real minimumWidth: units.gu(38)
221 property real minimumHeight: units.gu(32)
222 property real maxHeight: 3*dialog.height/4
223 property real margins: units.gu(4)
224 property real itemSpacing: units.gu(2)
225 property Item dismissArea: dialog.dismissArea
226 property real keyboardHeight: dialog.anchorToKeyboard && LomiriApplication.inputMethod.visible ? LomiriApplication.inputMethod.keyboardRectangle.height : 0
227
228 height: Math.min(contentsColumn.height + foreground.margins, dialog.height - keyboardHeight)
229
230 Keys.onEscapePressed: dialog.hide()
231
232 Flickable {
233 anchors.fill: parent
234 anchors.margins: foreground.margins
235 contentWidth: contentsColumn.width
236 contentHeight: contentsColumn.height - foreground.margins
237 boundsBehavior: Flickable.StopAtBounds
238
239 Column {
240 id: contentsColumn
241 spacing: foreground.itemSpacing
242 width: foreground.width - foreground.margins * 2
243 height: childrenRect.height + foreground.margins
244 onWidthChanged: updateChildrenWidths();
245
246 // put the context into this component to save ActionContext lookup
247 PopupContext {
248 id: localContext
249 objectName: dialog.objectName + "DialogContext"
250 active: foreground.visible
251 }
252
253 Label {
254 horizontalAlignment: Text.AlignHCenter
255 text: dialog.title
256 wrapMode: Text.Wrap
257 maximumLineCount: 2
258 elide: Text.ElideRight
259 textSize: Label.Large
260 color: theme.palette.normal.overlayText
261 visible: (text !== "")
262 }
263
264 Label {
265 horizontalAlignment: Text.AlignHCenter
266 text: dialog.text
267 color: theme.palette.normal.overlayText
268 wrapMode: Text.Wrap
269 visible: (text !== "")
270 }
271
272 onChildrenChanged: updateChildrenWidths()
273
274 function updateChildrenWidths() {
275 for (var i = 0; i < children.length; i++) {
276 children[i].width = contentsColumn.width;
277 }
278 }
279 }
280 }
281
282 styleName: "DialogForegroundStyle"
283
284 Component.onCompleted: foreground.forceActiveFocus()
285 }
286 }