Changeset 8934
- Timestamp:
- 07/18/08 00:49:02
- Files:
-
- grc/trunk/notes/todo.txt (modified) (1 diff)
- grc/trunk/src/grc_gnuradio/FlowGraph.py (modified) (1 diff)
- grc/trunk/src/grc_gnuradio/blocks/variables/variable_chooser.xml (modified) (1 diff)
- grc/trunk/src/grc_gnuradio/blocks/variables/variable_slider.xml (modified) (2 diffs)
- grc/trunk/src/grc_gnuradio/blocks/variables/variable_text_box.xml (copied) (copied from grc/trunk/src/grc_gnuradio/blocks/variables/variable_slider.xml) (4 diffs)
- grc/trunk/src/grc_gnuradio/data/block_tree.xml (modified) (1 diff)
- grc/trunk/src/grc_gnuradio/wxgui/__init__.py (modified) (1 diff)
- grc/trunk/src/grc_gnuradio/wxgui/callback_controls.py (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
grc/trunk/notes/todo.txt
r8863 r8934 15 15 -dtd for external blocks 16 16 -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 18 21 19 22 ############ Suggestions: #################### grc/trunk/src/grc_gnuradio/FlowGraph.py
r8910 r8934 81 81 @return a sorted list of variable blocks 82 82 """ 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()) 84 84 #map var id to variable block 85 85 id2var = dict([(var.get_id(), var) for var in variables]) grc/trunk/src/grc_gnuradio/blocks/variables/variable_chooser.xml
r8865 r8934 16 16 label=$label, 17 17 #else 18 label= "$id",18 label="$id", 19 19 #end if 20 20 index=$value_index, grc/trunk/src/grc_gnuradio/blocks/variables/variable_slider.xml
r8865 r8934 3 3 <!-- 4 4 ################################################### 5 ##Variable block: a grc variable with key, value, min, max, step5 ##Variable Slider: a grc variable with key, value, min, max, step 6 6 ################################################### 7 7 --> … … 16 16 label=$label, 17 17 #else 18 label= "$id",18 label="$id", 19 19 #end if 20 20 value=$id, grc/trunk/src/grc_gnuradio/blocks/variables/variable_text_box.xml
r8924 r8934 3 3 <!-- 4 4 ################################################### 5 ##Variable block: a grc variable with key, value, min, max, step5 ##Variable Text Box: a grc variable with key, value 6 6 ################################################### 7 7 --> 8 8 <block> 9 <name>Variable Slider</name>10 <key>variable_ slider</key>9 <name>Variable Text Box</name> 10 <key>variable_text_box</key> 11 11 <make>$value 12 _$(id)_control = grc_wxgui. slider_$(slider_type)_control(12 _$(id)_control = grc_wxgui.text_box_control( 13 13 window=self.GetWin(), 14 14 callback=self.set_$(id), … … 16 16 label=$label, 17 17 #else 18 label= "$id",18 label="$id", 19 19 #end if 20 20 value=$id, 21 min=$min,22 max=$max,23 num_steps=$num_steps,24 21 ) 25 22 #set $grid_pos = $grid_pos.eval … … 38 35 <name>Default Value</name> 39 36 <key>value</key> 40 <value>50</value>41 <type>real</type>42 </param>43 <param>44 <name>Minimum</name>45 <key>min</key>46 37 <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> 74 39 </param> 75 40 <param> … … 79 44 <type>grid_pos</type> 80 45 </param> 81 <check>$min <= $value <= $max</check>82 <check>$min < $max</check>83 <check>0 < $num_steps <= 1000</check>84 46 <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. 47 This block creates a variable with a text box. \ 48 Leave the label blank to use the variable id as the label. 90 49 91 50 Use 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 240 240 <block>variable_slider</block> 241 241 <block>variable_chooser</block> 242 <block>variable_text_box</block> 242 243 <block>variable_sink</block> 243 244 </cat> grc/trunk/src/grc_gnuradio/wxgui/__init__.py
r8580 r8934 25 25 radio_buttons_vertical_control, \ 26 26 slider_horizontal_control, \ 27 slider_vertical_control 27 slider_vertical_control, \ 28 text_box_control 28 29 from top_block_gui import top_block_gui 29 30 grc/trunk/src/grc_gnuradio/wxgui/callback_controls.py
r8580 r8934 1 1 # Copyright 2008 Free Software Foundation, Inc. 2 # 2 # 3 3 # This file is part of GNU Radio 4 # 4 # 5 5 # GNU Radio is free software; you can redistribute it and/or modify 6 6 # it under the terms of the GNU General Public License as published by 7 7 # the Free Software Foundation; either version 3, or (at your option) 8 8 # any later version. 9 # 9 # 10 10 # GNU Radio is distributed in the hope that it will be useful, 11 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 13 # GNU General Public License for more details. 14 # 14 # 15 15 # You should have received a copy of the GNU General Public License 16 16 # along with GNU Radio; see the file COPYING. If not, write to 17 17 # the Free Software Foundation, Inc., 51 Franklin Street, 18 18 # Boston, MA 02110-1301, USA. 19 # 19 # 20 20 21 21 import wx … … 24 24 class LabelText(wx.StaticText): 25 25 """Label text class for uniform labels among all controls.""" 26 26 27 27 def __init__(self, window, label): 28 28 wx.StaticText.__init__(self, window, -1, str(label)) 29 29 font = self.GetFont() 30 30 font.SetWeight(wx.FONTWEIGHT_BOLD) 31 self.SetFont(font) 31 self.SetFont(font) 32 32 33 33 class _control_base(wx.BoxSizer): 34 34 """Control base class""" 35 35 36 36 def __init__(self, window, callback): 37 37 self.window = window 38 38 self.callback = callback 39 wx.BoxSizer.__init__(self, wx.VERTICAL) 40 39 wx.BoxSizer.__init__(self, wx.VERTICAL) 40 41 41 def get_window(self): return self.window 42 42 43 43 def call(self): return self.callback(self.get_value()) 44 44 45 45 def get_value(self): raise NotImplementedError 46 46 47 47 class _chooser_control_base(_control_base): 48 48 """House a drop down or radio buttons for variable control.""" … … 66 66 self.choices = choices 67 67 self.labels = map(str, labels or choices) 68 self._init() 69 68 self._init() 69 70 70 def _handle_changed(self, event=None): 71 71 """! … … 73 73 """ 74 74 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 76 76 77 77 def get_value(self): … … 81 81 """ 82 82 self._update() 83 return self.choices[self.index] 83 return self.choices[self.index] 84 84 85 85 ############################################################################################## … … 93 93 self.button.Bind(wx.EVT_BUTTON, self._handle_changed) 94 94 self.Add(self.button, 0, wx.ALIGN_CENTER) 95 95 96 96 def _update(self): 97 97 self.index = (self.index + 1)%len(self.choices) #circularly increment index … … 107 107 self.drop_down = wx.Choice(self.get_window(), -1, choices=self.labels) 108 108 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) 110 110 self.drop_down.SetSelection(self.index) 111 111 112 112 def _update(self): 113 113 self.index = self.drop_down.GetSelection() … … 135 135 #set one radio button active 136 136 self.radio_buttons[self.index].SetValue(True) 137 137 138 138 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] 140 140 self.index = self.radio_buttons.index(selected_radio_button) 141 141 … … 152 152 class _slider_control_base(_control_base): 153 153 """House a Slider and a Text Box for variable control.""" 154 154 155 155 def __init__(self, window, callback, label='Label', value=50, min=0, max=100, num_steps=100): 156 156 """! … … 171 171 self.num_steps = int(num_steps) 172 172 #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)) 175 175 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 event177 for obj in (label_text, text_box): #fill the container with label and text entry box176 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 178 178 label_text_sizer.Add(obj, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL) 179 179 self.Add(label_text_sizer, 0, wx.ALIGN_CENTER) 180 180 #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) 182 182 try: slider.SetRange(0, num_steps) 183 183 except Exception, e: 184 184 print >> sys.stderr, 'Error in set slider range: "%s".'%e 185 185 sys.exit(-1) 186 slider.Bind(wx.EVT_SCROLL, self._handle_scroll) #bind the scrolling event186 slider.Bind(wx.EVT_SCROLL, self._handle_scroll) #bind the scrolling event 187 187 self.Add(slider, 0, wx.ALIGN_CENTER) 188 188 #init slider and text box 189 189 self._value = value 190 self._set_slider_value(self._value) #sets the slider's value190 self._set_slider_value(self._value) #sets the slider's value 191 191 self.text_box.SetValue(str(self._value)) 192 192 193 193 def get_value(self): 194 194 """! … … 197 197 """ 198 198 return self._value 199 199 200 200 def _set_slider_value(self, real_value): 201 201 """! 202 Translate the real numerical value into a slider value and, 202 Translate the real numerical value into a slider value and, 203 203 write the value to the slider. 204 204 @param real_value the numeric value the slider should represent 205 """ 205 """ 206 206 slider_value = (float(real_value) - self.min)*self.num_steps/(self.max - self.min) 207 207 self.slider.SetValue(slider_value) 208 208 209 209 def _handle_scroll(self, event=None): 210 210 """! 211 211 A scroll event is detected. Read the slider, call the callback. 212 212 """ 213 slider_value = self.slider.GetValue() 213 slider_value = self.slider.GetValue() 214 214 new_value = slider_value*(self.max - self.min)/self.num_steps + self.min 215 215 self.text_box.SetValue(str(new_value)) … … 217 217 try: self.call() 218 218 except Exception, e: print >> sys.stderr, 'Error in exec callback from handle scroll.\n', e 219 219 220 220 def _handle_enter(self, event=None): 221 221 """! 222 222 An enter key was pressed. Read the text box, call the callback. 223 """ 223 """ 224 224 new_value = float(self.text_box.GetValue()) 225 225 self._set_slider_value(new_value) … … 227 227 try: self.call() 228 228 except Exception, e: print >> sys.stderr, 'Error in exec callback from handle enter.\n', e 229 229 230 230 class slider_horizontal_control(_slider_control_base): 231 231 label_text_orientation = wx.HORIZONTAL 232 232 slider_style = wx.SL_HORIZONTAL 233 233 slider_size = 200, 20 234 ptr = ' -> '235 234 class slider_vertical_control(_slider_control_base): 236 235 label_text_orientation = wx.VERTICAL 237 236 slider_style = wx.SL_VERTICAL 238 237 slider_size = 20, 200 239 ptr = '' 238 239 ############################################################################################## 240 # Text Box Control 241 ############################################################################################## 242 class 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
