~richardjones/withgui/trunk

« back to all changes in this revision

Viewing changes to withgui/withsimplui.py

  • Committer: Richard Jones
  • Date: 2009-08-27 05:57:06 UTC
  • Revision ID: richard@l-rjones.off.ekorp.com-20090827055706-rm3nnqy4sonkot95
reorg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import sys
 
3
 
 
4
import pyglet
 
5
import simplui
 
6
 
 
7
import withgui
 
8
from withtk import HasProperties
 
9
 
 
10
class GUI(object):
 
11
    def __init__(self, spec):
 
12
        # base pyglet setup
 
13
        self.window = pyglet.window.Window(caption='withgui')
 
14
        self.gui = self
 
15
 
 
16
        theme = kytten.Theme(os.path.join(os.getcwd(), 'theme'), override={
 
17
            "gui_color": [128, 128, 128, 255],
 
18
            "font_size": 14
 
19
        })
 
20
        top_frame = kytten.VerticalLayout([Implementation.create(child, self).kytten_widget
 
21
            for child in spec.children])
 
22
 
 
23
        self.dialog = kytten.Dialog(top_frame, window=self.window, theme=theme)
 
24
        self.dialog.do_layout()
 
25
        self.dialog.needs_layout = True
 
26
        self.window.set_size(self.dialog.width, self.dialog.height)
 
27
 
 
28
        self.window.push_handlers(self)
 
29
 
 
30
    def on_draw(self):
 
31
        self.window.clear()
 
32
        self.dialog.draw()
 
33
 
 
34
    def update(self, dt):
 
35
        self.dialog.on_update(dt)
 
36
 
 
37
    def run(self):
 
38
        pyglet.clock.schedule(self.update)
 
39
        pyglet.app.run()
 
40
 
 
41
    def stop(self, data=None):
 
42
        pyglet.app.stop()
 
43
        return data
 
44
 
 
45
class Implementation(HasProperties):
 
46
    '''Wrapper around a kytten widget allowing read/write access
 
47
    to the widget's properties including its current value.
 
48
    '''
 
49
    implementation_classes = {}
 
50
    @classmethod
 
51
    def register_class(cls, klass, name=None):
 
52
        if name is None:
 
53
            name = klass.__name__
 
54
        cls.implementation_classes[name] = klass
 
55
 
 
56
    @classmethod
 
57
    def create(cls, spec, parent):
 
58
        klass = cls.implementation_classes[spec.__class__.__name__]
 
59
        return klass(spec, parent)
 
60
 
 
61
    def __init__(self, spec, parent):
 
62
        self.spec = spec
 
63
        self.parent = parent
 
64
        self.gui = parent.gui
 
65
        self.kytten_widget = None
 
66
        self.args = self.generate_properties(spec)
 
67
 
 
68
        self.create_widget()
 
69
 
 
70
        # set up event handlers
 
71
        if 'on_mouse' in spec.event_handlers:
 
72
            TODO
 
73
            spec.event_handlers['on_mouse']
 
74
 
 
75
        self.spec.implementation = self
 
76
 
 
77
    def create_widget(self):
 
78
        '''Create the underlying kytten_widget 
 
79
        '''
 
80
        raise NotImplementeError('create_widget must be implemented')
 
81
 
 
82
    def set_value(self, value):
 
83
        # TODO no value modification in kytten yet
 
84
        TODO
 
85
    def get_value(self):
 
86
        return self.kytten_widget.get_value()
 
87
    def set_property(self, name, value):
 
88
        name, value = self.translate_value(name, value)
 
89
        if name == 'value':
 
90
            self.set_value(value)
 
91
            return
 
92
        if name == 'x':
 
93
            self.kytten_widget.x = value
 
94
            self.kytten_widget.place(x=value)
 
95
        elif name == 'y':
 
96
            self.kytten_widget.y = value
 
97
            self.kytten_widget.place(y=value)
 
98
        else:
 
99
            # TODO no property modification in kytten yet
 
100
            TODO
 
101
    def get_property(self, name):
 
102
        if name == 'value':
 
103
            return self.get_value()
 
104
        elif name == 'x':
 
105
            return self.kytten_widget.x
 
106
        elif name == 'y':
 
107
            return self.kytten_widget.y
 
108
        else:
 
109
            return getattr(self.kytten_widget, name)
 
110
 
 
111
    def destroy(self):
 
112
        self.spec.implementation = None
 
113
        self.parent = None
 
114
        self.gui = None
 
115
        self.args = None
 
116
        self.kytten_widget.delete()
 
117
        self.kytten_widget = None
 
118
 
 
119
class Frame(Implementation):
 
120
    def create_widget(self):
 
121
 
 
122
        width = self.args.get('width')
 
123
        height = self.args.get('height')
 
124
 
 
125
        image = self.args.get('image')
 
126
        if image is not None:
 
127
            if 'width' is None:
 
128
                width = image.width()
 
129
            if 'height' is None:
 
130
                height = image.height()
 
131
 
 
132
        self.kytten_widget = kytten.FreeLayout(width, height)
 
133
 
 
134
        # XXX do somehing with image
 
135
 
 
136
        for child in spec.children:
 
137
            self.add_child(child)
 
138
 
 
139
    def add_child(self, spec):
 
140
            i = Implementation.create(child, self)
 
141
            anchor = i.args['anchor']
 
142
            x = i.args['x']
 
143
            y = i.args['y']
 
144
            self.kytten_widget.add(anchor, x, y, i.kytten_widget)
 
145
Implementation.register_class(Frame)
 
146
 
 
147
class Column(Implementation):
 
148
    def create_widget(self):
 
149
        self.kytten_widget = kytten.VerticalLayout([
 
150
            Implementation.create(child, self).kytten_widget
 
151
                for child in self.spec.children])
 
152
    def add_child(self, spec):
 
153
            i = Implementation.create(child, self)
 
154
            self.add(i.kytten_widget)
 
155
Implementation.register_class(Column)
 
156
 
 
157
class Row(Implementation):
 
158
    def create_widget(self):
 
159
        self.kytten_widget = kytten.HorizontalLayout([
 
160
            Implementation.create(child, self).kytten_widget
 
161
                for child in self.spec.children])
 
162
    def add_child(self, spec):
 
163
            i = Implementation.create(child, self)
 
164
            self.add(i.kytten_widget)
 
165
Implementation.register_class(Row)
 
166
 
 
167
class Label(Implementation):
 
168
    def create_widget(self):
 
169
        self.kytten_widget = kytten.Label(self.args['value'])
 
170
Implementation.register_class(Label)
 
171
 
 
172
class Button(Implementation):
 
173
    def create_widget(self):
 
174
        def click(spec=self.spec):
 
175
            return spec.event_handlers['on_click'](spec)
 
176
        self.kytten_widget = kytten.Button(self.args['value'], on_click=click)
 
177
Implementation.register_class(Button)
 
178
 
 
179
class Selection(Implementation):
 
180
    properties = dict(
 
181
        options=('options', HasProperties.prop_passthrough),
 
182
        value=('value', HasProperties.prop_passthrough),
 
183
    )
 
184
    def create_widget(self):
 
185
        options = self.args['options']
 
186
        value = self.args.get('value', options[0])
 
187
        self.kytten_widget = kytten.Dropdown(options=options, selected=value)
 
188
Implementation.register_class(Selection)
 
189
 
 
190
class Text(Implementation):
 
191
    def create_widget(self):
 
192
        value = self.args.get('value', '')
 
193
        self.kytten_widget = kytten.Input(text=text)
 
194
    def set_value(self, value):
 
195
        self.kytten_widget.set_text(value)
 
196
Implementation.register_class(Text)
 
197