commit-gnuradio
[Top][All Lists]
Advanced

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

[Commit-gnuradio] r10822 - gnuradio/branches/developers/jblum/grc/grc/sr


From: jblum
Subject: [Commit-gnuradio] r10822 - gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui
Date: Mon, 13 Apr 2009 22:19:14 -0600 (MDT)

Author: jblum
Date: 2009-04-13 22:19:13 -0600 (Mon, 13 Apr 2009)
New Revision: 10822

Modified:
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Block.py
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Constants.py
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Param.py
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Port.py
   gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Utils.py
Log:
Cleaned up some string formatting code by replacing it with templates.



Modified: gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Block.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Block.py       
2009-04-13 23:19:59 UTC (rev 10821)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Block.py       
2009-04-14 04:19:13 UTC (rev 10822)
@@ -23,14 +23,17 @@
 from ... utils import odict
 from ... gui.Constants import BORDER_PROXIMITY_SENSITIVITY
 from Constants import \
-       BLOCK_FONT, BLOCK_LABEL_PADDING, \
+       BLOCK_LABEL_PADDING, \
        PORT_SEPARATION, LABEL_SEPARATION, \
        PORT_BORDER_SEPARATION, POSSIBLE_ROTATIONS
 import pygtk
 pygtk.require('2.0')
 import gtk
-import pango
 
+BLOCK_MARKUP_TMPL="""\
+#set $foreground = $block.is_valid() and 'black' or 'red'
+<span foreground="$foreground" font_desc="Sans 
8"><b>$encode($block.get_name())</b></span>"""
+
 class Block(Element):
        """The graphical signal block."""
 
@@ -131,10 +134,7 @@
                #create the main layout
                layout = gtk.DrawingArea().create_pango_layout('')
                layouts.append(layout)
-               if self.is_valid():     
layout.set_markup('<b>'+Utils.xml_encode(self.get_name())+'</b>')
-               else: layout.set_markup('<span 
foreground="red"><b>'+Utils.xml_encode(self.get_name())+'</b></span>')
-               desc = pango.FontDescription(BLOCK_FONT)
-               layout.set_font_description(desc)
+               layout.set_markup(Utils.parse_template(BLOCK_MARKUP_TMPL, 
block=self))
                self.label_width, self.label_height = layout.get_pixel_size()
                #display the params
                for param in filter(lambda p: p.get_hide() not in ('all', 
'part'), self.get_params()):

Modified: 
gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Constants.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Constants.py   
2009-04-13 23:19:59 UTC (rev 10821)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Constants.py   
2009-04-14 04:19:13 UTC (rev 10822)
@@ -1,24 +1,22 @@
-#
-# Copyright 2008 Free Software Foundation, Inc.
-#
-# This file is part of GNU Radio
-#
-# GNU Radio 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 3, or (at your option)
-# any later version.
-#
-# GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
-# the Free Software Foundation, Inc., 51 Franklin Street,
-# Boston, MA 02110-1301, USA.
-#
+"""
+Copyright 2008, 2009 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
+"""
+
 #label constraint dimensions
 LABEL_SEPARATION = 3
 BLOCK_LABEL_PADDING = 7
@@ -27,11 +25,6 @@
 PORT_SEPARATION = 17
 PORT_BORDER_SEPARATION = 9
 PORT_MIN_WIDTH = 20
-#fonts
-PARAM_LABEL_FONT = 'Sans 9.5'
-PARAM_FONT = 'Sans 7.5'
-BLOCK_FONT = 'Sans 8'
-PORT_FONT = 'Sans 7.5'
 #minimal length of connector
 CONNECTOR_EXTENSION_MINIMAL = 11
 #increment length for connector

Modified: gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Param.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Param.py       
2009-04-13 23:19:59 UTC (rev 10821)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Param.py       
2009-04-14 04:19:13 UTC (rev 10822)
@@ -22,9 +22,31 @@
 import pygtk
 pygtk.require('2.0')
 import gtk
-import pango
-from Constants import PARAM_LABEL_FONT, PARAM_FONT
 
+PARAM_MARKUP_TMPL="""\
+#set $foreground = $param.is_valid() and 'black' or 'red'
+#set $value = $param.is_valid() and repr($param) or 'error'
+<span foreground="$foreground" font_desc="Sans 
7.5"><b>$encode($param.get_name()): </b>$encode($value)</span>"""
+
+PARAM_LABEL_MARKUP_TMPL="""\
+#set $foreground = $param.is_valid() and 'black' or 'red'
+#set $underline = $has_cb and 'low' or 'none'
+<span underline="$underline" foreground="$foreground" font_desc="Sans 
9.5"><b>$encode($param.get_name())</b></span>"""
+
+TIP_MARKUP_TMPL="""\
+Key: $param.get_key()
+Type: $param.get_type()
+#if $param.is_valid()
+Value: $param.evaluate()
+#elif len($param.get_error_messages()) == 1
+Error: $(param.get_error_messages()[0])
+#else
+Error:
+       #for $error_msg in $param.get_error_messages()
+ * $error_msg
+       #end for
+#end if"""
+
 class Param(Element):
        """The graphical parameter."""
 
@@ -53,21 +75,11 @@
                Finish by calling the exteral callback.
                """
                self.set_value(self._input.get_text())
-               #set the markup on the label, red for errors in corresponding 
data type.
-               name = '<span font_desc="%s">%s</span>'%(
-                       PARAM_LABEL_FONT,
-                       Utils.xml_encode(self.get_name()),
-               )
-               #special markups if param is involved in a callback
-               if hasattr(self.get_parent(), 'get_callbacks') and \
-                       filter(lambda c: self.get_key() in c, 
self.get_parent()._callbacks):
-                       name = '<span underline="low">%s</span>'%name
-               if not self.is_valid():
-                       self._input.set_markup('<span 
foreground="red">%s</span>'%name)
-                       tip = 'Error: ' + ' '.join(self.get_error_messages())
-               else:
-                       self._input.set_markup(name)
-                       tip = 'Value: %s'%str(self.evaluate())
+               #is param is involved in a callback? #FIXME: messy
+               has_cb = \
+                       hasattr(self.get_parent(), 'get_callbacks') and \
+                       filter(lambda c: self.get_key() in c, 
self.get_parent()._callbacks)
+               
self._input.set_markup(Utils.parse_template(PARAM_LABEL_MARKUP_TMPL, 
param=self, has_cb=has_cb))
                #hide/show
                if self.get_hide() == 'all': self._input.hide_all()
                else: self._input.show_all()
@@ -76,30 +88,16 @@
                #set the tooltip
                if self._input.tp: self._input.tp.set_tip(
                        self._input.entry,
-                       'Key: %s\nType: %s\n%s'%(self.get_key(), 
self.get_type(), tip),
+                       Utils.parse_template(TIP_MARKUP_TMPL, 
param=self).strip(),
                )
                #execute the external callback
                if self._callback: self._callback(self)
 
-       def get_markup(self):
-               """
-               Create a markup to display the param as a label on the block.
-               If the param is valid, use the param's repr representation.
-               Otherwise, create a markup for error.
-               @return pango markup string
-               """
-               if self.is_valid():
-                       return '<b>%s:</b> 
%s'%(Utils.xml_encode(self.get_name()), Utils.xml_encode(repr(self)))
-               else:
-                       return '<span foreground="red"><b>%s:</b> 
error</span>'%Utils.xml_encode(self.get_name())
-
        def get_layout(self):
                """
                Create a layout based on the current markup.
                @return the pango layout
                """
                layout = gtk.DrawingArea().create_pango_layout('')
-               layout.set_markup(self.get_markup())
-               desc = pango.FontDescription(PARAM_FONT)
-               layout.set_font_description(desc)
+               layout.set_markup(Utils.parse_template(PARAM_MARKUP_TMPL, 
param=self))
                return layout

Modified: gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Port.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Port.py        
2009-04-13 23:19:59 UTC (rev 10821)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Port.py        
2009-04-14 04:19:13 UTC (rev 10822)
@@ -20,14 +20,17 @@
 from Element import Element
 from Constants import \
        PORT_SEPARATION, CONNECTOR_EXTENSION_MINIMAL, \
-       CONNECTOR_EXTENSION_INCREMENT, PORT_FONT, \
+       CONNECTOR_EXTENSION_INCREMENT, \
        PORT_LABEL_PADDING, PORT_MIN_WIDTH
+import Utils
 import Colors
 import pygtk
 pygtk.require('2.0')
 import gtk
-import pango
 
+PORT_MARKUP_TMPL="""\
+<span foreground="black" font_desc="Sans 
7.5">$encode($port.get_name())</span>"""
+
 class Port(Element):
        """The graphical port."""
 
@@ -83,9 +86,8 @@
                """Create the labels for the socket."""
                self.BG_color = Colors.get_color(self.get_color())
                #create the layout
-               layout = gtk.DrawingArea().create_pango_layout(self.get_name())
-               desc = pango.FontDescription(PORT_FONT)
-               layout.set_font_description(desc)
+               layout = gtk.DrawingArea().create_pango_layout('')
+               layout.set_markup(Utils.parse_template(PORT_MARKUP_TMPL, 
port=self))
                self.w, self.h = layout.get_pixel_size()
                self.W, self.H = 2*PORT_LABEL_PADDING+self.w, 
2*PORT_LABEL_PADDING+self.h
                #create the pixmap

Modified: gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Utils.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Utils.py       
2009-04-13 23:19:59 UTC (rev 10821)
+++ gnuradio/branches/developers/jblum/grc/grc/src/platforms/gui/Utils.py       
2009-04-14 04:19:13 UTC (rev 10822)
@@ -1,5 +1,5 @@
 """
-Copyright 2008 Free Software Foundation, Inc.
+Copyright 2008, 2009 Free Software Foundation, Inc.
 This file is part of GNU Radio
 
 GNU Radio Companion is free software; you can redistribute it and/or
@@ -18,6 +18,7 @@
 """
 
 from Constants import POSSIBLE_ROTATIONS
+from Cheetah.Template import Template
 
 def get_rotated_coordinate(coor, rotation):
        """
@@ -69,3 +70,13 @@
                        ("'", '&apos;'),
        ): string = string.replace(char, safe)
        return string
+
+def parse_template(tmpl_str, **kwargs):
+       """
+       Parse the template string with the given args.
+       Pass in the xml encode method for pango escape chars.
+       @param tmpl_str the template as a string
+       @return a string of the parsed template
+       """
+       kwargs['encode'] = xml_encode
+       return str(Template(tmpl_str, kwargs))





reply via email to

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