File indexing completed on 2024-05-19 03:51:40

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2012 Dennis Nienhüser <nienhueser@kde.org>
0004 //
0005 
0006 #include "GeoPhotoGraphicsItem.h"
0007 
0008 #include "GeoPainter.h"
0009 #include "GeoDataStyle.h"
0010 #include "GeoDataIconStyle.h"
0011 #include "GeoDataFeature.h"
0012 #include "StyleBuilder.h"
0013 #include "ViewportParams.h"
0014 
0015 #include <QDebug>
0016 
0017 namespace Marble
0018 {
0019 
0020 GeoPhotoGraphicsItem::GeoPhotoGraphicsItem( const GeoDataFeature *feature )
0021     : GeoGraphicsItem( feature )
0022 {
0023     if (feature) {
0024         QString const paintLayer = QStringLiteral("Photo");
0025         setPaintLayers(QStringList() << paintLayer);
0026     }
0027 }
0028 
0029 void GeoPhotoGraphicsItem::paint(GeoPainter* painter, const ViewportParams* viewport , const QString &layer, int tileZoomLevel)
0030 {
0031     Q_UNUSED(layer);
0032     Q_UNUSED(tileZoomLevel);
0033     /* The code below loads the image lazily (only
0034     * when it will actually be displayed). Once it was
0035     * loaded but moves out of the viewport, it is unloaded
0036     * again. Otherwise memory consumption gets quite high
0037     * for a large set of photos
0038     */
0039     bool unloadImage = true;
0040 
0041     qreal x(0.0), y( 0.0 );
0042     viewport->screenCoordinates( m_point.coordinates(), x, y );
0043 
0044     QRectF position( QPointF( x, y ), style()->iconStyle().icon().size() );
0045     position.moveCenter( QPointF( x, y ) );
0046 
0047     QRectF displayed = position & QRectF( QPointF( 0, 0 ), viewport->size() );
0048 
0049     if ( !displayed.isEmpty() ) {
0050         if ( m_photo.isNull() ) {
0051             m_photo = style()->iconStyle().icon();
0052         }
0053         unloadImage = false;
0054         painter->drawImage( position, m_photo );
0055     }
0056 
0057     if ( unloadImage ) {
0058         m_photo = QImage();
0059     }
0060 }
0061 
0062 const GeoDataLatLonAltBox& GeoPhotoGraphicsItem::latLonAltBox() const
0063 {
0064     return m_point.latLonAltBox();
0065 }
0066 
0067 bool GeoPhotoGraphicsItem::contains(const QPoint &curpos, const ViewportParams *viewport) const
0068 {
0069     qreal x(0.0), y( 0.0 );
0070     viewport->screenCoordinates(m_point.coordinates(), x, y);
0071     auto itemStyle = style();
0072     if (itemStyle != nullptr && !itemStyle->iconStyle().icon().isNull()) {
0073         int halfIconWidth = itemStyle->iconStyle().icon().size().width() / 2;
0074         int halfIconHeight = itemStyle->iconStyle().icon().size().height() / 2;
0075 
0076         if ( x - halfIconWidth < curpos.x() &&
0077              curpos.x() < x + halfIconWidth &&
0078              y - halfIconHeight / 2 < curpos.y() &&
0079              curpos.y() < y + halfIconHeight / 2 ) {
0080             return true;
0081         }
0082     } else if ( curpos.x() == x && curpos.y() == y ) {
0083         return true;
0084     }
0085 
0086     return false;
0087 }
0088 
0089 void GeoPhotoGraphicsItem::setPoint( const GeoDataPoint &point )
0090 {
0091     m_point = point;
0092 }
0093 
0094 GeoDataPoint GeoPhotoGraphicsItem::point() const
0095 {
0096     return m_point;
0097 }
0098 
0099 }