KCMUtils

GridDelegate.qml
1/*
2 SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7import QtQuick
8import QtQuick.Layouts
9import QtQuick.Controls as QQC2
10import QtQuick.Templates as T2
11import org.kde.kirigami 2 as Kirigami
12
13/**
14 * Base delegate for KControlmodules based on Grid views of thumbnails
15 * Use the onClicked signal handler for managing the main action when
16 * the user clicks on the thumbnail
17 * @inherits QtQuick.Templates.ItemDelegate
18 */
19T2.ItemDelegate {
20 id: delegate
21
22 /**
23 * toolTip: string
24 * string for a tooltip for the whole delegate
25 */
26 property string toolTip
27
28 /**
29 * subtitle: string
30 * optional string for the text to show below the main label
31 */
32 property string subtitle
34 /**
35 * thumbnail: Item
36 * the item actually implementing the thumbnail: the visualization is up to the implementation
37 */
38 property alias thumbnail: thumbnailArea.data
39
40 /**
41 * thumbnailAvailable: bool
42 * Set it to true when a thumbnail is actually available: when false,
43 * only an icon will be shown instead of the actual thumbnail
44 * ("edit-none" if pluginName is "None", otherwise it uses "view-preview").
45 */
46 property bool thumbnailAvailable: false
47
48 /**
49 * actions: list<Kirigami.Action>
50 * A list of extra actions for the thumbnails. They will be shown as
51 * icons on the bottom-right corner of the thumbnail on mouse over
52 */
53 property list<Kirigami.Action> actions
54
55 width: GridView.view.cellWidth
56 height: GridView.view.cellHeight
57 hoverEnabled: !Kirigami.Settings.isMobile
58
59 Accessible.description: {
60 if (delegate.toolTip.length === 0) {
61 return subtitle;
62 } else if (delegate.subtitle.length === 0) {
63 return toolTip;
64 }
65 return `${subtitle}; ${toolTip}`
66 }
67
68 Keys.onEnterPressed: thumbnail.trigger()
69 Keys.onMenuPressed: thumbnail.trigger()
70 Keys.onSpacePressed: thumbnail.trigger()
71
72 Kirigami.ShadowedRectangle {
73 id: thumbnail
74 anchors {
75 centerIn: parent
76 verticalCenterOffset: Math.ceil(-labelLayout.height / 2)
77 }
78 width: Kirigami.Settings.isMobile ? delegate.width - Kirigami.Units.gridUnit : Math.min(delegate.GridView.view.implicitCellWidth, delegate.width - Kirigami.Units.gridUnit)
79 height: Kirigami.Settings.isMobile ? Math.round((delegate.width - Kirigami.Units.gridUnit) / 1.6)
80 : Math.min(delegate.GridView.view.implicitCellHeight - Kirigami.Units.gridUnit * 3,
81 delegate.height - Kirigami.Units.gridUnit)
82 radius: Kirigami.Units.smallSpacing
83 Kirigami.Theme.inherit: false
84 Kirigami.Theme.colorSet: Kirigami.Theme.View
85
86 shadow.xOffset: 0
87 shadow.yOffset: 2
88 shadow.size: 10
89 shadow.color: Qt.rgba(0, 0, 0, 0.3)
90
91 color: {
92 if (delegate.GridView.isCurrentItem) {
93 if (delegate.enabled && delegate.GridView.view.neutralHighlight) {
94 return Kirigami.Theme.neutralTextColor;
95 }
96 return Kirigami.Theme.highlightColor;
97 }
98 if (delegate.enabled && delegate.hovered) {
99 // Match appearance of hovered list items
100 return Qt.alpha(Kirigami.Theme.highlightColor, 0.5);
101 }
102 return Kirigami.Theme.backgroundColor;
103 }
104
105 // The menu is only used for keyboard navigation, so no need to always load
106 // it. This speeds up the compilation of GridDelegate.
107 property QQC2.Menu menu: null
108
109 function trigger() {
110 if (menu) {
111 menu.trigger();
112 return;
113 }
114 const component = Qt.createComponent("private/GridDelegateMenu.qml");
115 menu = component.createObject(delegate);
116 menu.trigger();
117 component.destroy();
118 }
119
120 Rectangle {
121 id: thumbnailArea
122
123 radius: Kirigami.Units.smallSpacing / 2
124 anchors {
125 fill: parent
126 margins: Kirigami.Units.smallSpacing
127 }
128
129 color: Kirigami.Theme.backgroundColor
130
131 // "None/There's nothing here" indicator
132 Kirigami.Icon {
133 visible: !delegate.thumbnailAvailable
134 anchors.centerIn: parent
135 width: Kirigami.Units.iconSizes.large
136 height: Kirigami.Units.iconSizes.large
137 source: typeof pluginName === "string" && pluginName === "None" ? "edit-none" : "view-preview"
138 }
139
140 RowLayout {
141 anchors {
142 right: parent.right
143 rightMargin: Kirigami.Units.smallSpacing
144 bottom: parent.bottom
145 bottomMargin: Kirigami.Units.smallSpacing
146 }
147 spacing: Kirigami.Units.smallSpacing
148
149 // Always show above thumbnail content
150 z: 9999
151
152 visible: delegate.actions.length > 0 && (Kirigami.Settings.isMobile || delegate.hovered || delegate.GridView.isCurrentItem)
153
154 Repeater {
155 model: delegate.actions
156 delegate: QQC2.Button {
157 required property Kirigami.Action modelData
158
159 icon.name: modelData.icon.name
160 text: modelData.text || modelData.tooltip
161 display: QQC2.AbstractButton.IconOnly
162
163 enabled: modelData.enabled
164 visible: modelData.visible
165
166 activeFocusOnTab: false
167
168 onClicked: modelData.trigger()
169
170 QQC2.ToolTip.text: text
171 QQC2.ToolTip.visible: (Kirigami.Settings.tabletMode ? pressed : hovered) && (text !== "")
172 QQC2.ToolTip.delay: Kirigami.Settings.tabletMode ? Qt.styleHints.mousePressAndHoldInterval : Kirigami.Units.toolTipDelay
173 }
174 }
175 }
176 }
177 }
178
179 ColumnLayout {
180 id: labelLayout
181
182 spacing: 0
183 height: Kirigami.Units.gridUnit * 2
184 anchors {
185 left: thumbnail.left
186 right: thumbnail.right
187 top: thumbnail.bottom
188 topMargin: Kirigami.Units.largeSpacing
189 }
190
191 QQC2.Label {
192 id: title
193
194 Layout.fillWidth: true
195 horizontalAlignment: Text.AlignHCenter
196 verticalAlignment: Text.AlignTop
197 text: delegate.text
198 color: enabled ? Kirigami.Theme.textColor : Kirigami.Theme.disabledTextColor
199 elide: Text.ElideRight
200 font.bold: delegate.GridView.isCurrentItem
201 textFormat: Text.PlainText
202 }
203 QQC2.Label {
204 id: caption
205
206 Layout.fillWidth: true
207 horizontalAlignment: Text.AlignHCenter
208 visible: delegate.subtitle.length > 0
209 opacity: 0.6
210 text: delegate.subtitle
211 font.pointSize: Kirigami.Theme.smallFont.pointSize
212 font.bold: delegate.GridView.isCurrentItem
213 elide: Text.ElideRight
214 textFormat: Text.PlainText
215 }
216
217 Rectangle {
218 Layout.preferredHeight: 1
219 Layout.preferredWidth: Math.max(title.paintedWidth, caption.paintedWidth)
220 Layout.maximumWidth: labelLayout.width // Otherwise labels can overflow
221 Layout.alignment: Qt.AlignHCenter
222
223 color: Kirigami.Theme.highlightColor
224
225 opacity: delegate.visualFocus ? 1 : 0
226 }
227
228 Item { Layout.fillWidth: true; Layout.fillHeight: true; }
229 }
230
231 QQC2.ToolTip.delay: 1000
232 QQC2.ToolTip.timeout: 5000
233 QQC2.ToolTip.visible: hovered && (delegate.toolTip.length > 0 || title.truncated || caption.truncated)
234 QQC2.ToolTip.text: {
235 if (delegate.toolTip.length > 0) {
236 return delegate.toolTip;
237 }
238 const parts = [];
239 if (title.truncated) {
240 parts.push(title.text);
241 }
242 if (caption.truncated) {
243 parts.push(caption.text);
244 }
245 return parts.join("\n");
246 }
247}
A ScrollView containing a GridView, with the default behavior about sizing and background as recommen...
Definition GridView.qml:16
alias view
view: GridView Exposes the internal GridView: in order to set a model or a delegate to it,...
Definition GridView.qml:30
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 Tue Mar 26 2024 11:17:56 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.