Kirigami-addons

FormGridContainer.qml
1// SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
2// SPDX-FileCopyrightText: 2023 Rishi Kumar <rsi.dev17@gmail.com>
3// SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
4// SPDX-License-Identifier: LGPL-2.0-or-later
5
6import QtQml
7import QtQuick
8import QtQuick.Controls as QQC2
9import QtQuick.Templates as T
10import QtQuick.Layouts
11
12import org.kde.kirigami as Kirigami
13
14import "private" as Private
15
16/**
17 * This component render a grid of small cards.
18 *
19 * This is used to display multiple information in a FormCard.FormLayout
20 * without taking too much vertical space.
21 *
22 * @code{.qml}
23 * import org.kde.kirigamiaddons.formcard as FormCard
24 *
25 * FormCard.FormGridContainer {
26 * id: container
27 *
28 * Layout.topMargin: Kirigami.Units.largeSpacing
29 * Layout.fillWidth: true
30 *
31 * infoCards: [
32 * FormCard.FormGridContainer.InfoCard {
33 * title: "42"
34 * subtitle: i18nc("@info:Number of Posts", "Posts")
35 * },
36 * FormCard.FormGridContainer.InfoCard {
37 * title: "42"
38 * subtitle: i18nc("@info:Number of followers.", "Followers")
39 * }
40 * ]
41 * }
42 * @endcode
43 *
44 *
45 * @since KirigamiAddons 0.11.0
46 *
47 * @inherit QtQuick.Item
48 */
49Item {
50 id: root
51
52 /**
53 * This property holds the maximum width of the grid.
54 */
55 property real maximumWidth: Kirigami.Units.gridUnit * 30
56
57 /**
58 * @brief This property holds the padding used around the content edges.
59 *
60 * default: `0`
61 */
62 property real padding: 0
63 property real verticalPadding: padding
64 property real horizontalPadding: padding
65 property real topPadding: verticalPadding
66 property real bottomPadding: verticalPadding
67 property real leftPadding: horizontalPadding
68 property real rightPadding: horizontalPadding
69
70 /**
71 * This property holds whether the card's width is being restricted.
72 */
73 readonly property bool cardWidthRestricted: root.width > root.maximumWidth
74
75 /**
76 * This property holds the InfoCards which should be displayed.
77 *
78 * Each InfoCard contains a title and an optional subtitle
79 *
80 * @code{.qml}
81 * import org.kde.kirigamiaddons.formcard as FormCard
82 *
83 * FormCard.FormGridContainer {
84 * infoCards: [
85 * FormCard.FormGridContainer.InfoCard {
86 * title: "42"
87 * subtitle: i18nc("@info:Number of Posts", "Posts")
88 * },
89 * FormCard.FormGridContainer.InfoCard {
90 * title: "42"
91 * },
92 * FormCard.FormGridContainer.InfoCard {
93 * title: "Details"
94 * action: Kirigami.Action {
95 * onClicked: pageStack.push("Details.qml")
96 * }
97 * }
98 * ]
99 * }
100 * @endcode
101 */
102 property list<QtObject> infoCards
103
104 component InfoCard: QtObject {
105 property bool visible: true
106 property string title
107 property string subtitle
108 property string buttonIcon
109 property string tooltipText
110 property int subtitleTextFormat: Text.AutoText
111 property Kirigami.Action action
112 }
113
114 Kirigami.Theme.colorSet: Kirigami.Theme.View
115 Kirigami.Theme.inherit: false
116
117 implicitHeight: topPadding + bottomPadding + grid.implicitHeight
118
119 Item {
120 anchors {
121 top: parent.top
122 bottom: parent.bottom
123 left: parent.left
124 right: parent.right
125
126 leftMargin: root.cardWidthRestricted ? Math.round((root.width - root.maximumWidth) / 2) : 0
127 rightMargin: root.cardWidthRestricted ? Math.round((root.width - root.maximumWidth) / 2) : 0
128 }
129
130 GridLayout {
131 id: grid
132
133 readonly property int cellWidth: Kirigami.Units.gridUnit * 10
134 readonly property int visibleChildrenCount: visibleChildren.length - 1
135
136 anchors {
137 fill: parent
138 leftMargin: root.leftPadding
139 rightMargin: root.rightPadding
140 topMargin: root.topPadding
141 bottomMargin: root.bottomPadding
142 }
143
144 columns: root.cardWidthRestricted && grid.visibleChildrenCount % 3 === 0 ? 3 : 2
145 columnSpacing: Kirigami.Units.smallSpacing
146 rowSpacing: Kirigami.Units.smallSpacing
147
148 Repeater {
149 id: cardRepeater
150
151 model: root.infoCards
152
153 QQC2.AbstractButton {
154 id: infoCardDelegate
155
156 required property int index
157 required property QtObject modelData
158
159 readonly property string title: modelData.title
160 readonly property string subtitle: modelData.subtitle
161 readonly property string buttonIcon: modelData.buttonIcon
162 readonly property string tooltipText: modelData.tooltipText
163 readonly property int subtitleTextFormat: modelData.subtitleTextFormat
164
165 visible: modelData.visible
166
167 action: modelData.action
168
169 leftPadding: Kirigami.Units.largeSpacing
170 rightPadding: Kirigami.Units.largeSpacing
171 topPadding: Kirigami.Units.largeSpacing
172 bottomPadding: Kirigami.Units.largeSpacing
173
174 leftInset: root.cardWidthRestricted ? 0 : -infoCardDelegate.background.border.width
175 rightInset: root.cardWidthRestricted ? 0 : -infoCardDelegate.background.border.width
176
177 hoverEnabled: true
178
179 Accessible.name: title + " " + subtitle
180 Accessible.role: action ? Accessible.Button : Accessible.Note
181
182 Layout.preferredWidth: grid.cellWidth
183 Layout.columnSpan: grid.visibleChildrenCount % grid.columns !== 0 && index === grid.visibleChildrenCount - 1 ? 2 : 1
184 Layout.fillWidth: true
185 Layout.fillHeight: true
186
187 QQC2.ToolTip.text: tooltipText
188 QQC2.ToolTip.visible: tooltipText && hovered
189 QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
190
191 background: Rectangle {
192 radius: root.cardWidthRestricted ? Kirigami.Units.cornerRadius : 0
193 color: Kirigami.Theme.backgroundColor
194
195 border {
196 color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, Kirigami.Theme.frameContrast)
197 width: 1
198 }
199
200 Rectangle {
201 anchors.fill: parent
202 radius: root.cardWidthRestricted ? Kirigami.Units.cornerRadius : 0
203
204 color: {
205 let alpha = 0;
206
207 if (!infoCardDelegate.enabled || !infoCardDelegate.action) {
208 alpha = 0;
209 } else if (infoCardDelegate.pressed) {
210 alpha = 0.2;
211 } else if (infoCardDelegate.visualFocus) {
212 alpha = 0.1;
213 } else if (!Kirigami.Settings.tabletMode && infoCardDelegate.hovered) {
214 alpha = 0.07;
215 }
216
217 return Qt.rgba(Kirigami.Theme.textColor.r, Kirigami.Theme.textColor.g, Kirigami.Theme.textColor.b, alpha)
218 }
219
220 Behavior on color {
221 ColorAnimation { duration: Kirigami.Units.shortDuration }
222 }
223 }
224 }
225
226 contentItem: RowLayout {
227 Kirigami.Icon {
228 id: icon
229
230 source: infoCardDelegate.buttonIcon
231 visible: source
232 Layout.alignment: Qt.AlignTop
233 }
234
235 ColumnLayout {
236 spacing: 0
237
238 // Title
239 Kirigami.Heading {
240 Layout.fillWidth: true
241 level: 4
242 text: infoCardDelegate.title
243 verticalAlignment: Text.AlignVCenter
244 horizontalAlignment: icon.visible ? Text.AlignLeft : Text.AlignHCenter
245 maximumLineCount: 2
246 elide: Text.ElideRight
247 wrapMode: Text.Wrap
248 }
249
250 // Subtitle
251 QQC2.Label {
252 Layout.fillWidth: true
253 Layout.fillHeight: true
254 visible: infoCardDelegate.subtitle
255 text: infoCardDelegate.subtitle
256 horizontalAlignment: icon.visible ? Text.AlignLeft : Text.AlignHCenter
257 elide: Text.ElideRight
258 wrapMode: Text.Wrap
259 textFormat: infoCardDelegate.subtitleTextFormat
260 opacity: 0.6
261 verticalAlignment: Text.AlignTop
262 onLinkActivated: (link) => modelData.linkActivated(link)
263 }
264 }
265 }
266 }
267 }
268 }
269 }
270}
KIOCORE_EXPORT CopyJob * link(const QList< QUrl > &src, const QUrl &destDir, JobFlags flags=DefaultFlags)
QStringView level(QStringView ifopt)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:12:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.