-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomDialog.qml
318 lines (255 loc) · 6.52 KB
/
CustomDialog.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* QML Material - An application framework implementing Material Design.
* Copyright (C) 2014-2015 Michael Spencer <sonrisesoftware@gmail.com>
* 2015 Bogdan Cuza <bogdan.cuza@hotmail.com>
* 2015 Mikhail Ivchenko <ematirov@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.0
import QtQuick.Layouts 1.1
import Material 0.1
import Material.Extras 0.1
/*!
\qmltype Dialog
\inqmlmodule Material 0.1
\brief Dialogs inform users about critical information, require users to make
decisions, or encapsulate multiple tasks within a discrete process
*/
PopupBase {
id: dialog
overlayLayer: "dialogOverlayLayer"
overlayColor: Qt.rgba(0, 0, 0, 0.3)
opacity: showing ? 1 : 0
visible: opacity > 0
width: Math.max(minimumWidth,
content.contentWidth + 2 * contentMargins)
height: Math.min(parent.height - Units.dp(64),
headerView.height + (contentMargins * 2) +
content.contentHeight +
content.topMargin +
content.bottomMargin +
(floatingActions ? 0 : buttonContainer.height))
property int contentMargins: Units.dp(16)
property int minimumWidth: Units.dp(270)
property alias title: titleLabel.text
property alias text: textLabel.text
/*!
\qmlproperty Button negativeButton
The negative button, displayed as the leftmost button on the right of the dialog buttons.
This is usually used to dismiss the dialog.
*/
property alias negativeButton: negativeButton
/*!
\qmlproperty Button primaryButton
The primary button, displayed as the rightmost button in the dialog buttons row. This is
usually used to accept the dialog's action.
*/
property alias positiveButton: positiveButton
property string negativeButtonText: "Cancel"
property string positiveButtonText: "Ok"
property alias positiveButtonEnabled: positiveButton.enabled
property bool hasActions: true
property bool floatingActions: false
default property alias dialogContent: column.data
signal accepted()
signal rejected()
anchors {
centerIn: parent
verticalCenterOffset: showing ? 0 : -(dialog.height/3)
Behavior on verticalCenterOffset {
NumberAnimation { duration: 200 }
}
}
Behavior on opacity {
NumberAnimation { duration: 200 }
}
Behavior on width {
NumberAnimation { duration: 200 }
}
Behavior on height {
NumberAnimation { duration: 200 }
}
Keys.onPressed: {
if (event.key === Qt.Key_Escape) {
closeKeyPressed(event)
}
}
Keys.onReleased: {
if (event.key === Qt.Key_Back) {
closeKeyPressed(event)
}
}
function closeKeyPressed(event) {
if (dialog.showing) {
if (dialog.dismissOnTap) {
dialog.close()
}
event.accepted = true
}
}
function show() {
open()
}
View {
id: dialogContainer
anchors.fill: parent
elevation: 5
radius: Units.dp(2)
MouseArea {
anchors.fill: parent
propagateComposedEvents: false
onClicked: {
mouse.accepted = false
}
}
Item {
anchors {
left: parent.left
right: parent.right
top: parent.top
topMargin: Units.dp(8)
}
clip: true
height: headerView.height + Units.dp(32)
View {
backgroundColor: "white"
elevation: content.atYBeginning ? 0 : 1
fullWidth: true
anchors {
left: parent.left
right: parent.right
top: parent.top
}
height: headerView.height + Units.dp(16)
}
}
Column {
id: headerView
spacing: Units.dp(16)
anchors {
left: parent.left
right: parent.right
top: parent.top
leftMargin: Units.dp(16)
rightMargin: Units.dp(16)
topMargin: title == "" && text == "" ? 0 : Units.dp(16)
}
Label {
id: titleLabel
width: parent.width
wrapMode: Text.Wrap
style: "title"
visible: title != ""
}
Label {
id: textLabel
width: parent.width
wrapMode: Text.Wrap
style: "dialog"
visible: text != ""
}
}
Rectangle {
anchors.fill: content
}
Flickable {
id: content
contentWidth: column.implicitWidth
contentHeight: column.height
clip: true
anchors {
left: parent.left
right: parent.right
top: headerView.bottom
topMargin: title == "" && text == "" ? 0 :Units.dp(8)
bottomMargin: Units.dp(-8)
bottom: floatingActions ? parent.bottom : buttonContainer.top
}
interactive: contentHeight + Units.dp(8) > height
bottomMargin: hasActions || contentMargins == 0 ? 0 : Units.dp(8)
Rectangle {
Column {
id: column
anchors {
left: parent.left
margins: contentMargins
}
width: content.width - 2 * contentMargins
spacing: Units.dp(16)
}
}
}
Scrollbar {
flickableItem: content
}
Item {
id: buttonContainer
anchors {
bottomMargin: Units.dp(8)
bottom: parent.bottom
right: parent.right
left: parent.left
}
height: hasActions ? buttonView.height + Units.dp(8) : 0
clip: true
View {
id: buttonView
height: hasActions ? positiveButton.implicitHeight + Units.dp(8) : 0
visible: hasActions
backgroundColor: floatingActions ? "transparent" : "white"
elevation: content.atYEnd ? 0 : 1
fullWidth: true
elevationInverted: true
anchors {
bottom: parent.bottom
right: parent.right
left: parent.left
}
Button {
id: negativeButton
text: negativeButtonText
textColor: Theme.accentColor
context: "dialog"
anchors {
top: parent.top
right: positiveButton.visible ? positiveButton.left : parent.right
topMargin: Units.dp(8)
rightMargin: positiveButton.visible ? Units.dp(8) : Units.dp(16)
}
onClicked: {
// close();
rejected();
}
}
Button {
id: positiveButton
text: positiveButtonText
textColor: Theme.accentColor
context: "dialog"
anchors {
top: parent.top
topMargin: Units.dp(8)
rightMargin: Units.dp(16)
right: parent.right
}
onClicked: {
// close()
accepted();
}
}
}
}
}
}