Kirigami2

PromptDialog.qml
1/*
2 SPDX-FileCopyrightText: 2021 Devin Lin <espidev@gmail.com>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6pragma ComponentBehavior: Bound
7
8import QtQuick
9import QtQuick.Layouts
10import QtQuick.Controls as QQC2
11import org.kde.kirigami as Kirigami
12
13/**
14 * A simple dialog to quickly prompt a user with information,
15 * and possibly perform an action.
16 *
17 * Provides content padding (instead of padding outside of the scroll
18 * area). Also has a default preferredWidth, as well as the `subtitle` property.
19 *
20 * <b>Note:</b> If a `mainItem` is specified, it will replace
21 * the subtitle label, and so the respective property will have no effect.
22 *
23 * @see Dialog
24 * @see MenuDialog
25 *
26 * Example usage:
27 *
28 * @code{.qml}
29 * Kirigami.PromptDialog {
30 * title: "Reset settings?"
31 * subtitle: "The stored settings for the application will be deleted, with the defaults restored."
32 * standardButtons: Kirigami.Dialog.Ok | Kirigami.Dialog.Cancel
33 *
34 * onAccepted: console.log("Accepted")
35 * onRejected: console.log("Rejected")
36 * }
37 * @endcode
38 *
39 * Text field prompt dialog:
40 *
41 * @code{.qml}
42 * Kirigami.PromptDialog {
43 * id: textPromptDialog
44 * title: qsTr("New Folder")
45 *
46 * standardButtons: Kirigami.Dialog.NoButton
47 * customFooterActions: [
48 * Kirigami.Action {
49 * text: qsTr("Create Folder")
50 * icon.name: "dialog-ok"
51 * onTriggered: {
52 * showPassiveNotification("Created");
53 * textPromptDialog.close();
54 * }
55 * },
56 * Kirigami.Action {
57 * text: qsTr("Cancel")
58 * icon.name: "dialog-cancel"
59 * onTriggered: {
60 * textPromptDialog.close();
61 * }
62 * }
63 * ]
64 *
65 * QQC2.TextField {
66 * placeholderText: qsTr("Folder nameā€¦")
67 * }
68 * }
69 * @endcode
70 *
71 * @inherit Dialog
72 */
73Kirigami.Dialog {
74 id: root
75
76 default property alias mainItem: mainLayout.data
77
78 enum DialogType {
79 Success,
80 Warning,
81 Error,
82 Information,
83 None
84 }
85
86 /**
87 * This property holds the dialogType. It can be either:
88 *
89 * - `PromptDialog.Success`: For a sucess message
90 * - `PromptDialog.Warning`: For a warning message
91 * - `PromptDialog.Error`: For an actual error
92 * - `PromptDialog.Information`: For an informational message
93 * - `PromptDialog.None`: No specific dialog type.
94 *
95 * By default, the dialogType is `Kirigami.PromptDialog.None`
96 */
97 property int dialogType: Kirigami.PromptDialog.None
98
99 /**
100 * The text to use in the dialog's contents.
101 */
102 property string subtitle
103
104 /**
105 * The padding around the content, within the scroll area.
107 * Default is `Kirigami.Units.largeSpacing`.
108 */
109 property real contentPadding: Kirigami.Units.largeSpacing
111 /**
112 * The top padding of the content, within the scroll area.
113 */
114 property real contentTopPadding: contentPadding
116 /**
117 * The bottom padding of the content, within the scroll area.
118 */
119 property real contentBottomPadding: footer.padding === 0 ? contentPadding : 0 // add bottom padding if there is no footer
120
121 /**
122 * The left padding of the content, within the scroll area.
123 */
124 property real contentLeftPadding: contentPadding
125
126 /**
127 * The right padding of the content, within the scroll area.
128 */
129 property real contentRightPadding: contentPadding
130
131 /**
132 * This property holds the icon name used by the PromptDialog.
133 */
134 property string iconName: switch (dialogType) {
135 case Kirigami.PromptDialog.Success:
136 return "data-success";
137 case Kirigami.PromptDialog.Warning:
138 return "data-warning";
139 case Kirigami.PromptDialog.Error:
140 return "data-error";
141 case Kirigami.PromptDialog.Information:
142 return "data-information";
143 default:
144 return "";
145 }
146
147 padding: 0
148
149 header: null
150
151 Kirigami.Padding {
152 id: wrapper
153
154 topPadding: root.contentTopPadding
155 leftPadding: root.contentLeftPadding
156 rightPadding: root.contentRightPadding
157 bottomPadding: root.contentBottomPadding
158
159 contentItem: RowLayout {
160 spacing: Kirigami.Units.largeSpacing
161
162 Kirigami.Icon {
163 source: root.iconName
164 visible: root.iconName.length > 0
165
166 Layout.preferredWidth: Kirigami.Units.iconSizes.huge
167 Layout.preferredHeight: Kirigami.Units.iconSizes.huge
168 Layout.alignment: Qt.AlignTop
169 }
170
171 ColumnLayout {
172 id: mainLayout
173
174 spacing: Kirigami.Units.smallSpacing
175
176 Layout.fillWidth: true
177
178 ColumnLayout {
179 spacing: 0
180
181 Kirigami.Heading {
182 text: root.title
183 visible: root.title.length > 0
184 elide: QQC2.Label.ElideRight
185 wrapMode: Text.WordWrap
186 Layout.fillWidth: true
187 }
188
189 Kirigami.SelectableLabel {
190 text: root.subtitle
191 wrapMode: TextEdit.Wrap
192 visible: text.length > 0
193 Layout.fillWidth: true
194 }
195 }
196 }
197 }
198 }
199}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:05:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.