Graphics/PixmapDiagram.py

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

eric ide

mercurial