Skip to content

Commit c0a05b8

Browse files
kryksyhsaintmatthieu
authored andcommitted
simplify sort column selection
1 parent f8ad29a commit c0a05b8

4 files changed

Lines changed: 27 additions & 24 deletions

File tree

src/appshell/qml/MuseScore/AppShell/DevTools/Gallery/GeneralComponentsGallery.qml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ Rectangle {
11081108
valueTitle: "Age"
11091109

11101110
model: ListModel {
1111-
ListElement {
1111+
ListElement {
11121112
name: "Alex"
11131113
age: 12
11141114
valueType: "Int"
@@ -1174,9 +1174,8 @@ Rectangle {
11741174
return null
11751175
}
11761176

1177-
sortOrderProvider: function(column) {
1178-
return tableProxy.columnSortOrder(column)
1179-
}
1177+
sortIndicatorColumn: tableProxy.sortIndicatorColumn
1178+
sortIndicatorOrder: tableProxy.sortIndicatorOrder
11801179

11811180
onHorizontalHeaderClicked: function (column) {
11821181
tableProxy.toggleColumnSort(column)

src/framework/uicomponents/qml/Muse/UiComponents/StyledTableView.qml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Item {
3434
property alias model: tableView.model
3535
property var sourceComponentCallback
3636

37-
property var sortOrderProvider: null
37+
property int sortIndicatorColumn: -1
38+
property int sortIndicatorOrder: ColumnSortOrder.Unsorted
3839

3940
property bool showVerticalHeader: false
4041
property bool horizontalHeaderNavigationEnabled: true
@@ -85,7 +86,6 @@ Item {
8586
QtObject {
8687
id: prv
8788

88-
property int sortRevision: 0
8989
// Set when sorting deactivates the header that the user had focused;
9090
// the new (or reused) delegate at navigationIndexForRestore picks
9191
// this up and re-activates itself, restoring the focus border.
@@ -99,8 +99,6 @@ Item {
9999
target: root.model
100100
ignoreUnknownSignals: true
101101
function onSortChanged() {
102-
prv.sortRevision++
103-
104102
// Sorting causes HorizontalHeaderView to rebuild its delegates.
105103
// The active NavigationControl is destroyed (or reused on a
106104
// different visual column), losing the focus border. Mark that
@@ -145,7 +143,7 @@ Item {
145143

146144
headerCapitalization: root.headerCapitalization
147145

148-
sortOrder: root.sortOrderProvider ? (prv.sortRevision, root.sortOrderProvider(index)) : ColumnSortOrder.Unsorted
146+
sortOrder: index === root.sortIndicatorColumn ? root.sortIndicatorOrder : ColumnSortOrder.Unsorted
149147

150148
navigation.panel: root.navigationPanel
151149
navigation.row: 0

src/framework/uicomponents/qml/Muse/UiComponents/tablesortfilterproxymodel.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ QItemSelectionModel* TableSortFilterProxyModel::selectionModel() const
5353
return m_selectionModel;
5454
}
5555

56+
int TableSortFilterProxyModel::sortIndicatorColumn() const
57+
{
58+
if (m_sortPipeline.empty() || m_sortPipeline.back().column != m_sortIconColumn) {
59+
return -1;
60+
}
61+
return m_sortIconColumn;
62+
}
63+
64+
ColumnSortOrder::Type TableSortFilterProxyModel::sortIndicatorOrder() const
65+
{
66+
if (m_sortPipeline.empty() || m_sortPipeline.back().column != m_sortIconColumn) {
67+
return ColumnSortOrder::Type::Unsorted;
68+
}
69+
return m_sortPipeline.back().ascending ? ColumnSortOrder::Type::Ascending : ColumnSortOrder::Type::Descending;
70+
}
71+
5672
void TableSortFilterProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
5773
{
5874
QSortFilterProxyModel::setSourceModel(sourceModel);
@@ -113,20 +129,6 @@ int TableSortFilterProxyModel::mapRowToSource(int proxyRow) const
113129
return idx.isValid() ? idx.row() : -1;
114130
}
115131

116-
ColumnSortOrder::Type TableSortFilterProxyModel::columnSortOrder(int column) const
117-
{
118-
if (column != m_sortIconColumn) {
119-
return ColumnSortOrder::Type::Unsorted;
120-
}
121-
122-
const auto it = std::find_if(m_sortPipeline.begin(), m_sortPipeline.end(),
123-
[column](const SortKey& k) { return k.column == column; });
124-
if (it != m_sortPipeline.end() - 1) {
125-
return ColumnSortOrder::Type::Unsorted;
126-
}
127-
return it->ascending ? ColumnSortOrder::Type::Ascending : ColumnSortOrder::Type::Descending;
128-
}
129-
130132
void TableSortFilterProxyModel::reapplySort()
131133
{
132134
if (!sourceModel()) {

src/framework/uicomponents/qml/Muse/UiComponents/tablesortfilterproxymodel.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,23 @@ class TableSortFilterProxyModel : public QSortFilterProxyModel
5757

5858
Q_PROPERTY(QItemSelectionModel* selectionModel READ selectionModel CONSTANT)
5959
Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged)
60+
Q_PROPERTY(int sortIndicatorColumn READ sortIndicatorColumn NOTIFY sortChanged)
61+
Q_PROPERTY(ColumnSortOrder::Type sortIndicatorOrder READ sortIndicatorOrder NOTIFY sortChanged)
6062

6163
public:
6264
explicit TableSortFilterProxyModel(QObject* parent = nullptr);
6365

6466
QItemSelectionModel* selectionModel() const;
6567

68+
int sortIndicatorColumn() const;
69+
ColumnSortOrder::Type sortIndicatorOrder() const;
70+
6671
void setSourceModel(QAbstractItemModel* sourceModel) override;
6772

6873
Q_INVOKABLE void toggleColumnSort(int column);
6974
Q_INVOKABLE void clearSort();
7075
Q_INVOKABLE void invalidateFilters();
7176
Q_INVOKABLE int mapRowToSource(int proxyRow) const;
72-
Q_INVOKABLE ColumnSortOrder::Type columnSortOrder(int column) const;
7377

7478
signals:
7579
void rowCountChanged();

0 commit comments

Comments
 (0)