From 68a61ff2dc80644edd25d2b909ac2183cedd110e Mon Sep 17 00:00:00 2001 From: Pierre Raybaut Date: Mon, 16 Oct 2023 16:27:08 +0200 Subject: [PATCH] plot_renderer: fixed resolution type + added tests --- qwt/plot.py | 32 +++++++++++-------------- qwt/plot_renderer.py | 42 ++++++++++++++++----------------- qwt/tests/test_bodedemo.py | 48 +++++++++++++++++++++++++------------- qwt/tests/test_simple.py | 17 ++++++++++++-- 4 files changed, 80 insertions(+), 59 deletions(-) diff --git a/qwt/plot.py b/qwt/plot.py index 6860ebd..038ae89 100644 --- a/qwt/plot.py +++ b/qwt/plot.py @@ -21,27 +21,21 @@ import math -from qtpy.QtWidgets import ( - QWidget, - QSizePolicy, - QFrame, - QApplication, -) -from qtpy.QtGui import QFont, QPainter, QPalette, QColor, QBrush -from qtpy.QtCore import Qt, Signal, QEvent, QSize, QRectF +import numpy as np +from qtpy.QtCore import QEvent, QRectF, QSize, Qt, Signal +from qtpy.QtGui import QBrush, QColor, QFont, QPainter, QPalette +from qtpy.QtWidgets import QApplication, QFrame, QSizePolicy, QWidget -from qwt.text import QwtText, QwtTextLabel -from qwt.scale_widget import QwtScaleWidget -from qwt.scale_draw import QwtScaleDraw -from qwt.scale_engine import QwtLinearScaleEngine +from qwt.graphic import QwtGraphic +from qwt.interval import QwtInterval +from qwt.legend import QwtLegendData from qwt.plot_canvas import QwtPlotCanvas from qwt.scale_div import QwtScaleDiv +from qwt.scale_draw import QwtScaleDraw +from qwt.scale_engine import QwtLinearScaleEngine from qwt.scale_map import QwtScaleMap -from qwt.graphic import QwtGraphic -from qwt.legend import QwtLegendData -from qwt.interval import QwtInterval - -import numpy as np +from qwt.scale_widget import QwtScaleWidget +from qwt.text import QwtText, QwtTextLabel def qwtSetTabOrder(first, second, with_children): @@ -1644,7 +1638,7 @@ def print_(self, printer): renderer.renderTo(self, printer) def exportTo( - self, filename, size=(800, 600), size_mm=None, resolution=72.0, format_=None + self, filename, size=(800, 600), size_mm=None, resolution=85, format_=None ): """ Export plot to PDF or image file (SVG, PNG, ...) @@ -1652,7 +1646,7 @@ def exportTo( :param str filename: Filename :param tuple size: (width, height) size in pixels :param tuple size_mm: (width, height) size in millimeters - :param float resolution: Image resolution + :param int resolution: Resolution in dots per Inch (dpi) :param str format_: File format (PDF, SVG, PNG, ...) """ if size_mm is None: diff --git a/qwt/plot_renderer.py b/qwt/plot_renderer.py index 858d24d..7eabba1 100644 --- a/qwt/plot_renderer.py +++ b/qwt/plot_renderer.py @@ -13,31 +13,31 @@ :members: """ -from qwt.painter import QwtPainter -from qwt.plot import QwtPlot -from qwt.plot_layout import QwtPlotLayout -from qwt.scale_draw import QwtScaleDraw -from qwt.scale_map import QwtScaleMap +import math +import os.path as osp +from qtpy.compat import getsavefilename +from qtpy.QtCore import QObject, QRect, QRectF, QSizeF, Qt from qtpy.QtGui import ( - QPainter, - QImageWriter, - QImage, QColor, + QImage, + QImageWriter, QPaintDevice, - QTransform, - QPalette, + QPainter, QPainterPath, + QPalette, QPen, + QTransform, ) -from qtpy.QtWidgets import QFileDialog from qtpy.QtPrintSupport import QPrinter -from qtpy.QtCore import Qt, QRect, QRectF, QObject, QSizeF from qtpy.QtSvg import QSvgGenerator -from qtpy.compat import getsavefilename +from qtpy.QtWidgets import QFileDialog -import math -import os.path as osp +from qwt.painter import QwtPainter +from qwt.plot import QwtPlot +from qwt.plot_layout import QwtPlotLayout +from qwt.scale_draw import QwtScaleDraw +from qwt.scale_map import QwtScaleMap def qwtCanvasClip(canvas, canvasRect): @@ -368,9 +368,9 @@ def render(self, plot, painter, plotRect): scaleWidget = plot.axisWidget(axisId) if scaleWidget: mgn = scaleWidget.contentsMargins() - baseLineDists[axisId] = max([ - mgn.left(), mgn.top(), mgn.right(), mgn.bottom() - ]) + baseLineDists[axisId] = max( + [mgn.left(), mgn.top(), mgn.right(), mgn.bottom()] + ) scaleWidget.setMargin(0) if not plot.axisEnabled(axisId): # When we have a scale the frame is painted on @@ -434,9 +434,7 @@ def render(self, plot, painter, plotRect): scaleWidget = plot.axisWidget(axisId) if scaleWidget: mgn = scaleWidget.contentsMargins() - baseDist = max([ - mgn.left(), mgn.top(), mgn.right(), mgn.bottom() - ]) + baseDist = max([mgn.left(), mgn.top(), mgn.right(), mgn.bottom()]) startDist, endDist = scaleWidget.getBorderDistHint() self.renderScale( plot, @@ -558,8 +556,8 @@ def renderCanvas(self, plot, painter, canvasRect, maps): :param qwt.plot.QwtPlot plot: Plot widget :param QPainter painter: Painter - :param qwt.scale_map.QwtScaleMap maps: mapping between plot and paint device coordinates :param QRectF rect: Bounding rectangle + :param qwt.scale_map.QwtScaleMap maps: mapping between plot and paint device coordinates """ canvas = plot.canvas() r = canvasRect.adjusted(0.0, 0.0, -1.0, 1.0) diff --git a/qwt/tests/test_bodedemo.py b/qwt/tests/test_bodedemo.py index ee33ca0..a88c33c 100644 --- a/qwt/tests/test_bodedemo.py +++ b/qwt/tests/test_bodedemo.py @@ -8,35 +8,35 @@ SHOW = True # Show test in GUI-based test launcher -import numpy as np +import os +import numpy as np +from qtpy.QtCore import Qt +from qtpy.QtGui import QFont, QIcon, QPen, QPixmap +from qtpy.QtPrintSupport import QPrintDialog, QPrinter from qtpy.QtWidgets import ( QFrame, - QWidget, - QMainWindow, - QToolButton, - QToolBar, QHBoxLayout, QLabel, + QMainWindow, + QToolBar, + QToolButton, + QWidget, ) -from qtpy.QtGui import QPen, QFont, QIcon, QPixmap -from qtpy.QtPrintSupport import QPrinter, QPrintDialog -from qtpy.QtCore import Qt from qwt import ( + QwtLegend, + QwtLogScaleEngine, QwtPlot, + QwtPlotCurve, + QwtPlotGrid, QwtPlotMarker, + QwtPlotRenderer, QwtSymbol, - QwtLegend, - QwtPlotGrid, - QwtPlotCurve, - QwtLogScaleEngine, QwtText, - QwtPlotRenderer, ) from qwt.tests import utils - print_xpm = [ "32 32 12 1", "a c #ffffff", @@ -193,6 +193,9 @@ def setDamp(self, d): self.replot() +FNAME_PDF = "bode.pdf" + + class BodeDemo(QMainWindow): def __init__(self, *args): QMainWindow.__init__(self, *args) @@ -236,7 +239,10 @@ def __init__(self, *args): self.showInfo() - def print_(self): + if utils.TestEnvironment().unattended: + self.print_(unattended=True) + + def print_(self, unattended=False): printer = QPrinter(QPrinter.HighResolution) printer.setCreator("Bode example") @@ -249,7 +255,15 @@ def print_(self): printer.setDocName(docName) dialog = QPrintDialog(printer) - if dialog.exec_(): + if unattended: + # Configure QPrinter object to print to PDF file + printer.setOutputFormat(QPrinter.PdfFormat) + printer.setOutputFileName(FNAME_PDF) + dialog.accept() + ok = True + else: + ok = dialog.exec_() + if ok: renderer = QwtPlotRenderer() if QPrinter.GrayScale == printer.colorMode(): renderer.setDiscardFlag(QwtPlotRenderer.DiscardBackground) @@ -280,6 +294,8 @@ def selected(self, _): def test_bodedemo(): """Bode demo""" utils.test_widget(BodeDemo, (640, 480)) + if os.path.isfile(FNAME_PDF): + os.remove(FNAME_PDF) if __name__ == "__main__": diff --git a/qwt/tests/test_simple.py b/qwt/tests/test_simple.py index 99de087..9de6e5a 100644 --- a/qwt/tests/test_simple.py +++ b/qwt/tests/test_simple.py @@ -8,13 +8,16 @@ SHOW = True # Show test in GUI-based test launcher -import numpy as np +import os -from qtpy.QtCore import Qt +import numpy as np +from qtpy.QtCore import Qt, QTimer import qwt from qwt.tests import utils +FNAMES = ("test_simple.svg", "test_simple.pdf", "test_simple.png") + class SimplePlot(qwt.QwtPlot): def __init__(self): @@ -52,10 +55,20 @@ def __init__(self): plot=self, ) + if utils.TestEnvironment().unattended: + QTimer.singleShot(0, self.export_to_different_formats) + + def export_to_different_formats(self): + for fname in FNAMES: + self.exportTo(fname) + def test_simple(): """Simple plot example""" utils.test_widget(SimplePlot, size=(600, 400)) + for fname in FNAMES: + if os.path.isfile(fname): + os.remove(fname) if __name__ == "__main__":