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