KDChartPalette.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB.  All rights reserved.
00003 **
00004 ** This file is part of the KD Chart library.
00005 **
00006 ** Licensees holding valid commercial KD Chart licenses may use this file in
00007 ** accordance with the KD Chart Commercial License Agreement provided with
00008 ** the Software.
00009 **
00010 **
00011 ** This file may be distributed and/or modified under the terms of the
00012 ** GNU General Public License version 2 and version 3 as published by the
00013 ** Free Software Foundation and appearing in the file LICENSE.GPL included.
00014 **
00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 **
00018 ** Contact info@kdab.com if any conditions of this licensing are not
00019 ** clear to you.
00020 **
00021 **********************************************************************/
00022 
00023 #include "KDChartPalette.h"
00024 #include <QBrush>
00025 #include <QVector>
00026 
00027 #include <KDABLibFakes>
00028 
00029 using namespace KDChart;
00030 
00031 namespace {
00032 
00033     static Palette makeDefaultPalette() {
00034         Palette p;
00035 
00036         p.addBrush( Qt::red );
00037         p.addBrush( Qt::green );
00038         p.addBrush( Qt::blue );
00039         p.addBrush( Qt::cyan );
00040         p.addBrush( Qt::magenta );
00041         p.addBrush( Qt::yellow );
00042         p.addBrush( Qt::darkRed );
00043         p.addBrush( Qt::darkGreen );
00044         p.addBrush( Qt::darkBlue );
00045         p.addBrush( Qt::darkCyan );
00046         p.addBrush( Qt::darkMagenta );
00047         p.addBrush( Qt::darkYellow );
00048 
00049         return p;
00050     }
00051 
00052     static Palette makeSubduedPalette() {
00053         Palette p;
00054 
00055         p.addBrush( QColor( 0xe0,0x7f,0x70 ) );
00056         p.addBrush( QColor( 0xe2,0xa5,0x6f ) );
00057         p.addBrush( QColor( 0xe0,0xc9,0x70 ) );
00058         p.addBrush( QColor( 0xd1,0xe0,0x70 ) );
00059         p.addBrush( QColor( 0xac,0xe0,0x70 ) );
00060         p.addBrush( QColor( 0x86,0xe0,0x70 ) );
00061         p.addBrush( QColor( 0x70,0xe0,0x7f ) );
00062         p.addBrush( QColor( 0x70,0xe0,0xa4 ) );
00063         p.addBrush( QColor( 0x70,0xe0,0xc9 ) );
00064         p.addBrush( QColor( 0x70,0xd1,0xe0 ) );
00065         p.addBrush( QColor( 0x70,0xac,0xe0 ) );
00066         p.addBrush( QColor( 0x70,0x86,0xe0 ) );
00067         p.addBrush( QColor( 0x7f,0x70,0xe0 ) );
00068         p.addBrush( QColor( 0xa4,0x70,0xe0 ) );
00069         p.addBrush( QColor( 0xc9,0x70,0xe0 ) );
00070         p.addBrush( QColor( 0xe0,0x70,0xd1 ) );
00071         p.addBrush( QColor( 0xe0,0x70,0xac ) );
00072         p.addBrush( QColor( 0xe0,0x70,0x86 ) );
00073 
00074         return p;
00075     }
00076 
00077     static Palette makeRainbowPalette() {
00078         Palette p;
00079 
00080         p.addBrush( QColor(255,  0,196) );
00081         p.addBrush( QColor(255,  0, 96) );
00082         p.addBrush( QColor(255, 128,64) );
00083         p.addBrush( Qt::yellow );
00084         p.addBrush( Qt::green );
00085         p.addBrush( Qt::cyan );
00086         p.addBrush( QColor( 96, 96,255) );
00087         p.addBrush( QColor(160,  0,255) );
00088         for( int i = 8 ; i < 16 ; ++i )
00089             p.addBrush( p.getBrush(i-8).color().light(), i );
00090 
00091         return p;
00092     }
00093 
00094 }
00095 
00096 #define d d_func()
00097 
00098 class Palette::Private
00099 {
00100 public:
00101     explicit Private() {}
00102     ~Private() {}
00103 
00104     QVector<QBrush> brushes;
00105 };
00106 
00107 const Palette& Palette::defaultPalette()
00108 {
00109     static const Palette palette = makeDefaultPalette();
00110     return palette;
00111 }
00112 
00113 const Palette& Palette::subduedPalette()
00114 {
00115     static const Palette palette = makeSubduedPalette();
00116     return palette;
00117 }
00118 
00119 const Palette& Palette::rainbowPalette()
00120 {
00121     static const Palette palette = makeRainbowPalette();
00122     return palette;
00123 }
00124 
00125 Palette::Palette( QObject *parent )
00126   : QObject( parent ), _d( new Private )
00127 {
00128 
00129 }
00130 
00131 Palette::~Palette()
00132 {
00133     delete _d; _d = 0;
00134 }
00135 
00136 
00137 
00138 Palette::Palette( const Palette& r )
00139     : QObject(), _d( new Private( *r.d ) )
00140 {
00141 }
00142 
00143 Palette& Palette::operator=( const Palette& r )
00144 {
00145     Palette copy( r );
00146     copy.swap( *this );
00147 
00148     // emit changed() ?
00149     return *this;
00150 }
00151 
00152 bool Palette::isValid() const
00153 {
00154   return d->brushes.size() >= 1;
00155 }
00156 
00157 int Palette::size() const
00158 {
00159   return d->brushes.size();
00160 }
00161 
00162 void Palette::addBrush( const QBrush& brush, int position )
00163 {
00164   if ( position < 0 || position >= size() ) {
00165     d->brushes.append( brush );
00166   } else {
00167     d->brushes.insert( position, brush );
00168   }
00169   emit changed();
00170 }
00171 
00172 QBrush Palette::getBrush( int position ) const
00173 {
00174   if ( !isValid() ) return QBrush();
00175   return d->brushes.at( position % size() );
00176 }
00177 
00178 void Palette::removeBrush( int position )
00179 {
00180   if ( position < 0 || position >= size() ) return;
00181   d->brushes.remove( position );
00182   emit changed();
00183 }
00184