-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPanAndZoomWidget.cpp
More file actions
143 lines (118 loc) · 2.95 KB
/
PanAndZoomWidget.cpp
File metadata and controls
143 lines (118 loc) · 2.95 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
138
139
140
141
142
143
#include "PanAndZoomWidget.h"
#include <QSizePolicy>
#include <QPainter>
#include <QMouseEvent>
#include <QScrollEvent>
PanAndZoomWidget::PanAndZoomWidget(QWidget *parent)
: QWidget{parent}
, translation_(0, 0)
, scale_(1)
, rotation_(0)
, dragBegin_(0, 0)
, dragging_(false)
{
}
void PanAndZoomWidget::Zoom(int steps)
{
SetScale(scale_ * (1.0 + (0.01 * steps)));
}
void PanAndZoomWidget::ZoomIn()
{
Zoom(+1);
}
void PanAndZoomWidget::ZoomOut()
{
Zoom(-1);
}
void PanAndZoomWidget::SetScale(qreal scale)
{
scale_ = scale;
emit(onScaleUpdated(scale_));
update();
}
void PanAndZoomWidget::ResetScale()
{
SetScale(1);
}
void PanAndZoomWidget::Translate(const QPointF& translation)
{
SetTranslation(translation_ + translation);
}
void PanAndZoomWidget::SetTranslationX(qreal newX)
{
SetTranslation(QPointF(newX, translation_.y()));
}
void PanAndZoomWidget::SetTranslationY(qreal newY)
{
SetTranslation(QPointF(translation_.x(), newY));
}
void PanAndZoomWidget::SetTranslation(const QPointF& translation)
{
translation_ = translation;
emit(onTranslationUpdated(translation_));
update();
}
void PanAndZoomWidget::ResetTranslation()
{
Translate(-translation_);
}
void PanAndZoomWidget::Rotate(qreal radians)
{
SetRotation(rotation_ + radians);
}
void PanAndZoomWidget::SetRotation(qreal rotation)
{
rotation_ = rotation;
rotation_ = std::fmod(rotation_, 360);
emit(onRotationUpdated(rotation_));
update();
}
void PanAndZoomWidget::ResetRotation()
{
Rotate(-rotation_);
}
void PanAndZoomWidget::ResetView()
{
ResetRotation();
ResetTranslation();
ResetScale();
}
void PanAndZoomWidget::wheelEvent(QWheelEvent* event)
{
Zoom(event->angleDelta().y() / 10.0);
}
void PanAndZoomWidget::mouseReleaseEvent(QMouseEvent* /*event*/)
{
dragging_ = false;
}
void PanAndZoomWidget::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::MouseButton::LeftButton) {
dragging_ = true;
dragBegin_ = event->globalPosition();
}
}
void PanAndZoomWidget::mouseMoveEvent(QMouseEvent* event)
{
QPointF newPosition = event->globalPosition();
qreal deltaX = (newPosition.x() - dragBegin_.x()) / scale_;
qreal deltaY = (newPosition.y() - dragBegin_.y()) / scale_;
// rotation in radians for std functions
qreal rotation = rotation_ * (std::numbers::pi / 180.0);
// account for rotation
qreal rotatedDeltaX = (deltaX * std::cos(rotation)) + (deltaY * std::sin(rotation));
qreal rotatedDeltaY = -(deltaX * std::sin(rotation)) + (deltaY * std::cos(rotation));
Translate(QPointF(rotatedDeltaX, rotatedDeltaY));
dragBegin_ = newPosition;
}
void PanAndZoomWidget::resizeEvent(QResizeEvent* /*event*/)
{
update();
}
void PanAndZoomWidget::TransformPainter(QPainter& painter) const
{
painter.translate(width() / 2, height() / 2);
painter.scale(scale_, scale_);
painter.rotate(rotation_);
painter.translate(translation_);
}