port dimensions based on label text
[debian/gnuradio] / grc / src / platforms / gui / Block.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 ... gui import Preferences
21 from Element import Element
22 import Utils
23 import Colors
24 from ... gui.Constants import BORDER_PROXIMITY_SENSITIVITY
25 from Constants import \
26         BLOCK_FONT, BLOCK_LABEL_PADDING, \
27         PORT_SEPARATION, LABEL_SEPARATION, \
28         PORT_BORDER_SEPARATION, POSSIBLE_ROTATIONS
29 import pygtk
30 pygtk.require('2.0')
31 import gtk
32 import pango
33
34 class Block(Element):
35         """The graphical signal block."""
36
37         def __init__(self, *args, **kwargs):
38                 """
39                 Block contructor.
40                 Add graphics related params to the block.
41                 """
42                 #add the position param
43                 self._params['_coordinate'] = self.get_parent().get_parent().Param(
44                         self,
45                         {
46                                 'name': 'GUI Coordinate',
47                                 'key': '_coordinate',
48                                 'type': 'raw',
49                                 'value': '(0, 0)',
50                                 'hide': 'all',
51                         }
52                 )
53                 self._params['_rotation'] = self.get_parent().get_parent().Param(
54                         self,
55                         {
56                                 'name': 'GUI Rotation',
57                                 'key': '_rotation',
58                                 'type': 'raw',
59                                 'value': '0',
60                                 'hide': 'all',
61                         }
62                 )
63                 Element.__init__(self)
64
65         def get_coordinate(self):
66                 """
67                 Get the coordinate from the position param.
68                 @return the coordinate tuple (x, y) or (0, 0) if failure
69                 """
70                 try: #should evaluate to tuple
71                         coor = eval(self.get_param('_coordinate').get_value())
72                         x, y = map(int, coor)
73                         fgW,fgH = self.get_parent().get_size()
74                         if x <= 0:
75                                 x = 0
76                         elif x >= fgW - BORDER_PROXIMITY_SENSITIVITY:
77                                 x = fgW - BORDER_PROXIMITY_SENSITIVITY
78                         if y <= 0:
79                                 y = 0
80                         elif y >= fgH - BORDER_PROXIMITY_SENSITIVITY:
81                                 y = fgH - BORDER_PROXIMITY_SENSITIVITY
82                         return (x, y)
83                 except:
84                         self.set_coordinate((0, 0))
85                         return (0, 0)
86
87         def set_coordinate(self, coor):
88                 """
89                 Set the coordinate into the position param.
90                 @param coor the coordinate tuple (x, y)
91                 """
92                 self.get_param('_coordinate').set_value(str(coor))
93
94         def get_rotation(self):
95                 """
96                 Get the rotation from the position param.
97                 @return the rotation in degrees or 0 if failure
98                 """
99                 try: #should evaluate to dict
100                         rotation = eval(self.get_param('_rotation').get_value())
101                         return int(rotation)
102                 except:
103                         self.set_rotation(POSSIBLE_ROTATIONS[0])
104                         return POSSIBLE_ROTATIONS[0]
105
106         def set_rotation(self, rot):
107                 """
108                 Set the rotation into the position param.
109                 @param rot the rotation in degrees
110                 """
111                 self.get_param('_rotation').set_value(str(rot))
112
113         def update(self):
114                 """Update the block, parameters, and ports when a change occurs."""
115                 self.bg_color = self.get_enabled() and Colors.BG_COLOR or Colors.DISABLED_BG_COLOR
116                 self.clear()
117                 self._create_labels()
118                 self.W = self.label_width + 2*BLOCK_LABEL_PADDING
119                 self.H = max(*(
120                         [self.label_height+2*BLOCK_LABEL_PADDING] + [2*PORT_BORDER_SEPARATION + \
121                         sum([port.H + PORT_SEPARATION for port in ports]) - PORT_SEPARATION
122                         for ports in (self.get_sources(), self.get_sinks())]
123                 ))
124                 if self.is_horizontal(): self.add_area((0, 0), (self.W, self.H))
125                 elif self.is_vertical(): self.add_area((0, 0), (self.H, self.W))
126                 map(lambda p: p.update(), self.get_ports())
127
128         def _create_labels(self):
129                 """Create the labels for the signal block."""
130                 layouts = list()
131                 #create the main layout
132                 layout = gtk.DrawingArea().create_pango_layout('')
133                 layouts.append(layout)
134                 if self.is_valid():     layout.set_markup('<b>'+Utils.xml_encode(self.get_name())+'</b>')
135                 else: layout.set_markup('<span foreground="red"><b>'+Utils.xml_encode(self.get_name())+'</b></span>')
136                 desc = pango.FontDescription(BLOCK_FONT)
137                 layout.set_font_description(desc)
138                 self.label_width, self.label_height = layout.get_pixel_size()
139                 #display the params
140                 if Preferences.show_params():
141                         for param in filter(lambda p: p.get_hide() not in ('all', 'part'), self.get_params()):
142                                 layout = param.get_layout()
143                                 layouts.append(layout)
144                                 w,h = layout.get_pixel_size()
145                                 self.label_width = max(w, self.label_width)
146                                 self.label_height = self.label_height + h + LABEL_SEPARATION
147                 width = self.label_width
148                 height = self.label_height
149                 #setup the pixmap
150                 pixmap = gtk.gdk.Pixmap(self.get_parent().get_window(), width, height, -1)
151                 gc = pixmap.new_gc()
152                 gc.foreground = self.bg_color
153                 pixmap.draw_rectangle(gc, True, 0, 0, width, height)
154                 gc.foreground = Colors.TXT_COLOR
155                 #draw the layouts
156                 h_off = 0
157                 for i,layout in enumerate(layouts):
158                         w,h = layout.get_pixel_size()
159                         if i == 0: w_off = (width-w)/2
160                         else: w_off = 0
161                         pixmap.draw_layout(gc, w_off, h_off, layout)
162                         h_off = h + h_off + LABEL_SEPARATION
163                 #create vertical and horizontal images
164                 self.horizontal_label = image = pixmap.get_image(0, 0, width, height)
165                 if self.is_vertical():
166                         self.vertical_label = vimage = gtk.gdk.Image(gtk.gdk.IMAGE_NORMAL, pixmap.get_visual(), height, width)
167                         for i in range(width):
168                                 for j in range(height): vimage.put_pixel(j, width-i-1, image.get_pixel(i, j))
169                 map(lambda p: p._create_labels(), self.get_ports())
170
171         def draw(self, window):
172                 """
173                 Draw the signal block with label and inputs/outputs.
174                 @param window the gtk window to draw on
175                 """
176                 x, y = self.get_coordinate()
177                 #draw main block
178                 Element.draw(self, window, BG_color=self.bg_color)
179                 #draw label image
180                 gc = self.get_gc()
181                 if self.is_horizontal():
182                         window.draw_image(gc, self.horizontal_label, 0, 0, x+BLOCK_LABEL_PADDING, y+(self.H-self.label_height)/2, -1, -1)
183                 elif self.is_vertical():
184                         window.draw_image(gc, self.vertical_label, 0, 0, x+(self.H-self.label_height)/2, y+BLOCK_LABEL_PADDING, -1, -1)
185                 #draw ports
186                 map(lambda p: p.draw(window), self.get_ports())
187
188         def what_is_selected(self, coor, coor_m=None):
189                 """
190                 Get the element that is selected.
191                 @param coor the (x,y) tuple
192                 @param coor_m the (x_m, y_m) tuple
193                 @return this block, a port, or None
194                 """
195                 for port in self.get_ports():
196                         port_selected = port.what_is_selected(coor, coor_m)
197                         if port_selected: return port_selected
198                 return Element.what_is_selected(self, coor, coor_m)