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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-08-21 11:22:34 +02:00 committed by bstoeger
parent 832398180c
commit 9ccb940a1b
8 changed files with 23 additions and 48 deletions

View file

@ -202,7 +202,7 @@ void setSelection(const std::vector<dive *> &selection, dive *currentDive, int c
auto selectedDives = setSelectionCore(selection, currentDive, currentDc); auto selectedDives = setSelectionCore(selection, currentDive, currentDc);
// Send the new selection to the UI. // 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, // Set selection, but try to keep the current dive. If current dive is not in selection,

View file

@ -111,7 +111,7 @@ signals:
void tripChanged(dive_trip *trip, TripField field); void tripChanged(dive_trip *trip, TripField field);
// Selection changes // Selection changes
void divesSelected(const QVector<dive *> &dives); void divesSelected(const QVector<dive *> &dives, dive *currentDive);
// Dive site signals. Add and delete events are sent per dive site and // Dive site signals. Add and delete events are sent per dive site and
// provide an index into the global dive site table. // provide an index into the global dive site table.

View file

@ -40,7 +40,6 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent),
MultiFilterSortModel *m = MultiFilterSortModel::instance(); MultiFilterSortModel *m = MultiFilterSortModel::instance();
setModel(m); setModel(m);
connect(m, &MultiFilterSortModel::selectionChanged, this, &DiveListView::diveSelectionChanged); connect(m, &MultiFilterSortModel::selectionChanged, this, &DiveListView::diveSelectionChanged);
connect(m, &MultiFilterSortModel::currentDiveChanged, this, &DiveListView::currentDiveChanged);
connect(&diveListNotifier, &DiveListNotifier::settingsChanged, this, &DiveListView::settingsChanged); connect(&diveListNotifier, &DiveListNotifier::settingsChanged, this, &DiveListView::settingsChanged);
setSortingEnabled(true); setSortingEnabled(true);
@ -227,7 +226,7 @@ void DiveListView::reset()
} }
// If items were selected, inform the selection model // If items were selected, inform the selection model
void DiveListView::diveSelectionChanged(const QVector<QModelIndex> &indices) void DiveListView::diveSelectionChanged(const QVector<QModelIndex> &indices, QModelIndex currentDive)
{ {
// This is the entry point for programmatical selection changes. // This is the entry point for programmatical selection changes.
// Set a flag so that selection changes are not further processed, // Set a flag so that selection changes are not further processed,
@ -262,16 +261,13 @@ void DiveListView::diveSelectionChanged(const QVector<QModelIndex> &indices)
selectionChangeDone(); selectionChangeDone();
programmaticalSelectionChange = false; programmaticalSelectionChange = false;
}
void DiveListView::currentDiveChanged(QModelIndex index)
{
// Set the currently activated row. // Set the currently activated row.
// Note, we have to use the QItemSelectionModel::Current mode to avoid // Note, we have to use the QItemSelectionModel::Current mode to avoid
// changing our selection (in contrast to Qt's documentation, which // changing our selection (in contrast to Qt's documentation, which
// instructs to use QItemSelectionModel::NoUpdate, which results in // instructs to use QItemSelectionModel::NoUpdate, which results in
// funny side-effects). // 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 // If rows are added, check which of these rows is a trip and expand the first column

View file

@ -51,8 +51,7 @@ slots:
void renumberDives(); void renumberDives();
void addDivesToTrip(); void addDivesToTrip();
void shiftTimes(); void shiftTimes();
void diveSelectionChanged(const QVector<QModelIndex> &indices); void diveSelectionChanged(const QVector<QModelIndex> &indices, QModelIndex currentDive);
void currentDiveChanged(QModelIndex index);
void tripChanged(dive_trip *trip, TripField); void tripChanged(dive_trip *trip, TripField);
private: private:
void rowsInserted(const QModelIndex &parent, int start, int end) override; void rowsInserted(const QModelIndex &parent, int start, int end) override;

View file

@ -530,7 +530,7 @@ static ShownChange updateShownAll()
return res; 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. // On Desktop we use a signal to forward current-dive changed, on mobile we use ROLE_CURRENT.
// TODO: Unify - use the role for both. // TODO: Unify - use the role for both.
@ -540,21 +540,12 @@ void DiveTripModelBase::currentChanged()
QModelIndex oldIdx = diveToIdx(oldCurrent); QModelIndex oldIdx = diveToIdx(oldCurrent);
dataChanged(oldIdx, oldIdx, roles); dataChanged(oldIdx, oldIdx, roles);
} }
if (current_dive && oldCurrent != current_dive) { if (currentDive && oldCurrent != currentDive) {
QModelIndex newIdx = diveToIdx(current_dive); QModelIndex newIdx = diveToIdx(currentDive);
dataChanged(newIdx, newIdx, roles); 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 #endif
oldCurrent = current_dive; oldCurrent = currentDive;
} }
// Find a range of matching elements in a vector. // 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<dive *> &divesIn) void DiveTripModelTree::divesSelected(const QVector<dive *> &divesIn, dive *currentDive)
{ {
QVector <dive *> dives = visibleDives(divesIn); QVector <dive *> dives = visibleDives(divesIn);
@ -1390,10 +1381,10 @@ void DiveTripModelTree::divesSelected(const QVector<dive *> &divesIn)
processByTrip(dives, [this, &indices] (dive_trip *trip, const QVector<dive *> &divesInTrip) processByTrip(dives, [this, &indices] (dive_trip *trip, const QVector<dive *> &divesInTrip)
{ divesSelectedTrip(trip, divesInTrip, indices); }); { 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. // 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<dive *> &dives, QVector<QModelIndex> &indices) void DiveTripModelTree::divesSelectedTrip(dive_trip *trip, const QVector<dive *> &dives, QVector<QModelIndex> &indices)
@ -1648,7 +1639,7 @@ QModelIndex DiveTripModelList::diveToIdx(const dive *d) const
return createIndex(it - items.begin(), 0); return createIndex(it - items.begin(), 0);
} }
void DiveTripModelList::divesSelected(const QVector<dive *> &divesIn) void DiveTripModelList::divesSelected(const QVector<dive *> &divesIn, dive *currentDive)
{ {
QVector<dive *> dives = visibleDives(divesIn); QVector<dive *> dives = visibleDives(divesIn);
@ -1668,10 +1659,10 @@ void DiveTripModelList::divesSelected(const QVector<dive *> &divesIn)
indices.append(createIndex(j, 0, noParent)); 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. // 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 // Simple sorting helper for sorting against a criterium and if

View file

@ -84,8 +84,7 @@ signals:
// into QModelIndexes according to the current view (tree/list). Finally, the DiveListView transforms these // 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 // indices into local indices according to current sorting/filtering and instructs the QSelectionModel to
// perform the appropriate actions. // perform the appropriate actions.
void selectionChanged(const QVector<QModelIndex> &indices); void selectionChanged(const QVector<QModelIndex> &indices, QModelIndex currentDive);
void currentDiveChanged(QModelIndex index);
protected: protected:
dive *oldCurrent; dive *oldCurrent;
QBrush invalidForeground; QBrush invalidForeground;
@ -97,7 +96,7 @@ protected:
static QString tripTitle(const dive_trip *trip); static QString tripTitle(const dive_trip *trip);
static QString tripShortDate(const dive_trip *trip); static QString tripShortDate(const dive_trip *trip);
static QString getDescription(int column); 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 dive *diveOrNull(const QModelIndex &index) const = 0; // Returns a dive if this index represents a dive, null otherwise
virtual void clearData() = 0; virtual void clearData() = 0;
@ -116,7 +115,7 @@ public slots:
void divesChanged(const QVector<dive *> &dives); void divesChanged(const QVector<dive *> &dives);
void diveChanged(dive *d); void diveChanged(dive *d);
void divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives); void divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives);
void divesSelected(const QVector<dive *> &dives); void divesSelected(const QVector<dive *> &dives, dive *currentDive);
void tripChanged(dive_trip *trip, TripField); void tripChanged(dive_trip *trip, TripField);
void filterReset(); void filterReset();
@ -194,7 +193,7 @@ public slots:
void divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives); void divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives);
// Does nothing in list view. // Does nothing in list view.
//void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &dives); //void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &dives);
void divesSelected(const QVector<dive *> &dives); void divesSelected(const QVector<dive *> &dives, dive *currentDive);
void filterReset(); void filterReset();
public: public:

View file

@ -27,13 +27,12 @@ void MultiFilterSortModel::resetModel(DiveTripModelBase::Layout layout)
setSourceModel(model.get()); setSourceModel(model.get());
connect(model.get(), &DiveTripModelBase::selectionChanged, this, &MultiFilterSortModel::selectionChangedSlot); connect(model.get(), &DiveTripModelBase::selectionChanged, this, &MultiFilterSortModel::selectionChangedSlot);
connect(model.get(), &DiveTripModelBase::currentDiveChanged, this, &MultiFilterSortModel::currentDiveChangedSlot);
model->initSelection(); model->initSelection();
LocationInformationModel::instance()->update(); LocationInformationModel::instance()->update();
} }
// Translate selection into local indices and re-emit signal // Translate selection into local indices and re-emit signal
void MultiFilterSortModel::selectionChangedSlot(const QVector<QModelIndex> &indices) void MultiFilterSortModel::selectionChangedSlot(const QVector<QModelIndex> &indices, QModelIndex currentDive)
{ {
QVector<QModelIndex> indicesLocal; QVector<QModelIndex> indicesLocal;
indicesLocal.reserve(indices.size()); indicesLocal.reserve(indices.size());
@ -42,15 +41,8 @@ void MultiFilterSortModel::selectionChangedSlot(const QVector<QModelIndex> &indi
if (local.isValid()) if (local.isValid())
indicesLocal.push_back(local); indicesLocal.push_back(local);
} }
emit selectionChanged(indicesLocal);
}
// Translate current dive into local indices and re-emit signal emit selectionChanged(indicesLocal, mapFromSource(currentDive));
void MultiFilterSortModel::currentDiveChangedSlot(QModelIndex index)
{
QModelIndex local = mapFromSource(index);
if (local.isValid())
emit currentDiveChanged(mapFromSource(index));
} }
bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const

View file

@ -18,11 +18,9 @@ public:
void resetModel(DiveTripModelBase::Layout layout); void resetModel(DiveTripModelBase::Layout layout);
signals: signals:
void selectionChanged(const QVector<QModelIndex> &indices); void selectionChanged(const QVector<QModelIndex> &indices, QModelIndex currentDive);
void currentDiveChanged(QModelIndex index);
private slots: private slots:
void selectionChangedSlot(const QVector<QModelIndex> &indices); void selectionChangedSlot(const QVector<QModelIndex> &indices, QModelIndex currentDive);
void currentDiveChangedSlot(QModelIndex index);
private: private:
MultiFilterSortModel(QObject *parent = 0); MultiFilterSortModel(QObject *parent = 0);
std::unique_ptr<DiveTripModelBase> model; std::unique_ptr<DiveTripModelBase> model;