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

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

Generalise checkbox code

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
93    def icon(self, icon):
94      self.iconSet.draw(self.cr,icon,self.x1,self.y1,self.w,self.h)
95    def setEvent(self,text):
96        self.event = text
97    def xc(self,p):
98        return(self.x1 + p * self.w)
99    def yc(self,p):
100        return(self.y1 + p * self.h)
101    def copyself(self,px1,py1,px2,py2):
102        x1 = self.xc(px1)
103        y1 = self.yc(py1)
104        return(overlayArea( \
105            self.cr,
106            x1,
107            y1,
108            self.xc(px2) - x1,
109            self.yc(py2) - y1,
110            self.modules,
111            self.iconSet))
112    def copyAndExtendTo(self,otherRect):
113        return(overlayArea( \
114            self.cr,
115            self.x1,
116            self.y1,
117            otherRect.x2 - self.x1,
118            otherRect.y2 - self.y1,
119            self.modules,
120            self.iconSet))
121    def xsplit(self,p):
122        a = self.copyself(0,0,p,1)
123        b = self.copyself(p,0,1,1)
124        return(a,b)
125    def ysplit(self,p):
126        a = self.copyself(0,0,1,p)
127        b = self.copyself(0,p,1,1)
128        return(a,b)
129    def xsplitn(self,px1,py1,px2,py2,n):
130        dpx = (px2 - px1) / n
131        cells = []
132        for i in range(0,n-1):
133            px = px1 + i * dpx
134            cells.append(self.copyself(px,py1,px + dpx,py2))
135        return(cells)
136    def ysplitn(self,px1,py1,px2,py2,n):
137        dpy = (py2 - py1) / n
138        cells = []
139        for i in range(0,n):
140            py = py1 + i * dpy
141            cells.append(self.copyself(px1,py,px2,py+dpy))
142        return(cells)
143           
144    def contains(self,x,y):
145        if(x > self.x1 and x < self.x2 and y > self.y1 and y < self.y2):
146            return(1)
147    def handleClick(self,x,y):
148        if(self.event):
149          self.modules['events'].handleEvent(self.event)
150          return(1)
151        return(0)
Note: See TracBrowser for help on using the repository browser.