-
Notifications
You must be signed in to change notification settings - Fork 2
/
qscrolltext.cpp
101 lines (85 loc) · 1.97 KB
/
qscrolltext.cpp
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
#include "qscrolltext.h"
#include <QPainter>
#include <QDebug>
QScrollText::QScrollText(QWidget *parent) :
QWidget(parent), scrollPos(0)
{
staticText.setTextFormat(Qt::PlainText);
setFixedHeight(fontMetrics().height());
leftMargin = width();
setSeparator(" ");
connect(&timer, SIGNAL(timeout()), this, SLOT(timer_timeout()));
timer.setInterval(50);
}
QString QScrollText::text() const
{
return _text;
}
void QScrollText::setText(QString text)
{
_text = text;
updateText();
update();
}
QString QScrollText::separator() const
{
return _separator;
}
void QScrollText::setSeparator(QString separator)
{
_separator = separator;
updateText();
update();
}
void QScrollText::mousePressEvent(QMouseEvent *event)
{
if(wholeTextSize.width() < width())
return;
scrollPos = 0;
if(timer.isActive())
{
timer.stop();
}
else
{
timer.start();
}
update();
}
void QScrollText::updateText()
{
timer.stop();
singleTextWidth = fontMetrics().width(_text);
//timer.start();
scrollPos = 0;
staticText.setText(_text + _separator);
staticText.prepare(QTransform(), font());
wholeTextSize = QSize(fontMetrics().width(staticText.text()), fontMetrics().height());
}
void QScrollText::paintEvent(QPaintEvent*)
{
QPainter painter(this);
buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
buffer.fill(qRgba(0, 0 ,0, 0));
QPainter pb(&buffer);
pb.setPen(painter.pen());
pb.setFont(painter.font());
int x = qMin(-scrollPos, 0);
if(x < width())
{
pb.drawStaticText(QPointF(x, (height() - wholeTextSize.height()) / 2) + QPoint(0,0), staticText);
x += wholeTextSize.width();
if(x < 0)
{
scrollPos = 0;
timer.stop();
update();
}
painter.drawImage(0, 0, buffer);
}
}
void QScrollText::timer_timeout()
{
scrollPos = (scrollPos+ 2);
update();
}