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