Browse Source

Improved treeWidget in order to support dynamic data

pull/17/head
Eugenio Parodi 5 years ago
parent
commit
d2e85cc273
  1. 15
      TermTk/TTkCore/constant.py
  2. 16
      TermTk/TTkTypes/treewidgetitem.py
  3. 6
      TermTk/TTkWidgets/treewidget.py
  4. 2
      demo/demo.py
  5. 36
      demo/showcase/tree.py
  6. 6
      demo/showcase/windows.py

15
TermTk/TTkCore/constant.py

@ -96,14 +96,23 @@ class TTkConstant:
Checked = CheckState.Checked
class InsertPolicy:
NoInsert = 0x00 # The string will not be inserted into the combobox.
InsertAtTop = 0x01 # The string will be inserted as the first item in the combobox.
NoInsert = 0x00 # The string will not be inserted into the combobox.
InsertAtTop = 0x01 # The string will be inserted as the first item in the combobox.
# InsertAtCurrent = 0x02 # The current item will be replaced by the string.
InsertAtBottom = 0x03 # The string will be inserted after the last item in the combobox.
InsertAtBottom = 0x03 # The string will be inserted after the last item in the combobox.
# InsertAfterCurrent = 0x04 # The string is inserted after the current item in the combobox.
# InsertBeforeCurrent = 0x05 # The string is inserted before the current item in the combobox.
# InsertAlphabetically = 0x06 # The string is inserted in the alphabetic order in the combobox.
class ChildIndicatorPolicy:
ShowIndicator = 0x00 #The controls for expanding and collapsing will be shown for this item even if there are no children.
DontShowIndicator = 0x01 #The controls for expanding and collapsing will never be shown even if there are children. If the node is forced open the user will not be able to expand or collapse the item.
DontShowIndicatorWhenChildless = 0x02 #The controls for expanding and collapsing will be shown if the item contains children.
ShowIndicator = ChildIndicatorPolicy.ShowIndicator
DontShowIndicator = ChildIndicatorPolicy.DontShowIndicator
DontShowIndicatorWhenChildless = ChildIndicatorPolicy.DontShowIndicatorWhenChildless
NoInsert = InsertPolicy.NoInsert
InsertAtTop = InsertPolicy.InsertAtTop
# InsertAtCurrent = InsertPolicy.InsertAtCurrent

16
TermTk/TTkTypes/treewidgetitem.py

@ -28,13 +28,27 @@ from TermTk.TTkCore.log import TTkLog
from TermTk.TTkCore.signal import pyTTkSlot, pyTTkSignal
class TTkTreeWidgetItem():
__slots__ = ('_parent', '_data', '_childs', '_expand')
__slots__ = ('_parent', '_data', '_childs', '_expand', '_childIndicatorPolicy',
# Signals
'refreshData')
def __init__(self, *args, **kwargs):
# Signals
self.refreshData = pyTTkSignal(TTkTreeWidgetItem)
self._data = args[0]
self._childs = []
self._childIndicatorPolicy = kwargs.get('childIndicatorPolicy', TTkK.DontShowIndicatorWhenChildless)
self._expand = False
self._parent = kwargs.get("parent", None)
def childIndicatorPolicy(self):
return self._childIndicatorPolicy
def setChildIndicatorPolicy(self, policy):
self._childIndicatorPolicy = policy
def refresh(self):
self.refreshData.emit(self)
def setExpand(self, status):
self._expand = status

6
TermTk/TTkWidgets/treewidget.py

@ -58,7 +58,8 @@ class _TTkDisplayedTreeItem(TTkWidget):
self._text = kwargs.get('text' , "" )
self._id = kwargs.get('id' , 0 )
self._treeWidgetItem = kwargs.get('treeWidgetItem', None)
self._isLeaf = len(self._treeWidgetItem.childs())==0
self._isLeaf = self._treeWidgetItem.childIndicatorPolicy() == TTkK.DontShowIndicator
self._isLeaf |= self._treeWidgetItem.childIndicatorPolicy() == TTkK.DontShowIndicatorWhenChildless and not self._treeWidgetItem.childs()
if self._isLeaf:
self._control = None
else:
@ -69,7 +70,6 @@ class _TTkDisplayedTreeItem(TTkWidget):
@pyTTkSlot(bool)
def _controlClicked(self, status):
self._clicked.emit(status, self, self._treeWidgetItem)
pass
def paintEvent(self):
if self._isLeaf:
@ -88,6 +88,7 @@ class TTkTreeWidget(TTkTableView):
def _expand(self, item, depth):
item.setExpand(True)
item.refresh()
toExpand = []
index = self.indexOf(item.data())+1
if index != 0:
@ -102,6 +103,7 @@ class TTkTreeWidget(TTkTableView):
def _shrink(self, item):
item.setExpand(False)
item.refresh()
index = self.indexOf(item.data())
parent = item.parent()
if item == parent.childs()[-1]:

2
demo/demo.py

@ -118,7 +118,7 @@ def demoShowcase(root=None, border=True):
listMenu.textClicked.connect(_listCallback)
listMenu.setCurrentRow(0)
listMenu.setCurrentRow(6)
return splitter

36
demo/showcase/tree.py

@ -27,10 +27,18 @@
import os
import sys
import random
import argparse
sys.path.append(os.path.join(sys.path[0],'../..'))
import TermTk as ttk
words = ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing", "elit,", "sed", "do", "eiusmod", "tempor", "incididunt", "ut", "labore", "et", "dolore", "magna", "aliqua.", "Ut", "enim", "ad", "minim", "veniam,", "quis", "nostrud", "exercitation", "ullamco", "laboris", "nisi", "ut", "aliquip", "ex", "ea", "commodo", "consequat.", "Duis", "aute", "irure", "dolor", "in", "reprehenderit", "in", "voluptate", "velit", "esse", "cillum", "dolore", "eu", "fugiat", "nulla", "pariatur.", "Excepteur", "sint", "occaecat", "cupidatat", "non", "proident,", "sunt", "in", "culpa", "qui", "officia", "deserunt", "mollit", "anim", "id", "est", "laborum."]
def getWord():
return random.choice(words)
def getSentence(a,b):
return " ".join([getWord() for i in range(0,random.randint(a,b))])
def demoTree(root=None):
# tw = ttk.TTkTreeWidget(parent=rootTree1)
tw = ttk.TTkTree(parent=root)
@ -46,8 +54,8 @@ def demoTree(root=None):
l3 = ttk.TTkTreeWidgetItem(["String AAA", "String BBB", "String CCC"])
l4 = ttk.TTkTreeWidgetItem(["String AAAA", "String BBBB", "String CCCC"])
l5 = ttk.TTkTreeWidgetItem(["String AAAAA", "String BBBBB", "String CCCCC"])
l2.addChild(l5)
l2.addChild(l5)
for i in range(3):
l1_child = ttk.TTkTreeWidgetItem(["Child A" + str(i), "Child B" + str(i), "Child C" + str(i)])
@ -69,25 +77,41 @@ def demoTree(root=None):
l5_child = ttk.TTkTreeWidgetItem(["Child AAAAA" + str(j), "Child BBBBB" + str(j), "Child CCCCC" + str(j)])
l5.addChild(l5_child)
l6 = ttk.TTkTreeWidgetItem(["RND", "RND", "RND"], childIndicatorPolicy=ttk.TTkK.ShowIndicator)
def updateChilds(item):
if item.childs(): return
for _ in range(0,random.randint(3,8)):
child = ttk.TTkTreeWidgetItem([getWord(),getWord(),getWord()])
if random.randint(0,10)>5:
child.setChildIndicatorPolicy(ttk.TTkK.ShowIndicator)
child.refreshData.connect(updateChilds)
item.addChild(child)
l6.refreshData.connect(updateChilds)
tw.addTopLevelItem(l1)
tw.addTopLevelItem(l2)
tw.addTopLevelItem(l3)
tw.addTopLevelItem(l4)
return tw
tw.addTopLevelItem(l6)
return tw
def main():
ttk.TTkLog.use_default_file_logging()
parser = argparse.ArgumentParser()
parser.add_argument('-f', help='Full Screen', action='store_true')
args = parser.parse_args()
fullscreen = False
ttk.TTkLog.use_default_file_logging()
root = ttk.TTk()
if fullscreen:
if args.f:
rootTree1 = root
root.setLayout(ttk.TTkGridLayout())
else:
rootTree1 = ttk.TTkWindow(parent=root,pos = (0,0), size=(150,50), title="Test Tree 1", layout=ttk.TTkGridLayout(), border=True)
rootTree1 = ttk.TTkWindow(parent=root,pos = (0,0), size=(70,40), title="Test Tree 1", layout=ttk.TTkGridLayout(), border=True)
demoTree(rootTree1)
root.mainloop()

6
demo/showcase/windows.py

@ -32,15 +32,15 @@ import TermTk as ttk
def demoWindows(root=None):
frame = ttk.TTkFrame(parent=root, border=False)
win2_1 = ttk.TTkWindow(parent=frame,pos = (3,3), size=(40,20), title="Test Window 2.1", border=True)
win2_1 = ttk.TTkWindow(parent=frame,pos = (0,0), size=(50,25), title="Test Window 2.1", border=True)
win2_1.setLayout(ttk.TTkHBoxLayout())
ttk.TTkTestWidget(parent=win2_1, border=False)
win2_2 = ttk.TTkWindow(parent=frame,pos = (5,5), size=(40,20), title="Test Window 2.2", border=True)
win2_2 = ttk.TTkWindow(parent=frame,pos = (23,4), size=(52,20), title="Test Window 2.2", border=True)
win2_2.setLayout(ttk.TTkHBoxLayout())
ttk.TTkTestWidget(parent=win2_2, border=False)
ttk.TTkAbout(parent=frame, pos=(7,7))
ttk.TTkAbout(parent=frame, pos=(11,7))
return frame

Loading…
Cancel
Save