]> sigrok.org Git - sigrok-meter.git/blame_incremental - datamodel.py
license: remove FSF postal address from boiler plate license text
[sigrok-meter.git] / datamodel.py
... / ...
CommitLineData
1##
2## This file is part of the sigrok-meter project.
3##
4## Copyright (C) 2014 Jens Steinhauser <jens.steinhauser@gmail.com>
5##
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.
10##
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.
15##
16## You should have received a copy of the GNU General Public License
17## along with this program; if not, see <http://www.gnu.org/licenses/>.
18##
19
20import itertools
21import math
22import qtcompat
23import sigrok.core as sr
24import util
25
26try:
27 from itertools import izip
28except ImportError:
29 izip = zip
30
31QtCore = qtcompat.QtCore
32QtGui = qtcompat.QtGui
33
34class Trace(object):
35 '''Class to hold the measured samples.'''
36
37 def __init__(self):
38 self.samples = []
39 self.new = False
40
41 def append(self, sample):
42 self.samples.append(sample)
43 self.new = True
44
45class MeasurementDataModel(QtGui.QStandardItemModel):
46 '''Model to hold the measured values.'''
47
48 '''Role used to identify and find the item.'''
49 idRole = QtCore.Qt.UserRole + 1
50
51 '''Role used to store the device vendor and model.'''
52 descRole = QtCore.Qt.UserRole + 2
53
54 '''Role used to store a dictionary with the traces.'''
55 tracesRole = QtCore.Qt.UserRole + 3
56
57 '''Role used to store the color to draw the graph of the channel.'''
58 colorRole = QtCore.Qt.UserRole + 4
59
60 def __init__(self, parent):
61 super(self.__class__, self).__init__(parent)
62
63 # Use the description text to sort the items for now, because the
64 # idRole holds tuples, and using them to sort doesn't work.
65 self.setSortRole(MeasurementDataModel.descRole)
66
67 # A generator for the colors of the channels.
68 self._colorgen = self._make_colorgen()
69
70 def _make_colorgen(self):
71 cols = [
72 QtGui.QColor(0x8F, 0x52, 0x02), # brown
73 QtGui.QColor(0x73, 0xD2, 0x16), # green
74 QtGui.QColor(0xCC, 0x00, 0x00), # red
75 QtGui.QColor(0x34, 0x65, 0xA4), # blue
76 QtGui.QColor(0xF5, 0x79, 0x00), # orange
77 QtGui.QColor(0xED, 0xD4, 0x00), # yellow
78 QtGui.QColor(0x75, 0x50, 0x7B) # violet
79 ]
80
81 def myrepeat(g, n):
82 '''Repeats every element from 'g' 'n' times'.'''
83 for e in g:
84 for f in itertools.repeat(e, n):
85 yield f
86
87 colorcycle = itertools.cycle(cols)
88 darkness = myrepeat(itertools.count(100, 10), len(cols))
89
90 for c, d in izip(colorcycle, darkness):
91 yield QtGui.QColor(c).darker(d)
92
93 def format_mqflags(self, mqflags):
94 if sr.QuantityFlag.AC in mqflags:
95 return 'AC'
96 elif sr.QuantityFlag.DC in mqflags:
97 return 'DC'
98 else:
99 return ''
100
101 def format_value(self, mag):
102 if math.isinf(mag):
103 return u'\u221E'
104 return '{:f}'.format(mag)
105
106 def getItem(self, device, channel):
107 '''Return the item for the device + channel combination from the
108 model, or create a new item if no existing one matches.'''
109
110 # Unique identifier for the device + channel.
111 # TODO: Isn't there something better?
112 uid = (
113 device.vendor,
114 device.model,
115 device.serial_number(),
116 device.connection_id(),
117 channel.index
118 )
119
120 # Find the correct item in the model.
121 for row in range(self.rowCount()):
122 item = self.item(row)
123 rid = item.data(MeasurementDataModel.idRole)
124 rid = tuple(rid) # PySide returns a list.
125 if uid == rid:
126 return item
127
128 # Nothing found, create a new item.
129 desc = '{} {}, {}'.format(
130 device.vendor, device.model, channel.name)
131
132 item = QtGui.QStandardItem()
133 item.setData(uid, MeasurementDataModel.idRole)
134 item.setData(desc, MeasurementDataModel.descRole)
135 item.setData({}, MeasurementDataModel.tracesRole)
136 item.setData(next(self._colorgen), MeasurementDataModel.colorRole)
137 self.appendRow(item)
138 self.sort(0)
139 return item
140
141 @QtCore.Slot(float, sr.classes.Device, sr.classes.Channel, tuple)
142 def update(self, timestamp, device, channel, data):
143 '''Update the data for the device (+channel) with the most recent
144 measurement from the given payload.'''
145
146 item = self.getItem(device, channel)
147
148 value, unit, mqflags = data
149 value_str = self.format_value(value)
150 unit_str = util.format_unit(unit)
151 mqflags_str = self.format_mqflags(mqflags)
152
153 # The display role is a tuple containing the value and the unit/flags.
154 disp = (value_str, ' '.join([unit_str, mqflags_str]))
155 item.setData(disp, QtCore.Qt.DisplayRole)
156
157 # The samples role is a dictionary that contains the old samples for each unit.
158 # Should be trimmed periodically, otherwise it grows larger and larger.
159 if not math.isinf(value) and not math.isnan(value):
160 sample = (timestamp, value)
161 traces = item.data(MeasurementDataModel.tracesRole)
162
163 # It's not possible to use 'collections.defaultdict' here, because
164 # PySide doesn't return the original type that was passed in.
165 if not (unit in traces):
166 traces[unit] = Trace()
167 traces[unit].append(sample)
168
169 item.setData(traces, MeasurementDataModel.tracesRole)
170
171 def clear_samples(self):
172 '''Removes all old samples from the model.'''
173 for row in range(self.rowCount()):
174 idx = self.index(row, 0)
175 self.setData(idx, {},
176 MeasurementDataModel.tracesRole)
177
178class MultimeterDelegate(QtGui.QStyledItemDelegate):
179 '''Delegate to show the data items from a MeasurementDataModel.'''
180
181 def __init__(self, parent, font):
182 '''Initialize the delegate.
183
184 :param font: Font used for the text.
185 '''
186
187 super(self.__class__, self).__init__(parent)
188
189 self._nfont = font
190
191 fi = QtGui.QFontInfo(self._nfont)
192 self._nfontheight = fi.pixelSize()
193
194 fm = QtGui.QFontMetrics(self._nfont)
195 r = fm.boundingRect('-XX.XXXXXX X XX')
196
197 w = 1.4 * r.width() + 2 * self._nfontheight
198 h = 2.6 * self._nfontheight
199 self._size = QtCore.QSize(w, h)
200
201 def sizeHint(self, option=None, index=None):
202 return self._size
203
204 def _color_rect(self, outer):
205 '''Returns the dimensions of the clickable rectangle.'''
206 x1 = (outer.height() - self._nfontheight) / 2
207 r = QtCore.QRect(x1, x1, self._nfontheight, self._nfontheight)
208 r.translate(outer.topLeft())
209 return r
210
211 def paint(self, painter, options, index):
212 value, unit = index.data(QtCore.Qt.DisplayRole)
213 desc = index.data(MeasurementDataModel.descRole)
214 color = index.data(MeasurementDataModel.colorRole)
215
216 painter.setFont(self._nfont)
217
218 # Draw the clickable rectangle.
219 painter.fillRect(self._color_rect(options.rect), color)
220
221 # Draw the text
222 h = options.rect.height()
223 p = options.rect.topLeft()
224 p += QtCore.QPoint(h, (h + self._nfontheight) / 2 - 2)
225 painter.drawText(p, desc + ': ' + value + ' ' + unit)
226
227 def editorEvent(self, event, model, options, index):
228 if type(event) is QtGui.QMouseEvent:
229 if event.type() == QtCore.QEvent.MouseButtonPress:
230 rect = self._color_rect(options.rect)
231 if rect.contains(event.x(), event.y()):
232 c = index.data(MeasurementDataModel.colorRole)
233 c = QtGui.QColorDialog.getColor(c, None,
234 'Choose new color for channel')
235 if c.isValid():
236 # False if cancel is pressed (resulting in a black
237 # color).
238 item = model.itemFromIndex(index)
239 item.setData(c, MeasurementDataModel.colorRole)
240
241 return True
242
243 return False