KChart

KChartPalette.cpp
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 #include "KChartPalette.h"
21 
22 #include "KChartMath_p.h"
23 
24 #include <QBrush>
25 #include <QVector>
26 
27 using namespace KChart;
28 
29 namespace {
30 
31  static Palette makeDefaultPalette() {
32  Palette p;
33 
34  p.addBrush( Qt::red );
35  p.addBrush( Qt::green );
36  p.addBrush( Qt::blue );
37  p.addBrush( Qt::cyan );
38  p.addBrush( Qt::magenta );
39  p.addBrush( Qt::yellow );
40  p.addBrush( Qt::darkRed );
46 
47  return p;
48  }
49 
50  static Palette makeSubduedPalette() {
51  Palette p;
52 
53  p.addBrush( QColor( 0xe0,0x7f,0x70 ) );
54  p.addBrush( QColor( 0xe2,0xa5,0x6f ) );
55  p.addBrush( QColor( 0xe0,0xc9,0x70 ) );
56  p.addBrush( QColor( 0xd1,0xe0,0x70 ) );
57  p.addBrush( QColor( 0xac,0xe0,0x70 ) );
58  p.addBrush( QColor( 0x86,0xe0,0x70 ) );
59  p.addBrush( QColor( 0x70,0xe0,0x7f ) );
60  p.addBrush( QColor( 0x70,0xe0,0xa4 ) );
61  p.addBrush( QColor( 0x70,0xe0,0xc9 ) );
62  p.addBrush( QColor( 0x70,0xd1,0xe0 ) );
63  p.addBrush( QColor( 0x70,0xac,0xe0 ) );
64  p.addBrush( QColor( 0x70,0x86,0xe0 ) );
65  p.addBrush( QColor( 0x7f,0x70,0xe0 ) );
66  p.addBrush( QColor( 0xa4,0x70,0xe0 ) );
67  p.addBrush( QColor( 0xc9,0x70,0xe0 ) );
68  p.addBrush( QColor( 0xe0,0x70,0xd1 ) );
69  p.addBrush( QColor( 0xe0,0x70,0xac ) );
70  p.addBrush( QColor( 0xe0,0x70,0x86 ) );
71 
72  return p;
73  }
74 
75  static Palette makeRainbowPalette() {
76  Palette p;
77 
78  p.addBrush( QColor(255, 0,196) );
79  p.addBrush( QColor(255, 0, 96) );
80  p.addBrush( QColor(255, 128,64) );
81  p.addBrush( Qt::yellow );
82  p.addBrush( Qt::green );
83  p.addBrush( Qt::cyan );
84  p.addBrush( QColor( 96, 96,255) );
85  p.addBrush( QColor(160, 0,255) );
86  for ( int i = 8 ; i < 16 ; ++i ) {
87  p.addBrush( p.getBrush( i - 8 ).color().lighter(), i );
88  }
89  return p;
90  }
91 
92 }
93 
94 #define d d_func()
95 
96 class Q_DECL_HIDDEN Palette::Private
97 {
98 public:
99  explicit Private() {}
100  ~Private() {}
101 
102  QVector<QBrush> brushes;
103 };
104 
106 {
107  static const Palette palette = makeDefaultPalette();
108  return palette;
109 }
110 
111 const Palette& Palette::subduedPalette()
112 {
113  static const Palette palette = makeSubduedPalette();
114  return palette;
115 }
116 
117 const Palette& Palette::rainbowPalette()
118 {
119  static const Palette palette = makeRainbowPalette();
120  return palette;
121 }
122 
123 Palette::Palette( QObject *parent )
124  : QObject( parent ), _d( new Private )
125 {
126 
127 }
128 
129 Palette::~Palette()
130 {
131  delete _d; _d = nullptr;
132 }
133 
134 
135 
136 Palette::Palette( const Palette& r )
137  : QObject(), _d( new Private( *r.d ) )
138 {
139 }
140 
141 Palette& Palette::operator=( const Palette& r )
142 {
143  Palette copy( r );
144  copy.swap( *this );
145 
146  // emit changed() ?
147  return *this;
148 }
149 
150 bool Palette::isValid() const
151 {
152  return d->brushes.size() >= 1;
153 }
154 
155 int Palette::size() const
156 {
157  return d->brushes.size();
158 }
159 
160 void Palette::addBrush( const QBrush& brush, int position )
161 {
162  if ( position < 0 || position >= size() ) {
163  d->brushes.append( brush );
164  } else {
165  d->brushes.insert( position, brush );
166  }
167  emit changed();
168 }
169 
170 QBrush Palette::getBrush( int position ) const
171 {
172  if ( !isValid() ) return QBrush();
173  return d->brushes.at( position % size() );
174 }
175 
176 void Palette::removeBrush( int position )
177 {
178  if ( position < 0 || position >= size() ) return;
179  d->brushes.remove( position );
180  emit changed();
181 }
182 
int size() const
QBrush getBrush(int position) const
Query the palette for a brush at the specified position.
A Palette is a set of brushes (or colors) to be used for painting data sets.
Definition: KChartPalette.h:46
void removeBrush(int position)
Remove the brush at position position, if there is one.
void addBrush(const QBrush &brush, int position=-1)
Adds brush to the palette.
const QColor & color() const const
bool isValid() const
Class only listed here to document inheritance of some KChart classes.
static const Palette & defaultPalette()
Provide access to the three builtin palettes, one with standard bright colors, one with more subdued ...
QColor lighter(int factor) const const
Global namespace.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Dec 3 2020 22:37:14 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.