Marble

BillboardGraphicsItem.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2012 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
4// SPDX-FileCopyrightText: 2012 Mohammed Nafees <nafees.technocool@gmail.com>
5//
6
7// self
8#include "BillboardGraphicsItem.h"
9#include "MarbleGraphicsItem_p.h"
10
11// Marble
12#include "ViewportParams.h"
13
14namespace Marble
15{
16
17class BillboardGraphicsItemPrivate : public MarbleGraphicsItemPrivate
18{
19 public:
20 BillboardGraphicsItemPrivate(BillboardGraphicsItem *parent)
21 : MarbleGraphicsItemPrivate(parent),
22 m_alignment( Qt::AlignHCenter | Qt::AlignVCenter )
23 {
24 }
25
26 QVector<QPointF> positions() const override
27 {
28 return m_positions;
29 }
30
31 QVector<QPointF> absolutePositions() const override
32 {
33 return m_positions;
34 }
35
36 Qt::Alignment m_alignment;
37
38 void setProjection(const ViewportParams *viewport) override
39 {
40 m_positions.clear();
41
42 qreal x[100], y;
43 int pointRepeatNumber = 0;
44 bool globeHidesPoint;
45
46 viewport->screenCoordinates( m_coordinates, x, y, pointRepeatNumber,
47 m_size, globeHidesPoint );
48
49 // Don't display items if they are on the far side of the globe.
50 if (globeHidesPoint) return;
51
52 m_positions.reserve(pointRepeatNumber);
53 // handle vertical alignment
54 qint32 topY = ( viewport->height() - m_size.height() ) / 2;
55 if ( m_alignment & Qt::AlignTop ) {
56 topY = y - m_size.height();
57 }
58 else if ( m_alignment & Qt::AlignVCenter ) {
59 topY = y - ( m_size.height() / 2 );
60 }
61 else if ( m_alignment & Qt::AlignBottom ) {
62 topY = y;
63 }
64
65 for ( int i = 0; i < pointRepeatNumber; ++i ) {
66 // handle horizontal alignment
67 qint32 leftX = ( viewport->width() - m_size.width() ) / 2;
68 if ( m_alignment & Qt::AlignLeft ) {
69 leftX = x[i] - m_size.width();
70 }
71 else if ( m_alignment & Qt::AlignHCenter ) {
72 leftX = x[i] - ( m_size.width() / 2 );
73 }
74 else if ( m_alignment & Qt::AlignRight ) {
75 leftX = x[i];
76 }
77
78 m_positions.append( QPoint( leftX, topY ) );
79 }
80 }
81
82 GeoDataCoordinates m_coordinates;
83 QVector<QPointF> m_positions;
84};
85
86BillboardGraphicsItem::BillboardGraphicsItem()
87 : MarbleGraphicsItem(new BillboardGraphicsItemPrivate(this))
88{
89}
90
91GeoDataCoordinates BillboardGraphicsItem::coordinate() const
92{
93 Q_D(const BillboardGraphicsItem);
94 return d->m_coordinates;
95}
96
97void BillboardGraphicsItem::setCoordinate( const GeoDataCoordinates &coordinates )
98{
99 Q_D(BillboardGraphicsItem);
100 d->m_coordinates = coordinates;
101}
102
103QVector<QPointF> BillboardGraphicsItem::positions() const
104{
106 return d->positions();
107}
108
109QVector<QRectF> BillboardGraphicsItem::boundingRects() const
110{
112
113 QVector<QRectF> rects;
114 rects.reserve(d->m_positions.size());
115
116 QSizeF const size = d->m_size;
117 for (const QPointF &point: d->m_positions) {
118 rects << QRectF(point, size);
119 }
120 return rects;
121}
122
123QRectF BillboardGraphicsItem::containsRect( const QPointF &point ) const
124{
125 for( const QRectF &rect: boundingRects() ) {
126 if( rect.contains( point ) )
127 return rect;
128 }
129
130 return QRectF();
131}
132
133Qt::Alignment BillboardGraphicsItem::alignment() const
134{
136 return d->m_alignment;
137}
138
139void BillboardGraphicsItem::setAlignment(Qt::Alignment alignment)
140{
141 Q_D(BillboardGraphicsItem);
142 d->m_alignment = alignment;
143}
144
145} // Marble namespace
This file contains the headers for ViewportParams.
Base class for all 2D labels (a.k.a.
Binds a QML item to a specific geodetic location in screen coordinates.
void reserve(qsizetype size)
AlignHCenter
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:00:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.