-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageView.cpp
More file actions
110 lines (88 loc) · 3.06 KB
/
ImageView.cpp
File metadata and controls
110 lines (88 loc) · 3.06 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
#include "ImageView.h"
#include <QScrollBar>
float cross = 12;
float crossSize = 2;
ImageView::ImageView(QWidget *parent) {
//set view background and its border transparent
setStyleSheet("background: transparent");
//window motion without cursor
this->setDragMode(QGraphicsView::NoDrag);
//set view as child window
this->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
this->setCursor(Qt::CrossCursor);
setMouseTracking(true);
//close view scrollbar
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scaleUp = 1.2;
scaleDown = 1 / 1.2;
}
ImageView::~ImageView() {
}
void ImageView::on_moveImg(QPointF cursorTranslation) {
cursorTranslation *= this->transform().m11();
//call scale method
this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
//translation need to scale
this->centerOn(this->mapToScene(QPoint(this->viewport()->rect().width() / 2 - cursorTranslation.x(),
this->viewport()->rect().height() / 2 - cursorTranslation.y())));
this->setTransformationAnchor(QGraphicsView::AnchorViewCenter);
}
//虚拟十字标
void ImageView::on_virtualMouseMove(QPointF point)
{
//调用scene的虚拟十字标
ImageScene* tmpScene = (ImageScene*)this->scene();
tmpScene->moveVirtualPos(point);
}
void ImageView::on_wheelImg(QPointF viewPos, float scaleIndex) {
//scene coordinate
QPointF scenePos = this->mapToScene(viewPos.toPoint());
this->scale(scaleIndex, scaleIndex);
//image in view has scaled
QPointF scaledViewPos = this->matrix().map(scenePos);
}
void ImageView::mouseMoveEvent(QMouseEvent *event) {
if (this->dragMode() == QGraphicsView::ScrollHandDrag) {
QPoint currentCursorPos = event->pos();
QPointF cursorTranslation = mapToScene(currentCursorPos) - mapToScene(lastCursorPos);
emit moveImg(cursorTranslation);
on_moveImg(cursorTranslation);
lastCursorPos = currentCursorPos;
}
lastCursorPos = event->pos();
emit virtualMouseMove(mapToScene(event->pos()));
on_virtualMouseMove(mapToScene(event->pos()));
//display cursor location
QGraphicsView::mouseMoveEvent(event);
}
void ImageView::mousePressEvent(QMouseEvent* event) {
if (event->button() == Qt::MidButton) {
this->setCursor(Qt::OpenHandCursor);
this->setDragMode(QGraphicsView::ScrollHandDrag);
//view coordinate
lastCursorPos = event->pos();
}
QGraphicsView::mousePressEvent(event);
}
void ImageView::mouseReleaseEvent(QMouseEvent *event){
if (event->button() == Qt::MidButton){
this->setDragMode(QGraphicsView::NoDrag);
this->setCursor(Qt::CrossCursor);
}
QGraphicsView::mouseReleaseEvent(event);
}
void ImageView::wheelEvent(QWheelEvent* event) {
float scaleIndex;
QPointF viewPos = event->pos();
event->delta() > 0 ? (scaleIndex = scaleUp) : (scaleIndex = scaleDown);
//change linked cross-cusor as soon as wheeling
cross /= scaleIndex;
crossSize /= scaleIndex;
emit wheelImg(viewPos, scaleIndex);
on_wheelImg(viewPos, scaleIndex);
//缩放后刷新十字标
lastCursorPos = event->pos();
emit virtualMouseMove(mapToScene(lastCursorPos));
on_virtualMouseMove(mapToScene(lastCursorPos));
}