From 9ccb940a1be0eb6eb2f6ac455474946ba3f71c24 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 21 Aug 2022 11:22:34 +0200 Subject: [PATCH] list models: include current dive in selection signal After sending a selection-change signal, there follows a current dive changed signal. Combine these two into a single signal, since usually the current dive is changed when the selection is changed. Signed-off-by: Berthold Stoeger --- core/selection.cpp | 2 +- core/subsurface-qt/divelistnotifier.h | 2 +- desktop-widgets/divelistview.cpp | 8 ++------ desktop-widgets/divelistview.h | 3 +-- qt-models/divetripmodel.cpp | 29 +++++++++------------------ qt-models/divetripmodel.h | 9 ++++----- qt-models/filtermodels.cpp | 12 ++--------- qt-models/filtermodels.h | 6 ++---- 8 files changed, 23 insertions(+), 48 deletions(-) diff --git a/core/selection.cpp b/core/selection.cpp index c4a7a0fe9..09f097e0f 100644 --- a/core/selection.cpp +++ b/core/selection.cpp @@ -202,7 +202,7 @@ void setSelection(const std::vector &selection, dive *currentDive, int c auto selectedDives = setSelectionCore(selection, currentDive, currentDc); // Send the new selection to the UI. - emit diveListNotifier.divesSelected(selectedDives); + emit diveListNotifier.divesSelected(selectedDives, current_dive); } // Set selection, but try to keep the current dive. If current dive is not in selection, diff --git a/core/subsurface-qt/divelistnotifier.h b/core/subsurface-qt/divelistnotifier.h index f39ea156b..7bbbdfbfb 100644 --- a/core/subsurface-qt/divelistnotifier.h +++ b/core/subsurface-qt/divelistnotifier.h @@ -111,7 +111,7 @@ signals: void tripChanged(dive_trip *trip, TripField field); // Selection changes - void divesSelected(const QVector &dives); + void divesSelected(const QVector &dives, dive *currentDive); // Dive site signals. Add and delete events are sent per dive site and // provide an index into the global dive site table. diff --git a/desktop-widgets/divelistview.cpp b/desktop-widgets/divelistview.cpp index bad5048e4..1346b0be7 100644 --- a/desktop-widgets/divelistview.cpp +++ b/desktop-widgets/divelistview.cpp @@ -40,7 +40,6 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), MultiFilterSortModel *m = MultiFilterSortModel::instance(); setModel(m); connect(m, &MultiFilterSortModel::selectionChanged, this, &DiveListView::diveSelectionChanged); - connect(m, &MultiFilterSortModel::currentDiveChanged, this, &DiveListView::currentDiveChanged); connect(&diveListNotifier, &DiveListNotifier::settingsChanged, this, &DiveListView::settingsChanged); setSortingEnabled(true); @@ -227,7 +226,7 @@ void DiveListView::reset() } // If items were selected, inform the selection model -void DiveListView::diveSelectionChanged(const QVector &indices) +void DiveListView::diveSelectionChanged(const QVector &indices, QModelIndex currentDive) { // This is the entry point for programmatical selection changes. // Set a flag so that selection changes are not further processed, @@ -262,16 +261,13 @@ void DiveListView::diveSelectionChanged(const QVector &indices) selectionChangeDone(); programmaticalSelectionChange = false; -} -void DiveListView::currentDiveChanged(QModelIndex index) -{ // 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(index, QItemSelectionModel::Current); + selectionModel()->setCurrentIndex(currentDive, QItemSelectionModel::Current); } // If rows are added, check which of these rows is a trip and expand the first column diff --git a/desktop-widgets/divelistview.h b/desktop-widgets/divelistview.h index 5b342d7f3..e46abdcf4 100644 --- a/desktop-widgets/divelistview.h +++ b/desktop-widgets/divelistview.h @@ -51,8 +51,7 @@ slots: void renumberDives(); void addDivesToTrip(); void shiftTimes(); - void diveSelectionChanged(const QVector &indices); - void currentDiveChanged(QModelIndex index); + void diveSelectionChanged(const QVector &indices, QModelIndex currentDive); void tripChanged(dive_trip *trip, TripField); private: void rowsInserted(const QModelIndex &parent, int start, int end) override; diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index 86bf635d0..3ed42d864 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -530,7 +530,7 @@ static ShownChange updateShownAll() return res; } -void DiveTripModelBase::currentChanged() +void DiveTripModelBase::currentChanged(dive *currentDive) { // On Desktop we use a signal to forward current-dive changed, on mobile we use ROLE_CURRENT. // TODO: Unify - use the role for both. @@ -540,21 +540,12 @@ void DiveTripModelBase::currentChanged() QModelIndex oldIdx = diveToIdx(oldCurrent); dataChanged(oldIdx, oldIdx, roles); } - if (current_dive && oldCurrent != current_dive) { - QModelIndex newIdx = diveToIdx(current_dive); + if (currentDive && oldCurrent != currentDive) { + QModelIndex newIdx = diveToIdx(currentDive); dataChanged(newIdx, newIdx, roles); } -#else - if (oldCurrent == current_dive) - return; - if (current_dive) { - QModelIndex newIdx = diveToIdx(current_dive); - emit currentDiveChanged(newIdx); - } else { - emit currentDiveChanged(QModelIndex()); - } #endif - oldCurrent = current_dive; + oldCurrent = currentDive; } // Find a range of matching elements in a vector. @@ -1378,7 +1369,7 @@ QModelIndex DiveTripModelTree::diveToIdx(const dive *d) const } } -void DiveTripModelTree::divesSelected(const QVector &divesIn) +void DiveTripModelTree::divesSelected(const QVector &divesIn, dive *currentDive) { QVector dives = visibleDives(divesIn); @@ -1390,10 +1381,10 @@ void DiveTripModelTree::divesSelected(const QVector &divesIn) processByTrip(dives, [this, &indices] (dive_trip *trip, const QVector &divesInTrip) { divesSelectedTrip(trip, divesInTrip, indices); }); - emit selectionChanged(indices); + emit selectionChanged(indices, diveToIdx(currentDive)); // The current dive has changed. Transform the current dive into an index and pass it on to the view. - currentChanged(); + currentChanged(currentDive); } void DiveTripModelTree::divesSelectedTrip(dive_trip *trip, const QVector &dives, QVector &indices) @@ -1648,7 +1639,7 @@ QModelIndex DiveTripModelList::diveToIdx(const dive *d) const return createIndex(it - items.begin(), 0); } -void DiveTripModelList::divesSelected(const QVector &divesIn) +void DiveTripModelList::divesSelected(const QVector &divesIn, dive *currentDive) { QVector dives = visibleDives(divesIn); @@ -1668,10 +1659,10 @@ void DiveTripModelList::divesSelected(const QVector &divesIn) indices.append(createIndex(j, 0, noParent)); } - emit selectionChanged(indices); + emit selectionChanged(indices, diveToIdx(currentDive)); // The current dive has changed. Transform the current dive into an index and pass it on to the view. - currentChanged(); + currentChanged(currentDive); } // Simple sorting helper for sorting against a criterium and if diff --git a/qt-models/divetripmodel.h b/qt-models/divetripmodel.h index 62f9efceb..e2bee4a47 100644 --- a/qt-models/divetripmodel.h +++ b/qt-models/divetripmodel.h @@ -84,8 +84,7 @@ signals: // into QModelIndexes according to the current view (tree/list). Finally, the DiveListView transforms these // indices into local indices according to current sorting/filtering and instructs the QSelectionModel to // perform the appropriate actions. - void selectionChanged(const QVector &indices); - void currentDiveChanged(QModelIndex index); + void selectionChanged(const QVector &indices, QModelIndex currentDive); protected: dive *oldCurrent; QBrush invalidForeground; @@ -97,7 +96,7 @@ protected: static QString tripTitle(const dive_trip *trip); static QString tripShortDate(const dive_trip *trip); static QString getDescription(int column); - void currentChanged(); + void currentChanged(dive *currentDive); virtual dive *diveOrNull(const QModelIndex &index) const = 0; // Returns a dive if this index represents a dive, null otherwise virtual void clearData() = 0; @@ -116,7 +115,7 @@ public slots: void divesChanged(const QVector &dives); void diveChanged(dive *d); void divesTimeChanged(timestamp_t delta, const QVector &dives); - void divesSelected(const QVector &dives); + void divesSelected(const QVector &dives, dive *currentDive); void tripChanged(dive_trip *trip, TripField); void filterReset(); @@ -194,7 +193,7 @@ public slots: void divesTimeChanged(timestamp_t delta, const QVector &dives); // Does nothing in list view. //void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector &dives); - void divesSelected(const QVector &dives); + void divesSelected(const QVector &dives, dive *currentDive); void filterReset(); public: diff --git a/qt-models/filtermodels.cpp b/qt-models/filtermodels.cpp index fc0ed9396..3b2381f78 100644 --- a/qt-models/filtermodels.cpp +++ b/qt-models/filtermodels.cpp @@ -27,13 +27,12 @@ void MultiFilterSortModel::resetModel(DiveTripModelBase::Layout layout) setSourceModel(model.get()); connect(model.get(), &DiveTripModelBase::selectionChanged, this, &MultiFilterSortModel::selectionChangedSlot); - connect(model.get(), &DiveTripModelBase::currentDiveChanged, this, &MultiFilterSortModel::currentDiveChangedSlot); model->initSelection(); LocationInformationModel::instance()->update(); } // Translate selection into local indices and re-emit signal -void MultiFilterSortModel::selectionChangedSlot(const QVector &indices) +void MultiFilterSortModel::selectionChangedSlot(const QVector &indices, QModelIndex currentDive) { QVector indicesLocal; indicesLocal.reserve(indices.size()); @@ -42,15 +41,8 @@ void MultiFilterSortModel::selectionChangedSlot(const QVector &indi if (local.isValid()) indicesLocal.push_back(local); } - emit selectionChanged(indicesLocal); -} -// Translate current dive into local indices and re-emit signal -void MultiFilterSortModel::currentDiveChangedSlot(QModelIndex index) -{ - QModelIndex local = mapFromSource(index); - if (local.isValid()) - emit currentDiveChanged(mapFromSource(index)); + emit selectionChanged(indicesLocal, mapFromSource(currentDive)); } bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const diff --git a/qt-models/filtermodels.h b/qt-models/filtermodels.h index 8895f5b2c..27ff21943 100644 --- a/qt-models/filtermodels.h +++ b/qt-models/filtermodels.h @@ -18,11 +18,9 @@ public: void resetModel(DiveTripModelBase::Layout layout); signals: - void selectionChanged(const QVector &indices); - void currentDiveChanged(QModelIndex index); + void selectionChanged(const QVector &indices, QModelIndex currentDive); private slots: - void selectionChangedSlot(const QVector &indices); - void currentDiveChangedSlot(QModelIndex index); + void selectionChangedSlot(const QVector &indices, QModelIndex currentDive); private: MultiFilterSortModel(QObject *parent = 0); std::unique_ptr model;