aboutsummaryrefslogtreecommitdiffstats
path: root/desktop-widgets/divelistview.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-11-04 17:54:38 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2018-11-05 07:55:53 -0800
commitf2f18b4e16656f0eb0d6b41981baa991ade3d33c (patch)
treec833d718a3f0c34e4dfb3fa38a9d6e0e7a94e74b /desktop-widgets/divelistview.cpp
parent236373b6ba92b9a3935958a367e88b05d4f54854 (diff)
downloadsubsurface-f2f18b4e16656f0eb0d6b41981baa991ade3d33c.tar.gz
Dive list: split reload() in reload() and setSortOrder()
The DiveListView code had a very fundamental problem with its header: Each had its own idea of who is responsible for sorting. Since we can't easily change QHeaderView, accept QHeaderView as the authority on sort-column and order. To make this possible, split the reload() function in two distinct functions: - reload() reloads the model and sorts according to the current sort criterion. - setSortOrder() tells the header to display a certain sort criterion. If this is a new criterion, it will then emit a signal. In this signal, resort according to that criterion. Thus, the actual sorting code has to be moved from the headerClicked() to a new sortIndicatorChanged() slot. Morover, the sorting of the QHeaderView has to be used. Reported-by: Stefan Fuchs <sfuchs@gmx.de> Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/divelistview.cpp')
-rw-r--r--desktop-widgets/divelistview.cpp33
1 files changed, 14 insertions, 19 deletions
diff --git a/desktop-widgets/divelistview.cpp b/desktop-widgets/divelistview.cpp
index a31813697..227ae502e 100644
--- a/desktop-widgets/divelistview.cpp
+++ b/desktop-widgets/divelistview.cpp
@@ -27,7 +27,7 @@
#include "core/subsurface-qt/DiveListNotifier.h"
DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelection(false), sortColumn(DiveTripModel::NR),
- currentOrder(Qt::AscendingOrder), dontEmitDiveChangedSignal(false), selectionSaved(false),
+ currentOrder(Qt::AscendingOrder), currentLayout(DiveTripModel::TREE), dontEmitDiveChangedSignal(false), selectionSaved(false),
initialColumnWidths(DiveTripModel::COLUMNS, 50) // Set up with default length 50
{
setItemDelegate(new DiveListDelegate(this));
@@ -48,7 +48,7 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
header()->setStretchLastSection(true);
header()->setSortIndicatorShown(true);
header()->setSectionsClickable(true);
- connect(header(), &QHeaderView::sectionPressed, this, &DiveListView::headerClicked);
+ connect(header(), &QHeaderView::sortIndicatorChanged, this, &DiveListView::sortIndicatorChanged);
installEventFilter(this);
@@ -459,18 +459,12 @@ bool DiveListView::eventFilter(QObject *, QEvent *event)
return true;
}
-void DiveListView::headerClicked(int i)
+void DiveListView::sortIndicatorChanged(int i, Qt::SortOrder order)
{
DiveTripModel::Layout newLayout = i == (int)DiveTripModel::NR ? DiveTripModel::TREE : DiveTripModel::LIST;
/* No layout change? Just re-sort, and scroll to first selection, making sure all selections are expanded */
if (currentLayout == newLayout) {
- // If this is the same column as before, change sort order. Otherwise, choose a default
- // sort order (ascending).
- if (sortColumn == i)
- currentOrder = (currentOrder == Qt::DescendingOrder) ? Qt::AscendingOrder : Qt::DescendingOrder;
- else
- currentOrder = Qt::AscendingOrder;
- sortByColumn(i, currentOrder);
+ sortByColumn(i, order);
} else {
// clear the model, repopulate with new indexes.
rememberSelection();
@@ -478,27 +472,28 @@ void DiveListView::headerClicked(int i)
if (currentLayout == DiveTripModel::TREE)
backupExpandedRows();
currentLayout = newLayout;
- currentOrder = Qt::AscendingOrder;
MultiFilterSortModel::instance()->setLayout(newLayout);
- sortByColumn(i, currentOrder);
+ sortByColumn(i, order);
if (newLayout == DiveTripModel::TREE)
restoreExpandedRows();
restoreSelection();
}
// remember the new sort column
sortColumn = i;
+ currentOrder = order;
}
-void DiveListView::reload(DiveTripModel::Layout layout)
+void DiveListView::setSortOrder(int i, Qt::SortOrder order)
{
- if (layout == DiveTripModel::CURRENT)
- layout = currentLayout;
- else
- currentLayout = layout;
+ // The QHeaderView will call our signal if the sort order changed
+ header()->setSortIndicator(i, order);
+}
- MultiFilterSortModel::instance()->setLayout(layout);
+void DiveListView::reload()
+{
+ // A side-effect of setting the layout is reloading the model data
+ MultiFilterSortModel::instance()->setLayout(currentLayout);
- sortByColumn(sortColumn, currentOrder);
if (amount_selected && current_dive != NULL)
selectDive(get_divenr(current_dive), true);
else