Graphics/PixmapDiagram.py

branch
Py2 comp.
changeset 3057
10516539f238
parent 2525
8b507a9a2d40
parent 2992
dbdf27746da5
child 3058
0a02c433f52d
equal deleted inserted replaced
3056:9986ec0e559a 3057:10516539f238
8 """ 8 """
9 9
10 from __future__ import unicode_literals # __IGNORE_WARNING__ 10 from __future__ import unicode_literals # __IGNORE_WARNING__
11 11
12 from PyQt4.QtCore import Qt, QSize, QEvent 12 from PyQt4.QtCore import Qt, QSize, QEvent
13 from PyQt4.QtGui import QLabel, QPalette, QSizePolicy, QScrollArea, QAction, QMenu, \ 13 from PyQt4.QtGui import QLabel, QPalette, QSizePolicy, QScrollArea, QAction, \
14 QToolBar, QImage, QPixmap, QPrinter, QPrintDialog, QPainter, QFont, QColor 14 QMenu, QToolBar, QImage, QPixmap, QPrinter, QPrintDialog, QPainter, \
15 QFont, QColor
15 16
16 from E5Gui import E5MessageBox 17 from E5Gui import E5MessageBox
17 from E5Gui.E5MainWindow import E5MainWindow 18 from E5Gui.E5MainWindow import E5MainWindow
18 from E5Gui.E5ZoomWidget import E5ZoomWidget 19 from E5Gui.E5ZoomWidget import E5ZoomWidget
19 20
51 self.setWindowTitle(self.trUtf8("Pixmap-Viewer")) 52 self.setWindowTitle(self.trUtf8("Pixmap-Viewer"))
52 53
53 self.pixmapLabel = QLabel() 54 self.pixmapLabel = QLabel()
54 self.pixmapLabel.setObjectName("pixmapLabel") 55 self.pixmapLabel.setObjectName("pixmapLabel")
55 self.pixmapLabel.setBackgroundRole(QPalette.Base) 56 self.pixmapLabel.setBackgroundRole(QPalette.Base)
56 self.pixmapLabel.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) 57 self.pixmapLabel.setSizePolicy(
58 QSizePolicy.Ignored, QSizePolicy.Ignored)
57 self.pixmapLabel.setScaledContents(True) 59 self.pixmapLabel.setScaledContents(True)
58 60
59 self.pixmapView = QScrollArea() 61 self.pixmapView = QScrollArea()
60 self.pixmapView.setObjectName("pixmapView") 62 self.pixmapView.setObjectName("pixmapView")
61 self.pixmapView.setBackgroundRole(QPalette.Dark) 63 self.pixmapView.setBackgroundRole(QPalette.Dark)
62 self.pixmapView.setWidget(self.pixmapLabel) 64 self.pixmapView.setWidget(self.pixmapLabel)
63 65
64 self.setCentralWidget(self.pixmapView) 66 self.setCentralWidget(self.pixmapView)
65 67
66 self.__zoomWidget = E5ZoomWidget(UI.PixmapCache.getPixmap("zoomOut.png"), 68 self.__zoomWidget = E5ZoomWidget(
69 UI.PixmapCache.getPixmap("zoomOut.png"),
67 UI.PixmapCache.getPixmap("zoomIn.png"), 70 UI.PixmapCache.getPixmap("zoomIn.png"),
68 UI.PixmapCache.getPixmap("zoomReset.png"), self) 71 UI.PixmapCache.getPixmap("zoomReset.png"), self)
69 self.statusBar().addPermanentWidget(self.__zoomWidget) 72 self.statusBar().addPermanentWidget(self.__zoomWidget)
70 self.__zoomWidget.setMapping( 73 self.__zoomWidget.setMapping(
71 PixmapDiagram.ZoomLevels, PixmapDiagram.ZoomLevelDefault) 74 PixmapDiagram.ZoomLevels, PixmapDiagram.ZoomLevelDefault)
215 pinch.setScaleFactor(self.__zoom() / 100) 218 pinch.setScaleFactor(self.__zoom() / 100)
216 else: 219 else:
217 self.__doZoom(int(pinch.scaleFactor() * 100)) 220 self.__doZoom(int(pinch.scaleFactor() * 100))
218 evt.accept() 221 evt.accept()
219 222
220 ############################################################################ 223 ###########################################################################
221 ## Private menu handling methods below. 224 ## Private menu handling methods below.
222 ############################################################################ 225 ###########################################################################
223 226
224 def __adjustScrollBar(self, scrollBar, factor): 227 def __adjustScrollBar(self, scrollBar, factor):
225 """ 228 """
226 Private method to adjust a scrollbar by a certain factor. 229 Private method to adjust a scrollbar by a certain factor.
227 230
252 255
253 @param value zoom value in percent (integer) 256 @param value zoom value in percent (integer)
254 """ 257 """
255 oldValue = self.__zoom() 258 oldValue = self.__zoom()
256 if value != oldValue: 259 if value != oldValue:
257 self.pixmapLabel.resize(value / 100 * self.pixmapLabel.pixmap().size()) 260 self.pixmapLabel.resize(
261 value / 100 * self.pixmapLabel.pixmap().size())
258 262
259 factor = value / oldValue 263 factor = value / oldValue
260 self.__adjustScrollBar(self.pixmapView.horizontalScrollBar(), factor) 264 self.__adjustScrollBar(
261 self.__adjustScrollBar(self.pixmapView.verticalScrollBar(), factor) 265 self.pixmapView.horizontalScrollBar(), factor)
266 self.__adjustScrollBar(
267 self.pixmapView.verticalScrollBar(), factor)
262 268
263 self.__zoomWidget.setValue(value) 269 self.__zoomWidget.setValue(value)
264 270
265 def __zoomIn(self): 271 def __zoomIn(self):
266 """ 272 """
288 """ 294 """
289 Public method to get the current zoom factor in percent. 295 Public method to get the current zoom factor in percent.
290 296
291 @return current zoom factor in percent (integer) 297 @return current zoom factor in percent (integer)
292 """ 298 """
293 return int(self.pixmapLabel.width() / self.pixmapLabel.pixmap().width() * 100.0) 299 return int(self.pixmapLabel.width() /
300 self.pixmapLabel.pixmap().width() * 100.0)
294 301
295 def __printDiagram(self): 302 def __printDiagram(self):
296 """ 303 """
297 Private slot called to print the diagram. 304 Private slot called to print the diagram.
298 """ 305 """
354 font = QFont("times", 10) 361 font = QFont("times", 10)
355 painter.setFont(font) 362 painter.setFont(font)
356 fm = painter.fontMetrics() 363 fm = painter.fontMetrics()
357 fontHeight = fm.lineSpacing() 364 fontHeight = fm.lineSpacing()
358 marginX = printer.pageRect().x() - printer.paperRect().x() 365 marginX = printer.pageRect().x() - printer.paperRect().x()
359 marginX = \ 366 marginX = Preferences.getPrinter("LeftMargin") * \
360 Preferences.getPrinter("LeftMargin") * int(printer.resolution() / 2.54) \ 367 int(printer.resolution() / 2.54) - marginX
361 - marginX
362 marginY = printer.pageRect().y() - printer.paperRect().y() 368 marginY = printer.pageRect().y() - printer.paperRect().y()
363 marginY = \ 369 marginY = Preferences.getPrinter("TopMargin") * \
364 Preferences.getPrinter("TopMargin") * int(printer.resolution() / 2.54) \ 370 int(printer.resolution() / 2.54) - marginY
365 - marginY 371
366 372 width = printer.width() - marginX - \
367 width = printer.width() - marginX \ 373 Preferences.getPrinter("RightMargin") * \
368 - Preferences.getPrinter("RightMargin") * int(printer.resolution() / 2.54) 374 int(printer.resolution() / 2.54)
369 height = printer.height() - fontHeight - 4 - marginY \ 375 height = printer.height() - fontHeight - 4 - marginY - \
370 - Preferences.getPrinter("BottomMargin") * int(printer.resolution() / 2.54) 376 Preferences.getPrinter("BottomMargin") * \
377 int(printer.resolution() / 2.54)
371 378
372 # write a foot note 379 # write a foot note
373 s = self.trUtf8("Diagram: {0}").format(self.getDiagramName()) 380 s = self.trUtf8("Diagram: {0}").format(self.getDiagramName())
374 tc = QColor(50, 50, 50) 381 tc = QColor(50, 50, 50)
375 painter.setPen(tc) 382 painter.setPen(tc)
382 389
383 # render the diagram 390 # render the diagram
384 size = self.pixmapLabel.pixmap().size() 391 size = self.pixmapLabel.pixmap().size()
385 size.scale(QSize(width - 10, height - 10), # 5 px inner margin 392 size.scale(QSize(width - 10, height - 10), # 5 px inner margin
386 Qt.KeepAspectRatio) 393 Qt.KeepAspectRatio)
387 painter.setViewport(marginX + 5, marginY + 5, size.width(), size.height()) 394 painter.setViewport(marginX + 5, marginY + 5,
395 size.width(), size.height())
388 painter.setWindow(self.pixmapLabel.pixmap().rect()) 396 painter.setWindow(self.pixmapLabel.pixmap().rect())
389 painter.drawPixmap(0, 0, self.pixmapLabel.pixmap()) 397 painter.drawPixmap(0, 0, self.pixmapLabel.pixmap())
390 painter.end() 398 painter.end()

eric ide

mercurial