MauiKit Controls

ToastArea.qml
1
2/*
3 * Copyright 2018 Camilo Higuita <milo.h@aol.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License as
7 * published by the Free Software Foundation; either version 2, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21import QtQuick
22import QtQuick.Controls
23import QtQuick.Layouts
24import QtQuick.Window
25
26import Qt5Compat.GraphicalEffects
27
28import QtQml.Models
29
30import QtMultimedia
31
32import org.mauikit.controls as Maui
33
34Control
35{
36 id: control
37 focus: true
38 padding: Maui.Style.contentMargins
39 visible: _container.count > 0
40
41 hoverEnabled: true
42
43 property bool autoClose : Window.window.active
44
45 property Item previousItem : null
46
47 SoundEffect
48 {
49 id: playSound
50 source: "qrc:/assets/notification_simple-01.wav"
51 }
52
53 SoundEffect
54 {
55 id: _dismissSound
56 source: "qrc:/assets/notification_simple-02.wav"
57 }
58
59 Keys.enabled: true
60 Keys.onEscapePressed:
61 {
62 control.dismiss()
63 }
64
65 onVisibleChanged:
66 {
67 if(visible)
68 {
69 control.previousItem = Window.window.activeFocusItem
70 control.forceActiveFocus()
71 }else
72 {
73 if(control.previousItem)
74 {
75 control.previousItem.forceActiveFocus()
76 control.previousItem = null
77 }
78 }
79 }
80
81 background: MouseArea
82 {
83 // opacity: 0.8
84
85 onClicked:
86 {
87 if(_container.count === 1)
88 control.dismiss()
89 }
90
91 LinearGradient
92 {
93 anchors.fill: parent
94 start: Qt.point(control.width/2, 0)
95 end: Qt.point(control.width/2, control.height - _container.height)
96 gradient: Gradient {
97 GradientStop { position: 0.0; color: "transparent" }
98 GradientStop { position: 1.0; color: Maui.Theme.backgroundColor }
99 }
100 }
101 }
102
103 Component
104 {
105 id: _toastComponent
106
108 {
109 id: _toast
110 clip: false
111
112 Maui.Theme.colorSet: Maui.Theme.View
113 Maui.Theme.inherit: false
114
115 readonly property int mindex : ObjectModel.index
116 width: ListView.view.width
117 height: _layout.implicitHeight + topPadding +bottomPadding
118
119 hoverEnabled: true
120
121 padding: Maui.Style.contentMargins
122
123 property alias title : _template.label1.text
124 property alias iconSource: _template.iconSource
125 property alias imageSource: _template.imageSource
126 property alias body: _template.label2.text
127 property var callback : ({})
128 property alias buttonText: _button.text
129 property int timeout : 3500
130
131 onClicked: control.remove(mindex)
132
133 background: Rectangle
134 {
135 radius: Maui.Style.radiusV
136 color: _toast.hovered? Maui.Theme.hoverColor : Maui.Theme.backgroundColor
137
138 ProgressBar
139 {
140 id: _progressBar
141 anchors.bottom: parent.bottom
142 height: 2
143 width: parent.width
144 from: 0
145 to : _toastTimer.interval
146 value: _progressTimer.progress
147
148 Timer
149 {
150 id: _progressTimer
151 property int progress : 0
152 interval: 5
153 repeat: _toastTimer.running
154 onTriggered: progress += _progressTimer.interval
155 }
156
157 function restart()
158 {
159 _progressTimer.progress = 0
160 _progressTimer.restart()
161 }
162 }
163
164 layer.enabled: true
165 layer.effect: DropShadow
166 {
167 horizontalOffset: 0
168 verticalOffset: 0
169 radius: 8
170 samples: 16
171 color: "#80000000"
172 transparentBorder: true
173 }
174 }
175
176 Component.onCompleted:
177 {
178 _progressTimer.start()
179 _toastTimer.start()
180 }
181
182 Timer
183 {
184 id: _toastTimer
185 interval: _toast.timeout + (_toast.mindex * 500)
186
187 onTriggered:
188 {
189 if(_toast.hovered || _container.hovered || !control.autoClose)
190 {
191 _toastTimer.restart()
192 _progressBar.restart()
193 return;
194 }
195 _progressTimer.stop()
196 control.remove(_toast.mindex)
197 }
198 }
199
200 contentItem: ColumnLayout
201 {
202 id: _layout
203 spacing: Maui.Style.space.medium
204
205 Maui.ListItemTemplate
206 {
207 id: _template
208 Layout.fillWidth: true
209 Layout.fillHeight: true
210 label1.text: "Title"
211 label2.text: "Body of the message"
212 label2.wrapMode: Text.Wrap
213 iconSource: "dialog-warning"
214 iconSizeHint: Maui.Style.iconSizes.big
215 }
216
217 Button
218 {
219 id: _button
220 visible: _toast.callback instanceof Function
221 text: i18n("Accept")
222 Layout.fillWidth: true
223 onClicked:
224 {
225 if(_toast.callback instanceof Function)
226 {
227 _toast.callback(_toast.mindex)
228 }
229 control.remove(_toast.mindex)
230 }
231 }
232 }
233
234 DragHandler
235 {
236 id: _dragHandler
237 yAxis.enabled: false
238
239 onActiveChanged:
240 {
241 if(!active)
242 {
243 if(_dragHandler.centroid.scenePressPosition.x.toFixed(1) - _dragHandler.centroid.scenePosition.x.toFixed(1) > 80)
244 {
245 control.remove(_toast.mindex)
246 }else
247 {
248 _toast.x = 0
249 }
250 }
251 }
252 }
253 }
254 }
255
256 contentItem: Item
257 {
258
259 Container
260 {
261 id: _container
262 clip: false
263 hoverEnabled: true
264
265 width: Math.min(400, parent.width)
266 height: Math.min( _listView.implicitHeight + topPadding + bottomPadding, 500)
267
268 anchors.bottom: parent.bottom
269 anchors.horizontalCenter: parent.horizontalCenter
270
271 contentItem: Maui.ListBrowser
272 {
273 id: _listView
274
275 property bool expanded : true
276 clip: false
277 orientation: ListView.Vertical
278 snapMode: ListView.SnapOneItem
279
280 spacing: Maui.Style.space.medium
281
282 model: _container.contentModel
283
284 footer: Item
285 {
286 width: ListView.view.width
287 height: Maui.Style.toolBarHeight
288
289 Button
290 {
291 id: _dimissButton
292 visible: _container.count > 1
293 width: parent.width
294 anchors.centerIn: parent
295 text: i18n("Dismiss All")
296 onClicked: control.dismiss()
297 }
298 }
299 }
300 }
301 }
302
303 function add(icon, title, body, callback = ({}), buttonText = "")
304 {
305 const properties = ({
306 'iconSource': icon,
307 'title': title,
308 'body': body,
309 'callback': callback,
310 'buttonText': buttonText
311 })
312 const object = _toastComponent.createObject(_listView.flickable, properties);
313 _container.insertItem(0, object)
314 playSound.play()
315 }
316
317
318 function dismiss()
319 {
320 let count = _container.count
321 let items = []
322 for(var i = 0; i< count; i++)
323 {
324 items.push(_container.itemAt(i))
325 }
326
327 for(var j of items)
328 {
329 _container.removeItem(j)
330 }
331
332 _dismissSound.play()
333 }
334
335 function remove(index)
336 {
337 _container.removeItem(_container.itemAt(index))
338 }
339 }
ItemDelegate is the base for the MauiKit delegate controls.
Q_SCRIPTABLE Q_NOREPLY void start()
QString i18n(const char *text, const TYPE &arg...)
bool remove(const QString &column, const QVariant &value)
QAction * restart(const QObject *recvr, const char *slot, QObject *parent)
QAction * repeat(const QObject *recvr, const char *slot, QObject *parent)
KGuiItem properties()
const QList< QKeySequence > & end()
QString & remove(QChar ch, Qt::CaseSensitivity cs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:56:16 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.