12 changed files with 487 additions and 27 deletions
@ -1,2 +1,2 @@
|
||||
from .viewitem import * |
||||
from .treeitem import * |
||||
from .treewidgetitem import * |
||||
|
||||
@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python3 |
||||
|
||||
# MIT License |
||||
# |
||||
# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com> |
||||
# |
||||
# 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.cfg import TTkCfg |
||||
from TermTk.TTkCore.constant import TTkK |
||||
from TermTk.TTkCore.log import TTkLog |
||||
from TermTk.TTkCore.signal import pyTTkSlot, pyTTkSignal |
||||
from TermTk.TTkCore.color import TTkColor |
||||
from TermTk.TTkWidgets.treewidget import TTkTreeWidget |
||||
from TermTk.TTkLayouts.gridlayout import TTkGridLayout |
||||
from TermTk.TTkAbstract.abstractscrollarea import TTkAbstractScrollArea |
||||
|
||||
class TTkTree(TTkAbstractScrollArea): |
||||
__slots__ = ('_treeView', 'activated') |
||||
def __init__(self, *args, **kwargs): |
||||
TTkAbstractScrollArea.__init__(self, *args, **kwargs) |
||||
self._name = kwargs.get('name' , 'TTkTree' ) |
||||
if 'parent' in kwargs: kwargs.pop('parent') |
||||
self._treeView = TTkTreeWidget(*args, **kwargs) |
||||
# Forward the signal |
||||
self.activated = self._treeView.activated |
||||
|
||||
self.setFocusPolicy(TTkK.ClickFocus) |
||||
self.setViewport(self._treeView) |
||||
|
||||
def setAlignment(self, *args, **kwargs) : |
||||
self._treeView.setAlignment(*args, **kwargs) |
||||
def setHeader(self, *args, **kwargs) : |
||||
self._treeView.setHeader(*args, **kwargs) |
||||
def setHeaderLabels(self, *args, **kwargs) : |
||||
self._treeView.setHeaderLabels(*args, **kwargs) |
||||
def setColumnSize(self, *args, **kwargs) : |
||||
self._treeView.setColumnSize(*args, **kwargs) |
||||
def setColumnColors(self, *args, **kwargs): |
||||
self._treeView.setColumnColors(*args, **kwargs) |
||||
def appendItem(self, *args, **kwargs) : |
||||
self._treeView.appendItem(*args, **kwargs) |
||||
def addTopLevelItem(self, *args, **kwargs) : |
||||
self._treeView.addTopLevelItem(*args, **kwargs) |
||||
|
||||
|
||||
|
||||
@ -0,0 +1,147 @@
|
||||
#!/usr/bin/env python3 |
||||
|
||||
# MIT License |
||||
# |
||||
# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com> |
||||
# |
||||
# 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.cfg import TTkCfg |
||||
from TermTk.TTkCore.constant import TTkK |
||||
from TermTk.TTkCore.log import TTkLog |
||||
from TermTk.TTkCore.signal import pyTTkSlot, pyTTkSignal |
||||
from TermTk.TTkCore.color import TTkColor |
||||
from TermTk.TTkWidgets.widget import TTkWidget |
||||
from TermTk.TTkWidgets.checkbox import TTkCheckbox |
||||
from TermTk.TTkWidgets.tableview import TTkTableView |
||||
from TermTk.TTkLayouts.gridlayout import TTkGridLayout |
||||
from TermTk.TTkTypes.treewidgetitem import TTkTreeWidgetItem |
||||
|
||||
class _TTkDisplayedTreeItemControl(TTkCheckbox): |
||||
def __init__(self, *args, **kwargs): |
||||
super().__init__(self, *args, **kwargs) |
||||
self._name = kwargs.get('name' , '_TTkDisplayedTreeItemControl' ) |
||||
self.setMinimumSize(1, 1) |
||||
|
||||
def paintEvent(self): |
||||
if self._checked: |
||||
self._canvas.drawText(pos=(0,0), text="▼") |
||||
else: |
||||
self._canvas.drawText(pos=(0,0), text="▶") |
||||
|
||||
|
||||
class _TTkDisplayedTreeItem(TTkWidget): |
||||
__slots__ = ('_depth', '_control', '_text', '_id', '_clicked', '_treeWidgetItem', '_isLeaf' ) |
||||
def __init__(self, *args, **kwargs): |
||||
super().__init__(self, *args, **kwargs) |
||||
#Signals |
||||
self._clicked = pyTTkSignal(bool, _TTkDisplayedTreeItem, TTkTreeWidgetItem) |
||||
|
||||
self._name = kwargs.get('name' , '_TTkDisplayedTreeItem' ) |
||||
self._depth = kwargs.get('depth' , 0 ) |
||||
self._text = kwargs.get('text' , "" ) |
||||
self._id = kwargs.get('id' , 0 ) |
||||
self._treeWidgetItem = kwargs.get('treeWidgetItem', None) |
||||
self._isLeaf = len(self._treeWidgetItem.childs())==0 |
||||
if self._isLeaf: |
||||
self._control = None |
||||
else: |
||||
self._control = _TTkDisplayedTreeItemControl(parent=self, checked=self._treeWidgetItem.expand()) |
||||
self._control.setGeometry(self._depth, 0, 1, 1) |
||||
self._control.clicked.connect(self._controlClicked) |
||||
|
||||
@pyTTkSlot(bool) |
||||
def _controlClicked(self, status): |
||||
self._clicked.emit(status, self, self._treeWidgetItem) |
||||
pass |
||||
|
||||
def paintEvent(self): |
||||
if self._isLeaf: |
||||
self._canvas.drawText(pos=(self._depth, 0), text="⏺") |
||||
self._canvas.drawText(pos=(self._depth+2, 0), text=self._text) |
||||
|
||||
|
||||
class TTkTreeWidget(TTkTableView): |
||||
__slots__ = ( '_topLevelItems') |
||||
|
||||
def __init__(self, *args, **kwargs): |
||||
super().__init__(self, *args, **kwargs) |
||||
self._name = kwargs.get('name' , 'TTkTreeView' ) |
||||
self._topLevelItems = TTkTreeWidgetItem(None) |
||||
# if 'parent' in kwargs: kwargs.pop('parent') |
||||
|
||||
def _expand(self, item, depth): |
||||
item.setExpand(True) |
||||
toExpand = [] |
||||
index = self.indexOf(item.data())+1 |
||||
if index != 0: |
||||
for child in item.childs(): |
||||
self._addTreeWidgetItem(item=child, depth=depth, index=index) |
||||
index+=1 |
||||
if child.expand(): |
||||
toExpand.append(child) |
||||
for child in toExpand: |
||||
self._expand(item=child, depth=(depth+1)) |
||||
|
||||
|
||||
def _shrink(self, item): |
||||
item.setExpand(False) |
||||
index = self.indexOf(item.data()) |
||||
parent = item.parent() |
||||
if item == parent.childs()[-1]: |
||||
self.removeItemsFrom(index+1) |
||||
else: |
||||
nextItemIndex = parent.childs().index(item) |
||||
nextItem = parent.childs()[nextItemIndex+1] |
||||
indexTo = self.indexOf(nextItem.data()) |
||||
for id in reversed(range(index+1,indexTo)): |
||||
self.removeItemAt(id) |
||||
|
||||
|
||||
@pyTTkSlot(bool, _TTkDisplayedTreeItem, TTkTreeWidgetItem) |
||||
def _controlClicked(self, status, widget, item): |
||||
TTkLog.debug(f"{status} {widget._name}") |
||||
if status: # we need to expand the TtkTreeWidgetItem |
||||
self._expand(item=item, depth=(widget._depth+1)) |
||||
else: # we need to shrink the TtkTreeWidgetItem |
||||
self._shrink(item=item) |
||||
|
||||
|
||||
|
||||
def _addTreeWidgetItem(self, item, depth=0, index=-1): |
||||
if not isinstance(item, TTkTreeWidgetItem): |
||||
raise TypeError("TTkTreeWidgetItem is required in TTkTreeWidget.addTopLevelItem(item)") |
||||
if item.parent() is None: |
||||
self._topLevelItems.addChild(item) |
||||
displayedItems = [i for i in item.data()] |
||||
displayTreeItem = _TTkDisplayedTreeItem(text=displayedItems[0], id=0, depth=depth, treeWidgetItem=item) |
||||
displayTreeItem._clicked.connect(self._controlClicked) |
||||
displayedItems[0] = displayTreeItem |
||||
if index == -1: |
||||
self.appendItem(item=displayedItems, id=item.data()) |
||||
else: |
||||
self.insertItem(item=displayedItems, id=item.data(), index=index, ) |
||||
|
||||
def addTopLevelItem(self, item): |
||||
self._addTreeWidgetItem(item) |
||||
|
||||
def setHeaderLabels(self, labels): |
||||
columns = [-1]*len(labels) |
||||
self.setColumnSize(columns) |
||||
self.setHeader(labels) |
||||
@ -0,0 +1,88 @@
|
||||
#!/usr/bin/env python3 |
||||
|
||||
# MIT License |
||||
# |
||||
# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com> |
||||
# |
||||
# 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. |
||||
|
||||
# Demo inspired from: |
||||
# https://stackoverflow.com/questions/41204234/python-pyqt5-qtreewidget-sub-item |
||||
|
||||
import os |
||||
import sys |
||||
|
||||
sys.path.append(os.path.join(sys.path[0],'..')) |
||||
import TermTk as ttk |
||||
|
||||
ttk.TTkLog.use_default_file_logging() |
||||
|
||||
fullscreen = False |
||||
|
||||
root = ttk.TTk() |
||||
if fullscreen: |
||||
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) |
||||
|
||||
# tw = ttk.TTkTreeWidget(parent=rootTree1) |
||||
tw = ttk.TTkTree(parent=rootTree1) |
||||
tw.setHeaderLabels(["Column 1", "Column 2", "Column 3"]) |
||||
tw.setColumnSize((20,20,-1)) |
||||
tw.setColumnColors(( |
||||
ttk.TTkColor.RST, |
||||
ttk.TTkColor.fg('#00dddd', modifier=ttk.TTkColorGradient(increment=-4)), |
||||
ttk.TTkColor.fg('#cccc00', modifier=ttk.TTkColorGradient(increment=-2)) |
||||
)) |
||||
l1 = ttk.TTkTreeWidgetItem(["String A", "String B", "String C"]) |
||||
l2 = ttk.TTkTreeWidgetItem(["String AA", "String BB", "String CC"]) |
||||
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) |
||||
|
||||
|
||||
for i in range(3): |
||||
l1_child = ttk.TTkTreeWidgetItem(["Child A" + str(i), "Child B" + str(i), "Child C" + str(i)]) |
||||
l1.addChild(l1_child) |
||||
|
||||
for j in range(2): |
||||
l2_child = ttk.TTkTreeWidgetItem(["Child AA" + str(j), "Child BB" + str(j), "Child CC" + str(j)]) |
||||
l2.addChild(l2_child) |
||||
|
||||
for j in range(2): |
||||
l3_child = ttk.TTkTreeWidgetItem(["Child AAA" + str(j), "Child BBB" + str(j), "Child CCC" + str(j)]) |
||||
l3.addChild(l3_child) |
||||
|
||||
for j in range(2): |
||||
l4_child = ttk.TTkTreeWidgetItem(["Child AAAA" + str(j), "Child BBBB" + str(j), "Child CCCC" + str(j)]) |
||||
l4.addChild(l4_child) |
||||
|
||||
for j in range(2): |
||||
l5_child = ttk.TTkTreeWidgetItem(["Child AAAAA" + str(j), "Child BBBBB" + str(j), "Child CCCCC" + str(j)]) |
||||
l5.addChild(l5_child) |
||||
|
||||
|
||||
tw.addTopLevelItem(l1) |
||||
tw.addTopLevelItem(l2) |
||||
tw.addTopLevelItem(l3) |
||||
tw.addTopLevelItem(l4) |
||||
|
||||
root.mainloop() |
||||
Loading…
Reference in new issue