source: subversion/applications/routing/pyroute/overlayArea.py @ 5864

Last change on this file since 5864 was 5864, checked in by ojw, 12 years ago

test for gps "select source" menu

File size: 5.1 KB
Line 
1#!/usr/bin/python
2#-----------------------------------------------------------------------------
3# Graphics library for drawing pyroute GUI menus
4#
5# Usage:
6#   (library code for pyroute GUI)
7#
8# Features:
9#   Start by initialising a 'rectangle' that covers a certain bit of screen
10#   Then you can
11#     * Draw an icon there
12#     * Draw text in the rectangle
13#     * Make it clickable (so that it triggers an event when clicked)
14#     * Split it into smaller rectangles (to do complex GUI layouts)
15#-----------------------------------------------------------------------------
16# Copyright 2007, Oliver White
17#
18# This program is free software: you can redistribute it and/or modify
19# it under the terms of the GNU General Public License as published by
20# the Free Software Foundation, either version 3 of the License, or
21# (at your option) any later version.
22#
23# This program is distributed in the hope that it will be useful,
24# but WITHOUT ANY WARRANTY; without even the implied warranty of
25# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26# GNU General Public License for more details.
27#
28# You should have received a copy of the GNU General Public License
29# along with this program.  If not, see <http://www.gnu.org/licenses/>.
30#-----------------------------------------------------------------------------
31import cairo
32import os
33
34class overlayArea:
35    def __init__(self,cr,x,y,dx,dy,modules,iconSet):
36        self.cr = cr
37        self.x1 = x;
38        self.y1 = y;
39        self.x2 = x + dx;
40        self.y2 = y + dy;
41        self.w = dx
42        self.h = dy
43        self.cx = x + 0.5 * dx
44        self.cy = y + 0.5 * dy
45        self.event = None
46        self.modules = modules
47        self.iconSet = iconSet
48    def fill(self,r,g,b,outline=0):
49        self.cr.set_source_rgb(r,g,b)
50        self.cr.rectangle(self.x1,self.y1,self.w,self.h)
51        if(outline):
52            self.cr.stroke()
53        else:
54            self.cr.fill()
55    def drawTextSomewhere(self,text,px1,py1,px2,py2):
56        innerBox = self.copyself(px1,py1,px2,py2)
57        innerBox.drawText(text)
58    def drawText(self,text):
59        self.cr.select_font_face('Verdana', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
60        test_fontsize = 60
61        self.cr.set_font_size(test_fontsize)
62        xbearing, ybearing, textwidth, textheight, xadvance, yadvance = self.cr.text_extents(text)
63        # Resize the font to fit
64        ratiox = textwidth / self.w
65        ratioy = textheight / self.h
66        ratio = max(ratiox,ratioy)
67       
68        self.cr.set_font_size(test_fontsize / ratio)
69        textwidth = textwidth / ratio
70        textheight = textheight / ratio
71
72        marginx = 0.5 * (self.w - textwidth)
73        marginy = 0.5 * (self.h - textheight)
74        # Text
75        self.cr.move_to(self.x1 + marginx, self.y2 - marginy)
76        self.cr.set_source_rgba(0, 0, 0, 0.5)
77        self.cr.show_text(text)
78       
79    def button(self,text=None,event=None,icon=None):
80        # Rectangle
81        if(not icon):
82          if(text):
83            self.icon("generic")
84          else:
85            self.icon("blank")
86        else:
87            self.icon(icon)
88        if(text):
89            self.drawTextSomewhere(text,0.2,0.6,0.8,0.8)
90        if(event):
91            self.setEvent(event)
92    def icon(self, icon):
93      self.iconSet.draw(self.cr,icon,self.x1,self.y1,self.w,self.h)
94    def setEvent(self,text):
95        self.event = text
96    def xc(self,p):
97        return(self.x1 + p * self.w)
98    def yc(self,p):
99        return(self.y1 + p * self.h)
100    def copyself(self,px1,py1,px2,py2):
101        x1 = self.xc(px1)
102        y1 = self.yc(py1)
103        return(overlayArea( \
104            self.cr,
105            x1,
106            y1,
107            self.xc(px2) - x1,
108            self.yc(py2) - y1,
109            self.modules,
110            self.iconSet))
111    def copyAndExtendTo(self,otherRect):
112        return(overlayArea( \
113            self.cr,
114            self.x1,
115            self.y1,
116            otherRect.x2 - self.x1,
117            otherRect.y2 - self.y1,
118            self.modules,
119            self.iconSet))
120    def xsplit(self,p):
121        a = self.copyself(0,0,p,1)
122        b = self.copyself(p,0,1,1)
123        return(a,b)
124    def ysplit(self,p):
125        a = self.copyself(0,0,1,p)
126        b = self.copyself(0,p,1,1)
127        return(a,b)
128    def xsplitn(self,px1,py1,px2,py2,n):
129        dpx = (px2 - px1) / n
130        cells = []
131        for i in range(0,n-1):
132            px = px1 + i * dpx
133            cells.append(self.copyself(px,py1,px + dpx,py2))
134        return(cells)
135    def ysplitn(self,px1,py1,px2,py2,n):
136        dpy = (py2 - py1) / n
137        cells = []
138        for i in range(0,n):
139            py = py1 + i * dpy
140            cells.append(self.copyself(px1,py,px2,py+dpy))
141        return(cells)
142           
143    def contains(self,x,y):
144        if(x > self.x1 and x < self.x2 and y > self.y1 and y < self.y2):
145            return(1)
146    def handleClick(self,x,y):
147        if(self.event):
148          self.modules['events'].handleEvent(self.event)
149          return(1)
150        return(0)
Note: See TracBrowser for help on using the repository browser.