Created recursive create labels and shapes method for gui element.
authorJosh Blum <josh@joshknows.com>
Sat, 5 Sep 2009 09:21:37 +0000 (02:21 -0700)
committerJosh Blum <josh@joshknows.com>
Sat, 5 Sep 2009 09:21:37 +0000 (02:21 -0700)
Replaces update methods in the gui classes and simplifies calls.
The master update method in flow graph calls create labels and shapes.

grc/gui/Block.py
grc/gui/Connection.py
grc/gui/Element.py
grc/gui/FlowGraph.py
grc/gui/Port.py

index 68c4da9c34fd062c2bef642dd247b215b341f5be..fd8cfc226edb0c488de4796e33342204b5a41d08 100644 (file)
@@ -113,23 +113,16 @@ class Block(Element):
                """
                self.get_param('_rotation').set_value(str(rot))
 
-       def update(self):
+       def create_shapes(self):
                """Update the block, parameters, and ports when a change occurs."""
-               self._bg_color = self.get_enabled() and Colors.BLOCK_ENABLED_COLOR or Colors.BLOCK_DISABLED_COLOR
-               self.clear()
-               self._create_labels()
-               self.W = self.label_width + 2*BLOCK_LABEL_PADDING
-               self.H = max(*(
-                       [self.label_height+2*BLOCK_LABEL_PADDING] + [2*PORT_BORDER_SEPARATION + \
-                       sum([port.H + PORT_SEPARATION for port in ports]) - PORT_SEPARATION
-                       for ports in (self.get_sources(), self.get_sinks())]
-               ))
+               Element.create_shapes(self)
                if self.is_horizontal(): self.add_area((0, 0), (self.W, self.H))
                elif self.is_vertical(): self.add_area((0, 0), (self.H, self.W))
-               map(lambda p: p.update(), self.get_ports())
 
-       def _create_labels(self):
+       def create_labels(self):
                """Create the labels for the signal block."""
+               Element.create_labels(self)
+               self._bg_color = self.get_enabled() and Colors.BLOCK_ENABLED_COLOR or Colors.BLOCK_DISABLED_COLOR
                layouts = list()
                #create the main layout
                layout = gtk.DrawingArea().create_pango_layout('')
@@ -164,7 +157,13 @@ class Block(Element):
                        self.vertical_label = vimage = gtk.gdk.Image(gtk.gdk.IMAGE_NORMAL, pixmap.get_visual(), height, width)
                        for i in range(width):
                                for j in range(height): vimage.put_pixel(j, width-i-1, image.get_pixel(i, j))
-               map(lambda p: p._create_labels(), self.get_ports())
+               #calculate width and height needed
+               self.W = self.label_width + 2*BLOCK_LABEL_PADDING
+               self.H = max(*(
+                       [self.label_height+2*BLOCK_LABEL_PADDING] + [2*PORT_BORDER_SEPARATION + \
+                       sum([port.H + PORT_SEPARATION for port in ports]) - PORT_SEPARATION
+                       for ports in (self.get_sources(), self.get_sinks())]
+               ))
 
        def draw(self, gc, window):
                """
index a85650ee2da9213c68adfd09b269cea3e1246c6f..45f8a689a923026d33bcdbbdfd04c0a1ffc52850 100644 (file)
@@ -50,8 +50,9 @@ class Connection(Element):
                """
                return 0
 
-       def update(self):
+       def create_shapes(self):
                """Precalculate relative coordinates."""
+               Element.create_shapes(self)
                self._sink_rot = None
                self._source_rot = None
                self._sink_coor = None
@@ -74,7 +75,7 @@ class Connection(Element):
 
        def _update_after_move(self):
                """Calculate coordinates."""
-               self.clear()
+               self.clear() #FIXME do i want this here?
                #source connector
                source = self.get_source()
                X, Y = source.get_connector_coordinate()
@@ -125,7 +126,7 @@ class Connection(Element):
                sink = self.get_sink()
                source = self.get_source()
                #check for changes
-               if self._sink_rot != sink.get_rotation() or self._source_rot != source.get_rotation(): self.update()
+               if self._sink_rot != sink.get_rotation() or self._source_rot != source.get_rotation(): self.create_shapes()
                elif self._sink_coor != sink.get_coordinate() or self._source_coor != source.get_coordinate(): self._update_after_move()
                #cache values
                self._sink_rot = sink.get_rotation()
index bda1870598be03901bddc694b52adb5025a89932..2e20b9a224ed372f784ec6fa7a5734ac22d065d0 100644 (file)
@@ -61,6 +61,21 @@ class Element(object):
                rotation = rotation or self.get_rotation()
                return rotation in (90, 270)
 
+       def create_labels(self):
+               """
+               Create labels (if applicable) and call on all children.
+               Call this base method before creating labels in the element.
+               """
+               for child in self.get_children(): child.create_labels()
+
+       def create_shapes(self):
+               """
+               Create shapes (if applicable) and call on all children.
+               Call this base method before creating shapes in the element.
+               """
+               self.clear()
+               for child in self.get_children(): child.create_shapes()
+
        def draw(self, gc, window, border_color, bg_color):
                """
                Draw in the given window.
@@ -216,7 +231,3 @@ class Element(object):
                if rotation not in POSSIBLE_ROTATIONS:
                        raise Exception('"%s" is not one of the possible rotations: (%s)'%(rotation, POSSIBLE_ROTATIONS))
                self.rotation = rotation
-
-       def update(self):
-               """Do nothing for the update. Dummy method."""
-               pass
index 8a908ff5044014fd402eee6dffa79811e43b1fb2..35ccf5e27a37dbc2551d0c38dc20d095adecccb1 100644 (file)
@@ -291,12 +291,13 @@ class FlowGraph(Element):
 
        def update(self):
                """
-               Do a global rewrite and validate.
-               Call update on all elements.
+               Call the top level rewrite and validate.
+               Call the top level create labels and shapes.
                """
                self.rewrite()
                self.validate()
-               for element in self.get_elements(): element.update()
+               self.create_labels()
+               self.create_shapes()
 
        ##########################################################################
        ## Get Selected
index f8bfefee69aa0d8ffc481d35955731690ad767f0..6763f6cbd553f61ab7673095f247687000e06dc4 100644 (file)
@@ -42,9 +42,9 @@ class Port(Element):
                Element.__init__(self)
                self.connector_coordinates = dict()
 
-       def update(self):
+       def create_shapes(self):
                """Create new areas and labels for the port."""
-               self.clear()
+               Element.create_shapes(self)
                #get current rotation
                rotation = self.get_rotation()
                #get all sibling ports
@@ -82,8 +82,9 @@ class Port(Element):
                #the connector length
                self._connector_length = CONNECTOR_EXTENSION_MINIMAL + CONNECTOR_EXTENSION_INCREMENT*index
 
-       def _create_labels(self):
+       def create_labels(self):
                """Create the labels for the socket."""
+               Element.create_labels(self)
                self._bg_color = Colors.get_color(self.get_color())
                #create the layout
                layout = gtk.DrawingArea().create_pango_layout('')