Skip to content

Commit

Permalink
ENH: add icons to TreeModel
Browse files Browse the repository at this point in the history
  • Loading branch information
tangkong committed Jul 26, 2024
1 parent 134d73b commit d2b2e30
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion superscore/widgets/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
from typing import Any, ClassVar, Generator, List, Optional
from weakref import WeakValueDictionary

import qtawesome as qta
from PyQt5.QtCore import Qt
from qtpy import QtCore

from superscore.client import Client
from superscore.model import Entry, Nestable, Root
from superscore.qt_helpers import QDataclassBridge
from superscore.widgets import ICON_MAP

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -180,6 +182,13 @@ def takeChildren(self) -> list[EntryItem]:

return children

def icon(self):
"""return icon for this item"""
icon_id = ICON_MAP.get(type(self._data), None)
if icon_id is None:
return
return qta.icon(ICON_MAP[type(self._data)])


def build_tree(entry: Entry, parent: Optional[EntryItem] = None) -> EntryItem:
"""
Expand Down Expand Up @@ -402,7 +411,7 @@ def data(self, index: QtCore.QModelIndex, role: int) -> Any:
if role == Qt.DisplayRole:
return item.data(1)
if role == Qt.TextAlignmentRole:
return Qt.AlignCenter
return Qt.AlignLeft

if role == Qt.ToolTipRole:
return item.tooltip()
Expand All @@ -412,4 +421,7 @@ def data(self, index: QtCore.QModelIndex, role: int) -> Any:
if role == Qt.UserRole:
return item

if role == Qt.DecorationRole and index.column() == 0:
return item.icon()

return None

0 comments on commit d2b2e30

Please sign in to comment.