Merged r9481:9518 on jblum/grc_reorganize into trunk. Reorganized grc source under...
[debian/gnuradio] / grc / src / platforms / gui / Utils.py
1 """
2 Copyright 2008 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
22 def get_rotated_coordinate(coor, rotation):
23         """
24         Rotate the coordinate by the given rotation.
25         @param coor the coordinate x, y tuple
26         @param rotation the angle in degrees
27         @return the rotated coordinates
28         """
29         #handles negative angles
30         rotation = (rotation + 360)%360
31         assert rotation in POSSIBLE_ROTATIONS
32         #determine the number of degrees to rotate
33         cos_r, sin_r = {
34                 0: (1, 0),
35                 90: (0, 1),
36                 180: (-1, 0),
37                 270: (0, -1),
38         }[rotation]
39         x, y = coor
40         return (x*cos_r + y*sin_r, -x*sin_r + y*cos_r)
41
42 def get_angle_from_coordinates((x1,y1), (x2,y2)):
43         """
44         Given two points, calculate the vector direction from point1 to point2, directions are multiples of 90 degrees.
45         @param (x1,y1) the coordinate of point 1
46         @param (x2,y2) the coordinate of point 2
47         @return the direction in degrees
48         """
49         if y1 == y2:#0 or 180
50                 if x2 > x1: return 0
51                 else: return 180
52         else:#90 or 270
53                 if y2 > y1: return 270
54                 else: return 90
55
56 def xml_encode(string):
57         """
58         Encode a string into an xml safe string by replacing special characters.
59         Needed for gtk pango markup in labels.
60         @param string the input string
61         @return output string with safe characters
62         """
63         string = str(string)
64         for char, safe in (
65                         ('&', '&'),
66                         ('<', '&lt;'),
67                         ('>', '&gt;'),
68                         ('"', '&quot;'),
69                         ("'", '&apos;'),
70         ): string = string.replace(char, safe)
71         return string