Kirigami-addons

MessageDialog.qml
1// SPDX-FileCopyrightText: 2021 Claudio Cambra <claudio.cambra@gmail.com>
2// SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5import QtQuick
6import QtQuick.Controls as QQC2
7import QtQuick.Templates as T
8import QtQuick.Layouts
9import org.kde.kirigami as Kirigami
10import org.kde.kirigamiaddons.components as Components
11import org.kde.kirigamiaddons.formcard as FormCard
12import org.kde.kirigamiaddons.delegates as Delegates
13
14/**
15 * A dialog to show a message. This dialog exists has 4 modes: success, warning, error, information.
16 *
17 * @image html messagedialog.png
18 */
19T.Dialog {
20 id: root
21
22 enum DialogType {
23 Success,
24 Warning,
25 Error,
26 Information
27 }
28
29 /**
30 * This property holds the dialogType. It can be either:
31 *
32 * - `MessageDialog.Success`: For a sucess message
33 * - `MessageDialog.Warning`: For a warning message
34 * - `MessageDialog.Error`: For an actual error
35 * - `MessageDialog.Information`: For an informational message
36 *
37 * By default, the dialogType is `MessageDialog.Success`
38 */
39 property int dialogType: Components.MessageDialog.Success
40
41 /**
42 * @brief This property holds the name of setting to store the "dont's show again" preference.
43 *
44 * If provided, a checkbox is added with which further notifications can be turned off.
45 * The string is used to lookup and store the setting in the applications config file.
46 * The setting is stored in the "Notification Messages" group.
47 *
48 * When set use, openDialog() instead of open() to open this dialog.
49 *
50 * @warning Overwriting the dialog's footer will disable this feature.
51 */
52 property string dontShowAgainName: ''
53
54 default property alias mainContent: mainLayout.data
55
56 property string iconName: ''
57
58 x: Math.round((parent.width - width) / 2)
59 y: Math.round((parent.height - height) / 2)
60 z: Kirigami.OverlayZStacking.z
61
62 parent: applicationWindow().QQC2.Overlay.overlay
63
64 width: Math.min(parent.width - Kirigami.Units.gridUnit * 2, Kirigami.Units.gridUnit * 20)
65 height: Math.min(implicitHeight, parent.height - Kirigami.Units.gridUnit * 2, Kirigami.Units.gridUnit * 30)
66
67 implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
68 contentWidth + leftPadding + rightPadding,
69 implicitHeaderWidth,
70 implicitFooterWidth)
71 implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
72 contentHeight + topPadding + bottomPadding
73 + (implicitHeaderHeight > 0 ? implicitHeaderHeight + spacing : 0)
74 + (implicitFooterHeight > 0 ? implicitFooterHeight + spacing : 0))
75
76 title: switch (root.dialogType) {
77 case MessageDialog.Success:
78 return i18nc("@title:dialog", "Success");
79 case MessageDialog.Warning:
80 return i18nc("@title:dialog", "Warning");
81 case MessageDialog.Error:
82 return i18nc("@title:dialog", "Error");
83 case MessageDialog.Information:
84 return i18nc("@title:dialog", "Information");
85 default:
86 return i18nc("@title:dialog", "Warning");
87 }
88
89 padding: Kirigami.Units.largeSpacing * 2
90 bottomPadding: Kirigami.Units.largeSpacing
91
92 property bool _automaticallyClosed: false
93
94 /**
95 * Open the dialog only if the user didn't check the "do not remind me" checkbox
96 * previously.
97 */
98 function openDialog(): void {
99 if (root.dontShowAgainName) {
100 if (root.standardButtons === QQC2.Dialog.Ok) {
101 const show = MessageDialogHelper.shouldBeShownContinue(root.dontShowAgainName);
102 if (!show) {
103 root._automaticallyClosed = true;
104 root.accepted();
105 } else {
106 checkbox.checked = false;
107 root._automaticallyClosed = false;
108 root.open();
109 }
110 } else {
111 const result = MessageDialogHelper.shouldBeShownTwoActions(root.dontShowAgainName);
112 if (!result.show) {
113 root._automaticallyClosed = true;
114 if (result.result) {
115 root.accepted();
116 root.close();
117 } else {
118 root.discarded();
119 root.close();
120 }
121 } else {
122 checkbox.checked = false;
123 root._automaticallyClosed = false;
124 root.open();
125 }
126 }
127 }
128 }
129
130 onAccepted: {
131 if (root.dontShowAgainName && checkbox.checked && !root._automaticallyClosed) {
132 if (root.standardButtons === QQC2.Dialog.Ok) {
133 MessageDialogHelper.saveDontShowAgainContinue(root.dontShowAgainName);
134 } else {
135 MessageDialogHelper.saveDontShowAgainTwoActions(root.dontShowAgainName, true);
136 }
137 }
138 }
139
140 onDiscarded: {
141 if (root.dontShowAgainName && checkbox.checked && !root._automaticallyClosed) {
142 if (root.standardButtons === QQC2.Dialog.Ok) {
143 MessageDialogHelper.saveDontShowAgainContinue(root.dontShowAgainName);
144 } else {
145 MessageDialogHelper.saveDontShowAgainTwoActions(root.dontShowAgainName, false);
146 }
147 }
148 }
149
150 contentItem: GridLayout {
151 id: gridLayout
152
153 columns: root.width > Kirigami.Units.gridUnit * 18 ? 2 : 1
154 rowSpacing: Kirigami.Units.largeSpacing
155
156 Kirigami.Icon {
157 source: {
158 if (root.iconName.length > 0) {
159 return root.iconName
160 }
161 switch (root.dialogType) {
162 case MessageDialog.Success:
163 return "data-success";
164 case MessageDialog.Warning:
165 return "data-warning";
166 case MessageDialog.Error:
167 return "data-error";
168 case MessageDialog.Information:
169 return "data-information";
170 default:
171 return "data-warning";
172 }
173 }
174 Layout.preferredWidth: Kirigami.Units.iconSizes.huge
175 Layout.preferredHeight: Kirigami.Units.iconSizes.huge
176 Layout.alignment: gridLayout.columns === 2 ? Qt.AlignTop : Qt.AlignHCenter
177 }
178
179 ColumnLayout {
180 id: mainLayout
181
182 spacing: Kirigami.Units.smallSpacing
183
184 Layout.fillWidth: true
185
186 Kirigami.Heading {
187 text: root.title
188 visible: root.title
189 elide: QQC2.Label.ElideRight
190 wrapMode: Text.WordWrap
191 Layout.fillWidth: true
192 }
193 }
194 }
195
196 footer: RowLayout {
197 spacing: Kirigami.Units.largeSpacing
198
199 FormCard.FormCheckDelegate {
200 id: checkbox
201
202 visible: dontShowAgainName.length > 0
203
204 text: i18nc("@label:checkbox", "Do not show again")
205 background: null
206
207 Layout.fillWidth: true
208 }
209
210 QQC2.DialogButtonBox {
211 leftPadding: Kirigami.Units.largeSpacing * 2
212 rightPadding: Kirigami.Units.largeSpacing * 2
213 bottomPadding: Kirigami.Units.largeSpacing * 2
214 topPadding: Kirigami.Units.largeSpacing * 2
215
216 standardButtons: root.standardButtons
217
218 onAccepted: root.accepted();
219 onDiscarded: root.discarded();
220 onApplied: root.applied();
221 onHelpRequested: root.helpRequested();
222 onRejected: root.rejected();
223
224 Layout.fillWidth: true
225 }
226 }
227
228 enter: Transition {
229 NumberAnimation {
230 property: "opacity"
231 from: 0
232 to: 1
233 easing.type: Easing.InOutQuad
234 duration: Kirigami.Units.longDuration
235 }
236 }
237
238 exit: Transition {
239 NumberAnimation {
240 property: "opacity"
241 from: 1
242 to: 0
243 easing.type: Easing.InOutQuad
244 duration: Kirigami.Units.longDuration
245 }
246 }
247
248 modal: true
249 focus: true
250
251 background: Components.DialogRoundedBackground {}
252
253 // black background, fades in and out
254 QQC2.Overlay.modal: Rectangle {
255 color: Qt.rgba(0, 0, 0, 0.3)
256
257 // the opacity of the item is changed internally by QQuickPopup on open/close
258 Behavior on opacity {
259 OpacityAnimator {
260 duration: Kirigami.Units.longDuration
261 easing.type: Easing.InOutQuad
262 }
263 }
264 }
265}
A single card that follows a form style.
Definition FormCard.qml:35
A dialog to show a message.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:46:57 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.