Kirigami-addons

Banner.qml
1// SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
2// SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
3
4import QtQuick 2.15
5import QtQuick.Controls 2.15 as QQC2
6import QtQuick.Templates 2.15 as T
7import QtQuick.Layouts 1.15
8import org.kde.kirigami 2.20 as Kirigami
9
10/**
11 * @brief An banner Item with support for informational, positive,
12 * warning and error types, and with support for associated actions.
13 *
14 * Banner can be used to inform or interact with the user
15 * without requiring the use of a dialog and are positionned in the footer
16 * or header of a page. For inline content, see org::kde::kirigami::InlineMessage.
17 *
18 * The Banner is hidden by default.
19 *
20 * Optionally, actions can be added which are shown alongside an
21 * optional close button on the right side of the Item. If more
22 * actions are set than can fit, an overflow menu is provided.
23 *
24 * Example usage:
25 * @code{.qml}
26 * Banner {
27 * type: Kirigami.MessageType.Error
28 *
29 * text: "My error message"
30 *
31 * actions: [
32 * Kirigami.Action {
33 * icon.name: "edit"
34 * text: "Action text"
35 * onTriggered: {
36 * // do stuff
37 * }
38 * },
39 * Kirigami.Action {
40 * icon.name: "edit"
41 * text: "Action text"
42 * onTriggered: {
43 * // do stuff
44 * }
45 * }
46 * ]
47 * }
48 * @endcode
49 * @since KirigamiAddons 0.10.0
50 * @inherit QtQuick.Controls.Control
51 */
52T.ToolBar {
53 id: root
54
55 /**
56 * @brief This signal is emitted when a link is hovered in the message text.
57 * @param The hovered link.
58 */
59 signal linkHovered(string link)
60
61 /**
62 * @brief This signal is emitted when a link is clicked or tapped in the message text.
63 * @param The clicked or tapped link.
64 */
65 signal linkActivated(string link)
67 /**
68 * @brief This property holds the link embedded in the message text that the user is hovering over.
69 */
70 readonly property alias hoveredLink: label.hoveredLink
71
72 /**
73 * @brief This property holds the message type.
74 *
75 * The following values are allowed:
76 * * ``Kirigami.MessageType.Information``
77 * * ``Kirigami.MessageType.Positive``
78 * * ``Kirigami.MessageType.Warning``
79 * * ``Kirigami.MessageType.Error``
80 *
81 * default: ``Kirigami.MessageType.Information``
82 *
83 * @property Kirigami.MessageType type
84 */
85 property int type: Kirigami.MessageType.Information
86
87 /**
88 * @brief This property holds the message title.
89 */
90 property string title
91
92 /**
93 * @brief This property holds the message text.
94 */
95 property string text
96
97 /**
98 * @brief This property holds whether the close button is displayed.
99 *
100 * default: ``false``
101 */
102 property bool showCloseButton: false
103
104 /**
105 * This property holds the list of Kirigami Actions to show in the banner's
106 * internal kirigami::ActionToolBar.
107 *
108 * Actions are added from left to right. If more actions
109 * are set than can fit, an overflow menu is provided.
110 */
111 property list<Kirigami.Action> actions
112
113 padding: Kirigami.Units.smallSpacing
114
115 implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset, contentWidth + leftPadding + rightPadding)
116 implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset, contentHeight + topPadding + bottomPadding)
117
118 visible: false
119
120 contentItem: GridLayout {
121 columns: 3
122 columnSpacing: Kirigami.Units.mediumSpacing
123 rowSpacing: 0
124
125 Item {
126 Layout.preferredWidth: closeButton.visible ? closeButton.implicitWidth : Kirigami.Units.iconSizes.medium
127 Layout.preferredHeight: closeButton.visible ? closeButton.implicitWidth : Kirigami.Units.iconSizes.medium
128 Layout.alignment: Qt.AlignTop
129 Layout.rowSpan: 2
130
131 Kirigami.Icon {
132 source: {
133 switch (root.type) {
134 case Kirigami.MessageType.Positive:
135 return "emblem-positive";
136 case Kirigami.MessageType.Warning:
137 return "data-warning";
138 case Kirigami.MessageType.Error:
139 return "data-error";
140 default:
141 return "data-information";
142 }
143 }
144
145 anchors.centerIn: parent
146 }
147 }
148
149 Kirigami.Heading {
150 id: heading
151
152 level: 2
153 text: root.title
154 visible: text.length > 0
155
156 Layout.row: visible ? 0 : 1
157 Layout.column: 1
158 }
159
160 Kirigami.SelectableLabel {
161 id: label
162
163 color: Kirigami.Theme.textColor
164 wrapMode: Text.WordWrap
165
166 text: root.text
167
168 verticalAlignment: Text.AlignVCenter
169
170 onLinkHovered: link => root.linkHovered(link)
171 onLinkActivated: link => root.linkActivated(link)
172
173 Layout.fillWidth: true
174 Layout.fillHeight: true
175 Layout.minimumHeight: closeButton.visible ? closeButton.implicitWidth : Kirigami.Units.iconSizes.medium
176 Layout.alignment: heading.text.length > 0 || label.lineCount > 1 ? Qt.AlignTop : Qt.AlignBaseline
177
178 Layout.row: heading.visible ? 1 : 0
179 Layout.column: 1
180 }
181
182 QQC2.ToolButton {
183 id: closeButton
184
185 visible: root.showCloseButton
186 text: i18ndc("kirigami-addons6", "@action:button", "Close")
187
188 icon.name: "dialog-close"
189 display: QQC2.ToolButton.IconOnly
190
191 onClicked: root.visible = false
192
193 Layout.alignment: Qt.AlignTop
194
195 QQC2.ToolTip.visible: Kirigami.Settings.tabletMode ? closeButton.pressed : closeButton.hovered
196 QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
197 QQC2.ToolTip.text: text
198
199 Layout.rowSpan: 2
200 Layout.column: 2
201 }
202
203 Kirigami.ActionToolBar {
204 id: actionsLayout
205
206 flat: false
207 actions: root.actions
208 visible: root.actions.length > 0
209 alignment: Qt.AlignRight
210
211 Layout.column: 0
212 Layout.columnSpan: 3
213 Layout.row: 2
214 }
215 }
216
217 background: Item {
218
219 Rectangle {
220 color: {
221 switch (root.type) {
222 case Kirigami.MessageType.Positive:
223 return Kirigami.Theme.positiveBackgroundColor;
224 case Kirigami.MessageType.Warning:
225 return Kirigami.Theme.neutralBackgroundColor;
226 case Kirigami.MessageType.Error:
227 return Kirigami.Theme.negativeBackgroundColor;
228 default:
229 return Kirigami.Theme.activeBackgroundColor;
230 }
231 }
232 anchors.fill: parent
233 }
234
235 Rectangle {
236 id: separator
237
238 height: 1
239 color: {
240 let separatorColor = Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, Kirigami.Theme.frameContrast);
241 let textColor = Kirigami.Theme.activeTextColor;
242
243 switch (root.type) {
244 case Kirigami.MessageType.Positive:
245 textColor = Kirigami.Theme.positiveTextColor;
246 break;
247 case Kirigami.MessageType.Warning:
248 textColor = Kirigami.Theme.neutralTextColor;
249 break;
250 case Kirigami.MessageType.Error:
251 textColor = Kirigami.Theme.negativeTextColor;
252 break;
253 }
254
255 return Qt.hsla(textColor.hslHue, textColor.hslSaturation, separatorColor.hslLightness, 1);
256 }
257
258 anchors {
259 left: parent.left
260 right: parent.right
261 top: root.position === QQC2.ToolBar.Header ? parent.bottom : undefined
262 bottom: root.position === QQC2.ToolBar.Header ? undefined : parent.top
263 }
264 }
265 }
266}
QString i18ndc(const char *domain, const char *context, const char *text, const TYPE &arg...)
qsizetype length() const const
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
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.