2 ## This file is part of the sigrok-meter project.
4 ## Copyright (C) 2014 Jens Steinhauser <jens.steinhauser@gmail.com>
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 import sigrok.core as sr
24 QtCore = qtcompat.QtCore
25 QtGui = qtcompat.QtGui
27 class MeasurementDataModel(QtGui.QStandardItemModel):
28 '''Model to hold the measured values.'''
30 '''Role used to identify and find the item.'''
31 _idRole = QtCore.Qt.UserRole + 1
33 '''Role used to store the device vendor and model.'''
34 descRole = QtCore.Qt.UserRole + 2
36 def __init__(self, parent):
37 super(self.__class__, self).__init__(parent)
39 # Use the description text to sort the items for now, because the
40 # _idRole holds tuples, and using them to sort doesn't work.
41 self.setSortRole(MeasurementDataModel.descRole)
43 # Used in 'format_value()' to check against.
44 self.inf = float('inf')
46 def format_unit(self, u):
50 sr.Unit.OHM: u'\u03A9',
53 sr.Unit.CELSIUS: u'\u00B0C',
54 sr.Unit.FAHRENHEIT: u'\u00B0F',
56 sr.Unit.PERCENTAGE: '%',
60 sr.Unit.DECIBEL_MW: 'dBu',
61 sr.Unit.DECIBEL_VOLT: 'dBV',
63 sr.Unit.DECIBEL_SPL: 'dB',
64 # sr.Unit.CONCENTRATION
65 sr.Unit.REVOLUTIONS_PER_MINUTE: 'rpm',
66 sr.Unit.VOLT_AMPERE: 'VA',
68 sr.Unit.WATT_HOUR: 'Wh',
69 sr.Unit.METER_SECOND: 'm/s',
70 sr.Unit.HECTOPASCAL: 'hPa',
71 sr.Unit.HUMIDITY_293K: '%rF',
72 sr.Unit.DEGREE: u'\u00B0',
76 return units.get(u, '')
78 def format_mqflags(self, mqflags):
79 if sr.QuantityFlag.AC in mqflags:
81 elif sr.QuantityFlag.DC in mqflags:
86 def format_value(self, mag):
89 return '{:f}'.format(mag)
91 def getItem(self, device, channel):
92 '''Return the item for the device + channel combination from the
93 model, or create a new item if no existing one matches.'''
95 # Unique identifier for the device + channel.
96 # TODO: Isn't there something better?
100 device.serial_number(),
101 device.connection_id(),
105 # Find the correct item in the model.
106 for row in range(self.rowCount()):
107 item = self.item(row)
108 rid = item.data(MeasurementDataModel._idRole)
109 rid = tuple(rid) # PySide returns a list.
113 # Nothing found, create a new item.
114 desc = '{} {}, channel "{}"'.format(
115 device.vendor, device.model, channel.name)
117 item = QtGui.QStandardItem()
118 item.setData(uid, MeasurementDataModel._idRole)
119 item.setData(desc, MeasurementDataModel.descRole)
124 @QtCore.Slot(object, object, object)
125 def update(self, device, channel, data):
126 '''Update the data for the device (+channel) with the most recent
127 measurement from the given payload.'''
129 item = self.getItem(device, channel)
131 value, unit, mqflags = data
132 value_str = self.format_value(value)
133 unit_str = self.format_unit(unit)
134 mqflags_str = self.format_mqflags(mqflags)
136 # The display role is a tuple containing the value and the unit/flags.
137 disp = (value_str, ' '.join([unit_str, mqflags_str]))
138 item.setData(disp, QtCore.Qt.DisplayRole)
140 class MultimeterDelegate(QtGui.QStyledItemDelegate):
141 '''Delegate to show the data items from a MeasurementDataModel.'''
143 def __init__(self, parent, font):
144 '''Initialize the delegate.
146 :param font: Font used for the description text, the value is drawn
147 with a slightly bigger and bold variant of the font.
150 super(self.__class__, self).__init__(parent)
153 self._bfont = QtGui.QFont(self._nfont)
155 self._bfont.setBold(True)
156 if self._bfont.pixelSize() != -1:
157 self._bfont.setPixelSize(self._bfont.pixelSize() * 1.8)
159 self._bfont.setPointSizeF(self._bfont.pointSizeF() * 1.8)
161 fi = QtGui.QFontInfo(self._nfont)
162 self._nfontheight = fi.pixelSize()
164 fm = QtGui.QFontMetrics(self._bfont)
165 r = fm.boundingRect('-XX.XXXXXX X XX')
166 self._size = QtCore.QSize(r.width() * 1.4, r.height() * 3.5)
168 # Values used to calculate the positions of the strings in the
169 # 'paint()' function.
170 self._space_width = fm.boundingRect('_').width()
171 self._value_width = fm.boundingRect('-XX.XXXXXX').width()
173 def sizeHint(self, option=None, index=None):
176 def paint(self, painter, options, index):
177 value, unit = index.data(QtCore.Qt.DisplayRole)
178 desc = index.data(MeasurementDataModel.descRole)
180 # Description in the top left corner.
181 painter.setFont(self._nfont)
182 p = options.rect.topLeft()
183 p += QtCore.QPoint(self._nfontheight, 2 * self._nfontheight)
184 painter.drawText(p, desc)
186 painter.setFont(self._bfont)
188 # Value about in the center.
189 p = options.rect.center()
190 p += QtCore.QPoint(-3 * self._space_width, self._nfontheight)
191 rect = QtCore.QRect(0, 0, self._value_width, 2 * self._nfontheight)
193 painter.drawText(rect, QtCore.Qt.AlignRight, value)
195 # Unit right of the value.
196 rect.moveLeft(rect.right())
197 rect.adjust(self._space_width, 0, 0, 0)
198 painter.drawText(rect, QtCore.Qt.AlignLeft, unit)