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>
This commit is contained in:
Berthold Stoeger 2018-10-26 17:03:54 +02:00 committed by Dirk Hohndel
parent acd44467c1
commit 724055f0af
28 changed files with 181 additions and 169 deletions

View file

@ -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;

View file

@ -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_selectedUuid = qvariant_cast<quint32>(uuid);
m_selectedDs = ds;
emit selectedDsChanged();
if (fromClick)
emit selectedLocationChanged(getMapLocation(m_selectedDs));
}
void MapLocationModel::setSelected(QVariant divesite, QVariant fromClick)
{
// 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);
}

View file

@ -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 *);
};