Source

ttgui / tobject.py

Full commit
  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
# -*- coding: utf-8 -*-
from console import Console, debug


class TEvent(object):
    pass


class TKeyEvent(TEvent):
    pass


class TPaintEvent(TEvent):

    def __init__(self, rect=None):
        super(TPaintEvent, self).__init__()
        self.rect = rect


class TCloseEvent(TEvent):
    pass


class THideEvent(TEvent):
    pass


class TShowEvent(TEvent):
    pass


class TResizeEvent(TEvent):
    
    def __init__(self, size):
        super(TResizeEvent, self).__init__()
        self.size = size


class TEventLoop(object):
    _instance = None

    def __init__(self):
        self.queue = []


    @classmethod
    def instance(cls):
        if not cls._instance:
            cls._instance = cls()
        return cls._instance


    def postEvent(self, obj, event):
        self.queue.insert(0, (obj, event))


    def processEvent(self):
        if self.queue:
            obj, event = self.queue.pop()
            debug("processing event", event, "for", obj)
            obj.event(event)
            return True
        return False



class TObject(object):
    
    def __init__(self, parent=None):
        self._children = set()
        self._parent = parent
        self._event_filters = set()
        if parent:
            parent._children.add(self)
            
            
    def children(self):
        return list(self._children)
    
    
    def parent(self):
        return self._parent
    
    
    def installEventFilter(self, receiver_object):
        self._event_filters.add(receiver_object)


    def postEvent(self, event):
        TEventLoop.instance().postEvent(self, event)


    def event(self, event):
        for receiver_object in self._event_filters:
            if receiver_object.eventFilter(self, event):
                return True
        return False
            
    
    def eventFilter(self, obj, event):
        pass
    

    
class TSize(object):
    
    def __init__(self, width, height):
        self.width = width
        self.height = height



class TPoint(object):
    
    def __init__(self, x, y):
        self.x = x
        self.y = y


        
class TRect(object):
    

    def __init__(self, point, size):
        self.topLeft = point
        self.size = size





class TApplication(TObject):

    instance = None

    
    def __init__(self):
        super(TApplication, self).__init__()
        self.event_loop = TEventLoop.instance()
        self._desktop = TDesktopWidget()
        TApplication.instance = self

        
    def desktop(self):
        return self._desktop


    def postEvent(self, receiver_object, event):
        self.event_loop.postEvent(receiver_object, event)
    
    
    def sendEvent(self, receiver_object, event):
        self.event_loop.sendEvent(receiver_object, event)

    
    def notify(self, receiver_object, event):
        self.event_loop.notify(receiver_object, event)
        
        
    def processEvent(self):
        return self.event_loop.processEvent()
    
        
    def run(self):
        self._desktop.show()
        while True:
            self.processEvent()

            
            


class TWidget(TObject):

    
    def __init__(self, parent=None):
        super(TWidget, self).__init__(parent)
        self._update_geom(TPoint(0,0), TSize(1,1))
        self._visible = True
        

    def window(self):
        parent = self
        parent_parent = parent.parent()
        while parent_parent:
            parent = parent_parent
            parent_parent = parent.parent()
        return parent

    
    def close(self):
        pass
    
        
    def show(self):
        self.setVisible(True)
        

    def hide(self):
        self.setVisible(False)

        
    def setVisible(self, flag):
        self._visible = flag
        self.postEvent(TPaintEvent())
        if flag:
            self.postEvent(TShowEvent())
        else:    
            self.postEvent(THideEvent())
        for child in self.children():
            child.setVisible(flag)


    def _update_geom(self, pos, size):
        x1 = pos.x
        y1 = pos.y
        x2 = x1 + size.width
        y2 = y1 + size.height
        self._geom = (x1, y1, x2, y2)
        self.postEvent(TResizeEvent(size))

        
    def size(self):
        (x1, y1, x2, y2) = self._geom
        width, height = x2 - y1, y2 - y1
        return TSize(width, height)
    

    def resize(self, size):
        (x1, y1, x2, y2) = self._geom
        width, height = size.width, size.height
        self._update_geom(TPoint(x1, y1), TSize(width, height))

        
    def pos(self):
        (x1, y1, x2, y2) = self._geom
        return TPoint(x1, y1)


    def move(self, pos):
        self._update_geom(pos, self.size())


    def geometry(self):
        return TRect(self.pos(), self.size())

    
    def setGeometry(self, pos, size):
        self._update_geom(pos, size)

    
    def mapToParent(self, pos):
        parent = self.parent()
        if not parent:
            return TPoint(pos.x, pos.y)
        else:
            ppos = parent.pos()
            return TPoint(ppos.x + pos.x, ppos.y + pos.y)


    def mapToGlobal(self, pos):
        obj = self
        while obj:
            pos = obj.mapToParent(pos)
            obj = obj.parent()
        return pos

    
    def event(self, event):
        if super(TWidget, self).event(event):
            return True
        if isinstance(event, TPaintEvent):
            return self.paintEvent(event)
        elif isinstance(event, TKeyEvent):
            return self.keyPressevent(event)

            
    def keyPressEvent(self, event):
        pass


    def paintEvent(self, event):
        painter = TPainter(self)
        if self.parent():
            painter.fillRect(self.geometry(), " ")
        else:
            rect = self.geometry()
            painter.drawRect(rect)
            pos = rect.topLeft
            width, height = rect.size.width, rect.size.height
            inner = TRect(TPoint(pos.x, pos.y), TSize(width-1, height-1))
            painter.fillRect(inner, " ")

    def resizeEvent(self, event):
        pass

    
    def update(self, rect=None):
        self.postEvent(TPaintEvent(rect))
    
        
    def repaint(self, rect=None):
        event = TPaintEvent(rect)
        self.paintEvent(event)


        
class TPainter(object):


    def __init__(self, obj=None):
        self.stack = []
        if obj:
            self.stack.append(obj)
        self.obj = obj
        self.con = Console.instance


    def begin(self, obj):
        self.stack.append(obj)
        self.obj = obj
        

    def end(self):
        self.stack.pop()
        if self.stack:
            self.obj = self.stack[-1]
        else:
            self.obj = None
            
            
    def drawRect(self, rect):
        gpos = self.obj.mapToGlobal(TPoint(0, 0))
        x1 = gpos.x + rect.topLeft.x
        y1 = gpos.y + rect.topLeft.y
        width = rect.size.width
        height = rect.size.height
        x2 = x1 + width - 1
        y2 = y1 + height - 1
        # topleft
        self.con.text(x1, y1, "+")
        # topright
        self.con.text(x2, y1, "+")
        # top line
        self.con.text(x1 + 1, y1, "-" * (width-2))
        # bottomleft
        self.con.text(x1, y2, "+")
        # bottomright
        self.con.text(x2, y2, "+")
        # bottom line
        self.con.text(x1+1, y2, "-" * (width-2))
        # side lines
        for y in range(y1+1, y2):
            self.con.text(x1, y, "|") # left
            self.con.text(x2, y, "|") # right

        
    def drawText(self, pos, text):
        gpos = self.obj.mapToGlobal(pos)
        self.con.text(gpos.x, gpos.y, text)
        


    def fillRect(self, rect, char):
        gpos = self.obj.mapToGlobal(TPoint(0, 0))
        x1 = gpos.x + rect.topLeft.x
        y1 = gpos.y + rect.topLeft.y
        width = rect.size.width
        height = rect.size.height
        x2 = x1 + width - 1
        y2 = y1 + height - 1
        for y in range(y1, y2):
            self.con.text(x1, y, char*width)
    


class TDesktopWidget(TWidget):


    def __init__(self):
        super(TDesktopWidget, self).__init__()
        self._update_geom(TPoint(1, 1), TSize(78, 23))
        self.console = Console()

        
    def paintEvent(self, event):
        painter = TPainter(self)
        painter.fillRect(self.geometry(), ".")

        
class TLabel(TWidget):


    def __init__(self, parent=None, text=None):
        super(TLabel, self).__init__(parent)
        self.setText(text)
        if text:
            self.resize(TSize(min(70, len(text)), 1))

        
    def setText(self, text):
        self._text = text
        self.update()

        
    def text(self):
        return self._text
    

    def paintEvent(self, event):
        painter = TPainter(self)
        painter.drawText(self.pos(), self.text())