]> sigrok.org Git - sigrok-meter.git/blob - datamodel.py
Show log messages in the GUI.
[sigrok-meter.git] / datamodel.py
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
21 import itertools
22 import math
23 import qtcompat
24 import sigrok.core as sr
25 import util
26
27 try:
28     from itertools import izip
29 except ImportError:
30     izip = zip
31
32 QtCore = qtcompat.QtCore
33 QtGui = qtcompat.QtGui
34
35 class 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
46 class 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
172     def clear_samples(self):
173         '''Removes all old samples from the model.'''
174         for row in range(self.rowCount()):
175             idx = self.index(row, 0)
176             self.setData(idx, {},
177                 MeasurementDataModel.tracesRole)
178
179 class MultimeterDelegate(QtGui.QStyledItemDelegate):
180     '''Delegate to show the data items from a MeasurementDataModel.'''
181
182     def __init__(self, parent, font):
183         '''Initialize the delegate.
184
185         :param font: Font used for the text.
186         '''
187
188         super(self.__class__, self).__init__(parent)
189
190         self._nfont = font
191
192         fi = QtGui.QFontInfo(self._nfont)
193         self._nfontheight = fi.pixelSize()
194
195         fm = QtGui.QFontMetrics(self._nfont)
196         r = fm.boundingRect('-XX.XXXXXX X XX')
197
198         w = 1.4 * r.width() + 2 * self._nfontheight
199         h = 2.6 * self._nfontheight
200         self._size = QtCore.QSize(w, h)
201
202     def sizeHint(self, option=None, index=None):
203         return self._size
204
205     def _color_rect(self, outer):
206         '''Returns the dimensions of the clickable rectangle.'''
207         x1 = (outer.height() - self._nfontheight) / 2
208         r = QtCore.QRect(x1, x1, self._nfontheight, self._nfontheight)
209         r.translate(outer.topLeft())
210         return r
211
212     def paint(self, painter, options, index):
213         value, unit = index.data(QtCore.Qt.DisplayRole)
214         desc = index.data(MeasurementDataModel.descRole)
215         color = index.data(MeasurementDataModel.colorRole)
216
217         painter.setFont(self._nfont)
218
219         # Draw the clickable rectangle.
220         painter.fillRect(self._color_rect(options.rect), color)
221
222         # Draw the text
223         h = options.rect.height()
224         p = options.rect.topLeft()
225         p += QtCore.QPoint(h, (h + self._nfontheight) / 2 - 2)
226         painter.drawText(p, desc + ': ' + value + ' ' + unit)
227
228     def editorEvent(self, event, model, options, index):
229         if type(event) is QtGui.QMouseEvent:
230             if event.type() == QtCore.QEvent.MouseButtonPress:
231                 rect = self._color_rect(options.rect)
232                 if rect.contains(event.x(), event.y()):
233                     c = index.data(MeasurementDataModel.colorRole)
234                     c = QtGui.QColorDialog.getColor(c, None,
235                         'Choose new color for channel')
236                     if c.isValid():
237                         # False if cancel is pressed (resulting in a black
238                         # color)
239                         item = model.itemFromIndex(index)
240                         item.setData(c, MeasurementDataModel.colorRole)
241
242                     return True
243
244         return False