Skip to content

Commit e50850a

Browse files
author
Pierre Raybaut
committed
Added 'print_' and 'exportTo' (png, pdf, ...) methods to QwtPlot + implemented QwtPlotRenderer
1 parent 73197b6 commit e50850a

File tree

12 files changed

+577
-242
lines changed

12 files changed

+577
-242
lines changed

examples/BodeDemo.py

Lines changed: 75 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -104,39 +104,6 @@
104104
'...########################..##.']
105105

106106

107-
108-
class PrintFilter(QwtPlotPrintFilter):
109-
def __init__(self):
110-
QwtPlotPrintFilter.__init__(self)
111-
112-
# __init___()
113-
114-
def color(self, c, item):
115-
if not (self.options() & QwtPlotPrintFilter.CanvasBackground):
116-
if item == QwtPlotPrintFilter.MajorGrid:
117-
return Qt.darkGray
118-
elif item == QwtPlotPrintFilter.MinorGrid:
119-
return Qt.gray
120-
if item == QwtPlotPrintFilter.Title:
121-
return Qt.red
122-
elif item == QwtPlotPrintFilter.AxisScale:
123-
return Qt.green
124-
elif item == QwtPlotPrintFilter.AxisTitle:
125-
return Qt.blue
126-
return c
127-
128-
# color()
129-
130-
def font(self, f, _):
131-
result = QFont(f)
132-
result.setPointSize(int(f.pointSize()*1.25))
133-
return result
134-
135-
# font()
136-
137-
# class PrintFilter
138-
139-
140107
class BodePlot(QwtPlot):
141108

142109
def __init__(self, *args):
@@ -148,14 +115,11 @@ def __init__(self, *args):
148115
# legend
149116
legend = QwtLegend()
150117
legend.setFrameStyle(QFrame.Box | QFrame.Sunken)
151-
legend.setItemMode(QwtLegend.ClickableItem)
152118
self.insertLegend(legend, QwtPlot.BottomLegend)
153119

154120
# grid
155121
self.grid = QwtPlotGrid()
156122
self.grid.enableXMin(True)
157-
self.grid.setMajPen(QPen(Qt.white, 0, Qt.DotLine))
158-
self.grid.setMinPen(QPen(Qt.gray, 0 , Qt.DotLine))
159123
self.grid.attach(self)
160124

161125
# axes
@@ -166,7 +130,7 @@ def __init__(self, *args):
166130

167131
self.setAxisMaxMajor(QwtPlot.xBottom, 6)
168132
self.setAxisMaxMinor(QwtPlot.xBottom, 10)
169-
self.setAxisScaleEngine(QwtPlot.xBottom, QwtLog10ScaleEngine())
133+
self.setAxisScaleEngine(QwtPlot.xBottom, QwtLogScaleEngine())
170134

171135
# curves
172136
self.curve1 = QwtPlotCurve('Amplitude')
@@ -224,7 +188,7 @@ def __init__(self, *args):
224188
text.setFont(QFont(fn, 12, QFont.Bold))
225189
text.setColor(Qt.blue)
226190
text.setBackgroundBrush(QBrush(Qt.yellow))
227-
text.setBackgroundPen(QPen(Qt.red, 2))
191+
text.setBorderPen(QPen(Qt.red, 2))
228192
m.setLabel(text)
229193
m.attach(self)
230194

@@ -284,38 +248,38 @@ def __init__(self, *args):
284248
QMainWindow.__init__(self, *args)
285249

286250
self.plot = BodePlot(self)
287-
self.plot.setMargin(5)
251+
self.plot.setContentsMargins(5, 5, 5, 0)
288252

289253
self.setContextMenuPolicy(Qt.NoContextMenu)
290254

291-
self.zoomers = []
292-
zoomer = QwtPlotZoomer(
293-
QwtPlot.xBottom,
294-
QwtPlot.yLeft,
295-
QwtPicker.DragSelection,
296-
QwtPicker.AlwaysOff,
297-
self.plot.canvas())
298-
zoomer.setRubberBandPen(QPen(Qt.green))
299-
self.zoomers.append(zoomer)
300-
301-
zoomer = QwtPlotZoomer(
302-
QwtPlot.xTop,
303-
QwtPlot.yRight,
304-
QwtPicker.PointSelection | QwtPicker.DragSelection,
305-
QwtPicker.AlwaysOff,
306-
self.plot.canvas())
307-
zoomer.setRubberBand(QwtPicker.NoRubberBand)
308-
self.zoomers.append(zoomer)
309-
310-
self.picker = QwtPlotPicker(
311-
QwtPlot.xBottom,
312-
QwtPlot.yLeft,
313-
QwtPicker.PointSelection | QwtPicker.DragSelection,
314-
QwtPlotPicker.CrossRubberBand,
315-
QwtPicker.AlwaysOn,
316-
self.plot.canvas())
317-
self.picker.setRubberBandPen(QPen(Qt.green))
318-
self.picker.setTrackerPen(QPen(Qt.cyan))
255+
# self.zoomers = []
256+
# zoomer = QwtPlotZoomer(
257+
# QwtPlot.xBottom,
258+
# QwtPlot.yLeft,
259+
# QwtPicker.DragSelection,
260+
# QwtPicker.AlwaysOff,
261+
# self.plot.canvas())
262+
# zoomer.setRubberBandPen(QPen(Qt.green))
263+
# self.zoomers.append(zoomer)
264+
#
265+
# zoomer = QwtPlotZoomer(
266+
# QwtPlot.xTop,
267+
# QwtPlot.yRight,
268+
# QwtPicker.PointSelection | QwtPicker.DragSelection,
269+
# QwtPicker.AlwaysOff,
270+
# self.plot.canvas())
271+
# zoomer.setRubberBand(QwtPicker.NoRubberBand)
272+
# self.zoomers.append(zoomer)
273+
274+
# self.picker = QwtPlotPicker(
275+
# QwtPlot.xBottom,
276+
# QwtPlot.yLeft,
277+
# QwtPicker.PointSelection | QwtPicker.DragSelection,
278+
# QwtPlotPicker.CrossRubberBand,
279+
# QwtPicker.AlwaysOn,
280+
# self.plot.canvas())
281+
# self.picker.setRubberBandPen(QPen(Qt.green))
282+
# self.picker.setTrackerPen(QPen(Qt.cyan))
319283

320284
self.setCentralWidget(self.plot)
321285

@@ -336,21 +300,12 @@ def __init__(self, *args):
336300
toolBar.addWidget(btnPrint)
337301
self.connect(btnPrint, SIGNAL('clicked()'), self.print_)
338302

339-
if QT_VERSION >= 0X040100:
340-
btnPDF = QToolButton(toolBar)
341-
btnPDF.setText("PDF")
342-
btnPDF.setIcon(QIcon(QPixmap(print_xpm)))
343-
btnPDF.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
344-
toolBar.addWidget(btnPDF)
345-
self.connect(btnPDF, SIGNAL('clicked()'), self.exportPDF)
346-
347-
if QT_VERSION >= 0x040300:
348-
btnSVG = QToolButton(toolBar)
349-
btnSVG.setText("SVG")
350-
btnSVG.setIcon(QIcon(QPixmap(print_xpm)))
351-
btnSVG.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
352-
toolBar.addWidget(btnSVG)
353-
self.connect(btnSVG, SIGNAL('clicked()'), self.exportSVG)
303+
btnExport = QToolButton(toolBar)
304+
btnExport.setText("Export")
305+
btnExport.setIcon(QIcon(QPixmap(print_xpm)))
306+
btnExport.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
307+
toolBar.addWidget(btnExport)
308+
self.connect(btnExport, SIGNAL('clicked()'), self.exportDocument)
354309

355310
toolBar.addSeparator()
356311

@@ -361,10 +316,10 @@ def __init__(self, *args):
361316
dampLayout.addWidget(QLabel("Damping Factor", dampBox), 0)
362317
dampLayout.addSpacing(10)
363318

364-
self.cntDamp = QwtCounter(dampBox)
365-
self.cntDamp.setRange(0.01, 5.0, 0.01)
366-
self.cntDamp.setValue(0.01)
367-
dampLayout.addWidget(self.cntDamp, 10)
319+
# self.cntDamp = QwtCounter(dampBox)
320+
# self.cntDamp.setRange(0.01, 5.0, 0.01)
321+
# self.cntDamp.setValue(0.01)
322+
# dampLayout.addWidget(self.cntDamp, 10)
368323

369324
toolBar.addWidget(dampBox)
370325

@@ -373,26 +328,24 @@ def __init__(self, *args):
373328
self.zoom(False)
374329
self.showInfo()
375330

376-
self.connect(self.cntDamp,
377-
SIGNAL('valueChanged(double)'),
378-
self.plot.setDamp)
331+
# self.connect(self.cntDamp,
332+
# SIGNAL('valueChanged(double)'),
333+
# self.plot.setDamp)
379334
self.connect(btnZoom,
380335
SIGNAL('toggled(bool)'),
381336
self.zoom)
382-
self.connect(self.picker,
383-
SIGNAL('moved(const QPoint &)'),
384-
self.moved)
385-
self.connect(self.picker,
386-
SIGNAL('selected(const QPolygon &)'),
387-
self.selected)
337+
# self.connect(self.picker,
338+
# SIGNAL('moved(const QPoint &)'),
339+
# self.moved)
340+
# self.connect(self.picker,
341+
# SIGNAL('selected(const QPolygon &)'),
342+
# self.selected)
388343

389344
# __init__()
390345

391346
def print_(self):
392347
printer = QPrinter(QPrinter.HighResolution)
393348

394-
printer.setOutputFileName('bode-example-%s.ps' % qVersion())
395-
396349
printer.setCreator('Bode example')
397350
printer.setOrientation(QPrinter.Landscape)
398351
printer.setColorMode(QPrinter.Color)
@@ -404,72 +357,39 @@ def print_(self):
404357

405358
dialog = QPrintDialog(printer)
406359
if dialog.exec_():
407-
printFilter = PrintFilter()
360+
renderer = QwtPlotRenderer()
408361
if (QPrinter.GrayScale == printer.colorMode()):
409-
printFilter.setOptions(
410-
QwtPlotPrintFilter.PrintAll
411-
& ~QwtPlotPrintFilter.PrintBackground
412-
| QwtPlotPrintFilter.PrintFrameWithScales)
413-
self.plot.print_(printer, printFilter)
362+
renderer.setDiscardFlag(QwtPlotRenderer.DiscardBackground)
363+
renderer.setDiscardFlag(QwtPlotRenderer.DiscardCanvasBackground)
364+
renderer.setDiscardFlag(QwtPlotRenderer.DiscardCanvasFrame)
365+
renderer.setLayoutFlag(QwtPlotRenderer.FrameWithScales)
366+
renderer.renderTo(self.plot, printer)
414367

415-
# print_()
416-
417-
def exportPDF(self):
418-
if QT_VERSION > 0x040100:
419-
fileName = QFileDialog.getSaveFileName(
420-
self,
421-
'Export File Name',
422-
'bode-example-%s.pdf' % qVersion(),
423-
'PDF Documents (*.pdf)')
424-
425-
if not fileName.isEmpty():
426-
printer = QPrinter()
427-
printer.setOutputFormat(QPrinter.PdfFormat)
428-
printer.setOrientation(QPrinter.Landscape)
429-
printer.setOutputFileName(fileName)
430-
431-
printer.setCreator('Bode example')
432-
self.plot.print_(printer)
433-
434-
# exportPDF()
435-
436-
def exportSVG(self):
437-
if QT_VERSION >= 0x040300:
438-
fileName = QFileDialog.getSaveFileName(
439-
self,
440-
'Export File Name',
441-
'bode-example-%s.svg' % qVersion(),
442-
'SVG Documents (*.svg)')
443-
if not fileName.isEmpty():
444-
generator = QSvgGenerator()
445-
generator.setFileName(fileName)
446-
generator.setSize(QSize(800, 600))
447-
self.plot.print_(generator)
448-
449-
# exportSVG()
368+
def exportDocument(self):
369+
self.plot.exportTo("bode.pdf")
450370

451371
def zoom(self, on):
452-
self.zoomers[0].setEnabled(on)
453-
self.zoomers[0].zoom(0)
454-
455-
self.zoomers[1].setEnabled(on)
456-
self.zoomers[1].zoom(0)
372+
# self.zoomers[0].setEnabled(on)
373+
# self.zoomers[0].zoom(0)
374+
#
375+
# self.zoomers[1].setEnabled(on)
376+
# self.zoomers[1].zoom(0)
457377

458-
if on:
459-
self.picker.setRubberBand(Qwt.QwtPicker.NoRubberBand)
460-
else:
461-
self.picker.setRubberBand(Qwt.QwtPicker.CrossRubberBand)
378+
# if on:
379+
# self.picker.setRubberBand(Qwt.QwtPicker.NoRubberBand)
380+
# else:
381+
# self.picker.setRubberBand(Qwt.QwtPicker.CrossRubberBand)
462382

463383
self.showInfo()
464384

465385
# zoom()
466386

467-
def showInfo(self, text=None):
468-
if not text:
469-
if self.picker.rubberBand():
470-
text = 'Cursor Pos: Press left mouse button in plot region'
471-
else:
472-
text = 'Zoom: Press mouse button and drag'
387+
def showInfo(self, text=""):
388+
# if not text:
389+
# if self.picker.rubberBand():
390+
# text = 'Cursor Pos: Press left mouse button in plot region'
391+
# else:
392+
# text = 'Zoom: Press mouse button and drag'
473393

474394
self.statusBar().showMessage(text)
475395

@@ -489,40 +409,19 @@ def selected(self, _):
489409

490410
# selected()
491411

492-
# class BodeDemo
493-
494-
495412
def make():
496413
demo = BodeDemo()
497414
demo.resize(540, 400)
498415
demo.show()
499416
return demo
500417

501-
# make()
502418

503-
504-
def main(args):
505-
app = QApplication(args)
419+
if __name__ == '__main__':
420+
app = QApplication(sys.argv)
506421
fonts = QFontDatabase()
507422
for name in ('Verdana', 'STIXGeneral'):
508423
if name in fonts.families():
509424
app.setFont(QFont(name))
510425
break
511426
demo = make()
512427
sys.exit(app.exec_())
513-
514-
# main()
515-
516-
517-
# Admire!
518-
if __name__ == '__main__':
519-
if 'settracemask' in sys.argv:
520-
# for debugging, requires: python configure.py --trace ...
521-
import sip
522-
sip.settracemask(0x3f)
523-
524-
main(sys.argv)
525-
526-
# Local Variables: ***
527-
# mode: python ***
528-
# End: ***

examples/ImagePlotDemo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def __init__(self, *args):
137137
self.setAxisTitle(Qwt.QwtPlot.yLeft, 'frequency (Hz)')
138138

139139
colorMap = Qwt.QwtLinearColorMap(Qt.Qt.blue, Qt.Qt.red)
140-
interval = Qwt.QwtDoubleInterval(-1, 1)
140+
interval = Qwt.QwtInterval(-1, 1)
141141
self.enableAxis(Qwt.QwtPlot.yRight)
142142
self.setAxisScale(Qwt.QwtPlot.yRight, -1, 1)
143143
self.axisWidget(Qwt.QwtPlot.yRight).setColorBarEnabled(True)

0 commit comments

Comments
 (0)