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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# -*- coding: utf-8 -*-
"""
This module contains the class which inherits from QtWidgets.QGraphicsView in order to add some custom functionality
to a graphics view. Particularly, navigation controls for navigating the viewport.
"""
from artv2.third_party.Qt import QtWidgets, QtGui, QtCore
class ARTv2GraphicsView(QtWidgets.QGraphicsView):
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def __init__(self, scene, parent=None):
super(ARTv2GraphicsView, self).__init__(parent)
self.scene = scene
self._init_interface()
self.setScene(self.scene)
self.zoom_in_factor = 1.25
self.current_zoom = 10
self.zoom_step = 1
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _init_interface(self):
"""
Set some flags for the graphics view.
"""
self.setRenderHints(QtGui.QPainter.Antialiasing | QtGui.QPainter.HighQualityAntialiasing |
QtGui.QPainter.TextAntialiasing | QtGui.QPainter.SmoothPixmapTransform)
self.setViewportUpdateMode(QtWidgets.QGraphicsView.FullViewportUpdate)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setTransformationAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def mousePressEvent(self, event):
"""
Override mouse press event for dragging around viewport with the MMB
:param event:
:return:
"""
if event.button() == QtCore.Qt.MiddleButton:
self._middle_mouse_button_press(event)
elif event.button() == QtCore.Qt.LeftButton:
self._left_mouse_button_press(event)
elif event.button() == QtCore.Qt.RightButton:
self._right_mouse_button_press(event)
else:
super(ARTv2GraphicsView, self).mousePressEvent(event)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def mouseReleaseEvent(self, event):
"""
Override mouse press event for dragging around viewport with the MMB
:param event:
:return:
"""
if event.button() == QtCore.Qt.MiddleButton:
self._middle_mouse_button_release(event)
elif event.button() == QtCore.Qt.LeftButton:
self._left_mouse_button_release(event)
elif event.button() == QtCore.Qt.RightButton:
self._right_mouse_button_release(event)
else:
super(ARTv2GraphicsView, self).mouseReleaseEvent(event)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def wheelEvent(self, event):
"""
OVerride mouse scroll event for zooming in and out.
:param event:
:return:
"""
zoom_out_factor = 1 / self.zoom_in_factor
# if you are scrolling in (up) on the MMB
if event.angleDelta().y() > 0:
zoom_factor = self.zoom_in_factor
self.current_zoom += self.zoom_step
else:
zoom_factor = zoom_out_factor
self.current_zoom -= self.zoom_step
self.scale(zoom_factor, zoom_factor)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _middle_mouse_button_press(self, event):
"""
By default, setting drag mode wanted to use the LMB, so this will take in a MMB event, and make the MMB do
the same thing as the LMB
:param event:
:return:
"""
release_event = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonRelease, event.localPos(),
event.screenPos(), QtCore.Qt.LeftButton, QtCore.Qt.NoButton,
event.modifiers())
super(ARTv2GraphicsView, self).mouseReleaseEvent(release_event)
self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
fake_event = QtGui.QMouseEvent(event.type(), event.localPos(), event.screenPos(), QtCore.Qt.LeftButton,
event.buttons() | QtCore.Qt.LeftButton, event.modifiers())
super(ARTv2GraphicsView, self).mousePressEvent(fake_event)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _left_mouse_button_press(self, event):
return super(ARTv2GraphicsView, self).mousePressEvent(event)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _right_mouse_button_press(self, event):
return super(ARTv2GraphicsView, self).mousePressEvent(event)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _middle_mouse_button_release(self, event):
fake_event = QtGui.QMouseEvent(event.type(), event.localPos(), event.screenPos(), QtCore.Qt.LeftButton,
event.buttons() | QtCore.Qt.LeftButton, event.modifiers())
super(ARTv2GraphicsView, self).mouseReleaseEvent(fake_event)
self.setDragMode(QtWidgets.QGraphicsView.NoDrag)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _left_mouse_button_release(self, event):
return super(ARTv2GraphicsView, self).mouseReleaseEvent(event)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def _right_mouse_button_release(self, event):
return super(ARTv2GraphicsView, self).mouseReleaseEvent(event)
|