Graphics/PixmapDiagram.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 pixmap. 7 Module implementing a dialog showing a pixmap.
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 QLabel, QPalette, QSizePolicy, QScrollArea, QAction, QMenu, \ 11 from PyQt4.QtGui import QLabel, QPalette, QSizePolicy, QScrollArea, QAction, QMenu, \
12 QToolBar, QImage, QPixmap, QDialog, QPrinter, QPrintDialog, QPainter, QFont, QColor 12 QToolBar, QImage, QPixmap, QPrinter, QPrintDialog, QPainter, QFont, QColor
13 13
14 from E5Gui import E5MessageBox 14 from E5Gui import E5MessageBox
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 PixmapDiagram(E5MainWindow): 23 class PixmapDiagram(E5MainWindow):
25 """ 24 """
26 Class implementing a dialog showing a pixmap. 25 Class implementing a dialog showing a pixmap.
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, pixmap, parent=None, name=None): 36 def __init__(self, pixmap, parent=None, name=None):
29 """ 37 """
30 Constructor 38 Constructor
31 39
32 @param pixmap filename of a graphics file to show (string) 40 @param pixmap filename of a graphics file to show (string)
51 self.pixmapView.setBackgroundRole(QPalette.Dark) 59 self.pixmapView.setBackgroundRole(QPalette.Dark)
52 self.pixmapView.setWidget(self.pixmapLabel) 60 self.pixmapView.setWidget(self.pixmapLabel)
53 61
54 self.setCentralWidget(self.pixmapView) 62 self.setCentralWidget(self.pixmapView)
55 63
64 self.__zoomWidget = E5ZoomWidget(UI.PixmapCache.getPixmap("zoomOut.png"),
65 UI.PixmapCache.getPixmap("zoomIn.png"),
66 UI.PixmapCache.getPixmap("zoomReset.png"), self)
67 self.statusBar().addPermanentWidget(self.__zoomWidget)
68 self.__zoomWidget.setMapping(
69 PixmapDiagram.ZoomLevels, PixmapDiagram.ZoomLevelDefault)
70 self.__zoomWidget.valueChanged.connect(self.__doZoom)
71
56 # polish up the dialog 72 # polish up the dialog
57 self.resize(QSize(800, 600).expandedTo(self.minimumSizeHint())) 73 self.resize(QSize(800, 600).expandedTo(self.minimumSizeHint()))
58 74
59 self.zoom = 1.0
60 self.pixmapfile = pixmap 75 self.pixmapfile = pixmap
61 self.status = self.__showPixmap(self.pixmapfile) 76 self.status = self.__showPixmap(self.pixmapfile)
62 77
63 self.__initActions() 78 self.__initActions()
64 self.__initContextMenu() 79 self.__initContextMenu()
82 97
83 self.printPreviewAct = \ 98 self.printPreviewAct = \
84 QAction(UI.PixmapCache.getIcon("printPreview.png"), 99 QAction(UI.PixmapCache.getIcon("printPreview.png"),
85 self.trUtf8("Print Preview"), self) 100 self.trUtf8("Print Preview"), self)
86 self.printPreviewAct.triggered[()].connect(self.__printPreviewDiagram) 101 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 102
108 def __initContextMenu(self): 103 def __initContextMenu(self):
109 """ 104 """
110 Private method to initialize the context menu. 105 Private method to initialize the context menu.
111 """ 106 """
112 self.__menu = QMenu(self) 107 self.__menu = QMenu(self)
113 self.__menu.addAction(self.closeAct) 108 self.__menu.addAction(self.closeAct)
114 self.__menu.addSeparator() 109 self.__menu.addSeparator()
115 self.__menu.addAction(self.printPreviewAct) 110 self.__menu.addAction(self.printPreviewAct)
116 self.__menu.addAction(self.printAct) 111 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 112
123 self.setContextMenuPolicy(Qt.CustomContextMenu) 113 self.setContextMenuPolicy(Qt.CustomContextMenu)
124 self.customContextMenuRequested.connect(self.__showContextMenu) 114 self.customContextMenuRequested.connect(self.__showContextMenu)
125 115
126 def __showContextMenu(self, coord): 116 def __showContextMenu(self, coord):
141 131
142 self.graphicsToolBar = QToolBar(self.trUtf8("Graphics"), self) 132 self.graphicsToolBar = QToolBar(self.trUtf8("Graphics"), self)
143 self.graphicsToolBar.setIconSize(UI.Config.ToolBarIconSize) 133 self.graphicsToolBar.setIconSize(UI.Config.ToolBarIconSize)
144 self.graphicsToolBar.addAction(self.printPreviewAct) 134 self.graphicsToolBar.addAction(self.printPreviewAct)
145 self.graphicsToolBar.addAction(self.printAct) 135 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 136
152 self.addToolBar(Qt.TopToolBarArea, self.windowToolBar) 137 self.addToolBar(Qt.TopToolBarArea, self.windowToolBar)
153 self.addToolBar(Qt.TopToolBarArea, self.graphicsToolBar) 138 self.addToolBar(Qt.TopToolBarArea, self.graphicsToolBar)
154 139
155 def __showPixmap(self, filename): 140 def __showPixmap(self, filename):
223 @param evt reference to the gesture event (QGestureEvent 208 @param evt reference to the gesture event (QGestureEvent
224 """ 209 """
225 pinch = evt.gesture(Qt.PinchGesture) 210 pinch = evt.gesture(Qt.PinchGesture)
226 if pinch: 211 if pinch:
227 if pinch.state() == Qt.GestureStarted: 212 if pinch.state() == Qt.GestureStarted:
228 pinch.setScaleFactor(self.zoom) 213 pinch.setScaleFactor(self.__zoom() / 100)
229 else: 214 else:
230 self.__doZoom(pinch.scaleFactor() / self.zoom) 215 self.__doZoom(int(pinch.scaleFactor() * 100))
231 evt.accept() 216 evt.accept()
232 217
233 ############################################################################ 218 ############################################################################
234 ## Private menu handling methods below. 219 ## Private menu handling methods below.
235 ############################################################################ 220 ############################################################################
242 @param factor factor to adjust by (float) 227 @param factor factor to adjust by (float)
243 """ 228 """
244 scrollBar.setValue(int(factor * scrollBar.value() 229 scrollBar.setValue(int(factor * scrollBar.value()
245 + ((factor - 1) * scrollBar.pageStep() / 2))) 230 + ((factor - 1) * scrollBar.pageStep() / 2)))
246 231
247 def __doZoom(self, factor): 232 def __levelForZoom(self, zoom):
248 """ 233 """
249 Private method to perform the zooming. 234 Private method determining the zoom level index given a zoom factor.
250 235
251 @param factor zoom factor (float) 236 @param zoom zoom factor (integer)
252 """ 237 @return index of zoom factor (integer)
253 self.zoom *= factor 238 """
254 self.pixmapLabel.resize(self.zoom * self.pixmapLabel.pixmap().size()) 239 try:
255 240 index = PixmapDiagram.ZoomLevels.index(zoom)
256 self.__adjustScrollBar(self.pixmapView.horizontalScrollBar(), factor) 241 except ValueError:
257 self.__adjustScrollBar(self.pixmapView.verticalScrollBar(), factor) 242 for index in range(len(PixmapDiagram.ZoomLevels)):
243 if zoom <= PixmapDiagram.ZoomLevels[index]:
244 break
245 return index
246
247 def __doZoom(self, value):
248 """
249 Public method to set the zoom value in percent.
250
251 @param value zoom value in percent (integer)
252 """
253 oldValue = self.__zoom()
254 if value != oldValue:
255 self.pixmapLabel.resize(value / 100 * self.pixmapLabel.pixmap().size())
256
257 factor = value / oldValue
258 self.__adjustScrollBar(self.pixmapView.horizontalScrollBar(), factor)
259 self.__adjustScrollBar(self.pixmapView.verticalScrollBar(), factor)
260
261 self.__zoomWidget.setValue(value)
258 262
259 def __zoomIn(self): 263 def __zoomIn(self):
260 """ 264 """
261 Private method to handle the zoom in context menu entry. 265 Private method to zoom into the pixmap.
262 """ 266 """
263 self.__doZoom(1.25) 267 index = self.__levelForZoom(self.__zoom())
268 if index < len(PixmapDiagram.ZoomLevels) - 1:
269 self.__doZoom(PixmapDiagram.ZoomLevels[index + 1])
264 270
265 def __zoomOut(self): 271 def __zoomOut(self):
266 """ 272 """
267 Private method to handle the zoom out context menu entry. 273 Private method to zoom out of the pixmap.
268 """ 274 """
269 self.__doZoom(0.8) 275 index = self.__levelForZoom(self.__zoom())
276 if index > 0:
277 self.__doZoom(PixmapDiagram.ZoomLevels[index - 1])
270 278
271 def __zoomReset(self): 279 def __zoomReset(self):
272 """ 280 """
273 Private method to handle the reset zoom context menu entry. 281 Private method to reset the zoom value.
274 """ 282 """
275 self.zoom = 1.0 283 self.__doZoom(PixmapDiagram.ZoomLevels[PixmapDiagram.ZoomLevelDefault])
276 self.pixmapLabel.adjustSize()
277 284
278 def __zoom(self): 285 def __zoom(self):
279 """ 286 """
280 Private method to handle the zoom context menu action. 287 Public method to get the current zoom factor in percent.
281 """ 288
282 dlg = ZoomDialog(self.zoom, self) 289 @return current zoom factor in percent (integer)
283 if dlg.exec_() == QDialog.Accepted: 290 """
284 zoom = dlg.getZoomSize() 291 return int(self.pixmapLabel.width() / self.pixmapLabel.pixmap().width() * 100.0)
285 factor = zoom / self.zoom
286 self.__doZoom(factor)
287 292
288 def __printDiagram(self): 293 def __printDiagram(self):
289 """ 294 """
290 Private slot called to print the diagram. 295 Private slot called to print the diagram.
291 """ 296 """

eric ide

mercurial