1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
4 //
5
6 // Self
7 #include "GeoGraphicsItem.h"
8 #include "GeoGraphicsItem_p.h"
9
10 #include "GeoDataPlacemark.h"
11
12 // Qt
13 #include "MarbleDebug.h"
14
15 #include <QColor>
16
17 using namespace Marble;
18
GeoGraphicsItem(const GeoDataFeature * feature)19 GeoGraphicsItem::GeoGraphicsItem( const GeoDataFeature *feature )
20 : d( new GeoGraphicsItemPrivate( feature ) )
21 {
22 setFlag( ItemIsVisible, true );
23 }
24
~GeoGraphicsItem()25 GeoGraphicsItem::~GeoGraphicsItem()
26 {
27 delete d;
28 }
29
visible() const30 bool GeoGraphicsItem::visible() const
31 {
32 return d->m_flags & ItemIsVisible;
33 }
34
setVisible(bool visible)35 void GeoGraphicsItem::setVisible( bool visible )
36 {
37 setFlag( ItemIsVisible, visible );
38 }
39
flags() const40 GeoGraphicsItem::GeoGraphicsItemFlags GeoGraphicsItem::flags() const
41 {
42 return d->m_flags;
43 }
44
setFlag(GeoGraphicsItemFlag flag,bool enabled)45 void GeoGraphicsItem::setFlag( GeoGraphicsItemFlag flag, bool enabled )
46 {
47 if( enabled ) {
48 d->m_flags = d->m_flags | flag;
49 } else {
50 d->m_flags = d->m_flags & ~flag;
51 }
52 }
53
setFlags(GeoGraphicsItemFlags flags)54 void GeoGraphicsItem::setFlags( GeoGraphicsItemFlags flags )
55 {
56 d->m_flags = flags;
57 }
58
feature() const59 const GeoDataFeature* GeoGraphicsItem::feature() const
60 {
61 return d->m_feature;
62 }
63
setHighlightStyle(const GeoDataStyle::ConstPtr & highlightStyle)64 void GeoGraphicsItem::setHighlightStyle( const GeoDataStyle::ConstPtr &highlightStyle)
65 {
66 /**
67 * Delete any previously set style
68 * and assign the new style highlightStyle
69 */
70 d->m_highlightStyle = highlightStyle;
71 }
72
style() const73 GeoDataStyle::ConstPtr GeoGraphicsItem::style() const
74 {
75 /**
76 * m_isHighlight is set true when the item is
77 * supposed to be colored highlighted
78 */
79 if ( d->m_highlighted && d->m_highlightStyle ) {
80 return d->m_highlightStyle;
81 }
82
83 if (!d->m_style) {
84 if (const GeoDataPlacemark *placemark = geodata_cast<GeoDataPlacemark>(d->m_feature)) {
85 auto styling = StyleParameters(placemark, d->m_renderContext.tileLevel());
86 for (auto relation: d->m_relations) {
87 if (relation->isVisible()) {
88 styling.relation = relation;
89 break;
90 }
91 }
92 d->m_style = d->m_styleBuilder->createStyle(styling);
93 } else {
94 d->m_style = d->m_feature->style();
95 }
96 }
97
98 return d->m_style;
99 }
100
setStyleBuilder(const StyleBuilder * styleBuilder)101 void GeoGraphicsItem::setStyleBuilder(const StyleBuilder *styleBuilder)
102 {
103 d->m_styleBuilder = styleBuilder;
104 }
105
resetStyle()106 void GeoGraphicsItem::resetStyle()
107 {
108 d->m_style = GeoDataStyle::ConstPtr();
109 handleRelationUpdate(d->m_relations);
110 }
111
zValue() const112 qreal GeoGraphicsItem::zValue() const
113 {
114 return d->m_zValue;
115 }
116
setZValue(qreal z)117 void GeoGraphicsItem::setZValue( qreal z )
118 {
119 d->m_zValue = z;
120 }
121
setHighlighted(bool highlight)122 void GeoGraphicsItem::setHighlighted( bool highlight )
123 {
124 d->m_highlighted = highlight;
125 }
126
isHighlighted() const127 bool GeoGraphicsItem::isHighlighted() const
128 {
129 return d->m_highlighted;
130 }
131
paintLayers() const132 QStringList GeoGraphicsItem::paintLayers() const
133 {
134 return d->m_paintLayers;
135 }
136
setPaintLayers(const QStringList & paintLayers)137 void GeoGraphicsItem::setPaintLayers(const QStringList &paintLayers)
138 {
139 d->m_paintLayers = paintLayers;
140 }
141
setRenderContext(const RenderContext & renderContext)142 void GeoGraphicsItem::setRenderContext(const RenderContext &renderContext)
143 {
144 if (renderContext != d->m_renderContext) {
145 d->m_renderContext = renderContext;
146 d->m_style = GeoDataStyle::ConstPtr();
147 }
148 }
149
contains(const QPoint &,const ViewportParams *) const150 bool GeoGraphicsItem::contains(const QPoint &, const ViewportParams *) const
151 {
152 return false;
153 }
154
setRelations(const QSet<const GeoDataRelation * > & relations)155 void GeoGraphicsItem::setRelations(const QSet<const GeoDataRelation*> &relations)
156 {
157 d->m_relations.clear();
158 std::copy(relations.begin(), relations.end(), std::back_inserter(d->m_relations));
159 std::sort(d->m_relations.begin(), d->m_relations.end(),
160 [](const GeoDataRelation * a, const GeoDataRelation * b) {
161 return *a < *b;
162 });
163
164 d->m_style = GeoDataStyle::ConstPtr();
165 handleRelationUpdate(d->m_relations);
166 }
167
handleRelationUpdate(const QVector<const GeoDataRelation * > &)168 void GeoGraphicsItem::handleRelationUpdate(const QVector<const GeoDataRelation *> &)
169 {
170 // does nothing
171 }
172
minZoomLevel() const173 int GeoGraphicsItem::minZoomLevel() const
174 {
175 return d->m_minZoomLevel;
176 }
177
setMinZoomLevel(int zoomLevel)178 void GeoGraphicsItem::setMinZoomLevel(int zoomLevel)
179 {
180 d->m_minZoomLevel = zoomLevel;
181 }
182
zValueLessThan(GeoGraphicsItem * one,GeoGraphicsItem * two)183 bool GeoGraphicsItem::zValueLessThan(GeoGraphicsItem *one, GeoGraphicsItem *two)
184 {
185 return one->d->m_zValue < two->d->m_zValue;
186 }
187
styleLessThan(GeoGraphicsItem * one,GeoGraphicsItem * two)188 bool GeoGraphicsItem::styleLessThan(GeoGraphicsItem *one, GeoGraphicsItem *two)
189 {
190 return reinterpret_cast<quint64>(one->d->m_style.data()) < reinterpret_cast<quint64>(two->d->m_style.data());
191 }
192
zValueAndStyleLessThan(GeoGraphicsItem * one,GeoGraphicsItem * two)193 bool GeoGraphicsItem::zValueAndStyleLessThan(GeoGraphicsItem *one, GeoGraphicsItem *two)
194 {
195 if (one->d->m_zValue == two->d->m_zValue) {
196 return reinterpret_cast<quint64>(one->d->m_style.data()) < reinterpret_cast<quint64>(two->d->m_style.data());
197 }
198
199 return one->d->m_zValue < two->d->m_zValue;
200 }
201
202
operator ==(const RenderContext & other) const203 bool RenderContext::operator==(const RenderContext &other) const
204 {
205 return m_tileLevel == other.m_tileLevel;
206 }
207
operator !=(const RenderContext & other) const208 bool RenderContext::operator!=(const RenderContext &other) const
209 {
210 return !operator==(other);
211 }
212
tileLevel() const213 int RenderContext::tileLevel() const
214 {
215 return m_tileLevel;
216 }
217
RenderContext(int tileLevel)218 RenderContext::RenderContext(int tileLevel) :
219 m_tileLevel(tileLevel)
220 {
221 // nothing to do
222 }
223