KChart

KChartPalette.cpp
1/*
2 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3 *
4 * This file is part of the KD Chart library.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "KChartPalette.h"
10
11#include "KChartMath_p.h"
12
13#include <QBrush>
14#include <QVector>
15
16using namespace KChart;
17
18namespace {
19
20 static Palette makeDefaultPalette() {
21 Palette p;
22
23 p.addBrush( Qt::red );
24 p.addBrush( Qt::green );
25 p.addBrush( Qt::blue );
26 p.addBrush( Qt::cyan );
27 p.addBrush( Qt::magenta );
28 p.addBrush( Qt::yellow );
29 p.addBrush( Qt::darkRed );
30 p.addBrush( Qt::darkGreen );
31 p.addBrush( Qt::darkBlue );
32 p.addBrush( Qt::darkCyan );
33 p.addBrush( Qt::darkMagenta );
34 p.addBrush( Qt::darkYellow );
35
36 return p;
37 }
38
39 static Palette makeSubduedPalette() {
40 Palette p;
41
42 p.addBrush( QColor( 0xe0,0x7f,0x70 ) );
43 p.addBrush( QColor( 0xe2,0xa5,0x6f ) );
44 p.addBrush( QColor( 0xe0,0xc9,0x70 ) );
45 p.addBrush( QColor( 0xd1,0xe0,0x70 ) );
46 p.addBrush( QColor( 0xac,0xe0,0x70 ) );
47 p.addBrush( QColor( 0x86,0xe0,0x70 ) );
48 p.addBrush( QColor( 0x70,0xe0,0x7f ) );
49 p.addBrush( QColor( 0x70,0xe0,0xa4 ) );
50 p.addBrush( QColor( 0x70,0xe0,0xc9 ) );
51 p.addBrush( QColor( 0x70,0xd1,0xe0 ) );
52 p.addBrush( QColor( 0x70,0xac,0xe0 ) );
53 p.addBrush( QColor( 0x70,0x86,0xe0 ) );
54 p.addBrush( QColor( 0x7f,0x70,0xe0 ) );
55 p.addBrush( QColor( 0xa4,0x70,0xe0 ) );
56 p.addBrush( QColor( 0xc9,0x70,0xe0 ) );
57 p.addBrush( QColor( 0xe0,0x70,0xd1 ) );
58 p.addBrush( QColor( 0xe0,0x70,0xac ) );
59 p.addBrush( QColor( 0xe0,0x70,0x86 ) );
60
61 return p;
62 }
63
64 static Palette makeRainbowPalette() {
65 Palette p;
66
67 p.addBrush( QColor(255, 0,196) );
68 p.addBrush( QColor(255, 0, 96) );
69 p.addBrush( QColor(255, 128,64) );
70 p.addBrush( Qt::yellow );
71 p.addBrush( Qt::green );
72 p.addBrush( Qt::cyan );
73 p.addBrush( QColor( 96, 96,255) );
74 p.addBrush( QColor(160, 0,255) );
75 for ( int i = 8 ; i < 16 ; ++i ) {
76 p.addBrush( p.getBrush( i - 8 ).color().lighter(), i );
77 }
78 return p;
79 }
80
81}
82
83#define d d_func()
84
85class Q_DECL_HIDDEN Palette::Private
86{
87public:
88 explicit Private() {}
89 ~Private() {}
90
91 QVector<QBrush> brushes;
92};
93
94const Palette& Palette::defaultPalette()
95{
96 static const Palette palette = makeDefaultPalette();
97 return palette;
98}
99
100const Palette& Palette::subduedPalette()
101{
102 static const Palette palette = makeSubduedPalette();
103 return palette;
104}
105
106const Palette& Palette::rainbowPalette()
107{
108 static const Palette palette = makeRainbowPalette();
109 return palette;
110}
111
112Palette::Palette( QObject *parent )
113 : QObject( parent ), _d( new Private )
114{
115
116}
117
118Palette::~Palette()
119{
120 delete _d; _d = nullptr;
121}
122
123
124
125Palette::Palette( const Palette& r )
126 : QObject(), _d( new Private( *r.d ) )
127{
128}
129
130Palette& Palette::operator=( const Palette& r )
131{
132 Palette copy( r );
133 copy.swap( *this );
134
135 // Q_EMIT changed() ?
136 return *this;
137}
138
140{
141 return d->brushes.size() >= 1;
142}
143
144int Palette::size() const
145{
146 return d->brushes.size();
147}
148
149void Palette::addBrush( const QBrush& brush, int position )
150{
151 if ( position < 0 || position >= size() ) {
152 d->brushes.append( brush );
153 } else {
154 d->brushes.insert( position, brush );
155 }
156 Q_EMIT changed();
157}
158
159QBrush Palette::getBrush( int position ) const
160{
161 if ( !isValid() ) return QBrush();
162 return d->brushes.at( position % size() );
163}
164
165void Palette::removeBrush( int position )
166{
167 if ( position < 0 || position >= size() ) return;
168 d->brushes.remove( position );
169 Q_EMIT changed();
170}
171
A Palette is a set of brushes (or colors) to be used for painting data sets.
QBrush getBrush(int position) const
Query the palette for a brush at the specified position.
void removeBrush(int position)
Remove the brush at position position, if there is one.
bool isValid() const
void addBrush(const QBrush &brush, int position=-1)
Adds brush to the palette.
void changed()
Emitted whenever the palette changes.
KIOCORE_EXPORT CopyJob * copy(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Wed Nov 6 2024 12:05:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.