-
Notifications
You must be signed in to change notification settings - Fork 26
/
dot.cpp
84 lines (64 loc) · 1.67 KB
/
dot.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
#include <QtWidgets>
#include <QtGui>
#include <QtCore>
#include "imageviewerplateselector.h"
#include "utils.h"
#include "dot.h"
constexpr int DOT_WIDTH = 14;
constexpr int DOT_HEIGHT = 14;
Dot::Dot(QWidget *parent)
: QWidget(parent)
, inMove(false)
{
setMouseTracking(true);
setFixedSize(DOT_WIDTH, DOT_HEIGHT);
show();
}
void Dot::mousePressEvent(QMouseEvent *e)
{
e->accept();
m_lastMousePoint = e->pos();
inMove = true;
}
void Dot::mouseMoveEvent(QMouseEvent *e)
{
e->accept();
if(!inMove)
return;
dragTo(e->pos());
}
void Dot::mouseReleaseEvent(QMouseEvent *e)
{
e->accept();
dragTo(e->pos());
emit moved();
inMove = false;
}
void Dot::dragTo(const QPoint &pos)
{
ImageViewerPlateSelector *plateSelectorParent = qobject_cast<ImageViewerPlateSelector *>(parent());
if(!plateSelectorParent)
{
qWarning("Parent must be of class ImageViewerPlateSelector");
return;
}
const QPoint &targetPos = this->pos() + (pos - m_lastMousePoint);
if(plateSelectorParent->acceptableDotPosition(QRect(targetPos, size())))
{
move(targetPos);
emit moving();
}
}
void Dot::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
painter.setClipRegion(e->region());
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Utils::polygonPaintingPen());
painter.setBrush(painter.pen().brush());
// squeeze the ellipse by the pen width
painter.drawEllipse(painter.pen().width(),
painter.pen().width(),
width() - painter.pen().width()*2,
height() - painter.pen().width()*2);
}