Skip to content

Commit

Permalink
TreeItem: make arguments explicit
Browse files Browse the repository at this point in the history
- sortable becomes optional
- parent is now a keyword argument
- obj is mandatory (unchanged)
- fix up callers accordingly
- introduce a post_init() method to allow post initialization without duplicating __init__()
  • Loading branch information
zas committed May 29, 2024
1 parent 2ec8248 commit e7dff30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
25 changes: 14 additions & 11 deletions picard/ui/itemviews/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ class FileTreeView(BaseTreeView):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.unmatched_files = ClusterItem(self.tagger.unclustered_files, False, self)
self.unmatched_files = ClusterItem(self.tagger.unclustered_files, parent=self)
self.unmatched_files.update()
self.unmatched_files.setExpanded(True)
self.clusters = ClusterItem(self.tagger.clusters, False, self)
self.clusters = ClusterItem(self.tagger.clusters, parent=self)
self.set_clusters_text()
self.clusters.setExpanded(True)
self.tagger.cluster_added.connect(self.add_file_cluster)
Expand Down Expand Up @@ -282,10 +282,10 @@ def __init__(self, *args, **kwargs):

def add_album(self, album):
if isinstance(album, NatAlbum):
item = NatAlbumItem(album, True)
item = NatAlbumItem(album, sortable=True)
self.insertTopLevelItem(0, item)
else:
item = AlbumItem(album, True, self)
item = AlbumItem(album, sortable=True, parent=self)
item.setIcon(ITEM_ICON_COLUMN, AlbumItem.icon_cd)
for i, column in enumerate(DEFAULT_COLUMNS):
font = item.font(i)
Expand All @@ -301,13 +301,17 @@ def remove_album(self, album):

class TreeItem(QtWidgets.QTreeWidgetItem):

def __init__(self, obj, sortable, *args):
super().__init__(*args)
def __init__(self, obj, sortable=False, parent=None):
super().__init__(parent)
self.obj = obj
if obj is not None:
obj.item = self
self.sortable = sortable
self._sortkeys = {}
self.post_init()

def post_init(self):
pass

def setText(self, column, text):
self._sortkeys[column] = None
Expand Down Expand Up @@ -352,8 +356,7 @@ def update_colums_text(self, color=None, bgcolor=None):

class ClusterItem(TreeItem):

def __init__(self, *args):
super().__init__(*args)
def post_init(self):
self.setIcon(ITEM_ICON_COLUMN, ClusterItem.icon_dir)

def update(self, update_selection=True):
Expand All @@ -375,7 +378,7 @@ def add_files(self, files):
# to be certain about item order in the cluster (addChildren adds in reverse order).
# Benchmarked performance was not noticeably different.
for file in files:
item = FileItem(file, True)
item = FileItem(file, sortable=True)
self.addChild(item)
item.update()

Expand Down Expand Up @@ -412,7 +415,7 @@ def update(self, update_tracks=True, update_selection=True):
if newnum > oldnum: # add new items
items = []
for i in range(oldnum, newnum):
item = TrackItem(album.tracks[i], False)
item = TrackItem(album.tracks[i])
item.setHidden(False) # Workaround to make sure the parent state gets updated
items.append(item)
# insertChildren behaves differently if sorting is disabled / enabled, which results
Expand Down Expand Up @@ -520,7 +523,7 @@ def update(self, update_album=True, update_files=True, update_selection=True):
if newnum > oldnum: # add new items
items = []
for i in range(newnum - 1, oldnum - 1, -1):
item = FileItem(track.files[i], False)
item = FileItem(track.files[i])
item.update(update_track=False, update_selection=update_selection)
items.append(item)
self.addChildren(items)
Expand Down
2 changes: 1 addition & 1 deletion picard/ui/itemviews/basetreeview.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def add_cluster(self, cluster, parent_item=None):
if parent_item is None:
parent_item = self.clusters
from picard.ui.itemviews import ClusterItem
cluster_item = ClusterItem(cluster, not cluster.special, parent_item)
cluster_item = ClusterItem(cluster, sortable=not cluster.special, parent=parent_item)
if cluster.hide_if_empty and not cluster.files:
cluster_item.update()
cluster_item.setHidden(True)
Expand Down

0 comments on commit e7dff30

Please sign in to comment.