2 ## This file is part of the sigrok-meter project.
4 ## Copyright (C) 2014 Jens Steinhauser <jens.steinhauser@gmail.com>
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.
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.
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
24 import sigrok
.core
as sr
28 from itertools
import izip
32 QtCore
= qtcompat
.QtCore
33 QtGui
= qtcompat
.QtGui
36 '''Class to hold the measured samples.'''
42 def append(self
, sample
):
43 self
.samples
.append(sample
)
46 class MeasurementDataModel(QtGui
.QStandardItemModel
):
47 '''Model to hold the measured values.'''
49 '''Role used to identify and find the item.'''
50 idRole
= QtCore
.Qt
.UserRole
+ 1
52 '''Role used to store the device vendor and model.'''
53 descRole
= QtCore
.Qt
.UserRole
+ 2
55 '''Role used to store a dictionary with the traces'''
56 tracesRole
= QtCore
.Qt
.UserRole
+ 3
58 '''Role used to store the color to draw the graph of the channel.'''
59 colorRole
= QtCore
.Qt
.UserRole
+ 4
61 def __init__(self
, parent
):
62 super(self
.__class
__, self
).__init
__(parent
)
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
)
68 # A generator for the colors of the channels.
69 self
._colorgen
= self
._make
_colorgen
()
71 def _make_colorgen(self
):
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
83 '''Repeats every element from 'g' 'n' times'.'''
85 for f
in itertools
.repeat(e
, n
):
88 colorcycle
= itertools
.cycle(cols
)
89 darkness
= myrepeat(itertools
.count(100, 10), len(cols
))
91 for c
, d
in izip(colorcycle
, darkness
):
92 yield QtGui
.QColor(c
).darker(d
)
94 def format_mqflags(self
, mqflags
):
95 if sr
.QuantityFlag
.AC
in mqflags
:
97 elif sr
.QuantityFlag
.DC
in mqflags
:
102 def format_value(self
, mag
):
105 return '{:f}'.format(mag
)
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.'''
111 # Unique identifier for the device + channel.
112 # TODO: Isn't there something better?
116 device
.serial_number(),
117 device
.connection_id(),
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.
129 # Nothing found, create a new item.
130 desc
= '{} {}, {}'.format(
131 device
.vendor
, device
.model
, channel
.name
)
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
)
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.'''
147 item
= self
.getItem(device
, channel
)
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
)
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
)
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
)
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
)
170 item
.setData(traces
, MeasurementDataModel
.tracesRole
)
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
)
179 class MultimeterDelegate(QtGui
.QStyledItemDelegate
):
180 '''Delegate to show the data items from a MeasurementDataModel.'''
182 def __init__(self
, parent
, font
):
183 '''Initialize the delegate.
185 :param font: Font used for the text.
188 super(self
.__class
__, self
).__init
__(parent
)
192 fi
= QtGui
.QFontInfo(self
._nfont
)
193 self
._nfontheight
= fi
.pixelSize()
195 fm
= QtGui
.QFontMetrics(self
._nfont
)
196 r
= fm
.boundingRect('-XX.XXXXXX X XX')
198 w
= 1.4 * r
.width() + 2 * self
._nfontheight
199 h
= 2.6 * self
._nfontheight
200 self
._size
= QtCore
.QSize(w
, h
)
202 def sizeHint(self
, option
=None, index
=None):
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())
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
)
217 painter
.setFont(self
._nfont
)
219 # Draw the clickable rectangle.
220 painter
.fillRect(self
._color
_rect
(options
.rect
), color
)
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
)
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')
237 # False if cancel is pressed (resulting in a black
239 item
= model
.itemFromIndex(index
)
240 item
.setData(c
, MeasurementDataModel
.colorRole
)