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