Merged r9481:9518 on jblum/grc_reorganize into trunk. Reorganized grc source under...
[debian/gnuradio] / grc / src / platforms / 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_HEIGHT, PORT_SEPARATION, \
23         PORT_WIDTH, CONNECTOR_EXTENSION_MINIMAL, \
24         CONNECTOR_EXTENSION_INCREMENT, PORT_FONT
25 import Colors
26 import pygtk
27 pygtk.require('2.0')
28 import gtk
29 import pango
30
31 class Port(Element):
32         """The graphical port."""
33
34         def __init__(self, *args, **kwargs):
35                 """
36                 Port contructor.
37                 Create list of connector coordinates.
38                 """
39                 Element.__init__(self)
40                 self.connector_coordinates = dict()
41
42         def update(self):
43                 """Create new areas and labels for the port."""
44                 self.clear()
45                 self.BG_color = Colors.get_color(self.get_color())
46                 self._create_labels()
47                 #get current rotation
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)
54                 length = len(ports)
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):
60                         x = -1*PORT_WIDTH
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):
70                         y = -1*PORT_WIDTH
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)
79                 #the connector length
80                 self._connector_length = CONNECTOR_EXTENSION_MINIMAL + CONNECTOR_EXTENSION_INCREMENT*index
81
82         def _create_labels(self):
83                 """Create the labels for the socket."""
84                 #create the layout
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()
89                 #create the pixmap
90                 pixmap = gtk.gdk.Pixmap(self.get_parent().get_parent().get_window(), w, h, -1)
91                 gc = pixmap.new_gc()
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)
96                 #create the images
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)
100                         for i in range(w):
101                                 for j in range(h): vimage.put_pixel(j, w-i-1, image.get_pixel(i, j))
102
103         def draw(self, window):
104                 """
105                 Draw the socket with a label.
106                 @param window the gtk window to draw on
107                 """
108                 Element.draw(self, window, BG_color=self.BG_color)
109                 gc = self.get_gc()
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)
117
118         def get_connector_coordinate(self):
119                 """
120                 Get the coordinate where connections may attach to.
121                 @return the connector coordinate (x, y) tuple
122                 """
123                 x,y = self._connector_coordinate
124                 X,Y = self.get_coordinate()
125                 return (x+X, y+Y)
126
127         def get_connector_direction(self):
128                 """
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
133                 """
134                 if self.is_source(): return self.get_rotation()
135                 elif self.is_sink(): return (self.get_rotation() + 180)%360
136
137         def get_connector_length(self):
138                 """
139                 Get the length of the connector.
140                 The connector length increases as the port index changes.
141                 @return the length in pixels
142                 """
143                 return self._connector_length
144
145         def get_rotation(self):
146                 """
147                 Get the parent's rotation rather than self.
148                 @return the parent's rotation
149                 """
150                 return self.get_parent().get_rotation()
151
152         def move(self, delta_coor):
153                 """
154                 Move the parent rather than self.
155                 @param delta_corr the (delta_x, delta_y) tuple
156                 """
157                 self.get_parent().move(delta_coor)
158
159         def rotate(self, direction):
160                 """
161                 Rotate the parent rather than self.
162                 @param direction degrees to rotate
163                 """
164                 self.get_parent().rotate(direction)
165
166         def get_coordinate(self):
167                 """
168                 Get the parent's coordinate rather than self.
169                 @return the parents coordinate
170                 """
171                 return self.get_parent().get_coordinate()
172
173         def set_highlighted(self, highlight):
174                 """
175                 Set the parent highlight rather than self.
176                 @param highlight true to enable highlighting
177                 """
178                 self.get_parent().set_highlighted(highlight)
179
180         def is_highlighted(self):
181                 """
182                 Get the parent's is highlight rather than self.
183                 @return the parent's highlighting status
184                 """
185                 return self.get_parent().is_highlighted()