2 Copyright 2007 Free Software Foundation, Inc.
3 This file is part of GNU Radio
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.
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.
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
20 from Element import Element
21 from Constants import \
22 PORT_HEIGHT, PORT_SEPARATION, \
23 PORT_WIDTH, CONNECTOR_EXTENSION_MINIMAL, \
24 CONNECTOR_EXTENSION_INCREMENT, PORT_FONT
32 """The graphical port."""
34 def __init__(self, *args, **kwargs):
37 Create list of connector coordinates.
39 Element.__init__(self)
40 self.connector_coordinates = dict()
43 """Create new areas and labels for the port."""
45 self.BG_color = Colors.get_color(self.get_color())
48 rotation = self.get_rotation()
49 #get all sibling ports
50 if self.is_source(): ports = self.get_parent().get_sources()
51 elif self.is_sink(): ports = self.get_parent().get_sinks()
52 #get a numeric index for this port relative to its sibling ports
53 index = ports.index(self)
55 #reverse the order of ports for these rotations
56 if rotation in (180, 270): index = length-index-1
57 offset = (self.get_parent().H - length*PORT_HEIGHT - (length-1)*PORT_SEPARATION)/2
58 #create areas and connector coordinates
59 if (self.is_sink() and rotation == 0) or (self.is_source() and rotation == 180):
61 y = (PORT_SEPARATION+PORT_HEIGHT)*index+offset
62 self.add_area((x, y), (PORT_WIDTH, PORT_HEIGHT))
63 self._connector_coordinate = (x-1, y+PORT_HEIGHT/2)
64 elif (self.is_source() and rotation == 0) or (self.is_sink() and rotation == 180):
65 x = self.get_parent().W
66 y = (PORT_SEPARATION+PORT_HEIGHT)*index+offset
67 self.add_area((x, y), (PORT_WIDTH, PORT_HEIGHT))
68 self._connector_coordinate = (x+1+PORT_WIDTH, y+PORT_HEIGHT/2)
69 elif (self.is_source() and rotation == 90) or (self.is_sink() and rotation == 270):
71 x = (PORT_SEPARATION+PORT_HEIGHT)*index+offset
72 self.add_area((x, y), (PORT_HEIGHT, PORT_WIDTH))
73 self._connector_coordinate = (x+PORT_HEIGHT/2, y-1)
74 elif (self.is_sink() and rotation == 90) or (self.is_source() and rotation == 270):
75 y = self.get_parent().W
76 x = (PORT_SEPARATION+PORT_HEIGHT)*index+offset
77 self.add_area((x, y), (PORT_HEIGHT, PORT_WIDTH))
78 self._connector_coordinate = (x+PORT_HEIGHT/2, y+1+PORT_WIDTH)
80 self._connector_length = CONNECTOR_EXTENSION_MINIMAL + CONNECTOR_EXTENSION_INCREMENT*index
82 def _create_labels(self):
83 """Create the labels for the socket."""
85 layout = gtk.DrawingArea().create_pango_layout(self.get_name())
86 desc = pango.FontDescription(PORT_FONT)
87 layout.set_font_description(desc)
88 w,h = self.w,self.h = layout.get_pixel_size()
90 pixmap = gtk.gdk.Pixmap(self.get_parent().get_parent().get_window(), w, h, -1)
92 gc.foreground = self.BG_color
93 pixmap.draw_rectangle(gc, True, 0, 0, w, h)
94 gc.foreground = Colors.TXT_COLOR
95 pixmap.draw_layout(gc, 0, 0, layout)
97 self.horizontal_label = image = pixmap.get_image(0, 0, w, h)
98 if self.is_vertical():
99 self.vertical_label = vimage = gtk.gdk.Image(gtk.gdk.IMAGE_NORMAL, pixmap.get_visual(), h, w)
101 for j in range(h): vimage.put_pixel(j, w-i-1, image.get_pixel(i, j))
103 def draw(self, window):
105 Draw the socket with a label.
106 @param window the gtk window to draw on
108 Element.draw(self, window, BG_color=self.BG_color)
110 gc.foreground = Colors.TXT_COLOR
111 X,Y = self.get_coordinate()
112 (x,y),(w,h) = self.areas_dict[self.get_rotation()][0] #use the first area's sizes to place the labels
113 if self.is_horizontal():
114 window.draw_image(gc, self.horizontal_label, 0, 0, x+X+(PORT_WIDTH-self.w)/2, y+Y+(PORT_HEIGHT-self.h)/2, -1, -1)
115 elif self.is_vertical():
116 window.draw_image(gc, self.vertical_label, 0, 0, x+X+(PORT_HEIGHT-self.h)/2, y+Y+(PORT_WIDTH-self.w)/2, -1, -1)
118 def get_connector_coordinate(self):
120 Get the coordinate where connections may attach to.
121 @return the connector coordinate (x, y) tuple
123 x,y = self._connector_coordinate
124 X,Y = self.get_coordinate()
127 def get_connector_direction(self):
129 Get the direction that the socket points: 0,90,180,270.
130 This is the rotation degree if the socket is an output or
131 the rotation degree + 180 if the socket is an input.
132 @return the direction in degrees
134 if self.is_source(): return self.get_rotation()
135 elif self.is_sink(): return (self.get_rotation() + 180)%360
137 def get_connector_length(self):
139 Get the length of the connector.
140 The connector length increases as the port index changes.
141 @return the length in pixels
143 return self._connector_length
145 def get_rotation(self):
147 Get the parent's rotation rather than self.
148 @return the parent's rotation
150 return self.get_parent().get_rotation()
152 def move(self, delta_coor):
154 Move the parent rather than self.
155 @param delta_corr the (delta_x, delta_y) tuple
157 self.get_parent().move(delta_coor)
159 def rotate(self, direction):
161 Rotate the parent rather than self.
162 @param direction degrees to rotate
164 self.get_parent().rotate(direction)
166 def get_coordinate(self):
168 Get the parent's coordinate rather than self.
169 @return the parents coordinate
171 return self.get_parent().get_coordinate()
173 def set_highlighted(self, highlight):
175 Set the parent highlight rather than self.
176 @param highlight true to enable highlighting
178 self.get_parent().set_highlighted(highlight)
180 def is_highlighted(self):
182 Get the parent's is highlight rather than self.
183 @return the parent's highlighting status
185 return self.get_parent().is_highlighted()