##// END OF EJS Templates
Enabled color themes again
Tero Ahola -
r91:a70d6fec8ca6
parent child
Show More
@@ -1,395 +1,394
1 1 #include "qchart.h"
2 2 #include "qchartseries.h"
3 3 #include "qscatterseries.h"
4 4 #include "qscatterseries_p.h"
5 5 #include "qpieseries.h"
6 6 #include "qxychartseries.h"
7 7 #include "qchartaxis.h"
8 8 #include "barchartseries.h"
9 9 #include "bargroup.h"
10 10
11 11 #include "xylinechartitem_p.h"
12 12 #include "plotdomain_p.h"
13 13 #include "axisitem_p.h"
14 14 #include <QGraphicsScene>
15 15 #include <QDebug>
16 16
17 17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18 18
19 19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
20 20 m_backgroundItem(0),
21 21 m_titleItem(0),
22 22 m_axisXItem(new AxisItem(AxisItem::X_AXIS,this)),
23 23 m_plotDataIndex(0),
24 24 m_marginSize(0)
25 25 {
26 26 // TODO: the default theme?
27 //setTheme(QChart::ChartThemeVanilla);
27 setTheme(QChart::ChartThemeDefault);
28 28
29 29 PlotDomain domain;
30 30 m_plotDomainList<<domain;
31 31 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
32 32 m_chartItems<<m_axisXItem;
33 33 m_chartItems<<m_axisYItem.at(0);
34 34 }
35 35
36 36 QChart::~QChart(){}
37 37
38 38 QRectF QChart::boundingRect() const
39 39 {
40 40 return m_rect;
41 41 }
42 42
43 43 void QChart::addSeries(QChartSeries* series)
44 44 {
45 45 // TODO: we should check the series not already added
46 46
47 47 m_chartSeries << series;
48 48
49 49 switch(series->type())
50 50 {
51 51 case QChartSeries::SeriesTypeLine: {
52 52
53 53 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
54 54 // Use color defined by theme in case the series does not define a custom color
55 55
56 56 if (!xyseries->pen().color().isValid() && m_themeColors.count()) //TODO: wtf
57 57 xyseries->setPen(nextColor());
58 58
59 59 m_plotDataIndex = 0 ;
60 60 m_plotDomainList.resize(1);
61 61
62 62 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
63 63
64 64 for (int i = 0 ; i < xyseries->count() ; i++)
65 65 {
66 66 qreal x = xyseries->x(i);
67 67 qreal y = xyseries->y(i);
68 68 domain.m_minX = qMin(domain.m_minX,x);
69 69 domain.m_minY = qMin(domain.m_minY,y);
70 70 domain.m_maxX = qMax(domain.m_maxX,x);
71 71 domain.m_maxY = qMax(domain.m_maxY,y);
72 72 }
73 73
74 74 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
75 75 m_chartItems<<item;
76 76
77 77 foreach(ChartItem* i ,m_chartItems)
78 78 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
79 79
80 80 break;
81 81 }
82 82 case QChartSeries::SeriesTypeBar: {
83 83
84 84 qDebug() << "barSeries added";
85 85 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
86 86 BarGroup* barGroup = new BarGroup(*barSeries,this);
87 87
88 88 // Add some fugly colors for 5 fist series...
89 89 barGroup->addColor(QColor(255,0,0,128));
90 90 barGroup->addColor(QColor(255,255,0,128));
91 91 barGroup->addColor(QColor(0,255,0,128));
92 92 barGroup->addColor(QColor(0,0,255,128));
93 93 barGroup->addColor(QColor(255,128,0,128));
94 94
95 95 m_chartItems<<barGroup;
96 96 childItems().append(barGroup);
97 97 break;
98 98 }
99 99 case QChartSeries::SeriesTypeScatter: {
100 100 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
101 101 scatterSeries->d->setParentItem(this);
102 102 // Set pre-defined colors in case the series has no colors defined
103 103 if (!scatterSeries->markerColor().isValid())
104 104 scatterSeries->setMarkerColor(nextColor());
105 105 connect(this, SIGNAL(sizeChanged(QRectF)),
106 106 scatterSeries, SLOT(chartSizeChanged(QRectF)));
107 107 // QColor nextColor = m_themeColors.takeFirst();
108 108 // nextColor.setAlpha(150); // TODO: default opacity?
109 109 // scatterSeries->setMarkerColor(nextColor);
110 110 break;
111 111 }
112 112 case QChartSeries::SeriesTypePie: {
113 113 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
114 114 for (int i(0); i < pieSeries->sliceCount(); i++) {
115 115 if (!pieSeries->sliceColor(i).isValid())
116 116 pieSeries->setSliceColor(i, nextColor());
117 117 }
118 118 connect(this, SIGNAL(sizeChanged(QRectF)),
119 119 pieSeries, SLOT(chartSizeChanged(QRectF)));
120 120
121 121 // Set pre-defined colors in case the series has no colors defined
122 122 // TODO: how to define the color for all the slices of a pie?
123 123 // for (int (i); i < pieSeries.sliceCount(); i++)
124 124 break;
125 125 }
126 126 }
127 127 }
128 128
129 129 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
130 130 {
131 131 // TODO: support also other types; not only scatter and pie
132 132
133 133 QChartSeries *series(0);
134 134
135 135 switch (type) {
136 136 case QChartSeries::SeriesTypeLine: {
137 137 series = QXYChartSeries::create();
138 138 break;
139 139 }
140 140 case QChartSeries::SeriesTypeBar: {
141 141 series = new BarChartSeries(this);
142 142 break;
143 143 }
144 144 case QChartSeries::SeriesTypeScatter: {
145 145 series = new QScatterSeries(this);
146 146 break;
147 147 }
148 148 case QChartSeries::SeriesTypePie: {
149 149 series = new QPieSeries(this);
150 150 break;
151 151 }
152 152 default:
153 153 Q_ASSERT(false);
154 154 break;
155 155 }
156 156
157 157 addSeries(series);
158 158 return series;
159 159 }
160 160
161 161 void QChart::setSize(const QSize& size)
162 162 {
163 163 m_rect = QRect(QPoint(0,0),size);
164 164 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
165 165
166 166 //recaculate title
167 167 if(m_titleItem){
168 168 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
169 169 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
170 170
171 171 }
172 172
173 173 //recalculate background gradient
174 174 if(m_backgroundItem){
175 175 m_backgroundItem->setRect(rect);
176 176 if(m_bacgroundOrinetation==HorizonatlGradientOrientation)
177 177 m_backgroundGradient.setFinalStop(m_backgroundItem->rect().width(),0);
178 178 else
179 179 m_backgroundGradient.setFinalStop(0,m_backgroundItem->rect().height());
180 180
181 181 m_backgroundItem->setBrush(m_backgroundGradient);
182 182 }
183 183
184 184 //resize elements
185 185 foreach (ChartItem* item ,m_chartItems) {
186 186 item->setPos(rect.topLeft());
187 187 item->setSize(rect.size());
188 188
189 189 }
190 190 // TODO: TTD for setting scale
191 191 //emit scaleChanged(100, 100);
192 192 // TODO: calculate the origo
193 193 // TODO: not sure if emitting a signal here is the best from performance point of view
194 194 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
195 195
196 196 update();
197 197 }
198 198
199 199 void QChart::setBackground(const QColor& startColor, const QColor& endColor, GradientOrientation orientation)
200 200 {
201 201
202 202 if(!m_backgroundItem){
203 203 m_backgroundItem = new QGraphicsRectItem(this);
204 204 m_backgroundItem->setZValue(-1);
205 205 }
206 206
207 207 m_bacgroundOrinetation = orientation;
208 208 m_backgroundGradient.setColorAt( 0.0, startColor);
209 209 m_backgroundGradient.setColorAt( 1.0, endColor);
210 210 m_backgroundGradient.setStart(0,0);
211 211
212 212 if(orientation == VerticalGradientOrientation){
213 213 m_backgroundGradient.setFinalStop(0,m_rect.height());
214 214 }else{
215 215 m_backgroundGradient.setFinalStop(m_rect.width(),0);
216 216 }
217 217
218 218 m_backgroundItem->setBrush(m_backgroundGradient);
219 219 m_backgroundItem->setPen(Qt::NoPen);
220 220 m_backgroundItem->update();
221 221 }
222 222
223 223 void QChart::setTitle(const QString& title,const QFont& font)
224 224 {
225 225 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
226 226 m_titleItem->setPlainText(title);
227 227 m_titleItem->setFont(font);
228 228 }
229 229
230 230 int QChart::margin() const
231 231 {
232 232 return m_marginSize;
233 233 }
234 234
235 235 void QChart::setMargin(int margin)
236 236 {
237 237 m_marginSize = margin;
238 238 }
239 239
240 240 void QChart::setTheme(QChart::ChartThemeId theme)
241 241 {
242 242 // if (theme != m_currentTheme) {
243 243 m_themeColors.clear();
244 244
245 245 // TODO: define color themes
246 246 switch (theme) {
247 247 case QChart::ChartThemeDefault:
248 248 // TODO: define the default theme based on the OS
249 // For now we just fallthrough to "vanilla"
249 m_themeColors.append(QColor(QRgb(0xff000000)));
250 m_themeColors.append(QColor(QRgb(0xff707070)));
251 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
252 break;
250 253 case QChart::ChartThemeVanilla:
251 254 m_themeColors.append(QColor(217, 197, 116));
252 255 m_themeColors.append(QColor(214, 168, 150));
253 256 m_themeColors.append(QColor(160, 160, 113));
254 257 m_themeColors.append(QColor(210, 210, 52));
255 258 m_themeColors.append(QColor(136, 114, 58));
256 259
257 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xff9d844d)));
258 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
260 setBackground(QColor(QRgb(0xff9d844d)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
259 261 break;
260 262 case QChart::ChartThemeIcy:
261 263 m_themeColors.append(QColor(0, 3, 165));
262 264 m_themeColors.append(QColor(49, 52, 123));
263 265 m_themeColors.append(QColor(71, 114, 187));
264 266 m_themeColors.append(QColor(48, 97, 87));
265 267 m_themeColors.append(QColor(19, 71, 90));
266 268 m_themeColors.append(QColor(110, 70, 228));
267 269
268 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffe4ffff)));
269 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffe4ffff)));
270 setBackground(QColor(QRgb(0xffe4ffff)), QColor(QRgb(0xffe4ffff)), VerticalGradientOrientation);
270 271 break;
271 272 case QChart::ChartThemeGrayscale:
272 273 m_themeColors.append(QColor(0, 0, 0));
273 274 m_themeColors.append(QColor(50, 50, 50));
274 275 m_themeColors.append(QColor(100, 100, 100));
275 276 m_themeColors.append(QColor(140, 140, 140));
276 277 m_themeColors.append(QColor(180, 180, 180));
277 278
278 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffffffff)));
279 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
279 setBackground(QColor(QRgb(0xffffffff)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
280 280 break;
281 281 case QChart::ChartThemeUnnamed1:
282 282 m_themeColors.append(QColor(QRgb(0xff3fa9f5)));
283 283 m_themeColors.append(QColor(QRgb(0xff7AC943)));
284 284 m_themeColors.append(QColor(QRgb(0xffFF931E)));
285 285 m_themeColors.append(QColor(QRgb(0xffFF1D25)));
286 286 m_themeColors.append(QColor(QRgb(0xffFF7BAC)));
287 287
288 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xfff3dc9e)));
289 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
288 setBackground(QColor(QRgb(0xfff3dc9e)), QColor(QRgb(0xffafafaf)), VerticalGradientOrientation);
290 289 break;
291 290 default:
292 291 Q_ASSERT(false);
293 292 break;
294 293 }
295 294
296 295 if(m_backgroundItem){
297 296 m_backgroundItem->setBrush(m_backgroundGradient);
298 297 m_backgroundItem->setPen(Qt::NoPen);
299 298 }
300 299
301 300 foreach(QChartSeries* series, m_chartSeries) {
302 301 // TODO: other series interested on themes?
303 302 if (series->type() == QChartSeries::SeriesTypeLine) {
304 303 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
305 304 lineseries->setPen(nextColor());
306 305 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
307 306 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
308 307 scatter->setMarkerColor(nextColor());
309 308 } else if (series->type() == QChartSeries::SeriesTypePie) {
310 309 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
311 310 for (int i(0); i < pieSeries->sliceCount(); i++)
312 311 pieSeries->setSliceColor(i, nextColor());
313 312 }
314 313 }
315 314 update();
316 315 }
317 316
318 317 QColor QChart::nextColor()
319 318 {
320 319 QColor nextColor = m_themeColors.first();
321 320 m_themeColors.move(0, m_themeColors.size() - 1);
322 321 return nextColor;
323 322 }
324 323
325 324 void QChart::zoomInToRect(const QRect& rectangle)
326 325 {
327 326
328 327 if(!rectangle.isValid()) return;
329 328
330 329 qreal margin = this->margin();
331 330
332 331 QRect rect = rectangle.normalized();
333 332 rect.translate(-margin, -margin);
334 333
335 334 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
336 335
337 336 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
338 337
339 338 m_plotDomainList.resize(m_plotDataIndex + 1);
340 339 m_plotDomainList<<domain;
341 340 m_plotDataIndex++;
342 341
343 342 foreach (ChartItem* item ,m_chartItems)
344 343 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
345 344 update();
346 345 }
347 346
348 347 void QChart::zoomIn()
349 348 {
350 349 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
351 350 m_plotDataIndex++;
352 351 foreach (ChartItem* item ,m_chartItems)
353 352 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
354 353 update();
355 354 }else{
356 355 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
357 356 rect.setWidth(rect.width()/2);
358 357 rect.setHeight(rect.height()/2);
359 358 rect.moveCenter(m_rect.center());
360 359 zoomInToRect(rect);
361 360 }
362 361 }
363 362
364 363 void QChart::zoomOut()
365 364 {
366 365 if (m_plotDataIndex > 0) {
367 366 m_plotDataIndex--;
368 367 foreach (ChartItem* item ,m_chartItems)
369 368 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
370 369 update();
371 370 }
372 371 }
373 372
374 373 void QChart::setAxisX(const QChartAxis& axis)
375 374 {
376 375 setAxis(m_axisXItem,axis);
377 376 }
378 377 void QChart::setAxisY(const QChartAxis& axis)
379 378 {
380 379 setAxis(m_axisYItem.at(0),axis);
381 380 }
382 381
383 382 void QChart::setAxisY(const QList<QChartAxis>& axis)
384 383 {
385 384 //TODO not implemented
386 385 }
387 386
388 387 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
389 388 {
390 389 item->setVisible(axis.isAxisVisible());
391 390 }
392 391
393 392 #include "moc_qchart.cpp"
394 393
395 394 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,38 +1,40
1 1 #ifndef QSCATTERSERIES_H
2 2 #define QSCATTERSERIES_H
3 3
4 4 #include "qchartseries.h"
5 5 #include <QRectF>
6 6 #include <QColor>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9 class QScatterSeriesPrivate;
10 10
11 11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
12 12 {
13 13 Q_OBJECT
14 14 public:
15 15 //QScatterSeries(QSeriesData *data, QObject *chart);
16 16 QScatterSeries(QObject *parent = 0);
17 17 ~QScatterSeries();
18 18
19 19 public: // from QChartSeries
20 20 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
21 21 bool setData(QList<qreal> x, QList<qreal> y);
22 22
23 23 public Q_SLOTS:
24 24 void chartSizeChanged(QRectF rect);
25 // TODO: also affects opacity of the marker...? To be documented
25 26 void setMarkerColor(QColor color);
26 27 QColor markerColor();
27 //void chartScaleChanged(qreal xscale, qreal yscale);
28 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
29 //void setMarkerShape(QChartSeries::MarkerShape/QScatterSeries::MarkerShape shape);
28 30
29 31 private:
30 32 Q_DECLARE_PRIVATE(QScatterSeries)
31 33 Q_DISABLE_COPY(QScatterSeries)
32 34 friend class QChart;
33 35 QScatterSeriesPrivate *const d;
34 36 };
35 37
36 38 QTCOMMERCIALCHART_END_NAMESPACE
37 39
38 40 #endif // QSCATTERSERIES_H
General Comments 0
You need to be logged in to leave comments. Login now