Cleanup: make "struct dive *" and "struct dive_trip *" Qt metatypes

Just as we did for pointer to struct dive_site, make pointers to
struct dive and struct dive_trip "Qt metatypes". This means that
they can be passed through QVariants without taking a detour via
void *.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-11-22 08:06:07 +01:00 committed by Dirk Hohndel
parent fba6ec5ad5
commit 9e565e3552
4 changed files with 33 additions and 29 deletions

View file

@ -758,10 +758,6 @@ extern void set_git_prefs(const char *prefs);
extern char *get_dive_date_c_string(timestamp_t when); extern char *get_dive_date_c_string(timestamp_t when);
extern void update_setpoint_events(const struct dive *dive, struct divecomputer *dc); extern void update_setpoint_events(const struct dive *dive, struct divecomputer *dc);
#ifdef __cplusplus
}
#endif
extern weight_t string_to_weight(const char *str); extern weight_t string_to_weight(const char *str);
extern depth_t string_to_depth(const char *str); extern depth_t string_to_depth(const char *str);
extern pressure_t string_to_pressure(const char *str); extern pressure_t string_to_pressure(const char *str);
@ -769,6 +765,16 @@ extern volume_t string_to_volume(const char *str, pressure_t workp);
extern fraction_t string_to_fraction(const char *str); extern fraction_t string_to_fraction(const char *str);
extern void average_max_depth(struct diveplan *dive, int *avg_depth, int *max_depth); extern void average_max_depth(struct diveplan *dive, int *avg_depth, int *max_depth);
#ifdef __cplusplus
}
/* Make pointers to dive and dive_trip "Qt metatypes" so that they can
* be passed through QVariants. */
Q_DECLARE_METATYPE(struct dive *);
Q_DECLARE_METATYPE(struct dive_trip *);
#endif
#include "pref.h" #include "pref.h"
#endif // DIVE_H #endif // DIVE_H

View file

@ -248,11 +248,11 @@ void DiveListView::rememberSelection()
Q_FOREACH (const QModelIndex &index, selection.indexes()) { Q_FOREACH (const QModelIndex &index, selection.indexes()) {
if (index.column() != 0) // We only care about the dives, so, let's stick to rows and discard columns. if (index.column() != 0) // We only care about the dives, so, let's stick to rows and discard columns.
continue; continue;
struct dive *d = (struct dive *)index.data(DiveTripModel::DIVE_ROLE).value<void *>(); struct dive *d = index.data(DiveTripModel::DIVE_ROLE).value<struct dive *>();
if (d) { if (d) {
selectedDives.insert(d->divetrip, get_divenr(d)); selectedDives.insert(d->divetrip, get_divenr(d));
} else { } else {
struct dive_trip *t = (struct dive_trip *)index.data(DiveTripModel::TRIP_ROLE).value<void *>(); struct dive_trip *t = index.data(DiveTripModel::TRIP_ROLE).value<dive_trip *>();
if (t) if (t)
selectedDives.insert(t, -1); selectedDives.insert(t, -1);
} }
@ -288,7 +288,7 @@ void DiveListView::selectTrip(dive_trip_t *trip)
return; return;
QAbstractItemModel *m = model(); QAbstractItemModel *m = model();
QModelIndexList match = m->match(m->index(0, 0), DiveTripModel::TRIP_ROLE, QVariant::fromValue<void *>(trip), 2, Qt::MatchRecursive); QModelIndexList match = m->match(m->index(0, 0), DiveTripModel::TRIP_ROLE, QVariant::fromValue(trip), 2, Qt::MatchRecursive);
QItemSelectionModel::SelectionFlags flags; QItemSelectionModel::SelectionFlags flags;
if (!match.count()) if (!match.count())
return; return;
@ -313,7 +313,7 @@ void DiveListView::clearTripSelection()
// we want to make sure no trips are selected // we want to make sure no trips are selected
Q_FOREACH (const QModelIndex &index, selectionModel()->selectedRows()) { Q_FOREACH (const QModelIndex &index, selectionModel()->selectedRows()) {
dive_trip_t *trip = static_cast<dive_trip_t *>(index.data(DiveTripModel::TRIP_ROLE).value<void *>()); dive_trip_t *trip = index.data(DiveTripModel::TRIP_ROLE).value<dive_trip *>();
if (!trip) if (!trip)
continue; continue;
selectionModel()->select(index, QItemSelectionModel::Deselect); selectionModel()->select(index, QItemSelectionModel::Deselect);
@ -342,7 +342,7 @@ QList<dive_trip_t *> DiveListView::selectedTrips()
{ {
QList<dive_trip_t *> ret; QList<dive_trip_t *> ret;
Q_FOREACH (const QModelIndex &index, selectionModel()->selectedRows()) { Q_FOREACH (const QModelIndex &index, selectionModel()->selectedRows()) {
dive_trip_t *trip = static_cast<dive_trip_t *>(index.data(DiveTripModel::TRIP_ROLE).value<void *>()); dive_trip_t *trip = index.data(DiveTripModel::TRIP_ROLE).value<dive_trip *>();
if (!trip) if (!trip)
continue; continue;
ret.push_back(trip); ret.push_back(trip);
@ -607,9 +607,9 @@ void DiveListView::selectionChanged(const QItemSelection &selected, const QItemS
if (index.column() != 0) if (index.column() != 0)
continue; continue;
const QAbstractItemModel *model = index.model(); const QAbstractItemModel *model = index.model();
struct dive *dive = (struct dive *)model->data(index, DiveTripModel::DIVE_ROLE).value<void *>(); struct dive *dive = model->data(index, DiveTripModel::DIVE_ROLE).value<struct dive *>();
if (!dive) // it's a trip! if (!dive) // it's a trip!
deselect_dives_in_trip((dive_trip_t *)model->data(index, DiveTripModel::TRIP_ROLE).value<void *>()); deselect_dives_in_trip(model->data(index, DiveTripModel::TRIP_ROLE).value<dive_trip *>());
else else
deselect_dive(dive); deselect_dive(dive);
} }
@ -618,11 +618,11 @@ void DiveListView::selectionChanged(const QItemSelection &selected, const QItemS
continue; continue;
const QAbstractItemModel *model = index.model(); const QAbstractItemModel *model = index.model();
struct dive *dive = (struct dive *)model->data(index, DiveTripModel::DIVE_ROLE).value<void *>(); struct dive *dive = model->data(index, DiveTripModel::DIVE_ROLE).value<struct dive *>();
if (!dive) { // it's a trip! if (!dive) { // it's a trip!
if (model->rowCount(index)) { if (model->rowCount(index)) {
QItemSelection selection; QItemSelection selection;
select_dives_in_trip((dive_trip_t *)model->data(index, DiveTripModel::TRIP_ROLE).value<void *>()); select_dives_in_trip(model->data(index, DiveTripModel::TRIP_ROLE).value<dive_trip *>());
selection.select(index.child(0, 0), index.child(model->rowCount(index) - 1, 0)); selection.select(index.child(0, 0), index.child(model->rowCount(index) - 1, 0));
selectionModel()->select(selection, QItemSelectionModel::Select | QItemSelectionModel::Rows); selectionModel()->select(selection, QItemSelectionModel::Select | QItemSelectionModel::Rows);
selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select | QItemSelectionModel::NoUpdate); selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select | QItemSelectionModel::NoUpdate);
@ -724,8 +724,8 @@ void DiveListView::merge_trip(const QModelIndex &a, int offset)
int i = a.row() + offset; int i = a.row() + offset;
QModelIndex b = a.sibling(i, 0); QModelIndex b = a.sibling(i, 0);
dive_trip_t *trip_a = (dive_trip_t *)a.data(DiveTripModel::TRIP_ROLE).value<void *>(); dive_trip_t *trip_a = a.data(DiveTripModel::TRIP_ROLE).value<dive_trip *>();
dive_trip_t *trip_b = (dive_trip_t *)b.data(DiveTripModel::TRIP_ROLE).value<void *>(); dive_trip_t *trip_b = b.data(DiveTripModel::TRIP_ROLE).value<dive_trip *>();
if (trip_a == trip_b || !trip_a || !trip_b) if (trip_a == trip_b || !trip_a || !trip_b)
return; return;
Command::mergeTrips(trip_a, trip_b); Command::mergeTrips(trip_a, trip_b);
@ -756,7 +756,7 @@ void DiveListView::removeFromTrip()
void DiveListView::newTripAbove() void DiveListView::newTripAbove()
{ {
struct dive *d = (struct dive *)contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void *>(); struct dive *d = contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<struct dive *>();
if (!d) // shouldn't happen as we only are setting up this action if this is a dive if (!d) // shouldn't happen as we only are setting up this action if this is a dive
return; return;
//TODO: port to c-code. //TODO: port to c-code.
@ -782,7 +782,7 @@ void DiveListView::addToTripAbove()
void DiveListView::addToTrip(int delta) void DiveListView::addToTrip(int delta)
{ {
// d points to the row that has (mouse-)pointer focus, and there are nr rows selected // d points to the row that has (mouse-)pointer focus, and there are nr rows selected
struct dive *d = (struct dive *)contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void *>(); struct dive *d = contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<struct dive *>();
int nr = selectionModel()->selectedRows().count(); int nr = selectionModel()->selectedRows().count();
QModelIndex t; QModelIndex t;
dive_trip_t *trip = NULL; dive_trip_t *trip = NULL;
@ -791,7 +791,7 @@ void DiveListView::addToTrip(int delta)
// check if its sibling is a trip. // check if its sibling is a trip.
for (int i = 1; i <= nr; i++) { for (int i = 1; i <= nr; i++) {
t = contextMenuIndex.sibling(contextMenuIndex.row() + (delta > 0 ? i: i * -1), 0); t = contextMenuIndex.sibling(contextMenuIndex.row() + (delta > 0 ? i: i * -1), 0);
trip = (dive_trip_t *)t.data(DiveTripModel::TRIP_ROLE).value<void *>(); trip = t.data(DiveTripModel::TRIP_ROLE).value<dive_trip *>();
if (trip) if (trip)
break; break;
} }
@ -814,7 +814,7 @@ void DiveListView::addToTrip(int delta)
void DiveListView::markDiveInvalid() void DiveListView::markDiveInvalid()
{ {
int i; int i;
struct dive *d = (struct dive *)contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void *>(); struct dive *d = contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<struct dive *>();
if (!d) if (!d)
return; return;
for_each_dive (i, d) { for_each_dive (i, d) {
@ -833,7 +833,7 @@ void DiveListView::markDiveInvalid()
void DiveListView::deleteDive() void DiveListView::deleteDive()
{ {
struct dive *d = (struct dive *)contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void *>(); struct dive *d = contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<struct dive *>();
if (!d) if (!d)
return; return;
@ -851,8 +851,8 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event)
QAction *collapseAction = NULL; QAction *collapseAction = NULL;
// let's remember where we are // let's remember where we are
contextMenuIndex = indexAt(event->pos()); contextMenuIndex = indexAt(event->pos());
struct dive *d = (struct dive *)contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<void *>(); struct dive *d = contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value<struct dive *>();
dive_trip_t *trip = (dive_trip_t *)contextMenuIndex.data(DiveTripModel::TRIP_ROLE).value<void *>(); dive_trip_t *trip = contextMenuIndex.data(DiveTripModel::TRIP_ROLE).value<dive_trip *>();
QMenu popup(this); QMenu popup(this);
if (currentLayout == DiveTripModel::TREE) { if (currentLayout == DiveTripModel::TREE) {
// verify if there is a node that`s not expanded. // verify if there is a node that`s not expanded.
@ -861,7 +861,7 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event)
uint expanded_nodes = 0; uint expanded_nodes = 0;
for(int i = 0, end = model()->rowCount(); i < end; i++) { for(int i = 0, end = model()->rowCount(); i < end; i++) {
QModelIndex idx = model()->index(i, 0); QModelIndex idx = model()->index(i, 0);
if (idx.data(DiveTripModel::DIVE_ROLE).value<void *>()) if (idx.data(DiveTripModel::DIVE_ROLE).value<struct dive *>())
continue; continue;
if (!isExpanded(idx)) { if (!isExpanded(idx)) {

View file

@ -51,7 +51,7 @@ QVariant DiveTripModel::tripData(const dive_trip *trip, int column, int role)
bool oneDayTrip=true; bool oneDayTrip=true;
if (role == TRIP_ROLE) if (role == TRIP_ROLE)
return QVariant::fromValue<void *>((void *)trip); // Not nice: casting away a const return QVariant::fromValue(const_cast<dive_trip *>(trip)); // Not nice: casting away a const
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole) {
switch (column) { switch (column) {
@ -248,7 +248,7 @@ QVariant DiveTripModel::diveData(const struct dive *d, int column, int role)
case STAR_ROLE: case STAR_ROLE:
return d->rating; return d->rating;
case DIVE_ROLE: case DIVE_ROLE:
return QVariant::fromValue<void *>((void *)d); // Not nice: casting away a const return QVariant::fromValue(const_cast<dive *>(d)); // Not nice: casting away a const
case DIVE_IDX: case DIVE_IDX:
return get_divenr(d); return get_divenr(d);
case SELECTED_ROLE: case SELECTED_ROLE:

View file

@ -619,16 +619,14 @@ bool MultiFilterSortModel::showDive(const struct dive *d) const
bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{ {
QModelIndex index0 = sourceModel()->index(source_row, 0, source_parent); QModelIndex index0 = sourceModel()->index(source_row, 0, source_parent);
QVariant diveVariant = sourceModel()->data(index0, DiveTripModel::DIVE_ROLE); struct dive *d = sourceModel()->data(index0, DiveTripModel::DIVE_ROLE).value<struct dive *>();
struct dive *d = (struct dive *)diveVariant.value<void *>();
// For dives, simply check the hidden_by_filter flag // For dives, simply check the hidden_by_filter flag
if (d) if (d)
return !d->hidden_by_filter; return !d->hidden_by_filter;
// Since this is not a dive, it must be a trip // Since this is not a dive, it must be a trip
QVariant tripVariant = sourceModel()->data(index0, DiveTripModel::TRIP_ROLE); dive_trip *trip = sourceModel()->data(index0, DiveTripModel::TRIP_ROLE).value<dive_trip *>();
dive_trip *trip = (dive_trip *)tripVariant.value<void *>();
if (!trip) if (!trip)
return false; // Oops. Neither dive nor trip, something is seriously wrong. return false; // Oops. Neither dive nor trip, something is seriously wrong.