ARGH
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / examples / gui9.py
1 """<title>Menus, Toolboxes, a full Application</title>
2 Most all widgets are used in this example.  A full custom widget
3 is included.  A number of connections are used to make the application
4 function.
5 """
6 import pygame
7 from pygame.locals import *
8
9 # the following line is not needed if pgu is installed
10 import sys; sys.path.insert(0, "..")
11
12 from pgu import gui
13
14 from gui6 import AboutDialog
15 from gui7 import ColorDialog
16 from gui8 import NewDialog
17
18 class HelpDialog(gui.Dialog):
19     def __init__(self,**params):
20         title = gui.Label("Help")
21         
22         doc = gui.Document(width=400)
23         
24         space = title.style.font.size(" ")
25         
26         doc.block(align=-1)
27         doc.add(gui.Image("cuzco.png"),align=1)
28         for word in """Cuzco's Paint is a revolutionary new paint program it has all the awesome features that you need to paint really great pictures.""".split(" "): 
29             doc.add(gui.Label(word))
30             doc.space(space)
31         doc.br(space[1])
32         
33         doc.block(align=-1)    
34         for word in """This help isn't really here for any other reason than to have a help dialog.""".split(" "): 
35             doc.add(gui.Label(word))
36             doc.space(space)
37
38         gui.Dialog.__init__(self,title,doc)
39         
40 class QuitDialog(gui.Dialog):
41     def __init__(self,**params):
42         title = gui.Label("Quit")
43         
44         t = gui.Table()
45         
46         t.tr()
47         t.add(gui.Label("Are you sure you want to quit?"),colspan=2)
48         
49         t.tr()
50         e = gui.Button("Okay")
51         e.connect(gui.CLICK,self.send,gui.QUIT)
52         t.td(e)
53         
54         e = gui.Button("Cancel")
55         e.connect(gui.CLICK,self.close,None)
56         t.td(e)
57         
58         gui.Dialog.__init__(self,title,t)
59
60 class WelcomeDialog(gui.Dialog):
61     def __init__(self,**params):
62         title = gui.Label("Welcome")
63         
64         doc = gui.Document(width=400)
65         
66         space = title.style.font.size(" ")
67
68         doc.block(align=-1)
69         doc.add(gui.Image("cuzco.png"),align=1)
70         for word in """Welcome to Cuzco's Paint.  Cuzco's Paint is a demo of the features of Phil's Pygame GUI.  Cuzco's Paint only supports saving in the .TGA format.""".split(" "): 
71             doc.add(gui.Label(word))
72             doc.space(space)
73
74         gui.Dialog.__init__(self,title,doc)
75
76 class OpenDialog(gui.Dialog):
77     def __init__(self,**params):
78         title = gui.Label("Open Picture")
79         
80         t = gui.Table()
81         
82         self.value = gui.Form()
83         
84         t.tr()
85         t.td(gui.Label("Open: "))
86         t.td(gui.Input(name="fname"),colspan=3)
87         
88         t.tr()
89         e = gui.Button("Okay")
90         e.connect(gui.CLICK,self.send,gui.CHANGE)
91         t.td(e,colspan=2)
92         
93         e = gui.Button("Cancel")
94         e.connect(gui.CLICK,self.close,None)
95         t.td(e,colspan=2)
96         
97         gui.Dialog.__init__(self,title,t)
98
99 class SaveDialog(gui.Dialog):
100     def __init__(self,**params):
101         title = gui.Label("Save As...")
102         
103         t = gui.Table()
104         
105         self.value = gui.Form()
106         
107         t.tr()
108         t.td(gui.Label("Save: "))
109         t.td(gui.Input(name="fname"),colspan=3)
110         
111         t.tr()
112         e = gui.Button("Okay")
113         e.connect(gui.CLICK,self.send,gui.CHANGE)
114         t.td(e,colspan=2)
115         
116         e = gui.Button("Cancel")
117         e.connect(gui.CLICK,self.close,None)
118         t.td(e,colspan=2)
119         
120         gui.Dialog.__init__(self,title,t)
121
122
123 class Painter(gui.Widget):
124     def __init__(self,**params):
125         gui.Widget.__init__(self,**params)
126         
127         self.surface = None
128         self.state = 0
129         self.cuzco = pygame.image.load("cuzco.png")
130         
131     def init(self,v):
132         self.surface = pygame.Surface((int(v['width']),int(v['height'])))
133         color = v['color']
134         if v['color'] == 'custom': 
135             color = v['custom']
136         else: color = pygame.Color(color)
137         self.surface.fill(color)
138         self.overlay = pygame.Surface((int(v['width']),int(v['height'])),pygame.SRCALPHA,32)
139         self.repaint()
140         
141     def event(self,e):
142         if not self.surface: return
143         
144         if e.type == gui.MOUSEBUTTONDOWN:
145             if hasattr(self,app.mode.value+"_down"):
146                 action = getattr(self,app.mode.value+"_down")
147                 action(e)
148         if e.type == gui.MOUSEMOTION:
149             if hasattr(self,app.mode.value+"_motion"):
150                 action = getattr(self,app.mode.value+"_motion")
151                 action(e)
152         if e.type is gui.MOUSEBUTTONUP:
153             if hasattr(self,app.mode.value+"_up"):
154                 action = getattr(self,app.mode.value+"_up")
155                 action(e)
156     
157     ##The Painter class has its own paint method to render the painting surface and overlay.
158     ##::
159     def paint(self,s):
160         s.blit(self.surface,(0,0))
161         s.blit(self.overlay,(0,0))
162     ##
163                             
164     def draw_down(self,e):
165         self.state = 1
166         self.pos = e.pos
167         self.draw_motion(e)
168     def draw_motion(self,e):
169         if self.state == 0: return
170         pygame.draw.line(self.surface,app.color.value,self.pos,e.pos,2)
171         self.pos = e.pos
172         self.repaint()
173     def draw_up(self,e):
174         self.state = 0
175         
176     def cuzco_down(self,e):
177         self.state = 1
178         self.cuzco_motion(e)
179     def cuzco_motion(self,e):
180         if self.state == 0: return
181         img = self.cuzco
182         self.overlay.fill((0,0,0,0))
183         self.overlay.blit(img,(e.pos[0]-img.get_width()/2,e.pos[1]-img.get_height()/2))
184         self.repaint()
185     def cuzco_up(self,e):
186         self.state = 0
187         self.surface.blit(self.overlay,(0,0))
188         self.overlay.fill((0,0,0,0))
189         self.repaint()
190         
191     def box_down(self,e):
192         self.state = 1
193         self.pos = e.pos
194         self.box_motion(e)
195     def box_motion(self,e):
196         if self.state == 0: return
197         self.overlay.fill((0,0,0,0))
198         pygame.draw.rect(self.overlay,app.color.value,(self.pos[0],self.pos[1],e.pos[0]-self.pos[0],e.pos[1]-self.pos[1]))
199         self.repaint()
200     def box_up(self,e):
201         self.state = 0
202         self.surface.blit(self.overlay,(0,0))
203         self.overlay.fill((0,0,0,0))
204         self.repaint()
205         
206     
207     def circle_down(self,e):
208         self.state = 1
209         self.pos = e.pos
210         self.circle_motion(e)
211     def circle_motion(self,e):
212         if self.state == 0: return
213         self.overlay.fill((0,0,0,0))
214         r = pygame.Rect(self.pos[0],self.pos[1],e.pos[0]-self.pos[0],e.pos[1]-self.pos[1])
215         r.x -= r.w
216         r.w *= 2
217         r.y -= r.h
218         r.h *= 2
219         r.normalize()
220         pygame.draw.ellipse(self.overlay,app.color.value,r)
221         self.repaint()
222     def circle_up(self,e):
223         self.state = 0
224         self.surface.blit(self.overlay,(0,0))
225         self.overlay.fill((0,0,0,0))
226         self.repaint()
227         
228         
229
230 class App(gui.Desktop):
231     def __init__(self,**params):
232         gui.Desktop.__init__(self,**params)
233         
234         self.connect(gui.QUIT,self.quit,None)
235         
236         c = gui.Container(width=640,height=480)
237         spacer = 8
238         
239         self.fname = 'untitled.tga'
240         
241         self.new_d = NewDialog()
242         self.new_d.connect(gui.CHANGE,self.action_new,None)
243         self.open_d = OpenDialog()
244         self.open_d.connect(gui.CHANGE,self.action_open,None)
245         self.save_d = SaveDialog()
246         self.save_d.connect(gui.CHANGE,self.action_saveas,None)
247         self.quit_d = QuitDialog()
248         self.quit_d.connect(QUIT,self.quit,None)
249         
250         self.help_d = HelpDialog()
251         self.about_d = AboutDialog()
252         
253         ##Initializing the Menus, we connect to a number of Dialog.open methods for each of the dialogs.
254         ##::
255         menus = gui.Menus([
256             ('File/New',self.new_d.open,None),
257             ('File/Open',self.open_d.open,None),
258             ('File/Save',self.action_save,None),
259             ('File/Save As',self.save_d.open,None),
260             ('File/Exit',self.quit_d.open,None),
261             ('Help/Help',self.help_d.open,None),
262             ('Help/About',self.about_d.open,None),
263             ])
264         ##
265         c.add(menus,0,0)
266         menus.rect.w,menus.rect.h = menus.resize()
267         #print 'menus',menus.rect
268         
269         ##We utilize a Toolbox.  The value of this widget determins how drawing is done in the Painter class.
270         ##::
271         self.mode = mode = gui.Toolbox([
272             ('Draw','draw'),
273             ('Box','box'),
274             ('Circle','circle'),
275             ('Cuzco','cuzco'),
276             ],cols=1,value='draw')
277         ##
278         c.add(mode,0,menus.rect.bottom+spacer)
279         mode.rect.x,mode.rect.y = mode.style.x,mode.style.y
280         mode.rect.w,mode.rect.h = mode.resize()
281         #mode._resize()
282         
283         default = "#000000"
284         self.color = color = gui.Color(default,width=mode.rect.w,height=mode.rect.w)
285         self.color_d = ColorDialog(default)
286         
287         color.connect(gui.CLICK,self.color_d.open,None)
288
289         # Updates the toolbox color picker with the value in the color dialog box
290         def change_cb(*args):
291             self.color.value = self.color_d.value
292         self.color_d.connect(gui.CHANGE, change_cb)
293
294         c.add(self.color,0,mode.rect.bottom+spacer)
295         self.color.rect.w,self.color.rect.h = self.color.resize()
296         #self.color._resize()
297
298         self.painter = Painter(width=c.rect.w-mode.rect.w-spacer*2,height=c.rect.h-menus.rect.h-spacer*2,style={'border':1})
299         c.add(self.painter,mode.rect.w+spacer,menus.rect.h+spacer)
300         self.painter.init({'width':256,'height':256,'color':'#ffffff'})
301         self.painter.rect.w,self.painter.rect.h = self.painter.resize()
302         #self.painter._resize()
303
304         welcome_d = WelcomeDialog()
305         self.connect(gui.INIT,welcome_d.open,None)
306         
307         self.widget = c
308         
309     def action_new(self,value):
310         self.new_d.close()
311         self.fname = 'untitled.tga'
312         self.painter.init(self.new_d.value.results())
313         
314     def action_save(self,value):
315         pygame.image.save(self.painter.surface,self.fname)
316         
317     def action_saveas(self,value):
318         self.save_d.close()
319         self.fname = self.save_d.value['fname'].value
320         pygame.image.save(self.painter.surface,self.fname)
321         
322     def action_open(self,value):
323         self.open_d.close()
324         self.fname = self.open_d.value['fname']
325         self.painter.surface = pygame.image.load(self.fname)
326         self.painter.repaint()
327         
328         
329 app = App()
330 app.run()

UCC git Repository :: git.ucc.asn.au