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

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

allow brightness control (not that it should be needed on mobile device, but nice on my laptop)

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