mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
map: use value semantics for MapLocation
This makes memory management more simple, as not explicit deletion is necessary. A rather large commit, because changing QVector<> to std::vector<> is propagated up the call chain. Adds a new range_contains() helper function for collection types such as std::vector<>. I didn't want to call it contains(), since we already have a contains function for strings and let's keep argument overloading simple. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
e39dea3d68
commit
6b835710bc
17 changed files with 84 additions and 75 deletions
|
@ -4,11 +4,13 @@
|
|||
#include "core/divesite.h"
|
||||
#include "core/divefilter.h"
|
||||
#include "core/divelog.h"
|
||||
#include "core/range.h"
|
||||
#include "core/settings/qPrefDisplay.h"
|
||||
#if !defined(SUBSURFACE_MOBILE) && !defined(SUBSURFACE_DOWNLOADER)
|
||||
#include "qt-models/filtermodels.h"
|
||||
#include "desktop-widgets/mapwidget.h"
|
||||
#endif
|
||||
#include <map>
|
||||
|
||||
#define MIN_DISTANCE_BETWEEN_DIVE_SITES_M 50.0
|
||||
|
||||
|
@ -78,15 +80,14 @@ MapLocationModel::MapLocationModel(QObject *parent) : QAbstractListModel(parent)
|
|||
|
||||
MapLocationModel::~MapLocationModel()
|
||||
{
|
||||
qDeleteAll(m_mapLocations);
|
||||
}
|
||||
|
||||
QVariant MapLocationModel::data(const QModelIndex & index, int role) const
|
||||
{
|
||||
if (index.row() < 0 || index.row() >= m_mapLocations.size())
|
||||
if (index.row() < 0 || index.row() >= (int)m_mapLocations.size())
|
||||
return QVariant();
|
||||
|
||||
return m_mapLocations.at(index.row())->getRole(role);
|
||||
return m_mapLocations[index.row()].getRole(role);
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> MapLocationModel::roleNames() const
|
||||
|
@ -103,17 +104,19 @@ QHash<int, QByteArray> MapLocationModel::roleNames() const
|
|||
|
||||
int MapLocationModel::rowCount(const QModelIndex&) const
|
||||
{
|
||||
return m_mapLocations.size();
|
||||
return (int)m_mapLocations.size();
|
||||
}
|
||||
|
||||
/* UNUSED?
|
||||
void MapLocationModel::add(MapLocation *location)
|
||||
{
|
||||
beginInsertRows(QModelIndex(), m_mapLocations.size(), m_mapLocations.size());
|
||||
beginInsertRows(QModelIndex(), (int)m_mapLocations.size(), (int)m_mapLocations.size());
|
||||
m_mapLocations.append(location);
|
||||
endInsertRows();
|
||||
}
|
||||
*/
|
||||
|
||||
const QVector<dive_site *> &MapLocationModel::selectedDs() const
|
||||
const std::vector<dive_site *> &MapLocationModel::selectedDs() const
|
||||
{
|
||||
return m_selectedDs;
|
||||
}
|
||||
|
@ -132,22 +135,21 @@ static bool hasSelectedDive(const dive_site &ds)
|
|||
|
||||
void MapLocationModel::selectionChanged()
|
||||
{
|
||||
if (m_mapLocations.isEmpty())
|
||||
if (m_mapLocations.empty())
|
||||
return;
|
||||
for(MapLocation *m: m_mapLocations)
|
||||
m->selected = m_selectedDs.contains(m->divesite);
|
||||
emit dataChanged(createIndex(0, 0), createIndex(m_mapLocations.size() - 1, 0));
|
||||
for(MapLocation &m: m_mapLocations)
|
||||
m.selected = range_contains(m_selectedDs, m.divesite);
|
||||
emit dataChanged(createIndex(0, 0), createIndex((int)m_mapLocations.size() - 1, 0));
|
||||
}
|
||||
|
||||
void MapLocationModel::reload(QObject *map)
|
||||
{
|
||||
beginResetModel();
|
||||
|
||||
qDeleteAll(m_mapLocations);
|
||||
m_mapLocations.clear();
|
||||
m_selectedDs.clear();
|
||||
|
||||
QMap<QString, MapLocation *> locationNameMap;
|
||||
std::map<QString, size_t> locationNameMap;
|
||||
|
||||
#if defined(SUBSURFACE_MOBILE) || defined(SUBSURFACE_DOWNLOADER)
|
||||
bool diveSiteMode = false;
|
||||
|
@ -170,7 +172,7 @@ void MapLocationModel::reload(QObject *map)
|
|||
// Dive sites that do not have a gps location are not shown in normal mode.
|
||||
// In dive-edit mode, selected sites are placed at the center of the map,
|
||||
// so that the user can drag them somewhere without having to enter coordinates.
|
||||
if (!diveSiteMode || !m_selectedDs.contains(ds.get()) || !map)
|
||||
if (!diveSiteMode || !range_contains(m_selectedDs, ds.get()) || !map)
|
||||
continue;
|
||||
dsCoord = map->property("center").value<QGeoCoordinate>();
|
||||
} else {
|
||||
|
@ -178,24 +180,24 @@ void MapLocationModel::reload(QObject *map)
|
|||
qreal longitude = ds->location.lon.udeg * 0.000001;
|
||||
dsCoord = QGeoCoordinate(latitude, longitude);
|
||||
}
|
||||
if (!diveSiteMode && hasSelectedDive(*ds) && !m_selectedDs.contains(ds.get()))
|
||||
m_selectedDs.append(ds.get());
|
||||
if (!diveSiteMode && hasSelectedDive(*ds) && !range_contains(m_selectedDs, ds.get()))
|
||||
m_selectedDs.push_back(ds.get());
|
||||
QString name = siteMapDisplayName(ds->name);
|
||||
if (!diveSiteMode) {
|
||||
// don't add dive locations with the same name, unless they are
|
||||
// at least MIN_DISTANCE_BETWEEN_DIVE_SITES_M apart
|
||||
if (locationNameMap.contains(name)) {
|
||||
MapLocation *existingLocation = locationNameMap[name];
|
||||
QGeoCoordinate coord = existingLocation->coordinate;
|
||||
auto it = locationNameMap.find(name);
|
||||
if (it != locationNameMap.end()) {
|
||||
const MapLocation &existingLocation = m_mapLocations[it->second];
|
||||
QGeoCoordinate coord = existingLocation.coordinate;
|
||||
if (dsCoord.distanceTo(coord) < MIN_DISTANCE_BETWEEN_DIVE_SITES_M)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
bool selected = m_selectedDs.contains(ds.get());
|
||||
MapLocation *location = new MapLocation(ds.get(), dsCoord, name, selected);
|
||||
m_mapLocations.append(location);
|
||||
bool selected = range_contains(m_selectedDs, ds.get());
|
||||
m_mapLocations.emplace_back(ds.get(), dsCoord, name, selected);
|
||||
if (!diveSiteMode)
|
||||
locationNameMap[name] = location;
|
||||
locationNameMap[name] = m_mapLocations.size() - 1;
|
||||
}
|
||||
|
||||
endResetModel();
|
||||
|
@ -205,19 +207,19 @@ void MapLocationModel::setSelected(struct dive_site *ds)
|
|||
{
|
||||
m_selectedDs.clear();
|
||||
if (ds)
|
||||
m_selectedDs.append(ds);
|
||||
m_selectedDs.push_back(ds);
|
||||
}
|
||||
|
||||
void MapLocationModel::setSelected(const QVector<dive_site *> &divesites)
|
||||
void MapLocationModel::setSelected(const std::vector<dive_site *> &divesites)
|
||||
{
|
||||
m_selectedDs = divesites;
|
||||
}
|
||||
|
||||
MapLocation *MapLocationModel::getMapLocation(const struct dive_site *ds)
|
||||
{
|
||||
for (MapLocation *location: m_mapLocations) {
|
||||
if (ds == location->divesite)
|
||||
return location;
|
||||
for (MapLocation &location: m_mapLocations) {
|
||||
if (location.divesite == ds)
|
||||
return &location;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -225,12 +227,9 @@ MapLocation *MapLocationModel::getMapLocation(const struct dive_site *ds)
|
|||
void MapLocationModel::diveSiteChanged(struct dive_site *ds, int field)
|
||||
{
|
||||
// Find dive site
|
||||
int row;
|
||||
for (row = 0; row < m_mapLocations.size(); ++row) {
|
||||
if (m_mapLocations[row]->divesite == ds)
|
||||
break;
|
||||
}
|
||||
if (row == m_mapLocations.size())
|
||||
auto it = std::find_if(m_mapLocations.begin(), m_mapLocations.end(),
|
||||
[ds](auto &entry) { return entry.divesite == ds; });
|
||||
if (it == m_mapLocations.end())
|
||||
return;
|
||||
|
||||
switch (field) {
|
||||
|
@ -239,15 +238,16 @@ void MapLocationModel::diveSiteChanged(struct dive_site *ds, int field)
|
|||
const qreal latitude_r = ds->location.lat.udeg * 0.000001;
|
||||
const qreal longitude_r = ds->location.lon.udeg * 0.000001;
|
||||
QGeoCoordinate coord(latitude_r, longitude_r);
|
||||
m_mapLocations[row]->coordinate = coord;
|
||||
it->coordinate = coord;
|
||||
}
|
||||
break;
|
||||
case LocationInformationModel::NAME:
|
||||
m_mapLocations[row]->name = siteMapDisplayName(ds->name);
|
||||
it->name = siteMapDisplayName(ds->name);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
int row = static_cast<int>(it - m_mapLocations.begin());
|
||||
emit dataChanged(createIndex(row, 0), createIndex(row, 0));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue