src/eric7/Graphics/PixmapDiagram.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
7 Module implementing a dialog showing a pixmap. 7 Module implementing a dialog showing a pixmap.
8 """ 8 """
9 9
10 from PyQt6.QtCore import Qt, QSize, QEvent, QMarginsF 10 from PyQt6.QtCore import Qt, QSize, QEvent, QMarginsF
11 from PyQt6.QtGui import ( 11 from PyQt6.QtGui import (
12 QPalette, QImage, QPixmap, QPainter, QFont, QColor, QAction, QPageLayout 12 QPalette,
13 QImage,
14 QPixmap,
15 QPainter,
16 QFont,
17 QColor,
18 QAction,
19 QPageLayout,
13 ) 20 )
14 from PyQt6.QtWidgets import ( 21 from PyQt6.QtWidgets import QLabel, QSizePolicy, QScrollArea, QMenu, QToolBar
15 QLabel, QSizePolicy, QScrollArea, QMenu, QToolBar
16 )
17 from PyQt6.QtPrintSupport import QPrinter, QPrintDialog 22 from PyQt6.QtPrintSupport import QPrinter, QPrintDialog
18 23
19 from EricWidgets import EricMessageBox 24 from EricWidgets import EricMessageBox
20 from EricWidgets.EricMainWindow import EricMainWindow 25 from EricWidgets.EricMainWindow import EricMainWindow
21 from EricWidgets.EricZoomWidget import EricZoomWidget 26 from EricWidgets.EricZoomWidget import EricZoomWidget
27 32
28 class PixmapDiagram(EricMainWindow): 33 class PixmapDiagram(EricMainWindow):
29 """ 34 """
30 Class implementing a dialog showing a pixmap. 35 Class implementing a dialog showing a pixmap.
31 """ 36 """
37
32 ZoomLevels = [ 38 ZoomLevels = [
33 1, 3, 5, 7, 9, 39 1,
34 10, 20, 30, 50, 67, 80, 90, 40 3,
41 5,
42 7,
43 9,
44 10,
45 20,
46 30,
47 50,
48 67,
49 80,
50 90,
35 100, 51 100,
36 110, 120, 133, 150, 170, 200, 240, 300, 400, 52 110,
37 500, 600, 700, 800, 900, 1000, 53 120,
54 133,
55 150,
56 170,
57 200,
58 240,
59 300,
60 400,
61 500,
62 600,
63 700,
64 800,
65 900,
66 1000,
38 ] 67 ]
39 ZoomLevelDefault = 100 68 ZoomLevelDefault = 100
40 69
41 def __init__(self, pixmap, parent=None, name=None): 70 def __init__(self, pixmap, parent=None, name=None):
42 """ 71 """
43 Constructor 72 Constructor
44 73
45 @param pixmap filename of a graphics file to show 74 @param pixmap filename of a graphics file to show
46 @type str 75 @type str
47 @param parent parent widget of the view 76 @param parent parent widget of the view
48 @type QWidget 77 @type QWidget
49 @param name name of the view widget 78 @param name name of the view widget
53 if name: 82 if name:
54 self.setObjectName(name) 83 self.setObjectName(name)
55 else: 84 else:
56 self.setObjectName("PixmapDiagram") 85 self.setObjectName("PixmapDiagram")
57 self.setWindowTitle(self.tr("Pixmap-Viewer")) 86 self.setWindowTitle(self.tr("Pixmap-Viewer"))
58 87
59 self.pixmapLabel = QLabel() 88 self.pixmapLabel = QLabel()
60 self.pixmapLabel.setObjectName("pixmapLabel") 89 self.pixmapLabel.setObjectName("pixmapLabel")
61 self.pixmapLabel.setBackgroundRole(QPalette.ColorRole.Base) 90 self.pixmapLabel.setBackgroundRole(QPalette.ColorRole.Base)
62 self.pixmapLabel.setSizePolicy( 91 self.pixmapLabel.setSizePolicy(
63 QSizePolicy.Policy.Ignored, QSizePolicy.Policy.Ignored) 92 QSizePolicy.Policy.Ignored, QSizePolicy.Policy.Ignored
93 )
64 self.pixmapLabel.setScaledContents(True) 94 self.pixmapLabel.setScaledContents(True)
65 95
66 self.pixmapView = QScrollArea() 96 self.pixmapView = QScrollArea()
67 self.pixmapView.setObjectName("pixmapView") 97 self.pixmapView.setObjectName("pixmapView")
68 self.pixmapView.setBackgroundRole(QPalette.ColorRole.Dark) 98 self.pixmapView.setBackgroundRole(QPalette.ColorRole.Dark)
69 self.pixmapView.setWidget(self.pixmapLabel) 99 self.pixmapView.setWidget(self.pixmapLabel)
70 100
71 self.setCentralWidget(self.pixmapView) 101 self.setCentralWidget(self.pixmapView)
72 102
73 self.__zoomWidget = EricZoomWidget( 103 self.__zoomWidget = EricZoomWidget(
74 UI.PixmapCache.getPixmap("zoomOut"), 104 UI.PixmapCache.getPixmap("zoomOut"),
75 UI.PixmapCache.getPixmap("zoomIn"), 105 UI.PixmapCache.getPixmap("zoomIn"),
76 UI.PixmapCache.getPixmap("zoomReset"), self) 106 UI.PixmapCache.getPixmap("zoomReset"),
107 self,
108 )
77 self.statusBar().addPermanentWidget(self.__zoomWidget) 109 self.statusBar().addPermanentWidget(self.__zoomWidget)
78 self.__zoomWidget.setMapping( 110 self.__zoomWidget.setMapping(
79 PixmapDiagram.ZoomLevels, PixmapDiagram.ZoomLevelDefault) 111 PixmapDiagram.ZoomLevels, PixmapDiagram.ZoomLevelDefault
112 )
80 self.__zoomWidget.valueChanged.connect(self.__doZoom) 113 self.__zoomWidget.valueChanged.connect(self.__doZoom)
81 114
82 # polish up the dialog 115 # polish up the dialog
83 self.resize(QSize(800, 600).expandedTo(self.minimumSizeHint())) 116 self.resize(QSize(800, 600).expandedTo(self.minimumSizeHint()))
84 117
85 self.pixmapfile = pixmap 118 self.pixmapfile = pixmap
86 self.status = self.__showPixmap(self.pixmapfile) 119 self.status = self.__showPixmap(self.pixmapfile)
87 120
88 self.__initActions() 121 self.__initActions()
89 self.__initContextMenu() 122 self.__initContextMenu()
90 self.__initToolBars() 123 self.__initToolBars()
91 124
92 self.grabGesture(Qt.GestureType.PinchGesture) 125 self.grabGesture(Qt.GestureType.PinchGesture)
93 126
94 def __initActions(self): 127 def __initActions(self):
95 """ 128 """
96 Private method to initialize the view actions. 129 Private method to initialize the view actions.
97 """ 130 """
98 self.closeAct = QAction( 131 self.closeAct = QAction(UI.PixmapCache.getIcon("close"), self.tr("Close"), self)
99 UI.PixmapCache.getIcon("close"),
100 self.tr("Close"), self)
101 self.closeAct.triggered.connect(self.close) 132 self.closeAct.triggered.connect(self.close)
102 133
103 self.printAct = QAction( 134 self.printAct = QAction(UI.PixmapCache.getIcon("print"), self.tr("Print"), self)
104 UI.PixmapCache.getIcon("print"),
105 self.tr("Print"), self)
106 self.printAct.triggered.connect(self.__printDiagram) 135 self.printAct.triggered.connect(self.__printDiagram)
107 136
108 self.printPreviewAct = QAction( 137 self.printPreviewAct = QAction(
109 UI.PixmapCache.getIcon("printPreview"), 138 UI.PixmapCache.getIcon("printPreview"), self.tr("Print Preview"), self
110 self.tr("Print Preview"), self) 139 )
111 self.printPreviewAct.triggered.connect(self.__printPreviewDiagram) 140 self.printPreviewAct.triggered.connect(self.__printPreviewDiagram)
112 141
113 def __initContextMenu(self): 142 def __initContextMenu(self):
114 """ 143 """
115 Private method to initialize the context menu. 144 Private method to initialize the context menu.
116 """ 145 """
117 self.__menu = QMenu(self) 146 self.__menu = QMenu(self)
118 self.__menu.addAction(self.closeAct) 147 self.__menu.addAction(self.closeAct)
119 self.__menu.addSeparator() 148 self.__menu.addSeparator()
120 self.__menu.addAction(self.printPreviewAct) 149 self.__menu.addAction(self.printPreviewAct)
121 self.__menu.addAction(self.printAct) 150 self.__menu.addAction(self.printAct)
122 151
123 self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) 152 self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
124 self.customContextMenuRequested.connect(self.__showContextMenu) 153 self.customContextMenuRequested.connect(self.__showContextMenu)
125 154
126 def __showContextMenu(self, coord): 155 def __showContextMenu(self, coord):
127 """ 156 """
128 Private slot to show the context menu of the listview. 157 Private slot to show the context menu of the listview.
129 158
130 @param coord the position of the mouse pointer 159 @param coord the position of the mouse pointer
131 @type QPoint 160 @type QPoint
132 """ 161 """
133 self.__menu.popup(self.mapToGlobal(coord)) 162 self.__menu.popup(self.mapToGlobal(coord))
134 163
135 def __initToolBars(self): 164 def __initToolBars(self):
136 """ 165 """
137 Private method to populate the toolbars with our actions. 166 Private method to populate the toolbars with our actions.
138 """ 167 """
139 self.windowToolBar = QToolBar(self.tr("Window"), self) 168 self.windowToolBar = QToolBar(self.tr("Window"), self)
140 self.windowToolBar.setIconSize(UI.Config.ToolBarIconSize) 169 self.windowToolBar.setIconSize(UI.Config.ToolBarIconSize)
141 self.windowToolBar.addAction(self.closeAct) 170 self.windowToolBar.addAction(self.closeAct)
142 171
143 self.graphicsToolBar = QToolBar(self.tr("Graphics"), self) 172 self.graphicsToolBar = QToolBar(self.tr("Graphics"), self)
144 self.graphicsToolBar.setIconSize(UI.Config.ToolBarIconSize) 173 self.graphicsToolBar.setIconSize(UI.Config.ToolBarIconSize)
145 self.graphicsToolBar.addAction(self.printPreviewAct) 174 self.graphicsToolBar.addAction(self.printPreviewAct)
146 self.graphicsToolBar.addAction(self.printAct) 175 self.graphicsToolBar.addAction(self.printAct)
147 176
148 self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.windowToolBar) 177 self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.windowToolBar)
149 self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.graphicsToolBar) 178 self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.graphicsToolBar)
150 179
151 def __showPixmap(self, filename): 180 def __showPixmap(self, filename):
152 """ 181 """
153 Private method to show a file. 182 Private method to show a file.
154 183
155 @param filename name of the file to be shown 184 @param filename name of the file to be shown
156 @type str 185 @type str
157 @return flag indicating success 186 @return flag indicating success
158 @rtype bool 187 @rtype bool
159 """ 188 """
162 EricMessageBox.warning( 191 EricMessageBox.warning(
163 self, 192 self,
164 self.tr("Pixmap-Viewer"), 193 self.tr("Pixmap-Viewer"),
165 self.tr( 194 self.tr(
166 """<p>The file <b>{0}</b> cannot be displayed.""" 195 """<p>The file <b>{0}</b> cannot be displayed."""
167 """ The format is not supported.</p>""").format(filename)) 196 """ The format is not supported.</p>"""
197 ).format(filename),
198 )
168 return False 199 return False
169 200
170 self.pixmapLabel.setPixmap(QPixmap.fromImage(image)) 201 self.pixmapLabel.setPixmap(QPixmap.fromImage(image))
171 self.pixmapLabel.adjustSize() 202 self.pixmapLabel.adjustSize()
172 return True 203 return True
173 204
174 def getDiagramName(self): 205 def getDiagramName(self):
175 """ 206 """
176 Public method to retrieve a name for the diagram. 207 Public method to retrieve a name for the diagram.
177 208
178 @return name for the diagram 209 @return name for the diagram
179 @rtype str 210 @rtype str
180 """ 211 """
181 return self.pixmapfile 212 return self.pixmapfile
182 213
183 def getStatus(self): 214 def getStatus(self):
184 """ 215 """
185 Public method to retrieve the status of the canvas. 216 Public method to retrieve the status of the canvas.
186 217
187 @return flag indicating a successful pixmap loading 218 @return flag indicating a successful pixmap loading
188 @rtype bool 219 @rtype bool
189 """ 220 """
190 return self.status 221 return self.status
191 222
192 def wheelEvent(self, evt): 223 def wheelEvent(self, evt):
193 """ 224 """
194 Protected method to handle wheel events. 225 Protected method to handle wheel events.
195 226
196 @param evt reference to the wheel event 227 @param evt reference to the wheel event
197 @type QWheelEvent 228 @type QWheelEvent
198 """ 229 """
199 if evt.modifiers() & Qt.KeyboardModifier.ControlModifier: 230 if evt.modifiers() & Qt.KeyboardModifier.ControlModifier:
200 delta = evt.angleDelta().y() 231 delta = evt.angleDelta().y()
202 self.__zoomOut() 233 self.__zoomOut()
203 elif delta > 0: 234 elif delta > 0:
204 self.__zoomIn() 235 self.__zoomIn()
205 evt.accept() 236 evt.accept()
206 return 237 return
207 238
208 super().wheelEvent(evt) 239 super().wheelEvent(evt)
209 240
210 def event(self, evt): 241 def event(self, evt):
211 """ 242 """
212 Public method handling events. 243 Public method handling events.
213 244
214 @param evt reference to the event 245 @param evt reference to the event
215 @type QEvent 246 @type QEvent
216 @return flag indicating, if the event was handled 247 @return flag indicating, if the event was handled
217 @rtype bool 248 @rtype bool
218 """ 249 """
219 if evt.type() == QEvent.Type.Gesture: 250 if evt.type() == QEvent.Type.Gesture:
220 self.gestureEvent(evt) 251 self.gestureEvent(evt)
221 return True 252 return True
222 253
223 return super().event(evt) 254 return super().event(evt)
224 255
225 def gestureEvent(self, evt): 256 def gestureEvent(self, evt):
226 """ 257 """
227 Protected method handling gesture events. 258 Protected method handling gesture events.
228 259
229 @param evt reference to the gesture event 260 @param evt reference to the gesture event
230 @type QGestureEvent 261 @type QGestureEvent
231 """ 262 """
232 pinch = evt.gesture(Qt.GestureType.PinchGesture) 263 pinch = evt.gesture(Qt.GestureType.PinchGesture)
233 if pinch: 264 if pinch:
234 if pinch.state() == Qt.GestureState.GestureStarted: 265 if pinch.state() == Qt.GestureState.GestureStarted:
235 pinch.setTotalScaleFactor(self.__zoom() / 100) 266 pinch.setTotalScaleFactor(self.__zoom() / 100)
236 elif pinch.state() == Qt.GestureState.GestureUpdated: 267 elif pinch.state() == Qt.GestureState.GestureUpdated:
237 self.__doZoom(int(pinch.totalScaleFactor() * 100)) 268 self.__doZoom(int(pinch.totalScaleFactor() * 100))
238 evt.accept() 269 evt.accept()
239 270
240 ########################################################################### 271 ###########################################################################
241 ## Private menu handling methods below. 272 ## Private menu handling methods below.
242 ########################################################################### 273 ###########################################################################
243 274
244 def __adjustScrollBar(self, scrollBar, factor): 275 def __adjustScrollBar(self, scrollBar, factor):
245 """ 276 """
246 Private method to adjust a scrollbar by a certain factor. 277 Private method to adjust a scrollbar by a certain factor.
247 278
248 @param scrollBar reference to the scrollbar object 279 @param scrollBar reference to the scrollbar object
249 @type QScrollBar 280 @type QScrollBar
250 @param factor factor to adjust by 281 @param factor factor to adjust by
251 @type float 282 @type float
252 """ 283 """
253 scrollBar.setValue(int(factor * scrollBar.value() + 284 scrollBar.setValue(
254 ((factor - 1) * scrollBar.pageStep() / 2))) 285 int(factor * scrollBar.value() + ((factor - 1) * scrollBar.pageStep() / 2))
255 286 )
287
256 def __levelForZoom(self, zoom): 288 def __levelForZoom(self, zoom):
257 """ 289 """
258 Private method determining the zoom level index given a zoom factor. 290 Private method determining the zoom level index given a zoom factor.
259 291
260 @param zoom zoom factor 292 @param zoom zoom factor
261 @type int 293 @type int
262 @return index of zoom factor 294 @return index of zoom factor
263 @rtype int 295 @rtype int
264 """ 296 """
267 except ValueError: 299 except ValueError:
268 for index in range(len(PixmapDiagram.ZoomLevels)): 300 for index in range(len(PixmapDiagram.ZoomLevels)):
269 if zoom <= PixmapDiagram.ZoomLevels[index]: 301 if zoom <= PixmapDiagram.ZoomLevels[index]:
270 break 302 break
271 return index 303 return index
272 304
273 def __doZoom(self, value): 305 def __doZoom(self, value):
274 """ 306 """
275 Private method to set the zoom value in percent. 307 Private method to set the zoom value in percent.
276 308
277 @param value zoom value in percent 309 @param value zoom value in percent
278 @type int 310 @type int
279 """ 311 """
280 oldValue = self.__zoom() 312 oldValue = self.__zoom()
281 if value != oldValue: 313 if value != oldValue:
282 self.pixmapLabel.resize( 314 self.pixmapLabel.resize(value / 100 * self.pixmapLabel.pixmap().size())
283 value / 100 * self.pixmapLabel.pixmap().size()) 315
284
285 factor = value / oldValue 316 factor = value / oldValue
286 self.__adjustScrollBar( 317 self.__adjustScrollBar(self.pixmapView.horizontalScrollBar(), factor)
287 self.pixmapView.horizontalScrollBar(), factor) 318 self.__adjustScrollBar(self.pixmapView.verticalScrollBar(), factor)
288 self.__adjustScrollBar( 319
289 self.pixmapView.verticalScrollBar(), factor)
290
291 self.__zoomWidget.setValue(value) 320 self.__zoomWidget.setValue(value)
292 321
293 def __zoomIn(self): 322 def __zoomIn(self):
294 """ 323 """
295 Private method to zoom into the pixmap. 324 Private method to zoom into the pixmap.
296 """ 325 """
297 index = self.__levelForZoom(self.__zoom()) 326 index = self.__levelForZoom(self.__zoom())
298 if index < len(PixmapDiagram.ZoomLevels) - 1: 327 if index < len(PixmapDiagram.ZoomLevels) - 1:
299 self.__doZoom(PixmapDiagram.ZoomLevels[index + 1]) 328 self.__doZoom(PixmapDiagram.ZoomLevels[index + 1])
300 329
301 def __zoomOut(self): 330 def __zoomOut(self):
302 """ 331 """
303 Private method to zoom out of the pixmap. 332 Private method to zoom out of the pixmap.
304 """ 333 """
305 index = self.__levelForZoom(self.__zoom()) 334 index = self.__levelForZoom(self.__zoom())
306 if index > 0: 335 if index > 0:
307 self.__doZoom(PixmapDiagram.ZoomLevels[index - 1]) 336 self.__doZoom(PixmapDiagram.ZoomLevels[index - 1])
308 337
309 def __zoomReset(self): 338 def __zoomReset(self):
310 """ 339 """
311 Private method to reset the zoom value. 340 Private method to reset the zoom value.
312 """ 341 """
313 self.__doZoom(PixmapDiagram.ZoomLevels[PixmapDiagram.ZoomLevelDefault]) 342 self.__doZoom(PixmapDiagram.ZoomLevels[PixmapDiagram.ZoomLevelDefault])
314 343
315 def __zoom(self): 344 def __zoom(self):
316 """ 345 """
317 Private method to get the current zoom factor in percent. 346 Private method to get the current zoom factor in percent.
318 347
319 @return current zoom factor in percent 348 @return current zoom factor in percent
320 @rtype int 349 @rtype int
321 """ 350 """
322 return int(self.pixmapLabel.width() / 351 return int(self.pixmapLabel.width() / self.pixmapLabel.pixmap().width() * 100.0)
323 self.pixmapLabel.pixmap().width() * 100.0) 352
324
325 def __printDiagram(self): 353 def __printDiagram(self):
326 """ 354 """
327 Private slot called to print the diagram. 355 Private slot called to print the diagram.
328 """ 356 """
329 printer = QPrinter(mode=QPrinter.PrinterMode.ScreenResolution) 357 printer = QPrinter(mode=QPrinter.PrinterMode.ScreenResolution)
335 if Preferences.getPrinter("FirstPageFirst"): 363 if Preferences.getPrinter("FirstPageFirst"):
336 printer.setPageOrder(QPrinter.PageOrder.FirstPageFirst) 364 printer.setPageOrder(QPrinter.PageOrder.FirstPageFirst)
337 else: 365 else:
338 printer.setPageOrder(QPrinter.PageOrder.LastPageFirst) 366 printer.setPageOrder(QPrinter.PageOrder.LastPageFirst)
339 printer.setPrinterName(Preferences.getPrinter("PrinterName")) 367 printer.setPrinterName(Preferences.getPrinter("PrinterName"))
340 368
341 printDialog = QPrintDialog(printer, self) 369 printDialog = QPrintDialog(printer, self)
342 if printDialog.exec(): 370 if printDialog.exec():
343 self.__print(printer) 371 self.__print(printer)
344 372
345 def __printPreviewDiagram(self): 373 def __printPreviewDiagram(self):
346 """ 374 """
347 Private slot called to show a print preview of the diagram. 375 Private slot called to show a print preview of the diagram.
348 """ 376 """
349 from PyQt6.QtPrintSupport import QPrintPreviewDialog 377 from PyQt6.QtPrintSupport import QPrintPreviewDialog
350 378
351 printer = QPrinter(mode=QPrinter.PrinterMode.ScreenResolution) 379 printer = QPrinter(mode=QPrinter.PrinterMode.ScreenResolution)
352 printer.setFullPage(True) 380 printer.setFullPage(True)
353 if Preferences.getPrinter("ColorMode"): 381 if Preferences.getPrinter("ColorMode"):
354 printer.setColorMode(QPrinter.ColorMode.Color) 382 printer.setColorMode(QPrinter.ColorMode.Color)
355 else: 383 else:
356 printer.setColorMode(QPrinter.ColorMode.GrayScale) 384 printer.setColorMode(QPrinter.ColorMode.GrayScale)
357 if Preferences.getPrinter("FirstPageFirst"): 385 if Preferences.getPrinter("FirstPageFirst"):
358 printer.setPageOrder(QPrinter.PageOrder.FirstPageFirst) 386 printer.setPageOrder(QPrinter.PageOrder.FirstPageFirst)
359 else: 387 else:
360 printer.setPageOrder(QPrinter.PageOrder.LastPageFirst) 388 printer.setPageOrder(QPrinter.PageOrder.LastPageFirst)
361 printer.setPageMargins(QMarginsF( 389 printer.setPageMargins(
362 Preferences.getPrinter("LeftMargin") * 10, 390 QMarginsF(
363 Preferences.getPrinter("TopMargin") * 10, 391 Preferences.getPrinter("LeftMargin") * 10,
364 Preferences.getPrinter("RightMargin") * 10, 392 Preferences.getPrinter("TopMargin") * 10,
365 Preferences.getPrinter("BottomMargin") * 10), 393 Preferences.getPrinter("RightMargin") * 10,
366 QPageLayout.Unit.Millimeter 394 Preferences.getPrinter("BottomMargin") * 10,
395 ),
396 QPageLayout.Unit.Millimeter,
367 ) 397 )
368 printer.setPrinterName(Preferences.getPrinter("PrinterName")) 398 printer.setPrinterName(Preferences.getPrinter("PrinterName"))
369 399
370 preview = QPrintPreviewDialog(printer, self) 400 preview = QPrintPreviewDialog(printer, self)
371 preview.paintRequested[QPrinter].connect(self.__print) 401 preview.paintRequested[QPrinter].connect(self.__print)
372 preview.exec() 402 preview.exec()
373 403
374 def __print(self, printer): 404 def __print(self, printer):
375 """ 405 """
376 Private slot to the actual printing. 406 Private slot to the actual printing.
377 407
378 @param printer reference to the printer object 408 @param printer reference to the printer object
379 @type QPrinter 409 @type QPrinter
380 """ 410 """
381 painter = QPainter() 411 painter = QPainter()
382 painter.begin(printer) 412 painter.begin(printer)
385 font = QFont(["times"], 10) 415 font = QFont(["times"], 10)
386 painter.setFont(font) 416 painter.setFont(font)
387 fm = painter.fontMetrics() 417 fm = painter.fontMetrics()
388 fontHeight = fm.lineSpacing() 418 fontHeight = fm.lineSpacing()
389 marginX = ( 419 marginX = (
390 printer.pageLayout().paintRectPixels(printer.resolution()).x() - 420 printer.pageLayout().paintRectPixels(printer.resolution()).x()
391 printer.pageLayout().fullRectPixels(printer.resolution()).x() 421 - printer.pageLayout().fullRectPixels(printer.resolution()).x()
392 ) 422 )
393 marginX = ( 423 marginX = (
394 Preferences.getPrinter("LeftMargin") * 424 Preferences.getPrinter("LeftMargin") * int(printer.resolution() / 2.54)
395 int(printer.resolution() / 2.54) - marginX 425 - marginX
396 ) 426 )
397 marginY = ( 427 marginY = (
398 printer.pageLayout().paintRectPixels(printer.resolution()).y() - 428 printer.pageLayout().paintRectPixels(printer.resolution()).y()
399 printer.pageLayout().fullRectPixels(printer.resolution()).y() 429 - printer.pageLayout().fullRectPixels(printer.resolution()).y()
400 ) 430 )
401 marginY = ( 431 marginY = (
402 Preferences.getPrinter("TopMargin") * 432 Preferences.getPrinter("TopMargin") * int(printer.resolution() / 2.54)
403 int(printer.resolution() / 2.54) - marginY 433 - marginY
404 ) 434 )
405 435
406 width = ( 436 width = (
407 printer.width() - marginX - 437 printer.width()
408 Preferences.getPrinter("RightMargin") * 438 - marginX
409 int(printer.resolution() / 2.54) 439 - Preferences.getPrinter("RightMargin") * int(printer.resolution() / 2.54)
410 ) 440 )
411 height = ( 441 height = (
412 printer.height() - fontHeight - 4 - marginY - 442 printer.height()
413 Preferences.getPrinter("BottomMargin") * 443 - fontHeight
414 int(printer.resolution() / 2.54) 444 - 4
445 - marginY
446 - Preferences.getPrinter("BottomMargin") * int(printer.resolution() / 2.54)
415 ) 447 )
416 448
417 # write a foot note 449 # write a foot note
418 s = self.tr("Diagram: {0}").format(self.getDiagramName()) 450 s = self.tr("Diagram: {0}").format(self.getDiagramName())
419 tc = QColor(50, 50, 50) 451 tc = QColor(50, 50, 50)
420 painter.setPen(tc) 452 painter.setPen(tc)
421 painter.drawRect(marginX, marginY, width, height) 453 painter.drawRect(marginX, marginY, width, height)
422 painter.drawLine(marginX, marginY + height + 2, 454 painter.drawLine(
423 marginX + width, marginY + height + 2) 455 marginX, marginY + height + 2, marginX + width, marginY + height + 2
456 )
424 painter.setFont(font) 457 painter.setFont(font)
425 painter.drawText(marginX, marginY + height + 4, width, 458 painter.drawText(
426 fontHeight, Qt.AlignmentFlag.AlignRight, s) 459 marginX,
460 marginY + height + 4,
461 width,
462 fontHeight,
463 Qt.AlignmentFlag.AlignRight,
464 s,
465 )
427 466
428 # render the diagram 467 # render the diagram
429 size = self.pixmapLabel.pixmap().size() 468 size = self.pixmapLabel.pixmap().size()
430 size.scale(QSize(width - 10, height - 10), # 5 px inner margin 469 size.scale(
431 Qt.AspectRatioMode.KeepAspectRatio) 470 QSize(width - 10, height - 10), # 5 px inner margin
432 painter.setViewport(marginX + 5, marginY + 5, 471 Qt.AspectRatioMode.KeepAspectRatio,
433 size.width(), size.height()) 472 )
473 painter.setViewport(marginX + 5, marginY + 5, size.width(), size.height())
434 painter.setWindow(self.pixmapLabel.pixmap().rect()) 474 painter.setWindow(self.pixmapLabel.pixmap().rect())
435 painter.drawPixmap(0, 0, self.pixmapLabel.pixmap()) 475 painter.drawPixmap(0, 0, self.pixmapLabel.pixmap())
436 painter.end() 476 painter.end()

eric ide

mercurial