MauiKit Controls

IconItem.qml
1/*
2 * Copyright 2018 Camilo Higuita <milo.h@aol.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20import QtQuick
21import QtQuick.Controls
22import QtQuick.Effects
23
24import org.mauikit.controls as Maui
25
26/**
27 * @inherit QtQuick.Item
28 * @since org.mauikit.controls 1.0
29 * @brief An element to display an icon from the icon theme or file asset; or an image from a local file or a remote URL.
30 *
31 * <a href="https://doc.qt.io/qt-6/qml-qtquick-controls-item.html">This controls inherits from QQC2 Item, to checkout its inherited properties refer to the Qt Docs.</a>
32 *
33 * @note The image can be masked to have a rounded borders.
34 * @see maskRadius
35 *
36 * @image html Misc/iconitem.png
37 *
38 * @code
39 * Row
40 * {
41 * anchors.centerIn: parent
42 * spacing: Maui.Style.space.big
43 *
44 * Maui.IconItem
45 * {
46 * imageSource: "file:///home/camiloh/Downloads/premium_photo-1664203068007-52240d0ca48f.avif"
47 * imageSizeHint: 200
48 * maskRadius: 100
49 * fillMode: Image.PreserveAspectCrop
50 *
51 * }
52 *
53 * Maui.IconItem
54 * {
55 * iconSource: "vvave"
56 * iconSizeHint: 94
57 * }
58 * }
59 * @endcode
60 *
61 * @section notes Notes
62 *
63 * By default this item is only visible if the image source is ready or if the icon is valid. You can make it always visible, but would be a better idea to set a fallback icon with `icon.fallback: "icon-name"`.
64 *
65 * <a href="https://invent.kde.org/maui/mauikit/-/blob/qt6-2/examples/SideBarView.qml">You can find a more complete example at this link.</a>
66 */
67Item
68{
69 id: control
70
71 visible: icon.valid || img.status === Image.Ready
72
73 implicitHeight: visible ? Math.max(img.height, icon.height, 0) : 0
74 implicitWidth: visible ? Math.max(img.width, icon.width, 0) : 0
76 smooth: !Maui.Handy.isMobile
77
78 /**
79 * @brief Whether the control should be styled as being highlighted by some external event.
80 * By default this is set to `false`.
81 * @note When highlighted a monochromatic icon will take the color fo the accent color.
82 */
83 property bool highlighted: false
84
85 /**
86 * @brief Whether the control should be styled as being hovered by a cursor.
87 * By default his is set to `false`.
88 */
89 property bool hovered: false
90
91 /**
92 * @brief A hint for the size of the icon. It will never be larger than the actual container size.
93 * @note If the container size is 200x200, and the icon size hint has been set to 64, then the icon will be centered. If the icon size hint is larger, then the maximum value will be the container size.
94 *
95 * By default this is set to `Style.iconSizes.big`.
96 */
97 property int iconSizeHint : Maui.Style.iconSizes.big
98
99 /**
100 * @brief A hint for the size of the image. It will never be larger than the actual container size.
101 * @note If the container size is 200x200, and the image size hint has been set to 140, then the image will be aligned following the `alignment` property. If the image size hint is larger, then the maximum value will be the container size.
102 * @see alignment
103 *
104 * By default this is set to `-1`.
105 */
106 property int imageSizeHint : -1
107
108 /**
109 * @brief The local or remote file URL of the image to be used.
110 * @property string IconItem::imageSource
111 */
112 property alias imageSource : img.source
113
114 /**
115 * @brief The name of the icon to be used.
116 * @property string IconItem::iconSource
117 */
118 property alias iconSource : icon.source
119
120 /**
121 * @brief The preferred fill mode for the image.
122 * By default this is set to `Image.PreserveAspectFit`.
123 * @note For more options and information review the QQC2 Image documentation.
124 * @property enum IconItem::fillMode
125 */
126 property alias fillMode : img.fillMode
128 /**
129 * @brief The border radius to mask the icon/image header section.
130 * By default this is set to `0`.
131 */
132 property int maskRadius: 0
133
134 /**
135 * @brief The painted width size of the image. This will make the image resolution fit this size.
136 * By default this is set to `-1`, which means that the image will be loaded with its original resolution size.
137 */
138 property int imageWidth : -1
139
140 /**
141 * @brief The painted height size of the image. This will make the image resolution fit this size.
142 * By default this is set to `-1`, which means that the image will be loaded with its original resolution size.
143 */
144 property int imageHeight : -1
146 /**
147 * @brief Whether the icon should be masked and tinted with the text color, this is used for monochromatic icons. If you plan to use a colorful icon, consider setting this property to `false`.
148 */
149 property alias isMask : icon.isMask
151 /**
152 * @brief An alias to the QQC2 Image control for displaying the image.
153 * @note Review the control own properties on Qt documentation.
154 * @property Image IconItem::image
155 */
156 readonly property alias image : img
157
158 /**
159 * @brief An alias to the MauiKit Icon control for displaying the icon.
160 * @see Icon
161 * @property Icon IconItem::icon
162 */
163 readonly property alias icon: icon
164
165 /**
166 * @brief The desired color for tinting the monochromatic icons.
167 * By default this is set to check the `isMask` property, and then decide base on the `highlighted` property is use the text color or accent color.
168 */
169 property color color : isMask ? (control.highlighted ? Maui.Theme.highlightedTextColor : Maui.Theme.textColor) : "transparent"
170
171 /**
172 * @brief The aligment of the image in the container.
173 * If the `imageSizeHint` has been set to a smaller size than the container, then its alignment will be dtermined by this property. Otherwise the image will fill the container size.
174 * By default this is set to `Qt.AlignHCenter`.
175 * Possible values are:
176 * - Qt.AlignLeft
177 * - Qt.AlignRight
178 * - Qt.AlignHCenter
179 */
180 property int alignment: Qt.AlignHCenter
181
182 Maui.Icon
183 {
184 id: icon
185 visible: img.status === Image.Null || img.status !== Image.Ready || img.status === Image.Error
186
187 smooth: control.smooth
188 anchors.centerIn: parent
189 height: valid ? Math.min(parent.height, control.iconSizeHint) : 0
190 width: height
191 color: control.color
192 isMask: (height <= Maui.Style.iconSizes.medium)
193 }
194
195 Image
196 {
197 id: img
198
199
200// width: Math.min(imageSizeHint >=0 ? imageSizeHint : parent.width, parent.width)
201// height: Math.min(imageSizeHint >= 0 ? imageSizeHint : parent.height, parent.height)
202//
203 height: control.imageSizeHint >=0 ? control.imageSizeHint : parent.height
204 width: control.imageSizeHint >=0 ? control.imageSizeHint : parent.width
205
206 anchors.verticalCenter: parent.verticalCenter
207 x: switch(control.alignment)
208 {
209 case Qt.AlignLeft: return 0
210 case Qt.AlignHCenter: return control.width/2 - width/2
211 case Qt.AlignRight: return control.width - width
212 }
213
214 sourceSize.width: control.imageWidth > 0 ? control.imageWidth : width
215 sourceSize.height: control.imageHeight > 0 ? control.imageHeight : height
216
217 horizontalAlignment: Qt.AlignHCenter
218 verticalAlignment: Qt.AlignVCenter
219
220 cache: !Maui.Handy.isMobile
221 asynchronous: true
222 smooth: control.smooth
223 mipmap: false
224
225 layer.enabled: GraphicsInfo.api !== GraphicsInfo.Software && control.maskRadius
226 layer.effect: MultiEffect
227 {
228 maskEnabled: true
229 maskThresholdMin: 0.5
230 maskSpreadAtMin: 1.0
231 maskSpreadAtMax: 0.0
232 maskThresholdMax: 1.0
233 maskSource: ShaderEffectSource
234 {
235 sourceItem: Item
236 {
237 width: img.width
238 height: img.height
239
240 Rectangle
241 {
242 anchors.centerIn: parent
243 width: Math.min(parent.width, img.paintedWidth)
244 height: Math.min(parent.height, img.paintedHeight)
245 radius: control.maskRadius
246 }
247 }
248 }
249 }
250 }
251}
alias icon
An alias to the MauiKit Icon control for displaying the icon.
Definition IconItem.qml:145
alias isMask
Whether the icon should be masked and tinted with the text color, this is used for monochromatic icon...
Definition IconItem.qml:131
int alignment
The aligment of the image in the container.
Definition IconItem.qml:160
color color
The desired color for tinting the monochromatic icons.
Definition IconItem.qml:150
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Mar 14 2025 11:53:05 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.