Marble

GeoPhotoGraphicsItem.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2012 Dennis Nienhüser <nienhueser@kde.org>
4//
5
6#include "GeoPhotoGraphicsItem.h"
7
8#include "GeoDataFeature.h"
9#include "GeoDataIconStyle.h"
10#include "GeoDataStyle.h"
11#include "GeoPainter.h"
12#include "ViewportParams.h"
13
14#include <QDebug>
15
16namespace Marble
17{
18
19GeoPhotoGraphicsItem::GeoPhotoGraphicsItem(const GeoDataFeature *feature)
20 : GeoGraphicsItem(feature)
21{
22 if (feature) {
23 QString const paintLayer = QStringLiteral("Photo");
24 setPaintLayers(QStringList() << paintLayer);
25 }
26}
27
28void GeoPhotoGraphicsItem::paint(GeoPainter *painter, const ViewportParams *viewport, const QString &layer, int tileZoomLevel)
29{
30 Q_UNUSED(layer);
31 Q_UNUSED(tileZoomLevel);
32 /* The code below loads the image lazily (only
33 * when it will actually be displayed). Once it was
34 * loaded but moves out of the viewport, it is unloaded
35 * again. Otherwise memory consumption gets quite high
36 * for a large set of photos
37 */
38 bool unloadImage = true;
39
40 qreal x(0.0), y(0.0);
41 viewport->screenCoordinates(m_point.coordinates(), x, y);
42
43 QRectF position(QPointF(x, y), style()->iconStyle().icon().size());
44 position.moveCenter(QPointF(x, y));
45
46 QRectF displayed = position & QRectF(QPointF(0, 0), viewport->size());
47
48 if (!displayed.isEmpty()) {
49 if (m_photo.isNull()) {
50 m_photo = style()->iconStyle().icon();
51 }
52 unloadImage = false;
53 painter->drawImage(position, m_photo);
54 }
55
56 if (unloadImage) {
57 m_photo = QImage();
58 }
59}
60
61const GeoDataLatLonAltBox &GeoPhotoGraphicsItem::latLonAltBox() const
62{
63 return m_point.latLonAltBox();
64}
65
66bool GeoPhotoGraphicsItem::contains(const QPoint &curpos, const ViewportParams *viewport) const
67{
68 qreal x(0.0), y(0.0);
69 viewport->screenCoordinates(m_point.coordinates(), x, y);
70 auto itemStyle = style();
71 if (itemStyle != nullptr && !itemStyle->iconStyle().icon().isNull()) {
72 int halfIconWidth = itemStyle->iconStyle().icon().size().width() / 2;
73 int halfIconHeight = itemStyle->iconStyle().icon().size().height() / 2;
74
75 if (x - halfIconWidth < curpos.x() && curpos.x() < x + halfIconWidth && y - halfIconHeight / 2 < curpos.y() && curpos.y() < y + halfIconHeight / 2) {
76 return true;
77 }
78 } else if (curpos.x() == x && curpos.y() == y) {
79 return true;
80 }
81
82 return false;
83}
84
85void GeoPhotoGraphicsItem::setPoint(const GeoDataPoint &point)
86{
87 m_point = point;
88}
89
90GeoDataPoint GeoPhotoGraphicsItem::point() const
91{
92 return m_point;
93}
94
95}
This file contains the headers for ViewportParams.
Binds a QML item to a specific geodetic location in screen coordinates.
int x() const const
int y() const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:37:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.