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 Qt5Compat.GraphicalEffects
23
24import org.mauikit.controls 1.3 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(iconSizeHint, imageSizeHint) : 0
74 implicitWidth: implicitHeight
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`, which means the image will fill the container size..
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.floor(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 width: Math.min(imageSizeHint >=0 ? imageSizeHint : parent.width, parent.width)
200 height: Math.min(imageSizeHint >= 0 ? imageSizeHint : parent.height, parent.height)
201
202 anchors.verticalCenter: parent.verticalCenter
203 x: switch(control.alignment)
204 {
205 case Qt.AlignLeft: return 0
206 case Qt.AlignHCenter: return control.width/2 - width/2
207 case Qt.AlignRight: return control.width - width
208 }
209
210 sourceSize.width: (control.imageWidth > -1 ? control.imageWidth : width)
211 sourceSize.height: (control.imageHeight > -1 ? control.imageHeight : height)
212
213 horizontalAlignment: Qt.AlignHCenter
214 verticalAlignment: Qt.AlignVCenter
215
216 cache: !Maui.Handy.isMobile
217 asynchronous: true
218 smooth: control.smooth
219 mipmap: false
220
221 layer.enabled: control.maskRadius
222 layer.effect: OpacityMask
223 {
224 maskSource: Item
225 {
226 width: img.width
227 height: img.height
228
229 Rectangle
230 {
231 anchors.centerIn: parent
232 width: Math.min(parent.width, img.paintedWidth)
233 height: Math.min(parent.height, img.paintedHeight)
234 radius: control.maskRadius
235 }
236 }
237 }
238 }
239}
The Handy class.
Definition handy.h:41
AlignHCenter
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:56:16 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.