diff --git a/core/dive.h b/core/dive.h index 8b98d3b49..eff04ba23 100644 --- a/core/dive.h +++ b/core/dive.h @@ -758,10 +758,6 @@ extern void set_git_prefs(const char *prefs); extern char *get_dive_date_c_string(timestamp_t when); 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 depth_t string_to_depth(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 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" #endif // DIVE_H diff --git a/desktop-widgets/divelistview.cpp b/desktop-widgets/divelistview.cpp index c75460c64..9167bea5e 100644 --- a/desktop-widgets/divelistview.cpp +++ b/desktop-widgets/divelistview.cpp @@ -248,11 +248,11 @@ void DiveListView::rememberSelection() 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. continue; - struct dive *d = (struct dive *)index.data(DiveTripModel::DIVE_ROLE).value(); + struct dive *d = index.data(DiveTripModel::DIVE_ROLE).value(); if (d) { selectedDives.insert(d->divetrip, get_divenr(d)); } else { - struct dive_trip *t = (struct dive_trip *)index.data(DiveTripModel::TRIP_ROLE).value(); + struct dive_trip *t = index.data(DiveTripModel::TRIP_ROLE).value(); if (t) selectedDives.insert(t, -1); } @@ -288,7 +288,7 @@ void DiveListView::selectTrip(dive_trip_t *trip) return; QAbstractItemModel *m = model(); - QModelIndexList match = m->match(m->index(0, 0), DiveTripModel::TRIP_ROLE, QVariant::fromValue(trip), 2, Qt::MatchRecursive); + QModelIndexList match = m->match(m->index(0, 0), DiveTripModel::TRIP_ROLE, QVariant::fromValue(trip), 2, Qt::MatchRecursive); QItemSelectionModel::SelectionFlags flags; if (!match.count()) return; @@ -313,7 +313,7 @@ void DiveListView::clearTripSelection() // we want to make sure no trips are selected Q_FOREACH (const QModelIndex &index, selectionModel()->selectedRows()) { - dive_trip_t *trip = static_cast(index.data(DiveTripModel::TRIP_ROLE).value()); + dive_trip_t *trip = index.data(DiveTripModel::TRIP_ROLE).value(); if (!trip) continue; selectionModel()->select(index, QItemSelectionModel::Deselect); @@ -342,7 +342,7 @@ QList DiveListView::selectedTrips() { QList ret; Q_FOREACH (const QModelIndex &index, selectionModel()->selectedRows()) { - dive_trip_t *trip = static_cast(index.data(DiveTripModel::TRIP_ROLE).value()); + dive_trip_t *trip = index.data(DiveTripModel::TRIP_ROLE).value(); if (!trip) continue; ret.push_back(trip); @@ -607,9 +607,9 @@ void DiveListView::selectionChanged(const QItemSelection &selected, const QItemS if (index.column() != 0) continue; const QAbstractItemModel *model = index.model(); - struct dive *dive = (struct dive *)model->data(index, DiveTripModel::DIVE_ROLE).value(); + struct dive *dive = model->data(index, DiveTripModel::DIVE_ROLE).value(); if (!dive) // it's a trip! - deselect_dives_in_trip((dive_trip_t *)model->data(index, DiveTripModel::TRIP_ROLE).value()); + deselect_dives_in_trip(model->data(index, DiveTripModel::TRIP_ROLE).value()); else deselect_dive(dive); } @@ -618,11 +618,11 @@ void DiveListView::selectionChanged(const QItemSelection &selected, const QItemS continue; const QAbstractItemModel *model = index.model(); - struct dive *dive = (struct dive *)model->data(index, DiveTripModel::DIVE_ROLE).value(); + struct dive *dive = model->data(index, DiveTripModel::DIVE_ROLE).value(); if (!dive) { // it's a trip! if (model->rowCount(index)) { QItemSelection selection; - select_dives_in_trip((dive_trip_t *)model->data(index, DiveTripModel::TRIP_ROLE).value()); + select_dives_in_trip(model->data(index, DiveTripModel::TRIP_ROLE).value()); selection.select(index.child(0, 0), index.child(model->rowCount(index) - 1, 0)); selectionModel()->select(selection, QItemSelectionModel::Select | QItemSelectionModel::Rows); 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; QModelIndex b = a.sibling(i, 0); - dive_trip_t *trip_a = (dive_trip_t *)a.data(DiveTripModel::TRIP_ROLE).value(); - dive_trip_t *trip_b = (dive_trip_t *)b.data(DiveTripModel::TRIP_ROLE).value(); + dive_trip_t *trip_a = a.data(DiveTripModel::TRIP_ROLE).value(); + dive_trip_t *trip_b = b.data(DiveTripModel::TRIP_ROLE).value(); if (trip_a == trip_b || !trip_a || !trip_b) return; Command::mergeTrips(trip_a, trip_b); @@ -756,7 +756,7 @@ void DiveListView::removeFromTrip() void DiveListView::newTripAbove() { - struct dive *d = (struct dive *)contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value(); + struct dive *d = contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value(); if (!d) // shouldn't happen as we only are setting up this action if this is a dive return; //TODO: port to c-code. @@ -782,7 +782,7 @@ void DiveListView::addToTripAbove() void DiveListView::addToTrip(int delta) { // 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(); + struct dive *d = contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value(); int nr = selectionModel()->selectedRows().count(); QModelIndex t; dive_trip_t *trip = NULL; @@ -791,7 +791,7 @@ void DiveListView::addToTrip(int delta) // check if its sibling is a trip. for (int i = 1; i <= nr; i++) { t = contextMenuIndex.sibling(contextMenuIndex.row() + (delta > 0 ? i: i * -1), 0); - trip = (dive_trip_t *)t.data(DiveTripModel::TRIP_ROLE).value(); + trip = t.data(DiveTripModel::TRIP_ROLE).value(); if (trip) break; } @@ -814,7 +814,7 @@ void DiveListView::addToTrip(int delta) void DiveListView::markDiveInvalid() { int i; - struct dive *d = (struct dive *)contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value(); + struct dive *d = contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value(); if (!d) return; for_each_dive (i, d) { @@ -833,7 +833,7 @@ void DiveListView::markDiveInvalid() void DiveListView::deleteDive() { - struct dive *d = (struct dive *)contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value(); + struct dive *d = contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value(); if (!d) return; @@ -851,8 +851,8 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event) QAction *collapseAction = NULL; // let's remember where we are contextMenuIndex = indexAt(event->pos()); - struct dive *d = (struct dive *)contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value(); - dive_trip_t *trip = (dive_trip_t *)contextMenuIndex.data(DiveTripModel::TRIP_ROLE).value(); + struct dive *d = contextMenuIndex.data(DiveTripModel::DIVE_ROLE).value(); + dive_trip_t *trip = contextMenuIndex.data(DiveTripModel::TRIP_ROLE).value(); QMenu popup(this); if (currentLayout == DiveTripModel::TREE) { // verify if there is a node that`s not expanded. @@ -861,7 +861,7 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event) uint expanded_nodes = 0; for(int i = 0, end = model()->rowCount(); i < end; i++) { QModelIndex idx = model()->index(i, 0); - if (idx.data(DiveTripModel::DIVE_ROLE).value()) + if (idx.data(DiveTripModel::DIVE_ROLE).value()) continue; if (!isExpanded(idx)) { diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index c2d9e62a9..51284c41d 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -51,7 +51,7 @@ QVariant DiveTripModel::tripData(const dive_trip *trip, int column, int role) bool oneDayTrip=true; if (role == TRIP_ROLE) - return QVariant::fromValue((void *)trip); // Not nice: casting away a const + return QVariant::fromValue(const_cast(trip)); // Not nice: casting away a const if (role == Qt::DisplayRole) { switch (column) { @@ -248,7 +248,7 @@ QVariant DiveTripModel::diveData(const struct dive *d, int column, int role) case STAR_ROLE: return d->rating; case DIVE_ROLE: - return QVariant::fromValue((void *)d); // Not nice: casting away a const + return QVariant::fromValue(const_cast(d)); // Not nice: casting away a const case DIVE_IDX: return get_divenr(d); case SELECTED_ROLE: diff --git a/qt-models/filtermodels.cpp b/qt-models/filtermodels.cpp index d485684f3..4063ba149 100644 --- a/qt-models/filtermodels.cpp +++ b/qt-models/filtermodels.cpp @@ -619,16 +619,14 @@ bool MultiFilterSortModel::showDive(const struct dive *d) const bool MultiFilterSortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { QModelIndex index0 = sourceModel()->index(source_row, 0, source_parent); - QVariant diveVariant = sourceModel()->data(index0, DiveTripModel::DIVE_ROLE); - struct dive *d = (struct dive *)diveVariant.value(); + struct dive *d = sourceModel()->data(index0, DiveTripModel::DIVE_ROLE).value(); // For dives, simply check the hidden_by_filter flag if (d) return !d->hidden_by_filter; // Since this is not a dive, it must be a trip - QVariant tripVariant = sourceModel()->data(index0, DiveTripModel::TRIP_ROLE); - dive_trip *trip = (dive_trip *)tripVariant.value(); + dive_trip *trip = sourceModel()->data(index0, DiveTripModel::TRIP_ROLE).value(); if (!trip) return false; // Oops. Neither dive nor trip, something is seriously wrong.