]> sigrok.org Git - sigrok-meter.git/commitdiff
Use different colors for individual channels.
authorJens Steinhauser <redacted>
Thu, 1 Oct 2015 20:41:19 +0000 (22:41 +0200)
committerJens Steinhauser <redacted>
Sun, 4 Oct 2015 03:23:41 +0000 (05:23 +0200)
datamodel.py
mainwindow.py

index d6b1443f5e9962f2cb0811eebca3045d3a780d20..8132e40cc2287bec123a02a2e18a2ad01ede2920 100644 (file)
 ##
 
 import collections
+import itertools
 import qtcompat
 import sigrok.core as sr
 import time
 import util
 
+try:
+    from itertools import izip
+except ImportError:
+    izip = zip
+
 QtCore = qtcompat.QtCore
 QtGui = qtcompat.QtGui
 
@@ -39,6 +45,9 @@ class MeasurementDataModel(QtGui.QStandardItemModel):
     '''Role used to store past samples.'''
     samplesRole = QtCore.Qt.UserRole + 3
 
+    '''Role used to store the color to draw the graph of the channel.'''
+    colorRole = QtCore.Qt.UserRole + 4
+
     def __init__(self, parent):
         super(self.__class__, self).__init__(parent)
 
@@ -49,6 +58,32 @@ class MeasurementDataModel(QtGui.QStandardItemModel):
         # Used in 'format_value()' to check against.
         self.inf = float('inf')
 
+        # A generator for the colors of the channels.
+        self._colorgen = self._make_colorgen()
+
+    def _make_colorgen(self):
+        cols = [
+            QtGui.QColor(0x8F, 0x52, 0x02), # brown
+            QtGui.QColor(0xCC, 0x00, 0x00), # red
+            QtGui.QColor(0xF5, 0x79, 0x00), # orange
+            QtGui.QColor(0xED, 0xD4, 0x00), # yellow
+            QtGui.QColor(0x73, 0xD2, 0x16), # green
+            QtGui.QColor(0x34, 0x65, 0xA4), # blue
+            QtGui.QColor(0x75, 0x50, 0x7B)  # violet
+        ]
+
+        def myrepeat(g, n):
+            '''Repeats every element from 'g' 'n' times'.'''
+            for e in g:
+                for f in itertools.repeat(e, n):
+                    yield f
+
+        colorcycle = itertools.cycle(cols)
+        darkness = myrepeat(itertools.count(100, 10), len(cols))
+
+        for c, d in izip(colorcycle, darkness):
+            yield QtGui.QColor(c).darker(d)
+
     def format_mqflags(self, mqflags):
         if sr.QuantityFlag.AC in mqflags:
             return 'AC'
@@ -92,6 +127,7 @@ class MeasurementDataModel(QtGui.QStandardItemModel):
         item.setData(uid, MeasurementDataModel.idRole)
         item.setData(desc, MeasurementDataModel.descRole)
         item.setData(collections.defaultdict(list), MeasurementDataModel.samplesRole)
+        item.setData(next(self._colorgen), MeasurementDataModel.colorRole)
         self.appendRow(item)
         self.sort(0)
         return item
index 3e07ff7b381fcd21b62a61d204ff8b8f21fd8324..19ce4d3ce79061cdcd0a0635f7d074d3c04249b7 100644 (file)
@@ -53,7 +53,7 @@ class MainWindow(QtGui.QMainWindow):
     '''The main window of the application.'''
 
     # Number of seconds that the plots display.
-    BACKLOG = 10
+    BACKLOG = 30
 
     # Update interval of the plots in milliseconds.
     UPDATEINTERVAL = 100
@@ -160,7 +160,12 @@ class MainWindow(QtGui.QMainWindow):
             return self._curves[key]
 
         # create a new curve
-        curve = pyqtgraph.PlotDataItem()
+        curve = pyqtgraph.PlotDataItem(
+            antialias=True,
+            symbolPen=pyqtgraph.mkPen(QtGui.QColor(QtCore.Qt.black)),
+            symbolBrush=pyqtgraph.mkBrush(QtGui.QColor(QtCore.Qt.black)),
+            symbolSize=1
+        )
         plot.view.addItem(curve)
 
         self._curves[key] = curve
@@ -173,14 +178,17 @@ class MainWindow(QtGui.QMainWindow):
             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])
+                self._updatePlot(deviceID, unit, sampledict[unit], color)
 
-    def _updatePlot(self, deviceID, unit, samples):
-        '''Updates the curve of device 'deviceID' and 'unit' with 'samples'.'''
+    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()