File indexing completed on 2024-05-12 15:54:18
0001 /* 0002 * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 0003 * 0004 * This file is part of the KD Chart library. 0005 * 0006 * This program is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU General Public License as 0008 * published by the Free Software Foundation; either version 2 of 0009 * the License, or (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "KChartPalette.h" 0021 0022 #include "KChartMath_p.h" 0023 0024 #include <QBrush> 0025 #include <QVector> 0026 0027 using namespace KChart; 0028 0029 namespace { 0030 0031 static Palette makeDefaultPalette() { 0032 Palette p; 0033 0034 p.addBrush( Qt::red ); 0035 p.addBrush( Qt::green ); 0036 p.addBrush( Qt::blue ); 0037 p.addBrush( Qt::cyan ); 0038 p.addBrush( Qt::magenta ); 0039 p.addBrush( Qt::yellow ); 0040 p.addBrush( Qt::darkRed ); 0041 p.addBrush( Qt::darkGreen ); 0042 p.addBrush( Qt::darkBlue ); 0043 p.addBrush( Qt::darkCyan ); 0044 p.addBrush( Qt::darkMagenta ); 0045 p.addBrush( Qt::darkYellow ); 0046 0047 return p; 0048 } 0049 0050 static Palette makeSubduedPalette() { 0051 Palette p; 0052 0053 p.addBrush( QColor( 0xe0,0x7f,0x70 ) ); 0054 p.addBrush( QColor( 0xe2,0xa5,0x6f ) ); 0055 p.addBrush( QColor( 0xe0,0xc9,0x70 ) ); 0056 p.addBrush( QColor( 0xd1,0xe0,0x70 ) ); 0057 p.addBrush( QColor( 0xac,0xe0,0x70 ) ); 0058 p.addBrush( QColor( 0x86,0xe0,0x70 ) ); 0059 p.addBrush( QColor( 0x70,0xe0,0x7f ) ); 0060 p.addBrush( QColor( 0x70,0xe0,0xa4 ) ); 0061 p.addBrush( QColor( 0x70,0xe0,0xc9 ) ); 0062 p.addBrush( QColor( 0x70,0xd1,0xe0 ) ); 0063 p.addBrush( QColor( 0x70,0xac,0xe0 ) ); 0064 p.addBrush( QColor( 0x70,0x86,0xe0 ) ); 0065 p.addBrush( QColor( 0x7f,0x70,0xe0 ) ); 0066 p.addBrush( QColor( 0xa4,0x70,0xe0 ) ); 0067 p.addBrush( QColor( 0xc9,0x70,0xe0 ) ); 0068 p.addBrush( QColor( 0xe0,0x70,0xd1 ) ); 0069 p.addBrush( QColor( 0xe0,0x70,0xac ) ); 0070 p.addBrush( QColor( 0xe0,0x70,0x86 ) ); 0071 0072 return p; 0073 } 0074 0075 static Palette makeRainbowPalette() { 0076 Palette p; 0077 0078 p.addBrush( QColor(255, 0,196) ); 0079 p.addBrush( QColor(255, 0, 96) ); 0080 p.addBrush( QColor(255, 128,64) ); 0081 p.addBrush( Qt::yellow ); 0082 p.addBrush( Qt::green ); 0083 p.addBrush( Qt::cyan ); 0084 p.addBrush( QColor( 96, 96,255) ); 0085 p.addBrush( QColor(160, 0,255) ); 0086 for ( int i = 8 ; i < 16 ; ++i ) { 0087 p.addBrush( p.getBrush( i - 8 ).color().lighter(), i ); 0088 } 0089 return p; 0090 } 0091 0092 } 0093 0094 #define d d_func() 0095 0096 class Q_DECL_HIDDEN Palette::Private 0097 { 0098 public: 0099 explicit Private() {} 0100 ~Private() {} 0101 0102 QVector<QBrush> brushes; 0103 }; 0104 0105 const Palette& Palette::defaultPalette() 0106 { 0107 static const Palette palette = makeDefaultPalette(); 0108 return palette; 0109 } 0110 0111 const Palette& Palette::subduedPalette() 0112 { 0113 static const Palette palette = makeSubduedPalette(); 0114 return palette; 0115 } 0116 0117 const Palette& Palette::rainbowPalette() 0118 { 0119 static const Palette palette = makeRainbowPalette(); 0120 return palette; 0121 } 0122 0123 Palette::Palette( QObject *parent ) 0124 : QObject( parent ), _d( new Private ) 0125 { 0126 0127 } 0128 0129 Palette::~Palette() 0130 { 0131 delete _d; _d = nullptr; 0132 } 0133 0134 0135 0136 Palette::Palette( const Palette& r ) 0137 : QObject(), _d( new Private( *r.d ) ) 0138 { 0139 } 0140 0141 Palette& Palette::operator=( const Palette& r ) 0142 { 0143 Palette copy( r ); 0144 copy.swap( *this ); 0145 0146 // Q_EMIT changed() ? 0147 return *this; 0148 } 0149 0150 bool Palette::isValid() const 0151 { 0152 return d->brushes.size() >= 1; 0153 } 0154 0155 int Palette::size() const 0156 { 0157 return d->brushes.size(); 0158 } 0159 0160 void Palette::addBrush( const QBrush& brush, int position ) 0161 { 0162 if ( position < 0 || position >= size() ) { 0163 d->brushes.append( brush ); 0164 } else { 0165 d->brushes.insert( position, brush ); 0166 } 0167 Q_EMIT changed(); 0168 } 0169 0170 QBrush Palette::getBrush( int position ) const 0171 { 0172 if ( !isValid() ) return QBrush(); 0173 return d->brushes.at( position % size() ); 0174 } 0175 0176 void Palette::removeBrush( int position ) 0177 { 0178 if ( position < 0 || position >= size() ) return; 0179 d->brushes.remove( position ); 0180 Q_EMIT changed(); 0181 } 0182