src/eric7/EricGraphics/EricGraphicsView.py

branch
eric7
changeset 10922
36a90a94765c
parent 10917
4f40180b98dc
child 10923
9b0dee552ccc
equal deleted inserted replaced
10921:495f084a737e 10922:36a90a94765c
7 Module implementing a canvas view class. 7 Module implementing a canvas view class.
8 """ 8 """
9 9
10 import sys 10 import sys
11 11
12 from PyQt6.QtCore import QRectF, QSize, QSizeF, Qt, pyqtSignal 12 from PyQt6.QtCore import (
13 QCoreApplication,
14 QMarginsF,
15 QRectF,
16 QSize,
17 QSizeF,
18 Qt,
19 pyqtSignal,
20 )
13 from PyQt6.QtGui import QBrush, QColor, QFont, QPainter, QPixmap 21 from PyQt6.QtGui import QBrush, QColor, QFont, QPainter, QPixmap
14 from PyQt6.QtSvg import QSvgGenerator 22 from PyQt6.QtSvg import QSvgGenerator
15 from PyQt6.QtWidgets import QGraphicsView 23 from PyQt6.QtWidgets import QGraphicsView
16
17 from eric7 import Preferences
18 from eric7.EricWidgets.EricApplication import ericApp
19 24
20 25
21 class EricGraphicsView(QGraphicsView): 26 class EricGraphicsView(QGraphicsView):
22 """ 27 """
23 Class implementing a graphics view. 28 Class implementing a graphics view.
57 900, 62 900,
58 1000, 63 1000,
59 ] 64 ]
60 ZoomLevelDefault = 100 65 ZoomLevelDefault = 100
61 66
62 def __init__(self, scene, parent=None): 67 def __init__(self, scene, drawingMode="automatic", parent=None):
63 """ 68 """
64 Constructor 69 Constructor
65 70
66 @param scene reference to the scene object 71 @param scene reference to the scene object
67 @type QGraphicsScene 72 @type QGraphicsScene
73 @param drawingMode name of the drawing mode (one of "automatic",
74 "black_white" or "white_black") (defaults to "automatic")
75 @type str (optional)
68 @param parent parent widget 76 @param parent parent widget
69 @type QWidget 77 @type QWidget
70 """ 78 """
71 super().__init__(scene, parent) 79 super().__init__(scene, parent)
72 self.setObjectName("EricGraphicsView") 80 self.setObjectName("EricGraphicsView")
73 81
74 self.__initialSceneSize = self.scene().sceneRect().size() 82 self.__initialSceneSize = self.scene().sceneRect().size()
75 self.setBackgroundBrush(QBrush(self.getBackgroundColor())) 83 self.setBackgroundBrush(
84 QBrush(self.getBackgroundColor(drawingMode=drawingMode))
85 )
76 self.setRenderHint(QPainter.RenderHint.Antialiasing, True) 86 self.setRenderHint(QPainter.RenderHint.Antialiasing, True)
77 self.setDragMode(QGraphicsView.DragMode.RubberBandDrag) 87 self.setDragMode(QGraphicsView.DragMode.RubberBandDrag)
78 self.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop) 88 self.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop)
79 self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn) 89 self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
80 self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn) 90 self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
98 "whole selection.</li>\n" 108 "whole selection.</li>\n"
99 "</ul>\n" 109 "</ul>\n"
100 ) 110 )
101 ) 111 )
102 112
103 def getDrawingColors(self): 113 def getDrawingColors(self, drawingMode="automatic"):
104 """ 114 """
105 Public method to get the configured drawing colors. 115 Public method to get the configured drawing colors.
106 116
117 @param drawingMode name of the drawing mode (one of "automatic",
118 "black_white" or "white_black") (defaults to "automatic")
119 @type str (optional)
107 @return tuple containing the foreground and background colors 120 @return tuple containing the foreground and background colors
108 @rtype tuple of (QColor, QColor) 121 @rtype tuple of (QColor, QColor)
109 """ 122 """
110 drawingMode = Preferences.getGraphics("DrawingMode")
111 if drawingMode == "automatic": 123 if drawingMode == "automatic":
112 if ericApp().usesDarkPalette(): 124 if QCoreApplication.instance().usesDarkPalette():
113 drawingMode = "white_black" 125 drawingMode = "white_black"
114 else: 126 else:
115 drawingMode = "black_white" 127 drawingMode = "black_white"
116 128
117 if drawingMode == "white_black": 129 if drawingMode == "white_black":
118 return (QColor("#ffffff"), QColor("#262626")) 130 return (QColor("#ffffff"), QColor("#262626"))
119 else: 131 else:
120 return (QColor("#000000"), QColor("#ffffff")) 132 return (QColor("#000000"), QColor("#ffffff"))
121 133
122 def getForegroundColor(self): 134 def getForegroundColor(self, drawingMode="automatic"):
123 """ 135 """
124 Public method to get the configured foreground color. 136 Public method to get the configured foreground color.
125 137
138 @param drawingMode name of the drawing mode (one of "automatic",
139 "black_white" or "white_black") (defaults to "automatic")
140 @type str (optional)
126 @return foreground color 141 @return foreground color
127 @rtype QColor 142 @rtype QColor
128 """ 143 """
129 return self.getDrawingColors()[0] 144 return self.getDrawingColors(drawingMode=drawingMode)[0]
130 145
131 def getBackgroundColor(self): 146 def getBackgroundColor(self, drawingMode="automatic"):
132 """ 147 """
133 Public method to get the configured background color. 148 Public method to get the configured background color.
134 149
150 @param drawingMode name of the drawing mode (one of "automatic",
151 "black_white" or "white_black") (defaults to "automatic")
152 @type str (optional)
135 @return background color 153 @return background color
136 @rtype QColor 154 @rtype QColor
137 """ 155 """
138 return self.getDrawingColors()[1] 156 return self.getDrawingColors(drawingMode=drawingMode)[1]
139 157
140 def __levelForZoom(self, zoom): 158 def __levelForZoom(self, zoom):
141 """ 159 """
142 Private method determining the zoom level index given a zoom factor. 160 Private method determining the zoom level index given a zoom factor.
143 161
375 return True 393 return True
376 else: 394 else:
377 pixmap = self.__getDiagram(rect) 395 pixmap = self.__getDiagram(rect)
378 return pixmap.save(filename, imageFormat) 396 return pixmap.save(filename, imageFormat)
379 397
380 def printDiagram(self, printer, diagramName=""): 398 def printDiagram(
399 self, printer, margins=None, diagramName=""
400 ):
381 """ 401 """
382 Public method to print the diagram. 402 Public method to print the diagram.
383 403
384 @param printer reference to a ready configured printer object 404 @param printer reference to a ready configured printer object
385 @type QPrinter 405 @type QPrinter
406 @param margins diagram margins (defaults to None)
407 @type QMarginsF or None (optional)
386 @param diagramName name of the diagram 408 @param diagramName name of the diagram
387 @type float 409 @type str
388 """ 410 """
411 if margins is None:
412 margins = QMarginsF(1.0, 1.0, 1.0, 1.0)
413
389 painter = QPainter(printer) 414 painter = QPainter(printer)
390 415
391 font = QFont(["times"], 10) 416 font = QFont(["times"], 10)
392 painter.setFont(font) 417 painter.setFont(font)
393 fm = painter.fontMetrics() 418 fm = painter.fontMetrics()
394 fontHeight = fm.lineSpacing() 419 fontHeight = fm.lineSpacing()
395 marginX = ( 420 marginX = (
396 printer.pageLayout().paintRectPixels(printer.resolution()).x() 421 printer.pageLayout().paintRectPixels(printer.resolution()).x()
397 - printer.pageLayout().fullRectPixels(printer.resolution()).x() 422 - printer.pageLayout().fullRectPixels(printer.resolution()).x()
398 ) 423 )
399 marginX = ( 424 marginX = int(margins.left() * printer.resolution() / 2.54) - marginX
400 int(Preferences.getPrinter("LeftMargin") * printer.resolution() / 2.54)
401 - marginX
402 )
403 marginY = ( 425 marginY = (
404 printer.pageLayout().paintRectPixels(printer.resolution()).y() 426 printer.pageLayout().paintRectPixels(printer.resolution()).y()
405 - printer.pageLayout().fullRectPixels(printer.resolution()).y() 427 - printer.pageLayout().fullRectPixels(printer.resolution()).y()
406 ) 428 )
407 marginY = ( 429 marginY = int(margins.top() * printer.resolution() / 2.54) - marginY
408 int(Preferences.getPrinter("TopMargin") * printer.resolution() / 2.54)
409 - marginY
410 )
411 430
412 width = ( 431 width = (
413 printer.width() 432 printer.width()
414 - marginX 433 - marginX
415 - int(Preferences.getPrinter("RightMargin") * printer.resolution() / 2.54) 434 - int(margins.right() * printer.resolution() / 2.54)
416 ) 435 )
417 height = ( 436 height = (
418 printer.height() 437 printer.height()
419 - fontHeight 438 - fontHeight
420 - 4 439 - 4
421 - marginY 440 - marginY
422 - int(Preferences.getPrinter("BottomMargin") * printer.resolution() / 2.54) 441 - int(margins.bottom() * printer.resolution() / 2.54)
423 ) 442 )
424 443
425 self.render(painter, target=QRectF(marginX, marginY, width, height)) 444 self.render(painter, target=QRectF(marginX, marginY, width, height))
426 445
427 # write a foot note 446 # write a foot note

eric ide

mercurial