MauiKit Controls

IconLabel.qml
1/*
2 * Copyright 2020 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.Layouts
23
24import org.mauikit.controls 1.3 as Maui
25
26/**
27 * @inherit QtQuick.Item
28 * @brief A icon and a label put together in two possible positions, a horizontal or vertical layout.
29 *
30 * <a href="https://doc.qt.io/qt-6/qml-qtquick-controls-item.html">This control inherits from QQC2 Item, to checkout its inherited properties refer to the Qt Docs.</a>
31 *
32 * This is a base template for other controls, such as for setting the contents of the ToolButton, Button, MenuItem, etc, in the style.
33 *
34 * @note This control is only a visual item, and does not handle any event or gets any focus.
35 *
36 * @image html Misc/iconlabel.png
37 *
38 * @code
39 * ColumnLayout
40 * {
41 * anchors.centerIn: parent
42 * spacing: Maui.Style.space.big
43 *
44 * Maui.IconLabel
45 * {
46 * icon: ({name: "folder", height: "22", width: "22", color: "yellow"})
47 * text: "Testing"
48 * display: ToolButton.TextBesideIcon
49 * alignment: Qt.AlignLeft
50 * color: "yellow"
51 * }
52 *
53 * Maui.IconLabel
54 * {
55 * icon: ({name: "vvave", height: "64", width: "64"})
56 * text: "Vvave"
57 * display: ToolButton.TextUnderIcon
58 * alignment: Qt.AlignHCenter
59 * }
60 * }
61 * @endcode
62 *
63 * @warning To set the `icon` properties, you need to set it as a dictionary map. The supported key values are:
64 * - name
65 * - source
66 * - color
67 * - height
68 * - width
69 * - cache
70 * @see icon
71 *
72 * <a href="https://invent.kde.org/maui/mauikit/-/blob/qt6-2/examples/IconLabel.qml">You can find a more complete example at this link.</a>
73 */
74Item
75{
76 id: control
77
78 focus: false
79
80 implicitWidth: _layoutButton.implicitWidth + leftPadding + rightPadding
81 implicitHeight: _layoutButton.implicitHeight + topPadding + bottomPadding
83 /**
84 * @brief The color for the text.
85 * By default this is set to `Theme.textColor`.
86 * @note to set the icon color, use the `icon.color` property.
87 * @property color IconLabel::color
88 */
89 property alias color : _label.color
90
91 /**
92 * @brief The text to be used.
93 * @property string IconLabel::text
94 */
95 property alias text : _label.text
96
97 /**
98 * @brief This is a dictionary, which represents the properties for the icon.
99 * The supported key values are:
100 * - name
101 * - source
102 * - color
103 * - height
104 * - width
105 * - cache
106 *
107 * @code
108 * Maui.IconLabel
109 * {
110 * icon: ({name: "folder", height: "22", width: "22", color: "yellow"})
111 * }
112 * @endcode
113 *
114 */
115 property var icon
116
117 /**
118 * @brief How to display the icon and the label.
119 * The available options are:
120 * - ToolButton.TextBesidesIcon
121 * - ToolButton.TextUnderIcon
122 * - ToolButton.TextOnly
123 * - ToolButton.IconOnly
124 *
125 * By default this is set to `ToolButton.IconOnly`
126 */
127 property int display : ToolButton.IconOnly
128
129 /**
130 * @brief The preferred horizontal alignment of the text.
131 * By default this is set to `Qt.AlignLeft`.
132 * @note The alignment will depend on the width of the container. If there is a width bigger then the implicit width, then the alignment will be set as preferred.
133 * @property enum IconLabel::alignment
134 */
135 property alias alignment : _label.horizontalAlignment
136
137 // font.pointSize: control.display === ToolButton.TextUnderIcon ? Maui.Style.fontSizes.small : Maui.Style.fontSizes.medium
138
139 /**
140 * @brief The total padding all around the element.
141 * By default this is set to `0`.
142 */
143 property int padding : 0
144
145 /**
146 * @brief Left padding.
147 * By default this is set to `padding`
148 */
149 property int leftPadding: padding
151 /**
152 * @brief Right padding.
153 * By default this is set to `padding`
154 */
155 property int rightPadding: padding
157 /**
158 * @brief Bottom padding.
159 * By default this is set to `padding`
160 */
161 property int bottomPadding: padding
163 /**
164 * @brief Top padding.
165 * By default this is set to `padding`
166 */
167 property int topPadding: padding
169 /**
170 * @brief The spacing value between the icon element and the text label.
171 * By default this is set to `Style.space.small`.
172 * @see Style
173 */
174 property int spacing: Maui.Style.space.small
175
176 /**
177 * @brief The font properties of the text.
178 * By default this is set to `Style.defaultFont`.
179 * @property font IconLabel::font
180 */
181 property alias font : _label.font
182
183 /**
184 * @brief An alias tot he QQC2 Label handling the text.
185 * Exposed for fine tuning the label properties.
186 * @property Label IconLabel::label
187 */
188 property alias label : _label
189
190 GridLayout
191 {
192 id: _layoutButton
193
194 anchors.fill: parent
195
196 anchors.leftMargin: control.leftPadding
197 anchors.rightMargin: control.rightPadding
198 anchors.bottomMargin: control.bottomPadding
199 anchors.topMargin: control.topPadding
200
201 rowSpacing: control.spacing
202 columnSpacing: control.spacing
203
204 columns: control.display === ToolButton.TextUnderIcon ? 1 : 2
205
206 Maui.Icon
207 {
208 id: _icon
209
210 implicitHeight: visible && control.icon ? control.icon.height : 0
211 implicitWidth: visible && control.icon ? control.icon.width : 0
212
213 Layout.alignment: Qt.AlignCenter
214
215 visible: String(_icon.source).length > 0 && (control.display !== ToolButton.TextOnly)
216
217 color: control.icon ? control.icon.color : control.color
218 source: control.icon ? control.icon.name || control.icon.source : ""
219 }
220
221 Label
222 {
223 id: _label
224
225 visible: text.length && (control.display === ToolButton.TextOnly || control.display === ToolButton.TextBesideIcon || control.display === ToolButton.TextUnderIcon || !_icon.visible)
226
227 opacity: visible ? ( enabled ? 1 : 0.5) : 0
228
229 horizontalAlignment: Qt.AlignLeft
230 verticalAlignment: Qt.AlignVCenter
231
232 Layout.fillWidth: true
233
234 color: Maui.Style.defaultFont
235 font: Maui.Style.defaultFont
236
237 elide: Text.ElideRight
238 wrapMode: Text.NoWrap
239
240 Behavior on opacity
241 {
242 NumberAnimation
243 {
244 duration: Maui.Style.units.shortDuration
245 easing.type: Easing.InQuad
246 }
247 }
248 }
249 }
250}
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.