diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-10-26 17:03:54 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-10-29 00:09:31 +0000 |
commit | 724055f0af4fb7cdb9f1570967fe4b34797f3419 (patch) | |
tree | 6aab8c34e7d0a6df1d6fe84bc5794e8eb5404b7d /qt-models | |
parent | acd44467c1100a1a774cc644921b1dc33dca1266 (diff) | |
download | subsurface-724055f0af4fb7cdb9f1570967fe4b34797f3419.tar.gz |
Dive site: replace dive->dive_site_uuid by dive_site
Replace the UUID reference of struct dive by a pointer to dive_site.
This commit is rather large in lines, but nevertheless quite simple
since most of the UUID->pointer work was done in previous commits.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models')
-rw-r--r-- | qt-models/filtermodels.cpp | 2 | ||||
-rw-r--r-- | qt-models/maplocationmodel.cpp | 67 | ||||
-rw-r--r-- | qt-models/maplocationmodel.h | 28 |
3 files changed, 59 insertions, 38 deletions
diff --git a/qt-models/filtermodels.cpp b/qt-models/filtermodels.cpp index 9e9c6de60..1d6830ee0 100644 --- a/qt-models/filtermodels.cpp +++ b/qt-models/filtermodels.cpp @@ -589,7 +589,7 @@ void MultiFilterSortModel::divesDeleted(const QVector<dive *> &dives) bool MultiFilterSortModel::showDive(const struct dive *d) const { if (curr_dive_site) { - dive_site *ds = get_dive_site_by_uuid(d->dive_site_uuid); + dive_site *ds = d->dive_site; if (!ds) return false; return same_string(ds->name, curr_dive_site->name) || ds->uuid == curr_dive_site->uuid; diff --git a/qt-models/maplocationmodel.cpp b/qt-models/maplocationmodel.cpp index 45759a57f..ab34a7b16 100644 --- a/qt-models/maplocationmodel.cpp +++ b/qt-models/maplocationmodel.cpp @@ -1,26 +1,31 @@ // SPDX-License-Identifier: GPL-2.0 #include <QDebug> #include "maplocationmodel.h" +#include "core/divesite.h" const char *MapLocation::PROPERTY_NAME_COORDINATE = "coordinate"; -const char *MapLocation::PROPERTY_NAME_UUID = "uuid"; +const char *MapLocation::PROPERTY_NAME_DIVESITE = "divesite"; const char *MapLocation::PROPERTY_NAME_NAME = "name"; -MapLocation::MapLocation() +MapLocation::MapLocation() : m_ds(nullptr) { - m_uuid = 0; } -MapLocation::MapLocation(quint32 uuid, QGeoCoordinate coord, QString name) : - m_uuid(uuid), m_coordinate(coord), m_name(name) +MapLocation::MapLocation(struct dive_site *ds, QGeoCoordinate coord, QString name) : + m_ds(ds), m_coordinate(coord), m_name(name) { } QVariant MapLocation::getRole(int role) const { switch (role) { - case Roles::RoleUuid: - return QVariant::fromValue(m_uuid); + case Roles::RoleDivesite: + // To pass the dive site as an opaque object to QML, we convert it to uintptr_t. + // This type is guaranteed to hold a full pointer, therefore false equivalence + // owing to truncation can happen. The more logical type would of course be void *, + // but in tests all QVariant<void *> compared equal. It is unclear whether this is + // a bug in a certain version of QML or QML is inredibly broken by design. + return QVariant::fromValue((uintptr_t)m_ds); case Roles::RoleCoordinate: return QVariant::fromValue(m_coordinate); case Roles::RoleName: @@ -46,17 +51,23 @@ void MapLocation::setCoordinateNoEmit(QGeoCoordinate coord) m_coordinate = coord; } -quint32 MapLocation::uuid() +struct dive_site *MapLocation::divesite() { - return m_uuid; + return m_ds; } -MapLocationModel::MapLocationModel(QObject *parent) : QAbstractListModel(parent) +QVariant MapLocation::divesiteVariant() { - m_roles[MapLocation::Roles::RoleUuid] = MapLocation::PROPERTY_NAME_UUID; + // See comment on uintptr_t above + return QVariant::fromValue((uintptr_t)m_ds); +} + +MapLocationModel::MapLocationModel(QObject *parent) : QAbstractListModel(parent), + m_selectedDs(nullptr) +{ + m_roles[MapLocation::Roles::RoleDivesite] = MapLocation::PROPERTY_NAME_DIVESITE; m_roles[MapLocation::Roles::RoleCoordinate] = MapLocation::PROPERTY_NAME_COORDINATE; m_roles[MapLocation::Roles::RoleName] = MapLocation::PROPERTY_NAME_NAME; - m_selectedUuid = 0; } MapLocationModel::~MapLocationModel() @@ -120,36 +131,44 @@ void MapLocationModel::clear() endRemoveRows(); } -void MapLocationModel::setSelectedUuid(QVariant uuid, QVariant fromClick) +void MapLocationModel::setSelected(struct dive_site *ds, bool fromClick) +{ + m_selectedDs = ds; + emit selectedDsChanged(); + if (fromClick) + emit selectedLocationChanged(getMapLocation(m_selectedDs)); +} + +void MapLocationModel::setSelected(QVariant divesite, QVariant fromClick) { - m_selectedUuid = qvariant_cast<quint32>(uuid); + // See comment on uintptr_t above + struct dive_site *ds = (struct dive_site *)qvariant_cast<uintptr_t>(divesite); const bool fromClickBool = qvariant_cast<bool>(fromClick); - emit selectedUuidChanged(); - if (fromClickBool) - emit selectedLocationChanged(getMapLocationForUuid(m_selectedUuid)); + setSelected(ds, fromClickBool); } -quint32 MapLocationModel::selectedUuid() +QVariant MapLocationModel::selectedDs() { - return m_selectedUuid; + // See comment on uintptr_t above + return QVariant::fromValue((uintptr_t)m_selectedDs); } -MapLocation *MapLocationModel::getMapLocationForUuid(quint32 uuid) +MapLocation *MapLocationModel::getMapLocation(const struct dive_site *ds) { MapLocation *location; foreach(location, m_mapLocations) { - if (uuid == location->uuid()) + if (ds == location->divesite()) return location; } return NULL; } -void MapLocationModel::updateMapLocationCoordinates(quint32 uuid, QGeoCoordinate coord) +void MapLocationModel::updateMapLocationCoordinates(const struct dive_site *ds, QGeoCoordinate coord) { MapLocation *location; int row = 0; foreach(location, m_mapLocations) { - if (uuid == location->uuid()) { + if (ds == location->divesite()) { location->setCoordinateNoEmit(coord); emit dataChanged(createIndex(0, row), createIndex(0, row)); return; @@ -157,5 +176,5 @@ void MapLocationModel::updateMapLocationCoordinates(quint32 uuid, QGeoCoordinate row++; } // should not happen, as this should be called only when editing an existing marker - qWarning() << "MapLocationModel::updateMapLocationCoordinates(): cannot find MapLocation for uuid:" << uuid; + qWarning() << "MapLocationModel::updateMapLocationCoordinates(): cannot find MapLocation for uuid:" << (ds ? ds->uuid : 0); } diff --git a/qt-models/maplocationmodel.h b/qt-models/maplocationmodel.h index e198773a5..33a22366f 100644 --- a/qt-models/maplocationmodel.h +++ b/qt-models/maplocationmodel.h @@ -12,32 +12,33 @@ class MapLocation : public QObject { Q_OBJECT - Q_PROPERTY(quint32 uuid READ uuid) + Q_PROPERTY(QVariant divesite READ divesiteVariant) Q_PROPERTY(QGeoCoordinate coordinate READ coordinate WRITE setCoordinate NOTIFY coordinateChanged) Q_PROPERTY(QString name MEMBER m_name) public: static const char *PROPERTY_NAME_COORDINATE; - static const char *PROPERTY_NAME_UUID; + static const char *PROPERTY_NAME_DIVESITE; static const char *PROPERTY_NAME_NAME; explicit MapLocation(); - explicit MapLocation(quint32 uuid, QGeoCoordinate coord, QString name); + explicit MapLocation(struct dive_site *ds, QGeoCoordinate coord, QString name); QVariant getRole(int role) const; QGeoCoordinate coordinate(); void setCoordinate(QGeoCoordinate coord); void setCoordinateNoEmit(QGeoCoordinate coord); - quint32 uuid(); + QVariant divesiteVariant(); + struct dive_site *divesite(); enum Roles { - RoleUuid = Qt::UserRole + 1, + RoleDivesite = Qt::UserRole + 1, RoleCoordinate, RoleName }; private: - quint32 m_uuid; + struct dive_site *m_ds; QGeoCoordinate m_coordinate; QString m_name; @@ -49,7 +50,7 @@ class MapLocationModel : public QAbstractListModel { Q_OBJECT Q_PROPERTY(int count READ count NOTIFY countChanged) - Q_PROPERTY(quint32 selectedUuid READ selectedUuid NOTIFY selectedUuidChanged) + Q_PROPERTY(QVariant selectedDs READ selectedDs NOTIFY selectedDsChanged) public: MapLocationModel(QObject *parent = NULL); @@ -62,10 +63,11 @@ public: void add(MapLocation *); void addList(QVector<MapLocation *>); void clear(); - MapLocation *getMapLocationForUuid(quint32 uuid); - void updateMapLocationCoordinates(quint32 uuid, QGeoCoordinate coord); - Q_INVOKABLE void setSelectedUuid(QVariant uuid, QVariant fromClick = true); - quint32 selectedUuid(); + MapLocation *getMapLocation(const struct dive_site *ds); + void updateMapLocationCoordinates(const struct dive_site *ds, QGeoCoordinate coord); + void setSelected(struct dive_site *ds, bool fromClick = true); + Q_INVOKABLE void setSelected(QVariant divesite, QVariant fromClick = true); + QVariant selectedDs(); protected: QHash<int, QByteArray> roleNames() const override; @@ -73,11 +75,11 @@ protected: private: QVector<MapLocation *> m_mapLocations; QHash<int, QByteArray> m_roles; - quint32 m_selectedUuid; + struct dive_site *m_selectedDs; signals: void countChanged(int c); - void selectedUuidChanged(); + void selectedDsChanged(); void selectedLocationChanged(MapLocation *); }; |