]> sigrok.org Git - sigrok-meter.git/blob - multiplotwidget.py
Allow changing the channel color.
[sigrok-meter.git] / multiplotwidget.py
1 ##
2 ## This file is part of the sigrok-meter project.
3 ##
4 ## Copyright (C) 2015 Jens Steinhauser <jens.steinhauser@gmail.com>
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 ##
20
21 import qtcompat
22
23 QtCore = qtcompat.QtCore
24 QtGui = qtcompat.QtGui
25 pyqtgraph = qtcompat.pyqtgraph
26
27 # black foreground on white background
28 pyqtgraph.setConfigOption('background', 'w')
29 pyqtgraph.setConfigOption('foreground', 'k')
30
31 class MultiPlotItem(pyqtgraph.GraphicsWidget):
32
33     class Plot:
34         def __init__(self, view, xaxis, yaxis):
35             self.view = view
36             self.xaxis = xaxis
37             self.yaxis = yaxis
38
39     def __init__(self, parent=None):
40         pyqtgraph.GraphicsWidget.__init__(self, parent)
41
42         self.layout = QtGui.QGraphicsGridLayout()
43         self.layout.setContentsMargins(10, 10, 10, 1)
44         self.layout.setHorizontalSpacing(0)
45         self.layout.setVerticalSpacing(0)
46         self.setLayout(self.layout)
47
48         self._plots = []
49
50         for i in range(2):
51             self.layout.setColumnPreferredWidth(i, 0)
52             self.layout.setColumnMinimumWidth(i, 0)
53             self.layout.setColumnSpacing(i, 0)
54
55         self.layout.setColumnStretchFactor(0, 0)
56         self.layout.setColumnStretchFactor(1, 100)
57
58     def addPlot(self):
59         row = self.layout.rowCount()
60
61         view = pyqtgraph.ViewBox(parent=self)
62         if self._plots:
63             view.setXLink(self._plots[-1].view)
64         self.layout.addItem(view, row, 1)
65
66         yaxis = pyqtgraph.AxisItem(parent=self, orientation='left')
67         yaxis.linkToView(view)
68         yaxis.setGrid(255)
69         self.layout.addItem(yaxis, row, 0, QtCore.Qt.AlignRight)
70
71         xaxis = pyqtgraph.AxisItem(parent=self, orientation='bottom')
72         xaxis.linkToView(view)
73         xaxis.setGrid(255)
74         self.layout.addItem(xaxis, row + 1, 1)
75
76         for i in range(row, row + 2):
77             self.layout.setRowPreferredHeight(i, 0)
78             self.layout.setRowMinimumHeight(i, 0)
79             self.layout.setRowSpacing(i, 0)
80
81         self.layout.setRowStretchFactor(row,     100)
82         self.layout.setRowStretchFactor(row + 1,   0)
83
84         p = MultiPlotItem.Plot(view, xaxis, yaxis)
85         self._plots.append(p)
86         return p
87
88 class MultiPlotWidget(pyqtgraph.GraphicsView):
89     '''Widget that aligns multiple plots on top of each other.
90
91     (The built in classes fail at doing this correctly when the axis grow,
92     just try zooming in the "GraphicsLayout" or the "Linked View" examples.)'''
93
94     def __init__(self, parent=None):
95         pyqtgraph.GraphicsView.__init__(self, parent)
96
97         self.multiPlotItem = MultiPlotItem()
98         self.setCentralItem(self.multiPlotItem)
99
100         for m in [
101             'addPlot'
102         ]:
103             setattr(self, m, getattr(self.multiPlotItem, m))