File indexing completed on 2024-05-12 03:50:12

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2007 Murad Tagirov <tmurad@gmail.com>
0004 //
0005 
0006 
0007 #include "GeoDataLabelStyle.h"
0008 
0009 #include <QFont>
0010 #include <QColor>
0011 #include <QDataStream>
0012 
0013 #include "GeoDataTypes.h"
0014 
0015 namespace Marble
0016 {
0017 #ifdef Q_OS_MACX
0018 static const int defaultSize = 10;
0019 #else
0020 static const int defaultSize = 8;
0021 #endif
0022 
0023 
0024 class GeoDataLabelStylePrivate
0025 {
0026   public:
0027     GeoDataLabelStylePrivate() 
0028         : m_scale( 1.0 ),
0029           m_alignment( GeoDataLabelStyle::Corner ),
0030           m_font( QFont(QStringLiteral("Sans Serif")).family(), defaultSize, 50, false ),
0031           m_glow( true )
0032     {
0033     }
0034 
0035     explicit GeoDataLabelStylePrivate( const QFont &font )
0036         : m_scale( 1.0 ),
0037           m_alignment( GeoDataLabelStyle::Corner ),
0038           m_font( font ),
0039           m_glow( true )
0040     {
0041     }
0042 
0043     /// The current scale of the label
0044     float  m_scale;
0045     /// The current alignment of the label
0046     GeoDataLabelStyle::Alignment m_alignment;
0047     /// The current font of the label
0048     QFont  m_font;   // Not a KML property
0049     /// Whether or not the text should glow
0050     bool m_glow; // Not a KML property
0051 };
0052 
0053 GeoDataLabelStyle::GeoDataLabelStyle()
0054     : d (new GeoDataLabelStylePrivate )
0055 {
0056     setColor( QColor( Qt::black ) );
0057 }
0058 
0059 GeoDataLabelStyle::GeoDataLabelStyle( const GeoDataLabelStyle& other )
0060     : GeoDataColorStyle( other ), d (new GeoDataLabelStylePrivate( *other.d ) )
0061 {
0062 }
0063 
0064 GeoDataLabelStyle::GeoDataLabelStyle( const QFont &font, const QColor &color )
0065     : d (new GeoDataLabelStylePrivate( font ) )
0066 {
0067     setColor( color );
0068 }
0069 
0070 GeoDataLabelStyle::~GeoDataLabelStyle()
0071 {
0072     delete d;
0073 }
0074 
0075 GeoDataLabelStyle& GeoDataLabelStyle::operator=( const GeoDataLabelStyle& other )
0076 {
0077     GeoDataColorStyle::operator=( other );
0078     *d = *other.d;
0079     return *this;
0080 }
0081 
0082 bool GeoDataLabelStyle::operator==( const GeoDataLabelStyle &other ) const
0083 {
0084     if ( GeoDataColorStyle::operator!=( other ) ) {
0085         return false;
0086     }
0087 
0088     return d->m_scale == other.d->m_scale &&
0089            d->m_alignment == other.d->m_alignment &&
0090            d->m_font == other.d->m_font &&
0091            d->m_glow == other.d->m_glow;
0092 }
0093 
0094 bool GeoDataLabelStyle::operator!=( const GeoDataLabelStyle &other ) const
0095 {
0096     return !this->operator==( other );
0097 }
0098 
0099 const char* GeoDataLabelStyle::nodeType() const
0100 {
0101     return GeoDataTypes::GeoDataLabelStyleType;
0102 }
0103 
0104 void GeoDataLabelStyle::setAlignment( GeoDataLabelStyle::Alignment alignment )
0105 {
0106     d->m_alignment = alignment;
0107 }
0108 
0109 GeoDataLabelStyle::Alignment GeoDataLabelStyle::alignment() const
0110 {
0111     return d->m_alignment;
0112 }
0113 
0114 void GeoDataLabelStyle::setScale(float scale)
0115 {
0116     d->m_scale = scale;
0117 }
0118 
0119 float GeoDataLabelStyle::scale() const
0120 {
0121     return d->m_scale;
0122 }
0123 
0124 void GeoDataLabelStyle::setFont( const QFont &font )
0125 {
0126     d->m_font = font;
0127 }
0128 
0129 QFont GeoDataLabelStyle::font() const
0130 {
0131     return d->m_font;
0132 }
0133 
0134 QFont GeoDataLabelStyle::scaledFont() const
0135 {
0136    // Font shouldn't be smaller (or equal to) than 0, but if it is, regular font is returned
0137    // setPointSize() takes an integer as parameter, so rounded value should be checked
0138    if( qRound( font().pointSize() * scale() ) <= 0 ) {
0139        return font();
0140    }
0141 
0142    QFont scaledFont = font();
0143    scaledFont.setPointSize( qRound( scaledFont.pointSize() * scale() ));
0144    return scaledFont;
0145 }
0146 
0147 bool GeoDataLabelStyle::glow() const
0148 {
0149     return d->m_glow;
0150 }
0151 
0152 void GeoDataLabelStyle::setGlow(bool on)
0153 {
0154     d->m_glow = on;
0155 }
0156 
0157 void GeoDataLabelStyle::pack( QDataStream& stream ) const
0158 {
0159     GeoDataColorStyle::pack( stream );
0160 
0161     stream << d->m_scale;
0162     stream << d->m_alignment;
0163     stream << d->m_font;
0164 }
0165 
0166 void GeoDataLabelStyle::unpack( QDataStream& stream )
0167 {
0168     int a;
0169     GeoDataColorStyle::unpack( stream );
0170 
0171     stream >> d->m_scale;
0172     stream >> a;
0173     stream >> d->m_font;
0174 
0175     d->m_alignment = static_cast<GeoDataLabelStyle::Alignment>( a );
0176 }
0177 
0178 }