summary refs log tree commit diff
path: root/src/ui/Badge.cc
blob: 6701f9b7b619f78e6cae1cf0fe1e127b4c3faaa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <QPainter>

#include "Badge.h"

Badge::Badge(QWidget *parent)
  : OverlayWidget(parent)
{
        init();
}

Badge::Badge(const QIcon &icon, QWidget *parent)
  : OverlayWidget(parent)
{
        init();
        setIcon(icon);
}

Badge::Badge(const QString &text, QWidget *parent)
  : OverlayWidget(parent)
{
        init();
        setText(text);
}

void
Badge::init()
{
        x_ = 0;
        y_ = 0;
        // TODO: Make padding configurable.
        padding_  = 5;
        diameter_ = 24;

        setAttribute(Qt::WA_TransparentForMouseEvents);

        QFont _font(font());
        _font.setPointSizeF(7.5);
        _font.setStyleName("Bold");

        setFont(_font);
        setText("");
}

QString
Badge::text() const
{
        return text_;
}

QIcon
Badge::icon() const
{
        return icon_;
}

QSize
Badge::sizeHint() const
{
        const int d = diameter();
        return QSize(d + 4, d + 4);
}

qreal
Badge::relativeYPosition() const
{
        return y_;
}

qreal
Badge::relativeXPosition() const
{
        return x_;
}

QPointF
Badge::relativePosition() const
{
        return QPointF(x_, y_);
}

QColor
Badge::backgroundColor() const
{
        if (!background_color_.isValid())
                return QColor("black");

        return background_color_;
}

QColor
Badge::textColor() const
{
        if (!text_color_.isValid())
                return QColor("white");

        return text_color_;
}

void
Badge::setTextColor(const QColor &color)
{
        text_color_ = color;
}

void
Badge::setBackgroundColor(const QColor &color)
{
        background_color_ = color;
}

void
Badge::setRelativePosition(const QPointF &pos)
{
        setRelativePosition(pos.x(), pos.y());
}

void
Badge::setRelativePosition(qreal x, qreal y)
{
        x_ = x;
        y_ = y;
        update();
}

void
Badge::setRelativeXPosition(qreal x)
{
        x_ = x;
        update();
}

void
Badge::setRelativeYPosition(qreal y)
{
        y_ = y;
        update();
}

void
Badge::setIcon(const QIcon &icon)
{
        icon_ = icon;
        update();
}

void
Badge::setText(const QString &text)
{
        text_ = text;

        if (!icon_.isNull())
                icon_ = QIcon();

        size_ = fontMetrics().size(Qt::TextShowMnemonic, text);

        update();
}

void
Badge::setDiameter(int diameter)
{
        if (diameter > 0) {
                diameter_ = diameter;
                update();
        }
}

void
Badge::paintEvent(QPaintEvent *)
{
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.translate(x_, y_);

        QBrush brush;
        brush.setStyle(Qt::SolidPattern);

        painter.setBrush(brush);
        painter.setPen(Qt::NoPen);

        const int d = diameter();

        QRectF r(0, 0, d, d);
        r.translate(QPointF((width() - d), (height() - d)) / 2);

        if (icon_.isNull()) {
                QPen pen;
                // TODO: Make badge width configurable.
                pen.setWidth(1);
                pen.setColor(textColor());

                painter.setPen(pen);
                painter.drawEllipse(r);

                painter.setPen(textColor());
                painter.setBrush(Qt::NoBrush);
                painter.drawText(r.translated(0, -0.5), Qt::AlignCenter, text_);
        } else {
                painter.drawEllipse(r);
                QRectF q(0, 0, 16, 16);
                q.moveCenter(r.center());
                QPixmap pixmap = icon().pixmap(16, 16);
                QPainter icon(&pixmap);
                icon.setCompositionMode(QPainter::CompositionMode_SourceIn);
                icon.fillRect(pixmap.rect(), textColor());
                painter.drawPixmap(q.toRect(), pixmap);
        }
}

int
Badge::diameter() const
{
        if (icon_.isNull()) {
                return qMax(size_.width(), size_.height()) + padding_;
        }

        return diameter_;
}