KD Chart API Documentation  3.1
KDChartPalette.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Chart library.
4 **
5 ** SPDX-FileCopyrightText: 2001 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
11 #include "KDChartPalette.h"
12 #include <QBrush>
13 #include <QVector>
14 
15 #include <KDABLibFakes>
16 
17 using namespace KDChart;
18 
19 namespace {
20 
21 static Palette makeDefaultPalette()
22 {
23  Palette p;
24 
25  p.addBrush(Qt::red);
26  p.addBrush(Qt::green);
27  p.addBrush(Qt::blue);
28  p.addBrush(Qt::cyan);
29  p.addBrush(Qt::magenta);
30  p.addBrush(Qt::yellow);
31  p.addBrush(Qt::darkRed);
32  p.addBrush(Qt::darkGreen);
33  p.addBrush(Qt::darkBlue);
34  p.addBrush(Qt::darkCyan);
35  p.addBrush(Qt::darkMagenta);
36  p.addBrush(Qt::darkYellow);
37 
38  return p;
39 }
40 
41 static Palette makeSubduedPalette()
42 {
43  Palette p;
44 
45  p.addBrush(QColor(0xe0, 0x7f, 0x70));
46  p.addBrush(QColor(0xe2, 0xa5, 0x6f));
47  p.addBrush(QColor(0xe0, 0xc9, 0x70));
48  p.addBrush(QColor(0xd1, 0xe0, 0x70));
49  p.addBrush(QColor(0xac, 0xe0, 0x70));
50  p.addBrush(QColor(0x86, 0xe0, 0x70));
51  p.addBrush(QColor(0x70, 0xe0, 0x7f));
52  p.addBrush(QColor(0x70, 0xe0, 0xa4));
53  p.addBrush(QColor(0x70, 0xe0, 0xc9));
54  p.addBrush(QColor(0x70, 0xd1, 0xe0));
55  p.addBrush(QColor(0x70, 0xac, 0xe0));
56  p.addBrush(QColor(0x70, 0x86, 0xe0));
57  p.addBrush(QColor(0x7f, 0x70, 0xe0));
58  p.addBrush(QColor(0xa4, 0x70, 0xe0));
59  p.addBrush(QColor(0xc9, 0x70, 0xe0));
60  p.addBrush(QColor(0xe0, 0x70, 0xd1));
61  p.addBrush(QColor(0xe0, 0x70, 0xac));
62  p.addBrush(QColor(0xe0, 0x70, 0x86));
63 
64  return p;
65 }
66 
67 static Palette makeRainbowPalette()
68 {
69  Palette p;
70 
71  p.addBrush(QColor(255, 0, 196));
72  p.addBrush(QColor(255, 0, 96));
73  p.addBrush(QColor(255, 128, 64));
74  p.addBrush(Qt::yellow);
75  p.addBrush(Qt::green);
76  p.addBrush(Qt::cyan);
77  p.addBrush(QColor(96, 96, 255));
78  p.addBrush(QColor(160, 0, 255));
79  for (int i = 8; i < 16; ++i) {
80  p.addBrush(p.getBrush(i - 8).color().lighter(), i);
81  }
82  return p;
83 }
84 }
85 
86 #define d d_func()
87 
88 class Palette::Private
89 {
90 public:
91  explicit Private()
92  {
93  }
94  ~Private()
95  {
96  }
97 
98  QVector<QBrush> brushes;
99 };
100 
102 {
103  static const Palette palette = makeDefaultPalette();
104  return palette;
105 }
106 
108 {
109  static const Palette palette = makeSubduedPalette();
110  return palette;
111 }
112 
114 {
115  static const Palette palette = makeRainbowPalette();
116  return palette;
117 }
118 
119 Palette::Palette(QObject *parent)
120  : QObject(parent)
121  , _d(new Private)
122 {
123 }
124 
126 {
127  delete _d;
128  _d = nullptr;
129 }
130 
132  : QObject()
133  , _d(new Private(*r.d))
134 {
135 }
136 
138 {
139  Palette copy(r);
140  copy.swap(*this);
141 
142  // emit changed() ?
143  return *this;
144 }
145 
146 bool Palette::isValid() const
147 {
148  return d->brushes.size() >= 1;
149 }
150 
151 int Palette::size() const
152 {
153  return d->brushes.size();
154 }
155 
156 void Palette::addBrush(const QBrush &brush, int position)
157 {
158  if (position < 0 || position >= size()) {
159  d->brushes.append(brush);
160  } else {
161  d->brushes.insert(position, brush);
162  }
163  Q_EMIT changed();
164 }
165 
166 QBrush Palette::getBrush(int position) const
167 {
168  if (!isValid())
169  return QBrush();
170  return d->brushes.at(position % size());
171 }
172 
173 void Palette::removeBrush(int position)
174 {
175  if (position < 0 || position >= size())
176  return;
177  d->brushes.remove(position);
178  Q_EMIT changed();
179 }
#define d
A Palette is a set of brushes (or colors) to be used for painting data sets.
QBrush getBrush(int position) const
void removeBrush(int position)
bool isValid() const
void addBrush(const QBrush &brush, int position=-1)
Palette & operator=(const Palette &)
Palette(QObject *parent=nullptr)
static const Palette & subduedPalette()
static const Palette & defaultPalette()
static const Palette & rainbowPalette()

© 2001 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/
Generated by doxygen 1.9.1