Move key press extraction logic into actions module.
[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 gobject
23
24 def get_rotated_coordinate(coor, rotation):
25         """
26         Rotate the coordinate by the given rotation.
27         @param coor the coordinate x, y tuple
28         @param rotation the angle in degrees
29         @return the rotated coordinates
30         """
31         #handles negative angles
32         rotation = (rotation + 360)%360
33         assert rotation in POSSIBLE_ROTATIONS
34         #determine the number of degrees to rotate
35         cos_r, sin_r = {
36                 0: (1, 0),
37                 90: (0, 1),
38                 180: (-1, 0),
39                 270: (0, -1),
40         }[rotation]
41         x, y = coor
42         return (x*cos_r + y*sin_r, -x*sin_r + y*cos_r)
43
44 def get_angle_from_coordinates((x1,y1), (x2,y2)):
45         """
46         Given two points, calculate the vector direction from point1 to point2, directions are multiples of 90 degrees.
47         @param (x1,y1) the coordinate of point 1
48         @param (x2,y2) the coordinate of point 2
49         @return the direction in degrees
50         """
51         if y1 == y2:#0 or 180
52                 if x2 > x1: return 0
53                 else: return 180
54         else:#90 or 270
55                 if y2 > y1: return 270
56                 else: return 90
57
58 def parse_template(tmpl_str, **kwargs):
59         """
60         Parse the template string with the given args.
61         Pass in the xml encode method for pango escape chars.
62         @param tmpl_str the template as a string
63         @return a string of the parsed template
64         """
65         kwargs['encode'] = gobject.markup_escape_text
66         return str(Template(tmpl_str, kwargs))