diff --git a/TermTk/TTkCore/canvas.py b/TermTk/TTkCore/canvas.py index 8964a41d..0e010c9d 100644 --- a/TermTk/TTkCore/canvas.py +++ b/TermTk/TTkCore/canvas.py @@ -117,45 +117,46 @@ class TTkCanvas: def drawBox(self, pos, size, color=TTkColor.RST): self.drawGrid(pos=pos, size=size, color=color) - def drawGrid(self, pos, size, hlines=[], vlines=[], color=TTkColor.RST): + def drawGrid(self, pos, size, hlines=[], vlines=[], color=TTkColor.RST, grid=0): if not self._visible: return x,y = pos w,h = size + gg = self._theme.grid[grid] # 4 corners - self._set(y, x, self._theme.grid[2], color) - self._set(y, x+w-1, self._theme.grid[3], color) - self._set(y+h-1, x, self._theme.grid[4], color) - self._set(y+h-1, x+w-1, self._theme.grid[5], color) + self._set(y, x, gg[2], color) + self._set(y, x+w-1, gg[3], color) + self._set(y+h-1, x, gg[4], color) + self._set(y+h-1, x+w-1, gg[5], color) if w > 2: for i in range(x+1,x+w-1): - self._set(y, i, self._theme.grid[0], color) - self._set(y+h-1, i, self._theme.grid[0], color) + self._set(y, i, gg[0], color) + self._set(y+h-1, i, gg[0], color) if h > 2: for i in range(y+1,y+h-1): - self._set(i, x, self._theme.grid[1], color) - self._set(i, x+w-1, self._theme.grid[1], color) + self._set(i, x, gg[1], color) + self._set(i, x+w-1, gg[1], color) # Draw horizontal lines for iy in hlines: iy += y if not (0 < iy < h): continue - self._set(iy, x, self._theme.grid[6], color) - self._set(iy, x+w-1, self._theme.grid[7], color) + self._set(iy, x, gg[6], color) + self._set(iy, x+w-1, gg[7], color) if w > 2: for ix in range(x+1,x+w-1): - self._set(iy, ix, self._theme.grid[10], color) + self._set(iy, ix, gg[10], color) # Draw vertical lines for ix in vlines: ix+=x if not (0 < ix < w): continue - self._set(y, ix, self._theme.grid[8], color) - self._set(y+h-1, ix, self._theme.grid[9], color) + self._set(y, ix, gg[8], color) + self._set(y+h-1, ix, gg[9], color) if h > 2: for iy in range(y+1,y+h-1): - self._set(iy, ix, self._theme.grid[11], color) + self._set(iy, ix, gg[11], color) # Draw intersections for iy in hlines: for ix in vlines: - self._set(y+iy, x+ix, self._theme.grid[12], color) + self._set(y+iy, x+ix, gg[12], color) def drawScroll(self, pos, size, slider, orientation, color): if not self._visible: return diff --git a/TermTk/TTkCore/ttk.py b/TermTk/TTkCore/ttk.py index 1a57aa19..39f219c8 100644 --- a/TermTk/TTkCore/ttk.py +++ b/TermTk/TTkCore/ttk.py @@ -35,16 +35,21 @@ from TermTk.TTkWidgets.layout import * from TermTk.TTkWidgets.widget import * class TTkTimer(threading.Thread): - def __init__(self, callback): + def __init__(self, callback, waitTime=1, event=None): threading.Thread.__init__(self) self.stopped = threading.Event() + self._waitTime = waitTime self._callback = callback + self._event = event def quit(self): self.stopped.set() def run(self): - while not self.stopped.wait(0.05): + while not self.stopped.wait(self._waitTime): + if self._event is not None: + self._event.wait() + self._event.clear() self._callback() class TTk(TTkWidget): @@ -86,7 +91,9 @@ class TTk(TTkWidget): lbt.Term.registerResizeCb(self._win_resize_cb) threading.Thread(target=self._input_thread, daemon=True).start() - self._timer = TTkTimer(self._time_event) + self._timerEvent = threading.Event() + self._timerEvent.set() + self._timer = TTkTimer(self._time_event, 0.02, self._timerEvent) self._timer.start() self.running = True @@ -110,6 +117,7 @@ class TTk(TTkWidget): pass elif evt is TTk.TIME_EVENT: TTkHelper.paintAll() + self._timerEvent.set() pass elif evt is TTk.SCREEN_EVENT: self.setGeometry(0,0,TTkGlbl.term_w,TTkGlbl.term_h) diff --git a/TermTk/TTkGui/theme.py b/TermTk/TTkGui/theme.py index 8d9fbde1..07fb17e0 100644 --- a/TermTk/TTkGui/theme.py +++ b/TermTk/TTkGui/theme.py @@ -36,16 +36,35 @@ class TTkTheme(): │ └──┴──┘ │▒ └───────────────────┘▒ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ -''' + ''' + box = ( '═','║', '╔','╗', '╚','╝') - - grid = ( '═','║', - '╔','╗', - '╚','╝', - '╟','╢','╤','╧', - '─','│','┼',) + ''' + grid0 grid1 grid2 grid3 + ┌─┬┐ ╔═╦╗ ╔═╤╗ ┌─╥┐ ╓─╥╖ ╒═╤╕ + │ ││ ║ ║║ ║ │║ │ ║│ ║ ║║ │ ││ + ├─┼┤ ╠═╬╣ ╟─┼╢ ╞═╬╡ ╟─╫╢ ╞═╪╡ + └─┴┘ ╚═╩╝ ╚═╧╝ └─╨┘ ╙─╨╜ ╘═╧╛ + ''' + grid = ( + ( '─','│', # Grid 0 + '┌','┐','└','┘', + '├','┤','┬','┴', + '─','│','┼',), + ( '═','║', # Grid 1 + '╔','╗','╚','╝', + '╠','╣','╦','╩', + '═','║','╬',), + ( '═','║', # Grid 2 + '╔','╗','╚','╝', + '╟','╢','╤','╧', + '─','│','┼',), + ( '─','│', # Grid 3 + '┌','┐','└','┘', + '╞','╡','╥','╨', + '═','║','╬',)) hscroll = ('◀','┄','▓','▶') vscroll = ('▲','┊','▓','▼') \ No newline at end of file diff --git a/TermTk/TTkTemplates/color.py b/TermTk/TTkTemplates/color.py new file mode 100644 index 00000000..70bd1795 --- /dev/null +++ b/TermTk/TTkTemplates/color.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2021 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from TermTk.TTkCore.color import TTkColor + +class TColor(): + #__slots__ = ('_color') + def __init__(self, *args, **kwargs): + self._color = TTkColor.RST + self.color = kwargs.get('color', TTkColor.RST ) + + def colorUpdated(self, color): pass + + @property + def color(self): + return self._color + + @color.setter + def color(self, color): + if self.color != color: + self._color = color + self.colorUpdated(color) \ No newline at end of file diff --git a/TermTk/TTkTemplates/text.py b/TermTk/TTkTemplates/text.py new file mode 100644 index 00000000..cdfd1dec --- /dev/null +++ b/TermTk/TTkTemplates/text.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2021 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +class TText(): + #__slots__ = ('_text') + def __init__(self, *args, **kwargs): + self._text = "" + self.text = kwargs.get('text', "" ) + + def textUpdated(self, text): pass + + @property + def text(self): + return self._text + + @text.setter + def text(self, text): + if self.text != text: + self._text = text + self.textUpdated(text) + diff --git a/TermTk/TTkWidgets/label.py b/TermTk/TTkWidgets/label.py index 68cd028c..358f562e 100644 --- a/TermTk/TTkWidgets/label.py +++ b/TermTk/TTkWidgets/label.py @@ -23,39 +23,25 @@ # SOFTWARE. from TermTk.TTkCore.log import TTkLog -from .widget import * +from TermTk.TTkWidgets.widget import * +from TermTk.TTkTemplates.color import TColor +from TermTk.TTkTemplates.text import TText -class TTkLabel(TTkWidget): - __slots__ = ('_text', '_color') +class TTkLabel(TTkWidget, TColor, TText): + __slots__ = ('_color', '_text') def __init__(self, *args, **kwargs): TTkWidget.__init__(self, *args, **kwargs) + TColor.__init__(self, *args, **kwargs) + TText.__init__(self, *args, **kwargs) self._name = kwargs.get('name' , 'TTkLabel' ) - self._text = "" - self._color = TTkColor.RST - self.text = kwargs.get('text', "" ) - self.color = kwargs.get('color', TTkColor.RST ) def paintEvent(self): self._canvas.drawText(pos=(0,0), text=' '*self.width(), color=self.color) self._canvas.drawText(pos=(0,0), text=self.text, color=self.color) - @property - def text(self): - return self._text + def textUpdated(self, text): + self.setMinimumSize(len(text), 1) + self.update() - @text.setter - def text(self, text): - if self.text != text: - self._text = text - self.setMinimumSize(len(text), 1) - self.update() - - @property - def color(self): - return self._color - - @color.setter - def color(self, color): - if self.color != color: - self._color = color - self.update() \ No newline at end of file + def colorUpdated(self, color): + self.update() \ No newline at end of file diff --git a/TermTk/TTkWidgets/scrollbar.py b/TermTk/TTkWidgets/scrollbar.py index 68fe3f3a..47d8b849 100644 --- a/TermTk/TTkWidgets/scrollbar.py +++ b/TermTk/TTkWidgets/scrollbar.py @@ -27,6 +27,9 @@ from TermTk.TTkCore.log import TTkLog from TermTk.TTkCore.color import TTkColor from TermTk.TTkWidgets.widget import TTkWidget +''' + ref: https://doc.qt.io/qt-5/qscrollbar.html +''' class TTkScrollBar(TTkWidget): __slots__ = ( '_orientation', diff --git a/TermTk/TTkWidgets/window.py b/TermTk/TTkWidgets/window.py index 4c5d276d..0f1d1663 100644 --- a/TermTk/TTkWidgets/window.py +++ b/TermTk/TTkWidgets/window.py @@ -48,7 +48,7 @@ class TTkWindow(TTkWidget): self._canvas.drawGrid( color=color, pos=(0,0), size=self.size(), - hlines=[2]) + hlines=[2], grid=2) #self._canvas.drawBox(pos=(0,0), color=color, size=(self._width,3)) #self._canvas.drawBox(pos=(0,2), color=color, size=(self._width,self._height-2)) #self._canvas.drawText(pos=(0,2), color=color, text="╟"+("─"*(self._width-2))+"╢")