|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Licensed under the terms of the MIT License |
| 4 | +# Copyright (c) 2015 Pierre Raybaut |
| 5 | +# (see LICENSE file for more details) |
| 6 | + |
| 7 | +"""Curve benchmark example""" |
| 8 | + |
| 9 | +SHOW = True # Show test in GUI-based test launcher |
| 10 | + |
| 11 | +import time |
| 12 | +import numpy as np |
| 13 | + |
| 14 | +from qwt.qt.QtGui import (QApplication, QPen, QBrush, QMainWindow, QGridLayout, |
| 15 | + QTabWidget, QWidget, QTextEdit, QLineEdit, QFont, |
| 16 | + QFontDatabase) |
| 17 | +from qwt.qt.QtCore import QSize |
| 18 | +from qwt.qt.QtCore import Qt |
| 19 | +from qwt import QwtPlot, QwtSymbol, QwtPlotCurve |
| 20 | + |
| 21 | + |
| 22 | +COLOR_INDEX = None |
| 23 | + |
| 24 | +def get_curve_color(): |
| 25 | + global COLOR_INDEX |
| 26 | + colors = (Qt.blue, Qt.red, Qt.green, Qt.yellow, Qt.magenta, Qt.cyan) |
| 27 | + if COLOR_INDEX is None: |
| 28 | + COLOR_INDEX = 0 |
| 29 | + else: |
| 30 | + COLOR_INDEX = (COLOR_INDEX + 1) % len(colors) |
| 31 | + return colors[COLOR_INDEX] |
| 32 | + |
| 33 | + |
| 34 | +class BMPlot(QwtPlot): |
| 35 | + def __init__(self, title, xdata, ydata, style, symbol=None, *args): |
| 36 | + super(BMPlot, self).__init__(*args) |
| 37 | + self.setMinimumSize(200, 200) |
| 38 | + self.setTitle(title) |
| 39 | + self.setAxisTitle(QwtPlot.xBottom, 'x') |
| 40 | + self.setAxisTitle(QwtPlot.yLeft, 'y') |
| 41 | + curve = QwtPlotCurve() |
| 42 | + curve.setPen(QPen(get_curve_color())) |
| 43 | + curve.setStyle(style) |
| 44 | + curve.setRenderHint(QwtPlotCurve.RenderAntialiased) |
| 45 | + if symbol is not None: |
| 46 | + curve.setSymbol(symbol) |
| 47 | + curve.attach(self) |
| 48 | + curve.setData(xdata, ydata) |
| 49 | + self.replot() |
| 50 | + |
| 51 | + |
| 52 | +class BMWidget(QWidget): |
| 53 | + def __init__(self, points, parent=None): |
| 54 | + super(BMWidget, self).__init__(parent) |
| 55 | + layout = QGridLayout() |
| 56 | + x = np.linspace(.001, 20., points) |
| 57 | + y = (np.sin(x)/x)*np.cos(20*x) |
| 58 | + symb1 = QwtSymbol(QwtSymbol.Ellipse, QBrush(Qt.yellow), |
| 59 | + QPen(Qt.blue), QSize(5, 5)) |
| 60 | + symb2 = QwtSymbol(QwtSymbol.XCross, QBrush(), |
| 61 | + QPen(Qt.darkMagenta), QSize(5, 5)) |
| 62 | + nbcol, col, row = 2, 0, 0 |
| 63 | +# for style, symbol in (('Lines', None), |
| 64 | +# ('Lines', None), |
| 65 | +# ('Lines', None), |
| 66 | +# ('Lines', None)): |
| 67 | + for style, symbol in (('Sticks', symb1), |
| 68 | + ('Lines', None), |
| 69 | + ('Steps', None), |
| 70 | + ('NoCurve', symb2)): |
| 71 | + layout.addWidget(BMPlot(style, x, y, getattr(QwtPlotCurve, style), |
| 72 | + symbol=symbol), row, col) |
| 73 | + col += 1 |
| 74 | + if col >= nbcol: |
| 75 | + row +=1 |
| 76 | + col = 0 |
| 77 | + self.text = QLineEdit() |
| 78 | + self.text.setReadOnly(True) |
| 79 | + self.text.setAlignment(Qt.AlignCenter) |
| 80 | + self.text.setText("Rendering plot...") |
| 81 | + layout.addWidget(self.text, row+1, 0, 1, 2) |
| 82 | + self.setLayout(layout) |
| 83 | + |
| 84 | + |
| 85 | +class BMText(QTextEdit): |
| 86 | + def __init__(self, parent=None): |
| 87 | + super(BMText, self).__init__(parent) |
| 88 | + self.setReadOnly(True) |
| 89 | + self.setText("""\ |
| 90 | +<b>Curve benchmark example:</b><br><br> |
| 91 | +Click on each tab to test if plotting performance is acceptable in terms of |
| 92 | +GUI response time (switch between tabs, resize main windows, ...).<br> |
| 93 | +<br><br> |
| 94 | +<b>Benchmarks results:</b> |
| 95 | +""") |
| 96 | + |
| 97 | + |
| 98 | +class BMDemo(QMainWindow): |
| 99 | + def __init__(self, max_n, parent=None, **kwargs): |
| 100 | + super(BMDemo, self).__init__(parent=parent) |
| 101 | + self.setWindowTitle('Curve benchmark') |
| 102 | + tabs = QTabWidget() |
| 103 | + self.setCentralWidget(tabs) |
| 104 | + contents = BMText() |
| 105 | + tabs.addTab(contents, 'Contents') |
| 106 | + self.resize(1000, 800) |
| 107 | + self.show() |
| 108 | + t0g = time.time() |
| 109 | + for idx in range(3, -1, -1): |
| 110 | + points = max_n/10**idx |
| 111 | + t0 = time.time() |
| 112 | + widget = BMWidget(points) |
| 113 | + title = '%d points' % points |
| 114 | + tabs.addTab(widget, title) |
| 115 | + tabs.setCurrentWidget(widget) |
| 116 | + QApplication.processEvents() |
| 117 | + time_str = "Elapsed time: %d ms" % ((time.time()-t0)*1000) |
| 118 | + widget.text.setText(time_str) |
| 119 | + contents.append("<br><i>%s:</i><br>%s" % (title, time_str)) |
| 120 | + dt = time.time()-t0g |
| 121 | + contents.append("<br><br><u>Total elapsed time</u>: %d ms" % (dt*1000)) |
| 122 | + tabs.setCurrentIndex(0) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == '__main__': |
| 126 | + app = QApplication([]) |
| 127 | + for name in ('Calibri', 'Verdana', 'Arial'): |
| 128 | + if name in QFontDatabase().families(): |
| 129 | + app.setFont(QFont(name)) |
| 130 | + break |
| 131 | + demo = BMDemo(100000) |
| 132 | + app.exec_() |
0 commit comments