Marble

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

KDE's Doxygen guidelines are available online.