Removing PowerLine since we weren't using it and don't have plans to.
[debian/gnuradio] / grc / gui / Utils.py
1 """
2 Copyright 2008, 2009 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 Constants import POSSIBLE_ROTATIONS
21 from Cheetah.Template import Template
22 import pygtk
23 pygtk.require('2.0')
24 import gtk
25 import gobject
26
27 def rotate_pixmap(gc, src_pixmap, dst_pixmap, angle=gtk.gdk.PIXBUF_ROTATE_COUNTERCLOCKWISE):
28         """
29         Load the destination pixmap with a rotated version of the source pixmap.
30         The source pixmap will be loaded into a pixbuf, rotated, and drawn to the destination pixmap.
31         The pixbuf is a client-side drawable, where a pixmap is a server-side drawable.
32         @param gc the graphics context
33         @param src_pixmap the source pixmap
34         @param dst_pixmap the destination pixmap
35         @param angle the angle to rotate by
36         """
37         width, height = src_pixmap.get_size()
38         pixbuf = gtk.gdk.Pixbuf(
39                 colorspace=gtk.gdk.COLORSPACE_RGB,
40                 has_alpha=False, bits_per_sample=8,
41                 width=width, height=height,
42         )
43         pixbuf.get_from_drawable(src_pixmap, src_pixmap.get_colormap(), 0, 0, 0, 0, -1, -1)
44         pixbuf = pixbuf.rotate_simple(angle)
45         dst_pixmap.draw_pixbuf(gc, pixbuf, 0, 0, 0, 0)
46
47 def get_rotated_coordinate(coor, rotation):
48         """
49         Rotate the coordinate by the given rotation.
50         @param coor the coordinate x, y tuple
51         @param rotation the angle in degrees
52         @return the rotated coordinates
53         """
54         #handles negative angles
55         rotation = (rotation + 360)%360
56         assert rotation in POSSIBLE_ROTATIONS
57         #determine the number of degrees to rotate
58         cos_r, sin_r = {
59                 0: (1, 0),
60                 90: (0, 1),
61                 180: (-1, 0),
62                 270: (0, -1),
63         }[rotation]
64         x, y = coor
65         return (x*cos_r + y*sin_r, -x*sin_r + y*cos_r)
66
67 def get_angle_from_coordinates((x1,y1), (x2,y2)):
68         """
69         Given two points, calculate the vector direction from point1 to point2, directions are multiples of 90 degrees.
70         @param (x1,y1) the coordinate of point 1
71         @param (x2,y2) the coordinate of point 2
72         @return the direction in degrees
73         """
74         if y1 == y2:#0 or 180
75                 if x2 > x1: return 0
76                 else: return 180
77         else:#90 or 270
78                 if y2 > y1: return 270
79                 else: return 90
80
81 def parse_template(tmpl_str, **kwargs):
82         """
83         Parse the template string with the given args.
84         Pass in the xml encode method for pango escape chars.
85         @param tmpl_str the template as a string
86         @return a string of the parsed template
87         """
88         kwargs['encode'] = gobject.markup_escape_text
89         return str(Template(tmpl_str, kwargs))