Skip to content

Commit

Permalink
better sorting for group by metadata tags
Browse files Browse the repository at this point in the history
  • Loading branch information
KellyMWhitehead committed Nov 16, 2024
1 parent b928c66 commit 847e46c
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/view/frm_dockwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
}

USER_ROLES = {'date': QtCore.Qt.UserRole + 1,
'raster_type': QtCore.Qt.UserRole + 2}
'raster_type': QtCore.Qt.UserRole + 2,
'node_type': QtCore.Qt.UserRole + 3,}


class QRiSDockWidget(QtWidgets.QDockWidget):
Expand Down Expand Up @@ -587,11 +588,14 @@ def group_children(self, tree_node: QtGui.QStandardItem, group_key: str):
group_value = self.project.lookup_tables['lkp_raster_types'][db_item.raster_type_id].name
elif group_key == 'metadata_tag':
if db_item.metadata is not None and 'metadata' in db_item.metadata:
group_value = db_item.metadata['metadata'].get(metadata_tag, 'Unknown')
else:
group_value = 'Unknown'
else:
group_value = 'Unknown'
group_value = db_item.metadata['metadata'].get(metadata_tag, None)
else:
group_value = None

if group_value is None:
# just put the child back in the tree
tree_node.appendRow(child.clone())
continue

# If the group does not exist, create it
if group_value not in groups:
Expand All @@ -605,6 +609,10 @@ def group_children(self, tree_node: QtGui.QStandardItem, group_key: str):
# Add the child to the appropriate group
groups[group_value].appendRow(child.clone())

# Sort the groups folders to the top please
self.sort_children(tree_node, 'node_type')


def collect_all_children(self, tree_node: QtGui.QStandardItem):
"""Recursively collect all children of a tree node, including nested children."""
children = []
Expand Down Expand Up @@ -635,6 +643,17 @@ def sort_children(self, tree_node: QtGui.QStandardItem, sort_key: str):
if (tree_node.child(i).data(USER_ROLES['date']) or QtCore.QDate()) > (tree_node.child(i + 1).data(USER_ROLES['date']) or QtCore.QDate()):
current_order = False
break
elif sort_key == 'node_type':
tree_node.model().setSortRole(USER_ROLES['node_type'])
current_order = True
for i in range(0, tree_node.rowCount()):
item = tree_node.child(i)
db_item = item.data(QtCore.Qt.UserRole)
if isinstance(db_item, str):
item.setData(f'_{db_item}', USER_ROLES['node_type'])
else:
item.setData(db_item.name, USER_ROLES['node_type'])

elif sort_key == 'raster_type':
tree_node.model().setSortRole(USER_ROLES['raster_type'])
for i in range(0, tree_node.rowCount()):
Expand Down

0 comments on commit 847e46c

Please sign in to comment.