Graphics/SvgDiagram.py

changeset 2334
fc69ad77e18a
parent 2302
f29e9405c851
child 2525
8b507a9a2d40
child 2992
dbdf27746da5
equal deleted inserted replaced
2331:9e623311630d 2334:fc69ad77e18a
7 Module implementing a dialog showing a SVG graphic. 7 Module implementing a dialog showing a SVG graphic.
8 """ 8 """
9 9
10 from PyQt4.QtCore import Qt, QSize, QEvent 10 from PyQt4.QtCore import Qt, QSize, QEvent
11 from PyQt4.QtGui import QPalette, QSizePolicy, QScrollArea, QAction, QMenu, QToolBar, \ 11 from PyQt4.QtGui import QPalette, QSizePolicy, QScrollArea, QAction, QMenu, QToolBar, \
12 QDialog, QPrinter, QPrintDialog, QPainter, QFont, QColor 12 QPrinter, QPrintDialog, QPainter, QFont, QColor
13 from PyQt4.QtSvg import QSvgWidget 13 from PyQt4.QtSvg import QSvgWidget
14 14
15 from E5Gui.E5MainWindow import E5MainWindow 15 from E5Gui.E5MainWindow import E5MainWindow
16 16 from E5Gui.E5ZoomWidget import E5ZoomWidget
17 from .ZoomDialog import ZoomDialog
18 17
19 import UI.Config 18 import UI.Config
20 19
21 import Preferences 20 import Preferences
22 21
23 22
24 class SvgDiagram(E5MainWindow): 23 class SvgDiagram(E5MainWindow):
25 """ 24 """
26 Class implementing a dialog showing a SVG graphic. 25 Class implementing a dialog showing a SVG graphic.
27 """ 26 """
27 ZoomLevels = [
28 1, 3, 5, 7, 9,
29 10, 20, 30, 50, 67, 80, 90,
30 100,
31 110, 120, 133, 150, 170, 200, 240, 300, 400,
32 500, 600, 700, 800, 900, 1000,
33 ]
34 ZoomLevelDefault = 100
35
28 def __init__(self, svgFile, parent=None, name=None): 36 def __init__(self, svgFile, parent=None, name=None):
29 """ 37 """
30 Constructor 38 Constructor
31 39
32 @param svgFile filename of a SVG graphics file to show (string) 40 @param svgFile filename of a SVG graphics file to show (string)
50 self.svgView.setBackgroundRole(QPalette.Dark) 58 self.svgView.setBackgroundRole(QPalette.Dark)
51 self.svgView.setWidget(self.svgWidget) 59 self.svgView.setWidget(self.svgWidget)
52 60
53 self.setCentralWidget(self.svgView) 61 self.setCentralWidget(self.svgView)
54 62
63 self.__zoomWidget = E5ZoomWidget(UI.PixmapCache.getPixmap("zoomOut.png"),
64 UI.PixmapCache.getPixmap("zoomIn.png"),
65 UI.PixmapCache.getPixmap("zoomReset.png"), self)
66 self.statusBar().addPermanentWidget(self.__zoomWidget)
67 self.__zoomWidget.setMapping(
68 SvgDiagram.ZoomLevels, SvgDiagram.ZoomLevelDefault)
69 self.__zoomWidget.valueChanged.connect(self.__doZoom)
70
55 # polish up the dialog 71 # polish up the dialog
56 self.resize(QSize(800, 600).expandedTo(self.minimumSizeHint())) 72 self.resize(QSize(800, 600).expandedTo(self.minimumSizeHint()))
57 73
58 self.zoom = 1.0 74 self.zoom = 1.0
59 self.svgFile = svgFile 75 self.svgFile = svgFile
82 98
83 self.printPreviewAct = \ 99 self.printPreviewAct = \
84 QAction(UI.PixmapCache.getIcon("printPreview.png"), 100 QAction(UI.PixmapCache.getIcon("printPreview.png"),
85 self.trUtf8("Print Preview"), self) 101 self.trUtf8("Print Preview"), self)
86 self.printPreviewAct.triggered[()].connect(self.__printPreviewDiagram) 102 self.printPreviewAct.triggered[()].connect(self.__printPreviewDiagram)
87
88 self.zoomInAct = \
89 QAction(UI.PixmapCache.getIcon("zoomIn.png"),
90 self.trUtf8("Zoom in"), self)
91 self.zoomInAct.triggered[()].connect(self.__zoomIn)
92
93 self.zoomOutAct = \
94 QAction(UI.PixmapCache.getIcon("zoomOut.png"),
95 self.trUtf8("Zoom out"), self)
96 self.zoomOutAct.triggered[()].connect(self.__zoomOut)
97
98 self.zoomAct = \
99 QAction(UI.PixmapCache.getIcon("zoomTo.png"),
100 self.trUtf8("Zoom..."), self)
101 self.zoomAct.triggered[()].connect(self.__zoom)
102
103 self.zoomResetAct = \
104 QAction(UI.PixmapCache.getIcon("zoomReset.png"),
105 self.trUtf8("Zoom reset"), self)
106 self.zoomResetAct.triggered[()].connect(self.__zoomReset)
107 103
108 def __initContextMenu(self): 104 def __initContextMenu(self):
109 """ 105 """
110 Private method to initialize the context menu. 106 Private method to initialize the context menu.
111 """ 107 """
112 self.__menu = QMenu(self) 108 self.__menu = QMenu(self)
113 self.__menu.addAction(self.closeAct) 109 self.__menu.addAction(self.closeAct)
114 self.__menu.addSeparator() 110 self.__menu.addSeparator()
115 self.__menu.addAction(self.printPreviewAct) 111 self.__menu.addAction(self.printPreviewAct)
116 self.__menu.addAction(self.printAct) 112 self.__menu.addAction(self.printAct)
117 self.__menu.addSeparator()
118 self.__menu.addAction(self.zoomInAct)
119 self.__menu.addAction(self.zoomOutAct)
120 self.__menu.addAction(self.zoomAct)
121 self.__menu.addAction(self.zoomResetAct)
122 113
123 self.setContextMenuPolicy(Qt.CustomContextMenu) 114 self.setContextMenuPolicy(Qt.CustomContextMenu)
124 self.customContextMenuRequested.connect(self.__showContextMenu) 115 self.customContextMenuRequested.connect(self.__showContextMenu)
125 116
126 def __showContextMenu(self, coord): 117 def __showContextMenu(self, coord):
141 132
142 self.graphicsToolBar = QToolBar(self.trUtf8("Graphics"), self) 133 self.graphicsToolBar = QToolBar(self.trUtf8("Graphics"), self)
143 self.graphicsToolBar.setIconSize(UI.Config.ToolBarIconSize) 134 self.graphicsToolBar.setIconSize(UI.Config.ToolBarIconSize)
144 self.graphicsToolBar.addAction(self.printPreviewAct) 135 self.graphicsToolBar.addAction(self.printPreviewAct)
145 self.graphicsToolBar.addAction(self.printAct) 136 self.graphicsToolBar.addAction(self.printAct)
146 self.graphicsToolBar.addSeparator()
147 self.graphicsToolBar.addAction(self.zoomInAct)
148 self.graphicsToolBar.addAction(self.zoomOutAct)
149 self.graphicsToolBar.addAction(self.zoomAct)
150 self.graphicsToolBar.addAction(self.zoomResetAct)
151 137
152 self.addToolBar(Qt.TopToolBarArea, self.windowToolBar) 138 self.addToolBar(Qt.TopToolBarArea, self.windowToolBar)
153 self.addToolBar(Qt.TopToolBarArea, self.graphicsToolBar) 139 self.addToolBar(Qt.TopToolBarArea, self.graphicsToolBar)
154 140
155 def getDiagramName(self): 141 def getDiagramName(self):
196 @param evt reference to the gesture event (QGestureEvent 182 @param evt reference to the gesture event (QGestureEvent
197 """ 183 """
198 pinch = evt.gesture(Qt.PinchGesture) 184 pinch = evt.gesture(Qt.PinchGesture)
199 if pinch: 185 if pinch:
200 if pinch.state() == Qt.GestureStarted: 186 if pinch.state() == Qt.GestureStarted:
201 pinch.setScaleFactor(self.zoom) 187 pinch.setScaleFactor(self.__zoom() / 100)
202 else: 188 else:
203 self.__doZoom(pinch.scaleFactor() / self.zoom) 189 self.__doZoom(int(pinch.scaleFactor() * 100))
204 evt.accept() 190 evt.accept()
205 191
206 ############################################################################ 192 ############################################################################
207 ## Private menu handling methods below. 193 ## Private menu handling methods below.
208 ############################################################################ 194 ############################################################################
215 @param factor factor to adjust by (float) 201 @param factor factor to adjust by (float)
216 """ 202 """
217 scrollBar.setValue(int(factor * scrollBar.value() 203 scrollBar.setValue(int(factor * scrollBar.value()
218 + ((factor - 1) * scrollBar.pageStep() / 2))) 204 + ((factor - 1) * scrollBar.pageStep() / 2)))
219 205
220 def __doZoom(self, factor): 206 def __levelForZoom(self, zoom):
221 """ 207 """
222 Private method to perform the zooming. 208 Private method determining the zoom level index given a zoom factor.
223 209
224 @param factor zoom factor (float) 210 @param zoom zoom factor (integer)
225 """ 211 @return index of zoom factor (integer)
226 self.zoom *= factor 212 """
227 self.svgWidget.resize(self.zoom * self.svgWidget.sizeHint()) 213 try:
228 214 index = SvgDiagram.ZoomLevels.index(zoom)
229 self.__adjustScrollBar(self.svgView.horizontalScrollBar(), factor) 215 except ValueError:
230 self.__adjustScrollBar(self.svgView.verticalScrollBar(), factor) 216 for index in range(len(SvgDiagram.ZoomLevels)):
217 if zoom <= SvgDiagram.ZoomLevels[index]:
218 break
219 return index
220
221 def __doZoom(self, value):
222 """
223 Public method to set the zoom value in percent.
224
225 @param value zoom value in percent (integer)
226 """
227 oldValue = self.__zoom()
228 if value != oldValue:
229 self.svgWidget.resize(value / 100 * self.svgWidget.sizeHint())
230
231 factor = value / oldValue
232 self.__adjustScrollBar(self.svgView.horizontalScrollBar(), factor)
233 self.__adjustScrollBar(self.svgView.verticalScrollBar(), factor)
234
235 self.__zoomWidget.setValue(value)
231 236
232 def __zoomIn(self): 237 def __zoomIn(self):
233 """ 238 """
234 Private method to handle the zoom in context menu entry. 239 Private method to zoom into the SVG.
235 """ 240 """
236 self.__doZoom(1.25) 241 index = self.__levelForZoom(self.__zoom())
242 if index < len(SvgDiagram.ZoomLevels) - 1:
243 self.__doZoom(SvgDiagram.ZoomLevels[index + 1])
237 244
238 def __zoomOut(self): 245 def __zoomOut(self):
239 """ 246 """
240 Private method to handle the zoom out context menu entry. 247 Private method to zoom out of the SVG.
241 """ 248 """
242 self.__doZoom(0.8) 249 index = self.__levelForZoom(self.__zoom())
250 if index > 0:
251 self.__doZoom(SvgDiagram.ZoomLevels[index - 1])
243 252
244 def __zoomReset(self): 253 def __zoomReset(self):
245 """ 254 """
246 Private method to handle the reset zoom context menu entry. 255 Private method to reset the zoom value.
247 """ 256 """
248 self.zoom = 1.0 257 self.__doZoom(SvgDiagram.ZoomLevels[SvgDiagram.ZoomLevelDefault])
249 self.svgWidget.adjustSize()
250 258
251 def __zoom(self): 259 def __zoom(self):
252 """ 260 """
253 Private method to handle the zoom context menu action. 261 Public method to get the current zoom factor in percent.
254 """ 262
255 dlg = ZoomDialog(self.zoom, self) 263 @return current zoom factor in percent (integer)
256 if dlg.exec_() == QDialog.Accepted: 264 """
257 zoom = dlg.getZoomSize() 265 return int(self.svgWidget.width() / self.svgWidget.sizeHint().width() * 100.0)
258 factor = zoom / self.zoom
259 self.__doZoom(factor)
260 266
261 def __printDiagram(self): 267 def __printDiagram(self):
262 """ 268 """
263 Private slot called to print the diagram. 269 Private slot called to print the diagram.
264 """ 270 """

eric ide

mercurial