-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecoratedsimplelabel.cpp
More file actions
137 lines (111 loc) · 2.74 KB
/
Copy pathdecoratedsimplelabel.cpp
File metadata and controls
137 lines (111 loc) · 2.74 KB
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
#include "decoratedsimplelabel.h"
#include <QDebug>
#include <QPainter>
#include <QPropertyAnimation>
DecoratedSimpleLabel::DecoratedSimpleLabel(QWidget *parent)
: QFrame(parent)
, m_text("")
, m_showAlways(false)
{
m_borderPen = QPen(palette().text().color());
m_fillBrush = QBrush(palette().window().color());
m_opacity = 1.0;
m_opaqueAnimation = new QPropertyAnimation(this, "opacity");
m_opaqueAnimation->setDuration(2000);
// m_opaqueAnimation->setStartValue(1.0);
// m_opaqueAnimation->setEndValue(0.0);
m_opaqueAnimation->setKeyValueAt(0, 1.0);
m_opaqueAnimation->setKeyValueAt(0.5, 0.8);
m_opaqueAnimation->setKeyValueAt(1, 0.);
setText(tr(""));
}
DecoratedSimpleLabel::~DecoratedSimpleLabel()
{
delete m_opaqueAnimation;
}
//virtual
QSize DecoratedSimpleLabel::sizeHint() const
{
return m_textPath.boundingRect().size().toSize();
}
//virtual
QSize DecoratedSimpleLabel::minimumSizeHint() const
{
return QSize(0,0);
}
QString DecoratedSimpleLabel::text(void) const
{
return m_text;
}
QPen DecoratedSimpleLabel::borderPen(void) const
{
return m_borderPen;
}
QBrush DecoratedSimpleLabel::fillBrush(void) const
{
return m_fillBrush;
}
qreal DecoratedSimpleLabel::opacity(void) const
{
return m_opacity;
}
void DecoratedSimpleLabel::showAlways(bool enabled)
{
m_showAlways = enabled;
}
void DecoratedSimpleLabel::setText(const QString& text)
{
static int ll = 0;
bool changed = (m_text!=text);
if(changed) {
qDebug() << "setText[" << ll++ << "] : text = " << text;
m_text = text;
updatePath();
}
if(m_showAlways || changed) {
qDebug() << "setText[" << ll++ << "] : restart animation...";
m_opaqueAnimation->stop();
m_opaqueAnimation->start();
}
}
void DecoratedSimpleLabel::setBorderPen(const QPen& pen)
{
m_borderPen = pen;
update();
}
void DecoratedSimpleLabel::setFillBrush(const QBrush& brush)
{
m_fillBrush = brush;
update();
}
void DecoratedSimpleLabel::setOpacity(qreal opacity)
{
m_opacity = opacity;
update();
}
//virtual
void DecoratedSimpleLabel::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
painter.setOpacity(m_opacity);
drawFrame(&painter);
painter.setRenderHints(painter.renderHints() | QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.setPen(m_borderPen);
painter.setBrush(m_fillBrush);
painter.drawPath(m_textPath);
}
//virtual
void DecoratedSimpleLabel::mousePressEvent(QMouseEvent* e)
{
__super::mousePressEvent(e);
}
void DecoratedSimpleLabel::updatePath(void)
{
QPainterPath path;
path.addText(0, 0, font(), m_text);
QRectF box = path.boundingRect();
path.translate(-box.topLeft());
m_textPath = path;
updateGeometry();
update();
}