]> sigrok.org Git - sigrok-meter.git/blobdiff - datamodel.py
Temporary fixes for slightly more usable multi-device UI.
[sigrok-meter.git] / datamodel.py
index c36e295529420d2a0b2dd40615d4a25f0f3f4fce..ea00278c37b38cf28dfa3540499bcc37903f62b8 100644 (file)
@@ -89,11 +89,11 @@ class MeasurementDataModel(QtGui.QStandardItemModel):
         return '{:f}'.format(mag)
 
     def getItem(self, device, channel):
-        '''Returns the item for the device + channel combination from the model,
-        or creates a new item if no existing one matches.'''
+        '''Return the item for the device + channel combination from the
+        model, or create a new item if no existing one matches.'''
 
-        # unique identifier for the device + channel
-        # TODO: isn't there something better?
+        # Unique identifier for the device + channel.
+        # TODO: Isn't there something better?
         uid = (
             device.vendor,
             device.model,
@@ -102,16 +102,16 @@ class MeasurementDataModel(QtGui.QStandardItemModel):
             channel.index
         )
 
-        # find the correct item in the model
+        # Find the correct item in the model.
         for row in range(self.rowCount()):
             item = self.item(row)
             rid = item.data(MeasurementDataModel._idRole)
-            rid = tuple(rid) # PySide returns a list
+            rid = tuple(rid) # PySide returns a list.
             if uid == rid:
                 return item
 
-        # nothing found, create a new item
-        desc = '{} {}, channel "{}"'.format(
+        # Nothing found, create a new item.
+        desc = '{} {}, {}'.format(
                 device.vendor, device.model, channel.name)
 
         item = QtGui.QStandardItem()
@@ -123,7 +123,7 @@ class MeasurementDataModel(QtGui.QStandardItemModel):
 
     @QtCore.Slot(object, object, object)
     def update(self, device, channel, data):
-        '''Updates the data for the device (+channel) with the most recent
+        '''Update the data for the device (+channel) with the most recent
         measurement from the given payload.'''
 
         item = self.getItem(device, channel)
@@ -133,14 +133,15 @@ class MeasurementDataModel(QtGui.QStandardItemModel):
         unit_str = self.format_unit(unit)
         mqflags_str = self.format_mqflags(mqflags)
 
-        disp = ' '.join([value_str, unit_str, mqflags_str])
+        # 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)
 
 class MultimeterDelegate(QtGui.QStyledItemDelegate):
     '''Delegate to show the data items from a MeasurementDataModel.'''
 
     def __init__(self, parent, font):
-        '''Initializes the delegate.
+        '''Initialize the delegate.
 
         :param font: Font used for the description text, the value is drawn
                      with a slightly bigger and bold variant of the font.
@@ -153,32 +154,30 @@ 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.2, 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.
+        self._space_width = fm.boundingRect('_').width()
+        self._value_width = fm.boundingRect('-XX.XXXXXX').width()
 
     def sizeHint(self, option=None, index=None):
         return self._size
 
     def paint(self, painter, options, index):
-        value = index.data(QtCore.Qt.DisplayRole)
+        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)
-
-        # value in the center
-        painter.setFont(self._bfont)
-        r = options.rect.adjusted(self._nfontheight, 2.5 * self._nfontheight,
-                0, 0)
-        painter.drawText(r, QtCore.Qt.AlignCenter, value)
+        painter.drawText(p, desc + ': ' + value + ' ' + unit)