KD Chart 2  [rev.2.7]
KDChartPalette.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2020 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #include "KDChartPalette.h"
24 #include <QBrush>
25 #include <QVector>
26 
27 #include <KDABLibFakes>
28 
29 using namespace KDChart;
30 
31 namespace {
32 
33  static Palette makeDefaultPalette() {
34  Palette p;
35 
36  p.addBrush( Qt::red );
37  p.addBrush( Qt::green );
38  p.addBrush( Qt::blue );
39  p.addBrush( Qt::cyan );
40  p.addBrush( Qt::magenta );
41  p.addBrush( Qt::yellow );
42  p.addBrush( Qt::darkRed );
43  p.addBrush( Qt::darkGreen );
44  p.addBrush( Qt::darkBlue );
45  p.addBrush( Qt::darkCyan );
46  p.addBrush( Qt::darkMagenta );
47  p.addBrush( Qt::darkYellow );
48 
49  return p;
50  }
51 
52  static Palette makeSubduedPalette() {
53  Palette p;
54 
55  p.addBrush( QColor( 0xe0,0x7f,0x70 ) );
56  p.addBrush( QColor( 0xe2,0xa5,0x6f ) );
57  p.addBrush( QColor( 0xe0,0xc9,0x70 ) );
58  p.addBrush( QColor( 0xd1,0xe0,0x70 ) );
59  p.addBrush( QColor( 0xac,0xe0,0x70 ) );
60  p.addBrush( QColor( 0x86,0xe0,0x70 ) );
61  p.addBrush( QColor( 0x70,0xe0,0x7f ) );
62  p.addBrush( QColor( 0x70,0xe0,0xa4 ) );
63  p.addBrush( QColor( 0x70,0xe0,0xc9 ) );
64  p.addBrush( QColor( 0x70,0xd1,0xe0 ) );
65  p.addBrush( QColor( 0x70,0xac,0xe0 ) );
66  p.addBrush( QColor( 0x70,0x86,0xe0 ) );
67  p.addBrush( QColor( 0x7f,0x70,0xe0 ) );
68  p.addBrush( QColor( 0xa4,0x70,0xe0 ) );
69  p.addBrush( QColor( 0xc9,0x70,0xe0 ) );
70  p.addBrush( QColor( 0xe0,0x70,0xd1 ) );
71  p.addBrush( QColor( 0xe0,0x70,0xac ) );
72  p.addBrush( QColor( 0xe0,0x70,0x86 ) );
73 
74  return p;
75  }
76 
77  static Palette makeRainbowPalette() {
78  Palette p;
79 
80  p.addBrush( QColor(255, 0,196) );
81  p.addBrush( QColor(255, 0, 96) );
82  p.addBrush( QColor(255, 128,64) );
83  p.addBrush( Qt::yellow );
84  p.addBrush( Qt::green );
85  p.addBrush( Qt::cyan );
86  p.addBrush( QColor( 96, 96,255) );
87  p.addBrush( QColor(160, 0,255) );
88  for ( int i = 8 ; i < 16 ; ++i ) {
89  p.addBrush( p.getBrush( i - 8 ).color().light(), i );
90  }
91  return p;
92  }
93 
94 }
95 
96 #define d d_func()
97 
98 class Palette::Private
99 {
100 public:
101  explicit Private() {}
102  ~Private() {}
103 
104  QVector<QBrush> brushes;
105 };
106 
108 {
109  static const Palette palette = makeDefaultPalette();
110  return palette;
111 }
112 
114 {
115  static const Palette palette = makeSubduedPalette();
116  return palette;
117 }
118 
120 {
121  static const Palette palette = makeRainbowPalette();
122  return palette;
123 }
124 
126  : QObject( parent ), _d( new Private )
127 {
128 
129 }
130 
132 {
133  delete _d; _d = 0;
134 }
135 
136 
137 
139  : QObject(), _d( new Private( *r.d ) )
140 {
141 }
142 
144 {
145  Palette copy( r );
146  copy.swap( *this );
147 
148  // emit changed() ?
149  return *this;
150 }
151 
152 bool Palette::isValid() const
153 {
154  return d->brushes.size() >= 1;
155 }
156 
157 int Palette::size() const
158 {
159  return d->brushes.size();
160 }
161 
162 void Palette::addBrush( const QBrush& brush, int position )
163 {
164  if ( position < 0 || position >= size() ) {
165  d->brushes.append( brush );
166  } else {
167  d->brushes.insert( position, brush );
168  }
169  emit changed();
170 }
171 
172 QBrush Palette::getBrush( int position ) const
173 {
174  if ( !isValid() ) return QBrush();
175  return d->brushes.at( position % size() );
176 }
177 
178 void Palette::removeBrush( int position )
179 {
180  if ( position < 0 || position >= size() ) return;
181  d->brushes.remove( position );
182  emit changed();
183 }
184 
A Palette is a set of brushes (or colors) to be used for painting data sets.
void addBrush(const QBrush &brush, int position=-1)
Adds brush to the palette.
static const Palette & rainbowPalette()
void removeBrush(int position)
Remove the brush at position position, if there is one.
static const Palette & defaultPalette()
Provide access to the three builtin palettes, one with standard bright colors, one with more subdued ...
void changed()
Emitted whenever the palette changes.
QBrush getBrush(int position) const
Query the palette for a brush at the specified position.
#define d
Palette(QObject *parent=0)
Palette & operator=(const Palette &)
Class only listed here to document inheritance of some KDChart classes.
static const Palette & subduedPalette()
bool isValid() const

Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/

https://www.kdab.com/development-resources/qt-tools/kd-chart/