7 Module implementing a canvas view class. |
7 Module implementing a canvas view class. |
8 """ |
8 """ |
9 |
9 |
10 import sys |
10 import sys |
11 |
11 |
12 from PyQt4.QtCore import QRectF, QSize, QSizeF, Qt |
12 from PyQt4.QtCore import pyqtSignal, QRectF, QSize, QSizeF, Qt |
13 from PyQt4.QtGui import QGraphicsView, QBrush, QPainter, QPixmap, QFont, QColor |
13 from PyQt4.QtGui import QGraphicsView, QBrush, QPainter, QPixmap, QFont, QColor |
14 |
14 |
15 import Preferences |
15 import Preferences |
16 |
16 |
17 |
17 |
18 class E5GraphicsView(QGraphicsView): |
18 class E5GraphicsView(QGraphicsView): |
19 """ |
19 """ |
20 Class implementing a graphics view. |
20 Class implementing a graphics view. |
|
21 |
|
22 @signal zoomValueChanged(int) emitted to signal a change of the zoom value |
21 """ |
23 """ |
|
24 zoomValueChanged = pyqtSignal(int) |
|
25 |
|
26 ZoomLevels = [ |
|
27 1, 3, 5, 7, 9, |
|
28 10, 20, 30, 50, 67, 80, 90, |
|
29 100, |
|
30 110, 120, 133, 150, 170, 200, 240, 300, 400, |
|
31 500, 600, 700, 800, 900, 1000, |
|
32 ] |
|
33 ZoomLevelDefault = 100 |
|
34 |
22 def __init__(self, scene, parent=None): |
35 def __init__(self, scene, parent=None): |
23 """ |
36 """ |
24 Constructor |
37 Constructor |
25 |
38 |
26 @param scene reference to the scene object (QGraphicsScene) |
39 @param scene reference to the scene object (QGraphicsScene) |
52 "<li>Dragging the mouse over a selected item moves the \n" |
65 "<li>Dragging the mouse over a selected item moves the \n" |
53 "whole selection.</li>\n" |
66 "whole selection.</li>\n" |
54 "</ul>\n" |
67 "</ul>\n" |
55 )) |
68 )) |
56 |
69 |
|
70 def __levelForZoom(self, zoom): |
|
71 """ |
|
72 Private method determining the zoom level index given a zoom factor. |
|
73 |
|
74 @param zoom zoom factor (integer) |
|
75 @return index of zoom factor (integer) |
|
76 """ |
|
77 try: |
|
78 index = E5GraphicsView.ZoomLevels.index(zoom) |
|
79 except ValueError: |
|
80 for index in range(len(E5GraphicsView.ZoomLevels)): |
|
81 if zoom <= E5GraphicsView.ZoomLevels[index]: |
|
82 break |
|
83 return index |
|
84 |
57 def zoomIn(self): |
85 def zoomIn(self): |
58 """ |
86 """ |
59 Public method to zoom in. |
87 Public method to zoom in. |
60 """ |
88 """ |
61 self.scale(1.25, 1.25) |
89 index = self.__levelForZoom(self.zoom()) |
|
90 if index < len(E5GraphicsView.ZoomLevels) - 1: |
|
91 self.setZoom(E5GraphicsView.ZoomLevels[index + 1]) |
62 |
92 |
63 def zoomOut(self): |
93 def zoomOut(self): |
64 """ |
94 """ |
65 Public method to zoom out. |
95 Public method to zoom out. |
66 """ |
96 """ |
67 self.scale(0.8, 0.8) |
97 index = self.__levelForZoom(self.zoom()) |
|
98 if index > 0: |
|
99 self.setZoom(E5GraphicsView.ZoomLevels[index - 1]) |
68 |
100 |
69 def zoomReset(self): |
101 def zoomReset(self): |
70 """ |
102 """ |
71 Public method to handle the reset zoom context menu entry. |
103 Public method to handle the reset the zoom value. |
72 """ |
104 """ |
73 self.resetMatrix() |
105 self.setZoom(E5GraphicsView.ZoomLevels[E5GraphicsView.ZoomLevelDefault]) |
74 |
106 |
75 def setZoom(self, zoomFactor): |
107 def setZoom(self, value): |
76 """ |
108 """ |
77 Public method to set the zoom factor. |
109 Public method to set the zoom value in percent. |
78 |
110 |
79 @param zoomFactor new zoom factor (float) |
111 @param value zoom value in percent (integer) |
80 """ |
112 """ |
81 self.zoomReset() |
113 if value != self.zoom(): |
82 self.scale(zoomFactor, zoomFactor) |
114 self.resetMatrix() |
|
115 factor = value / 100.0 |
|
116 self.scale(factor, factor) |
|
117 self.zoomValueChanged.emit(value) |
83 |
118 |
84 def zoom(self): |
119 def zoom(self): |
85 """ |
120 """ |
86 Public method to get the current zoom factor. |
121 Public method to get the current zoom factor in percent. |
87 |
122 |
88 @return current zoom factor (float) |
123 @return current zoom factor in percent (integer) |
89 """ |
124 """ |
90 return self.matrix().m11() |
125 return int(self.matrix().m11() * 100.0) |
91 |
126 |
92 def resizeScene(self, amount, isWidth=True): |
127 def resizeScene(self, amount, isWidth=True): |
93 """ |
128 """ |
94 Public method to resize the scene. |
129 Public method to resize the scene. |
95 |
130 |
221 else: |
256 else: |
222 from PyQt4.QtSvg import QSvgGenerator |
257 from PyQt4.QtSvg import QSvgGenerator |
223 paintDevice = QSvgGenerator() |
258 paintDevice = QSvgGenerator() |
224 paintDevice.setResolution(100) # 100 dpi |
259 paintDevice.setResolution(100) # 100 dpi |
225 paintDevice.setSize(QSize(int(rect.width()), int(rect.height()))) |
260 paintDevice.setSize(QSize(int(rect.width()), int(rect.height()))) |
|
261 paintDevice.setViewBox(rect) |
226 paintDevice.setFileName(filename) |
262 paintDevice.setFileName(filename) |
227 painter = QPainter(paintDevice) |
263 painter = QPainter(paintDevice) |
228 painter.setRenderHint(QPainter.Antialiasing, True) |
264 painter.setRenderHint(QPainter.Antialiasing, True) |
229 self.scene().render(painter, QRectF(), rect) |
265 self.scene().render(painter, QRectF(), rect) |
230 |
266 |