-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGluGraphicsView.cpp
75 lines (68 loc) · 1.87 KB
/
GluGraphicsView.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
#include "GluGraphicsView.h"
#include <QApplication>
#include <QDebug>
GluGraphicsView::GluGraphicsView(QWidget *parent) :
QGraphicsView(parent)
{
setDragMode(RubberBandDrag); //可多选
setBackgroundBrush(QBrush(Qt::lightGray));
setRenderHints(QPainter::Antialiasing|
QPainter::TextAntialiasing);
// setBackgroundBrush(QColor(Qt::black));
setBackgroundBrush(QBrush(Qt::white));
}
void GluGraphicsView::zoomIn()
{
QTransform transform;
qreal tmp;
tmp = this->transform().m22()+0.1;
if (tmp > 3.0) //缩放范围0.1-3.0
return;
transform.scale(tmp, tmp);
setTransform(transform);
}
void GluGraphicsView::zoomOut()
{
QTransform transform;
qreal tmp;
tmp = this->transform().m22()-0.1;
if (tmp < 0.1) //缩放范围0.1-3.0
return;
transform.scale(tmp, tmp);
setTransform(transform);
}
void GluGraphicsView::restore()
{
resetTransform(); //重置缩放
}
void GluGraphicsView::wheelEvent(QWheelEvent *event)
{
if (QApplication::keyboardModifiers () == Qt::ControlModifier)
{
// scaleBy(std::pow(4.0 / 3.0, (-event->delta() / 240.0))); //原方法
QTransform transform;
qreal tmp;
tmp = (event->delta()>0 ? this->transform().m22()+0.1 : this->transform().m22()-0.1);
if (tmp > 3.0 || tmp < 0.1) //缩放范围0.1-3.0
return;
transform.scale(tmp, tmp);
setTransform(transform);
}
else {
QGraphicsView::wheelEvent(event);
}
}
void GluGraphicsView::mouseDoubleClickEvent(QMouseEvent *event)
{
if(event->button()==Qt::MidButton)
{
// resetCachedContent () ;
// resetMatrix () ;
resetTransform(); //重置缩放
}
QGraphicsView::mouseDoubleClickEvent(event);
}
void GluGraphicsView::scaleBy(double factor)
{
scale(factor, factor);
}