diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-11-27 21:55:37 +0100 |
---|---|---|
committer | bstoeger <32835590+bstoeger@users.noreply.github.com> | 2019-12-04 13:00:23 +0100 |
commit | e46b1e88d92990d26d34981baf70fcf0b58ebec7 (patch) | |
tree | bccf82da75c5e1c1a7e7578109e5229d22ca1288 /desktop-widgets/divelistview.cpp | |
parent | a431840075fbba6f882627496f18a0fb80630e4f (diff) | |
download | subsurface-e46b1e88d92990d26d34981baf70fcf0b58ebec7.tar.gz |
Selection: move translation of indexes to filter model
The DiveListView caught signals from the DiveTripModel
with the corresponding indexes. However, the DiveListView
is actually connected to the MultiFilterSortModel and
thus has to translate the indexes.
Instead, catch the signals in the MultiFilterSortModel,
transform them and resend. Let the DiveListView get
its signal from the MultiFilterSortModel.
Yes, this makes things less efficient because there is
an extra signal. On the upside, the makes data-flow much
more logical. Selection will have to be fixed anyway.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/divelistview.cpp')
-rw-r--r-- | desktop-widgets/divelistview.cpp | 41 |
1 files changed, 11 insertions, 30 deletions
diff --git a/desktop-widgets/divelistview.cpp b/desktop-widgets/divelistview.cpp index b81b16557..83d8b14f2 100644 --- a/desktop-widgets/divelistview.cpp +++ b/desktop-widgets/divelistview.cpp @@ -37,7 +37,10 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec setItemDelegate(new DiveListDelegate(this)); setUniformRowHeights(true); setItemDelegateForColumn(DiveTripModelBase::RATING, new StarWidgetsDelegate(this)); - setModel(MultiFilterSortModel::instance()); + MultiFilterSortModel *m = MultiFilterSortModel::instance(); + setModel(m); + connect(m, &MultiFilterSortModel::selectionChanged, this, &DiveListView::diveSelectionChanged); + connect(m, &MultiFilterSortModel::currentDiveChanged, this, &DiveListView::currentDiveChanged); setSortingEnabled(true); setContextMenuPolicy(Qt::DefaultContextMenu); @@ -83,14 +86,8 @@ DiveListView::~DiveListView() void DiveListView::resetModel() { - MultiFilterSortModel::instance()->resetModel(currentLayout); - // If the model was reset, we have to reconnect the signals and tell - // the filter model to update its source model. - DiveTripModelBase *m = DiveTripModelBase::instance(); - connect(m, &DiveTripModelBase::selectionChanged, this, &DiveListView::diveSelectionChanged); - connect(m, &DiveTripModelBase::currentDiveChanged, this, &DiveListView::currentDiveChanged); - // Get the initial selection - m->initSelection(); + MultiFilterSortModel *m = MultiFilterSortModel::instance(); + m->resetModel(currentLayout); } void DiveListView::calculateInitialColumnWidth(int col) @@ -201,25 +198,14 @@ void DiveListView::reset() void DiveListView::diveSelectionChanged(const QVector<QModelIndex> &indexes) { clearSelection(); - MultiFilterSortModel *m = MultiFilterSortModel::instance(); QItemSelectionModel *s = selectionModel(); for (const QModelIndex &index: indexes) { - // We have to transform the indices into local indices, since - // there might be sorting or filtering in effect. - QModelIndex localIndex = m->mapFromSource(index); - - // It might be possible that the item is not shown (filter is - // in effect). Then we get an invalid index and should ignore - // this selection. - if (!localIndex.isValid()) - continue; - - s->select(localIndex, QItemSelectionModel::Rows | QItemSelectionModel::Select); + s->select(index, QItemSelectionModel::Rows | QItemSelectionModel::Select); // If an item of a not-yet expanded trip is selected, expand the trip. - if (localIndex.parent().isValid() && !isExpanded(localIndex.parent())) { + if (index.parent().isValid() && !isExpanded(index.parent())) { setAnimated(false); - expand(localIndex.parent()); + expand(index.parent()); setAnimated(true); } } @@ -229,17 +215,12 @@ void DiveListView::diveSelectionChanged(const QVector<QModelIndex> &indexes) void DiveListView::currentDiveChanged(QModelIndex index) { - // Transform the index into a local index, since - // there might be sorting or filtering in effect. - MultiFilterSortModel *m = MultiFilterSortModel::instance(); - QModelIndex localIndex = m->mapFromSource(index); - - // Then, set the currently activated row. + // Set the currently activated row. // Note, we have to use the QItemSelectionModel::Current mode to avoid // changing our selection (in contrast to Qt's documentation, which // instructs to use QItemSelectionModel::NoUpdate, which results in // funny side-effects). - selectionModel()->setCurrentIndex(localIndex, QItemSelectionModel::Current); + selectionModel()->setCurrentIndex(index, QItemSelectionModel::Current); } // If rows are added, check which of these rows is a trip and expand the first column |