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 #ifndef __KCHART_PALETTE_H__
0021 #define __KCHART_PALETTE_H__
0022 
0023 #include <QObject>
0024 #include <QBrush>
0025 #include "KChartGlobal.h"
0026 
0027 namespace KChart {
0028 
0029   /**
0030    * \brief A Palette is a set of brushes (or colors) to be used
0031    * for painting data sets.
0032    *
0033    * The palette class encapsulates a colletion of brushes, which in
0034    * the simplest case are colors, to be used for painting a series of
0035    * data sets. When asked for the m-th color, a palette of size n will
0036    * wrap around and thus cycle through the available colors.
0037    *
0038    * Three builtin palettes are provided for convenience, one with a default
0039    * set of colors, one with a subdued color selection, one with rainbow
0040    * colors.
0041    *
0042    * When a palette changes, it emits a changed() signal. Hook up to it, if
0043    * you want to repaint when the color selection changes.
0044    */
0045 
0046 class KCHART_EXPORT Palette: public QObject
0047 {
0048     Q_OBJECT
0049 public:
0050     explicit Palette( QObject *parent  = nullptr );
0051     Palette( const Palette& );
0052     Palette &operator= ( const Palette & );
0053 
0054     ~Palette();
0055 
0056     /** Provide access to the three builtin palettes, one with standard bright
0057      * colors, one with more subdued colors, and one with rainbow colors.  */
0058     static const Palette& defaultPalette();
0059     static const Palette& subduedPalette();
0060     static const Palette& rainbowPalette();
0061 
0062     /** @return whether this represents a valid palette. For a palette to be
0063      * valid it needs to have at least one brush associated. */
0064     bool isValid() const;
0065 
0066     /** @return the number of brushed in the palette.  */
0067     int size() const;
0068 
0069     /** Adds \a brush to the palette. If no \a position is specified, the
0070      * brush is appended.
0071      */
0072     void addBrush( const QBrush & brush, int position = -1 );
0073 
0074     /**
0075      * Query the palette for a brush at the specified position. If the
0076      * position exceeds the size of the palette, it wraps around.
0077      */
0078     QBrush getBrush( int position ) const;
0079 
0080     /** Remove the brush at position \a position, if there is one.  */
0081     void removeBrush( int position );
0082 
0083 Q_SIGNALS:
0084     /**  Emitted whenever the palette changes. Views listen to this and repaints. */
0085     void changed();
0086 
0087 private:
0088     KCHART_DECLARE_PRIVATE_BASE_VALUE( Palette )
0089 };
0090 
0091 }
0092 
0093 KCHART_DECLARE_SWAP_SPECIALISATION( KChart::Palette )
0094 
0095 #endif