Kirigami2

PromptDialog.qml
1/*
2 SPDX-FileCopyrightText: 2021 Devin Lin <espidev@gmail.com>
3 SPDX-License-Identifier: GPL-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 implicitWidth: Math.min(preferredWidth, maximumWidth) + leftPadding + rightPadding
149 preferredWidth: Kirigami.Units.gridUnit * 18
150
151 header: null
152
153 Kirigami.Padding {
154 id: wrapper
155
156 topPadding: root.contentTopPadding
157 leftPadding: root.contentLeftPadding
158 rightPadding: root.contentRightPadding
159 bottomPadding: root.contentBottomPadding
160
161 contentItem: RowLayout {
162 spacing: Kirigami.Units.largeSpacing
163
164 Kirigami.Icon {
165 source: root.iconName
166 visible: root.iconName.length > 0
167
168 Layout.preferredWidth: Kirigami.Units.iconSizes.huge
169 Layout.preferredHeight: Kirigami.Units.iconSizes.huge
170 Layout.alignment: Qt.AlignTop
171 }
172
173 ColumnLayout {
174 id: mainLayout
175
176 spacing: Kirigami.Units.smallSpacing
177
178 Layout.fillWidth: true
179
180 ColumnLayout {
181 spacing: 0
182
183 Kirigami.Heading {
184 text: root.title
185 visible: root.title.length > 0
186 elide: QQC2.Label.ElideRight
187 wrapMode: Text.WordWrap
188 Layout.fillWidth: true
189 }
190
191 Kirigami.SelectableLabel {
192 text: root.subtitle
193 wrapMode: TextEdit.Wrap
194 visible: text.length > 0
195 Layout.fillWidth: true
196 }
197 }
198 }
199 }
200 }
201}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 12 2024 12:00:02 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.