]> sigrok.org Git - sigrok-meter.git/blobdiff - datamodel.py
doc: update IRC reference to Libera.Chat
[sigrok-meter.git] / datamodel.py
index 53c70bc46bc635c3f47102b13c6598bdbfcf7b9e..828374e823cb63155f7d0d83215a69138fded421 100644 (file)
 ## GNU General Public License for more details.
 ##
 ## You should have received a copy of the GNU General Public License
-## along with this program; if not, write to the Free Software
-## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+## along with this program; if not, see <http://www.gnu.org/licenses/>.
 ##
 
-import collections
 import itertools
+import math
 import qtcompat
 import sigrok.core as sr
-import time
 import util
 
 try:
@@ -53,7 +51,7 @@ class MeasurementDataModel(QtGui.QStandardItemModel):
     '''Role used to store the device vendor and model.'''
     descRole = QtCore.Qt.UserRole + 2
 
-    '''Role used to store a dictionary with the traces'''
+    '''Role used to store a dictionary with the traces.'''
     tracesRole = QtCore.Qt.UserRole + 3
 
     '''Role used to store the color to draw the graph of the channel.'''
@@ -66,20 +64,17 @@ class MeasurementDataModel(QtGui.QStandardItemModel):
         # idRole holds tuples, and using them to sort doesn't work.
         self.setSortRole(MeasurementDataModel.descRole)
 
-        # 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(0x73, 0xD2, 0x16), # green
             QtGui.QColor(0xCC, 0x00, 0x00), # red
+            QtGui.QColor(0x34, 0x65, 0xA4), # blue
             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
         ]
 
@@ -104,7 +99,7 @@ class MeasurementDataModel(QtGui.QStandardItemModel):
             return ''
 
     def format_value(self, mag):
-        if mag == self.inf:
+        if math.isinf(mag):
             return u'\u221E'
         return '{:f}'.format(mag)
 
@@ -137,14 +132,14 @@ class MeasurementDataModel(QtGui.QStandardItemModel):
         item = QtGui.QStandardItem()
         item.setData(uid, MeasurementDataModel.idRole)
         item.setData(desc, MeasurementDataModel.descRole)
-        item.setData(collections.defaultdict(Trace), MeasurementDataModel.tracesRole)
+        item.setData({}, MeasurementDataModel.tracesRole)
         item.setData(next(self._colorgen), MeasurementDataModel.colorRole)
         self.appendRow(item)
         self.sort(0)
         return item
 
-    @QtCore.Slot(object, object, object)
-    def update(self, device, channel, data):
+    @QtCore.Slot(float, sr.classes.Device, sr.classes.Channel, tuple)
+    def update(self, timestamp, device, channel, data):
         '''Update the data for the device (+channel) with the most recent
         measurement from the given payload.'''
 
@@ -161,9 +156,24 @@ class MeasurementDataModel(QtGui.QStandardItemModel):
 
         # The samples role is a dictionary that contains the old samples for each unit.
         # Should be trimmed periodically, otherwise it grows larger and larger.
-        sample = (time.time(), value)
-        traces = item.data(MeasurementDataModel.tracesRole)
-        traces[unit].append(sample)
+        if not math.isinf(value) and not math.isnan(value):
+            sample = (timestamp, value)
+            traces = item.data(MeasurementDataModel.tracesRole)
+
+            # It's not possible to use 'collections.defaultdict' here, because
+            # PySide doesn't return the original type that was passed in.
+            if not (unit in traces):
+                traces[unit] = Trace()
+            traces[unit].append(sample)
+
+            item.setData(traces, MeasurementDataModel.tracesRole)
+
+    def clear_samples(self):
+        '''Removes all old samples from the model.'''
+        for row in range(self.rowCount()):
+            idx = self.index(row, 0)
+            self.setData(idx, {},
+                MeasurementDataModel.tracesRole)
 
 class MultimeterDelegate(QtGui.QStyledItemDelegate):
     '''Delegate to show the data items from a MeasurementDataModel.'''
@@ -222,9 +232,11 @@ class MultimeterDelegate(QtGui.QStyledItemDelegate):
                     c = index.data(MeasurementDataModel.colorRole)
                     c = QtGui.QColorDialog.getColor(c, None,
                         'Choose new color for channel')
-
-                    item = model.itemFromIndex(index)
-                    item.setData(c, MeasurementDataModel.colorRole)
+                    if c.isValid():
+                        # False if cancel is pressed (resulting in a black
+                        # color).
+                        item = model.itemFromIndex(index)
+                        item.setData(c, MeasurementDataModel.colorRole)
 
                     return True