-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgraphics_view.py
More file actions
136 lines (106 loc) · 4.71 KB
/
graphics_view.py
File metadata and controls
136 lines (106 loc) · 4.71 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Distributed under the MIT licesnse.
# Copyright (c) 2013 Dave McCoy ([email protected])
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#A huge thanks to 'Rapid GUI Programming with Python and Qt' by Mark Summerfield
'''
Log
6/05/2013: Initial commit
'''
import sys
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class GraphicsView(QGraphicsView):
def __init__(self, parent = None):
super(GraphicsView, self).__init__(None)
self.parent = parent
self.setDragMode(QGraphicsView.RubberBandDrag)
self.setRenderHint(QPainter.Antialiasing)
self.setRenderHint(QPainter.TextAntialiasing)
self.setAcceptDrops(True)
self.icon = QIcon()
self.controller = parent
#self.setAcceptDrops(True)
self.scale_min = .01
self.scale_max = 1000
self.dbg = False
def set_controller(self, controller):
self.controller = controller
def wheelEvent(self, event):
factor = 1.41 ** (-event.delta() / 240.0)
self.scale(factor, factor)
def dragEnterEvent(self, event):
self.controller.drag_enter(event)
def dragLeaveEvent(self, event):
self.controller.drag_leave(event)
def dragMoveEvent(self, event):
if self.dbg: print "GV: dragMoveEvent"
self.controller.drag_move(event)
def mousePressEvent(self, event):
if self.dbg: print "GV: mousePressEvent"
super(GraphicsView, self).mousePressEvent(event)
def mouseReleaseEvent(self, event):
if self.dbg: print "GV: mouseReleaseEvent"
super(GraphicsView, self).mouseReleaseEvent(event)
def dropEvent(self, event):
if self.dbg: print "GV: dropEvent"
self.controller.drop_event(event)
def keyPressEvent(self, event):
if self.dbg: print "GV: Key press event"
task_list = {
Qt.Key_Plus: lambda: self._scale_view(1.2),
Qt.Key_Minus: lambda: self._scale_view(1 / 1.2),
#Qt.Key_Equal: lambda: self._scale_normal()
Qt.Key_Equal: lambda: self._scale_fit()
#TODO: Add more key interfaces here
}
if event.key() in task_list:
task_list[event.key()]()
else:
#Pass the key event to the system
QWidget.keyPressEvent(self, event)
def _scale_view(self, scale_factor):
"""Scale the view"""
if self.dbg: print "Canvas: Scale view by: %f" % scale_factor
#check if the scale factor is alright
factor = self.transform().scale(scale_factor, scale_factor).mapRect(QRectF(0, 0, 1, 1)).width()
if factor > self.scale_min and factor < self.scale_max:
#Scale factor is within limits
self.scale(scale_factor, scale_factor)
elif factor < self.scale_min:
if self.dbg: print "GV: Scale too small: %f" % factor
elif factor > self.scale_max:
if self.dbg: print "GV: Scale too large: %f" % factor
def _scale_normal(self):
scale_factor = 1.0
factor = self.transform().scale(scale_factor, scale_factor).mapRect(QRectF(0, 0, 1, 1)).width()
scale_factor = 1.0 / factor
if self.dbg: print "GV: Set scale back to 1.0"
self.scale(scale_factor, scale_factor)
def _scale_fit(self):
if self.dbg: print "GV: Set scale to fit all items"
self.fitInView(self.scene().sceneRect(), Qt.KeepAspectRatio)
def paint(self, painter, option, widget):
super(GraphicsView, self).paint(painter, option, widget)
def paintEvent(self, event):
super(GraphicsView, self).paintEvent(event)
def resizeEvent(self, event):
super(GraphicsView, self).resizeEvent(event)
self._scale_fit()