forked from gnuradio/gnuradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigitalnumbercontrol.py
409 lines (325 loc) · 12.5 KB
/
digitalnumbercontrol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2020 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
#
from PyQt5.QtWidgets import QFrame, QVBoxLayout, QLabel
from PyQt5.QtGui import QPainter, QPixmap, QFont, QFontMetrics, QBrush, QColor
from PyQt5.QtCore import Qt, QSize
from PyQt5 import QtCore
from PyQt5.QtCore import Qt as Qtc
from PyQt5.QtCore import pyqtSignal
from gnuradio import gr
import pmt
# -------------- Support Classes ---------------------------------
class LabeledDigitalNumberControl(QFrame):
def __init__(
self,
lbl="",
min_freq_hz=0,
max_freq_hz=6000000000,
parent=None,
thousands_separator=",",
background_color="black",
fontColor="white",
click_callback=None,
):
QFrame.__init__(self, parent)
self.numberControl = DigitalNumberControl(
min_freq_hz,
max_freq_hz,
self,
thousands_separator,
background_color,
fontColor,
click_callback,
)
layout = QVBoxLayout()
self.lbl = QLabel(lbl, self)
if len:
self.hasLabel = True
layout.addWidget(self.lbl)
else:
self.hasLabel = False
layout.addWidget(self.numberControl)
layout.setAlignment(Qtc.AlignCenter | Qtc.AlignVCenter)
self.setLayout(layout)
self.show()
def minimumSizeHint(self):
if self.hasLabel:
return QSize(self.numberControl.minimumWidth() + 10, 100)
else:
return QSize(self.numberControl.minimumWidth() + 10, 50)
def setReadOnly(self, b_read_only):
self.numberControl.setReadOnly(b_read_only)
def setFrequency(self, new_freq):
self.numberControl.setFrequency(new_freq)
def getFrequency(self):
return self.numberControl.getFrequency()
class DigitalNumberControl(QFrame):
# Notifies to avoid thread conflicts on paints
updateInt = pyqtSignal(int)
updateFloat = pyqtSignal(float)
def __init__(
self,
min_freq_hz=0,
max_freq_hz=6000000000,
parent=None,
thousands_separator=",",
background_color="black",
fontColor="white",
click_callback=None,
):
QFrame.__init__(self, parent)
self.updateInt.connect(self.onUpdateInt)
self.updateFloat.connect(self.onUpdateFloat)
self.min_freq = int(min_freq_hz)
self.max_freq = int(max_freq_hz)
self.numDigitsInFreq = len(str(max_freq_hz))
self.thousands_separator = thousands_separator
self.click_callback = click_callback
self.read_only = False
self.setColors(QColor(background_color), QColor(fontColor))
self.numberFont = QFont("Arial", 12, QFont.Normal)
self.cur_freq = min_freq_hz
# Determine what our width minimum is
teststr = ""
for i in range(0, self.numDigitsInFreq):
teststr += "0"
fm = QFontMetrics(self.numberFont)
if len(self.thousands_separator) > 0:
# The -1 makes sure we don't count an extra for 123,456,789.
# Answer should be 2 not 3.
numgroups = int(float(self.numDigitsInFreq - 1) / 3.0)
if numgroups > 0:
for i in range(0, numgroups):
teststr += self.thousands_separator
textstr = teststr
else:
textstr = teststr
width = fm.width(textstr)
self.minwidth = width
if self.minwidth < 410:
self.minwidth = 410
self.setMaximumHeight(70)
self.setMinimumWidth(self.minwidth)
# Show the control
self.show()
def minimumSizeHint(self):
return QSize(self.minwidth, 50)
def setReadOnly(self, b_read_only):
self.read_only = b_read_only
def mousePressEvent(self, event):
super(DigitalNumberControl, self).mousePressEvent(event)
self.offset = event.pos()
if self.read_only:
gr.log.trace("click received but read-only. Not changing frequency.")
return
fm = QFontMetrics(self.numberFont)
if len(self.thousands_separator) > 0:
if self.thousands_separator != ".":
textstr = format(self.getFrequency(), self.thousands_separator)
else:
textstr = format(self.getFrequency(), ",")
textstr = textstr.replace(",", ".")
else:
textstr = str(self.getFrequency())
width = fm.width(textstr)
# So we know:
# - the width of the text
# - The mouse click position relative to 0
# (pos relative to string start will be size().width() - 2 - pos.x)
clickpos = self.size().width() - 2 - self.offset.x()
found_number = False
clicked_thousands = False
for i in range(1, len(textstr) + 1):
width = fm.width(textstr[-i:])
charstr = textstr[-i:]
widthchar = fm.width(charstr[0])
if clickpos >= (width - widthchar) and clickpos <= width:
clicked_char = i - 1
clicked_num_index = clicked_char
found_number = True
if len(self.thousands_separator) > 0:
if charstr[0] != self.thousands_separator:
numSeps = charstr.count(self.thousands_separator)
clicked_num_index -= numSeps
gr.log.trace(f"clicked number: {clicked_num_index}")
else:
clicked_thousands = True
gr.log.trace("clicked thousands separator")
else:
gr.log.trace("clicked number: " + str(clicked_char))
# Remember y=0 is at the top so this is reversed
clicked_up = False
if self.offset.y() > self.size().height() / 2:
gr.log.trace("clicked down")
else:
gr.log.trace("clicked up")
clicked_up = True
if not clicked_thousands:
cur_freq = self.getFrequency()
increment = pow(10, clicked_num_index)
if clicked_up:
cur_freq += increment
else:
cur_freq -= increment
# Cannot call setFrequency to emit.
# Change must happen now for paint event when clicked.
self.setFrequencyNow(cur_freq)
if self.click_callback is not None:
self.click_callback(self.getFrequency())
break
if (not found_number) and (not clicked_thousands):
# See if we clicked in the high area, if so, increment there.
clicked_up = False
if self.offset.y() > self.size().height() / 2:
gr.log.trace("clicked down in the high area")
else:
gr.log.trace("clicked up in the high area")
clicked_up = True
textstr = str(self.getFrequency())
numNumbers = len(textstr)
increment = pow(10, numNumbers)
cur_freq = self.getFrequency()
if clicked_up:
cur_freq += increment
else:
cur_freq -= increment
# Cannot call setFrequency to emit. Change must happen now for
# paint event when clicked.
self.setFrequencyNow(cur_freq)
if self.click_callback is not None:
gr.log.trace("Calling self.click_callback")
self.click_callback(self.getFrequency())
else:
gr.log.trace("self.click_callback is None. Not calling callback.")
def setColors(self, background, fontColor):
self.background_color = background
self.fontColor = fontColor
def reverseString(self, astring):
astring = astring[::-1]
return astring
def onUpdateInt(self, new_freq):
if (new_freq >= self.min_freq) and (new_freq <= self.max_freq):
self.cur_freq = int(new_freq)
self.update()
def onUpdateFloat(self, new_freq):
if (new_freq >= self.min_freq) and (new_freq <= self.max_freq):
self.cur_freq = int(new_freq)
self.update()
def setFrequencyNow(self, new_freq):
# This setFrequency differs from setFrequency() in that it
# it updates the display immediately rather than emitting
# to the message queue as required during msg handling
if (new_freq >= self.min_freq) and (new_freq <= self.max_freq):
self.cur_freq = int(new_freq)
self.update()
def setFrequency(self, new_freq):
if type(new_freq) == int:
self.updateInt.emit(new_freq)
else:
self.updateFloat.emit(new_freq)
def getFrequency(self):
return self.cur_freq
def resizeEvent(self, event):
self.pxMap = QPixmap(self.size())
self.pxMap.fill(self.background_color)
self.update()
def paintEvent(self, event):
super().paintEvent(event)
painter = QPainter(self)
size = self.size()
brush = QBrush()
brush.setColor(self.background_color)
brush.setStyle(Qt.SolidPattern)
rect = QtCore.QRect(2, 2, size.width() - 4, size.height() - 4)
painter.fillRect(rect, brush)
self.numberFont.setPixelSize(int(0.9 * size.height()))
painter.setFont(self.numberFont)
painter.setPen(self.fontColor)
rect = event.rect()
if len(self.thousands_separator) > 0:
if self.thousands_separator != ".":
textstr = format(self.getFrequency(), self.thousands_separator)
else:
textstr = format(self.getFrequency(), ",")
textstr = textstr.replace(",", ".")
else:
textstr = str(self.getFrequency())
rect = QtCore.QRect(0, 0, size.width() - 4, size.height())
painter.drawText(rect, Qt.AlignRight + Qt.AlignVCenter, textstr)
class MsgDigitalNumberControl(gr.sync_block, LabeledDigitalNumberControl):
"""
GNU Radio Block Class
"""
def __init__(
self,
lbl="",
min_freq_hz=0,
max_freq_hz=6000000000,
parent=None,
thousands_separator=",",
background_color="black",
fontColor="white",
var_callback=None,
outputmsgname="freq",
):
gr.sync_block.__init__(
self, name="MsgDigitalNumberControl", in_sig=None, out_sig=None
)
LabeledDigitalNumberControl.__init__(
self,
lbl,
min_freq_hz,
max_freq_hz,
parent,
thousands_separator,
background_color,
fontColor,
self.click_callback,
)
self.var_callback = var_callback
self.outputmsgname = outputmsgname
self.message_port_register_in(pmt.intern("valuein"))
self.set_msg_handler(pmt.intern("valuein"), self.msgHandler)
self.message_port_register_out(pmt.intern("valueout"))
def msgHandler(self, msg):
try:
new_val = pmt.to_python(pmt.cdr(msg))
if type(new_val) == float or type(new_val) == int:
self.call_var_callback(new_val)
self.setValue(new_val)
else:
gr.log.error(
"Value received was not an int or a float. %s" % str(type(new_val))
)
except Exception as e:
gr.log.error("Error with message conversion: %s" % str(e))
def call_var_callback(self, new_value):
if self.var_callback is not None:
if type(self.var_callback) is float:
self.var_callback = float(new_value)
else:
self.var_callback(float(new_value))
def click_callback(self, new_value):
self.call_var_callback(new_value)
self.message_port_pub(
pmt.intern("valueout"),
pmt.cons(pmt.intern(self.outputmsgname), pmt.from_double(float(new_value))),
)
def setValue(self, new_val):
self.setFrequency(new_val)
self.message_port_pub(
pmt.intern("valueout"),
pmt.cons(pmt.intern(self.outputmsgname), pmt.from_double(float(new_val))),
)
def getValue(self):
self.getFrequency()
def setReadOnly(self, b_read_only):
super().setReadOnly(b_read_only)