MauiKit Controls

Badge.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.Controls.Control
28 * @since org.mauikit.controls 1.0
29 * @brief A Badge item to display text - as a counter - or an icon as a notification hint.
30 *
31 * <a href="https://doc.qt.io/qt-6/qml-qtquick-controls-control.html">This controls inherits from QQC2 Control, to checkout its inherited properties refer to the Qt Docs.</a>
32 *
33 * @image html Badge/badges.png "Badges with different sizes and colors"
34 *
35 * @code
36 * Button
37 * {
38 * text: "Example1"
39 *
40 * Maui.Badge
41 * {
42 * icon.name: "actor"
43 * color: Maui.Theme.neutralBackgroundColor
44 * anchors.horizontalCenter: parent.right
45 * anchors.verticalCenter: parent.top
46 * }
47 * }
48 * @endcode
49 *
50 * <a href="https://invent.kde.org/maui/mauikit/-/blob/qt6-2/examples/Badge.qml">You can find a more complete example at this link.</a>
51 *
52 */
53AbstractButton
54{
55 id: control
56
57 Maui.Theme.inherit: false
58 Maui.Theme.colorSet: Maui.Theme.Complementary
60 /**
61 * @brief Size of the badge. Used as width and height, unless the `implicitWidth` is wider.
62 * It is also used as the icon size doimentions.
63 * By default is set to `Style.iconSizes.small`
64 */
65 property int size: Maui.Style.iconSizes.small
66
67 /**
68 * @brief The color for the background of the badge
69 */
70 property color color: setBackgroundColor(control)
71
72 /**
73 * Display or not a drop shadow.
74 */
75 property bool flat : false
76 icon.color: setTextColor(control)
77
78 font.weight: Font.Light
79 font.bold: true
80 font.pointSize: Maui.Style.fontSizes.tiny
81
82 implicitWidth: Math.max(implicitHeight, _layout.implicitWidth + leftPadding + rightPadding)
83 implicitHeight: _layout.implicitHeight + topPadding + bottomPadding
84
85 padding: Maui.Style.space.small
86
87 background: Rectangle
88 {
89 id: _bg
90 visible: !_bgEffect.visible
91 radius: Math.min(width, height)
92 color: control.color
93 border.color: Qt.lighter(control.color)
94
95 Behavior on color
96 {
97 Maui.ColorTransition{}
98 }
99 }
100
101 MultiEffect
102 {
103 id: _bgEffect
104 visible: GraphicsInfo.api !== GraphicsInfo.Software && !control.flat
105 source: _bg
106 anchors.fill: _bg
107 shadowColor: "#80000000"
108 shadowEnabled: true
109 autoPaddingEnabled: true
110 // shadowVerticalOffset: 3
111 // shadowHorizontalOffset: -2
112 shadowBlur: 0.5
113 }
114
115 contentItem: Maui.IconLabel
116 {
117 id: _layout
118 z: 2
119 text: control.text
120 font: control.font
121 icon: control.icon
122 color: control.icon.color
123 spacing: control.spacing
124 alignment: Qt.AlignHCenter
125 }
126
127 function setTextColor(control)
128 {
129 if(control.Maui.Controls.status)
130 {
131 switch(control.Maui.Controls.status)
132 {
133 case Maui.Controls.Normal: return control.Maui.Theme.textColor
134 case Maui.Controls.Positive: return control.Maui.Theme.positiveTextColor
135 case Maui.Controls.Negative: return control.Maui.Theme.negativeTextColor
136 case Maui.Controls.Neutral: return control.Maui.Theme.neutralTextColor
137 }
138 }
139
140 return control.Maui.Theme.textColor
141 }
142
143 function setBackgroundColor(control)
144 {
145 if(control.Maui.Controls.status)
146 {
147 switch(control.Maui.Controls.status)
148 {
149 case Maui.Controls.Normal: return control.Maui.Theme.backgroundColor
150 case Maui.Controls.Positive: return control.Maui.Theme.positiveBackgroundColor
151 case Maui.Controls.Negative: return control.Maui.Theme.negativeBackgroundColor
152 case Maui.Controls.Neutral: return control.Maui.Theme.neutralBackgroundColor
153 }
154 }
155
156 return control.Maui.Theme.backgroundColor
157 }
158}
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 11:57:11 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.