File indexing completed on 2024-05-05 03:49:47

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
0004 //
0005 
0006 // Self
0007 #include "GeoGraphicsItem.h"
0008 #include "GeoGraphicsItem_p.h"
0009 
0010 #include "GeoDataPlacemark.h"
0011 
0012 // Qt
0013 #include "MarbleDebug.h"
0014 
0015 #include <QColor>
0016 
0017 using namespace Marble;
0018 
0019 GeoGraphicsItem::GeoGraphicsItem( const GeoDataFeature *feature )
0020     : d( new GeoGraphicsItemPrivate( feature ) )
0021 {
0022     setFlag( ItemIsVisible, true );
0023 }
0024 
0025 GeoGraphicsItem::~GeoGraphicsItem()
0026 {
0027     delete d;
0028 }
0029 
0030 bool GeoGraphicsItem::visible() const
0031 {
0032     return d->m_flags & ItemIsVisible;
0033 }
0034 
0035 void GeoGraphicsItem::setVisible( bool visible )
0036 {
0037     setFlag( ItemIsVisible, visible );
0038 }
0039 
0040 GeoGraphicsItem::GeoGraphicsItemFlags GeoGraphicsItem::flags() const
0041 {
0042     return d->m_flags;
0043 }
0044 
0045 void GeoGraphicsItem::setFlag( GeoGraphicsItemFlag flag, bool enabled )
0046 {
0047     if( enabled ) {
0048         d->m_flags = d->m_flags | flag;
0049     } else {
0050         d->m_flags = d->m_flags & ~flag;
0051     }
0052 }
0053 
0054 void GeoGraphicsItem::setFlags( GeoGraphicsItemFlags flags )
0055 {
0056     d->m_flags = flags;
0057 }
0058 
0059 const GeoDataFeature* GeoGraphicsItem::feature() const
0060 {
0061     return d->m_feature;
0062 }
0063 
0064 void GeoGraphicsItem::setHighlightStyle( const GeoDataStyle::ConstPtr &highlightStyle)
0065 {
0066     /**
0067      * Delete any previously set style
0068      * and assign the new style highlightStyle
0069      */
0070     d->m_highlightStyle = highlightStyle;
0071 }
0072 
0073 GeoDataStyle::ConstPtr GeoGraphicsItem::style() const
0074 {
0075     /**
0076      * m_isHighlight is set true when the item is
0077      * supposed to be colored highlighted
0078      */
0079     if ( d->m_highlighted && d->m_highlightStyle ) {
0080         return d->m_highlightStyle;
0081     }
0082 
0083     if (!d->m_style) {
0084         if (const GeoDataPlacemark *placemark = geodata_cast<GeoDataPlacemark>(d->m_feature)) {
0085             auto styling = StyleParameters(placemark, d->m_renderContext.tileLevel());
0086             for (auto relation: d->m_relations) {
0087                 if (relation->isVisible()) {
0088                     styling.relation = relation;
0089                     break;
0090                 }
0091             }
0092             d->m_style = d->m_styleBuilder->createStyle(styling);
0093         } else {
0094             d->m_style = d->m_feature->style();
0095         }
0096     }
0097 
0098     return d->m_style;
0099 }
0100 
0101 void GeoGraphicsItem::setStyleBuilder(const StyleBuilder *styleBuilder)
0102 {
0103     d->m_styleBuilder = styleBuilder;
0104 }
0105 
0106 void GeoGraphicsItem::resetStyle()
0107 {
0108     d->m_style = GeoDataStyle::ConstPtr();
0109     handleRelationUpdate(d->m_relations);
0110 }
0111 
0112 qreal GeoGraphicsItem::zValue() const
0113 {
0114     return d->m_zValue;
0115 }
0116 
0117 void GeoGraphicsItem::setZValue( qreal z )
0118 {
0119     d->m_zValue = z;
0120 }
0121 
0122 void GeoGraphicsItem::setHighlighted( bool highlight )
0123 {
0124     d->m_highlighted = highlight;
0125 }
0126 
0127 bool GeoGraphicsItem::isHighlighted() const
0128 {
0129     return d->m_highlighted;
0130 }
0131 
0132 QStringList GeoGraphicsItem::paintLayers() const
0133 {
0134     return d->m_paintLayers;
0135 }
0136 
0137 void GeoGraphicsItem::setPaintLayers(const QStringList &paintLayers)
0138 {
0139     d->m_paintLayers = paintLayers;
0140 }
0141 
0142 void GeoGraphicsItem::setRenderContext(const RenderContext &renderContext)
0143 {
0144     if (renderContext != d->m_renderContext) {
0145         d->m_renderContext = renderContext;
0146         d->m_style = GeoDataStyle::ConstPtr();
0147     }
0148 }
0149 
0150 bool GeoGraphicsItem::contains(const QPoint &, const ViewportParams *) const
0151 {
0152     return false;
0153 }
0154 
0155 void GeoGraphicsItem::setRelations(const QSet<const GeoDataRelation*> &relations)
0156 {
0157     d->m_relations.clear();
0158     std::copy(relations.begin(), relations.end(), std::back_inserter(d->m_relations));
0159     std::sort(d->m_relations.begin(), d->m_relations.end(),
0160     [](const GeoDataRelation * a, const GeoDataRelation * b) {
0161         return *a < *b;
0162     });
0163 
0164     d->m_style = GeoDataStyle::ConstPtr();
0165     handleRelationUpdate(d->m_relations);
0166 }
0167 
0168 void GeoGraphicsItem::handleRelationUpdate(const QVector<const GeoDataRelation *> &)
0169 {
0170     // does nothing
0171 }
0172 
0173 int GeoGraphicsItem::minZoomLevel() const
0174 {
0175     return d->m_minZoomLevel;
0176 }
0177 
0178 void GeoGraphicsItem::setMinZoomLevel(int zoomLevel)
0179 {
0180     d->m_minZoomLevel = zoomLevel;
0181 }
0182 
0183 bool GeoGraphicsItem::zValueLessThan(GeoGraphicsItem *one, GeoGraphicsItem *two)
0184 {
0185     return one->d->m_zValue < two->d->m_zValue;
0186 }
0187 
0188 bool GeoGraphicsItem::styleLessThan(GeoGraphicsItem *one, GeoGraphicsItem *two)
0189 {
0190     return reinterpret_cast<quint64>(one->d->m_style.data()) < reinterpret_cast<quint64>(two->d->m_style.data());
0191 }
0192 
0193 bool GeoGraphicsItem::zValueAndStyleLessThan(GeoGraphicsItem *one, GeoGraphicsItem *two)
0194 {
0195     if (one->d->m_zValue == two->d->m_zValue) {
0196         return reinterpret_cast<quint64>(one->d->m_style.data()) < reinterpret_cast<quint64>(two->d->m_style.data());
0197     }
0198 
0199     return one->d->m_zValue < two->d->m_zValue;
0200 }
0201 
0202 
0203 bool RenderContext::operator==(const RenderContext &other) const
0204 {
0205     return m_tileLevel == other.m_tileLevel;
0206 }
0207 
0208 bool RenderContext::operator!=(const RenderContext &other) const
0209 {
0210     return !operator==(other);
0211 }
0212 
0213 int RenderContext::tileLevel() const
0214 {
0215     return m_tileLevel;
0216 }
0217 
0218 RenderContext::RenderContext(int tileLevel) :
0219     m_tileLevel(tileLevel)
0220 {
0221     // nothing to do
0222 }