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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2023-05-15
* Description : geolocation engine based on Marble.
* (c) 2007-2022 Marble Team
* https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
*
* SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* ============================================================ */
#include "GeoGraphicsScene.h"
// Qt includes
#include <QMap>
#include <QRect>
// Local includes
#include "GeoDataFeature.h"
#include "GeoDataLatLonAltBox.h"
#include "GeoDataStyle.h"
#include "GeoDataStyleMap.h"
#include "GeoDataPlacemark.h"
#include "GeoDataDocument.h"
#include "GeoGraphicsItem.h"
#include "TileId.h"
#include "TileCoordsPyramid.h"
#include "digikam_debug.h"
namespace Marble
{
class Q_DECL_HIDDEN GeoGraphicsScenePrivate
{
public:
GeoGraphicsScene* q = nullptr;
explicit GeoGraphicsScenePrivate(GeoGraphicsScene* parent) :
q(parent)
{
}
~GeoGraphicsScenePrivate()
{
q->clear();
}
typedef QHash<const GeoDataFeature*, GeoGraphicsItem*> FeatureItemMap;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
QMultiHash<TileId, FeatureItemMap> m_tiledItems;
#else
QHash<TileId, FeatureItemMap> m_tiledItems;
#endif
QMultiHash<const GeoDataFeature*, TileId> m_features; // multi hash because multi track and multi geometry insert multiple items
// Stores the items which have been clicked;
QList<GeoGraphicsItem*> m_selectedItems;
static GeoDataStyle::Ptr highlightStyle(const GeoDataDocument* document, const GeoDataStyleMap& styleMap);
void selectItem(GeoGraphicsItem* item);
static void applyHighlightStyle(GeoGraphicsItem* item, const GeoDataStyle::Ptr& style);
};
GeoDataStyle::Ptr GeoGraphicsScenePrivate::highlightStyle(const GeoDataDocument* document,
const GeoDataStyleMap& styleMap)
{
// @todo Consider QUrl parsing when external styles are suppported
QString highlightStyleId = styleMap.value(QStringLiteral("highlight"));
highlightStyleId.remove(QLatin1Char('#'));
if (!highlightStyleId.isEmpty())
{
GeoDataStyle::Ptr highlightStyle(new GeoDataStyle(*document->style(highlightStyleId)));
return highlightStyle;
}
else
{
return GeoDataStyle::Ptr();
}
}
void GeoGraphicsScenePrivate::selectItem(GeoGraphicsItem* item)<--- Parameter 'item' can be declared as pointer to const
{
m_selectedItems.append(item);
}
void GeoGraphicsScenePrivate::applyHighlightStyle(GeoGraphicsItem* item, const GeoDataStyle::Ptr& highlightStyle)
{
item->setHighlightStyle(highlightStyle);
item->setHighlighted(true);
}
GeoGraphicsScene::GeoGraphicsScene(QObject* parent):
QObject(parent),
d(new GeoGraphicsScenePrivate(this))
{
}
GeoGraphicsScene::~GeoGraphicsScene()
{
delete d;
}
QList< GeoGraphicsItem* > GeoGraphicsScene::items(const GeoDataLatLonBox& box, int zoomLevel) const
{
if (box.west() > box.east())
{
// Handle boxes crossing the IDL by splitting it into two separate boxes
GeoDataLatLonBox left;
left.setWest(-M_PI);
left.setEast(box.east());
left.setNorth(box.north());
left.setSouth(box.south());
GeoDataLatLonBox right;
right.setWest(box.west());
right.setEast(M_PI);
right.setNorth(box.north());
right.setSouth(box.south());
return items(left, zoomLevel) + items(right, zoomLevel);
}
QList< GeoGraphicsItem* > result;
QRect rect;
qreal north, south, east, west;
box.boundaries(north, south, east, west);
TileId key;
key = TileId::fromCoordinates(GeoDataCoordinates(west, north, 0), zoomLevel);
rect.setLeft(key.x());
rect.setTop(key.y());
key = TileId::fromCoordinates(GeoDataCoordinates(east, south, 0), zoomLevel);
rect.setRight(key.x());
rect.setBottom(key.y());
TileCoordsPyramid pyramid(0, zoomLevel);
pyramid.setBottomLevelCoords(rect);
for (int level = pyramid.topLevel(); level <= pyramid.bottomLevel(); ++level)
{
QRect const coords = pyramid.coords(level);
int x1, y1, x2, y2;
coords.getCoords(&x1, &y1, &x2, &y2);
for (int x = x1; x <= x2; ++x)
{
bool const isBorderX = x == x1 || x == x2;
for (int y = y1; y <= y2; ++y)
{
bool const isBorder = isBorderX || y == y1 || y == y2;
const TileId tileId = TileId(0, level, x, y);
for (GeoGraphicsItem* object : d->m_tiledItems.value(tileId))
{
if (object->minZoomLevel() <= zoomLevel && object->visible())
{
if (!isBorder || object->latLonAltBox().intersects(box))
{
result.push_back(object);
}
}
}
}
}
}
return result;
}
QList< GeoGraphicsItem* > GeoGraphicsScene::selectedItems() const
{
return d->m_selectedItems;
}
void GeoGraphicsScene::resetStyle()
{
for (auto const& items : d->m_tiledItems)
{
for (auto item : items)
{
item->resetStyle();
}
}
Q_EMIT repaintNeeded();
}
void GeoGraphicsScene::applyHighlight(const QVector< GeoDataPlacemark* >& selectedPlacemarks)
{
/**
* First set the items, which were selected previously, to
* use normal style
*/
for (GeoGraphicsItem* item : d->m_selectedItems)
{
item->setHighlighted(false);
}
// Also clear the list to store the new selected items
d->m_selectedItems.clear();
/**
* Process the placemark. which were under mouse
* while clicking, and update corresponding graphics
* items to use highlight style
*/
for (const GeoDataPlacemark* placemark : selectedPlacemarks)
{
for (auto tileIter = d->m_features.find(placemark) ;
tileIter != d->m_features.end() && tileIter.key() == placemark ;
++tileIter)
{
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
auto const& clickedItemsList = d->m_tiledItems.values(*tileIter);
#else
auto const& clickedItemsList = static_cast<QMultiHash<TileId, GeoGraphicsScenePrivate::FeatureItemMap> >(d->m_tiledItems).values();
#endif
for (auto const& clickedItems : clickedItemsList)
{
// iterate through FeatureItemMap clickedItems (QHash)
for (auto iter = clickedItems.find(placemark); iter != clickedItems.end(); ++iter)
{
if (iter.key() == placemark)
{
const GeoDataObject* parent = placemark->parent();
if (parent)
{
auto item = *iter;
if (const GeoDataDocument* doc = geodata_cast<GeoDataDocument>(parent))
{
QString styleUrl = placemark->styleUrl();
styleUrl.remove(QLatin1Char('#'));
if (!styleUrl.isEmpty())
{
GeoDataStyleMap const& styleMap = doc->styleMap(styleUrl);
GeoDataStyle::Ptr style = d->highlightStyle(doc, styleMap);
if (style)
{
d->selectItem(item);
d->applyHighlightStyle(item, style);
}
}
/**
* If a placemark is using an inline style instead of a shared
* style ( e.g in case when theme file specifies the colorMap
* attribute ) then highlight it if any of the style maps have a
* highlight styleId
*/
else
{
for (const GeoDataStyleMap& styleMap : doc->styleMaps())
{
GeoDataStyle::Ptr style = d->highlightStyle(doc, styleMap);
if (style)
{
d->selectItem(item);
d->applyHighlightStyle(item, style);
break;
}
}
}
}
}
}
}
}
}
}
Q_EMIT repaintNeeded();
}
void GeoGraphicsScene::removeItem(const GeoDataFeature* feature)
{
for (auto tileIter = d->m_features.find(feature), end = d->m_features.end(); tileIter != end && tileIter.key() == feature;)
{
auto& tileList = d->m_tiledItems[*tileIter];
auto iter = tileList.find(feature);
if (iter != tileList.end())
{
auto item = iter.value();
tileIter = d->m_features.erase(tileIter);
tileList.erase(iter);
delete item;
}
else
{
++tileIter;
}
}
}
void GeoGraphicsScene::clear()
{
for (auto const& list : d->m_tiledItems.values())
{
qDeleteAll(list.values());
}
d->m_tiledItems.clear();
d->m_features.clear();
}
void GeoGraphicsScene::addItem(GeoGraphicsItem* item)
{
// Select zoom level so that the object fit in single tile
int zoomLevel;
qreal north, south, east, west;
item->latLonAltBox().boundaries(north, south, east, west);
for (zoomLevel = item->minZoomLevel(); zoomLevel >= 0; zoomLevel--)
{
if (TileId::fromCoordinates(GeoDataCoordinates(west, north, 0), zoomLevel) ==
TileId::fromCoordinates(GeoDataCoordinates(east, south, 0), zoomLevel))
{
break;
}
}
const TileId key = TileId::fromCoordinates(GeoDataCoordinates(west, north, 0), zoomLevel); // same as GeoDataCoordinates(east, south, 0), see above
auto& tileList = d->m_tiledItems[key];
auto feature = item->feature();
tileList.insert(feature, item);
d->m_features.insert(feature, key);
}
} // namespace Marble
#include "moc_GeoGraphicsScene.cpp"
|