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

KDE's Doxygen guidelines are available online.