Marble

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

KDE's Doxygen guidelines are available online.