Dive list: remember selected dives

Don't delesect dives, when unregistering them from the backend.
If a previously selected dive is added, select it in the dive-list.
For this purpose introduce a SELECTED_ROLE to query the DiveTripModel
for selected dives.

Unfortunately, when adding multiple selected dives, current_dive_changed
is called for each of them, making this very slow. This will have
to be fixed in subsequent commits.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-08-01 10:47:09 +02:00 committed by Dirk Hohndel
parent 96d8727399
commit 0d98da5261
4 changed files with 29 additions and 12 deletions

View file

@ -1065,9 +1065,9 @@ struct dive *unregister_dive(int idx)
if (!dive) if (!dive)
return NULL; /* this should never happen */ return NULL; /* this should never happen */
remove_dive_from_trip(dive, false); remove_dive_from_trip(dive, false);
if (dive->selected)
deselect_dive(idx);
unregister_dive_from_table(&dive_table, idx); unregister_dive_from_table(&dive_table, idx);
if (dive->selected)
amount_selected--;
return dive; return dive;
} }
@ -1075,7 +1075,12 @@ struct dive *unregister_dive(int idx)
* but doesn't deal with updating dive trips, etc */ * but doesn't deal with updating dive trips, etc */
void delete_single_dive(int idx) void delete_single_dive(int idx)
{ {
struct dive *dive = unregister_dive(idx); struct dive *dive = get_dive(idx);
if (!dive)
return; /* this should never happen */
if (dive->selected)
deselect_dive(idx);
dive = unregister_dive(idx);
free_dive(dive); free_dive(dive);
} }

View file

@ -181,10 +181,19 @@ void DiveListView::rowsInserted(const QModelIndex &parent, int start, int end)
// First, let the QTreeView do its thing. // First, let the QTreeView do its thing.
QTreeView::rowsInserted(parent, start, end); QTreeView::rowsInserted(parent, start, end);
QAbstractItemModel *m = model();
QItemSelectionModel *s = selectionModel();
// Check whether any of the items is selected
for (int i = start; i <= end; ++i) {
QModelIndex index = m->index(i, 0, parent);
if (m->data(index, DiveTripModel::SELECTED_ROLE).toBool())
s->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
}
// Now check for each inserted row whether this is a trip and expand the first column // Now check for each inserted row whether this is a trip and expand the first column
if (parent.isValid()) // Trips don't have a parent if (parent.isValid()) // Trips don't have a parent
return; return;
QAbstractItemModel *m = model();
for (int i = start; i <= end; ++i) { for (int i = start; i <= end; ++i) {
if (m->rowCount(m->index(i, 0)) != 0) if (m->rowCount(m->index(i, 0)) != 0)
setFirstColumnSpanned(i, QModelIndex(), true); setFirstColumnSpanned(i, QModelIndex(), true);

View file

@ -297,16 +297,18 @@ QVariant DiveItem::data(int column, int role) const
break; break;
} }
break; break;
} case DiveTripModel::STAR_ROLE:
if (role == DiveTripModel::STAR_ROLE) {
retVal = d->rating; retVal = d->rating;
} break;
if (role == DiveTripModel::DIVE_ROLE) { case DiveTripModel::DIVE_ROLE:
retVal = QVariant::fromValue<void *>(d); retVal = QVariant::fromValue<void *>(d);
} break;
if (role == DiveTripModel::DIVE_IDX) { case DiveTripModel::DIVE_IDX:
retVal = get_divenr(d); retVal = get_divenr(d);
break;
case DiveTripModel::SELECTED_ROLE:
retVal = d->selected;
break;
} }
return retVal; return retVal;
} }

View file

@ -88,7 +88,8 @@ public:
DIVE_ROLE, DIVE_ROLE,
TRIP_ROLE, TRIP_ROLE,
SORT_ROLE, SORT_ROLE,
DIVE_IDX DIVE_IDX,
SELECTED_ROLE
}; };
enum Layout { enum Layout {
TREE, TREE,