commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 03/16: qtgui: adds Type setting for Range w


From: git
Subject: [Commit-gnuradio] [gnuradio] 03/16: qtgui: adds Type setting for Range widget.
Date: Sun, 26 Apr 2015 23:18:02 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

jcorgan pushed a commit to branch master
in repository gnuradio.

commit bb8e65d506b1e28b38548649ed2cf7fa86e70762
Author: Tom Rondeau <address@hidden>
Date:   Tue Apr 21 16:46:55 2015 -0400

    qtgui: adds Type setting for Range widget.
    
    Can set type to Float, Int, or Long to connect to properties requiring
    specific data types. Defaults to Float.
---
 gr-qtgui/grc/qtgui_range.xml   | 210 +++++++++++++++++++++++------------------
 gr-qtgui/python/qtgui/range.py |  52 ++++++----
 2 files changed, 151 insertions(+), 111 deletions(-)

diff --git a/gr-qtgui/grc/qtgui_range.xml b/gr-qtgui/grc/qtgui_range.xml
index 05f3ffc..36bd330 100644
--- a/gr-qtgui/grc/qtgui_range.xml
+++ b/gr-qtgui/grc/qtgui_range.xml
@@ -6,105 +6,127 @@
 ###################################################
  -->
 <block>
-       <name>QT GUI Range</name>
-       <key>variable_qtgui_range</key>
-       <import>from gnuradio.qtgui import Range, RangeWidget</import>
-       <var_make>self.$(id) = $(id) = $value</var_make>
-       <make>#set $win = 'self._%s_win'%$id
+  <name>QT GUI Range</name>
+  <key>variable_qtgui_range</key>
+  <import>from gnuradio.qtgui import Range, RangeWidget</import>
+  <var_make>self.$(id) = $(id) = $value</var_make>
+  <make>#set $win = 'self._%s_win'%$id
                #set $range = 'self._%s_range'%$id
 #if not $label()
        #set $label = '"%s"'%$id
 #end if
 $(range) = Range($start, $stop, $step, $value, $min_len)
-$(win) = RangeWidget($range, self.set_$(id), $label, "$widget")
+$(win) = RangeWidget($range, self.set_$(id), $label, "$widget", $astype)
 $(gui_hint()($win))</make>
-       <callback>self.set_$(id)($value)</callback>
-       <param>
-               <name>Label</name>
-               <key>label</key>
-               <value></value>
-               <type>string</type>
-               <hide>#if $label() then 'none' else 'part'#</hide>
-       </param>
-       <param>
-               <name>Default Value</name>
-               <key>value</key>
-               <value>50</value>
-               <type>real</type>
-       </param>
-       <param>
-               <name>Start</name>
-               <key>start</key>
-               <value>0</value>
-               <type>real</type>
-       </param>
-       <param>
-               <name>Stop</name>
-               <key>stop</key>
-               <value>100</value>
-               <type>real</type>
-       </param>
-       <param>
-               <name>Step</name>
-               <key>step</key>
-               <value>1</value>
-               <type>real</type>
-       </param>
-       <param>
-               <name>Widget</name>
-               <key>widget</key>
-               <value>counter_slider</value>
-               <type>enum</type>
-               <hide>part</hide>
-               <option><name>Counter + 
Slider</name><key>counter_slider</key></option>
-               <option><name>Counter</name><key>counter</key></option>
-               <option><name>Slider</name><key>slider</key></option>
-               <option><name>Knob</name><key>dial</key></option>
-       </param>
-       <param>
-               <name>Orientation</name>
-               <key>orient</key>
-               <value>Qt.Horizontal</value>
-               <type>enum</type>
-               <hide>#if $widget() == "slider" then 'part' else 'all'#</hide>
-               <option>
-                       <name>Horizontal</name>
-                       <key>Qt.Horizontal</key>
-                       <opt>scalepos:BottomScale</opt>
-                       <opt>minfcn:setMinimumWidth</opt>
-               </option>
-               <option>
-                       <name>Vertical</name>
-                       <key>Qt.Vertical</key>
-                       <opt>scalepos:LeftScale</opt>
-                       <opt>minfcn:setMinimumHeight</opt>
-               </option>
-       </param>
-       <param>
-               <name>Minimum Length</name>
-               <key>min_len</key>
-               <value>200</value>
-               <type>int</type>
-               <hide>part</hide>
-       </param>
-<!--   from min_len    <hide>#if $widget().split('_')[0] in ("slider", 
"counter") then 'part' else 'all'#</hide>-->
-       <param>
-               <name>GUI Hint</name>
-               <key>gui_hint</key>
-               <value></value>
-               <type>gui_hint</type>
-               <hide>part</hide>
-       </param>
-       <check>$start &lt;= $value &lt;= $stop</check>
-       <check>$start &lt; $stop</check>
-       <doc>
-This block creates a variable with a slider. \
-Leave the label blank to use the variable id as the label. \
-The value must be a real number. \
-The value must be between the start and the stop.
+  <callback>self.set_$(id)($value)</callback>
 
-The GUI hint can be used to position the widget within the application. \
-The hint is of the form address@hidden: [row, col, row_span, col_span]. \
-Both the tab specification and the grid position are optional.
-       </doc>
+  <param>
+    <name>Label</name>
+    <key>label</key>
+    <value></value>
+    <type>string</type>
+    <hide>#if $label() then 'none' else 'part'#</hide>
+  </param>
+
+  <param>
+    <name>Type</name>
+    <key>astype</key>
+    <value>"float"</value>
+    <type>enum</type>
+    <hide>part</hide>
+    <option><name>Float</name><key>"float"</key></option>
+    <option><name>Int</name><key>"int"</key></option>
+    <option><name>Long</name><key>"long"</key></option>
+  </param>
+
+  <param>
+    <name>Default Value</name>
+    <key>value</key>
+    <value>50</value>
+    <type>real</type>
+  </param>
+
+  <param>
+    <name>Start</name>
+    <key>start</key>
+    <value>0</value>
+    <type>real</type>
+  </param>
+
+  <param>
+    <name>Stop</name>
+    <key>stop</key>
+    <value>100</value>
+    <type>real</type>
+  </param>
+
+  <param>
+    <name>Step</name>
+    <key>step</key>
+    <value>1</value>
+    <type>real</type>
+  </param>
+
+  <param>
+    <name>Widget</name>
+    <key>widget</key>
+    <value>counter_slider</value>
+    <type>enum</type>
+    <hide>part</hide>
+    <option><name>Counter + Slider</name><key>counter_slider</key></option>
+    <option><name>Counter</name><key>counter</key></option>
+    <option><name>Slider</name><key>slider</key></option>
+    <option><name>Knob</name><key>dial</key></option>
+  </param>
+
+  <param>
+    <name>Orientation</name>
+    <key>orient</key>
+    <value>Qt.Horizontal</value>
+    <type>enum</type>
+    <hide>#if $widget() == "slider" then 'part' else 'all'#</hide>
+    <option>
+      <name>Horizontal</name>
+      <key>Qt.Horizontal</key>
+      <opt>scalepos:BottomScale</opt>
+      <opt>minfcn:setMinimumWidth</opt>
+    </option>
+    <option>
+      <name>Vertical</name>
+      <key>Qt.Vertical</key>
+      <opt>scalepos:LeftScale</opt>
+      <opt>minfcn:setMinimumHeight</opt>
+    </option>
+  </param>
+
+  <param>
+    <name>Minimum Length</name>
+    <key>min_len</key>
+    <value>200</value>
+    <type>int</type>
+    <hide>part</hide>
+  </param>
+  <!-- from min_len    <hide>#if $widget().split('_')[0] in ("slider", 
"counter") then 'part' else 'all'#</hide>-->
+
+  <param>
+    <name>GUI Hint</name>
+    <key>gui_hint</key>
+    <value></value>
+    <type>gui_hint</type>
+    <hide>part</hide>
+  </param>
+
+  <check>$start &lt;= $value &lt;= $stop</check>
+  <check>$start &lt; $stop</check>
+
+  <doc>
+    This block creates a variable with a slider. \
+    Leave the label blank to use the variable id as the label. \
+    The value must be a real number. \
+    The value must be between the start and the stop.
+
+    The GUI hint can be used to position the widget within the application. \
+    The hint is of the form address@hidden: [row, col, row_span, col_span]. \
+    Both the tab specification and the grid position are optional.
+  </doc>
 </block>
diff --git a/gr-qtgui/python/qtgui/range.py b/gr-qtgui/python/qtgui/range.py
index 168e666..bb7c100 100755
--- a/gr-qtgui/python/qtgui/range.py
+++ b/gr-qtgui/python/qtgui/range.py
@@ -64,13 +64,17 @@ class Range(object):
 
 
 class RangeWidget(QtGui.QWidget):
-    def __init__(self, ranges, slot, label, style):
+    def __init__(self, ranges, slot, label, style, astype="float"):
         """ Creates the QT Range widget """
         QtGui.QWidget.__init__(self)
 
         self.range = ranges
         self.style = style
 
+        # astype tells the block how to return the value as a standard
+        # Python type. Can be: long, int, or float.
+        types = {"float": float, "long": long, "int": int}
+
         # Top-block function to call when any value changes
         # Some widgets call this directly when their value changes.
         # Others have intermediate functions to map the value into the right 
range.
@@ -80,25 +84,28 @@ class RangeWidget(QtGui.QWidget):
         label = Qt.QLabel(label)
         layout.addWidget(label)
 
+
         if style == "dial":
-            self.d_widget = self.Dial(self, self.range, self.notifyChanged)
+            self.d_widget = self.Dial(self, self.range, self.notifyChanged, 
types[astype])
         elif style == "slider":
-            self.d_widget = self.Slider(self, self.range, self.notifyChanged)
+            self.d_widget = self.Slider(self, self.range, self.notifyChanged, 
types[astype])
         elif style == "counter":
             # The counter widget can be directly wired to the notifyChanged 
slot
-            self.d_widget = self.Counter(self, self.range, self.notifyChanged)
+            self.d_widget = self.Counter(self, self.range, self.notifyChanged, 
types[astype])
         else:
             # The CounterSlider needs its own internal handlers before calling 
notifyChanged
-            self.d_widget = self.CounterSlider(self, self.range, 
self.notifyChanged)
+            self.d_widget = self.CounterSlider(self, self.range, 
self.notifyChanged, types[astype])
 
         layout.addWidget(self.d_widget)
         self.setLayout(layout)
 
     class Dial(QtGui.QDial):
         """ Creates the range using a dial """
-        def __init__(self, parent, ranges, slot):
+        def __init__(self, parent, ranges, slot, astype=float):
             QtGui.QDial.__init__(self, parent)
 
+            self.astype = astype
+
             # Setup the dial
             self.setRange(0, ranges.nsteps-1)
             self.setSingleStep(1)
@@ -116,13 +123,15 @@ class RangeWidget(QtGui.QWidget):
         def changed(self, value):
             """ Handles maping the value to the right range before calling the 
slot. """
             val = self.range.map_range(value)
-            self.notifyChanged(val)
+            self.notifyChanged(self.astype(val))
 
     class Slider(QtGui.QSlider):
         """ Creates the range using a slider """
-        def __init__(self, parent, ranges, slot):
+        def __init__(self, parent, ranges, slot, astype=float):
             QtGui.QSlider.__init__(self, QtCore.Qt.Horizontal, parent)
 
+            self.astype = astype
+
             # Setup the slider
             #self.setFocusPolicy(QtCore.Qt.NoFocus)
             self.setRange(0, ranges.nsteps - 1)
@@ -149,7 +158,7 @@ class RangeWidget(QtGui.QWidget):
         def changed(self, value):
             """ Handle the valueChanged signal and map the value into the 
correct range """
             val = self.range.map_range(value)
-            self.notifyChanged(val)
+            self.notifyChanged(self.astype(val))
 
         def mousePressEvent(self, event):
             if((event.button() == QtCore.Qt.LeftButton)):
@@ -168,9 +177,11 @@ class RangeWidget(QtGui.QWidget):
 
     class Counter(QtGui.QDoubleSpinBox):
         """ Creates the range using a counter """
-        def __init__(self, parent, ranges, slot):
+        def __init__(self, parent, ranges, slot, astype=float):
             QtGui.QDoubleSpinBox.__init__(self, parent)
 
+            self.astype = astype
+
             # Setup the counter
             self.setRange(ranges.min, ranges.max)
             self.setValue(ranges.default)
@@ -179,18 +190,25 @@ class RangeWidget(QtGui.QWidget):
             self.setDecimals(ranges.precision)
 
             # The counter already handles floats and can be connected directly.
-            self.valueChanged.connect(slot)
+            self.valueChanged.connect(self.changed)
+            self.notifyChanged = slot
+
+        def changed(self, value):
+            """ Handle the valueChanged signal by converting to the right type 
"""
+            self.notifyChanged(self.astype(value))
 
     class CounterSlider(QtGui.QWidget):
         """ Creates the range using a counter and slider """
-        def __init__(self, parent, ranges, slot):
+        def __init__(self, parent, ranges, slot, astype=float):
             QtGui.QWidget.__init__(self, parent)
 
+            self.astype = astype
+
             # Slot to call in the parent
             self.notifyChanged = slot
 
-            self.slider = RangeWidget.Slider(parent, ranges, 
self.sliderChanged)
-            self.counter = RangeWidget.Counter(parent, ranges, 
self.counterChanged)
+            self.slider = RangeWidget.Slider(parent, ranges, 
self.sliderChanged, astype)
+            self.counter = RangeWidget.Counter(parent, ranges, 
self.counterChanged, astype)
 
             # Need another horizontal layout to wrap the other widgets.
             layout = Qt.QHBoxLayout()
@@ -207,8 +225,8 @@ class RangeWidget(QtGui.QWidget):
             # If the counter was changed, ignore any of these events
             if not self.ignoreSlider:
                 # Value is already float. Just set the counter
-                self.counter.setValue(value)
-                self.notifyChanged(value)
+                self.counter.setValue(self.astype(value))
+                self.notifyChanged(self.astype(value))
             self.ignoreSlider = False
 
         def counterChanged(self, value):
@@ -223,4 +241,4 @@ class RangeWidget(QtGui.QWidget):
                 self.ignoreSlider = True
                 self.slider.setValue(new)
 
-            self.notifyChanged(value)
+            self.notifyChanged(self.astype(value))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]