|
| 1 | +#!/usr/bin/env python |
| 2 | +# /*########################################################################## |
| 3 | +# |
| 4 | +# Copyright (c) 2016-2019 European Synchrotron Radiation Facility |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in |
| 14 | +# all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +# THE SOFTWARE. |
| 23 | +# |
| 24 | +# ###########################################################################*/ |
| 25 | +""" |
| 26 | +Example to show the use of :class:`~silx.gui.items.group.Group`. |
| 27 | +""" |
| 28 | + |
| 29 | +__license__ = "MIT" |
| 30 | + |
| 31 | +import logging |
| 32 | +import numpy |
| 33 | +from silx.gui.plot import PlotWindow |
| 34 | +from silx.gui import qt |
| 35 | +from silx.gui.plot.items.group import Group |
| 36 | +from silx.gui.plot.items.curve import Curve |
| 37 | + |
| 38 | + |
| 39 | +logging.basicConfig() |
| 40 | +logger = logging.getLogger(__name__) |
| 41 | + |
| 42 | + |
| 43 | +class MyGroup(Group): |
| 44 | + |
| 45 | + def __init__(self): |
| 46 | + super(MyGroup, self).__init__() |
| 47 | + self._cursor = 0 |
| 48 | + self._tick = 0 |
| 49 | + |
| 50 | + def update(self): |
| 51 | + x = numpy.linspace(self._cursor, self._cursor + 100, 50) |
| 52 | + y = numpy.random.poisson((numpy.sin(x / 100) + 1) * 100) |
| 53 | + self._cursor += 100 |
| 54 | + self._tick += 1 |
| 55 | + |
| 56 | + curve = Curve() |
| 57 | + curve.setColor("C0") |
| 58 | + curve.setSymbol("") |
| 59 | + curve.setLineStyle("-") |
| 60 | + curve.setData(x, y, copy=False) |
| 61 | + curve.setVisible(self._tick % 7 != 0) |
| 62 | + self.addItem(curve) |
| 63 | + |
| 64 | + if len(self.getItems()) > 10: |
| 65 | + old = self.getItems()[0] |
| 66 | + self.removeItem(old) |
| 67 | + |
| 68 | + |
| 69 | +def main(argv=None): |
| 70 | + global app # QApplication must be global to avoid seg fault on quit |
| 71 | + app = qt.QApplication([]) |
| 72 | + sys.excepthook = qt.exceptionHandler |
| 73 | + |
| 74 | + win = qt.QWidget() |
| 75 | + win.setAttribute(qt.Qt.WA_DeleteOnClose) |
| 76 | + layout = qt.QVBoxLayout(win) |
| 77 | + |
| 78 | + plot = PlotWindow() |
| 79 | + layout.addWidget(plot) |
| 80 | + |
| 81 | + item = MyGroup() |
| 82 | + plot.addItem(item) |
| 83 | + |
| 84 | + def update(): |
| 85 | + item.update() |
| 86 | + plot.resetZoom() |
| 87 | + |
| 88 | + t = qt.QTimer() |
| 89 | + t.timeout.connect(update) |
| 90 | + t.start(1000) |
| 91 | + |
| 92 | + show = qt.QPushButton() |
| 93 | + show.setText("Show") |
| 94 | + layout.addWidget(show) |
| 95 | + show.clicked.connect(lambda: item.setVisible(True)) |
| 96 | + |
| 97 | + hide = qt.QPushButton() |
| 98 | + hide.setText("Hide") |
| 99 | + layout.addWidget(hide) |
| 100 | + hide.clicked.connect(lambda: item.setVisible(False)) |
| 101 | + |
| 102 | + win.show() |
| 103 | + return app.exec() |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + import sys |
| 108 | + sys.exit(main(argv=sys.argv[1:])) |
0 commit comments