-
Notifications
You must be signed in to change notification settings - Fork 4
/
drawablearea.cpp
118 lines (105 loc) · 2.87 KB
/
drawablearea.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <QPainter>
#include <QRect>
#include <QEvent>
#include <QPaintEvent>
#include <QResizeEvent>
#include <QMouseEvent>
#include "drawablearea.h"
DrawableArea::DrawableArea(QWidget* parent)
: QWidget(parent),
image(QImage(size(),QImage::Format_RGB32)),
backgroundColor(qRgb(255, 255, 255)),
userPenColor(QColor("green")),
trackerPenColor1(QColor("red")),
trackerPenColor2(QColor("blue"))
{
image.fill(backgroundColor);
setAttribute(Qt::WA_MouseTracking);
setAttribute(Qt::WA_StaticContents);
connect(&timer,SIGNAL(timeout()),this,SLOT(refreshTrack()));
timer.setInterval(50);
timer.start();
}
void DrawableArea::refreshTrack()
{
//if (buttonPushed)
{
QPoint pos = QCursor::pos();
{
if (lastTrackPred.isNull()) // first tracked mouse click
{
lastTrackPred = tracker.initializeStartState(pos); // get estimated value (track)
}
std::pair<QPoint,QPoint> prediction = tracker.getTrackPosition(pos);
QPainter painter(&image);
painter.setPen(trackerPenColor1);
painter.drawLine(lastTrackPred,prediction.first);
if (!lastTrackCorr.isNull())
{
painter.setPen(trackerPenColor2);
painter.drawLine(lastTrackCorr,prediction.second);
}
lastTrackPred = prediction.first;
lastTrackCorr = prediction.second;
}
if (lastPoint.isNull())
lastPoint = pos;
QPainter painter(&image);
painter.setPen(userPenColor);
painter.drawLine(lastPoint,pos);
painter.end();
lastPoint = pos;
update();
}
}
void DrawableArea::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
const QRect rect = event->rect();
painter.drawImage(rect.topLeft(), image, rect);
}
void DrawableArea::resizeEvent(QResizeEvent* event)
{
if (width() > image.width() || height() > image.height())
{
int newWidth = qMax(width(), image.width());
int newHeight = qMax(height(), image.height());
resizeImage(QSize(newWidth, newHeight));
update();
}
QWidget::resizeEvent(event);
}
bool DrawableArea::event(QEvent* event)
{
switch (event->type())
{
case QEvent::MouseButtonPress:
{
lastPoint = static_cast<QMouseEvent*>(event)->pos();
QMouseEvent* ev = static_cast<QMouseEvent*>(event);
if (ev->buttons() & Qt::RightButton) // if RMB clicked, clear image
{
lastPoint = QPoint();
lastTrackPred = QPoint();
lastTrackCorr = QPoint();
image = QImage(image.size(),image.format());
image.fill(backgroundColor);
update();
}
break;
}
default:
return QWidget::event(event);
}
return true;
}
void DrawableArea::resizeImage(const QSize& newSize)
{
if (image.size() == newSize)
return;
QImage newImage(newSize, QImage::Format_RGB32);
newImage.fill(backgroundColor);
QPainter painter(&newImage);
painter.drawImage(QPoint(0, 0), image);
image = newImage;
}