ttgui / tgui.py

  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
413
414
415
416
417
418
419
420
# -*- coding: utf-8 -*-
from console import Console, debug
from tcore import TSize, TPoint, TRect, TEvent, TEventLoop, TObject


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 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 drawLine(self, start_pos, end_pos):
        x1, y1 = start_pos.x, start_pos.y
        x2, y2 = end_pos.x, end_pos.y
        if y1 == y1:
            width = x2 - x1
            self.con.text(x1, y1, "-" * width)
        elif x1 == x2:
            for y in range(y1, y2+1):
                self.con.text(x1, y, "|")
        else:
            raise NotImplementedError("Only horizontal and vertial lines supported right now")

        
    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 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), self.sizeHint(), silent=True)
        self._visible = True
        

    def sizeHint(self):
        return TSize(1,1)

        
    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, silent=False):
        x1 = pos.x
        y1 = pos.y
        x2 = x1 + size.width
        y2 = y1 + size.height
        self._geom = (x1, y1, x2, y2)
        if not silent:
            self.postEvent(TResizeEvent(size))

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

    
    def left(seft):
        return self.pos().x

    
    def top(self):
        return self.pos().y
    


    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)
            painter.fillRect(rect.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 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 AbstractTextWidgetMixin(object):

    
    def __init__(self, text=""):
        self._text = text

            
    def sizeHint(self):
        return TSize(min(70, len(self._text)), 1)


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


    def text(self):
        return self._text



class TLabel(TWidget, AbstractTextWidgetMixin):

    
    def __init__(self, parent=None, text=""):
        AbstractTextWidgetMixin.__init__(self, text)
        TWidget.__init__(self, parent)

    

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



        
class TAbstractButton(TWidget, AbstractTextWidgetMixin):

    
    def __init__(self, parent=None, text="", checked=False):
        AbstractTextWidgetMixin.__init__(self, text)
        self._checked = checked
        TWidget.__init__(self, parent)


    def setChecked(self, flag):
        self._checked = flag
        self.update()
        
        
    def isChecked(self):
        return self._checked


        
        
class TCheckBox(TAbstractButton):
    
    
    def paintEvent(self, event):
        painter = TPainter(self)
        checked = self._checked and "x" or " "
        painter.drawText(self.pos(), "[%s] %s" % (checked, self.text()))

        

class TRadioButton(TAbstractButton):
    
    
    def paintEvent(self, event):
        painter = TPainter(self)
        checked = self._checked and "x" or " "
        painter.drawText(self.pos(), "(%s) %s" % (checked, self.text()))



        
class TPushButton(TAbstractButton):
    
    
    def paintEvent(self, event):
        painter = TPainter(self)
        painter.drawText(self.pos(), "< %s >" % self.text())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.