Marble

BillboardGraphicsItem.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2012 Bernhard Beschow <[email protected]>
4 // SPDX-FileCopyrightText: 2012 Mohammed Nafees <[email protected]>
5 //
6 
7 // self
8 #include "BillboardGraphicsItem.h"
9 #include "MarbleGraphicsItem_p.h"
10 
11 // Marble
12 #include "ViewportParams.h"
13 
14 namespace Marble
15 {
16 
17 class 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 
86 BillboardGraphicsItem::BillboardGraphicsItem()
87  : MarbleGraphicsItem(new BillboardGraphicsItemPrivate(this))
88 {
89 }
90 
91 GeoDataCoordinates BillboardGraphicsItem::coordinate() const
92 {
93  Q_D(const BillboardGraphicsItem);
94  return d->m_coordinates;
95 }
96 
97 void BillboardGraphicsItem::setCoordinate( const GeoDataCoordinates &coordinates )
98 {
99  Q_D(BillboardGraphicsItem);
100  d->m_coordinates = coordinates;
101 }
102 
104 {
105  Q_D(const BillboardGraphicsItem);
106  return d->positions();
107 }
108 
110 {
111  Q_D(const BillboardGraphicsItem);
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 
124 {
125  for( const QRectF &rect: boundingRects() ) {
126  if( rect.contains( point ) )
127  return rect;
128  }
129 
130  return QRectF();
131 }
132 
133 Qt::Alignment BillboardGraphicsItem::alignment() const
134 {
135  Q_D(const BillboardGraphicsItem);
136  return d->m_alignment;
137 }
138 
139 void BillboardGraphicsItem::setAlignment(Qt::Alignment alignment)
140 {
141  Q_D(BillboardGraphicsItem);
142  d->m_alignment = alignment;
143 }
144 
145 } // Marble namespace
AlignHCenter
QVector< QRectF > boundingRects() const
Returns the absolute screen bounding boxes of the item.
Base class for all 2D labels (a.k.a.
QVector< QPointF > positions() const
Returns the absolute screen positions of the item.
Binds a QML item to a specific geodetic location in screen coordinates.
void reserve(int size)
QRectF containsRect(const QPointF &point) const
Returns the rect of one representation of the object that is at the given position.
Q_D(Todo)
Q_INVOKABLE void setProjection(uint proj)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Jun 26 2022 04:10:35 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.