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: switch (root.dialogType) {
57 case MessageDialog.Success:
58 return "data-success";
59 case MessageDialog.Warning:
60 return "data-warning";
61 case MessageDialog.Error:
62 return "data-error";
63 case MessageDialog.Information:
64 return "data-information";
65 default:
66 return "data-warning";
67 }
69 x: Math.round((parent.width - width) / 2)
70 y: Math.round((parent.height - height) / 2)
71 z: Kirigami.OverlayZStacking.z
72
73 parent: applicationWindow().QQC2.Overlay.overlay
74
75 implicitWidth: Math.min(parent.width - Kirigami.Units.gridUnit * 2, Kirigami.Units.gridUnit * 20)
76 implicitHeight: Math.min(Math.max(implicitBackgroundHeight + topInset + bottomInset,
77 contentHeight + topPadding + bottomPadding
78 + (implicitHeaderHeight > 0 ? implicitHeaderHeight + spacing : 0)
79 + (implicitFooterHeight > 0 ? implicitFooterHeight + spacing : 0)), parent.height - Kirigami.Units.gridUnit * 2, Kirigami.Units.gridUnit * 30)
80
81 title: switch (root.dialogType) {
82 case MessageDialog.Success:
83 return i18ndc("kirigami-addons6", "@title:dialog", "Success");
84 case MessageDialog.Warning:
85 return i18ndc("kirigami-addons6", "@title:dialog", "Warning");
86 case MessageDialog.Error:
87 return i18ndc("kirigami-addons6", "@title:dialog", "Error");
88 case MessageDialog.Information:
89 return i18ndc("kirigami-addons6", "@title:dialog", "Information");
90 default:
91 return i18ndc("kirigami-addons6", "@title:dialog", "Warning");
92 }
93
94 padding: Kirigami.Units.largeSpacing * 2
95 bottomPadding: Kirigami.Units.largeSpacing
96
97 property bool _automaticallyClosed: false
98
99 /**
100 * Open the dialog only if the user didn't check the "do not remind me" checkbox
101 * previously.
102 */
103 function openDialog(): void {
104 if (root.dontShowAgainName.length > 0) {
105 if (root.standardButtons === QQC2.Dialog.Ok) {
106 const show = MessageDialogHelper.shouldBeShownContinue(root.dontShowAgainName);
107 if (!show) {
108 root._automaticallyClosed = true;
109 root.applied();
110 root.accepted();
111 } else {
112 checkbox.checked = false;
113 root._automaticallyClosed = false;
114 root.open();
115 }
116 } else {
117 const result = MessageDialogHelper.shouldBeShownTwoActions(root.dontShowAgainName);
118 if (!result.show) {
119 root._automaticallyClosed = true;
120 if (result.result) {
121 root.accepted();
122 root.applied();
123 } else {
124 root.discarded();
125 }
126 } else {
127 checkbox.checked = false;
128 root._automaticallyClosed = false;
129 root.open();
130 }
131 }
132 }
133 }
134
135 onApplied: {
136 if (root.dontShowAgainName && checkbox.checked && !root._automaticallyClosed) {
137 if (root.standardButtons === QQC2.Dialog.Ok) {
138 MessageDialogHelper.saveDontShowAgainContinue(root.dontShowAgainName);
139 } else {
140 MessageDialogHelper.saveDontShowAgainTwoActions(root.dontShowAgainName, true);
141 }
142 }
143 }
144
145 onAccepted: {
146 if (root.dontShowAgainName && checkbox.checked && !root._automaticallyClosed) {
147 if (root.standardButtons === QQC2.Dialog.Ok) {
148 MessageDialogHelper.saveDontShowAgainContinue(root.dontShowAgainName);
149 } else {
150 MessageDialogHelper.saveDontShowAgainTwoActions(root.dontShowAgainName, true);
151 }
152 }
153 }
154
155 onDiscarded: {
156 if (root.dontShowAgainName && checkbox.checked && !root._automaticallyClosed) {
157 if (root.standardButtons === QQC2.Dialog.Ok) {
158 MessageDialogHelper.saveDontShowAgainContinue(root.dontShowAgainName);
159 } else {
160 MessageDialogHelper.saveDontShowAgainTwoActions(root.dontShowAgainName, false);
161 }
162 }
163 }
164
165 contentItem: GridLayout {
166 id: gridLayout
167
168 columns: icon.visible && root.width > Kirigami.Units.gridUnit * 18 ? 2 : 1
169 rowSpacing: Kirigami.Units.largeSpacing
170
171 Kirigami.Icon {
172 id: icon
173
174 visible: root.iconName.length > 0
175 source: root.iconName
176 Layout.preferredWidth: Kirigami.Units.iconSizes.huge
177 Layout.preferredHeight: Kirigami.Units.iconSizes.huge
178 Layout.alignment: gridLayout.columns === 2 ? Qt.AlignTop : Qt.AlignHCenter
179 }
180
181 ColumnLayout {
182 id: mainLayout
183
184 spacing: Kirigami.Units.smallSpacing
185
186 Layout.fillWidth: true
187
188 Kirigami.Heading {
189 text: root.title
190 visible: root.title
191 elide: QQC2.Label.ElideRight
192 wrapMode: Text.WordWrap
193 Layout.fillWidth: true
194 }
195 }
196 }
197
198 footer: RowLayout {
199 spacing: Kirigami.Units.largeSpacing
200
201 FormCard.FormCheckDelegate {
202 id: checkbox
203
204 visible: dontShowAgainName.length > 0
205
206 text: i18ndc("kirigami-addons6", "@label:checkbox", "Do not show again")
207 background: null
208
209 Layout.fillWidth: true
210 }
211
212 QQC2.DialogButtonBox {
213 leftPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
214 rightPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
215 bottomPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
216 topPadding: Kirigami.Units.largeSpacing + Kirigami.Units.smallSpacing
217
218 standardButtons: root.standardButtons
219
220 onAccepted: root.accepted();
221 onDiscarded: root.discarded();
222 onApplied: root.applied();
223 onHelpRequested: root.helpRequested();
224 onRejected: root.rejected();
225
226 Layout.fillWidth: true
227 }
228 }
229
230 enter: Transition {
231 NumberAnimation {
232 property: "opacity"
233 from: 0
234 to: 1
235 easing.type: Easing.InOutQuad
236 duration: Kirigami.Units.longDuration
237 }
238 }
239
240 exit: Transition {
241 NumberAnimation {
242 property: "opacity"
243 from: 1
244 to: 0
245 easing.type: Easing.InOutQuad
246 duration: Kirigami.Units.longDuration
247 }
248 }
249
250 modal: true
251 focus: true
252
253 background: Components.DialogRoundedBackground {}
254
255 // black background, fades in and out
256 QQC2.Overlay.modal: Rectangle {
257 color: Qt.rgba(0, 0, 0, 0.3)
258
259 // the opacity of the item is changed internally by QQuickPopup on open/close
260 Behavior on opacity {
261 OpacityAnimator {
262 duration: Kirigami.Units.longDuration
263 easing.type: Easing.InOutQuad
264 }
265 }
266 }
267}
A single card that follows a form style.
Definition FormCard.qml:35
A dialog to show a message.
QString i18ndc(const char *domain, 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 Sat Dec 21 2024 17:03:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.