Imported Upstream version 3.2.2
[debian/gnuradio] / grc / gui / Port.py
1 """
2 Copyright 2007 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, *args, **kwargs):
38                 """
39                 Port contructor.
40                 Create list of connector coordinates.
41                 """
42                 Element.__init__(self)
43                 self.connector_coordinates = dict()
44
45         def update(self):
46                 """Create new areas and labels for the port."""
47                 self.clear()
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                 self._bg_color = Colors.get_color(self.get_color())
88                 #create the layout
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
93                 #create the pixmap
94                 pixmap = self.get_parent().get_parent().new_pixmap(self.w, self.h)
95                 gc = pixmap.new_gc()
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)
99                 #create the images
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))
105
106         def draw(self, gc, window):
107                 """
108                 Draw the socket with a label.
109                 @param gc the graphics context
110                 @param window the gtk window to draw on
111                 """
112                 Element.draw(
113                         self, gc, window, bg_color=self._bg_color,
114                         border_color=self.is_highlighted() and Colors.HIGHLIGHT_COLOR or Colors.BORDER_COLOR,
115                 )
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)
122
123         def get_connector_coordinate(self):
124                 """
125                 Get the coordinate where connections may attach to.
126                 @return the connector coordinate (x, y) tuple
127                 """
128                 x,y = self._connector_coordinate
129                 X,Y = self.get_coordinate()
130                 return (x+X, y+Y)
131
132         def get_connector_direction(self):
133                 """
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
138                 """
139                 if self.is_source(): return self.get_rotation()
140                 elif self.is_sink(): return (self.get_rotation() + 180)%360
141
142         def get_connector_length(self):
143                 """
144                 Get the length of the connector.
145                 The connector length increases as the port index changes.
146                 @return the length in pixels
147                 """
148                 return self._connector_length
149
150         def get_rotation(self):
151                 """
152                 Get the parent's rotation rather than self.
153                 @return the parent's rotation
154                 """
155                 return self.get_parent().get_rotation()
156
157         def move(self, delta_coor):
158                 """
159                 Move the parent rather than self.
160                 @param delta_corr the (delta_x, delta_y) tuple
161                 """
162                 self.get_parent().move(delta_coor)
163
164         def rotate(self, direction):
165                 """
166                 Rotate the parent rather than self.
167                 @param direction degrees to rotate
168                 """
169                 self.get_parent().rotate(direction)
170
171         def get_coordinate(self):
172                 """
173                 Get the parent's coordinate rather than self.
174                 @return the parents coordinate
175                 """
176                 return self.get_parent().get_coordinate()
177
178         def set_highlighted(self, highlight):
179                 """
180                 Set the parent highlight rather than self.
181                 @param highlight true to enable highlighting
182                 """
183                 self.get_parent().set_highlighted(highlight)
184
185         def is_highlighted(self):
186                 """
187                 Get the parent's is highlight rather than self.
188                 @return the parent's highlighting status
189                 """
190                 return self.get_parent().is_highlighted()