Package Gnumed :: Package timelinelib :: Package drawing :: Module utils
[frames] | no frames]

Source Code for Module Gnumed.timelinelib.drawing.utils

 1  # Copyright (C) 2009, 2010, 2011  Rickard Lindberg, Roger Lindberg 
 2  # 
 3  # This file is part of Timeline. 
 4  # 
 5  # Timeline is free software: you can redistribute it and/or modify 
 6  # it under the terms of the GNU General Public License as published by 
 7  # the Free Software Foundation, either version 3 of the License, or 
 8  # (at your option) any later version. 
 9  # 
10  # Timeline 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 Timeline.  If not, see <http://www.gnu.org/licenses/>. 
17   
18   
19  """ 
20  Utilities used by drawers. 
21  """ 
22   
23   
24  import wx 
25   
26   
27 -class Metrics(object):
28 """ 29 Convert between pixel coordinates and time coordinates. 30 """ 31
32 - def __init__(self, size, time_type, time_period, divider_line_slider):
33 self.width, self.height = size 34 self.half_width = self.width / 2 35 self.half_height = self.height / 2 36 self.half_height = int(round(divider_line_slider * self.height)) 37 self.time_type = time_type 38 self.time_period = time_period
39
40 - def calc_exact_x(self, time):
41 """Return the x position in pixels as a float for the given time.""" 42 delta1 = self.time_type.div_timedeltas(time - self.time_period.start_time, 43 self.time_period.delta()) 44 float_res = self.width * delta1 45 return float_res
46
47 - def calc_x(self, time):
48 """Return the x position in pixels as an integer for the given time.""" 49 return int(round(self.calc_exact_x(time)))
50
51 - def calc_exact_width(self, time_period):
52 """Return the with in pixels as a float for the given time_period.""" 53 return (self.calc_exact_x(time_period.end_time) - 54 self.calc_exact_x(time_period.start_time))
55
56 - def calc_width(self, time_period):
57 """Return the with in pixels as an integer for the given time_period.""" 58 return (self.calc_x(time_period.end_time) - 59 self.calc_x(time_period.start_time)) + 1
60
61 - def get_time(self, x):
62 """Return the time at pixel `x`.""" 63 return self.time_type.get_time_at_x(self.time_period, float(x) / self.width)
64
65 - def get_difftime(self, x1, x2):
66 """Return the time length between two x positions.""" 67 return self.get_time(x1) - self.get_time(x2)
68 69
70 -def get_default_font(size, bold=False):
71 if bold: 72 weight = wx.FONTWEIGHT_BOLD 73 else: 74 weight = wx.FONTWEIGHT_NORMAL 75 return wx.Font(size, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, weight)
76 77
78 -def darken_color(color, factor=0.7):
79 r, g, b = color 80 new_r = int(r * factor) 81 new_g = int(g * factor) 82 new_b = int(b * factor) 83 return (new_r, new_g, new_b)
84