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