selection: trickle down trip selection

The trip selection code was an awkward layering violation.
Whereas dive selections due to dive undo-commands trickled
down via DiveTripModel-->MultiFilterSortModel-->DiveListView,
for trip editing, the DiveListView directly intercepted the
TripEdited signal.

Instead, mimic the dive-selection code. This is a bit longer
but more consistent and logical. The undo/redo of trip changes
is now also a "programmatical" change of the selection.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-08-27 17:28:34 +02:00 committed by bstoeger
parent 9ccb940a1b
commit 8581e213ed
11 changed files with 113 additions and 57 deletions

View file

@ -683,6 +683,7 @@ 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::tripSelected, this, &DiveTripModelTree::tripSelected);
connect(&diveListNotifier, &DiveListNotifier::tripChanged, this, &DiveTripModelTree::tripChanged);
connect(&diveListNotifier, &DiveListNotifier::filterReset, this, &DiveTripModelTree::filterReset);
connect(&diveListNotifier, &DiveListNotifier::cylinderAdded, this, &DiveTripModelTree::diveChanged);
@ -1425,6 +1426,24 @@ void DiveTripModelTree::divesSelectedTrip(dive_trip *trip, const QVector<dive *>
}
}
void DiveTripModelTree::tripSelected(dive_trip *trip, dive *currentDive)
{
if (!trip)
return;
// Find the trip.
int idx = findTripIdx(trip);
if (idx < 0) {
// We don't know the trip - this shouldn't happen. We seem to have
// missed some signals!
qWarning() << "DiveTripModelTree::tripSelected(): unknown trip";
return;
}
QModelIndex tripIdx = createIndex(idx, 0, noParent);
emit DiveTripModelBase::tripSelected(tripIdx, diveToIdx(currentDive));
}
bool DiveTripModelTree::lessThan(const QModelIndex &i1, const QModelIndex &i2) const
{
// In tree mode we don't support any sorting!
@ -1445,6 +1464,7 @@ DiveTripModelList::DiveTripModelList(QObject *parent) : DiveTripModelBase(parent
//connect(&diveListNotifier, &DiveListNotifier::divesMovedBetweenTrips, this, &DiveTripModelList::divesMovedBetweenTrips);
connect(&diveListNotifier, &DiveListNotifier::divesTimeChanged, this, &DiveTripModelList::divesTimeChanged);
connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModelList::divesSelected);
connect(&diveListNotifier, &DiveListNotifier::tripSelected, this, &DiveTripModelList::tripSelected);
connect(&diveListNotifier, &DiveListNotifier::filterReset, this, &DiveTripModelList::filterReset);
connect(&diveListNotifier, &DiveListNotifier::cylinderAdded, this, &DiveTripModelList::diveChanged);
connect(&diveListNotifier, &DiveListNotifier::cylinderEdited, this, &DiveTripModelList::diveChanged);
@ -1665,6 +1685,21 @@ void DiveTripModelList::divesSelected(const QVector<dive *> &divesIn, dive *curr
currentChanged(currentDive);
}
void DiveTripModelList::tripSelected(dive_trip *trip, dive *currentDive)
{
if (!trip)
return;
// In the list view, there are no trips, so simply transform this into
// a dive selection.
QVector<dive *> dives;
dives.reserve(trip->dives.nr);
for (int i = 0; i < trip->dives.nr; ++i)
dives.push_back(trip->dives.dives[i]);
divesSelected(dives, currentDive);
}
// Simple sorting helper for sorting against a criterium and if
// that is undefined against a different criterium.
// Return true if diff1 < 0, false if diff1 > 0.

View file

@ -85,6 +85,7 @@ signals:
// indices into local indices according to current sorting/filtering and instructs the QSelectionModel to
// perform the appropriate actions.
void selectionChanged(const QVector<QModelIndex> &indices, QModelIndex currentDive);
void tripSelected(QModelIndex trip, QModelIndex currentDive);
protected:
dive *oldCurrent;
QBrush invalidForeground;
@ -116,6 +117,7 @@ public slots:
void diveChanged(dive *d);
void divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives);
void divesSelected(const QVector<dive *> &dives, dive *currentDive);
void tripSelected(dive_trip *trip, dive *currentDive);
void tripChanged(dive_trip *trip, TripField);
void filterReset();
@ -194,6 +196,7 @@ public slots:
// Does nothing in list view.
//void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &dives);
void divesSelected(const QVector<dive *> &dives, dive *currentDive);
void tripSelected(dive_trip *trip, dive *currentDive);
void filterReset();
public:

View file

@ -27,6 +27,7 @@ void MultiFilterSortModel::resetModel(DiveTripModelBase::Layout layout)
setSourceModel(model.get());
connect(model.get(), &DiveTripModelBase::selectionChanged, this, &MultiFilterSortModel::selectionChangedSlot);
connect(model.get(), &DiveTripModelBase::tripSelected, this, &MultiFilterSortModel::tripSelectedSlot);
model->initSelection();
LocationInformationModel::instance()->update();
}
@ -45,6 +46,16 @@ void MultiFilterSortModel::selectionChangedSlot(const QVector<QModelIndex> &indi
emit selectionChanged(indicesLocal, mapFromSource(currentDive));
}
// Translate selection into local indices and re-emit signal
void MultiFilterSortModel::tripSelectedSlot(QModelIndex trip, QModelIndex currentDive)
{
QModelIndex local = mapFromSource(trip);
if (!local.isValid())
return;
emit tripSelected(local, mapFromSource(currentDive));
}
bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
return true;

View file

@ -19,8 +19,10 @@ public:
void resetModel(DiveTripModelBase::Layout layout);
signals:
void selectionChanged(const QVector<QModelIndex> &indices, QModelIndex currentDive);
void tripSelected(QModelIndex trip, QModelIndex currentDive);
private slots:
void selectionChangedSlot(const QVector<QModelIndex> &indices, QModelIndex currentDive);
void tripSelectedSlot(QModelIndex trip, QModelIndex currentDive);
private:
MultiFilterSortModel(QObject *parent = 0);
std::unique_ptr<DiveTripModelBase> model;