File indexing completed on 2024-05-12 04:20:36

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #ifndef __KCHART_PALETTE_H__
0010 #define __KCHART_PALETTE_H__
0011 
0012 #include <QObject>
0013 #include <QBrush>
0014 #include "KChartGlobal.h"
0015 
0016 namespace KChart {
0017 
0018   /**
0019    * \brief A Palette is a set of brushes (or colors) to be used
0020    * for painting data sets.
0021    *
0022    * The palette class encapsulates a collection of brushes, which in
0023    * the simplest case are colors, to be used for painting a series of
0024    * data sets. When asked for the m-th color, a palette of size n will
0025    * wrap around and thus cycle through the available colors.
0026    *
0027    * Three builtin palettes are provided for convenience, one with a default
0028    * set of colors, one with a subdued color selection, one with rainbow
0029    * colors.
0030    *
0031    * When a palette changes, it emits a changed() signal. Hook up to it, if
0032    * you want to repaint when the color selection changes.
0033    */
0034 
0035 class KCHART_EXPORT Palette: public QObject
0036 {
0037     Q_OBJECT
0038 public:
0039     explicit Palette( QObject *parent  = nullptr );
0040     Palette( const Palette& );
0041     Palette &operator= ( const Palette & );
0042 
0043     ~Palette() override;
0044 
0045     /** Provide access to the three builtin palettes, one with standard bright
0046      * colors, one with more subdued colors, and one with rainbow colors.  */
0047     static const Palette& defaultPalette();
0048     static const Palette& subduedPalette();
0049     static const Palette& rainbowPalette();
0050 
0051     /** @return whether this represents a valid palette. For a palette to be
0052      * valid it needs to have at least one brush associated. */
0053     bool isValid() const;
0054 
0055     /** @return the number of brushed in the palette.  */
0056     int size() const;
0057 
0058     /** Adds \a brush to the palette. If no \a position is specified, the
0059      * brush is appended.
0060      */
0061     void addBrush( const QBrush & brush, int position = -1 );
0062 
0063     /**
0064      * Query the palette for a brush at the specified position. If the
0065      * position exceeds the size of the palette, it wraps around.
0066      */
0067     QBrush getBrush( int position ) const;
0068 
0069     /** Remove the brush at position \a position, if there is one.  */
0070     void removeBrush( int position );
0071 
0072 Q_SIGNALS:
0073     /**  Emitted whenever the palette changes. Views listen to this and repaints. */
0074     void changed();
0075 
0076 private:
0077     KCHART_DECLARE_PRIVATE_BASE_VALUE( Palette )
0078 };
0079 
0080 }
0081 
0082 KCHART_DECLARE_SWAP_SPECIALISATION( KChart::Palette )
0083 
0084 #endif