1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
// 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_DIVESITE = "divesite";
const char *MapLocation::PROPERTY_NAME_NAME = "name";
MapLocation::MapLocation() : m_ds(nullptr)
{
}
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::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:
return QVariant::fromValue(m_name);
default:
return QVariant();
}
}
QGeoCoordinate MapLocation::coordinate()
{
return m_coordinate;
}
void MapLocation::setCoordinate(QGeoCoordinate coord)
{
m_coordinate = coord;
emit coordinateChanged();
}
void MapLocation::setCoordinateNoEmit(QGeoCoordinate coord)
{
m_coordinate = coord;
}
struct dive_site *MapLocation::divesite()
{
return m_ds;
}
QVariant MapLocation::divesiteVariant()
{
// 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;
}
MapLocationModel::~MapLocationModel()
{
clear();
}
QVariant MapLocationModel::data(const QModelIndex & index, int role) const
{
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&) const
{
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);
}
void MapLocationModel::add(MapLocation *location)
{
beginInsertRows(QModelIndex(), m_mapLocations.size(), m_mapLocations.size());
m_mapLocations.append(location);
endInsertRows();
}
void MapLocationModel::addList(QVector<MapLocation *> list)
{
if (!list.size())
return;
beginInsertRows(QModelIndex(), m_mapLocations.size(), m_mapLocations.size() + list.size() - 1);
m_mapLocations.append(list);
endInsertRows();
}
void MapLocationModel::clear()
{
if (!m_mapLocations.size())
return;
beginRemoveRows(QModelIndex(), 0, m_mapLocations.size() - 1);
qDeleteAll(m_mapLocations);
m_mapLocations.clear();
endRemoveRows();
}
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)
{
// 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);
setSelected(ds, fromClickBool);
}
QVariant MapLocationModel::selectedDs()
{
// See comment on uintptr_t above
return QVariant::fromValue((uintptr_t)m_selectedDs);
}
MapLocation *MapLocationModel::getMapLocation(const struct dive_site *ds)
{
MapLocation *location;
foreach(location, m_mapLocations) {
if (ds == location->divesite())
return location;
}
return NULL;
}
void MapLocationModel::updateMapLocationCoordinates(const struct dive_site *ds, QGeoCoordinate coord)
{
MapLocation *location;
int row = 0;
foreach(location, m_mapLocations) {
if (ds == location->divesite()) {
location->setCoordinateNoEmit(coord);
emit dataChanged(createIndex(0, row), createIndex(0, row));
return;
}
row++;
}
// should not happen, as this should be called only when editing an existing marker
qWarning() << "MapLocationModel::updateMapLocationCoordinates(): cannot find MapLocation for uuid:" << (ds ? ds->uuid : 0);
}
|