My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# -*- coding: utf-8 -*-
"""

Marcelo Barros de Almeida
marcelobarrosalmeida (at) gmail.com

== Introduction ==

The API for writing applications using Python for S60 is really straightforward.
Since you have decided what kind of body your application will use (canvas,
listbox, or text), you need to define your main menu, application title
and default callback handler for exiting. The user interface (UI) is based
on events and there is a special mechanism that relies on a semaphore for
indicating if the application is closing. The user must obtain the semaphore
and wait for any signal on it. Once signalized, the application may close
properly. The UI allows tabs as well but in this case a body must be defined
for each tab. An routine for handling tabs changing is required in such
situation.

However, if your application has more than one window, I mean, more than a
set of body, menu and exit handler, you will need exchanging these elements
each time a new window is displayed, giving to the user the impression that
he is using a multiple windows application. Although this solution is possible,
some problems arise:

* the strategy for exiting, based on semaphores, must be unique across your
application.
* you can not commit any mistake when switching UI, that is, the set
body+menu+exit handler must be consistently changed
* an unified strategy for blocking UI when a time consuming operation is
pending is necessary. For instance, when downloading a file, you may want
to disable all menu options. Otherwise they will be available for the user
during the download operation.


For unifying this process, three additional classes are suggested in this
article. The first, called Window, is responsible for holding UI contents
like menu, body and exit handler and exchanging properly all UI elements
for the derived classes. Moreover, it may lock/unlock the UI when necessary.
The second class is named Application. It represents the running application
itself and it is responsible for handling the termination semaphore. Only one
Application class must be instantiated per application. Finally, the third
class is called Dialog. As its name suggests, it is in the charge of
showing/hiding dialogs when necessary. Many dialogs are allowed, each one
with their own set of body+menu+exit handler.

Application and Dialog inherit from Window the content handler ability while
each one has different ways for finishing itself (finishing application or
just the dialog).

== Usage examples ==

Using this framework, user attention is focused on application and not
on appuifw stuff. Consider this first example, with only one window
(no dialogs). Since your program inherits from Application, default actions
for some specific appuifw are already defined:

* Termination semaphore is controlled by Application class, being properly
initialized inside its constructor (Application.__lock = e32.Ao_lock()).
Only one Application object per program is allowed.
* exit_default_handler is set to Application.close_app(), where termination
semaphore is set (Application.__lock.signal()).
* Application.run() initializes the application, calling refresh() for
updating the UI and waits for termination semaphore. When signalized, all
necessary cleanup is done and application is closed.

We can see one simple example in action:

<code python>
# -*- coding: utf-8 -*-
#
# Marcelo Barros de Almeida
# marcelobarrosalmeida (at) gmail.com
#
from window import Application
from appuifw import Listbox, note

class MyApp(Application):
def __init__(self):
items = [ u"Option A",
u"Option B",
u"Option C" ]
menu = [ (u"Menu A", self.option_a),
(u"Menu B", self.option_b),
(u"Menu C", self.option_c) ]
body = Listbox(items, self.check_items )
Application.__init__(self,
u"MyApp title",
body,
menu)

def check_items(self):
idx = self.body.current()
( self.option_a, self.option_b, self.option_c )[idx]()

def option_a(self): note(u"A","info")
def option_b(self): note(u"B","info")
def option_c(self): note(u"C","info")

if __name__ == "__main__":

app = MyApp()
app.run()
</code>

Of course, if you need special actions, like some confirmation when exiting,
this may be implemented just overriding close_app():

<code python>
# -*- coding: utf-8 -*-
#
# Marcelo Barros de Almeida
# marcelobarrosalmeida (at) gmail.com
#
from window import Application
from appuifw import Listbox, note, popup_menu

class MyApp(Application):
def __init__(self):
items = [ u"Option A",
u"Option B",
u"Option C" ]
menu = [ (u"Menu A", self.option_a),
(u"Menu B", self.option_b),
(u"Menu C", self.option_c) ]
body = Listbox(items, self.check_items )
Application.__init__(self,
u"MyApp title",
body,
menu)

def check_items(self):
idx = self.body.current()
( self.option_a, self.option_b, self.option_c )[idx]()

def option_a(self): note(u"A","info")
def option_b(self): note(u"B","info")
def option_c(self): note(u"C","info")

def close_app(self):
ny = popup_menu( [u"No", u"Yes"], u"Exit ?")
if ny is not None:
if ny == 1:
Application.close_app(self)

if __name__ == "__main__":

app = MyApp()
app.run()
</code>

== Dialogs ==

Suppose now user wants to add a dialog call. Really simple and hiding all
appuifw stuff:

<code python>
# -*- coding: utf-8 -*-
#
# Marcelo Barros de Almeida
# marcelobarrosalmeida (at) gmail.com
#
from window import Application, Dialog
from appuifw import Listbox, note, popup_menu, Text

class Notepad(Dialog):
def __init__(self, cbk, txt=u""):
menu = [(u"Save", self.close_app),
(u"Discard", self.cancel_app)]
Dialog.__init__(self, cbk, u"MyDlg title", Text(txt), menu)

class MyApp(Application):
def __init__(self):
self.txt = u""
items = [ u"Text editor",
u"Option B",
u"Option C" ]
menu = [ (u"Text editor", self.text_editor),
(u"Menu B", self.option_b),
(u"Menu C", self.option_c) ]
body = Listbox(items, self.check_items )
Application.__init__(self,
u"MyApp title",
body,
menu)

def check_items(self):
idx = self.body.current()
( self.text_editor, self.option_b, self.option_c )[idx]()

def text_editor(self):
def cbk():
if not self.dlg.cancel:
self.txt = self.dlg.body.get()
note(self.txt, "info")
self.refresh()
return True

self.dlg = Notepad(cbk, self.txt)
self.dlg.run()

def option_b(self): note(u"B","info")
def option_c(self): note(u"C","info")

def close_app(self):
ny = popup_menu( [u"No", u"Yes"], u"Exit ?")
if ny is not None:
if ny == 1:
Application.close_app(self)

if __name__ == "__main__":

app = MyApp()
app.run()
</code>

When a dialog is created, a callback function need to be defined. This
callback is called when the user cancels or closes the dialog. Inside the
callback body, it is possible to check if the dialog was canceled just
verifying the cancel variable. Dialog variables may be accessed as well
as in any other python object and this is the way of retrieving dialog data.

The callback function must either return True or False, before finishing.
If it returns '''True''', ''self.refresh() must be called before, inside
callback body''. This way, the ''menu, body and exit handler will be updated
using the context of the dialog caller'' (MyApp, in this case).
If it returns '''False''', ''self.refresh() is called inside dialog context
and the dialog is restored''. This is an excellent way to check dialog data
and to avoid data loss. A better example with this feature is given below
(see '''number_sel()''' method).

<code python>
# -*- coding: utf-8 -*-
#
# Marcelo Barros de Almeida
# marcelobarrosalmeida (at) gmail.com
#
from window import Application, Dialog
from appuifw import Listbox, note, popup_menu, Text

class Notepad(Dialog):
def __init__(self, cbk, txt=u""):
menu = [(u"Save", self.close_app),
(u"Discard", self.cancel_app)]
Dialog.__init__(self, cbk, u"MyDlg title", Text(txt), menu)

class NumSel(Dialog):
def __init__(self, cbk):
self.items = [ u"1", u"2", u"a", u"b" ]
Dialog.__init__(self, cbk,
u"Select a number",
Listbox(self.items, self.close_app))

class MyApp(Application):
def __init__(self):
self.txt = u""
items = [ u"Text editor",
u"Number selection",
u"Option C" ]
menu = [ (u"Text editor", self.text_editor),
(u"Number selection", self.number_sel),
(u"Menu C", self.option_c) ]
body = Listbox(items, self.check_items )
Application.__init__(self,
u"MyApp title",
body,
menu)

def check_items(self):
idx = self.body.current()
( self.text_editor, self.number_sel, self.option_c )[idx]()

def text_editor(self):
def cbk():
if not self.dlg.cancel:
self.txt = self.dlg.body.get()
note(self.txt, "info")
self.refresh()
return True

self.dlg = Notepad(cbk, self.txt)
self.dlg.run()

def number_sel(self):
def cbk():
if not self.dlg.cancel:
val = self.dlg.items[self.dlg.body.current()]
try:
n = int(val)
except:
note(u"Invalid number. Try again.", "info")
return False
note(u"Valid number", "info")
self.refresh()
return True

self.dlg = NumSel(cbk)
self.dlg.run()

def option_c(self): note(u"C","info")

def close_app(self):
ny = popup_menu( [u"No", u"Yes"], u"Exit ?")
if ny is not None:
if ny == 1:
Application.close_app(self)

if __name__ == "__main__":

app = MyApp()
app.run()
</code>

As last example, how about changing menu and body dynamically ? Yes, it is
possible, just overriding '''refresh()'''. refresh() is responsible for
all UI updates, assigning desired values for menu, body and exit_handler.
If you made changes to any UI elements, it is necessary to redraw it. In
the next example, '''NameList()''' dialog has its menu and body contents
changed dynamically.

<code python>
# -*- coding: utf-8 -*-
#
# Marcelo Barros de Almeida
# marcelobarrosalmeida (at) gmail.com
#
from window import Application, Dialog
from appuifw import Listbox, note, popup_menu, Text, query, app

class Notepad(Dialog):
def __init__(self, cbk, txt=u""):
menu = [(u"Save", self.close_app),
(u"Discard", self.cancel_app)]
Dialog.__init__(self, cbk, u"MyDlg title", Text(txt), menu)

class NumSel(Dialog):
def __init__(self, cbk):
self.items = [ u"1", u"2", u"a", u"b" ]
Dialog.__init__(self, cbk,
u"Select a number",
Listbox(self.items, self.close_app))

class NameList(Dialog):
def __init__(self, cbk, names=[]):
self.names = names
self.body = Listbox([(u"")],self.options)
# Do not populate Listbox() ! Refresh will do this.
Dialog.__init__(self, cbk, u"Name list", self.body)

def options(self):
op = popup_menu( [u"Insert", u"Del"] , u"Names:")
if op is not None:
if op == 0:
name = query(u"New name:", "text", u"" )
if name is not None:
self.names.append(name)
print self.names
elif self.names:
del self.names[self.body.current()]
# Menu and body are changing !
# You need to refresh the interface.
self.refresh()

def refresh(self):
menu = []
if self.names:
menu = map(lambda x: (x, lambda: None), self.names)
items = self.names
else:
items = [u"<empty>"]
self.menu = menu + [(u"Exit", self.close_app)]
self.body.set_list(items,0)
# Since your self.menu and self.body have already defined their
# new values, call base class refresh()
Dialog.refresh(self)

class MyApp(Application):
def __init__(self):
self.txt = u""
self.names = []
items = [ u"Text editor",
u"Number selection",
u"Name list" ]
menu = [ (u"Text editor", self.text_editor),
(u"Number selection", self.number_sel),
(u"Name list", self.name_list) ]
body = Listbox(items, self.check_items )
Application.__init__(self,
u"MyApp title",
body,
menu)

def check_items(self):
idx = self.body.current()
( self.text_editor, self.number_sel, self.name_list )[idx]()

def text_editor(self):
def cbk():
if not self.dlg.cancel:
self.txt = self.dlg.body.get()
note(self.txt, "info")
self.refresh()
return True

self.dlg = Notepad(cbk, self.txt)
self.dlg.run()

def number_sel(self):
def cbk():
if not self.dlg.cancel:
val = self.dlg.items[self.dlg.body.current()]
try:
n = int(val)
except:
note(u"Invalid number", "info")
return False
note(u"Valid number", "info")
self.refresh()
return True

self.dlg = NumSel(cbk)
self.dlg.run()

def name_list(self):
def cbk():
if not self.dlg.cancel:
self.names = self.dlg.names
self.refresh()
return True

self.dlg = NameList(cbk, self.names)
self.dlg.run()

def close_app(self):
ny = popup_menu( [u"No", u"Yes"], u"Exit ?")
if ny is not None:
if ny == 1:
Application.close_app(self)

if __name__ == "__main__":

app = MyApp()
app.run()
</code>

== Tabbed dialogs ==

Tabbed dialogs are supporting as well. In this case, the body must be replaced by a list of bodies with the following format:

[(tab_text, body, menu),(tab_text, body, menu),...]


where:
* tab_text: unicode string used in tab
* body: a valid body (Listbox, Text or Canvas)
* menu: menu for that body

Each entry in this list will be displayed in a tab. You can specify a global menu to be added to the bottom of each tab menu. This way, it is simple to share common function (like exit) among all tabs. Just specify this menu when calling Dialog() or Application.

<code python>
# -*- coding: utf-8 -*-
#
# Marcelo Barros de Almeida
# marcelobarrosalmeida (at) gmail.com
# License: GPL3

from window import Application
from appuifw import *

class MyApp(Application):
def __init__(self):
# defining menus for each tab/body
ma=[(u"menu a",lambda:self.msg(u"menu a"))]
mb=[(u"menu b",lambda:self.msg(u"menu b"))]
mc=[(u"menu c",lambda:self.msg(u"menu c"))]
md=[(u"menu d",lambda:self.msg(u"menu d"))]
# common menu for all tabs
common_menu=[(u"common menu",lambda:self.msg(u"common menu"))]
# bodies
ba=Listbox([u"a",u"b",u"c"])
bb=Canvas()
bc=Text(u"Text")
bd=Listbox([u"1",u"2",u"3"])

Application.__init__(self,
u"MyApp title",
[(u"Tab a",ba,ma),(u"Tab b",bb,mb),
(u"Tab c",bc,mc),(u"Tab d",bd,md)],
common_menu)

def msg(self,m):
note(m,"info")

if __name__ == "__main__":
app = MyApp()
app.run()
</code>

== Locking UI ==

For time consuming operations, like network connections, it is interesting
to lock the user interface, avoiding undesired user actions. Two methods
are used in such situation: '''lock_ui()''' and '''unlock_ui()'''. Just
lock the UI, do wherever you want, and unlock UI. If you want, change the
application title during this locking.

<code python>
self.lock_ui(u"Connecting...")
#
# your stuff here
#
self.unlock_ui()
self.set_title(u"My app")
</code>

"""

from appuifw import *
import e32
import key_codes
__all__ = [ "Application", "Dialog" ]

class Window(object):
""" This class is responsible for holding UI contents like menu, body
and exit handler and exchanging properly all UI elements for
the derived classes. Moreover, it may lock/unlock the UI when necessary.
"""
__ui_lock = False

def __init__(self, title, body, global_menu = None, exit_handler = None):
""" Creates a new window given a title, body or a set of bodies (for tabbed
window) and optional global_menu and exit handler. Global menu is
available for all tabs at bottom or it is used as the default
menu for non tabbed window

The list of bodies must have the following format:
[(tab_text, body, menu),(tab_text, body, menu),...]

where:
tab_text: unicode string used in tab
body: a valid body (Listbox, Text or Canvas)
menu: typical menu
"""
self.title = title

if isinstance(body,list):
self.tabbed = True
self.bodies = body
self.body = None
else:
self.tabbed = False
self.bodies = None
self.body = body

if global_menu is None:
global_menu = [(u"Close", self.close_app)]

if exit_handler is None:
exit_handler = self.close_app

self.global_menu = global_menu
self.exit_handler = exit_handler
self.last_tab = 0

def set_title(self,title):
" Sets the current application title "
app.title = self.title = title

def get_title(self):
" Returns the current application title "
return self.title

def bind(self, key, cbk):
" Bind a key to the body. A callback must be provided."
self.body.bind(key,cbk)

def refresh(self):
" Update the application itens (menu, body and exit handler) "
if self.tabbed:
app.set_tabs([b[0] for b in self.bodies],self.tab_handler)
self.tab_handler(self.last_tab)
app.activate_tab(self.last_tab)
else:
app.set_tabs([], None)
app.menu = self.global_menu
app.body = self.body
app.title = self.title
app.exit_key_handler = self.exit_handler

def tab_handler(self,idx):
" Update tab and its related contents "
self.last_tab = idx
self.body = self.bodies[idx][1]
self.menu = self.bodies[idx][2] + self.global_menu
app.title = self.title
app.menu = self.menu
app.body = self.body

def run(self):
" Show the dialog/application "
self.refresh()

def lock_ui(self,title = u""):
""" Lock UI (menu, body and exit handler are disabled).
You may set a string to be shown in the title area.
"""
Window.__ui_lock = True
app.menu = []
app.set_tabs([], None)
app.exit_key_handler = lambda: None
if title:
app.title = title

def unlock_ui(self):
"Unlock UI. Call refresh() for restoring menu, body and exit handler."
Window.__ui_lock = False

def ui_is_locked(self):
"Chech if UI is locked or not, return True or False"
return Window.__ui_lock

class Application(Window):
""" This class represents the running application itself
and it is responsible for handling the termination semaphore.
Only one Application class must be instantiated per application.
"""
__highlander = None
__lock = None

def __init__(self, title, body, menu = None, exit_handler = None):
""" Only one application is allowed. It is resposible for starting
and finishing the program.
run() is overrided for controling this behavior.
"""
if Application.__highlander:
raise "Only one Application() allowed"
Application.__highlander = self

if not Application.__lock:
Application.__lock = e32.Ao_lock()

Window.__init__(self, title, body, menu, exit_handler)

def close_app(self):
""" Signalize the application lock, allowing run() to terminate the application.
"""
Application.__lock.signal()

def run(self):
""" Show the the application and wait until application lock is
signalized. After that, make all necessary cleanup.
"""
old_title = app.title
self.refresh()
Application.__lock.wait()
# restore everything !
app.set_tabs( [], None )
app.title = old_title
app.menu = []
app.body = None
app.set_exit()

class Dialog(Window):
""" This class is in the charge of showing/hiding dialogs when necessary.
Many dialogs are allowed, each one with their own set of body+menu+exit
handler.
"""
def __init__(self, cbk, title, body, menu = None, exit_handler = None):
""" Create a dialog. cbk is called when dialog is closing.
Dialog contents, like title and body need
to be specified. If menu or exit_handler are not specified,
defautl values for dialog class are used.
"""
self.cbk = cbk
self.cancel = False
Window.__init__(self, title, body, menu, exit_handler)

def close_app(self):
""" When closing the dialog, a call do self.cbk() is done.
If this function returns True the dialog is not refreshed
and the latest dialog/window takes control. If something fails
during calback execution, callback function should return False
and does not call refresh(). Using self.cancel it is possible
to determine when the dialog was canceled or not.
"""
if self.cbk() == False:
self.refresh()

def cancel_app(self):
""" Close the dialog but turn the cancel flag on.
"""
self.cancel = True
self.close_app()

Change log

r646 by marcelobarrosalmeida on Aug 7, 2009   Diff
Qik API first import
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 23427 bytes, 693 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting