File indexing completed on 2024-05-12 03:50:09
0001 // SPDX-License-Identifier: LGPL-2.1-or-later 0002 // 0003 // SPDX-FileCopyrightText: 2007 Murad Tagirov <tmurad@gmail.com> 0004 // SPDX-FileCopyrightText: 2012 Mohammed Nafees <nafees.technocool@gmail.com> 0005 // 0006 0007 #include "GeoDataColorStyle.h" 0008 0009 #include "GeoDataTypes.h" 0010 0011 #include <cstdlib> 0012 #include <QDataStream> 0013 #include <QColor> 0014 0015 namespace Marble 0016 { 0017 0018 class GeoDataColorStylePrivate 0019 { 0020 public: 0021 GeoDataColorStylePrivate() 0022 : m_color( Qt::white ), 0023 m_colorMode( GeoDataColorStyle::Normal ) 0024 { 0025 } 0026 0027 /// stores the current color 0028 QColor m_color; 0029 0030 /// stores random color 0031 QColor m_randomColor; 0032 0033 /// stores the current color mode 0034 GeoDataColorStyle::ColorMode m_colorMode; 0035 }; 0036 0037 GeoDataColorStyle::GeoDataColorStyle() 0038 : d( new GeoDataColorStylePrivate ) 0039 { 0040 } 0041 0042 GeoDataColorStyle::GeoDataColorStyle( const GeoDataColorStyle& other ) 0043 : GeoDataObject( other ), 0044 d( new GeoDataColorStylePrivate( *other.d ) ) 0045 { 0046 } 0047 0048 GeoDataColorStyle::~GeoDataColorStyle() 0049 { 0050 delete d; 0051 } 0052 0053 GeoDataColorStyle& GeoDataColorStyle::operator=( const GeoDataColorStyle& other ) 0054 { 0055 GeoDataObject::operator=( other ); 0056 *d = *other.d; 0057 return *this; 0058 } 0059 0060 bool GeoDataColorStyle::operator==( const GeoDataColorStyle &other ) const 0061 { 0062 return equals(other) && d->m_color == other.d->m_color && 0063 d->m_colorMode == other.d->m_colorMode; 0064 } 0065 0066 bool GeoDataColorStyle::operator!=( const GeoDataColorStyle &other ) const 0067 { 0068 return !this->operator==(other); 0069 } 0070 0071 const char* GeoDataColorStyle::nodeType() const 0072 { 0073 return GeoDataTypes::GeoDataColorStyleType; 0074 } 0075 0076 void GeoDataColorStyle::setColor( const QColor &value ) 0077 { 0078 d->m_color = value; 0079 0080 qreal red = d->m_color.redF(); 0081 qreal green = d->m_color.greenF(); 0082 qreal blue = d->m_color.blueF(); 0083 d->m_randomColor = d->m_color; 0084 qreal const randMax = RAND_MAX; 0085 d->m_randomColor.setRedF(red*(qrand()/randMax)); 0086 d->m_randomColor.setGreenF(green*(qrand()/randMax)); 0087 d->m_randomColor.setBlueF(blue*(qrand()/randMax)); 0088 } 0089 0090 QColor GeoDataColorStyle::color() const 0091 { 0092 return d->m_color; 0093 } 0094 0095 QColor GeoDataColorStyle::paintedColor() const 0096 { 0097 return d->m_colorMode == Normal ? d->m_color : d->m_randomColor; 0098 } 0099 0100 void GeoDataColorStyle::setColorMode(ColorMode colorMode) 0101 { 0102 d->m_colorMode = colorMode; 0103 } 0104 0105 GeoDataColorStyle::ColorMode GeoDataColorStyle::colorMode() const 0106 { 0107 return d->m_colorMode; 0108 } 0109 0110 void GeoDataColorStyle::pack( QDataStream& stream ) const 0111 { 0112 GeoDataObject::pack( stream ); 0113 0114 stream << d->m_color; 0115 // FIXME: Why is not colorMode saved? 0116 // stream << m_colorMode; 0117 } 0118 0119 void GeoDataColorStyle::unpack( QDataStream& stream ) 0120 { 0121 GeoDataObject::unpack( stream ); 0122 0123 stream >> d->m_color; 0124 // FIXME: Why is not colorMode saved? 0125 // stream >> m_colorMode; 0126 } 0127 0128 QString Marble::GeoDataColorStyle::contrastColor(const QColor &color) 0129 { 0130 return color.valueF() > 0.85 ? QStringLiteral("black") : QStringLiteral("white"); 0131 } 0132 0133 }