]> sigrok.org Git - sigrok-meter.git/blobdiff - mainwindow.py
Make it possible to hide plots.
[sigrok-meter.git] / mainwindow.py
index 19ce4d3ce79061cdcd0a0635f7d074d3c04249b7..8ac3b3de142f7fa828a0c7fdbf18a874b28c0097 100644 (file)
@@ -114,6 +114,7 @@ class MainWindow(QtGui.QMainWindow):
         self.listView.setMinimumSize(self.delegate.sizeHint())
 
         self.plotwidget = multiplotwidget.MultiPlotWidget(self)
+        self.plotwidget.plotHidden.connect(self._on_plotHidden)
 
         # Maps from 'unit' to the corresponding plot.
         self._plots = {}
@@ -174,33 +175,57 @@ class MainWindow(QtGui.QMainWindow):
     def timerEvent(self, event):
         '''Periodically updates all graphs.'''
 
-        for row in range(self.model.rowCount()):
-            idx = self.model.index(row, 0)
-            deviceID = self.model.data(idx, datamodel.MeasurementDataModel.idRole)
-            sampledict = self.model.data(idx, datamodel.MeasurementDataModel.samplesRole)
-            color = self.model.data(idx, datamodel.MeasurementDataModel.colorRole)
-            for unit in sampledict:
-                self._updatePlot(deviceID, unit, sampledict[unit], color)
-
-    def _updatePlot(self, deviceID, unit, samples, color):
-        '''Updates the curve of device 'deviceID' and 'unit' with 'samples',
-        changes the color of the curve to 'color'.'''
-
-        plot = self._getPlot(unit)
-        curve = self._getCurve(plot, deviceID)
-        curve.setPen(pyqtgraph.mkPen(color=color, width=1))
-
-        now = time.time()
+        self._updatePlots()
 
-        # remove old samples
-        l = now - MainWindow.BACKLOG
-        while samples and samples[0][0] < l:
-            samples.pop(0)
+    def _updatePlots(self):
+        '''Updates all plots.'''
 
-        xdata = [s[0] - now for s in samples]
-        ydata = [s[1]       for s in samples]
+        # loop over all devices and channels
+        for row in range(self.model.rowCount()):
+            idx = self.model.index(row, 0)
+            deviceID = self.model.data(idx,
+                            datamodel.MeasurementDataModel.idRole)
+            traces = self.model.data(idx,
+                            datamodel.MeasurementDataModel.tracesRole)
+
+            for unit, trace in traces.items():
+                now = time.time()
+
+                # remove old samples
+                l = now - MainWindow.BACKLOG
+                while trace.samples and trace.samples[0][0] < l:
+                    trace.samples.pop(0)
+
+                plot = self._getPlot(unit)
+                if not plot.visible:
+                    if trace.new:
+                        self.plotwidget.showPlot(plot)
+
+                if plot.visible:
+                    xdata = [s[0] - now for s in trace.samples]
+                    ydata = [s[1]       for s in trace.samples]
+
+                    color = self.model.data(idx,
+                                datamodel.MeasurementDataModel.colorRole)
+
+                    curve = self._getCurve(plot, deviceID)
+                    curve.setPen(pyqtgraph.mkPen(color=color))
+                    curve.setData(xdata, ydata)
+
+    @QtCore.Slot(multiplotwidget.Plot)
+    def _on_plotHidden(self, plot):
+        plotunit = [u for u, p in self._plots.items() if p == plot][0]
+
+        # Mark all traces of all devices/channels with the same unit as the
+        # plot as "old" ('trace.new = False'). As soon as a new sample arrives
+        # on one trace, the plot will be shown again
+        for row in range(self.model.rowCount()):
+            idx = self.model.index(row, 0)
+            traces = self.model.data(idx, datamodel.MeasurementDataModel.tracesRole)
 
-        curve.setData(xdata, ydata)
+            for traceunit, trace in traces.items():
+                if traceunit == plotunit:
+                    trace.new = False
 
     def closeEvent(self, event):
         self.thread.stop()