nicer code (api v3 / oldapi)
[enigma2.git] / mytest.py
1 from enigma import *
2
3 import sys
4 import time
5
6 from screens import *
7 from skin import applyGUIskin
8
9 # A screen is a function which instanciates all components of a screen into a temporary component.
10 # Thus, the global stuff is a screen, too.
11 # In a screen, components can either be instanciated from the class-tree, cloned (copied) or
12 # "linked" from the instance tree.
13 # A screen itself lives as the container of the components, so a screen is a component, too.
14
15 # we thus have one (static) hierarchy of screens (classes, not instances)
16 # and one with the instanciated components itself (both global and dynamic)
17
18 def dump(dir, p = ""):
19         if isinstance(dir, dict):
20                 for (entry, val) in dir.items():
21                         dump(val, p + "/" + entry)
22         print p + ":" + str(dir.__class__)
23
24 # defined components
25 components = {}
26
27 # do global
28 screens["global"](components)
29
30 # test our screens
31 components["$001"] = screens["testDialog"]()
32
33 print "*** classes:"
34 dump(screens)
35
36 print "*** instances:"
37 dump(components)
38
39 # display
40
41 class OutputDevice:
42         def create(self, screen): pass
43
44 # display: HTML
45
46 class HTMLOutputDevice(OutputDevice):
47         def create(self, comp):
48                 print comp.produceHTML()
49
50 html = HTMLOutputDevice()
51
52 class GUIOutputDevice(OutputDevice):
53         parent = None
54         def create(self, comp):
55                 comp.createGUIScreen(self.parent)
56
57 class Session:
58         def __init__(self):
59                 self.desktop = None
60                 self.delayTimer = eTimer()
61                 self.delayTimer.timeout.get().append(self.processDelay)
62                 
63                 self.currentDialog = None
64         
65         def processDelay(self):
66                 self.currentDialog.close()
67                 if self.currentWindow != None:
68                         self.currentWindow.hide()
69                 
70                 del self.currentDialog
71                 del self.currentWindow
72                 
73                 self.open(screens["testDialog"]())
74
75         def open(self, screen):
76                 self.currentDialog = screen
77                 screen.session = self
78                 
79                 if self.desktop != None:
80                         self.currentWindow = wnd = eWindow(self.desktop)
81                         wnd.setTitle("Screen from python!")
82                         wnd.move(ePoint(300, 100))
83                         wnd.resize(eSize(300, 300))
84
85                         gui = GUIOutputDevice()
86                         gui.parent = wnd
87                         gui.create(self.currentDialog)
88
89                         applyGUIskin(self.currentDialog, None, screen.__class__.__name__)
90
91                         wnd.show()
92                 else:
93                         self.currentWindow = None
94
95         def close(self):
96                 self.delayTimer.start(0, 1)
97
98 def runScreenTest():
99         session = Session()
100         session.desktop = getDesktop()
101         
102         session.open(screens["clockDisplay"](components["clock"]))
103 #       session.open(screens["testDialog"]())
104
105         # simple reason for this helper function: we want to call the currently
106         # active "okbutton", even when we changed the dialog
107         #
108         # more complicated reason: we don't want to hold a reference.
109         def blub():
110                 session.currentDialog.data["okbutton"]["instance"].push()
111         
112         tmr = eTimer()
113         tmr.timeout.get().append(blub)
114         tmr.start(4000, 0)
115         
116         runMainloop()
117         
118         return 0
119
120
121 # first, setup a screen
122 runScreenTest()
123
124 # now, run the mainloop