1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __KCHART_PALETTE_H__
21 #define __KCHART_PALETTE_H__
22 
23 #include <QObject>
24 #include <QBrush>
25 #include "KChartGlobal.h"
26 
27 namespace KChart {
28 
29   /**
30    * \brief A Palette is a set of brushes (or colors) to be used
31    * for painting data sets.
32    *
33    * The palette class encapsulates a colletion of brushes, which in
34    * the simplest case are colors, to be used for painting a series of
35    * data sets. When asked for the m-th color, a palette of size n will
36    * wrap around and thus cycle through the available colors.
37    *
38    * Three builtin palettes are provided for convenience, one with a default
39    * set of colors, one with a subdued color selection, one with rainbow
40    * colors.
41    *
42    * When a palette changes, it emits a changed() signal. Hook up to it, if
43    * you want to repaint when the color selection changes.
44    */
45 
46 class KCHART_EXPORT Palette: public QObject
47 {
48     Q_OBJECT
49 public:
50     explicit Palette( QObject *parent  = nullptr );
51     Palette( const Palette& );
52     Palette &operator= ( const Palette & );
53 
54     ~Palette();
55 
56     /** Provide access to the three builtin palettes, one with standard bright
57      * colors, one with more subdued colors, and one with rainbow colors.  */
58     static const Palette& defaultPalette();
59     static const Palette& subduedPalette();
60     static const Palette& rainbowPalette();
61 
62     /** @return whether this represents a valid palette. For a palette to be
63      * valid it needs to have at least one brush associated. */
64     bool isValid() const;
65 
66     /** @return the number of brushed in the palette.  */
67     int size() const;
68 
69     /** Adds \a brush to the palette. If no \a position is specified, the
70      * brush is appended.
71      */
72     void addBrush( const QBrush & brush, int position = -1 );
73 
74     /**
75      * Query the palette for a brush at the specified position. If the
76      * position exceeds the size of the palette, it wraps around.
77      */
78     QBrush getBrush( int position ) const;
79 
80     /** Remove the brush at position \a position, if there is one.  */
81     void removeBrush( int position );
82 
83 Q_SIGNALS:
84     /**  Emitted whenever the palette changes. Views listen to this and repaints. */
85     void changed();
86 
87 private:
88     KCHART_DECLARE_PRIVATE_BASE_VALUE( Palette )
89 };
90 
91 }
92 
93 KCHART_DECLARE_SWAP_SPECIALISATION( KChart::Palette )
94 
95 #endif
96