-
Notifications
You must be signed in to change notification settings - Fork 0
/
SvgView.cpp
64 lines (57 loc) · 1.28 KB
/
SvgView.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
#include "SvgView.h"
#include "SvgScene.h"
#include <QGraphicsView>
#include <QDebug>
#include <QWheelEvent>
#include <QGraphicsScene>
SvgView::SvgView(SvgScene* scene, QWidget* parent)
: QGraphicsView(scene, parent), m_scaleValue(1.0)
{
setDragMode(QGraphicsView::NoDrag);
setMouseTracking(true);
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setResizeAnchor(QGraphicsView::AnchorUnderMouse);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void SvgView::setGraphType(GraphType graphtype)
{
QGraphicsScene* scene = this->scene();
if (scene)
{
SvgScene* svgScene = qobject_cast<SvgScene*>(scene);
if (svgScene)
{
svgScene->setGraphType(graphtype);
}
}
}
void SvgView::zoomIn()
{
scale(1.1, 1.1);
m_scaleValue *= 1.1;
update();
}
void SvgView::zoomOut()
{
scale(0.9, 0.9);
m_scaleValue *= 0.9;
update();
}
void SvgView::wheelEvent(QWheelEvent* event)
{
// 按住Ctrl键,滚动鼠标滚轮,实现放大缩小
if (event->modifiers() == Qt::CTRL)
{
if ((event->delta() > 0) && (m_scaleValue <= 50))
{
zoomIn();
}
else if ((event->delta() < 0) && (m_scaleValue >= 0.01))
zoomOut();
}
else
{
QGraphicsView::wheelEvent(event);
}
}