2 Copyright 2007, 2008, 2009 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."""
40 Create list of connector coordinates.
42 Element.__init__(self)
43 self.connector_coordinates = dict()
45 def create_shapes(self):
46 """Create new areas and labels for the port."""
47 Element.create_shapes(self)
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 Element.create_labels(self)
88 self._bg_color = Colors.get_color(self.get_color())
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
95 pixmap = self.get_parent().get_parent().new_pixmap(self.w, self.h)
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)
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))
107 def draw(self, gc, window):
109 Draw the socket with a label.
110 @param gc the graphics context
111 @param window the gtk window to draw on
114 self, gc, window, bg_color=self._bg_color,
115 border_color=self.is_highlighted() and Colors.HIGHLIGHT_COLOR or Colors.BORDER_COLOR,
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)
124 def get_connector_coordinate(self):
126 Get the coordinate where connections may attach to.
127 @return the connector coordinate (x, y) tuple
129 x,y = self._connector_coordinate
130 X,Y = self.get_coordinate()
133 def get_connector_direction(self):
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
140 if self.is_source(): return self.get_rotation()
141 elif self.is_sink(): return (self.get_rotation() + 180)%360
143 def get_connector_length(self):
145 Get the length of the connector.
146 The connector length increases as the port index changes.
147 @return the length in pixels
149 return self._connector_length
151 def get_rotation(self):
153 Get the parent's rotation rather than self.
154 @return the parent's rotation
156 return self.get_parent().get_rotation()
158 def move(self, delta_coor):
160 Move the parent rather than self.
161 @param delta_corr the (delta_x, delta_y) tuple
163 self.get_parent().move(delta_coor)
165 def rotate(self, direction):
167 Rotate the parent rather than self.
168 @param direction degrees to rotate
170 self.get_parent().rotate(direction)
172 def get_coordinate(self):
174 Get the parent's coordinate rather than self.
175 @return the parents coordinate
177 return self.get_parent().get_coordinate()
179 def set_highlighted(self, highlight):
181 Set the parent highlight rather than self.
182 @param highlight true to enable highlighting
184 self.get_parent().set_highlighted(highlight)
186 def is_highlighted(self):
188 Get the parent's is highlight rather than self.
189 @return the parent's highlighting status
191 return self.get_parent().is_highlighted()