Debian Codegraph @

src/imports/Components/Popups/1.2/Dialog.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 Lomiri.Components 1.2
19 import "internalPopupUtils.js" as InternalPopupUtils
20
21 /*!
22 \qmltype Dialog
23 \inherits PopupBase
24 \inqmlmodule Lomiri.Components.Popups 1.0
25 \ingroup lomiri-popups
26 \brief The Dialog caters for cases in which the application requires the user to determine
27 between optional actions. The Dialog will interrupt the user flow and lock the view
28 for further interaction before the user has selected a desired action.
29 It can only be closed by selecting an optional action confirming or cancelling the operation.
30
31 \l {http://design.lomiri.com/apps/building-blocks/dialog}{See also the Design Guidelines on Dialogs}.
32
33 Example:
34 \qml
35 import QtQuick 2.4
36 import Lomiri.Components 1.2
37 import Lomiri.Components.Popups 1.0
38
39 Item {
40 width: units.gu(80)
41 height: units.gu(80)
42 Component {
43 id: dialog
44 Dialog {
45 id: dialogue
46 title: "Save file"
47 text: "Are you sure that you want to save this file?"
48 Button {
49 text: "cancel"
50 onClicked: PopupUtils.close(dialogue)
51 }
52 Button {
53 text: "overwrite previous version"
54 color: LomiriColors.orange
55 onClicked: PopupUtils.close(dialogue)
56 }
57 Button {
58 text: "save a copy"
59 color: LomiriColors.orange
60 onClicked: PopupUtils.close(dialogue)
61 }
62 }
63 }
64 Button {
65 anchors.centerIn: parent
66 id: saveButton
67 text: "save"
68 onClicked: PopupUtils.open(dialog)
69 }
70 }
71 \endqml
72 */
73
74 PopupBase {
75 id: dialog
76
77 /*!
78 \qmlproperty list<Object> contents
79 Content will be put inside a column in the foreround of the Dialog.
80 */
81 default property alias contents: contentsColumn.data
82
83 /*!
84 The title of the question to ask the user.
85 \qmlproperty string title
86 */
87 property alias title: foreground.title
88
89 /*!
90 The question to the user.
91 \qmlproperty string text
92 */
93 property alias text: foreground.text
94
95 /*!
96 The Item such as a \l Button that the user interacted with to open the Dialog.
97 This property will be used for the automatic positioning of the Dialog next to
98 the caller, if possible.
99 */
100 property Item caller
101
102 /*!
103 The property holds the item to which the pointer should be anchored to.
104 This can be same as the caller or any child of the caller. By default the
105 property is set to caller.
106 */
107 property Item pointerTarget
108 /*! \internal */
109 onPointerTargetChanged: {
110 console.debug("pointerTarget DEPRECATED")
111 }
112
113 /*!
114 The property holds the margins from the dialog's dismissArea. The property
115 is themed.
116 */
117 property real edgeMargins: units.gu(2)
118
119 /*!
120 The property holds the margin from the dialog's caller. The property
121 is themed.
122 */
123 property real callerMargin: units.gu(1)
124
125 /*!
126 The property controls whether the dialog is modal or not. Modal dialogs block
127 event propagation to items under dismissArea, when non-modal ones let these
128 events passed to these items. In addition, non-modal dialogs do not dim the
129 dismissArea.
130
131 The default value is true.
132 */
133 property bool modal: true
134
135 /*
136 QtObject {
137 id: internal
138
139 function updatePosition() {
140 var pos = new InternalPopupUtils.CallerPositioning(foreground, pointer, dialog, caller, pointerTarget, edgeMargins, callerMargin);
141 pos.auto();
142
143 }
144 }
145
146 Pointer { id: pointer }
147 */
148
149 __foreground: foreground
150 __eventGrabber.enabled: modal
151 __dimBackground: modal
152 fadingAnimation: LomiriNumberAnimation { duration: LomiriAnimation.SnapDuration }
153
154 StyledItem {
155 id: foreground
156 activeFocusOnPress: true
157 width: Math.min(minimumWidth, dialog.width)
158 anchors.centerIn: parent
159 clip: true
160 objectName: 'dialogForeground'
161
162 // used in the style
163 property string title
164 property string text
165 property real minimumWidth: units.gu(38)
166 property real minimumHeight: units.gu(32)
167 property real maxHeight: 3*dialog.height/4
168 property real margins: units.gu(4)
169 property real itemSpacing: units.gu(2)
170 property Item dismissArea: dialog.dismissArea
171 property real keyboardHeight: dialog.anchorToKeyboard && LomiriApplication.inputMethod.visible ? LomiriApplication.inputMethod.keyboardRectangle.height : 0
172
173 height: Math.min(contentsColumn.height + foreground.margins, dialog.height - keyboardHeight)
174
175 Flickable {
176 anchors.fill: parent
177 anchors.margins: foreground.margins
178 contentWidth: contentsColumn.width
179 contentHeight: contentsColumn.height - foreground.margins
180 boundsBehavior: Flickable.StopAtBounds
181
182 Column {
183 id: contentsColumn
184 spacing: foreground.itemSpacing
185 width: foreground.width - foreground.margins * 2
186 height: childrenRect.height + foreground.margins
187 onWidthChanged: updateChildrenWidths();
188
189 Label {
190 horizontalAlignment: Text.AlignHCenter
191 text: dialog.title
192 wrapMode: Text.Wrap
193 maximumLineCount: 2
194 elide: Text.ElideRight
195 fontSize: "large"
196 color: LomiriColors.darkGrey
197 visible: (text !== "")
198 }
199
200 Label {
201 horizontalAlignment: Text.AlignHCenter
202 text: dialog.text
203 fontSize: "medium"
204 color: LomiriColors.darkGrey
205 wrapMode: Text.Wrap
206 visible: (text !== "")
207 }
208
209 onChildrenChanged: updateChildrenWidths()
210
211 function updateChildrenWidths() {
212 for (var i = 0; i < children.length; i++) {
213 children[i].width = contentsColumn.width;
214 }
215 }
216 }
217 }
218
219 style: Theme.createStyleComponent("DialogForegroundStyle.qml", foreground)
220 }
221 }