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_SEPARATION, CONNECTOR_EXTENSION_MINIMAL, \
23 CONNECTOR_EXTENSION_INCREMENT, \
24 PORT_LABEL_PADDING, PORT_MIN_WIDTH
32 <span foreground="black" font_desc="Sans 7.5">$encode($port.get_name())</span>"""
35 """The graphical port."""
37 def __init__(self, *args, **kwargs):
40 Create list of connector coordinates.
42 Element.__init__(self)
43 self.connector_coordinates = dict()
46 """Create new areas and labels for the port."""
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()
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)
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):
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):
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)
83 self._connector_length = CONNECTOR_EXTENSION_MINIMAL + CONNECTOR_EXTENSION_INCREMENT*index
85 def _create_labels(self):
86 """Create the labels for the socket."""
87 self._bg_color = Colors.get_color(self.get_color())
89 layout = gtk.DrawingArea().create_pango_layout('')
90 layout.set_markup(Utils.parse_template(PORT_MARKUP_TMPL, port=self))
91 self.w, self.h = layout.get_pixel_size()
92 self.W, self.H = 2*PORT_LABEL_PADDING+self.w, 2*PORT_LABEL_PADDING+self.h
94 pixmap = self.get_parent().get_parent().new_pixmap(self.w, self.h)
96 gc.set_foreground(self._bg_color)
97 pixmap.draw_rectangle(gc, True, 0, 0, self.w, self.h)
98 pixmap.draw_layout(gc, 0, 0, layout)
100 self.horizontal_label = image = pixmap.get_image(0, 0, self.w, self.h)
101 if self.is_vertical():
102 self.vertical_label = vimage = gtk.gdk.Image(gtk.gdk.IMAGE_NORMAL, pixmap.get_visual(), self.h, self.w)
103 for i in range(self.w):
104 for j in range(self.h): vimage.put_pixel(j, self.w-i-1, image.get_pixel(i, j))
106 def draw(self, gc, window):
108 Draw the socket with a label.
109 @param gc the graphics context
110 @param window the gtk window to draw on
113 self, gc, window, bg_color=self._bg_color,
114 border_color=self.is_highlighted() and Colors.HIGHLIGHT_COLOR or Colors.BORDER_COLOR,
116 X,Y = self.get_coordinate()
117 (x,y),(w,h) = self.areas_dict[self.get_rotation()][0] #use the first area's sizes to place the labels
118 if self.is_horizontal():
119 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)
120 elif self.is_vertical():
121 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 def get_connector_coordinate(self):
125 Get the coordinate where connections may attach to.
126 @return the connector coordinate (x, y) tuple
128 x,y = self._connector_coordinate
129 X,Y = self.get_coordinate()
132 def get_connector_direction(self):
134 Get the direction that the socket points: 0,90,180,270.
135 This is the rotation degree if the socket is an output or
136 the rotation degree + 180 if the socket is an input.
137 @return the direction in degrees
139 if self.is_source(): return self.get_rotation()
140 elif self.is_sink(): return (self.get_rotation() + 180)%360
142 def get_connector_length(self):
144 Get the length of the connector.
145 The connector length increases as the port index changes.
146 @return the length in pixels
148 return self._connector_length
150 def get_rotation(self):
152 Get the parent's rotation rather than self.
153 @return the parent's rotation
155 return self.get_parent().get_rotation()
157 def move(self, delta_coor):
159 Move the parent rather than self.
160 @param delta_corr the (delta_x, delta_y) tuple
162 self.get_parent().move(delta_coor)
164 def rotate(self, direction):
166 Rotate the parent rather than self.
167 @param direction degrees to rotate
169 self.get_parent().rotate(direction)
171 def get_coordinate(self):
173 Get the parent's coordinate rather than self.
174 @return the parents coordinate
176 return self.get_parent().get_coordinate()
178 def set_highlighted(self, highlight):
180 Set the parent highlight rather than self.
181 @param highlight true to enable highlighting
183 self.get_parent().set_highlighted(highlight)
185 def is_highlighted(self):
187 Get the parent's is highlight rather than self.
188 @return the parent's highlighting status
190 return self.get_parent().is_highlighted()