commit-gnuradio
[Top][All Lists]
Advanced

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

[Commit-gnuradio] r7792 - in grc/branches/grc_reloaded/src/grc: . elemen


From: jblum
Subject: [Commit-gnuradio] r7792 - in grc/branches/grc_reloaded/src/grc: . elements gui/elements platforms/gnuradio_python platforms/gnuradio_python/blocks platforms/gnuradio_python/data wxgui
Date: Fri, 22 Feb 2008 23:22:16 -0700 (MST)

Author: jblum
Date: 2008-02-22 23:22:16 -0700 (Fri, 22 Feb 2008)
New Revision: 7792

Added:
   grc/branches/grc_reloaded/src/grc/blks2/
   grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/data/wx_gui.tmpl
   grc/branches/grc_reloaded/src/grc/usrp/
   grc/branches/grc_reloaded/src/grc/wxgui/
   grc/branches/grc_reloaded/src/grc/wxgui/Controls.py
Modified:
   grc/branches/grc_reloaded/src/grc/elements/Element.py
   grc/branches/grc_reloaded/src/grc/elements/Param.py
   grc/branches/grc_reloaded/src/grc/gui/elements/Param.py
   grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/Generator.py
   grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/Param.py
   
grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/blocks/options.xml
Log:
work with wxgui

Modified: grc/branches/grc_reloaded/src/grc/elements/Element.py
===================================================================
--- grc/branches/grc_reloaded/src/grc/elements/Element.py       2008-02-23 
03:06:22 UTC (rev 7791)
+++ grc/branches/grc_reloaded/src/grc/elements/Element.py       2008-02-23 
06:22:16 UTC (rev 7792)
@@ -43,7 +43,8 @@
                
        def is_valid(self):
                self._error_messages = []#reset err msgs
-               self.validate()
+               try: self.validate()
+               except: pass
                return not self.get_error_messages()
                
        def _add_error_message(self, msg):

Modified: grc/branches/grc_reloaded/src/grc/elements/Param.py
===================================================================
--- grc/branches/grc_reloaded/src/grc/elements/Param.py 2008-02-23 03:06:22 UTC 
(rev 7791)
+++ grc/branches/grc_reloaded/src/grc/elements/Param.py 2008-02-23 06:22:16 UTC 
(rev 7792)
@@ -174,7 +174,12 @@
        
        def get_key(self): return self._key
 
-       def get_value(self): return self._value
+       def get_value(self): 
+               value = self._value
+               if self.is_enum() and value not in self.get_option_keys():
+                       value = self.get_option_keys()[0]
+                       self.set_value(value)
+               return value
        
        def set_value(self, value): self._value = str(value) #must be a string
        

Modified: grc/branches/grc_reloaded/src/grc/gui/elements/Param.py
===================================================================
--- grc/branches/grc_reloaded/src/grc/gui/elements/Param.py     2008-02-23 
03:06:22 UTC (rev 7791)
+++ grc/branches/grc_reloaded/src/grc/gui/elements/Param.py     2008-02-23 
06:22:16 UTC (rev 7792)
@@ -195,7 +195,9 @@
                if self.is_valid():
                        data = self.evaluate()
                        t = self.get_type()
-                       if t in ('file_open', 'file_save'):     #file types
+                       if self.is_enum():
+                               dt_str = 
self.get_option(self.get_value()).get_name()
+                       elif t in ('file_open', 'file_save'):   #file types
                                suggested_length = 30
                                if len(data) <= suggested_length: dt_str = data
                                else: #truncate the tail if there is not enough 
space

Modified: 
grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/Generator.py
===================================================================
--- grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/Generator.py    
2008-02-23 03:06:22 UTC (rev 7791)
+++ grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/Generator.py    
2008-02-23 06:22:16 UTC (rev 7792)
@@ -27,6 +27,8 @@
 
 NO_GUI_TEMPLATE = PATH + '/data/no_gui.tmpl'
 
+WX_GUI_TEMPLATE = PATH + '/data/wx_gui.tmpl'
+
 class Generator(object):
        
        def __init__(self, flow_graph):
@@ -48,14 +50,29 @@
                #separate blocks
                blocks = filter(lambda b: b not in variables and b.get_key() != 
'options', all_blocks)
                blocks = sorted(blocks, lambda x, y: cmp(x.get_id(), 
y.get_id())) 
-               #load the namespace
-               namespace = {
-                       'imports':      imports,
-                       'flow_graph': self._flow_graph,
-                       'variables': variables,
-                       'blocks': blocks,
-                       'connections': self._flow_graph.get_connections(),
-               }
-               #build the template
-               t = Template(open(NO_GUI_TEMPLATE, 'r').read(), namespace)
-               return str(t)           
+               if self._flow_graph.get_option('generate_options') == 'no_gui': 
                
+                       #load the namespace
+                       namespace = {
+                               'imports':      imports,
+                               'flow_graph': self._flow_graph,
+                               'variables': variables,
+                               'blocks': blocks,
+                               'connections': 
self._flow_graph.get_connections(),
+                       }
+                       #build the template
+                       t = Template(open(NO_GUI_TEMPLATE, 'r').read(), 
namespace)
+               elif self._flow_graph.get_option('generate_options') == 
'wx_gui':                       
+                       #load the namespace
+                       namespace = {
+                               'imports':      imports,
+                               'flow_graph': self._flow_graph,
+                               'variables': variables,
+                               'blocks': blocks,
+                               'connections': 
self._flow_graph.get_connections(),
+                       }
+                       imports.add('from grc import wxgui')
+                       #build the template
+                       t = Template(open(WX_GUI_TEMPLATE, 'r').read(), 
namespace)
+               else: print self._flow_graph.get_option('generate_options')
+               return str(t)
+                       

Modified: grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/Param.py
===================================================================
--- grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/Param.py        
2008-02-23 03:06:22 UTC (rev 7791)
+++ grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/Param.py        
2008-02-23 06:22:16 UTC (rev 7792)
@@ -54,7 +54,7 @@
                t = self.get_type()
                v = self.get_value()
                if self.is_enum():
-                       return self.get_option(self.get_value()).get_name()
+                       return self.get_value()
                elif t in ('raw', 'complex', 'real', 'int', 'complex_vector', 
'real_vector', 'int_vector', 'hex'):
                        #raise exception if python cannot evaluate this value
                        try: e = self.get_parent().get_parent().evaluate(v)

Modified: 
grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/blocks/options.xml
===================================================================
--- 
grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/blocks/options.xml  
    2008-02-23 03:06:22 UTC (rev 7791)
+++ 
grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/blocks/options.xml  
    2008-02-23 06:22:16 UTC (rev 7792)
@@ -32,11 +32,11 @@
        <param>
                <name>Generate Options</name>
                <key>generate_options</key>
-               <value>wx</value>
+               <value>wx_gui</value>
                <type>enum</type>
                <option>
                        <name>WX GUI</name>
-                       <key>wx</key>   
+                       <key>wx_gui</key>       
                </option>
                <option>
                        <name>No GUI</name>

Added: 
grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/data/wx_gui.tmpl
===================================================================
--- 
grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/data/wx_gui.tmpl    
                            (rev 0)
+++ 
grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/data/wx_gui.tmpl    
    2008-02-23 06:22:16 UTC (rev 7792)
@@ -0,0 +1,57 @@
+########################################################
+##Cheetah template - gnuradio_python - wx_gui
+##
address@hidden imports the import statements
address@hidden flow_graph the flow_graph
address@hidden variables the variable blocks
address@hidden blocks the signal blocks
address@hidden connections the connections
+########################################################
+#import time
+$('#'*40)
+# Gnuradio Python Flow Graph (wx gui)
+$('# Name: %s'%$flow_graph.get_option('name'))
+$('# Generated: %s'%time.ctime())
+$('#'*40)
+$('"""')
+Description:
+$flow_graph.get_option('description')
+$('"""')
+
+import wx
+#for $imp in $imports
+$imp
+#end for
+
+class Frame(wx.Frame):
+               
+       def __init__(self):
+               
+               wx.Frame.__init__(self, None , -1, 'xxx' + ' - Executing: ' + 
'xxx')
+               self.SetSizeHints(200, 100)
+               main_box = wx.BoxSizer(wx.VERTICAL)
+               self.Bind(wx.EVT_CLOSE, self._quit)     
+               self.SetSizerAndFit(main_box)
+               self.Show()
+                       
+       def _quit(self, event):
+               """Exit the application."""
+               self.Destroy() #destroy the wx frame
+               
+class App(wx.App):
+       
+       def __init__(self):
+               
+               wx.App.__init__(self, 0)        
+                       
+       def OnInit(self):
+               self.SetTopWindow(Frame())
+               return True
+               
+if __name__ == '__main__':     
+       
+       app = App()
+       app.MainLoop()
+       sys.exit(0)
+
+

Added: grc/branches/grc_reloaded/src/grc/wxgui/Controls.py
===================================================================
--- grc/branches/grc_reloaded/src/grc/wxgui/Controls.py                         
(rev 0)
+++ grc/branches/grc_reloaded/src/grc/wxgui/Controls.py 2008-02-23 06:22:16 UTC 
(rev 7792)
@@ -0,0 +1,210 @@
+"""
+Copyright 2008 Free Software Foundation, Inc.
+This file is part of GNU Radio
+
+GNU Radio Companion is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+GNU Radio Companion is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+"""
address@hidden grc.wxgui.Controls 
+#WX control elements like sliders, drop downs, buttons
address@hidden Josh Blum
+
+import wx
+
+class Control(object):
+       """Control base class"""
+       
+       def __init__(self, window, callback):
+               self.window = window
+               self.callback = callback
+               
+       def get_window(self): return self.window
+       
+       def get_callback(self): return self.callback
+       
+       def get_value(self): raise NotImplementedError
+       
+##############################################################################################
+#      Button Control
+##############################################################################################
+class Button(Control, wx.BoxSizer):
+       """House a button for variable control."""
+
+       def __init__(self, window, callback, title, on, on_state, off_state):
+               """!
+               Slider contructor.
+               Create the slider, text box, and label.
+               @param window the wx parent window
+               @param callback call the callback on changes
+               @param title the label title
+               @param on true if the button starts on
+               @param on_state on state
+               @param off_state off state
+               """
+               #initialize
+               Control.__init__(self, window, callback)
+               self.state = bool(on)
+               self.on_state = on_state
+               self.off_state = off_state
+               wx.BoxSizer.__init__(self, wx.VERTICAL)
+               #create label
+               label = wx.StaticText(title)
+               self.Add(label, 0, wx.ALIGN_CENTER)
+               #create button
+               self.button = wx.Button(self.get_window(), -1, '')
+               self.Bind(wx.EVT_CHOICE, self._handle_changed)
+               self._handle_changed()
+  
+       def _handle_changed(self, event=None):
+               """!
+               A change is detected. Call the callback.
+               """
+               self.state = not self.state
+               self.button.SetLabel(str(self.get_value())[0:11])
+               try: self.get_callback()()
+               except Exception, e: print 'Error in handle change: "%s".'%e    
        
+
+       def get_value(self):
+               """!
+               Use the state of the button to determine the value.
+               @return one of the possible states
+               """
+               if self.state: return self.on_state
+               else: return self.off_state
+
+##############################################################################################
+#      Drop Down Control
+##############################################################################################
+class DropDown(Control, wx.BoxSizer):
+       """House a drop down for variable control."""
+
+       def __init__(self, window, callback, title, index, choices):
+               """!
+               Slider contructor.
+               Create the slider, text box, and label.
+               @param window the wx parent window
+               @param callback call the callback on changes
+               @param title the label title
+               @param index the default choice index
+               @param choices a list of choices
+               """
+               #initialize
+               Control.__init__(self, window, callback)
+               self.choices = choices
+               wx.BoxSizer.__init__(self, wx.VERTICAL)
+               #create label
+               label = wx.StaticText(title)
+               self.Add(label, 0, wx.ALIGN_CENTER)
+               #create chooser
+               self.chooser = wx.Choice.(self.get_window(), -1, choices)
+               self.Add(self.chooser, 0, wx.ALIGN_CENTER)
+               self.Bind(wx.EVT_CHOICE, self._handle_changed)
+               self.chooser.SetSelection(index)
+  
+       def _handle_changed(self, event=None):
+               """!
+               A change is detected. Call the callback.
+               """
+               try: self.get_callback()()
+               except Exception, e: print 'Error in handle change: "%s".'%e    
        
+
+       def get_value(self):
+               """!
+               Get the value from the drop down.
+               Read the selected index and parse the choice.
+               @return one of the possible choices
+               """
+               selection = chooser.GetSelection()
+               return self.choices[selection]
+
+##############################################################################################
+#      Slider Control
+##############################################################################################
+class Slider(Control, wx.BoxSizer):
+       """House a Slider and a Text Box for variable control."""
+       
+       def __init__(self, window, callback, title, value, min, max, num_steps):
+               """!
+               Slider contructor.
+               Create the slider, text box, and label.
+               @param window the wx parent window
+               @param callback call the callback on changes
+               @param title the label title
+               @param value the default value
+               @param min the min
+               @param max the max
+               @param num_steps the number of steps
+               """
+               #initialize
+               Control.__init__(self, window, callback)
+               self.min = min
+               self.max = max
+               self.num_steps = num_steps
+               #create gui elements
+               wx.BoxSizer.__init__(self, wx.VERTICAL)
+               label_text_sizer = wx.BoxSizer(wx.HORIZONTAL) #label and text 
box container 
+               label = wx.StaticText(self.get_window(), -1, title + ' -> ')
+               self.text_box = text_box = wx.TextCtrl(self.get_window(), -1, 
value, style=wx.TE_PROCESS_ENTER)
+               text_box.Bind(wx.EVT_TEXT_ENTER, self._handle_enter)    #bind 
this special enter hotkey event
+               for obj in (label, text_box):   #fill the container with label 
and text entry box
+                       label_text_sizer.Add(obj, 0, 
wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
+               self.Add(label_text_sizer, 0, wx.ALIGN_CENTER)
+               #make the slider
+               self.slider = slider = wx.Slider(self.get_window(), -1, 
size=wx.Size(slider_width, SLIDER_HEIGHT), style=wx.SL_HORIZONTAL)              
                
+               try: slider.SetRange(0, num_steps)
+               except Exception, e:
+                       sys.stderr.write('Slider, "%s", has too many 
steps!\n'%id)
+                       sys.exit(-1)
+               slider.Bind(wx.EVT_SCROLL, self._handle_scroll) #bind the 
scrolling event               
+               self.Add(slider, 0, wx.ALIGN_CENTER)
+               self._set_slider_value(value)#sets the slider's value
+                       
+       def get_value(self):
+               """!
+               Read the value from the slider and,  
+               translate the slider value into a real numerical value.
+               @return the numeric representation of the slider
+               """
+               slider_value = self.slider.GetValue()                   
+               return 
str(slider_value*(float(self.max)-float(self.min))/self.num_steps + 
float(self.min))
+               
+       def _set_slider_value(self, real_value):
+               """!
+               Translate the real numerical value into a slider value and,  
+               write the value to the slider.
+               @param real_value the numeric value the slider should represent
+               """             
+               slider_value = 
(float(real_value)-float(self.min))*self.num_steps/(float(self.max)-float(self.min))
+               self.slider.SetValue(slider_value)
+       
+       def _handle_scroll(self, event=None):
+               """!
+               A scroll event is detected. Read the slider, call the callback.
+               """
+               new_value = str(self._get_slider_value())       #the new value 
must be a string!
+               try: self.get_callback()()
+               except Exception, e: print 'Error in handle scroll: "%s".'%e 
+               self._set_slider_value(new_value)
+               self.text_box.SetValue(new_value)
+               
+       def _handle_enter(self, event=None):
+               """!
+               An enter key was pressed. Read the text box, call the callback.
+               """     
+               new_value = str(self.text_box.GetValue())       #the new value 
must be a string!        
+               try: self.get_callback()()
+               except Exception, e: print 'Error in handle enter: "%s".'%e 
+               self._set_slider_value(new_value)
+               self.text_box.SetValue(new_value)
+       





reply via email to

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