From e1abf9485cf59f1b8cb79d827fa386af48f095a4 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 23 Jun 2019 11:13:41 +0200 Subject: Undo: unify selection behavior in dive-list commands Some commands tried to retain the current selection on undo/redo, others set the selection to the modified dives. The latter was introduced because it was easier in some cases, but it is probably more user-friendly because the user gets feedback on the change. Therefore, unify to always select the affected dives on undo()/redo(). Signed-off-by: Berthold Stoeger --- qt-models/divetripmodel.cpp | 38 ++++++++++++-------------------------- qt-models/divetripmodel.h | 14 ++++---------- 2 files changed, 16 insertions(+), 36 deletions(-) (limited to 'qt-models') diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index e15d5d0fd..a0aa1f5ba 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -417,16 +417,6 @@ bool DiveTripModelBase::setData(const QModelIndex &index, const QVariant &value, return true; } -void DiveTripModelBase::divesSelected(const QVector &dives) -{ - changeDiveSelection(dives, true); -} - -void DiveTripModelBase::divesDeselected(const QVector &dives) -{ - changeDiveSelection(dives, false); -} - // Find a range of matching elements in a vector. // Input parameters: // v: vector to be searched @@ -561,7 +551,6 @@ DiveTripModelTree::DiveTripModelTree(QObject *parent) : DiveTripModelBase(parent connect(&diveListNotifier, &DiveListNotifier::divesMovedBetweenTrips, this, &DiveTripModelTree::divesMovedBetweenTrips); connect(&diveListNotifier, &DiveListNotifier::divesTimeChanged, this, &DiveTripModelTree::divesTimeChanged); connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModelTree::divesSelected); - connect(&diveListNotifier, &DiveListNotifier::divesDeselected, this, &DiveTripModelTree::divesDeselected); connect(&diveListNotifier, &DiveListNotifier::currentDiveChanged, this, &DiveTripModelTree::currentDiveChanged); connect(&diveListNotifier, &DiveListNotifier::tripChanged, this, &DiveTripModelTree::tripChanged); @@ -1037,7 +1026,6 @@ void DiveTripModelTree::divesMovedBetweenTrips(dive_trip *from, dive_trip *to, b QVector selectedDives = filterSelectedDives(dives); divesAdded(to, createTo, dives); divesDeleted(from, deleteFrom, dives); - changeDiveSelectionTrip(to, dives, true); } void DiveTripModelTree::divesTimeChanged(timestamp_t delta, const QVector &dives) @@ -1061,22 +1049,23 @@ void DiveTripModelTree::divesTimeChangedTrip(dive_trip *trip, timestamp_t delta, QVector selectedDives = filterSelectedDives(dives); divesDeleted(trip, false, dives); divesAdded(trip, false, dives); - changeDiveSelectionTrip(trip, selectedDives, true); -} - -void DiveTripModelTree::changeDiveSelection(const QVector &dives, bool select) -{ - processByTrip(dives, [this, select] (dive_trip *trip, const QVector &divesInTrip) - { changeDiveSelectionTrip(trip, divesInTrip, select); }); } -void DiveTripModelTree::changeDiveSelectionTrip(dive_trip *trip, const QVector &dives, bool select) +void DiveTripModelTree::divesSelected(const QVector &dives) { // We got a number of dives that have been selected. Turn this into QModelIndexes and // emit a signal, so that views can change the selection. QVector indexes; indexes.reserve(dives.count()); + processByTrip(dives, [this, &indexes] (dive_trip *trip, const QVector &divesInTrip) + { divesSelectedTrip(trip, divesInTrip, indexes); }); + + emit selectionChanged(indexes); +} + +void DiveTripModelTree::divesSelectedTrip(dive_trip *trip, const QVector &dives, QVector &indexes) +{ if (!trip) { // This is at the top level. // Since both lists are sorted, we can do this linearly. Perhaps a binary search @@ -1095,7 +1084,7 @@ void DiveTripModelTree::changeDiveSelectionTrip(dive_trip *trip, const QVector &dives, bool select) +void DiveTripModelList::divesSelected(const QVector &dives) { // We got a number of dives that have been selected. Turn this into QModelIndexes and // emit a signal, so that views can change the selection. @@ -1320,7 +1306,7 @@ void DiveTripModelList::changeDiveSelection(const QVector &dives, bool s indexes.append(createIndex(j, 0, noParent)); } - emit selectionChanged(indexes, select); + emit selectionChanged(indexes); } void DiveTripModelList::currentDiveChanged() diff --git a/qt-models/divetripmodel.h b/qt-models/divetripmodel.h index 8903cb0c1..734612ff7 100644 --- a/qt-models/divetripmodel.h +++ b/qt-models/divetripmodel.h @@ -89,19 +89,13 @@ signals: // into QModelIndexes according to the current view (tree/list). Finally, the DiveListView transforms these // indexes into local indexes according to current sorting/filtering and instructs the QSelectionModel to // perform the appropriate actions. - void selectionChanged(const QVector &indexes, bool select); + void selectionChanged(const QVector &indexes); void newCurrentDive(QModelIndex index); -protected slots: - void divesSelected(const QVector &dives); - void divesDeselected(const QVector &dives); protected: // Access trip and dive data static QVariant diveData(const struct dive *d, int column, int role); static QVariant tripData(const dive_trip *trip, int column, int role); - // Select or deselect dives - virtual void changeDiveSelection(const QVector &dives, bool select) = 0; - virtual dive *diveOrNull(const QModelIndex &index) const = 0; // Returns a dive if this index represents a dive, null otherwise }; @@ -114,6 +108,7 @@ public slots: void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector &dives); void divesChanged(const QVector &dives); void divesTimeChanged(timestamp_t delta, const QVector &dives); + void divesSelected(const QVector &dives); void currentDiveChanged(); void tripChanged(dive_trip *trip, TripField); @@ -126,8 +121,7 @@ private: QVariant data(const QModelIndex &index, int role) const override; void filterFinished() override; bool lessThan(const QModelIndex &i1, const QModelIndex &i2) const override; - void changeDiveSelection(const QVector &dives, bool select) override; - void changeDiveSelectionTrip(dive_trip *trip, const QVector &dives, bool select); + void divesSelectedTrip(dive_trip *trip, const QVector &dives, QVector &); dive *diveOrNull(const QModelIndex &index) const override; bool setShown(const QModelIndex &idx, bool shown); void divesChangedTrip(dive_trip *trip, const QVector &dives); @@ -181,6 +175,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 currentDiveChanged(); public: @@ -192,7 +187,6 @@ private: QVariant data(const QModelIndex &index, int role) const override; void filterFinished() override; bool lessThan(const QModelIndex &i1, const QModelIndex &i2) const override; - void changeDiveSelection(const QVector &dives, bool select) override; dive *diveOrNull(const QModelIndex &index) const override; bool setShown(const QModelIndex &idx, bool shown); -- cgit v1.2.3-70-g09d2