author | Radek Brich <radek.brich@devl.cz> |
Fri, 18 Jan 2013 22:36:50 +0100 | |
changeset 62 | 2f61931520c9 |
parent 59 | 729fcdfe6b57 |
child 63 | 2a0e04091898 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
3 |
import logging |
|
4 |
import time |
|
5 |
||
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
6 |
from tuikit.layout import AnchorLayout |
0 | 7 |
|
8 |
||
59 | 9 |
class Timer: |
10 |
def __init__(self): |
|
11 |
self.timeouts = [] |
|
12 |
self.timelast = None |
|
13 |
||
14 |
def add_timeout(self, delay, callback): |
|
15 |
"""Register callback to be called after delay seconds. |
|
16 |
||
17 |
delay -- in seconds, float |
|
18 |
callback -- function to be called with no parameters |
|
19 |
||
20 |
""" |
|
21 |
if not len(self.timeouts): |
|
22 |
self.timelast = time.time() |
|
23 |
self.timeouts += [[delay, callback]] |
|
24 |
||
25 |
def remove_timeout(self, callback): |
|
26 |
"""Unregister callback previously registered with add_timeout.""" |
|
27 |
for timeout in self.timeouts[:]: |
|
28 |
if timeout[1] == callback: |
|
29 |
self.timeouts.remove(timeout) |
|
30 |
||
31 |
def has_timeout(self): |
|
32 |
return len(self.timeouts) > 0 |
|
33 |
||
34 |
def nearest_timeout(self): |
|
35 |
if not self.has_timeout(): |
|
36 |
return None |
|
37 |
return min(self.timeouts)[0] |
|
38 |
||
39 |
def process_timeouts(self): |
|
40 |
if not self.has_timeout(): |
|
41 |
return |
|
42 |
now = time.time() |
|
43 |
lasted = now - self.timelast |
|
44 |
self.timelast = now |
|
45 |
for timeout in self.timeouts[:]: |
|
46 |
adjusted_delay = timeout[0] - lasted |
|
47 |
if adjusted_delay <= 0.0: |
|
48 |
timeout[1]() |
|
49 |
self.timeouts.remove(timeout) |
|
50 |
else: |
|
51 |
timeout[0] = adjusted_delay |
|
52 |
||
53 |
||
0 | 54 |
class Application: |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
55 |
|
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
56 |
'''Application class. Defines main loop.''' |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
57 |
|
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
58 |
def __init__(self, top_layout=AnchorLayout, driver='curses'): |
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
59 |
'''Create application.''' |
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
60 |
self._setup_logging() |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
61 |
|
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
62 |
# Top widget |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
63 |
self._top = None |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
64 |
self._timer = Timer() |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
65 |
self.top = top_layout() |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
66 |
|
0 | 67 |
self.quit = False |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
68 |
|
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
69 |
#: Driver class instance (render + input), e.g. DriverCurses. |
29
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
70 |
self.driver = self.get_driver_instance(driver) |
32
088b92ffb119
Clean up, refactoring. Rename EventSource to Emitter, begin merging emit() method with handle().
Radek Brich <radek.brich@devl.cz>
parents:
30
diff
changeset
|
71 |
|
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
72 |
def _setup_logging(self): |
0 | 73 |
self.log = logging.getLogger('tuikit') |
74 |
self.log.setLevel(logging.DEBUG) |
|
75 |
handler = logging.FileHandler('./tuikit.log') |
|
15 | 76 |
formatter = logging.Formatter('%(asctime)s %(levelname)-5s %(message)s', '%y-%m-%d %H:%M:%S') |
77 |
handler.setFormatter(formatter) |
|
0 | 78 |
self.log.addHandler(handler) |
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
79 |
|
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
80 |
@property |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
81 |
def top(self): |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
82 |
return self._top |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
83 |
|
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
84 |
@top.setter |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
85 |
def top(self, value): |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
86 |
self._top = value |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
87 |
self._top.top = value |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
88 |
self._top.timer = self._timer |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
89 |
self._top.add_handler('draw', self._on_top_draw) |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
90 |
|
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
91 |
def _on_top_draw(self, ev): |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
92 |
ev.driver.erase() |
0 | 93 |
|
94 |
def start(self): |
|
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
95 |
'''Start application. Runs main loop.''' |
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
96 |
self.log.info('=== start ===') |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
97 |
self.driver.start(self.main_loop) |
0 | 98 |
|
99 |
def terminate(self): |
|
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
100 |
'''Terminate application.''' |
0 | 101 |
self.quit = True |
102 |
||
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
103 |
def main_loop(self): |
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
104 |
'''The main loop.''' |
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
105 |
self.apply_theme() |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
106 |
self._top._size = self.driver.size # link top widget size to screen size |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
107 |
self._top.emit('resize') |
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
108 |
timer = self._timer |
0 | 109 |
|
110 |
while True: |
|
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
111 |
self._top.draw(self.driver, 0, 0) |
23
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
112 |
self.driver.commit() |
0 | 113 |
|
59 | 114 |
timeout = timer.nearest_timeout() |
24
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
115 |
events = self.driver.getevents(timeout) |
59 | 116 |
timer.process_timeouts() |
0 | 117 |
|
118 |
for event in events: |
|
24
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
119 |
if event[0] == 'quit': |
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
120 |
self.quit = True |
b248ef500557
Add DriverPygame (incomplete). Move unicode graphics constants to UnicodeGraphics class. Move shared parts of drivers to Driver base class.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
121 |
else: |
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
122 |
self._top.emit(event[0], *event[1:]) |
0 | 123 |
|
124 |
if self.quit: |
|
125 |
break |
|
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
126 |
self.log.info('=== quit ===') |
0 | 127 |
|
7
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
1
diff
changeset
|
128 |
|
62
2f61931520c9
Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents:
59
diff
changeset
|
129 |
def apply_theme(self): |
52
50a1857557da
Update SDL driver: Enlarge char, attr to 32 bits, 64 bits per terminal cell. Colors and attributes are complete, only blink does not work.
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
130 |
#TODO: allow custom colors: |
50a1857557da
Update SDL driver: Enlarge char, attr to 32 bits, 64 bits per terminal cell. Colors and attributes are complete, only blink does not work.
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
131 |
# e.g. "blue (#2020FF) on black (#101010), underline" |
50a1857557da
Update SDL driver: Enlarge char, attr to 32 bits, 64 bits per terminal cell. Colors and attributes are complete, only blink does not work.
Radek Brich <radek.brich@devl.cz>
parents:
49
diff
changeset
|
132 |
# color in brackets is used when driver supports custom colors |
23
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
133 |
driver = self.driver |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
134 |
driver.setcolor('normal', 'white on black') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
135 |
driver.setcolor('strong', 'white on black, bold') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
136 |
driver.setcolor('active', 'black on cyan') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
137 |
driver.setcolor('window:normal', 'white on blue') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
138 |
driver.setcolor('window:controls', 'white on blue, bold') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
139 |
driver.setcolor('window:controls-active', 'cyan on blue, bold') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
140 |
driver.setcolor('button', 'black on white') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
141 |
driver.setcolor('button-active', 'black on cyan') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
142 |
driver.setcolor('menu', 'black on cyan') |
4e72fd2a0e14
Rename BackendCurses to DriverCurses. Add DriverDummy - dummy driver for debugging purposes. Move clipping stack from driver to common.ClipStack class.
Radek Brich <radek.brich@devl.cz>
parents:
21
diff
changeset
|
143 |
driver.setcolor('menu-active', 'white on cyan, bold') |
30
05500124d7fb
Add setup script. Add Checkbox widget + demo. Updates.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
144 |
driver.setcolor('combo:normal', 'black on green') |
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
145 |
|
29
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
146 |
|
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
147 |
def get_driver_instance(self, name): |
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
148 |
module = __import__('tuikit.driver_' + name, fromlist=['driverclass']) |
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
149 |
return module.driverclass() |
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
150 |