src/eric7/EricGraphics/EricGraphicsView.py

branch
eric7
changeset 10922
36a90a94765c
parent 10917
4f40180b98dc
child 10923
9b0dee552ccc
--- a/src/eric7/EricGraphics/EricGraphicsView.py	Mon Sep 23 14:37:31 2024 +0200
+++ b/src/eric7/EricGraphics/EricGraphicsView.py	Tue Sep 24 17:52:41 2024 +0200
@@ -9,14 +9,19 @@
 
 import sys
 
-from PyQt6.QtCore import QRectF, QSize, QSizeF, Qt, pyqtSignal
+from PyQt6.QtCore import (
+    QCoreApplication,
+    QMarginsF,
+    QRectF,
+    QSize,
+    QSizeF,
+    Qt,
+    pyqtSignal,
+)
 from PyQt6.QtGui import QBrush, QColor, QFont, QPainter, QPixmap
 from PyQt6.QtSvg import QSvgGenerator
 from PyQt6.QtWidgets import QGraphicsView
 
-from eric7 import Preferences
-from eric7.EricWidgets.EricApplication import ericApp
-
 
 class EricGraphicsView(QGraphicsView):
     """
@@ -59,12 +64,15 @@
     ]
     ZoomLevelDefault = 100
 
-    def __init__(self, scene, parent=None):
+    def __init__(self, scene, drawingMode="automatic", parent=None):
         """
         Constructor
 
         @param scene reference to the scene object
         @type QGraphicsScene
+        @param drawingMode name of the drawing mode (one of "automatic",
+            "black_white" or "white_black") (defaults to "automatic")
+        @type str (optional)
         @param parent parent widget
         @type QWidget
         """
@@ -72,7 +80,9 @@
         self.setObjectName("EricGraphicsView")
 
         self.__initialSceneSize = self.scene().sceneRect().size()
-        self.setBackgroundBrush(QBrush(self.getBackgroundColor()))
+        self.setBackgroundBrush(
+            QBrush(self.getBackgroundColor(drawingMode=drawingMode))
+        )
         self.setRenderHint(QPainter.RenderHint.Antialiasing, True)
         self.setDragMode(QGraphicsView.DragMode.RubberBandDrag)
         self.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop)
@@ -100,16 +110,18 @@
             )
         )
 
-    def getDrawingColors(self):
+    def getDrawingColors(self, drawingMode="automatic"):
         """
         Public method to get the configured drawing colors.
 
+        @param drawingMode name of the drawing mode (one of "automatic",
+            "black_white" or "white_black") (defaults to "automatic")
+        @type str (optional)
         @return tuple containing the foreground and background colors
         @rtype tuple of (QColor, QColor)
         """
-        drawingMode = Preferences.getGraphics("DrawingMode")
         if drawingMode == "automatic":
-            if ericApp().usesDarkPalette():
+            if QCoreApplication.instance().usesDarkPalette():
                 drawingMode = "white_black"
             else:
                 drawingMode = "black_white"
@@ -119,23 +131,29 @@
         else:
             return (QColor("#000000"), QColor("#ffffff"))
 
-    def getForegroundColor(self):
+    def getForegroundColor(self, drawingMode="automatic"):
         """
         Public method to get the configured foreground color.
 
+        @param drawingMode name of the drawing mode (one of "automatic",
+            "black_white" or "white_black") (defaults to "automatic")
+        @type str (optional)
         @return foreground color
         @rtype QColor
         """
-        return self.getDrawingColors()[0]
+        return self.getDrawingColors(drawingMode=drawingMode)[0]
 
-    def getBackgroundColor(self):
+    def getBackgroundColor(self, drawingMode="automatic"):
         """
         Public method to get the configured background color.
 
+        @param drawingMode name of the drawing mode (one of "automatic",
+            "black_white" or "white_black") (defaults to "automatic")
+        @type str (optional)
         @return background color
         @rtype QColor
         """
-        return self.getDrawingColors()[1]
+        return self.getDrawingColors(drawingMode=drawingMode)[1]
 
     def __levelForZoom(self, zoom):
         """
@@ -377,15 +395,22 @@
             pixmap = self.__getDiagram(rect)
             return pixmap.save(filename, imageFormat)
 
-    def printDiagram(self, printer, diagramName=""):
+    def printDiagram(
+        self, printer, margins=None, diagramName=""
+    ):
         """
         Public method to print the diagram.
 
         @param printer reference to a ready configured printer object
         @type QPrinter
+        @param margins diagram margins (defaults to None)
+        @type QMarginsF or None (optional)
         @param diagramName name of the diagram
-        @type float
+        @type str
         """
+        if margins is None:
+            margins = QMarginsF(1.0, 1.0, 1.0, 1.0)
+
         painter = QPainter(printer)
 
         font = QFont(["times"], 10)
@@ -396,30 +421,24 @@
             printer.pageLayout().paintRectPixels(printer.resolution()).x()
             - printer.pageLayout().fullRectPixels(printer.resolution()).x()
         )
-        marginX = (
-            int(Preferences.getPrinter("LeftMargin") * printer.resolution() / 2.54)
-            - marginX
-        )
+        marginX = int(margins.left() * printer.resolution() / 2.54) - marginX
         marginY = (
             printer.pageLayout().paintRectPixels(printer.resolution()).y()
             - printer.pageLayout().fullRectPixels(printer.resolution()).y()
         )
-        marginY = (
-            int(Preferences.getPrinter("TopMargin") * printer.resolution() / 2.54)
-            - marginY
-        )
+        marginY = int(margins.top() * printer.resolution() / 2.54) - marginY
 
         width = (
             printer.width()
             - marginX
-            - int(Preferences.getPrinter("RightMargin") * printer.resolution() / 2.54)
+            - int(margins.right() * printer.resolution() / 2.54)
         )
         height = (
             printer.height()
             - fontHeight
             - 4
             - marginY
-            - int(Preferences.getPrinter("BottomMargin") * printer.resolution() / 2.54)
+            - int(margins.bottom() * printer.resolution() / 2.54)
         )
 
         self.render(painter, target=QRectF(marginX, marginY, width, height))

eric ide

mercurial