Skip to content

Commit 68a61ff

Browse files
committed
plot_renderer: fixed resolution type + added tests
1 parent ffba03e commit 68a61ff

4 files changed

Lines changed: 80 additions & 59 deletions

File tree

qwt/plot.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,21 @@
2121

2222
import math
2323

24-
from qtpy.QtWidgets import (
25-
QWidget,
26-
QSizePolicy,
27-
QFrame,
28-
QApplication,
29-
)
30-
from qtpy.QtGui import QFont, QPainter, QPalette, QColor, QBrush
31-
from qtpy.QtCore import Qt, Signal, QEvent, QSize, QRectF
24+
import numpy as np
25+
from qtpy.QtCore import QEvent, QRectF, QSize, Qt, Signal
26+
from qtpy.QtGui import QBrush, QColor, QFont, QPainter, QPalette
27+
from qtpy.QtWidgets import QApplication, QFrame, QSizePolicy, QWidget
3228

33-
from qwt.text import QwtText, QwtTextLabel
34-
from qwt.scale_widget import QwtScaleWidget
35-
from qwt.scale_draw import QwtScaleDraw
36-
from qwt.scale_engine import QwtLinearScaleEngine
29+
from qwt.graphic import QwtGraphic
30+
from qwt.interval import QwtInterval
31+
from qwt.legend import QwtLegendData
3732
from qwt.plot_canvas import QwtPlotCanvas
3833
from qwt.scale_div import QwtScaleDiv
34+
from qwt.scale_draw import QwtScaleDraw
35+
from qwt.scale_engine import QwtLinearScaleEngine
3936
from qwt.scale_map import QwtScaleMap
40-
from qwt.graphic import QwtGraphic
41-
from qwt.legend import QwtLegendData
42-
from qwt.interval import QwtInterval
43-
44-
import numpy as np
37+
from qwt.scale_widget import QwtScaleWidget
38+
from qwt.text import QwtText, QwtTextLabel
4539

4640

4741
def qwtSetTabOrder(first, second, with_children):
@@ -1644,15 +1638,15 @@ def print_(self, printer):
16441638
renderer.renderTo(self, printer)
16451639

16461640
def exportTo(
1647-
self, filename, size=(800, 600), size_mm=None, resolution=72.0, format_=None
1641+
self, filename, size=(800, 600), size_mm=None, resolution=85, format_=None
16481642
):
16491643
"""
16501644
Export plot to PDF or image file (SVG, PNG, ...)
16511645
16521646
:param str filename: Filename
16531647
:param tuple size: (width, height) size in pixels
16541648
:param tuple size_mm: (width, height) size in millimeters
1655-
:param float resolution: Image resolution
1649+
:param int resolution: Resolution in dots per Inch (dpi)
16561650
:param str format_: File format (PDF, SVG, PNG, ...)
16571651
"""
16581652
if size_mm is None:

qwt/plot_renderer.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@
1313
:members:
1414
"""
1515

16-
from qwt.painter import QwtPainter
17-
from qwt.plot import QwtPlot
18-
from qwt.plot_layout import QwtPlotLayout
19-
from qwt.scale_draw import QwtScaleDraw
20-
from qwt.scale_map import QwtScaleMap
16+
import math
17+
import os.path as osp
2118

19+
from qtpy.compat import getsavefilename
20+
from qtpy.QtCore import QObject, QRect, QRectF, QSizeF, Qt
2221
from qtpy.QtGui import (
23-
QPainter,
24-
QImageWriter,
25-
QImage,
2622
QColor,
23+
QImage,
24+
QImageWriter,
2725
QPaintDevice,
28-
QTransform,
29-
QPalette,
26+
QPainter,
3027
QPainterPath,
28+
QPalette,
3129
QPen,
30+
QTransform,
3231
)
33-
from qtpy.QtWidgets import QFileDialog
3432
from qtpy.QtPrintSupport import QPrinter
35-
from qtpy.QtCore import Qt, QRect, QRectF, QObject, QSizeF
3633
from qtpy.QtSvg import QSvgGenerator
37-
from qtpy.compat import getsavefilename
34+
from qtpy.QtWidgets import QFileDialog
3835

39-
import math
40-
import os.path as osp
36+
from qwt.painter import QwtPainter
37+
from qwt.plot import QwtPlot
38+
from qwt.plot_layout import QwtPlotLayout
39+
from qwt.scale_draw import QwtScaleDraw
40+
from qwt.scale_map import QwtScaleMap
4141

4242

4343
def qwtCanvasClip(canvas, canvasRect):
@@ -368,9 +368,9 @@ def render(self, plot, painter, plotRect):
368368
scaleWidget = plot.axisWidget(axisId)
369369
if scaleWidget:
370370
mgn = scaleWidget.contentsMargins()
371-
baseLineDists[axisId] = max([
372-
mgn.left(), mgn.top(), mgn.right(), mgn.bottom()
373-
])
371+
baseLineDists[axisId] = max(
372+
[mgn.left(), mgn.top(), mgn.right(), mgn.bottom()]
373+
)
374374
scaleWidget.setMargin(0)
375375
if not plot.axisEnabled(axisId):
376376
# When we have a scale the frame is painted on
@@ -434,9 +434,7 @@ def render(self, plot, painter, plotRect):
434434
scaleWidget = plot.axisWidget(axisId)
435435
if scaleWidget:
436436
mgn = scaleWidget.contentsMargins()
437-
baseDist = max([
438-
mgn.left(), mgn.top(), mgn.right(), mgn.bottom()
439-
])
437+
baseDist = max([mgn.left(), mgn.top(), mgn.right(), mgn.bottom()])
440438
startDist, endDist = scaleWidget.getBorderDistHint()
441439
self.renderScale(
442440
plot,
@@ -558,8 +556,8 @@ def renderCanvas(self, plot, painter, canvasRect, maps):
558556
559557
:param qwt.plot.QwtPlot plot: Plot widget
560558
:param QPainter painter: Painter
561-
:param qwt.scale_map.QwtScaleMap maps: mapping between plot and paint device coordinates
562559
:param QRectF rect: Bounding rectangle
560+
:param qwt.scale_map.QwtScaleMap maps: mapping between plot and paint device coordinates
563561
"""
564562
canvas = plot.canvas()
565563
r = canvasRect.adjusted(0.0, 0.0, -1.0, 1.0)

qwt/tests/test_bodedemo.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,35 @@
88

99
SHOW = True # Show test in GUI-based test launcher
1010

11-
import numpy as np
11+
import os
1212

13+
import numpy as np
14+
from qtpy.QtCore import Qt
15+
from qtpy.QtGui import QFont, QIcon, QPen, QPixmap
16+
from qtpy.QtPrintSupport import QPrintDialog, QPrinter
1317
from qtpy.QtWidgets import (
1418
QFrame,
15-
QWidget,
16-
QMainWindow,
17-
QToolButton,
18-
QToolBar,
1919
QHBoxLayout,
2020
QLabel,
21+
QMainWindow,
22+
QToolBar,
23+
QToolButton,
24+
QWidget,
2125
)
22-
from qtpy.QtGui import QPen, QFont, QIcon, QPixmap
23-
from qtpy.QtPrintSupport import QPrinter, QPrintDialog
24-
from qtpy.QtCore import Qt
2526

2627
from qwt import (
28+
QwtLegend,
29+
QwtLogScaleEngine,
2730
QwtPlot,
31+
QwtPlotCurve,
32+
QwtPlotGrid,
2833
QwtPlotMarker,
34+
QwtPlotRenderer,
2935
QwtSymbol,
30-
QwtLegend,
31-
QwtPlotGrid,
32-
QwtPlotCurve,
33-
QwtLogScaleEngine,
3436
QwtText,
35-
QwtPlotRenderer,
3637
)
3738
from qwt.tests import utils
3839

39-
4040
print_xpm = [
4141
"32 32 12 1",
4242
"a c #ffffff",
@@ -193,6 +193,9 @@ def setDamp(self, d):
193193
self.replot()
194194

195195

196+
FNAME_PDF = "bode.pdf"
197+
198+
196199
class BodeDemo(QMainWindow):
197200
def __init__(self, *args):
198201
QMainWindow.__init__(self, *args)
@@ -236,7 +239,10 @@ def __init__(self, *args):
236239

237240
self.showInfo()
238241

239-
def print_(self):
242+
if utils.TestEnvironment().unattended:
243+
self.print_(unattended=True)
244+
245+
def print_(self, unattended=False):
240246
printer = QPrinter(QPrinter.HighResolution)
241247

242248
printer.setCreator("Bode example")
@@ -249,7 +255,15 @@ def print_(self):
249255
printer.setDocName(docName)
250256

251257
dialog = QPrintDialog(printer)
252-
if dialog.exec_():
258+
if unattended:
259+
# Configure QPrinter object to print to PDF file
260+
printer.setOutputFormat(QPrinter.PdfFormat)
261+
printer.setOutputFileName(FNAME_PDF)
262+
dialog.accept()
263+
ok = True
264+
else:
265+
ok = dialog.exec_()
266+
if ok:
253267
renderer = QwtPlotRenderer()
254268
if QPrinter.GrayScale == printer.colorMode():
255269
renderer.setDiscardFlag(QwtPlotRenderer.DiscardBackground)
@@ -280,6 +294,8 @@ def selected(self, _):
280294
def test_bodedemo():
281295
"""Bode demo"""
282296
utils.test_widget(BodeDemo, (640, 480))
297+
if os.path.isfile(FNAME_PDF):
298+
os.remove(FNAME_PDF)
283299

284300

285301
if __name__ == "__main__":

qwt/tests/test_simple.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88

99
SHOW = True # Show test in GUI-based test launcher
1010

11-
import numpy as np
11+
import os
1212

13-
from qtpy.QtCore import Qt
13+
import numpy as np
14+
from qtpy.QtCore import Qt, QTimer
1415

1516
import qwt
1617
from qwt.tests import utils
1718

19+
FNAMES = ("test_simple.svg", "test_simple.pdf", "test_simple.png")
20+
1821

1922
class SimplePlot(qwt.QwtPlot):
2023
def __init__(self):
@@ -52,10 +55,20 @@ def __init__(self):
5255
plot=self,
5356
)
5457

58+
if utils.TestEnvironment().unattended:
59+
QTimer.singleShot(0, self.export_to_different_formats)
60+
61+
def export_to_different_formats(self):
62+
for fname in FNAMES:
63+
self.exportTo(fname)
64+
5565

5666
def test_simple():
5767
"""Simple plot example"""
5868
utils.test_widget(SimplePlot, size=(600, 400))
69+
for fname in FNAMES:
70+
if os.path.isfile(fname):
71+
os.remove(fname)
5972

6073

6174
if __name__ == "__main__":

0 commit comments

Comments
 (0)