Cleanup: remove accessor functions from MapLocation

Let's face it: this is a value type. No point in having Java-style
getters and setters. Replace by plain old and boring member variables.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-09-01 00:18:15 +02:00 committed by Dirk Hohndel
parent fe07a47989
commit 4eaf6b20be
3 changed files with 23 additions and 43 deletions

View file

@ -135,7 +135,7 @@ void MapWidgetHelper::selectedLocationChanged(struct dive_site *ds_in)
MapLocation *location = m_mapLocationModel->getMapLocation(ds_in); MapLocation *location = m_mapLocationModel->getMapLocation(ds_in);
if (!location) if (!location)
return; return;
QGeoCoordinate locationCoord = location->coordinate(); QGeoCoordinate locationCoord = location->coordinate;
for_each_dive (idx, dive) { for_each_dive (idx, dive) {
struct dive_site *ds = get_dive_site_for_dive(dive); struct dive_site *ds = get_dive_site_for_dive(dive);
@ -149,7 +149,7 @@ void MapWidgetHelper::selectedLocationChanged(struct dive_site *ds_in)
selectedDiveIds.append(idx); selectedDiveIds.append(idx);
} }
#else // the mobile version doesn't support multi-dive selection #else // the mobile version doesn't support multi-dive selection
if (ds == location->divesite()) if (ds == location->divesite)
selectedDiveIds.append(dive->id); // use id here instead of index selectedDiveIds.append(dive->id); // use id here instead of index
} }
int last; // get latest dive chronologically int last; // get latest dive chronologically
@ -237,7 +237,7 @@ void MapWidgetHelper::updateCurrentDiveSiteCoordinatesFromMap(struct dive_site *
{ {
MapLocation *loc = m_mapLocationModel->getMapLocation(ds); MapLocation *loc = m_mapLocationModel->getMapLocation(ds);
if (loc) if (loc)
loc->setCoordinate(coord); loc->coordinate = coord;
location_t location = mk_location(coord); location_t location = mk_location(coord);
emit coordinatesChanged(ds, location); emit coordinatesChanged(ds, location);
} }

View file

@ -12,12 +12,12 @@
#define MIN_DISTANCE_BETWEEN_DIVE_SITES_M 50.0 #define MIN_DISTANCE_BETWEEN_DIVE_SITES_M 50.0
MapLocation::MapLocation() : m_ds(nullptr), m_selected(false) MapLocation::MapLocation() : divesite(nullptr), selected(false)
{ {
} }
MapLocation::MapLocation(struct dive_site *ds, QGeoCoordinate coord, QString name, bool selected) : MapLocation::MapLocation(struct dive_site *dsIn, QGeoCoordinate coordIn, QString nameIn, bool selectedIn) :
m_ds(ds), m_coordinate(coord), m_name(name), m_selected(selected) divesite(dsIn), coordinate(coordIn), name(nameIn), selected(selectedIn)
{ {
} }
@ -37,39 +37,24 @@ QVariant MapLocation::getRole(int role) const
{ {
switch (role) { switch (role) {
case Roles::RoleDivesite: case Roles::RoleDivesite:
return QVariant::fromValue((dive_site *)m_ds); return QVariant::fromValue(divesite);
case Roles::RoleCoordinate: case Roles::RoleCoordinate:
return QVariant::fromValue(m_coordinate); return QVariant::fromValue(coordinate);
case Roles::RoleName: case Roles::RoleName:
return QVariant::fromValue(m_name); return QVariant::fromValue(name);
case Roles::RolePixmap: case Roles::RolePixmap:
return m_selected ? QString("qrc:///dive-location-marker-selected-icon") : return selected ? QString("qrc:///dive-location-marker-selected-icon") :
inEditMode() ? QString("qrc:///dive-location-marker-inactive-icon") : inEditMode() ? QString("qrc:///dive-location-marker-inactive-icon") :
QString("qrc:///dive-location-marker-icon"); QString("qrc:///dive-location-marker-icon");
case Roles::RoleZ: case Roles::RoleZ:
return m_selected ? 1 : 0; return selected ? 1 : 0;
case Roles::RoleIsSelected: case Roles::RoleIsSelected:
return QVariant::fromValue(m_selected); return QVariant::fromValue(selected);
default: default:
return QVariant(); return QVariant();
} }
} }
QGeoCoordinate MapLocation::coordinate()
{
return m_coordinate;
}
void MapLocation::setCoordinate(QGeoCoordinate coord)
{
m_coordinate = coord;
}
struct dive_site *MapLocation::divesite()
{
return m_ds;
}
MapLocationModel::MapLocationModel(QObject *parent) : QAbstractListModel(parent) MapLocationModel::MapLocationModel(QObject *parent) : QAbstractListModel(parent)
{ {
connect(&diveListNotifier, &DiveListNotifier::diveSiteChanged, this, &MapLocationModel::diveSiteChanged); connect(&diveListNotifier, &DiveListNotifier::diveSiteChanged, this, &MapLocationModel::diveSiteChanged);
@ -134,7 +119,7 @@ void MapLocationModel::selectionChanged()
if (m_mapLocations.isEmpty()) if (m_mapLocations.isEmpty())
return; return;
for(MapLocation *m: m_mapLocations) for(MapLocation *m: m_mapLocations)
m->m_selected = m_selectedDs.contains(m->divesite()); m->selected = m_selectedDs.contains(m->divesite);
emit dataChanged(createIndex(0, 0), createIndex(m_mapLocations.size() - 1, 0)); emit dataChanged(createIndex(0, 0), createIndex(m_mapLocations.size() - 1, 0));
} }
@ -186,7 +171,7 @@ void MapLocationModel::reload(QObject *map)
// at least MIN_DISTANCE_BETWEEN_DIVE_SITES_M apart // at least MIN_DISTANCE_BETWEEN_DIVE_SITES_M apart
if (locationNameMap.contains(name)) { if (locationNameMap.contains(name)) {
MapLocation *existingLocation = locationNameMap[name]; MapLocation *existingLocation = locationNameMap[name];
QGeoCoordinate coord = existingLocation->coordinate(); QGeoCoordinate coord = existingLocation->coordinate;
if (dsCoord.distanceTo(coord) < MIN_DISTANCE_BETWEEN_DIVE_SITES_M) if (dsCoord.distanceTo(coord) < MIN_DISTANCE_BETWEEN_DIVE_SITES_M)
continue; continue;
} }
@ -217,7 +202,7 @@ MapLocation *MapLocationModel::getMapLocation(const struct dive_site *ds)
{ {
MapLocation *location; MapLocation *location;
foreach(location, m_mapLocations) { foreach(location, m_mapLocations) {
if (ds == location->divesite()) if (ds == location->divesite)
return location; return location;
} }
return NULL; return NULL;
@ -228,10 +213,10 @@ void MapLocationModel::diveSiteChanged(struct dive_site *ds, int field)
// Find dive site // Find dive site
int row; int row;
for (row = 0; row < m_mapLocations.size(); ++row) { for (row = 0; row < m_mapLocations.size(); ++row) {
if (m_mapLocations[row]->divesite() == ds) if (m_mapLocations[row]->divesite == ds)
break; break;
} }
if (row == m_mapLocations.size()) if (row == m_mapLocations.size())
return; return;
switch (field) { switch (field) {
@ -240,11 +225,11 @@ void MapLocationModel::diveSiteChanged(struct dive_site *ds, int field)
const qreal latitude_r = ds->location.lat.udeg * 0.000001; const qreal latitude_r = ds->location.lat.udeg * 0.000001;
const qreal longitude_r = ds->location.lon.udeg * 0.000001; const qreal longitude_r = ds->location.lon.udeg * 0.000001;
QGeoCoordinate coord(latitude_r, longitude_r); QGeoCoordinate coord(latitude_r, longitude_r);
m_mapLocations[row]->setCoordinate(coord); m_mapLocations[row]->coordinate = coord;
} }
break; break;
case LocationInformationModel::NAME: case LocationInformationModel::NAME:
m_mapLocations[row]->setProperty("name", ds->name); m_mapLocations[row]->name = ds->name;
break; break;
default: default:
break; break;

View file

@ -19,9 +19,6 @@ public:
explicit MapLocation(struct dive_site *ds, QGeoCoordinate coord, QString name, bool selected); explicit MapLocation(struct dive_site *ds, QGeoCoordinate coord, QString name, bool selected);
QVariant getRole(int role) const; QVariant getRole(int role) const;
QGeoCoordinate coordinate();
void setCoordinate(QGeoCoordinate coord);
struct dive_site *divesite();
enum Roles { enum Roles {
RoleDivesite = Qt::UserRole + 1, RoleDivesite = Qt::UserRole + 1,
@ -32,12 +29,10 @@ public:
RoleIsSelected RoleIsSelected
}; };
private: struct dive_site *divesite;
struct dive_site *m_ds; QGeoCoordinate coordinate;
QGeoCoordinate m_coordinate; QString name;
QString m_name; bool selected = false;
public:
bool m_selected = false;
}; };
class MapLocationModel : public QAbstractListModel class MapLocationModel : public QAbstractListModel