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 | /* ============================================================
*
* 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 "MarbleGraphicsItem.h"
#include "MarbleGraphicsItem_p.h"
// Qt includes
#include <QList>
#include <QPainter>
#include <QPixmap>
#include <QMouseEvent>
// Local includes
#include "ViewportParams.h"
#include "digikam_debug.h"
namespace Marble
{
MarbleGraphicsItem::MarbleGraphicsItem(MarbleGraphicsItemPrivate* dd)
: d_ptr(dd)
{
}
MarbleGraphicsItem::~MarbleGraphicsItem()
{
delete d_ptr;
}
bool MarbleGraphicsItem::paintEvent(QPainter* painter, const ViewportParams* viewport)
{
Q_D(MarbleGraphicsItem);
if (!d->m_visibility)
{
return true;
}
if (d->m_repaintNeeded)
{
d->updateChildPositions();
d->m_pixmap = QPixmap();
d->m_repaintNeeded = false;
}
setProjection(viewport);
if (d->positions().size() == 0)
{
return true;
}
// At the moment, as GraphicsItems cannot be zoomed or rotated ItemCoordinateCache
// and DeviceCoordianteCache is exactly the same
if (
(ItemCoordinateCache == cacheMode()) ||
(DeviceCoordinateCache == cacheMode())
)
{
const qreal scale = painter->device()->devicePixelRatio();
const QSize neededPixmapSize = scale * size().toSize() + QSize(1, 1); // adding a pixel for rounding errors
if (
(d->m_pixmap.size() != neededPixmapSize) ||
(d->m_pixmap.devicePixelRatio() != scale)
)
{
if (size().isValid() && !size().isNull())
{
d->m_pixmap = QPixmap(neededPixmapSize);
d->m_pixmap.setDevicePixelRatio(scale);
}
else
{
qCDebug(DIGIKAM_GEOENGINE_LOG) << "Warning: Invalid pixmap size suggested: " << d->m_size;
}
d->m_pixmap.fill(Qt::transparent);
QPainter pixmapPainter(&d->m_pixmap);
// We paint in best quality here, as we only have to paint once.
pixmapPainter.setRenderHint(QPainter::Antialiasing, true);
// The cache image will get a 0.5 pixel bounding to save antialiasing effects.
pixmapPainter.translate(0.5, 0.5);
paint(&pixmapPainter);
// Paint children
for (MarbleGraphicsItem* const item : d->m_children)
{
item->paintEvent(&pixmapPainter, viewport);
}
}
for (const QPointF& position : d->positions())
{
painter->drawPixmap(position, d->m_pixmap);
}
}
else
{
for (const QPointF& position : d->positions())
{
painter->save();
painter->translate(position);
paint(painter);
// Paint children
for (MarbleGraphicsItem* const item : d->m_children)
{
item->paintEvent(painter, viewport);
}
painter->restore();
}
}
return true;
}
bool MarbleGraphicsItem::contains(const QPointF& point) const
{
Q_D(const MarbleGraphicsItem);
for (const QRectF& rect : d->boundingRects())
{
if (rect.contains(point))
{
return true;
}
}
return false;
}
QVector<QRectF> MarbleGraphicsItemPrivate::boundingRects() const
{
const QVector<QPointF> positions = this->positions();<--- Shadow variable
QVector<QRectF> list;
list.reserve(positions.count());
for (const QPointF& point : positions)
{
QRectF rect(point, m_size);
if (rect.x() < 0)
{
rect.setLeft(0);
}
if (rect.y() < 0)
{
rect.setTop(0);
}
list.append(rect);
}
return list;
}
QSizeF MarbleGraphicsItem::size() const
{
Q_D(const MarbleGraphicsItem);
return d->m_size;
}
AbstractMarbleGraphicsLayout* MarbleGraphicsItem::layout() const
{
Q_D(const MarbleGraphicsItem);
return d->m_layout;
}
void MarbleGraphicsItem::setLayout(AbstractMarbleGraphicsLayout* layout)
{
Q_D(MarbleGraphicsItem);
// Deleting the old layout
delete d->m_layout;
d->m_layout = layout;
update();
}
MarbleGraphicsItem::CacheMode MarbleGraphicsItem::cacheMode() const
{
Q_D(const MarbleGraphicsItem);
return d->m_cacheMode;
}
void MarbleGraphicsItem::setCacheMode(CacheMode mode)
{
Q_D(MarbleGraphicsItem);
d->m_cacheMode = mode;
if (d->m_cacheMode == NoCache)
{
d->m_repaintNeeded = true;
}
}
void MarbleGraphicsItem::update()
{
Q_D(MarbleGraphicsItem);
d->m_repaintNeeded = true;
// Update the parent.
if (d->m_parent)
{
d->m_parent->update();
}
}
bool MarbleGraphicsItem::visible() const
{
Q_D(const MarbleGraphicsItem);
return d->m_visibility;
}
void MarbleGraphicsItem::setVisible(bool visible)
{
Q_D(MarbleGraphicsItem);
d->m_visibility = visible;
}
void MarbleGraphicsItem::hide()
{
setVisible(false);
}
void MarbleGraphicsItem::show()
{
setVisible(true);
}
void MarbleGraphicsItem::setSize(const QSizeF& size)
{
Q_D(MarbleGraphicsItem);
if (d->m_size != size)
{
d->m_size = size;
update();
}
}
QSizeF MarbleGraphicsItem::contentSize() const
{
return size();
}
void MarbleGraphicsItem::setContentSize(const QSizeF& size)
{
setSize(size);
}
QRectF MarbleGraphicsItem::contentRect() const
{
return QRectF(QPointF(0, 0), contentSize());
}
void MarbleGraphicsItem::paint(QPainter* painter)
{
Q_UNUSED(painter);
}
bool MarbleGraphicsItem::eventFilter(QObject* object, QEvent* e)
{
if (
!(
(e->type() == QEvent::MouseButtonDblClick) ||
(e->type() == QEvent::MouseMove) ||
(e->type() == QEvent::MouseButtonPress) ||
(e->type() == QEvent::MouseButtonRelease)
)
)
{
return false;
}
Q_D(const MarbleGraphicsItem);
QMouseEvent* event = static_cast<QMouseEvent*>(e);
if (!d->m_children.isEmpty())
{
const QVector<QPointF> absolutePositions = d->absolutePositions();
for (const QPointF& absolutePosition : absolutePositions)
{
QPoint shiftedPos = event->pos() - absolutePosition.toPoint();
if (QRect(QPoint(0, 0), size().toSize()).contains(shiftedPos))
{
for (MarbleGraphicsItem* const child : d->m_children)
{
const QVector<QRectF> childRects = child->d_func()->boundingRects();
for (const QRectF& childRect : childRects)
{
if (childRect.toRect().contains(shiftedPos))
{
if (child->eventFilter(object, e))
{
return true;
}
}
}
}
}
}
}
return false;
}
void MarbleGraphicsItem::setProjection(const ViewportParams* viewport)
{
Q_D(MarbleGraphicsItem);
d->setProjection(viewport);
}
} // namespace Marble
|