From 920eb7576ff3c5fab19c12b7b291042817422ac5 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 28 Oct 2018 21:16:42 +0100 Subject: Dive site: pass pointer-to-dive_site via QVariant There was this ugly pattern of passing pointers-to-dive_site via a QVariant of void * type. This is of course inherently unsafe. Pass these pointers using their proper types instead. This makes it necessary to register them in Qt's meta-type system. Doing so, fixes a bug: QML couldn't call into updateDiveSiteCoordinates() because it didn't know the type and thus the coordinates of the moved flag were not reflected in the divesite-dialog. Signed-off-by: Berthold Stoeger --- core/divesite.h | 3 +++ desktop-widgets/locationinformation.cpp | 4 ++-- desktop-widgets/mapwidget.cpp | 2 +- desktop-widgets/modeldelegates.cpp | 4 ++-- qt-models/divelocationmodel.cpp | 6 +++--- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/core/divesite.h b/core/divesite.h index 895412a4b..3ce87128a 100644 --- a/core/divesite.h +++ b/core/divesite.h @@ -78,6 +78,9 @@ void merge_dive_sites(struct dive_site *ref, struct dive_site *dive_sites[], int } QString constructLocationTags(struct taxonomy_data *taxonomy, bool for_maintab); +/* Make pointer-to-dive_site a "Qt metatype" so that we can pass it through QVariants */ +Q_DECLARE_METATYPE(dive_site *); + #endif #endif // DIVESITE_H diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp index 2b57c122c..69ecbc9b2 100644 --- a/desktop-widgets/locationinformation.cpp +++ b/desktop-widgets/locationinformation.cpp @@ -82,7 +82,7 @@ void LocationInformationWidget::mergeSelectedDiveSites() std::vector selected_dive_sites; selected_dive_sites.reserve(selection.count()); Q_FOREACH (const QModelIndex &idx, selection) { - struct dive_site *ds = (struct dive_site *)idx.data(LocationInformationModel::DIVESITE_ROLE).value(); + dive_site *ds = idx.data(LocationInformationModel::DIVESITE_ROLE).value(); if (ds) selected_dive_sites.push_back(ds); } @@ -507,7 +507,7 @@ void DiveLocationLineEdit::itemActivated(const QModelIndex &index) if (index.column() == LocationInformationModel::DIVESITE) idx = index.model()->index(index.row(), LocationInformationModel::NAME); - dive_site *ds = (dive_site *)index.model()->index(index.row(), LocationInformationModel::DIVESITE).data().value(); + dive_site *ds = index.model()->index(index.row(), LocationInformationModel::DIVESITE).data().value(); currType = ds == RECENTLY_ADDED_DIVESITE ? NEW_DIVE_SITE : EXISTING_DIVE_SITE; currDs = ds; setText(idx.data().toString()); diff --git a/desktop-widgets/mapwidget.cpp b/desktop-widgets/mapwidget.cpp index 9d3735efb..ac90c577d 100644 --- a/desktop-widgets/mapwidget.cpp +++ b/desktop-widgets/mapwidget.cpp @@ -67,7 +67,7 @@ void MapWidget::centerOnDiveSite(struct dive_site *ds) void MapWidget::centerOnIndex(const QModelIndex& idx) { CHECK_IS_READY_RETURN_VOID(); - struct dive_site *ds = (struct dive_site *)idx.model()->index(idx.row(), LocationInformationModel::DIVESITE).data().value(); + dive_site *ds = idx.model()->index(idx.row(), LocationInformationModel::DIVESITE).data().value(); if (!ds || ds == RECENTLY_ADDED_DIVESITE || !dive_site_has_gps_location(ds)) centerOnSelectedDiveSite(); else diff --git a/desktop-widgets/modeldelegates.cpp b/desktop-widgets/modeldelegates.cpp index 7104af33c..f008f4bc2 100644 --- a/desktop-widgets/modeldelegates.cpp +++ b/desktop-widgets/modeldelegates.cpp @@ -453,8 +453,8 @@ void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem QString diveSiteName = index.data().toString(); QString bottomText; QIcon icon = index.data(Qt::DecorationRole).value(); - struct dive_site *ds = (struct dive_site *) - index.model()->data(index.model()->index(index.row(), LocationInformationModel::DIVESITE)).value(); + struct dive_site *ds = + index.model()->data(index.model()->index(index.row(), LocationInformationModel::DIVESITE)).value(); struct dive_site *currentDiveSite = current_dive ? get_dive_site_for_dive(current_dive) : nullptr; bool currentDiveSiteHasGPS = currentDiveSite && dive_site_has_gps_location(currentDiveSite); //Special case: do not show name, but instead, show diff --git a/qt-models/divelocationmodel.cpp b/qt-models/divelocationmodel.cpp index 18be89806..b84604bb5 100644 --- a/qt-models/divelocationmodel.cpp +++ b/qt-models/divelocationmodel.cpp @@ -41,7 +41,7 @@ QVariant LocationInformationModel::getDiveSiteData(const struct dive_site *ds, i case Qt::EditRole: case Qt::DisplayRole : switch(column) { - case DIVESITE: return QVariant::fromValue((void *)ds); // Not nice: casting away const + case DIVESITE: return QVariant::fromValue((dive_site *)ds); // Not nice: casting away const case NAME: return ds->name; case LATITUDE: return ds->location.lat.udeg; case LONGITUDE: return ds->location.lon.udeg; @@ -60,7 +60,7 @@ QVariant LocationInformationModel::getDiveSiteData(const struct dive_site *ds, i return QVariant(); } case DIVESITE_ROLE: - return QVariant::fromValue((void *)ds); // Not nice: casting away const + return QVariant::fromValue((dive_site *)ds); // Not nice: casting away const } return QVariant(); } @@ -119,7 +119,7 @@ GeoReferencingOptionsModel::GeoReferencingOptionsModel(QObject *parent) : QStrin bool GPSLocationInformationModel::filterAcceptsRow(int sourceRow, const QModelIndex &parent) const { - struct dive_site *ds = (struct dive_site *)sourceModel()->index(sourceRow, LocationInformationModel::DIVESITE, parent).data().value(); + struct dive_site *ds = sourceModel()->index(sourceRow, LocationInformationModel::DIVESITE, parent).data().value(); if (ds == ignoreDs || ds == RECENTLY_ADDED_DIVESITE) return false; -- cgit v1.2.3-70-g09d2