-
Notifications
You must be signed in to change notification settings - Fork 1
/
zoom_graphics_view.cpp
117 lines (103 loc) · 2.94 KB
/
zoom_graphics_view.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
#include "zoom_graphics_view.h"
ZoomGraphicsView::ZoomGraphicsView(QWidget *parent)
{
this->QGraphicsView::setParent(parent);
this->setDragMode(ScrollHandDrag);
this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
this->setCursor(Qt::ArrowCursor);
viewport()->setCursor(Qt::ArrowCursor);
scale = 1.0f;
ctr_pressed = false;
minus_pressed = false;
plus_pressed = false;
scale_100 = false;
}
float ZoomGraphicsView::getScale()
{
return scale;
}
void ZoomGraphicsView::mousePressEvent(QMouseEvent *event)
{
QGraphicsView::mousePressEvent(event);
viewport()->setCursor(Qt::ClosedHandCursor);
}
void ZoomGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
QGraphicsView::mouseReleaseEvent(event);
viewport()->setCursor(Qt::ArrowCursor);
}
void ZoomGraphicsView::setScale(float val)
{
scale = val;
QMatrix matrix;
matrix.reset();
matrix.scale(static_cast<qreal>(scale), static_cast<qreal>(scale));
this->setMatrix(matrix, false);
// MainWindow *main_window = MainWindow::get_instance();
// main_window->updateStatusBar();
}
void ZoomGraphicsView::wheelEvent(QWheelEvent *event)
{
if ( event->modifiers() & Qt::ControlModifier ) {
event->accept();
if ( event->delta() > 0 ) {
if ( scale < 16.f ) {
scale += 0.1f;
}
} else {
if ( scale > 0.1f ) {
scale -= 0.1f;
}
}
//correction due to floating-point precission
if ( scale < 0.1f ) {
scale = 0.1f;
} else if ( scale > 16.f ) {
scale = 16.f;
}
setScale(scale);
}
}
void ZoomGraphicsView::keyPressEvent(QKeyEvent *event)
{
minus_pressed = false;
plus_pressed = false;
scale_100 = false;
if ( event->key() == Qt::Key_Control ) {
ctr_pressed = true;
} else if ( event->key() == Qt::Key_Minus ) {
minus_pressed = true;
} else if ( (event->key() == Qt::Key_Plus) || (event->key() == Qt::Key_Equal) ) {
plus_pressed = true;
}else if ( event->key() == Qt::Key_0 ) {
scale_100 = true;
}
if ( (ctr_pressed&minus_pressed) == true ) {
if ( scale > 0.1f ) {
scale -= 0.1f;
}
if ( scale < 0.1f ) {
scale = 0.1f;
}
} else if ( (ctr_pressed&plus_pressed) == true ) {
if ( scale < 16.f ) {
scale += 0.1f;
}
if ( scale > 16.f ) {
scale = 16.f;
}
} else if ( (ctr_pressed&scale_100) == true ) {
scale = 1.f;
}
setScale(scale);
}
void ZoomGraphicsView::keyReleaseEvent(QKeyEvent *event)
{
if ( event->key() == Qt::Key_Control ) {
ctr_pressed = false;
}else if ( event->key() == Qt::Key_Minus ) {
minus_pressed = false;
}else if ( (event->key() == Qt::Key_Plus) || (event->key() == Qt::Key_Equal) ) {
plus_pressed = false;
}
}