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);
// 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,

View file

@ -111,7 +111,7 @@ signals:
void tripChanged(dive_trip *trip, TripField field);
// 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
// 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();
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<QModelIndex> &indices)
void DiveListView::diveSelectionChanged(const QVector<QModelIndex> &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<QModelIndex> &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

View file

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

View file

@ -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<dive *> &divesIn)
void DiveTripModelTree::divesSelected(const QVector<dive *> &divesIn, dive *currentDive)
{
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)
{ 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<dive *> &dives, QVector<QModelIndex> &indices)
@ -1648,7 +1639,7 @@ QModelIndex DiveTripModelList::diveToIdx(const dive *d) const
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);
@ -1668,10 +1659,10 @@ void DiveTripModelList::divesSelected(const QVector<dive *> &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

View file

@ -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<QModelIndex> &indices);
void currentDiveChanged(QModelIndex index);
void selectionChanged(const QVector<QModelIndex> &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<dive *> &dives);
void diveChanged(dive *d);
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 filterReset();
@ -194,7 +193,7 @@ public slots:
void divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives);
// 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);
void divesSelected(const QVector<dive *> &dives, dive *currentDive);
void filterReset();
public:

View file

@ -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<QModelIndex> &indices)
void MultiFilterSortModel::selectionChangedSlot(const QVector<QModelIndex> &indices, QModelIndex currentDive)
{
QVector<QModelIndex> indicesLocal;
indicesLocal.reserve(indices.size());
@ -42,15 +41,8 @@ void MultiFilterSortModel::selectionChangedSlot(const QVector<QModelIndex> &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

View file

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