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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414 | /* ============================================================
*
* 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-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* ============================================================ */
#include "PlacemarkLayer.h"
// Qt includes
#include <QPoint>
// Local includes
#include "AbstractProjection.h"
#include "GeoDataStyle.h"
#include "GeoPainter.h"
#include "GeoDataLatLonAltBox.h"
#include "ViewportParams.h"
#include "VisiblePlacemark.h"
#include "RenderState.h"
#include "OsmPlacemarkData.h"
#include "digikam_debug.h"
#define BATCH_RENDERING
namespace Marble
{
PlacemarkLayer::PlacemarkLayer(QAbstractItemModel* placemarkModel,
QItemSelectionModel* selectionModel,
MarbleClock* clock, const StyleBuilder* styleBuilder,
QObject* parent)
: QObject (parent),
m_layout (placemarkModel, selectionModel, clock, styleBuilder),
m_debugModeEnabled (false),
m_levelTagDebugModeEnabled(false),
m_tileLevel (0),
m_debugLevelTag (0)
{
connect(&m_layout, SIGNAL(repaintNeeded()),
this, SIGNAL(repaintNeeded()));
}
PlacemarkLayer::~PlacemarkLayer()
{
}
QStringList PlacemarkLayer::renderPosition() const
{
return QStringList(QStringLiteral("PLACEMARKS"));
}
qreal PlacemarkLayer::zValue() const
{
return 2.0;
}
bool PlacemarkLayer::render(GeoPainter* geoPainter, ViewportParams* viewport,
const QString& renderPos, GeoSceneLayer* layer)
{
Q_UNUSED(renderPos)
Q_UNUSED(layer)
QVector<VisiblePlacemark*> visiblePlacemarks = m_layout.generateLayout(viewport, m_tileLevel);
// draw placemarks less important first
QVector<VisiblePlacemark*>::const_iterator visit = visiblePlacemarks.constEnd();
QVector<VisiblePlacemark*>::const_iterator itEnd = visiblePlacemarks.constBegin();
QPainter* const painter = geoPainter;
bool const repeatableX = viewport->currentProjection()->repeatableX();
int const radius4 = 4 * viewport->radius();
#ifdef BATCH_RENDERING
QHash <QString, Fragment> hash;
#endif
while (visit != itEnd)
{
--visit;
VisiblePlacemark* const mark = *visit;
if (m_levelTagDebugModeEnabled)
{
if (mark->placemark()->hasOsmData())
{
QHash<QString, QString>::const_iterator tagIter = mark->placemark()->osmData().findTag(QStringLiteral("level"));
if (tagIter != mark->placemark()->osmData().tagsEnd())
{
const int val = tagIter.value().toInt();
if (val != m_debugLevelTag)
{
continue;
}
}
}
}
// Intentionally converting positions from floating point to pixel aligned screen grid below
QRect labelRect(mark->labelRect().toRect());
QPoint symbolPos(mark->symbolPosition().toPoint());
// when the map is such zoomed out that a given place
// appears many times, we draw one placemark at each
if (repeatableX)
{
const int symbolX = symbolPos.x();
const int textX = labelRect.x();
for (int i = symbolX % radius4, width = viewport->width() ; i <= width ; i += radius4)
{
labelRect.moveLeft(i - symbolX + textX);
symbolPos.setX(i);
if (!mark->symbolPixmap().isNull())
{
#ifdef BATCH_RENDERING
QRect symbolRect = mark->symbolPixmap().rect();
QPainter::PixmapFragment pixmapFragment = QPainter::PixmapFragment::create(QPointF(symbolPos + symbolRect.center()), QRectF(symbolRect));
auto iter = hash.find(mark->symbolId());
if (iter == hash.end())
{
Fragment fragment;
fragment.pixmap = mark->symbolPixmap();
fragment.fragments << pixmapFragment;
hash.insert(mark->symbolId(), fragment);
}
else
{
auto& fragment = iter.value();
fragment.fragments << pixmapFragment;
}
#else
painter->drawPixmap(symbolPos, mark->symbolPixmap());
#endif
}
if (!mark->labelPixmap().isNull())
{
painter->drawPixmap(labelRect, mark->labelPixmap());
}
}
}
else // simple case, one draw per placemark
{
if (!mark->symbolPixmap().isNull())
{
#ifdef BATCH_RENDERING
QRect symbolRect = mark->symbolPixmap().rect();
QPainter::PixmapFragment pixmapFragment = QPainter::PixmapFragment::create(QPointF(symbolPos + symbolRect.center()),
QRectF(symbolRect));
auto iter = hash.find(mark->symbolId());
if (iter == hash.end())
{
Fragment fragment;
fragment.pixmap = mark->symbolPixmap();
fragment.fragments << pixmapFragment;
hash.insert(mark->symbolId(), fragment);
}
else
{
auto& fragment = iter.value();
fragment.fragments << pixmapFragment;
}
#else
painter->drawPixmap(symbolPos, mark->symbolPixmap());
#endif
}
if (!mark->labelPixmap().isNull())
{
painter->drawPixmap(labelRect, mark->labelPixmap());
}
}
}
#ifdef BATCH_RENDERING
for (auto iter = hash.begin(), end = hash.end() ; iter != end ; ++iter)
{
auto const& fragment = iter.value();
if (m_debugModeEnabled)
{
QPixmap debugPixmap(fragment.pixmap.size());
QColor backgroundColor;
QString idStr = iter.key().section(QLatin1Char('/'), -1);
if (idStr.length() > 2)
{
idStr.remove(QString::fromUtf8("shop_"));
backgroundColor = QColor(
(10 * (int)(idStr[0].toLatin1())) % 255,
(10 * (int)(idStr[1].toLatin1())) % 255,
(10 * (int)(idStr[2].toLatin1())) % 255
);
}
else
{
backgroundColor = QColor((quint64)(&iter.key()));
}
debugPixmap.fill(backgroundColor);
QPainter pixpainter;
pixpainter.begin(&debugPixmap);
pixpainter.drawPixmap(0, 0, fragment.pixmap);
pixpainter.end();
iter.value().pixmap = debugPixmap;
}
painter->drawPixmapFragments(fragment.fragments.data(), fragment.fragments.size(), fragment.pixmap);
}
#endif
if (m_debugModeEnabled)
{
renderDebug(geoPainter, viewport, visiblePlacemarks);
}
return true;
}
RenderState PlacemarkLayer::renderState() const
{
return RenderState(QStringLiteral("Placemarks"));
}
QString PlacemarkLayer::runtimeTrace() const
{
return m_layout.runtimeTrace();
}
QVector<const GeoDataFeature*> PlacemarkLayer::whichPlacemarkAt(const QPoint& pos)
{
return m_layout.whichPlacemarkAt(pos);
}
bool PlacemarkLayer::hasPlacemarkAt(const QPoint& pos)
{
return m_layout.hasPlacemarkAt(pos);
}
bool PlacemarkLayer::isDebugModeEnabled() const
{
return m_debugModeEnabled;
}
void PlacemarkLayer::setDebugModeEnabled(bool enabled)
{
m_debugModeEnabled = enabled;
}
void PlacemarkLayer::setShowPlaces(bool show)
{
m_layout.setShowPlaces(show);
}
void PlacemarkLayer::setShowCities(bool show)
{
m_layout.setShowCities(show);
}
void PlacemarkLayer::setShowTerrain(bool show)
{
m_layout.setShowTerrain(show);
}
void PlacemarkLayer::setShowOtherPlaces(bool show)
{
m_layout.setShowOtherPlaces(show);
}
void PlacemarkLayer::setShowLandingSites(bool show)
{
m_layout.setShowLandingSites(show);
}
void PlacemarkLayer::setShowCraters(bool show)
{
m_layout.setShowCraters(show);
}
void PlacemarkLayer::setShowMaria(bool show)
{
m_layout.setShowMaria(show);
}
void PlacemarkLayer::requestStyleReset()
{
m_layout.requestStyleReset();
}
void PlacemarkLayer::setTileLevel(int tileLevel)
{
m_tileLevel = tileLevel;
}
void PlacemarkLayer::renderDebug(GeoPainter* painter, ViewportParams* viewport, const QVector<VisiblePlacemark*>& placemarks) const<--- Either there is a missing override/final keyword, or the parameter 'viewport' can be declared as pointer to const
{
painter->save();
painter->setFont(QFont(QStringLiteral("Sans Serif"), 7));
painter->setBrush(QBrush(Qt::NoBrush));
auto const latLonAltBox = viewport->viewLatLonAltBox();
using Placemarks = QSet<VisiblePlacemark*>;
const auto visiblePlacemarks = m_layout.visiblePlacemarks();
Placemarks const hidden = Placemarks(visiblePlacemarks.constBegin(), visiblePlacemarks.constEnd())
.subtract(Placemarks(placemarks.constBegin(), placemarks.constEnd()));
for (const auto placemark : hidden)
{
bool const inside = latLonAltBox.contains(placemark->coordinates());
painter->setPen(QPen(QColor(inside ? Qt::red : Qt::darkYellow)));
painter->drawRect(placemark->boundingBox());
}
painter->setPen(QPen(QColor(Qt::blue)));
for (const auto placemark : placemarks)
{
painter->drawRect(placemark->boundingBox());
}
painter->setPen(QPen(QColor(Qt::green)));
for (const auto placemark : placemarks)
{
painter->drawRect(placemark->labelRect());
painter->drawRect(placemark->symbolRect());
}
auto const height = painter->fontMetrics().height();
painter->setPen(QPen(QColor(Qt::black)));
for (const auto placemark : placemarks)
{
QPoint position = placemark->symbolRect().bottomLeft().toPoint() + QPoint(0, qRound(0.8 * height));
auto const popularity = placemark->placemark()->popularity();
painter->drawText(position, QStringLiteral("p: %1").arg(popularity));
position -= QPoint(0, placemark->symbolRect().height() + height);
auto const zoomLevel = placemark->placemark()->zoomLevel();
painter->drawText(position, QStringLiteral("z: %1").arg(zoomLevel));
}
painter->restore();
}
void PlacemarkLayer::setLevelTagDebugModeEnabled(bool enabled)
{
if (m_levelTagDebugModeEnabled != enabled)
{
m_levelTagDebugModeEnabled = enabled;
Q_EMIT repaintNeeded();
}
}
bool PlacemarkLayer::levelTagDebugModeEnabled() const
{
return m_levelTagDebugModeEnabled;
}
void PlacemarkLayer::setDebugLevelTag(int level)
{
if (m_debugLevelTag != level)
{
m_debugLevelTag = level;
Q_EMIT repaintNeeded();
}
}
} // namespace Marble
#include "moc_PlacemarkLayer.cpp"
|