Skip to content
Snippets Groups Projects
Commit b84898f7 authored by Allen, Bruce (CIV)'s avatar Allen, Bruce (CIV)
Browse files

add descending sort

parent 34e7f8d5
No related branches found
No related tags found
No related merge requests found
......@@ -22,30 +22,68 @@ class GraphListSortAndFilter(QObject):
graph_list_table.graphs_manager.signal_graphs_loaded.connect(
self.reset_selections)
# sort actions
self.action_sort_trace_index = QAction("Trace index")
self.action_sort_trace_index.setCheckable(True)
self.action_sort_trace_index.triggered.connect(
graph_list_table.sort_trace_index)
self.action_sort_mark = QAction("Mark")
self.action_sort_mark.setCheckable(True)
self.action_sort_mark.triggered.connect(
graph_list_table.sort_mark)
self.action_sort_probability = QAction("Probability")
self.action_sort_probability.setCheckable(True)
self.action_sort_probability.triggered.connect(
graph_list_table.sort_probability)
self.action_sort_size = QAction("Size")
self.action_sort_size.setCheckable(True)
self.action_sort_size.triggered.connect(
graph_list_table.sort_size)
# sort action trace index
self.action_sort_trace_index_ascending = QAction(
QIcon("icons/go-down-3.png"), "Trace index")
self.action_sort_trace_index_ascending.setCheckable(True)
self.action_sort_trace_index_ascending.triggered.connect(
graph_list_table.sort_trace_index_ascending)
self.action_sort_trace_index_descending = QAction(
QIcon("icons/go-up-3.png"), "Trace index")
self.action_sort_trace_index_descending.setCheckable(True)
self.action_sort_trace_index_descending.triggered.connect(
graph_list_table.sort_trace_index_descending)
# sort action mark
self.action_sort_mark_ascending = QAction(QIcon("icons/go-down-3.png"),
"Mark")
self.action_sort_mark_ascending.setCheckable(True)
self.action_sort_mark_ascending.triggered.connect(
graph_list_table.sort_mark_ascending)
self.action_sort_mark_descending = QAction(QIcon("icons/go-up-3.png"),
"Mark")
self.action_sort_mark_descending.setCheckable(True)
self.action_sort_mark_descending.triggered.connect(
graph_list_table.sort_mark_descending)
# sort action probability
self.action_sort_probability_ascending = QAction(
QIcon("icons/go-down-3.png"), "Probability")
self.action_sort_probability_ascending.setCheckable(True)
self.action_sort_probability_ascending.triggered.connect(
graph_list_table.sort_probability_ascending)
self.action_sort_probability_descending = QAction(
QIcon("icons/go-up-3.png"), "Probability")
self.action_sort_probability_descending.setCheckable(True)
self.action_sort_probability_descending.triggered.connect(
graph_list_table.sort_probability_descending)
# sort action size
self.action_sort_size_ascending = QAction(QIcon("icons/go-down-3.png"),
"Size")
self.action_sort_size_ascending.setCheckable(True)
self.action_sort_size_ascending.triggered.connect(
graph_list_table.sort_size_ascending)
self.action_sort_size_descending = QAction(QIcon("icons/go-up-3.png"),
"Size")
self.action_sort_size_descending.setCheckable(True)
self.action_sort_size_descending.triggered.connect(
graph_list_table.sort_size_descending)
# sort group
self.sort_group = QActionGroup(self)
self.sort_group.addAction(self.action_sort_trace_index)
self.sort_group.addAction(self.action_sort_mark)
self.sort_group.addAction(self.action_sort_probability)
self.sort_group.addAction(self.action_sort_size)
self.sort_group.addAction(self.action_sort_trace_index_ascending)
self.sort_group.addAction(self.action_sort_mark_ascending)
self.sort_group.addAction(self.action_sort_probability_ascending)
self.sort_group.addAction(self.action_sort_size_ascending)
self.sort_group.addAction(self.action_sort_trace_index_descending)
self.sort_group.addAction(self.action_sort_mark_descending)
self.sort_group.addAction(self.action_sort_probability_descending)
self.sort_group.addAction(self.action_sort_size_descending)
# sort menu pb
self.sort_menu_pb = QPushButton(
......@@ -56,10 +94,14 @@ class GraphListSortAndFilter(QObject):
self.sort_menu_pb.setToolTip("Sort the events list")
sort_menu = QMenu(self.sort_menu_pb)
self.sort_menu_pb.setMenu(sort_menu)
sort_menu.addAction(self.action_sort_trace_index)
sort_menu.addAction(self.action_sort_mark)
sort_menu.addAction(self.action_sort_probability)
sort_menu.addAction(self.action_sort_size)
sort_menu.addAction(self.action_sort_trace_index_ascending)
sort_menu.addAction(self.action_sort_trace_index_descending)
sort_menu.addAction(self.action_sort_probability_ascending)
sort_menu.addAction(self.action_sort_probability_descending)
sort_menu.addAction(self.action_sort_mark_ascending)
sort_menu.addAction(self.action_sort_mark_descending)
sort_menu.addAction(self.action_sort_size_ascending)
sort_menu.addAction(self.action_sort_size_descending)
# filter action
......@@ -90,7 +132,7 @@ class GraphListSortAndFilter(QObject):
@pyqtSlot()
def reset_selections(self):
self.action_filter_mark.setChecked(False)
self.action_sort_trace_index.setChecked(True)
self.graph_list_table.sort_trace_index()
self.action_sort_trace_index_ascending.setChecked(True)
self.graph_list_table.sort_trace_index_ascending()
self._filter()
......@@ -4,7 +4,8 @@ from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import Qt
from PyQt5.QtCore import QSortFilterProxyModel
from PyQt5.QtCore import QModelIndex
from graph_list_table_model import GraphListTableModel
from graph_list_table_model import GraphListTableModel, GRAPH_COLUMN, \
TRACE_INDEX_COLUMN, MARK_COLUMN, PROBABILITY_COLUMN, SIZE_COLUMN
from graph_list_table_view import GraphListTableView
from paint_graph_item import paint_graph_item
......@@ -20,13 +21,28 @@ class GraphListSortFilterProxyModel(QSortFilterProxyModel):
self.show_global = show_global
self.invalidateFilter()
"""Fully control custom filtering."""
# we use this to keep the global view, if present, at the top of the list
def lessThan(self, left_model_index, right_model_index):
left_row = left_model_index.row()
right_row = right_model_index.row()
# keep any global view at top
if left_row == 0:
return bool(self.sortOrder() == Qt.AscendingOrder)
if right_row == 0:
return not bool(self.sortOrder() == Qt.AscendingOrder)
# use normal less than
return self.source_model.data(left_model_index) \
< self.source_model.data(right_model_index)
# fully control custom filtering
def filterAcceptsRow(self, row, source_parent):
if row == 0 and not self.show_global:
return False
index2 = self.source_model.index(row, 2, source_parent)
if self.hide_unmarked and self.source_model.data(index2) != "M":
mark_index = self.source_model.index(row, MARK_COLUMN, source_parent)
if self.hide_unmarked and self.source_model.data(mark_index) != "M":
return False
return True
......@@ -78,22 +94,34 @@ class GraphListTable(QObject):
def filter_mark(self, filter_on_mark):
if filter_on_mark:
self.proxy_model.setFilterKeyColumn(2)
self.proxy_model.setFilterKeyColumn(MARK_COLUMN)
self.proxy_model.setFilterFixedString("M")
else:
self.proxy_model.setFilterFixedString("")
def sort_trace_index(self):
self.proxy_model.sort(1, order=Qt.AscendingOrder)
def sort_trace_index_ascending(self):
self.proxy_model.sort(TRACE_INDEX_COLUMN, order=Qt.AscendingOrder)
def sort_trace_index_descending(self):
self.proxy_model.sort(TRACE_INDEX_COLUMN, order=Qt.DescendingOrder)
def sort_mark(self):
self.proxy_model.sort(2, order=Qt.AscendingOrder)
def sort_mark_ascending(self):
self.proxy_model.sort(MARK_COLUMN, order=Qt.AscendingOrder)
def sort_probability(self):
self.proxy_model.sort(3, order=Qt.DescendingOrder)
def sort_mark_descending(self):
self.proxy_model.sort(MARK_COLUMN, order=Qt.DescendingOrder)
def sort_size(self):
self.proxy_model.sort(4, order=Qt.AscendingOrder)
def sort_probability_ascending(self):
self.proxy_model.sort(PROBABILITY_COLUMN, order=Qt.AscendingOrder)
def sort_probability_descending(self):
self.proxy_model.sort(PROBABILITY_COLUMN, order=Qt.DescendingOrder)
def sort_size_ascending(self):
self.proxy_model.sort(SIZE_COLUMN, order=Qt.AscendingOrder)
def sort_size_descending(self):
self.proxy_model.sort(SIZE_COLUMN, order=Qt.DescendingOrder)
# convert graph index(row, 0) to proxy table model index(row, col)
def graph_index_to_proxy_model_index(self, graph_index):
......@@ -140,3 +168,4 @@ class GraphListTable(QObject):
def has_non_empty_global_view(self):
return bool(self.graphs_manager.graphs \
and self.graphs_manager.graphs[0].json_views)
......@@ -5,25 +5,23 @@ from PyQt5.QtCore import QVariant
from PyQt5.QtCore import QModelIndex
# configuration values for this table model
COLUMN_COUNT = 5
GRAPH_COLUMN = 0 # the graph item
TRACE_INDEX_COLUMN = 1 # values start at 1
MARK_COLUMN = 2 # "M" or ""
PROBABILITY_COLUMN = 3 # float 0.0 to 1.0
SIZE_COLUMN = 4 # int, see implementation
COLUMN_COUNT = 5 # number of columns
# GraphListTableModel
class GraphListTableModel(QAbstractTableModel):
"""Provides a graph list table data model for displaying the graph views.
We use a table model instead of a list so we can define various columns
for sorting or filtering. Here are the columns:
0: The graph view rendered by graph_list_table_view_delegate.
1: trace index
2: Mark flag
3: probability
4: size = #nodes + #edges
for sorting or filtering.
The first row displayed in the graph list is the global view. The
remaining rows show the trace views. This model provides both views
as though they are homogeneous types. Sort and filter operations
need to keep the global view visible and on the top. To do this,
for the global view, use trace_index=0, mark=M, probability=1.0.
keep the global view, if present, visible and on the top.
"""
def __init__(self, graphs_manager):
......@@ -61,20 +59,16 @@ class GraphListTableModel(QAbstractTableModel):
if role == Qt.DisplayRole:
row = model_index.row()
column = model_index.column()
if column == 0:
if column == GRAPH_COLUMN:
# graph_list_table_view_delegate renders this
return QVariant()
if column == 1:
# trace index used for sorting so +1 is not required
if column == TRACE_INDEX_COLUMN:
return row
if column == 2:
# mark
if column == MARK_COLUMN:
return self.graphs[row].mark
if column == 3:
# probability
if column == PROBABILITY_COLUMN:
return self.graphs[row].probability
if column == 4:
# size
if column == SIZE_COLUMN:
return len(self.graphs[row].nodes) \
+ len(self.graphs[row].edges)
raise RuntimeError("bad %d"%column)
......
......@@ -7,6 +7,7 @@ from PyQt5.QtWidgets import QStyledItemDelegate
from PyQt5.QtWidgets import QStyle
from paint_graph_item import paint_graph_item
from graph_list_table_model import GRAPH_COLUMN
# ref. https://github.com/scopchanov/CustomList/blob/master/app/src/Delegate.cpp
class GraphListTableViewDelegate(QStyledItemDelegate):
......@@ -23,7 +24,7 @@ class GraphListTableViewDelegate(QStyledItemDelegate):
# paint
def paint(self, painter, option, proxy_model_index):
# all but column 0 should be hidden in graph_list_table_view
if proxy_model_index.column() != 0:
if proxy_model_index.column() != GRAPH_COLUMN:
raise Exception("Bad")
painter.save()
......@@ -52,7 +53,7 @@ class GraphListTableViewDelegate(QStyledItemDelegate):
# this graph
graph_index, column = self.graph_list_table\
.proxy_model_index_to_graph_index(proxy_model_index)
if column != 0:
if column != GRAPH_COLUMN:
raise RuntimeError("bad")
graph_item = self.graph_list_table.source_model.graphs[graph_index]
......
python/icons/go-down-3.png

1.99 KiB

python/icons/go-up-3.png

1.96 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment