X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=datamodel.py;h=a90cbc7ab40c6de02d9c9870388e7060f847c17b;hb=4946c320ea47806eb336f045c1b936df4d793683;hp=5ad084cc1001d60667d44f243b9debeefa58a0c4;hpb=86862cb44ace09720744eee2e80ae50c174e903f;p=sigrok-meter.git diff --git a/datamodel.py b/datamodel.py index 5ad084c..a90cbc7 100644 --- a/datamodel.py +++ b/datamodel.py @@ -18,8 +18,8 @@ ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ## -import collections import itertools +import math import qtcompat import sigrok.core as sr import util @@ -65,9 +65,6 @@ 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() @@ -103,7 +100,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) @@ -136,7 +133,7 @@ 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) @@ -160,9 +157,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 = (timestamp, 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.''' @@ -221,9 +233,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