Reworked actions api and actions objects:
[debian/gnuradio] / grc / gui / Port.py
1 """
2 Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 """
19
20 from Element import Element
21 from Constants import \
22         PORT_SEPARATION, CONNECTOR_EXTENSION_MINIMAL, \
23         CONNECTOR_EXTENSION_INCREMENT, \
24         PORT_LABEL_PADDING, PORT_MIN_WIDTH
25 import Utils
26 import Colors
27 import pygtk
28 pygtk.require('2.0')
29 import gtk
30
31 PORT_MARKUP_TMPL="""\
32 <span foreground="black" font_desc="Sans 7.5">$encode($port.get_name())</span>"""
33
34 class Port(Element):
35         """The graphical port."""
36
37         def __init__(self):
38                 """
39                 Port contructor.
40                 Create list of connector coordinates.
41                 """
42                 Element.__init__(self)
43                 self.connector_coordinates = dict()
44
45         def create_shapes(self):
46                 """Create new areas and labels for the port."""
47                 Element.create_shapes(self)
48                 #get current rotation
49                 rotation = self.get_rotation()
50                 #get all sibling ports
51                 if self.is_source(): ports = self.get_parent().get_sources()
52                 elif self.is_sink(): ports = self.get_parent().get_sinks()
53                 #get the max width
54                 self.W = max([port.W for port in ports] + [PORT_MIN_WIDTH])
55                 #get a numeric index for this port relative to its sibling ports
56                 index = ports.index(self)
57                 length = len(ports)
58                 #reverse the order of ports     for these rotations
59                 if rotation in (180, 270): index = length-index-1
60                 offset = (self.get_parent().H - length*self.H - (length-1)*PORT_SEPARATION)/2
61                 #create areas and connector coordinates
62                 if (self.is_sink() and rotation == 0) or (self.is_source() and rotation == 180):
63                         x = -1*self.W
64                         y = (PORT_SEPARATION+self.H)*index+offset
65                         self.add_area((x, y), (self.W, self.H))
66                         self._connector_coordinate = (x-1, y+self.H/2)
67                 elif (self.is_source() and rotation == 0) or (self.is_sink() and rotation == 180):
68                         x = self.get_parent().W
69                         y = (PORT_SEPARATION+self.H)*index+offset
70                         self.add_area((x, y), (self.W, self.H))
71                         self._connector_coordinate = (x+1+self.W, y+self.H/2)
72                 elif (self.is_source() and rotation == 90) or (self.is_sink() and rotation == 270):
73                         y = -1*self.W
74                         x = (PORT_SEPARATION+self.H)*index+offset
75                         self.add_area((x, y), (self.H, self.W))
76                         self._connector_coordinate = (x+self.H/2, y-1)
77                 elif (self.is_sink() and rotation == 90) or (self.is_source() and rotation == 270):
78                         y = self.get_parent().W
79                         x = (PORT_SEPARATION+self.H)*index+offset
80                         self.add_area((x, y), (self.H, self.W))
81                         self._connector_coordinate = (x+self.H/2, y+1+self.W)
82                 #the connector length
83                 self._connector_length = CONNECTOR_EXTENSION_MINIMAL + CONNECTOR_EXTENSION_INCREMENT*index
84
85         def create_labels(self):
86                 """Create the labels for the socket."""
87                 Element.create_labels(self)
88                 self._bg_color = Colors.get_color(self.get_color())
89                 #create the layout
90                 layout = gtk.DrawingArea().create_pango_layout('')
91                 layout.set_markup(Utils.parse_template(PORT_MARKUP_TMPL, port=self))
92                 self.w, self.h = layout.get_pixel_size()
93                 self.W, self.H = 2*PORT_LABEL_PADDING+self.w, 2*PORT_LABEL_PADDING+self.h
94                 #create the pixmap
95                 pixmap = self.get_parent().get_parent().new_pixmap(self.w, self.h)
96                 gc = pixmap.new_gc()
97                 gc.set_foreground(self._bg_color)
98                 pixmap.draw_rectangle(gc, True, 0, 0, self.w, self.h)
99                 pixmap.draw_layout(gc, 0, 0, layout)
100                 #create the images
101                 self.horizontal_label = image = pixmap.get_image(0, 0, self.w, self.h)
102                 if self.is_vertical():
103                         self.vertical_label = vimage = gtk.gdk.Image(gtk.gdk.IMAGE_NORMAL, pixmap.get_visual(), self.h, self.w)
104                         for i in range(self.w):
105                                 for j in range(self.h): vimage.put_pixel(j, self.w-i-1, image.get_pixel(i, j))
106
107         def draw(self, gc, window):
108                 """
109                 Draw the socket with a label.
110                 @param gc the graphics context
111                 @param window the gtk window to draw on
112                 """
113                 Element.draw(
114                         self, gc, window, bg_color=self._bg_color,
115                         border_color=self.is_highlighted() and Colors.HIGHLIGHT_COLOR or Colors.BORDER_COLOR,
116                 )
117                 X,Y = self.get_coordinate()
118                 (x,y),(w,h) = self._areas_list[0] #use the first area's sizes to place the labels
119                 if self.is_horizontal():
120                         window.draw_image(gc, self.horizontal_label, 0, 0, x+X+(self.W-self.w)/2, y+Y+(self.H-self.h)/2, -1, -1)
121                 elif self.is_vertical():
122                         window.draw_image(gc, self.vertical_label, 0, 0, x+X+(self.H-self.h)/2, y+Y+(self.W-self.w)/2, -1, -1)
123
124         def get_connector_coordinate(self):
125                 """
126                 Get the coordinate where connections may attach to.
127                 @return the connector coordinate (x, y) tuple
128                 """
129                 x,y = self._connector_coordinate
130                 X,Y = self.get_coordinate()
131                 return (x+X, y+Y)
132
133         def get_connector_direction(self):
134                 """
135                 Get the direction that the socket points: 0,90,180,270.
136                 This is the rotation degree if the socket is an output or
137                 the rotation degree + 180 if the socket is an input.
138                 @return the direction in degrees
139                 """
140                 if self.is_source(): return self.get_rotation()
141                 elif self.is_sink(): return (self.get_rotation() + 180)%360
142
143         def get_connector_length(self):
144                 """
145                 Get the length of the connector.
146                 The connector length increases as the port index changes.
147                 @return the length in pixels
148                 """
149                 return self._connector_length
150
151         def get_rotation(self):
152                 """
153                 Get the parent's rotation rather than self.
154                 @return the parent's rotation
155                 """
156                 return self.get_parent().get_rotation()
157
158         def move(self, delta_coor):
159                 """
160                 Move the parent rather than self.
161                 @param delta_corr the (delta_x, delta_y) tuple
162                 """
163                 self.get_parent().move(delta_coor)
164
165         def rotate(self, direction):
166                 """
167                 Rotate the parent rather than self.
168                 @param direction degrees to rotate
169                 """
170                 self.get_parent().rotate(direction)
171
172         def get_coordinate(self):
173                 """
174                 Get the parent's coordinate rather than self.
175                 @return the parents coordinate
176                 """
177                 return self.get_parent().get_coordinate()
178
179         def set_highlighted(self, highlight):
180                 """
181                 Set the parent highlight rather than self.
182                 @param highlight true to enable highlighting
183                 """
184                 self.get_parent().set_highlighted(highlight)
185
186         def is_highlighted(self):
187                 """
188                 Get the parent's is highlight rather than self.
189                 @return the parent's highlighting status
190                 """
191                 return self.get_parent().is_highlighted()