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

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

Move the map tile code into its own module

Add headers and usage instructions to a bunch of modules

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 mainMenuButton(self):
56        self.cr.set_line_width(2)
57        self.cr.set_dash((2,2,2), 0);
58        self.cr.set_source_rgba(0.4,0,0)
59        self.cr.arc(self.cx,self.cy, 0.5*self.w, 0, 2*3.14)
60        self.cr.stroke()
61        self.setEvent("menu:main")
62    def drawTextSomewhere(self,text,px1,py1,px2,py2):
63        innerBox = self.copyself(px1,py1,px2,py2)
64        innerBox.drawText(text)
65    def drawText(self,text):
66        self.cr.select_font_face('Verdana', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
67        test_fontsize = 60
68        self.cr.set_font_size(test_fontsize)
69        xbearing, ybearing, textwidth, textheight, xadvance, yadvance = self.cr.text_extents(text)
70        # Resize the font to fit
71        ratiox = textwidth / self.w
72        ratioy = textheight / self.h
73        ratio = max(ratiox,ratioy)
74       
75        self.cr.set_font_size(test_fontsize / ratio)
76        textwidth = textwidth / ratio
77        textheight = textheight / ratio
78
79        marginx = 0.5 * (self.w - textwidth)
80        marginy = 0.5 * (self.h - textheight)
81        # Text
82        self.cr.move_to(self.x1 + marginx, self.y2 - marginy)
83        self.cr.set_source_rgba(0, 0, 0, 0.5)
84        self.cr.show_text(text)
85       
86    def button(self,text=None,event=None,icon=None):
87        # Rectangle
88        if(not icon):
89          if(text):
90            self.iconSet.draw(self.cr,"generic",self.x1,self.y1,self.w,self.h)
91          else:
92            self.iconSet.draw(self.cr,"blank",self.x1,self.y1,self.w,self.h)
93        if(icon):
94            self.iconSet.draw(self.cr,icon,self.x1,self.y1,self.w,self.h)
95        if(text):
96            self.drawTextSomewhere(text,0.2,0.6,0.8,0.8)
97        if(event):
98            self.setEvent(event)
99    def setEvent(self,text):
100        self.event = text
101    def xc(self,p):
102        return(self.x1 + p * self.w)
103    def yc(self,p):
104        return(self.y1 + p * self.h)
105    def copyself(self,px1,py1,px2,py2):
106        x1 = self.xc(px1)
107        y1 = self.yc(py1)
108        return(overlayArea( \
109            self.cr,
110            x1,
111            y1,
112            self.xc(px2) - x1,
113            self.yc(py2) - y1,
114            self.modules,
115            self.iconSet))
116    def xsplit(self,p):
117        a = self.copyself(0,0,p,1)
118        b = self.copyself(p,0,1,1)
119        return(a,b)
120    def ysplit(self,p):
121        a = self.copyself(0,0,1,p)
122        b = self.copyself(0,p,1,1)
123        return(a,b)
124    def xsplitn(self,px1,py1,px2,py2,n):
125        dpx = (px2 - px1) / n
126        cells = []
127        for i in range(0,n-1):
128            px = px1 + i * dpx
129            cells.append(self.copyself(px,py1,px + dpx,py2))
130        return(cells)
131    def ysplitn(self,px1,py1,px2,py2,n):
132        dpy = (py2 - py1) / n
133        cells = []
134        for i in range(0,n):
135            py = py1 + i * dpy
136            cells.append(self.copyself(px1,py,px2,py+dpy))
137        return(cells)
138           
139    def contains(self,x,y):
140        if(x > self.x1 and x < self.x2 and y > self.y1 and y < self.y2):
141            return(1)
142    def handleClick(self,x,y):
143        if(self.event):
144            self.modules['data'].handleEvent(self.event)
145            return(1)
146        return(0)
Note: See TracBrowser for help on using the repository browser.