X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=datamodel.py;h=d6b1443f5e9962f2cb0811eebca3045d3a780d20;hb=f76b9df824276da861bbbdde658deb8130ea8541;hp=a7757e62dadebc76c6e90395e3a20623f7381fe2;hpb=480cdb7bc04548aec46b7342d6fa1acafa60331c;p=sigrok-meter.git diff --git a/datamodel.py b/datamodel.py index a7757e6..d6b1443 100644 --- a/datamodel.py +++ b/datamodel.py @@ -18,8 +18,11 @@ ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ## +import collections import qtcompat import sigrok.core as sr +import time +import util QtCore = qtcompat.QtCore QtGui = qtcompat.QtGui @@ -28,53 +31,24 @@ class MeasurementDataModel(QtGui.QStandardItemModel): '''Model to hold the measured values.''' '''Role used to identify and find the item.''' - _idRole = QtCore.Qt.UserRole + 1 + idRole = QtCore.Qt.UserRole + 1 '''Role used to store the device vendor and model.''' descRole = QtCore.Qt.UserRole + 2 + '''Role used to store past samples.''' + samplesRole = QtCore.Qt.UserRole + 3 + def __init__(self, parent): super(self.__class__, self).__init__(parent) # Use the description text to sort the items for now, because the - # _idRole holds tuples, and using them to sort doesn't work. + # 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') - def format_unit(self, u): - units = { - sr.Unit.VOLT: 'V', - sr.Unit.AMPERE: 'A', - sr.Unit.OHM: u'\u03A9', - sr.Unit.FARAD: 'F', - sr.Unit.KELVIN: 'K', - sr.Unit.CELSIUS: u'\u00B0C', - sr.Unit.FAHRENHEIT: u'\u00B0F', - sr.Unit.HERTZ: 'Hz', - sr.Unit.PERCENTAGE: '%', - # sr.Unit.BOOLEAN - sr.Unit.SECOND: 's', - sr.Unit.SIEMENS: 'S', - sr.Unit.DECIBEL_MW: 'dBu', - sr.Unit.DECIBEL_VOLT: 'dBV', - # sr.Unit.UNITLESS - sr.Unit.DECIBEL_SPL: 'dB', - # sr.Unit.CONCENTRATION - sr.Unit.REVOLUTIONS_PER_MINUTE: 'rpm', - sr.Unit.VOLT_AMPERE: 'VA', - sr.Unit.WATT: 'W', - sr.Unit.WATT_HOUR: 'Wh', - sr.Unit.METER_SECOND: 'm/s', - sr.Unit.HECTOPASCAL: 'hPa', - sr.Unit.HUMIDITY_293K: '%rF', - sr.Unit.DEGREE: u'\u00B0', - sr.Unit.HENRY: 'H' - } - - return units.get(u, '') - def format_mqflags(self, mqflags): if sr.QuantityFlag.AC in mqflags: return 'AC' @@ -105,18 +79,19 @@ class MeasurementDataModel(QtGui.QStandardItemModel): # Find the correct item in the model. for row in range(self.rowCount()): item = self.item(row) - rid = item.data(MeasurementDataModel._idRole) + rid = item.data(MeasurementDataModel.idRole) rid = tuple(rid) # PySide returns a list. if uid == rid: return item # Nothing found, create a new item. - desc = '{} {}, channel "{}"'.format( + desc = '{} {}, {}'.format( device.vendor, device.model, channel.name) item = QtGui.QStandardItem() - item.setData(uid, MeasurementDataModel._idRole) + item.setData(uid, MeasurementDataModel.idRole) item.setData(desc, MeasurementDataModel.descRole) + item.setData(collections.defaultdict(list), MeasurementDataModel.samplesRole) self.appendRow(item) self.sort(0) return item @@ -130,13 +105,19 @@ class MeasurementDataModel(QtGui.QStandardItemModel): value, unit, mqflags = data value_str = self.format_value(value) - unit_str = self.format_unit(unit) + unit_str = util.format_unit(unit) mqflags_str = self.format_mqflags(mqflags) # The display role is a tuple containing the value and the unit/flags. disp = (value_str, ' '.join([unit_str, mqflags_str])) item.setData(disp, QtCore.Qt.DisplayRole) + # 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) + d = item.data(MeasurementDataModel.samplesRole) + d[unit].append(sample) + class MultimeterDelegate(QtGui.QStyledItemDelegate): '''Delegate to show the data items from a MeasurementDataModel.''' @@ -154,16 +135,16 @@ class MultimeterDelegate(QtGui.QStyledItemDelegate): self._bfont.setBold(True) if self._bfont.pixelSize() != -1: - self._bfont.setPixelSize(self._bfont.pixelSize() * 1.8) + self._bfont.setPixelSize(self._bfont.pixelSize() * 1.2) else: - self._bfont.setPointSizeF(self._bfont.pointSizeF() * 1.8) + self._bfont.setPointSizeF(self._bfont.pointSizeF() * 1.2) fi = QtGui.QFontInfo(self._nfont) self._nfontheight = fi.pixelSize() fm = QtGui.QFontMetrics(self._bfont) r = fm.boundingRect('-XX.XXXXXX X XX') - self._size = QtCore.QSize(r.width() * 1.4, r.height() * 3.5) + self._size = QtCore.QSize(r.width() * 1.4, r.height() * 2.2) # Values used to calculate the positions of the strings in the # 'paint()' function. @@ -177,22 +158,7 @@ class MultimeterDelegate(QtGui.QStyledItemDelegate): value, unit = index.data(QtCore.Qt.DisplayRole) desc = index.data(MeasurementDataModel.descRole) - # Description in the top left corner. painter.setFont(self._nfont) p = options.rect.topLeft() p += QtCore.QPoint(self._nfontheight, 2 * self._nfontheight) - painter.drawText(p, desc) - - painter.setFont(self._bfont) - - # Value about in the center. - p = options.rect.center() - p += QtCore.QPoint(-3 * self._space_width, self._nfontheight) - rect = QtCore.QRect(0, 0, self._value_width, 2 * self._nfontheight) - rect.moveCenter(p) - painter.drawText(rect, QtCore.Qt.AlignRight, value) - - # Unit right of the value. - rect.moveLeft(rect.right()) - rect.adjust(self._space_width, 0, 0, 0) - painter.drawText(rect, QtCore.Qt.AlignLeft, unit) + painter.drawText(p, desc + ': ' + value + ' ' + unit)