2017-07-17 14:58:34 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "maplocationmodel.h"
|
|
|
|
|
2017-07-18 23:02:43 +00:00
|
|
|
const char *MapLocation::PROPERTY_NAME_COORDINATE = "coordinate";
|
|
|
|
const char *MapLocation::PROPERTY_NAME_UUID = "uuid";
|
|
|
|
|
2017-07-17 14:58:34 +00:00
|
|
|
MapLocation::MapLocation()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-18 23:02:43 +00:00
|
|
|
MapLocation::MapLocation(quint32 uuid, QGeoCoordinate coord) :
|
|
|
|
m_uuid(uuid), m_coordinate(coord)
|
2017-07-17 14:58:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant MapLocation::getRole(int role) const
|
|
|
|
{
|
|
|
|
switch (role) {
|
2017-07-18 23:02:43 +00:00
|
|
|
case Roles::RoleUuid:
|
|
|
|
return QVariant::fromValue(m_uuid);
|
2017-07-17 19:36:55 +00:00
|
|
|
case Roles::RoleCoordinate:
|
|
|
|
return QVariant::fromValue(m_coordinate);
|
2017-07-17 14:58:34 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MapLocationModel::MapLocationModel(QObject *parent) : QAbstractListModel(parent)
|
|
|
|
{
|
2017-07-18 23:02:43 +00:00
|
|
|
m_roles[MapLocation::Roles::RoleUuid] = MapLocation::PROPERTY_NAME_UUID;
|
|
|
|
m_roles[MapLocation::Roles::RoleCoordinate] = MapLocation::PROPERTY_NAME_COORDINATE;
|
2017-07-17 14:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MapLocationModel::~MapLocationModel()
|
|
|
|
{
|
2017-07-17 19:12:04 +00:00
|
|
|
clear();
|
2017-07-17 14:58:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 20:01:28 +00:00
|
|
|
QVariant MapLocationModel::data(const QModelIndex & index, int role) const
|
2017-07-17 14:58:34 +00:00
|
|
|
{
|
|
|
|
if (index.row() < 0 || index.row() >= m_mapLocations.size())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
return m_mapLocations.at(index.row())->getRole(role);
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> MapLocationModel::roleNames() const
|
|
|
|
{
|
|
|
|
return m_roles;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MapLocationModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
return m_mapLocations.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int MapLocationModel::count()
|
|
|
|
{
|
|
|
|
return m_mapLocations.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
MapLocation *MapLocationModel::get(int row)
|
|
|
|
{
|
|
|
|
if (row < 0 || row >= m_mapLocations.size())
|
|
|
|
return NULL;
|
|
|
|
return m_mapLocations.at(row);
|
|
|
|
}
|
2017-07-17 19:12:04 +00:00
|
|
|
|
|
|
|
void MapLocationModel::add(MapLocation *location)
|
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(), m_mapLocations.size(), m_mapLocations.size());
|
|
|
|
m_mapLocations.append(location);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2017-07-17 20:04:00 +00:00
|
|
|
void MapLocationModel::addList(QVector<MapLocation *> list)
|
2017-07-17 19:59:14 +00:00
|
|
|
{
|
|
|
|
if (!list.size())
|
|
|
|
return;
|
|
|
|
beginInsertRows(QModelIndex(), m_mapLocations.size(), m_mapLocations.size() + list.size() - 1);
|
|
|
|
m_mapLocations.append(list);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2017-07-17 19:12:04 +00:00
|
|
|
void MapLocationModel::clear()
|
|
|
|
{
|
|
|
|
if (!m_mapLocations.size())
|
|
|
|
return;
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_mapLocations.size() - 1);
|
|
|
|
qDeleteAll(m_mapLocations);
|
|
|
|
m_mapLocations.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
2017-07-18 23:27:30 +00:00
|
|
|
|
|
|
|
quint32 MapLocationModel::selectedUuid()
|
|
|
|
{
|
|
|
|
return m_selectedUuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapLocationModel::setSelectedUuid(quint32 uuid)
|
|
|
|
{
|
|
|
|
m_selectedUuid = uuid;
|
2017-07-19 00:02:34 +00:00
|
|
|
emit selectedLocationChanged(getMapLocationForUuid(uuid));
|
|
|
|
}
|
|
|
|
|
|
|
|
MapLocation *MapLocationModel::getMapLocationForUuid(quint32 uuid)
|
|
|
|
{
|
2017-07-18 23:27:30 +00:00
|
|
|
MapLocation *location = NULL;
|
|
|
|
foreach(location, m_mapLocations) {
|
|
|
|
if (location->getRole(MapLocation::Roles::RoleUuid) == uuid)
|
|
|
|
break;
|
|
|
|
}
|
2017-07-19 00:02:34 +00:00
|
|
|
return location;
|
2017-07-18 23:27:30 +00:00
|
|
|
}
|