Skip to content

Commit

Permalink
plot_renderer: fixed resolution type + added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreRaybaut committed Oct 16, 2023
1 parent ffba03e commit 68a61ff
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 59 deletions.
32 changes: 13 additions & 19 deletions qwt/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -1644,15 +1638,15 @@ 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, ...)
: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:
Expand Down
42 changes: 20 additions & 22 deletions qwt/plot_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
48 changes: 32 additions & 16 deletions qwt/tests/test_bodedemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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__":
Expand Down
17 changes: 15 additions & 2 deletions qwt/tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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__":
Expand Down

0 comments on commit 68a61ff

Please sign in to comment.