KD Chart 2: KDChartPalette.cpp Source File

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# KDChartPalette.cpp

Go to the documentation of this file.

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