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{
19public:
20 BillboardGraphicsItemPrivate(BillboardGraphicsItem *parent)
21 : MarbleGraphicsItemPrivate(parent)
22 , m_alignment(Qt::AlignHCenter | Qt::AlignVCenter)
23 {
24 }
25
26 QList<QPointF> positions() const override
27 {
28 return m_positions;
29 }
30
31 QList<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, m_size, globeHidesPoint);
47
48 // Don't display items if they are on the far side of the globe.
49 if (globeHidesPoint)
50 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 } else if (m_alignment & Qt::AlignVCenter) {
58 topY = y - (m_size.height() / 2);
59 } else if (m_alignment & Qt::AlignBottom) {
60 topY = y;
61 }
62
63 for (int i = 0; i < pointRepeatNumber; ++i) {
64 // handle horizontal alignment
65 qint32 leftX = (viewport->width() - m_size.width()) / 2;
66 if (m_alignment & Qt::AlignLeft) {
67 leftX = x[i] - m_size.width();
68 } else if (m_alignment & Qt::AlignHCenter) {
69 leftX = x[i] - (m_size.width() / 2);
70 } else if (m_alignment & Qt::AlignRight) {
71 leftX = x[i];
72 }
73
74 m_positions.append(QPoint(leftX, topY));
75 }
76 }
77
78 GeoDataCoordinates m_coordinates;
79 QList<QPointF> m_positions;
80};
81
82BillboardGraphicsItem::BillboardGraphicsItem()
83 : MarbleGraphicsItem(new BillboardGraphicsItemPrivate(this))
84{
85}
86
87GeoDataCoordinates BillboardGraphicsItem::coordinate() const
88{
89 Q_D(const BillboardGraphicsItem);
90 return d->m_coordinates;
91}
92
93void BillboardGraphicsItem::setCoordinate(const GeoDataCoordinates &coordinates)
94{
95 Q_D(BillboardGraphicsItem);
96 d->m_coordinates = coordinates;
97}
98
99QList<QPointF> BillboardGraphicsItem::positions() const
100{
102 return d->positions();
103}
104
105QList<QRectF> BillboardGraphicsItem::boundingRects() const
106{
108
109 QList<QRectF> rects;
110 rects.reserve(d->m_positions.size());
111
112 QSizeF const size = d->m_size;
113 for (const QPointF &point : d->m_positions) {
114 rects << QRectF(point, size);
115 }
116 return rects;
117}
118
119QRectF BillboardGraphicsItem::containsRect(const QPointF &point) const
120{
121 for (const QRectF &rect : boundingRects()) {
122 if (rect.contains(point))
123 return rect;
124 }
125
126 return {};
127}
128
129Qt::Alignment BillboardGraphicsItem::alignment() const
130{
132 return d->m_alignment;
133}
134
135void BillboardGraphicsItem::setAlignment(Qt::Alignment alignment)
136{
137 Q_D(BillboardGraphicsItem);
138 d->m_alignment = alignment;
139}
140
141} // 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 append(QList< T > &&value)
void clear()
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 Dec 6 2024 12:08:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.