1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
"""
Copyright 2007 Free Software Foundation, Inc.
This file is part of GNU Radio
GNU Radio Companion is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
GNU Radio Companion is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
from ... gui import Preferences
from Element import Element
import Utils
import Colors
from ... gui.Constants import BORDER_PROXIMITY_SENSITIVITY
from Constants import \
BLOCK_FONT, LABEL_PADDING_WIDTH, \
LABEL_PADDING_HEIGHT, PORT_HEIGHT, \
PORT_SEPARATION, LABEL_SEPARATION, \
PORT_BORDER_SEPARATION, POSSIBLE_ROTATIONS
import pygtk
pygtk.require('2.0')
import gtk
import pango
class Block(Element):
"""The graphical signal block."""
def __init__(self, *args, **kwargs):
"""
Block contructor.
Add graphics related params to the block.
"""
#add the position param
self._params['_coordinate'] = self.get_parent().get_parent().Param(
self,
{
'name': 'GUI Coordinate',
'key': '_coordinate',
'type': 'raw',
'value': '(0, 0)',
'hide': 'all',
}
)
self._params['_rotation'] = self.get_parent().get_parent().Param(
self,
{
'name': 'GUI Rotation',
'key': '_rotation',
'type': 'raw',
'value': '0',
'hide': 'all',
}
)
Element.__init__(self)
def get_coordinate(self):
"""
Get the coordinate from the position param.
@return the coordinate tuple (x, y) or (0, 0) if failure
"""
try: #should evaluate to tuple
coor = eval(self.get_param('_coordinate').get_value())
x, y = map(int, coor)
fgW,fgH = self.get_parent().get_size()
if x <= 0:
x = 0
elif x >= fgW - BORDER_PROXIMITY_SENSITIVITY:
x = fgW - BORDER_PROXIMITY_SENSITIVITY
if y <= 0:
y = 0
elif y >= fgH - BORDER_PROXIMITY_SENSITIVITY:
y = fgH - BORDER_PROXIMITY_SENSITIVITY
return (x, y)
except:
self.set_coordinate((0, 0))
return (0, 0)
def set_coordinate(self, coor):
"""
Set the coordinate into the position param.
@param coor the coordinate tuple (x, y)
"""
self.get_param('_coordinate').set_value(str(coor))
def get_rotation(self):
"""
Get the rotation from the position param.
@return the rotation in degrees or 0 if failure
"""
try: #should evaluate to dict
rotation = eval(self.get_param('_rotation').get_value())
return int(rotation)
except:
self.set_rotation(POSSIBLE_ROTATIONS[0])
return POSSIBLE_ROTATIONS[0]
def set_rotation(self, rot):
"""
Set the rotation into the position param.
@param rot the rotation in degrees
"""
self.get_param('_rotation').set_value(str(rot))
def update(self):
"""Update the block, parameters, and ports when a change occurs."""
self.bg_color = self.get_enabled() and Colors.BG_COLOR or Colors.DISABLED_BG_COLOR
self.clear()
self._create_labels()
self.W = self.label_width + 2*LABEL_PADDING_WIDTH
max_ports = max(len(self.get_sinks()), len(self.get_sources()), 1)
self.H = max(self.label_height+2*LABEL_PADDING_HEIGHT, 2*PORT_BORDER_SEPARATION + max_ports*PORT_HEIGHT + (max_ports-1)*PORT_SEPARATION)
if self.is_horizontal(): self.add_area((0,0),(self.W,self.H))
elif self.is_vertical(): self.add_area((0,0),(self.H,self.W))
map(lambda p: p.update(), self.get_sinks() + self.get_sources())
def _create_labels(self):
"""Create the labels for the signal block."""
layouts = list()
#create the main layout
layout = gtk.DrawingArea().create_pango_layout('')
layouts.append(layout)
if self.is_valid(): layout.set_markup('<b>'+Utils.xml_encode(self.get_name())+'</b>')
else: layout.set_markup('<span foreground="red"><b>'+Utils.xml_encode(self.get_name())+'</b></span>')
desc = pango.FontDescription(BLOCK_FONT)
layout.set_font_description(desc)
self.label_width, self.label_height = layout.get_pixel_size()
#display the params (except for the special params id and position)
if Preferences.show_params():
for param in filter(lambda p: p.get_hide() not in ('all', 'part'), self.get_params()):
if not Preferences.show_id() and param.get_key() == 'id': continue
layout = param.get_layout()
layouts.append(layout)
w,h = layout.get_pixel_size()
self.label_width = max(w, self.label_width)
self.label_height = self.label_height + h + LABEL_SEPARATION
width = self.label_width
height = self.label_height
#setup the pixmap
pixmap = gtk.gdk.Pixmap(self.get_parent().get_window(), width, height, -1)
gc = pixmap.new_gc()
gc.foreground = self.bg_color
pixmap.draw_rectangle(gc, True, 0, 0, width, height)
gc.foreground = Colors.TXT_COLOR
#draw the layouts
h_off = 0
for i,layout in enumerate(layouts):
w,h = layout.get_pixel_size()
if i == 0: w_off = (width-w)/2
else: w_off = 0
pixmap.draw_layout(gc, w_off, h_off, layout)
h_off = h + h_off + LABEL_SEPARATION
#create vertical and horizontal images
self.horizontal_label = image = pixmap.get_image(0, 0, width, height)
if self.is_vertical():
self.vertical_label = vimage = gtk.gdk.Image(gtk.gdk.IMAGE_NORMAL, pixmap.get_visual(), height, width)
for i in range(width):
for j in range(height): vimage.put_pixel(j, width-i-1, image.get_pixel(i, j))
def draw(self, window):
"""
Draw the signal block with label and inputs/outputs.
@param window the gtk window to draw on
"""
x, y = self.get_coordinate()
#draw main block
Element.draw(self, window, BG_color=self.bg_color)
#draw label image
gc = self.get_gc()
if self.is_horizontal():
window.draw_image(gc, self.horizontal_label, 0, 0, x+LABEL_PADDING_WIDTH, y+(self.H-self.label_height)/2, -1, -1)
elif self.is_vertical():
window.draw_image(gc, self.vertical_label, 0, 0, x+(self.H-self.label_height)/2, y+LABEL_PADDING_WIDTH, -1, -1)
#draw ports
map(lambda p: p.draw(window), self.get_ports())
def what_is_selected(self, coor, coor_m=None):
"""
Get the element that is selected.
@param coor the (x,y) tuple
@param coor_m the (x_m, y_m) tuple
@return this block, a port, or None
"""
for port in self.get_ports():
port_selected = port.what_is_selected(coor, coor_m)
if port_selected: return port_selected
return Element.what_is_selected(self, coor, coor_m)
|