author | Radek Brich <radek.brich@devl.cz> |
Wed, 03 Sep 2014 19:13:37 +0200 | |
changeset 110 | cf3d49cdd6e2 |
parent 77 | fc1989059e19 |
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.''' |
74
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
60 |
self.cfg = { |
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
61 |
'driver': driver, |
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
62 |
'log_level': 'info', |
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
63 |
} |
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
64 |
self._load_conf('tuikit.conf') |
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
|
65 |
self._setup_logging() |
63
2a0e04091898
Rework MenuBar. Add MenuButton. Add mouse event cascading to floaters.
Radek Brich <radek.brich@devl.cz>
parents:
62
diff
changeset
|
66 |
|
77
fc1989059e19
Propagate "quit" event, do not just terminate application. Resize: flag widgets to be resized, do resizes only once before draw. Draw: flag widgets to be redrawn, do not draw everything on any event.
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
67 |
#: Driver class instance (render + input), e.g. DriverCurses. |
fc1989059e19
Propagate "quit" event, do not just terminate application. Resize: flag widgets to be resized, do resizes only once before draw. Draw: flag widgets to be redrawn, do not draw everything on any event.
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
68 |
self.driver = self.get_driver_instance(self.cfg['driver']) |
fc1989059e19
Propagate "quit" event, do not just terminate application. Resize: flag widgets to be resized, do resizes only once before draw. Draw: flag widgets to be redrawn, do not draw everything on any event.
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
69 |
|
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
|
70 |
# 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
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
|
77
fc1989059e19
Propagate "quit" event, do not just terminate application. Resize: flag widgets to be resized, do resizes only once before draw. Draw: flag widgets to be redrawn, do not draw everything on any event.
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
75 |
self._quit = False |
74
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
76 |
|
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
77 |
def _load_conf(self, file_name): |
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
78 |
try: |
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
79 |
with open(file_name, 'r') as f: |
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
80 |
content = f.read() |
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
81 |
exec(content, self.cfg) |
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
82 |
except IOError: |
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
83 |
pass |
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
|
84 |
|
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
|
85 |
def _setup_logging(self): |
0 | 86 |
self.log = logging.getLogger('tuikit') |
74
23767a33a781
Add ScrollWindow. Rewrite EditBox to work with OffsetLayout. Add propery "exposed" to DrawEvent. Add Widget._view_size. Add config file (driver, log_level).
Radek Brich <radek.brich@devl.cz>
parents:
70
diff
changeset
|
87 |
self.log.setLevel(self.cfg['log_level'].upper()) |
0 | 88 |
handler = logging.FileHandler('./tuikit.log') |
15 | 89 |
formatter = logging.Formatter('%(asctime)s %(levelname)-5s %(message)s', '%y-%m-%d %H:%M:%S') |
90 |
handler.setFormatter(formatter) |
|
0 | 91 |
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
|
92 |
|
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
|
93 |
@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
|
94 |
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
|
95 |
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
|
96 |
|
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 |
@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
|
98 |
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
|
99 |
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
|
100 |
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
|
101 |
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
|
102 |
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
|
103 |
|
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
|
104 |
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
|
105 |
ev.driver.erase() |
0 | 106 |
|
107 |
def start(self): |
|
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
108 |
'''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
|
109 |
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
|
110 |
self.driver.start(self.main_loop) |
0 | 111 |
|
112 |
def terminate(self): |
|
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
113 |
'''Terminate application.''' |
77
fc1989059e19
Propagate "quit" event, do not just terminate application. Resize: flag widgets to be resized, do resizes only once before draw. Draw: flag widgets to be redrawn, do not draw everything on any event.
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
114 |
self._quit = True |
0 | 115 |
|
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
|
116 |
def main_loop(self): |
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
117 |
'''The main loop.''' |
70
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
118 |
self.startup() |
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
|
119 |
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
|
120 |
timer = self._timer |
0 | 121 |
|
77
fc1989059e19
Propagate "quit" event, do not just terminate application. Resize: flag widgets to be resized, do resizes only once before draw. Draw: flag widgets to be redrawn, do not draw everything on any event.
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
122 |
while not self._quit: |
fc1989059e19
Propagate "quit" event, do not just terminate application. Resize: flag widgets to be resized, do resizes only once before draw. Draw: flag widgets to be redrawn, do not draw everything on any event.
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
123 |
self._top.complete_requests() |
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
|
124 |
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
|
125 |
self.driver.commit() |
0 | 126 |
|
59 | 127 |
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
|
128 |
events = self.driver.getevents(timeout) |
59 | 129 |
timer.process_timeouts() |
0 | 130 |
|
131 |
for event in events: |
|
77
fc1989059e19
Propagate "quit" event, do not just terminate application. Resize: flag widgets to be resized, do resizes only once before draw. Draw: flag widgets to be redrawn, do not draw everything on any event.
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
132 |
self._top.emit(event[0], *event[1:]) |
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
|
133 |
self.log.info('=== quit ===') |
0 | 134 |
|
70
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
135 |
def startup(self): |
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
136 |
"""This is called after startup, before entering event loop.""" |
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
137 |
self.apply_theme() |
7
d4a291b31cbb
New color management - named colors.
Radek Brich <radek.brich@devl.cz>
parents:
1
diff
changeset
|
138 |
|
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
|
139 |
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
|
140 |
#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
|
141 |
# 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
|
142 |
# color in brackets is used when driver supports custom colors |
70
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
143 |
drv = self.driver |
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
144 |
drv.defcolor('normal', 'lightgray on black') |
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
145 |
drv.defcolor('strong', 'white on black, bold') |
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
146 |
drv.defcolor('active', 'black on cyan') |
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
147 |
drv.defcolor('window:normal', 'lightgray on blue') |
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
148 |
drv.defcolor('window:controls', 'white on blue, bold') |
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
149 |
drv.defcolor('window:controls-active', 'cyan on blue, bold') |
77
fc1989059e19
Propagate "quit" event, do not just terminate application. Resize: flag widgets to be resized, do resizes only once before draw. Draw: flag widgets to be redrawn, do not draw everything on any event.
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
150 |
drv.defcolor('button', 'black on lightgray') |
70
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
151 |
drv.defcolor('button-active', 'black on cyan') |
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
152 |
drv.defcolor('menu', 'black on cyan') |
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
153 |
drv.defcolor('menu-active', 'white on cyan, bold') |
db2eab0beb45
Update drivers: Rename setcolor to defcolor, add real setcolor which ignores color stack.
Radek Brich <radek.brich@devl.cz>
parents:
69
diff
changeset
|
154 |
drv.defcolor('combo:normal', 'black on green') |
13
19ebde2fd594
Add more generated documentation.
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
155 |
|
29
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
156 |
|
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
157 |
def get_driver_instance(self, name): |
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
158 |
module = __import__('tuikit.driver_' + name, fromlist=['driverclass']) |
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
159 |
return module.driverclass() |
c0cdef06fd16
Import only one driver from application.
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
160 |