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

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

"meta" menu, will list all the modules in use, and allow each one to
display status, memory use, cache size, warnings, etc.

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