Dive list: propagate current-item to frontend

The command-objects select a current item, but this selection
was not propagated to the front-end. The current item is the
base for keyboard-navigation through the dive-list and therefore
should be set correctly.

It took some experimentation to get the flags right:
 QItemSelectionModel::Current
Hopefully, these are the correct flags across all supported
Qt versions!

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-08-14 09:12:21 -04:00 committed by Dirk Hohndel
parent b16be29595
commit 3c6cdfd8c0
6 changed files with 74 additions and 5 deletions

View file

@ -423,7 +423,12 @@ void DiveListBase::restoreSelection(const std::vector<dive *> &selection, dive *
});
bool currentDiveChanged = false;
if (current_dive != currentDive) {
// If currentDive is null, we have no current dive. In such a case always
// signal the frontend.
if (!currentDive) {
currentDiveChanged = true;
emit diveListNotifier.currentDiveChanged();
} else if (current_dive != currentDive) {
currentDiveChanged = true;
// We cannot simply change the currentd dive to the given dive.

View file

@ -45,6 +45,7 @@ DiveListView::DiveListView(QWidget *parent) : QTreeView(parent), mouseClickSelec
setSelectionMode(ExtendedSelection);
header()->setContextMenuPolicy(Qt::ActionsContextMenu);
connect(DiveTripModel::instance(), &DiveTripModel::selectionChanged, this, &DiveListView::diveSelectionChanged);
connect(DiveTripModel::instance(), &DiveTripModel::newCurrentDive, this, &DiveListView::currentDiveChanged);
header()->setStretchLastSection(true);
@ -207,6 +208,21 @@ void DiveListView::diveSelectionChanged(const QVector<QModelIndex> &indexes, boo
}
}
void DiveListView::currentDiveChanged(QModelIndex index)
{
// Transform the index into a local index, since
// there might be sorting or filtering in effect.
MultiFilterSortModel *m = MultiFilterSortModel::instance();
QModelIndex localIndex = m->mapFromSource(index);
// Then, 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(localIndex, QItemSelectionModel::Current);
}
// If rows are added, check which of these rows is a trip and expand the first column
void DiveListView::rowsInserted(const QModelIndex &parent, int start, int end)
{

View file

@ -59,6 +59,7 @@ slots:
void loadImages();
void loadWebImages();
void diveSelectionChanged(const QVector<QModelIndex> &indexes, bool select);
void currentDiveChanged(QModelIndex index);
private:
bool mouseClickSelection;
QList<int> expandedRows;

View file

@ -530,10 +530,16 @@ void MainWindow::configureToolbar() {
void MainWindow::selectionChanged()
{
graphics()->plotDive(nullptr, false, true);
information()->updateDiveInfo();
configureToolbar();
MapWidget::instance()->reload();
if (!current_dive) {
information()->clearTabs();
information()->updateDiveInfo(true);
graphics()->setEmptyState();
} else {
graphics()->plotDive(nullptr, false, true);
information()->updateDiveInfo();
configureToolbar();
MapWidget::instance()->reload();
}
}
void MainWindow::on_actionNew_triggered()

View file

@ -442,6 +442,7 @@ DiveTripModel::DiveTripModel(QObject *parent) :
connect(&diveListNotifier, &DiveListNotifier::divesTimeChanged, this, &DiveTripModel::divesTimeChanged);
connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModel::divesSelected);
connect(&diveListNotifier, &DiveListNotifier::divesDeselected, this, &DiveTripModel::divesDeselected);
connect(&diveListNotifier, &DiveListNotifier::currentDiveChanged, this, &DiveTripModel::currentDiveChanged);
}
int DiveTripModel::columnCount(const QModelIndex&) const
@ -1166,3 +1167,41 @@ void DiveTripModel::changeDiveSelection(dive_trip *trip, const QVector<dive *> &
emit selectionChanged(indexes, select);
}
void DiveTripModel::currentDiveChanged()
{
// The current dive has changed. Transform the current dive into an index and pass it on to the view.
if (!current_dive) {
emit newCurrentDive(QModelIndex()); // No current dive -> tell view to clear current index with an invalid index
return;
}
dive_trip *trip = current_dive->divetrip;
if (!trip || currentLayout == LIST) {
// Either this is outside of a trip or we're in list mode.
int idx = findDiveIdx(current_dive);
if (idx < 0) {
// We don't know this dive. Something is wrong. Warn and bail.
qWarning() << "DiveTripModel::currentDiveChanged(): unknown top-level dive";
emit newCurrentDive(QModelIndex());
return;
}
emit newCurrentDive(createIndex(idx, 0, noParent));
} else {
int idx = findTripIdx(trip);
if (idx < 0) {
// We don't know the trip - this shouldn't happen. Warn and bail.
qWarning() << "DiveTripModel::currentDiveChanged(): unknown trip";
emit newCurrentDive(QModelIndex());
return;
}
int diveIdx = findDiveInTrip(idx, current_dive);
if (diveIdx < 0) {
// We don't know this dive. Something is wrong. Warn and bail.
qWarning() << "DiveTripModel::currentDiveChanged(): unknown top-level dive";
emit newCurrentDive(QModelIndex());
return;
}
emit newCurrentDive(createIndex(diveIdx, 0, idx));
}
}

View file

@ -118,6 +118,7 @@ signals:
// indexes into local indexes according to current sorting/filtering and instructs the QSelectionModel to
// perform the appropriate actions.
void selectionChanged(const QVector<QModelIndex> &indexes, bool select);
void newCurrentDive(QModelIndex index);
private slots:
void divesAdded(dive_trip *trip, bool addTrip, const QVector<dive *> &dives);
void divesDeleted(dive_trip *trip, bool deleteTrip, const QVector<dive *> &dives);
@ -126,6 +127,7 @@ private slots:
void divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &dives);
void divesSelected(dive_trip *trip, const QVector<dive *> &dives);
void divesDeselected(dive_trip *trip, const QVector<dive *> &dives);
void currentDiveChanged();
private:
// The model has up to two levels. At the top level, we have either trips or dives
// that do not belong to trips. Such a top-level item is represented by the "Item"