-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRotationHandle.cpp
More file actions
100 lines (81 loc) · 2.54 KB
/
Copy pathRotationHandle.cpp
File metadata and controls
100 lines (81 loc) · 2.54 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
#include "RotationHandle.h"
#include "QPainter"
#include "QGraphicsSceneMouseEvent"
#include "QCursor"
#include "QtMath"
#include "QGraphicsScene"
#include "qdebug.h"
RotationHandle::RotationHandle(QPoint origin,
QAbstractGraphicsShapeItem* parent)
: QAbstractGraphicsShapeItem(parent)
, _origin(origin)
{
setFlag(ItemIsMovable);
setPen(QPen(Qt::black));
setBrush(QBrush(Qt::red));
// setFlag(QGraphicsItem::ItemIsSelectable);
// setFlag(ItemIgnoresTransformations);
}
RotationHandle::~RotationHandle()
{
}
QRectF RotationHandle::boundingRect() const
{
return QRectF(-5, -5, 10, 10);
}
void RotationHandle::paint(QPainter* painter,
const QStyleOptionGraphicsItem* option,
QWidget* widget)
{
if (m_antialiasing) {
painter->setRenderHint(QPainter::Antialiasing);
}
painter->setPen(this->pen());
painter->setBrush(this->brush());
painter->drawEllipse(QRect(-5, -5, 10, 10));
// painter->drawRect(boundingRect());
}
void RotationHandle::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
// handle left mouse button here
// unselect all selected items so this can be moved
QList<QGraphicsItem*> allSelectedItems = scene()->selectedItems();
QGraphicsItem* item;
foreach (item, allSelectedItems) {
item->setSelected(false);
}
setCursor(Qt::ClosedHandCursor);
update();
}
// pass on
QGraphicsItem::mousePressEvent(event);
}
void RotationHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
// handle left mouse button here
setCursor(Qt::ArrowCursor);
double angleRad = atan2(this->y() - _origin.y(),
this->x() - _origin.x());
double angleDeg = qRadiansToDegrees(angleRad);
Q_EMIT emitShapeRotation(angleDeg, true);
update();
}
// pass on
QGraphicsItem::mouseReleaseEvent(event);
}
void RotationHandle::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
double angleRad = atan2(this->y() - _origin.y(), this->x() - _origin.x());
double angleDeg = qRadiansToDegrees(angleRad);
Q_EMIT emitShapeRotation(angleDeg);
update();
// pass on
QGraphicsItem::mouseMoveEvent(event);
}
void RotationHandle::setAntialiasing(bool toggle)
{
m_antialiasing = toggle;
update();
}