Changeset 8934

Show
Ignore:
Timestamp:
07/18/08 00:49:02
Author:
jblum
Message:

text box variable control

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • grc/trunk/notes/todo.txt

    r8863 r8934  
    1515-dtd for external blocks 
    1616-hotkeys in action descriptions 
    17 -switch to generation of top_block and hier_block2 classes 
     17-variables dependent on variables that change 
     18-text box gui control 
     19-log slider gui control 
     20-remove dtd from all block wrappers, setup dtd in platform 
    1821 
    1922############    Suggestions:    #################### 
  • grc/trunk/src/grc_gnuradio/FlowGraph.py

    r8910 r8934  
    8181                @return a sorted list of variable blocks 
    8282                """ 
    83                 variables = filter(lambda b: b.get_key() in ('variable', 'variable_slider', 'variable_chooser'), self.get_blocks()) 
     83                variables = filter(lambda b: b.get_key() in ('variable', 'variable_slider', 'variable_chooser', 'variable_text_box'), self.get_blocks()) 
    8484                #map var id to variable block 
    8585                id2var = dict([(var.get_id(), var) for var in variables]) 
  • grc/trunk/src/grc_gnuradio/blocks/variables/variable_chooser.xml

    r8865 r8934  
    1616        label=$label, 
    1717                #else 
    18         label="$id",  
     18        label="$id",  
    1919                #end if 
    2020        index=$value_index, 
  • grc/trunk/src/grc_gnuradio/blocks/variables/variable_slider.xml

    r8865 r8934  
    33<!--  
    44################################################### 
    5 ##Variable block: a grc variable with key, value, min, max, step 
     5##Variable Slider: a grc variable with key, value, min, max, step 
    66################################################### 
    77 --> 
     
    1616        label=$label, 
    1717                #else 
    18         label="$id",  
     18        label=&quot;$id&quot;,  
    1919                #end if 
    2020        value=$id, 
  • grc/trunk/src/grc_gnuradio/blocks/variables/variable_text_box.xml

    r8924 r8934  
    33<!--  
    44################################################### 
    5 ##Variable block: a grc variable with key, value, min, max, step 
     5##Variable Text Box: a grc variable with key, value 
    66################################################### 
    77 --> 
    88<block> 
    9         <name>Variable Slider</name> 
    10         <key>variable_slider</key> 
     9        <name>Variable Text Box</name> 
     10        <key>variable_text_box</key> 
    1111        <make>$value 
    12 _$(id)_control = grc_wxgui.slider_$(slider_type)_control( 
     12_$(id)_control = grc_wxgui.text_box_control( 
    1313        window=self.GetWin(),  
    1414        callback=self.set_$(id), 
     
    1616        label=$label, 
    1717                #else 
    18         label="$id",  
     18        label=&quot;$id&quot;,  
    1919                #end if 
    2020        value=$id, 
    21         min=$min, 
    22         max=$max, 
    23         num_steps=$num_steps, 
    2421) 
    2522#set $grid_pos = $grid_pos.eval 
     
    3835                <name>Default Value</name> 
    3936                <key>value</key> 
    40                 <value>50</value> 
    41                 <type>real</type> 
    42         </param> 
    43         <param> 
    44                 <name>Minimum</name> 
    45                 <key>min</key> 
    4637                <value>0</value> 
    47                 <type>real</type> 
    48         </param> 
    49         <param> 
    50                 <name>Maximum</name> 
    51                 <key>max</key> 
    52                 <value>100</value> 
    53                 <type>real</type> 
    54         </param> 
    55         <param> 
    56                 <name>Num Steps</name> 
    57                 <key>num_steps</key> 
    58                 <value>100</value> 
    59                 <type>int</type> 
    60         </param> 
    61                 <param> 
    62                 <name>Slider Type</name> 
    63                 <key>slider_type</key> 
    64                 <value>horizontal</value> 
    65                 <type>enum</type> 
    66                 <option> 
    67                         <name>Horizontal</name> 
    68                         <key>horizontal</key> 
    69                 </option> 
    70                 <option> 
    71                         <name>Vertical</name> 
    72                         <key>vertical</key> 
    73                 </option> 
     38                <type>raw</type> 
    7439        </param> 
    7540        <param> 
     
    7944                <type>grid_pos</type> 
    8045        </param> 
    81         <check>$min &lt;= $value &lt;= $max</check> 
    82         <check>$min &lt; $max</check> 
    83         <check>0 &lt; $num_steps &lt;= 1000</check> 
    8446        <doc> 
    85 This block creates a variable with a slider. \ 
    86 Leave the label blank to use the variable id as the label. \ 
    87 The value must be a real number. \ 
    88 The value must be between the minimum and the maximum. \ 
    89 The number of steps must be between 0 and 1000. 
     47This block creates a variable with a text box. \ 
     48Leave the label blank to use the variable id as the label. 
    9049 
    9150Use the Grid Position (row, column, row span, column span) to position the graphical element in the window. 
  • grc/trunk/src/grc_gnuradio/data/block_tree.xml

    r8807 r8934  
    240240                <block>variable_slider</block> 
    241241                <block>variable_chooser</block> 
     242                <block>variable_text_box</block> 
    242243                <block>variable_sink</block> 
    243244        </cat> 
  • grc/trunk/src/grc_gnuradio/wxgui/__init__.py

    r8580 r8934  
    2525        radio_buttons_vertical_control, \ 
    2626        slider_horizontal_control, \ 
    27         slider_vertical_control  
     27        slider_vertical_control, \ 
     28        text_box_control 
    2829from top_block_gui import top_block_gui 
    2930 
  • grc/trunk/src/grc_gnuradio/wxgui/callback_controls.py

    r8580 r8934  
    11# Copyright 2008 Free Software Foundation, Inc. 
    2 #  
     2# 
    33# This file is part of GNU Radio 
    4 #  
     4# 
    55# GNU Radio is free software; you can redistribute it and/or modify 
    66# it under the terms of the GNU General Public License as published by 
    77# the Free Software Foundation; either version 3, or (at your option) 
    88# any later version. 
    9 #  
     9# 
    1010# GNU Radio is distributed in the hope that it will be useful, 
    1111# but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1212# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1313# GNU General Public License for more details. 
    14 #  
     14# 
    1515# You should have received a copy of the GNU General Public License 
    1616# along with GNU Radio; see the file COPYING.  If not, write to 
    1717# the Free Software Foundation, Inc., 51 Franklin Street, 
    1818# Boston, MA 02110-1301, USA. 
    19 #  
     19# 
    2020 
    2121import wx 
     
    2424class LabelText(wx.StaticText): 
    2525        """Label text class for uniform labels among all controls.""" 
    26          
     26 
    2727        def __init__(self, window, label): 
    2828                wx.StaticText.__init__(self, window, -1, str(label)) 
    2929                font = self.GetFont() 
    3030                font.SetWeight(wx.FONTWEIGHT_BOLD) 
    31                 self.SetFont(font)      
     31                self.SetFont(font) 
    3232 
    3333class _control_base(wx.BoxSizer): 
    3434        """Control base class""" 
    35          
     35 
    3636        def __init__(self, window, callback): 
    3737                self.window = window 
    3838                self.callback = callback 
    39                 wx.BoxSizer.__init__(self, wx.VERTICAL)                         
    40                  
     39                wx.BoxSizer.__init__(self, wx.VERTICAL) 
     40 
    4141        def get_window(self): return self.window 
    42          
     42 
    4343        def call(self): return self.callback(self.get_value()) 
    44          
     44 
    4545        def get_value(self): raise NotImplementedError 
    46          
     46 
    4747class _chooser_control_base(_control_base): 
    4848        """House a drop down or radio buttons for variable control.""" 
     
    6666                self.choices = choices 
    6767                self.labels = map(str, labels or choices) 
    68                 self._init()            
    69    
     68                self._init() 
     69 
    7070        def _handle_changed(self, event=None): 
    7171                """! 
     
    7373                """ 
    7474                try: self.call() 
    75                 except Exception, e: print >> sys.stderr, 'Error in exec callback from handle changed.\n', e    
     75                except Exception, e: print >> sys.stderr, 'Error in exec callback from handle changed.\n', e 
    7676 
    7777        def get_value(self): 
     
    8181                """ 
    8282                self._update() 
    83                 return self.choices[self.index]                 
     83                return self.choices[self.index] 
    8484 
    8585############################################################################################## 
     
    9393                self.button.Bind(wx.EVT_BUTTON, self._handle_changed) 
    9494                self.Add(self.button, 0, wx.ALIGN_CENTER) 
    95                  
     95 
    9696        def _update(self): 
    9797                self.index = (self.index + 1)%len(self.choices) #circularly increment index 
     
    107107                self.drop_down = wx.Choice(self.get_window(), -1, choices=self.labels) 
    108108                self.Add(self.drop_down, 0, wx.ALIGN_CENTER) 
    109                 self.drop_down.Bind(wx.EVT_CHOICE, self._handle_changed)                
     109                self.drop_down.Bind(wx.EVT_CHOICE, self._handle_changed) 
    110110                self.drop_down.SetSelection(self.index) 
    111                  
     111 
    112112        def _update(self): 
    113113                self.index = self.drop_down.GetSelection() 
     
    135135                #set one radio button active 
    136136                self.radio_buttons[self.index].SetValue(True) 
    137                  
     137 
    138138        def _update(self): 
    139                 selected_radio_button = filter(lambda rb: rb.GetValue(), self.radio_buttons)[0]                                 
     139                selected_radio_button = filter(lambda rb: rb.GetValue(), self.radio_buttons)[0] 
    140140                self.index = self.radio_buttons.index(selected_radio_button) 
    141141 
     
    152152class _slider_control_base(_control_base): 
    153153        """House a Slider and a Text Box for variable control.""" 
    154          
     154 
    155155        def __init__(self, window, callback, label='Label', value=50, min=0, max=100, num_steps=100): 
    156156                """! 
     
    171171                self.num_steps = int(num_steps) 
    172172                #create gui elements 
    173                 label_text_sizer = wx.BoxSizer(self.label_text_orientation) #label and text box container  
    174                 label_text = LabelText(self.get_window(), '%s%s'%(str(label), self.ptr)) 
     173                label_text_sizer = wx.BoxSizer(self.label_text_orientation) #label and text box container 
     174                label_text = LabelText(self.get_window(), '%s: '%str(label)) 
    175175                self.text_box = text_box = wx.TextCtrl(self.get_window(), -1, str(value), style=wx.TE_PROCESS_ENTER) 
    176                 text_box.Bind(wx.EVT_TEXT_ENTER, self._handle_enter)   #bind this special enter hotkey event 
    177                 for obj in (label_text, text_box):     #fill the container with label and text entry box 
     176                text_box.Bind(wx.EVT_TEXT_ENTER, self._handle_enter) #bind this special enter hotkey event 
     177                for obj in (label_text, text_box): #fill the container with label and text entry box 
    178178                        label_text_sizer.Add(obj, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL) 
    179179                self.Add(label_text_sizer, 0, wx.ALIGN_CENTER) 
    180180                #make the slider 
    181                 self.slider = slider = wx.Slider(self.get_window(), -1, size=wx.Size(*self.slider_size), style=self.slider_style)                               
     181                self.slider = slider = wx.Slider(self.get_window(), -1, size=wx.Size(*self.slider_size), style=self.slider_style) 
    182182                try: slider.SetRange(0, num_steps) 
    183183                except Exception, e: 
    184184                        print >> sys.stderr, 'Error in set slider range: "%s".'%e 
    185185                        sys.exit(-1) 
    186                 slider.Bind(wx.EVT_SCROLL, self._handle_scroll)        #bind the scrolling event                
     186                slider.Bind(wx.EVT_SCROLL, self._handle_scroll) #bind the scrolling event 
    187187                self.Add(slider, 0, wx.ALIGN_CENTER) 
    188188                #init slider and text box 
    189189                self._value = value 
    190                 self._set_slider_value(self._value)#sets the slider's value 
     190                self._set_slider_value(self._value) #sets the slider's value 
    191191                self.text_box.SetValue(str(self._value)) 
    192                          
     192 
    193193        def get_value(self): 
    194194                """! 
     
    197197                """ 
    198198                return self._value 
    199                  
     199 
    200200        def _set_slider_value(self, real_value): 
    201201                """! 
    202                 Translate the real numerical value into a slider value and,   
     202                Translate the real numerical value into a slider value and, 
    203203                write the value to the slider. 
    204204                @param real_value the numeric value the slider should represent 
    205                 """             
     205                """ 
    206206                slider_value = (float(real_value) - self.min)*self.num_steps/(self.max - self.min) 
    207207                self.slider.SetValue(slider_value) 
    208          
     208 
    209209        def _handle_scroll(self, event=None): 
    210210                """! 
    211211                A scroll event is detected. Read the slider, call the callback. 
    212212                """ 
    213                 slider_value = self.slider.GetValue()                   
     213                slider_value = self.slider.GetValue() 
    214214                new_value = slider_value*(self.max - self.min)/self.num_steps + self.min 
    215215                self.text_box.SetValue(str(new_value)) 
     
    217217                try: self.call() 
    218218                except Exception, e: print >> sys.stderr, 'Error in exec callback from handle scroll.\n', e 
    219                  
     219 
    220220        def _handle_enter(self, event=None): 
    221221                """! 
    222222                An enter key was pressed. Read the text box, call the callback. 
    223                 """     
     223                """ 
    224224                new_value = float(self.text_box.GetValue()) 
    225225                self._set_slider_value(new_value) 
     
    227227                try: self.call() 
    228228                except Exception, e: print >> sys.stderr, 'Error in exec callback from handle enter.\n', e 
    229          
     229 
    230230class slider_horizontal_control(_slider_control_base): 
    231231        label_text_orientation = wx.HORIZONTAL 
    232232        slider_style = wx.SL_HORIZONTAL 
    233233        slider_size = 200, 20 
    234         ptr = ' -> ' 
    235234class slider_vertical_control(_slider_control_base): 
    236235        label_text_orientation = wx.VERTICAL 
    237236        slider_style = wx.SL_VERTICAL 
    238237        slider_size = 20, 200 
    239         ptr = '' 
     238 
     239############################################################################################## 
     240#       Text Box Control 
     241############################################################################################## 
     242class text_box_control(_control_base): 
     243        """House a Text Box for variable control.""" 
     244 
     245        def __init__(self, window, callback, label='Label', value=50): 
     246                """! 
     247                Text box contructor. 
     248                Create the text box, and label. 
     249                @param window the wx parent window 
     250                @param callback call the callback on changes 
     251                @param label the label title 
     252                @param value the default value 
     253                """ 
     254                #initialize 
     255                _control_base.__init__(self, window, callback) 
     256                #create gui elements 
     257                label_text_sizer = wx.BoxSizer(wx.HORIZONTAL) #label and text box container 
     258                label_text = LabelText(self.get_window(), '%s: '%str(label)) 
     259                self.text_box = text_box = wx.TextCtrl(self.get_window(), -1, str(value), style=wx.TE_PROCESS_ENTER) 
     260                text_box.Bind(wx.EVT_TEXT_ENTER, self._handle_enter) #bind this special enter hotkey event 
     261                for obj in (label_text, text_box): #fill the container with label and text entry box 
     262                        label_text_sizer.Add(obj, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL) 
     263                self.Add(label_text_sizer, 0, wx.ALIGN_CENTER) 
     264                self.text_box.SetValue(str(value)) 
     265 
     266        def get_value(self): 
     267                """! 
     268                Get the current set value. 
     269                @return the value (float) 
     270                """ 
     271                return self._value 
     272 
     273        def _handle_enter(self, event=None): 
     274                """! 
     275                An enter key was pressed. Read the text box, call the callback. 
     276                If the text cannot be evaluated, do not try callback. 
     277                """ 
     278                try: self._value = eval(self.text_box.GetValue()) 
     279                except: return 
     280                try: self.call() 
     281                except Exception, e: print >> sys.stderr, 'Error in exec callback from handle enter.\n', e