Browse Source

Hopefully Adapted the lineedit to the unicode variable sized chars

pull/68/head
Eugenio Parodi 3 years ago
parent
commit
dd88c87f17
  1. 9
      TermTk/TTkCore/string.py
  2. 2
      TermTk/TTkGui/textcursor.py
  3. 64
      TermTk/TTkWidgets/lineedit.py
  4. 8
      demo/showcase/formwidgets.py

9
TermTk/TTkCore/string.py

@ -144,6 +144,15 @@ class TTkString():
def __gt__(self, other): return self._text > other if type(other) is str else self._text > other._text
def __ge__(self, other): return self._text >= other if type(other) is str else self._text >= other._text
def isdigit(self):
return self._text.isdigit()
def lstrip(self, ch):
ret = TTkString()
ret._text = self._text.lstrip(ch)
ret._colors = self._colors[-len(ret._text):]
return ret
def charAt(self, pos):
return self._text[pos]

2
TermTk/TTkGui/textcursor.py

@ -452,7 +452,7 @@ class TTkTextCursor():
splitAfter = self._document._dataLines[line].substring(fr=pos)
xFrom = pos
xTo = pos
selectRE = '[a-zA-Z0-9:,./]*'
selectRE = '[^ \t\r\n\(\)\[\]\.\,\+\-\*\/]*'
if m := splitBefore.search(selectRE+'$'):
xFrom -= len(m.group(0))
if m := splitAfter.search('^'+selectRE):

64
TermTk/TTkWidgets/lineedit.py

@ -25,10 +25,11 @@
import re
from TermTk.TTkCore.cfg import TTkCfg
from TermTk.TTkCore.constant import TTkK
from TermTk.TTkCore.helper import TTkHelper
from TermTk.TTkCore.string import TTkString
from TermTk.TTkCore.signal import pyTTkSlot, pyTTkSignal
from TermTk.TTkWidgets.widget import *
from TermTk.TTkWidgets.widget import TTkWidget
'''
@ -48,11 +49,10 @@ class TTkLineEdit(TTkWidget):
self.textChanged = pyTTkSignal(str)
self.textEdited = pyTTkSignal(str)
TTkWidget.__init__(self, *args, **kwargs)
self._name = kwargs.get('name' , 'TTkLineEdit' )
self._inputType = kwargs.get('inputType' , TTkK.Input_Text )
self._text = kwargs.get('text' , '' )
self._text = TTkString(kwargs.get('text' , '' ))
if self._inputType & TTkK.Input_Number and\
not self._text.lstrip('-').isdigit(): self._text = ""
not self._text.lstrip('-').isdigit(): self._text = TTkString()
self._color = TTkCfg.theme.lineEditTextColor
self._offset = 0
self._cursorPos = 0
@ -67,7 +67,7 @@ class TTkLineEdit(TTkWidget):
def setText(self, text, cursorPos=0x1000):
if text != self._text:
self.textChanged.emit(text)
self._text = text
self._text = TTkString(text)
self._cursorPos = max(0,min(cursorPos, len(text)))
self._pushCursor()
@ -82,12 +82,13 @@ class TTkLineEdit(TTkWidget):
# Align the text and the offset and the cursor to the current view
self._offset = max(0, min(self._offset, len(self._text)-w))
# Scroll to the right if reached the edge
if self._cursorPos - self._offset > w:
self._offset = self._cursorPos-w
if self._cursorPos - self._offset < 0:
self._offset = self._cursorPos
cursorPos = self._text.substring(to=self._cursorPos).termWidth()
if cursorPos - self._offset > w:
self._offset = cursorPos-w
if cursorPos - self._offset < 0:
self._offset = cursorPos
TTkHelper.moveCursor(self,self._cursorPos-self._offset,0)
TTkHelper.moveCursor(self,cursorPos-self._offset,0)
if self._replace:
TTkHelper.showCursor(TTkK.Cursor_Blinking_Block)
else:
@ -116,9 +117,7 @@ class TTkLineEdit(TTkWidget):
self._canvas.drawText(pos=(0,0), text=text, color=color, width=w)
def mousePressEvent(self, evt):
txtPos = evt.x+self._offset
if txtPos > len(self._text):
txtPos = len(self._text)
txtPos = self._text.tabCharPos(evt.x+self._offset)
self._cursorPos = txtPos
self._selectionFrom = txtPos
self._selectionTo = txtPos
@ -126,7 +125,7 @@ class TTkLineEdit(TTkWidget):
return True
def mouseDragEvent(self, evt) -> bool:
txtPos = evt.x+self._offset
txtPos = self._text.tabCharPos(evt.x+self._offset)
self._selectionFrom = max(0, min(txtPos,self._cursorPos))
self._selectionTo = min(len(self._text),max(txtPos,self._cursorPos))
if self._selectionFrom < self._selectionTo:
@ -135,17 +134,17 @@ class TTkLineEdit(TTkWidget):
return True
def mouseDoubleClickEvent(self, evt) -> bool:
before = self._text[:self._cursorPos]
after = self._text[self._cursorPos:]
before = self._text.substring(to=self._cursorPos)
after = self._text.substring(fr=self._cursorPos)
self._selectionFrom = len(before)
self._selectionTo = len(before)
selectRE = '[a-zA-Z0-9:,./]*'
selectRE = '[^ \t\r\n\(\)\[\]\.\,\+\-\*\/]*'
if m := re.search(selectRE+'$',before):
if m := before.search(selectRE+'$'):
self._selectionFrom -= len(m.group(0))
if m := re.search('^'+selectRE,after):
if m := after.search('^'+selectRE):
self._selectionTo += len(m.group(0))
# TTkLog.debug("x"*self._selectionFrom)
@ -177,13 +176,11 @@ class TTkLineEdit(TTkWidget):
elif evt.key == TTkK.Key_Left:
if self._selectionFrom < self._selectionTo:
self._cursorPos = self._selectionTo
if self._cursorPos > 0:
self._cursorPos -= 1
self._cursorPos = self._text.prevPos(self._cursorPos)
elif evt.key == TTkK.Key_Right:
if self._selectionFrom < self._selectionTo:
self._cursorPos = self._selectionTo-1
if self._cursorPos < len(self._text):
self._cursorPos += 1
self._cursorPos = self._text.nextPos(self._cursorPos)
elif evt.key == TTkK.Key_End:
self._cursorPos = len(self._text)
elif evt.key == TTkK.Key_Home:
@ -192,17 +189,18 @@ class TTkLineEdit(TTkWidget):
self._replace = not self._replace
elif evt.key == TTkK.Key_Delete:
if self._selectionFrom < self._selectionTo:
self._text = self._text[:self._selectionFrom] + self._text[self._selectionTo:]
self._text = self._text.substring(to=self._selectionFrom) + self._text.substring(fr=self._selectionTo)
self._cursorPos = self._selectionFrom
else:
self._text = self._text[:self._cursorPos] + self._text[self._cursorPos+1:]
self._text = self._text.substring(to=self._cursorPos) + self._text.substring(fr=self._text.nextPos(self._cursorPos))
elif evt.key == TTkK.Key_Backspace:
if self._selectionFrom < self._selectionTo:
self._text = self._text[:self._selectionFrom] + self._text[self._selectionTo:]
self._text = self._text.substring(to=self._selectionFrom) + self._text.substring(fr=self._selectionTo)
self._cursorPos = self._selectionFrom
elif self._cursorPos > 0:
self._text = self._text[:self._cursorPos-1] + self._text[self._cursorPos:]
self._cursorPos -= 1
prev = self._text.prevPos(self._cursorPos)
self._text = self._text.substring(to=prev) + self._text.substring(fr=self._cursorPos)
self._cursorPos = prev
if self._inputType & TTkK.Input_Number and \
not self._text.lstrip('-').isdigit():
@ -216,15 +214,15 @@ class TTkLineEdit(TTkWidget):
text = self._text
if self._selectionFrom < self._selectionTo:
pre = text[:self._selectionFrom]
post = text[self._selectionTo:]
pre = text.substring(to=self._selectionFrom)
post = text.substring(fr=self._selectionTo)
self._cursorPos = self._selectionFrom
else:
pre = text[:self._cursorPos]
pre = text.substring(to=self._cursorPos)
if self._replace:
post = text[self._cursorPos+1:]
post = text.substring(fr=self._cursorPos+1)
else:
post = text[self._cursorPos:]
post = text.substring(fr=self._cursorPos)
text = pre + evt.key + post
if self._inputType & TTkK.Input_Number and \

8
demo/showcase/formwidgets.py

@ -28,10 +28,6 @@ import random
sys.path.append(os.path.join(sys.path[0],'../..'))
import TermTk as ttk
zc1 = chr(0x07a6) # Sero width chars oަ
zc2 = chr(0x20D7) # Sero width chars o
zc3 = chr(0x065f) # Sero width chars oٟ
sys.path.append(os.path.join(sys.path[0],'..'))
from showcase._showcasehelper import getUtfSentence
@ -61,9 +57,9 @@ def demoFormWidgets(root=None):
row +=1; win_form1_grid_layout.addWidget(ttk.TTkLabel(text='Line Edit Test 1'),row,0)
win_form1_grid_layout.addWidget(ttk.TTkLineEdit(text='Line Edit Test 1'),row,2)
row += 1; win_form1_grid_layout.addWidget(ttk.TTkLabel(text='Line Edit Test 2'),row,0)
win_form1_grid_layout.addWidget(ttk.TTkLineEdit(text='Line Edit Test 2'),row,2)
win_form1_grid_layout.addWidget(ttk.TTkLineEdit(text='Line Edit Test 2 😎 -'),row,2)
row += 1; win_form1_grid_layout.addWidget(ttk.TTkLabel(text='Line Edit Test 3'),row,0)
win_form1_grid_layout.addWidget(ttk.TTkLineEdit(text='Line Edit Test 3'),row,2)
win_form1_grid_layout.addWidget(ttk.TTkLineEdit(text='Line Edit Test 3 oަ -'),row,2)
row += 1; win_form1_grid_layout.addWidget(ttk.TTkLabel(text='Line Edit Test 4'),row,0)
win_form1_grid_layout.addWidget(ttk.TTkLineEdit(text='Line Edit Test 4'),row,2)
row += 1; win_form1_grid_layout.addWidget(ttk.TTkLabel(text='Line Edit Test 5'),row,0)

Loading…
Cancel
Save