From a180af38797f23b975bc092cf75e9703c4c93921 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sun, 12 May 2013 06:38:47 -0700 Subject: [PATCH] Modify selection changed slot to deal with trips correctly If a user clicks on a trip, all the dives in a trip should be selected. But if a user selects a range of dives that happens to have a trip header in it, then only the range of dives should be selected (the trip header is marked as 'selected' for visual consistency, even though not all dives in this trip are selected). This also changes the code to scrollTo the first selected dive instead of just expanding the parent. This seems to give us a more pleasant visual appearance (trying to keep the selected dive centered in the dive list) and as a side effect no longer hides the first dive trip at program start (before this change the first dive in the first trip would be the top entry in the dive list, with its trip just out of sight above). Signed-off-by: Dirk Hohndel --- qt-ui/mainwindow.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 863e35088..91de38a3f 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -87,19 +87,81 @@ void MainWindow::on_actionOpen_triggered() void MainWindow::dive_selection_changed(const QItemSelection& newSelection, const QItemSelection& oldSelection) { + int cnt, i; /* first deselect the dives that are no longer selected */ - Q_FOREACH(const QModelIndex& desselect, oldSelection.indexes()) { - struct dive *d = (struct dive*) desselect.data(TreeItemDT::DIVE_ROLE).value(); + Q_FOREACH(const QModelIndex& deselect, oldSelection.indexes()) { + struct dive *d = (struct dive*) deselect.data(TreeItemDT::DIVE_ROLE).value(); + if (!d) { + // this is a trip - if just the trip is deselected but not its children, + // then we manually need to deselect its children + const QAbstractItemModel *model = deselect.model(); + cnt = model->rowCount(deselect); + if (cnt == 0) + continue; + for (i = 0; i < cnt; i++) { + QModelIndex child = deselect.child(i,0); + if (oldSelection.contains(child)) + break; + } + // if none of the dives were in the deselect list (so the user only ctrl-clicked + // on the trip header) then manually deselect all the dives + if (i == model->rowCount(deselect)) { + QItemSelectionModel::SelectionFlags flags = (QItemSelectionModel::Deselect | + QItemSelectionModel::Rows); + QItemSelection removedDives = QItemSelection(); + removedDives.select(deselect.child(0,0), deselect.child(i - 1,0)); + ui->ListWidget->selectionModel()->select(removedDives,flags); + } + } if (!d || !d->selected) continue; deselect_dive(get_divenr(d)); } /* then select the newly selected dives */ + bool needToScroll = TRUE; Q_FOREACH(const QModelIndex& select, newSelection.indexes()) { struct dive *d = (struct dive*) select.data(TreeItemDT::DIVE_ROLE).value(); + if (!d) { + // this is a trip + const QAbstractItemModel *model = select.model(); + cnt = model->rowCount(select); + if (cnt == 0) + continue; + for (i = 0; i < cnt; i++) { + QModelIndex child = select.child(i,0); + if (newSelection.contains(child)) + break; + } + // if just the trip header was clicked and none of its children, + // select all of them + if (i == model->rowCount(select)) { + if (needToScroll) { + // make sure the trip header is visible + needToScroll = FALSE; + ui->ListWidget->scrollTo(select, QAbstractItemView::PositionAtCenter); + } + QItemSelectionModel::SelectionFlags flags = (QItemSelectionModel::Select | + QItemSelectionModel::Rows); + QItemSelection addedDives = QItemSelection(); + addedDives.select(select.child(0,0), select.child(i - 1,0)); + ui->ListWidget->selectionModel()->select(addedDives,flags); + } + } if (!d || d->selected) continue; select_dive(get_divenr(d)); + if (needToScroll) { + // make sure at least one of them is visible in the list + // and if this is the first dive of a trip, make the trip visible, too + needToScroll = FALSE; + if (select.row() == 0 && d->divetrip && select.parent().isValid()) + ui->ListWidget->scrollTo(select.parent(), QAbstractItemView::PositionAtCenter); + else + ui->ListWidget->scrollTo(select, QAbstractItemView::PositionAtCenter); + } else { + // but all selected dives should be in expanded trips + ui->ListWidget->expand(select.parent()); + } } redrawProfile(); ui->InfoWidget->updateDiveInfo(selected_dive); @@ -352,7 +414,7 @@ void MainWindow::readSettings() ui->ListWidget->resizeColumnToContents(i); } ui->ListWidget->collapseAll(); - ui->ListWidget->expand(sortModel->index(0,0)); + ui->ListWidget->scrollTo(sortModel->index(0,0), QAbstractItemView::PositionAtCenter); settings.endGroup(); settings.beginGroup("Units"); GET_UNIT(v, "feet", length, units::METERS, units::FEET);