##// END OF EJS Templates
Chenged the order of conditions checking in chartstheme decorate functions
Marek Rosa -
r1776:49bea5f62bed
parent child
Show More
@@ -1,639 +1,638
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarset.h"
21 #include "qbarset.h"
22 #include "qbarset_p.h"
22 #include "qbarset_p.h"
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 /*!
26 /*!
27 \class QBarSet
27 \class QBarSet
28 \brief Building block for different bar charts
28 \brief Building block for different bar charts
29
29
30 QBarSet represents one set of bars. Set of bars contains one data value for each category.
30 QBarSet represents one set of bars. Set of bars contains one data value for each category.
31 First value of set is assumed to belong to first category, second to second category and so on.
31 First value of set is assumed to belong to first category, second to second category and so on.
32 If set has fewer values than there are categories, then the missing values are assumed to be
32 If set has fewer values than there are categories, then the missing values are assumed to be
33 at the end of set. For missing values in middle of a set, numerical value of zero is used.
33 at the end of set. For missing values in middle of a set, numerical value of zero is used.
34
34
35 \mainclass
35 \mainclass
36
36
37 \sa QAbstractBarSeries, QBarSeries, QStackedBarSeries, QPercentBarSeries
37 \sa QAbstractBarSeries, QBarSeries, QStackedBarSeries, QPercentBarSeries
38 */
38 */
39 /*!
39 /*!
40 \qmlclass BarSet QBarSet
40 \qmlclass BarSet QBarSet
41
41
42 BarSet represents one set of bars. Set of bars contains one data value for each category.
42 BarSet represents one set of bars. Set of bars contains one data value for each category.
43 First value of set is assumed to belong to first category, second to second category and so on.
43 First value of set is assumed to belong to first category, second to second category and so on.
44 If set has fewer values than there are categories, then the missing values are assumed to be
44 If set has fewer values than there are categories, then the missing values are assumed to be
45 at the end of set. For missing values in middle of a set, numerical value of zero is used.
45 at the end of set. For missing values in middle of a set, numerical value of zero is used.
46 \sa AbstractBarSeries, BarSeries, StackedBarSeries, PercentBarSeries
46 \sa AbstractBarSeries, BarSeries, StackedBarSeries, PercentBarSeries
47 */
47 */
48
48
49 /*!
49 /*!
50 \property QBarSet::label
50 \property QBarSet::label
51 Defines the label of the barSet.
51 Defines the label of the barSet.
52 */
52 */
53 /*!
53 /*!
54 \qmlproperty string BarSet::label
54 \qmlproperty string BarSet::label
55 Defines the label of the barSet.
55 Defines the label of the barSet.
56 */
56 */
57
57
58 /*!
58 /*!
59 \property QBarSet::pen
59 \property QBarSet::pen
60 \brief Defines the pen used by the barSet.
60 \brief Defines the pen used by the barSet.
61 */
61 */
62
62
63 /*!
63 /*!
64 \property QBarSet::brush
64 \property QBarSet::brush
65 \brief Defines the brush used by the barSet.
65 \brief Defines the brush used by the barSet.
66 */
66 */
67
67
68 /*!
68 /*!
69 \property QBarSet::labelBrush
69 \property QBarSet::labelBrush
70 \brief Defines the brush used by the barSet's label.
70 \brief Defines the brush used by the barSet's label.
71 */
71 */
72
72
73 /*!
73 /*!
74 \property QBarSet::labelFont
74 \property QBarSet::labelFont
75 \brief Defines the font used by the barSet's label.
75 \brief Defines the font used by the barSet's label.
76 */
76 */
77
77
78 /*!
78 /*!
79 \qmlproperty Font BarSet::labelFont
79 \qmlproperty Font BarSet::labelFont
80 Defines the font used by the barSet's label.
80 Defines the font used by the barSet's label.
81
81
82 See the \l {Font} {QML Font Element} for detailed documentation.
82 See the \l {Font} {QML Font Element} for detailed documentation.
83 */
83 */
84
84
85 /*!
85 /*!
86 \property QBarSet::color
86 \property QBarSet::color
87 The fill (brush) color of the bar set.
87 The fill (brush) color of the bar set.
88 */
88 */
89 /*!
89 /*!
90 \qmlproperty color BarSet::color
90 \qmlproperty color BarSet::color
91 The fill (brush) color of the bar set.
91 The fill (brush) color of the bar set.
92 */
92 */
93
93
94 /*!
94 /*!
95 \property QBarSet::borderColor
95 \property QBarSet::borderColor
96 The line (pen) color of the bar set.
96 The line (pen) color of the bar set.
97 */
97 */
98 /*!
98 /*!
99 \qmlproperty color BarSet::borderColor
99 \qmlproperty color BarSet::borderColor
100 The line (pen) color of the bar set.
100 The line (pen) color of the bar set.
101 */
101 */
102
102
103 /*!
103 /*!
104 \property QBarSet::labelColor
104 \property QBarSet::labelColor
105 The text (label) color of the bar set.
105 The text (label) color of the bar set.
106 */
106 */
107 /*!
107 /*!
108 \qmlproperty color BarSet::labelColor
108 \qmlproperty color BarSet::labelColor
109 The text (label) color of the bar set.
109 The text (label) color of the bar set.
110 */
110 */
111
111
112 /*!
112 /*!
113 \fn void QBarSet::clicked(int index)
113 \fn void QBarSet::clicked(int index)
114
114
115 The signal is emitted if the user clicks with a mouse on top of barset.
115 The signal is emitted if the user clicks with a mouse on top of barset.
116 Clicked bar inside set is indexed by \a index
116 Clicked bar inside set is indexed by \a index
117 */
117 */
118
118
119 /*!
119 /*!
120 \fn void QBarSet::hovered(bool status)
120 \fn void QBarSet::hovered(bool status)
121
121
122 The signal is emitted if mouse is hovered on top of barset.
122 The signal is emitted if mouse is hovered on top of barset.
123 Parameter \a status is true, if mouse entered on top of barset, false if mouse left from top of barset.
123 Parameter \a status is true, if mouse entered on top of barset, false if mouse left from top of barset.
124 */
124 */
125
125
126
126
127 /*!
127 /*!
128 \fn void QBarSet::labelChanged()
128 \fn void QBarSet::labelChanged()
129 This signal is emitted when the label of the barSet has changed.
129 This signal is emitted when the label of the barSet has changed.
130 \sa label
130 \sa label
131 */
131 */
132 /*!
132 /*!
133 \qmlsignal BarSet::onLabelChanged()
133 \qmlsignal BarSet::onLabelChanged()
134 This signal is emitted when the label of the barSet has changed.
134 This signal is emitted when the label of the barSet has changed.
135 */
135 */
136
136
137 /*!
137 /*!
138 \fn void QBarSet::penChanged()
138 \fn void QBarSet::penChanged()
139 This signal is emitted when the pen of the barSet has changed.
139 This signal is emitted when the pen of the barSet has changed.
140 \sa pen
140 \sa pen
141 */
141 */
142
142
143 /*!
143 /*!
144 \fn void QBarSet::brushChanged()
144 \fn void QBarSet::brushChanged()
145 This signal is emitted when the brush of the barSet has changed.
145 This signal is emitted when the brush of the barSet has changed.
146 \sa brush
146 \sa brush
147 */
147 */
148
148
149 /*!
149 /*!
150 \fn void QBarSet::labelBrushChanged()
150 \fn void QBarSet::labelBrushChanged()
151 This signal is emitted when the brush of the barSet's label has changed.
151 This signal is emitted when the brush of the barSet's label has changed.
152 \sa labelBrush
152 \sa labelBrush
153 */
153 */
154
154
155 /*!
155 /*!
156 \fn void QBarSet::labelFontChanged()
156 \fn void QBarSet::labelFontChanged()
157 This signal is emitted when the font of the barSet's label has changed.
157 This signal is emitted when the font of the barSet's label has changed.
158 \sa labelBrush
158 \sa labelBrush
159 */
159 */
160
160
161 /*!
161 /*!
162 \fn void QBarSet::colorChanged(QColor)
162 \fn void QBarSet::colorChanged(QColor)
163 This signal is emitted when the fill (brush) color of the set has changed to \a color.
163 This signal is emitted when the fill (brush) color of the set has changed to \a color.
164 */
164 */
165 /*!
165 /*!
166 \qmlsignal BarSet::onColorChanged(color color)
166 \qmlsignal BarSet::onColorChanged(color color)
167 This signal is emitted when the fill (brush) color of the set has changed to \a color.
167 This signal is emitted when the fill (brush) color of the set has changed to \a color.
168 */
168 */
169
169
170 /*!
170 /*!
171 \fn void QBarSet::borderColorChanged(QColor)
171 \fn void QBarSet::borderColorChanged(QColor)
172 This signal is emitted when the line (pen) color of the set has changed to \a color.
172 This signal is emitted when the line (pen) color of the set has changed to \a color.
173 */
173 */
174 /*!
174 /*!
175 \qmlsignal BarSet::onBorderColorChanged(color color)
175 \qmlsignal BarSet::onBorderColorChanged(color color)
176 This signal is emitted when the line (pen) color of the set has changed to \a color.
176 This signal is emitted when the line (pen) color of the set has changed to \a color.
177 */
177 */
178
178
179 /*!
179 /*!
180 \fn void QBarSet::labelColorChanged(QColor)
180 \fn void QBarSet::labelColorChanged(QColor)
181 This signal is emitted when the text (label) color of the set has changed to \a color.
181 This signal is emitted when the text (label) color of the set has changed to \a color.
182 */
182 */
183 /*!
183 /*!
184 \qmlsignal BarSet::onLabelColorChanged(color color)
184 \qmlsignal BarSet::onLabelColorChanged(color color)
185 This signal is emitted when the text (label) color of the set has changed to \a color.
185 This signal is emitted when the text (label) color of the set has changed to \a color.
186 */
186 */
187
187
188 /*!
188 /*!
189 \fn void QBarSet::valuesAdded(int index, int count)
189 \fn void QBarSet::valuesAdded(int index, int count)
190 This signal is emitted when new values have been added to the set.
190 This signal is emitted when new values have been added to the set.
191 Parameter \a index indicates the position of the first inserted value.
191 Parameter \a index indicates the position of the first inserted value.
192 Parameter \a count is the number of iserted values.
192 Parameter \a count is the number of iserted values.
193 \sa append(), insert()
193 \sa append(), insert()
194 */
194 */
195 /*!
195 /*!
196 \qmlsignal BarSet::onValuesAdded(int index, int count)
196 \qmlsignal BarSet::onValuesAdded(int index, int count)
197 This signal is emitted when new values have been added to the set.
197 This signal is emitted when new values have been added to the set.
198 Parameter \a index indicates the position of the first inserted value.
198 Parameter \a index indicates the position of the first inserted value.
199 Parameter \a count is the number of iserted values.
199 Parameter \a count is the number of iserted values.
200 */
200 */
201
201
202 /*!
202 /*!
203 \fn void QBarSet::valuesRemoved(int index, int count)
203 \fn void QBarSet::valuesRemoved(int index, int count)
204 This signal is emitted values have been removed from the set.
204 This signal is emitted values have been removed from the set.
205 Parameter \a index indicates the position of the first removed value.
205 Parameter \a index indicates the position of the first removed value.
206 Parameter \a count is the number of removed values.
206 Parameter \a count is the number of removed values.
207 \sa remove()
207 \sa remove()
208 */
208 */
209 /*!
209 /*!
210 \qmlsignal BarSet::onValuesRemoved(int index, int count)
210 \qmlsignal BarSet::onValuesRemoved(int index, int count)
211 This signal is emitted values have been removed from the set.
211 This signal is emitted values have been removed from the set.
212 Parameter \a index indicates the position of the first removed value.
212 Parameter \a index indicates the position of the first removed value.
213 Parameter \a count is the number of removed values.
213 Parameter \a count is the number of removed values.
214 */
214 */
215
215
216 /*!
216 /*!
217 \fn void QBarSet::valueChanged(int index)
217 \fn void QBarSet::valueChanged(int index)
218 This signal is emitted values the value in the set has been modified.
218 This signal is emitted values the value in the set has been modified.
219 Parameter \a index indicates the position of the modified value.
219 Parameter \a index indicates the position of the modified value.
220 \sa at()
220 \sa at()
221 */
221 */
222 /*!
222 /*!
223 \qmlsignal BarSet::onValueChanged(int index)
223 \qmlsignal BarSet::onValueChanged(int index)
224 This signal is emitted values the value in the set has been modified.
224 This signal is emitted values the value in the set has been modified.
225 Parameter \a index indicates the position of the modified value.
225 Parameter \a index indicates the position of the modified value.
226 */
226 */
227
227
228 /*!
228 /*!
229 \qmlproperty int BarSet::count
229 \qmlproperty int BarSet::count
230 The count of values on the barset
230 The count of values on the barset
231 */
231 */
232
232
233 /*!
233 /*!
234 \qmlproperty QVariantList BarSet::values
234 \qmlproperty QVariantList BarSet::values
235 The values of the barset. You can set either a list of reals or a list of points as values. If you set a list of
235 The values of the barset. You can set either a list of reals or a list of points as values. If you set a list of
236 reals as values, the values are automatically completed to points by using the index of a value as it's
236 reals as values, the values are automatically completed to points by using the index of a value as it's
237 x-coordinate. For example:
237 x-coordinate. For example:
238 \code
238 \code
239 myBarSet1.values = [0, 5, 1, 5];
239 myBarSet1.values = [0, 5, 1, 5];
240 myBarSet2.values = [Qt.point(0, 1), Qt.point(1, 5), Qt.point(2.2, 4.3)];
240 myBarSet2.values = [Qt.point(0, 1), Qt.point(1, 5), Qt.point(2.2, 4.3)];
241 \endcode
241 \endcode
242 */
242 */
243
243
244 /*!
244 /*!
245 Constructs QBarSet with a label of \a label and with parent of \a parent
245 Constructs QBarSet with a label of \a label and with parent of \a parent
246 */
246 */
247 QBarSet::QBarSet(const QString label, QObject *parent)
247 QBarSet::QBarSet(const QString label, QObject *parent)
248 : QObject(parent)
248 : QObject(parent)
249 ,d_ptr(new QBarSetPrivate(label,this))
249 ,d_ptr(new QBarSetPrivate(label,this))
250 {
250 {
251 }
251 }
252
252
253 /*!
253 /*!
254 Destroys the barset
254 Destroys the barset
255 */
255 */
256 QBarSet::~QBarSet()
256 QBarSet::~QBarSet()
257 {
257 {
258 // NOTE: d_ptr destroyed by QObject
258 // NOTE: d_ptr destroyed by QObject
259 }
259 }
260
260
261 /*!
261 /*!
262 Sets new \a label for set.
262 Sets new \a label for set.
263 */
263 */
264 void QBarSet::setLabel(const QString label)
264 void QBarSet::setLabel(const QString label)
265 {
265 {
266 d_ptr->m_label = label;
266 d_ptr->m_label = label;
267 emit labelChanged();
267 emit labelChanged();
268 }
268 }
269
269
270 /*!
270 /*!
271 Returns label of the set.
271 Returns label of the set.
272 */
272 */
273 QString QBarSet::label() const
273 QString QBarSet::label() const
274 {
274 {
275 return d_ptr->m_label;
275 return d_ptr->m_label;
276 }
276 }
277
277
278 /*!
278 /*!
279 Appends new value \a value to the end of set.
279 Appends new value \a value to the end of set.
280 */
280 */
281 void QBarSet::append(const qreal value)
281 void QBarSet::append(const qreal value)
282 {
282 {
283 // Convert to QPointF
283 // Convert to QPointF
284 int index = d_ptr->m_values.count();
284 int index = d_ptr->m_values.count();
285 d_ptr->append(QPointF(d_ptr->m_values.count(), value));
285 d_ptr->append(QPointF(d_ptr->m_values.count(), value));
286 emit valuesAdded(index, 1);
286 emit valuesAdded(index, 1);
287 }
287 }
288
288
289 /*!
289 /*!
290 Appends a list of reals to set. Works like append with single real value. The \a values in list
290 Appends a list of reals to set. Works like append with single real value. The \a values in list
291 are appended to end of barset
291 are appended to end of barset
292 \sa append()
292 \sa append()
293 */
293 */
294 void QBarSet::append(const QList<qreal> &values)
294 void QBarSet::append(const QList<qreal> &values)
295 {
295 {
296 int index = d_ptr->m_values.count();
296 int index = d_ptr->m_values.count();
297 d_ptr->append(values);
297 d_ptr->append(values);
298 emit valuesAdded(index, values.count());
298 emit valuesAdded(index, values.count());
299 }
299 }
300
300
301 /*!
301 /*!
302 Convinience operator. Same as append, with real \a value.
302 Convinience operator. Same as append, with real \a value.
303 \sa append()
303 \sa append()
304 */
304 */
305 QBarSet& QBarSet::operator << (const qreal &value)
305 QBarSet& QBarSet::operator << (const qreal &value)
306 {
306 {
307 append(value);
307 append(value);
308 return *this;
308 return *this;
309 }
309 }
310
310
311 /*!
311 /*!
312 Inserts new \a value on the \a index position.
312 Inserts new \a value on the \a index position.
313 The value that is currently at this postion is moved to postion index + 1
313 The value that is currently at this postion is moved to postion index + 1
314 \sa remove()
314 \sa remove()
315 */
315 */
316 void QBarSet::insert(const int index, const qreal value)
316 void QBarSet::insert(const int index, const qreal value)
317 {
317 {
318 d_ptr->insert(index, value);
318 d_ptr->insert(index, value);
319 emit valuesAdded(index,1);
319 emit valuesAdded(index,1);
320 }
320 }
321
321
322 /*!
322 /*!
323 Removes \a count number of values from the set starting at \a index.
323 Removes \a count number of values from the set starting at \a index.
324 \sa insert()
324 \sa insert()
325 */
325 */
326 void QBarSet::remove(const int index, const int count)
326 void QBarSet::remove(const int index, const int count)
327 {
327 {
328 int removedCount = d_ptr->remove(index,count);
328 int removedCount = d_ptr->remove(index,count);
329 if (removedCount > 0) {
329 if (removedCount > 0) {
330 emit valuesRemoved(index,removedCount);
330 emit valuesRemoved(index,removedCount);
331 }
331 }
332 return;
332 return;
333 }
333 }
334
334
335 /*!
335 /*!
336 Sets a new value \a value to set, indexed by \a index
336 Sets a new value \a value to set, indexed by \a index
337 */
337 */
338 void QBarSet::replace(const int index, const qreal value)
338 void QBarSet::replace(const int index, const qreal value)
339 {
339 {
340 if (index >= 0 && index < d_ptr->m_values.count()) {
340 if (index >= 0 && index < d_ptr->m_values.count()) {
341 d_ptr->replace(index,value);
341 d_ptr->replace(index,value);
342 emit valueChanged(index);
342 emit valueChanged(index);
343 }
343 }
344 }
344 }
345
345
346
346
347 /*!
347 /*!
348 Returns value of set indexed by \a index.
348 Returns value of set indexed by \a index.
349 If the index is out of bounds 0.0 is returned.
349 If the index is out of bounds 0.0 is returned.
350 */
350 */
351 qreal QBarSet::at(const int index) const
351 qreal QBarSet::at(const int index) const
352 {
352 {
353 if (index < 0 || index >= d_ptr->m_values.count()) {
353 if (index < 0 || index >= d_ptr->m_values.count()) {
354 return 0;
354 return 0;
355 }
355 }
356
356
357 return d_ptr->m_values.at(index).y();
357 return d_ptr->m_values.at(index).y();
358 }
358 }
359
359
360 /*!
360 /*!
361 Returns value of set indexed by \a index.
361 Returns value of set indexed by \a index.
362 If the index is out of bounds 0.0 is returned.
362 If the index is out of bounds 0.0 is returned.
363 */
363 */
364 qreal QBarSet::operator [](const int index) const
364 qreal QBarSet::operator [](const int index) const
365 {
365 {
366 return at(index);
366 return at(index);
367 }
367 }
368
368
369 /*!
369 /*!
370 Returns count of values in set.
370 Returns count of values in set.
371 */
371 */
372 int QBarSet::count() const
372 int QBarSet::count() const
373 {
373 {
374 return d_ptr->m_values.count();
374 return d_ptr->m_values.count();
375 }
375 }
376
376
377 /*!
377 /*!
378 Returns sum of all values in barset.
378 Returns sum of all values in barset.
379 */
379 */
380 qreal QBarSet::sum() const
380 qreal QBarSet::sum() const
381 {
381 {
382 qreal total(0);
382 qreal total(0);
383 for (int i=0; i < d_ptr->m_values.count(); i++) {
383 for (int i=0; i < d_ptr->m_values.count(); i++) {
384 total += d_ptr->m_values.at(i).y();
384 total += d_ptr->m_values.at(i).y();
385 }
385 }
386 return total;
386 return total;
387 }
387 }
388
388
389 /*!
389 /*!
390 Sets pen for set. Bars of this set are drawn using \a pen
390 Sets pen for set. Bars of this set are drawn using \a pen
391 */
391 */
392 void QBarSet::setPen(const QPen &pen)
392 void QBarSet::setPen(const QPen &pen)
393 {
393 {
394 if(d_ptr->m_pen!=pen){
394 if(d_ptr->m_pen!=pen){
395 d_ptr->m_pen = pen;
395 d_ptr->m_pen = pen;
396 emit d_ptr->updatedBars();
396 emit d_ptr->updatedBars();
397 emit penChanged();
397 emit penChanged();
398 }
398 }
399 }
399 }
400
400
401 /*!
401 /*!
402 Returns pen of the set.
402 Returns pen of the set.
403 */
403 */
404 QPen QBarSet::pen() const
404 QPen QBarSet::pen() const
405 {
405 {
406 return d_ptr->m_pen;
406 return d_ptr->m_pen;
407 }
407 }
408
408
409 /*!
409 /*!
410 Sets brush for the set. Bars of this set are drawn using \a brush
410 Sets brush for the set. Bars of this set are drawn using \a brush
411 */
411 */
412 void QBarSet::setBrush(const QBrush &brush)
412 void QBarSet::setBrush(const QBrush &brush)
413 {
413 {
414 if(d_ptr->m_brush!=brush){
414 if(d_ptr->m_brush!=brush){
415 d_ptr->m_brush = brush;
415 d_ptr->m_brush = brush;
416 emit d_ptr->updatedBars();
416 emit d_ptr->updatedBars();
417 emit brushChanged();
417 emit brushChanged();
418 }
418 }
419 }
419 }
420
420
421 /*!
421 /*!
422 Returns brush of the set.
422 Returns brush of the set.
423 */
423 */
424 QBrush QBarSet::brush() const
424 QBrush QBarSet::brush() const
425 {
425 {
426 return d_ptr->m_brush;
426 return d_ptr->m_brush;
427 }
427 }
428
428
429 /*!
429 /*!
430 Sets \a brush of the values that are drawn on top of this barset
430 Sets \a brush of the values that are drawn on top of this barset
431 */
431 */
432 void QBarSet::setLabelBrush(const QBrush &brush)
432 void QBarSet::setLabelBrush(const QBrush &brush)
433 {
433 {
434 if(d_ptr->m_labelBrush!=brush){
434 if(d_ptr->m_labelBrush!=brush){
435 d_ptr->m_labelBrush = brush;
435 d_ptr->m_labelBrush = brush;
436 emit d_ptr->updatedBars();
436 emit d_ptr->updatedBars();
437 emit labelBrushChanged();
437 emit labelBrushChanged();
438 }
438 }
439 }
439 }
440
440
441 /*!
441 /*!
442 Returns brush of the values that are drawn on top of this barset
442 Returns brush of the values that are drawn on top of this barset
443 */
443 */
444 QBrush QBarSet::labelBrush() const
444 QBrush QBarSet::labelBrush() const
445 {
445 {
446 return d_ptr->m_labelBrush;
446 return d_ptr->m_labelBrush;
447 }
447 }
448
448
449 /*!
449 /*!
450 Sets the \a font for values that are drawn on top of this barset
450 Sets the \a font for values that are drawn on top of this barset
451 */
451 */
452 void QBarSet::setLabelFont(const QFont &font)
452 void QBarSet::setLabelFont(const QFont &font)
453 {
453 {
454 if(d_ptr->m_labelFont!=font) {
454 if(d_ptr->m_labelFont!=font) {
455 d_ptr->m_labelFont = font;
455 d_ptr->m_labelFont = font;
456 emit d_ptr->updatedBars();
456 emit d_ptr->updatedBars();
457 emit labelFontChanged();
457 emit labelFontChanged();
458 }
458 }
459
459
460 }
460 }
461
461
462 /*!
462 /*!
463 Returns the pen for values that are drawn on top of this barset
463 Returns the pen for values that are drawn on top of this barset
464 */
464 */
465 QFont QBarSet::labelFont() const
465 QFont QBarSet::labelFont() const
466 {
466 {
467 return d_ptr->m_labelFont;
467 return d_ptr->m_labelFont;
468 }
468 }
469
469
470 /*!
470 /*!
471 Returns the color of the brush of barset.
471 Returns the color of the brush of barset.
472 */
472 */
473 QColor QBarSet::color()
473 QColor QBarSet::color()
474 {
474 {
475 return brush().color();
475 return brush().color();
476 }
476 }
477
477
478 /*!
478 /*!
479 Sets the \a color of brush for this barset
479 Sets the \a color of brush for this barset
480 */
480 */
481 void QBarSet::setColor(QColor color)
481 void QBarSet::setColor(QColor color)
482 {
482 {
483 QBrush b = brush();
483 QBrush b = brush();
484 if (b.color() != color) {
484 if (b.color() != color) {
485 b.setColor(color);
485 b.setColor(color);
486 setBrush(b);
486 setBrush(b);
487 emit colorChanged(color);
487 emit colorChanged(color);
488 }
488 }
489 }
489 }
490
490
491 /*!
491 /*!
492 Returns the color of pen of this barset
492 Returns the color of pen of this barset
493 */
493 */
494 QColor QBarSet::borderColor()
494 QColor QBarSet::borderColor()
495 {
495 {
496 return pen().color();
496 return pen().color();
497 }
497 }
498
498
499 /*!
499 /*!
500 Sets the color of pen for this barset
500 Sets the color of pen for this barset
501 */
501 */
502 void QBarSet::setBorderColor(QColor color)
502 void QBarSet::setBorderColor(QColor color)
503 {
503 {
504 QPen p = pen();
504 QPen p = pen();
505 if (p.color() != color) {
505 if (p.color() != color) {
506 p.setColor(color);
506 p.setColor(color);
507 setPen(p);
507 setPen(p);
508 emit borderColorChanged(color);
508 emit borderColorChanged(color);
509 }
509 }
510 }
510 }
511
511
512 /*!
512 /*!
513 Returns the color of labels of this barset
513 Returns the color of labels of this barset
514 */
514 */
515 QColor QBarSet::labelColor()
515 QColor QBarSet::labelColor()
516 {
516 {
517 return labelBrush().color();
517 return labelBrush().color();
518 }
518 }
519
519
520 /*!
520 /*!
521 Sets the color of labels for this barset
521 Sets the color of labels for this barset
522 */
522 */
523 void QBarSet::setLabelColor(QColor color)
523 void QBarSet::setLabelColor(QColor color)
524 {
524 {
525 QBrush defaultBrush;
526 QBrush b = labelBrush();
525 QBrush b = labelBrush();
527 if (b == defaultBrush)
526 if (b == QBrush())
528 b.setStyle(Qt::SolidPattern);
527 b.setStyle(Qt::SolidPattern);
529
528
530 if (b.color() != color) {
529 if (b.color() != color) {
531 b.setColor(color);
530 b.setColor(color);
532 setLabelBrush(b);
531 setLabelBrush(b);
533 emit labelColorChanged(color);
532 emit labelColorChanged(color);
534 }
533 }
535 }
534 }
536
535
537 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
536 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
538
537
539 QBarSetPrivate::QBarSetPrivate(const QString label, QBarSet *parent) : QObject(parent),
538 QBarSetPrivate::QBarSetPrivate(const QString label, QBarSet *parent) : QObject(parent),
540 q_ptr(parent),
539 q_ptr(parent),
541 m_label(label)
540 m_label(label)
542 {
541 {
543 }
542 }
544
543
545 QBarSetPrivate::~QBarSetPrivate()
544 QBarSetPrivate::~QBarSetPrivate()
546 {
545 {
547 }
546 }
548
547
549 void QBarSetPrivate::append(QPointF value)
548 void QBarSetPrivate::append(QPointF value)
550 {
549 {
551 m_values.append(value);
550 m_values.append(value);
552 emit restructuredBars();
551 emit restructuredBars();
553 }
552 }
554
553
555 void QBarSetPrivate::append(QList<QPointF> values)
554 void QBarSetPrivate::append(QList<QPointF> values)
556 {
555 {
557 for (int i=0; i<values.count(); i++) {
556 for (int i=0; i<values.count(); i++) {
558 m_values.append(values.at(i));
557 m_values.append(values.at(i));
559 }
558 }
560 emit restructuredBars();
559 emit restructuredBars();
561 }
560 }
562
561
563 void QBarSetPrivate::append(QList<qreal> values)
562 void QBarSetPrivate::append(QList<qreal> values)
564 {
563 {
565 int index = m_values.count();
564 int index = m_values.count();
566 for (int i=0; i<values.count(); i++) {
565 for (int i=0; i<values.count(); i++) {
567 m_values.append(QPointF(index,values.at(i)));
566 m_values.append(QPointF(index,values.at(i)));
568 index++;
567 index++;
569 }
568 }
570 emit restructuredBars();
569 emit restructuredBars();
571 }
570 }
572
571
573 void QBarSetPrivate::insert(const int index, const qreal value)
572 void QBarSetPrivate::insert(const int index, const qreal value)
574 {
573 {
575 m_values.insert(index, QPointF(index, value));
574 m_values.insert(index, QPointF(index, value));
576 emit restructuredBars();
575 emit restructuredBars();
577 }
576 }
578
577
579 void QBarSetPrivate::insert(const int index, const QPointF value)
578 void QBarSetPrivate::insert(const int index, const QPointF value)
580 {
579 {
581 m_values.insert(index, value);
580 m_values.insert(index, value);
582 emit restructuredBars();
581 emit restructuredBars();
583 }
582 }
584
583
585 int QBarSetPrivate::remove(const int index, const int count)
584 int QBarSetPrivate::remove(const int index, const int count)
586 {
585 {
587 int removeCount = count;
586 int removeCount = count;
588
587
589 if ((index <0) || (m_values.count() == 0)) {
588 if ((index <0) || (m_values.count() == 0)) {
590 // Invalid index or not values in list, remove nothing.
589 // Invalid index or not values in list, remove nothing.
591 return 0;
590 return 0;
592 } else if ((index + count) > m_values.count()) {
591 } else if ((index + count) > m_values.count()) {
593 // Trying to remove more items than list has. Limit amount to be removed.
592 // Trying to remove more items than list has. Limit amount to be removed.
594 removeCount = m_values.count() - index;
593 removeCount = m_values.count() - index;
595 }
594 }
596
595
597 int c = 0;
596 int c = 0;
598 while (c < removeCount) {
597 while (c < removeCount) {
599 m_values.removeAt(index);
598 m_values.removeAt(index);
600 c++;
599 c++;
601 }
600 }
602 emit restructuredBars();
601 emit restructuredBars();
603 return removeCount;
602 return removeCount;
604 }
603 }
605
604
606 void QBarSetPrivate::replace(const int index, const qreal value)
605 void QBarSetPrivate::replace(const int index, const qreal value)
607 {
606 {
608 m_values.replace(index,QPointF(index,value));
607 m_values.replace(index,QPointF(index,value));
609 emit updatedBars();
608 emit updatedBars();
610 }
609 }
611
610
612 void QBarSetPrivate::replace(const int index, const QPointF value)
611 void QBarSetPrivate::replace(const int index, const QPointF value)
613 {
612 {
614 m_values.replace(index,value);
613 m_values.replace(index,value);
615 emit updatedBars();
614 emit updatedBars();
616 }
615 }
617
616
618 qreal QBarSetPrivate::pos(const int index)
617 qreal QBarSetPrivate::pos(const int index)
619 {
618 {
620 if (index < 0 || index >= m_values.count()) {
619 if (index < 0 || index >= m_values.count()) {
621 return 0;
620 return 0;
622 }
621 }
623
622
624 return m_values.at(index).x();
623 return m_values.at(index).x();
625 }
624 }
626
625
627 qreal QBarSetPrivate::value(const int index)
626 qreal QBarSetPrivate::value(const int index)
628 {
627 {
629 if (index < 0 || index >= m_values.count()) {
628 if (index < 0 || index >= m_values.count()) {
630 return 0;
629 return 0;
631 }
630 }
632
631
633 return m_values.at(index).y();
632 return m_values.at(index).y();
634 }
633 }
635
634
636 #include "moc_qbarset.cpp"
635 #include "moc_qbarset.cpp"
637 #include "moc_qbarset_p.cpp"
636 #include "moc_qbarset_p.cpp"
638
637
639 QTCOMMERCIALCHART_END_NAMESPACE
638 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,393 +1,393
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "charttheme_p.h"
21 #include "charttheme_p.h"
22 #include "qchart.h"
22 #include "qchart.h"
23 #include "qchart_p.h"
23 #include "qchart_p.h"
24 #include "qchartview.h"
24 #include "qchartview.h"
25 #include "qlegend.h"
25 #include "qlegend.h"
26 #include "qabstractaxis.h"
26 #include "qabstractaxis.h"
27 #include <QTime>
27 #include <QTime>
28
28
29 //series
29 //series
30 #include "qbarset.h"
30 #include "qbarset.h"
31 #include "qabstractbarseries.h"
31 #include "qabstractbarseries.h"
32 #include "qstackedbarseries.h"
32 #include "qstackedbarseries.h"
33 #include "qpercentbarseries.h"
33 #include "qpercentbarseries.h"
34 #include "qlineseries.h"
34 #include "qlineseries.h"
35 #include "qareaseries.h"
35 #include "qareaseries.h"
36 #include "qscatterseries.h"
36 #include "qscatterseries.h"
37 #include "qpieseries.h"
37 #include "qpieseries.h"
38 #include "qpieslice.h"
38 #include "qpieslice.h"
39 #include "qpieslice_p.h"
39 #include "qpieslice_p.h"
40 #include "qsplineseries.h"
40 #include "qsplineseries.h"
41
41
42 //items
42 //items
43 #include "chartaxis_p.h"
43 #include "chartaxis_p.h"
44 #include "abstractbarchartitem_p.h"
44 #include "abstractbarchartitem_p.h"
45 #include "stackedbarchartitem_p.h"
45 #include "stackedbarchartitem_p.h"
46 #include "percentbarchartitem_p.h"
46 #include "percentbarchartitem_p.h"
47 #include "linechartitem_p.h"
47 #include "linechartitem_p.h"
48 #include "areachartitem_p.h"
48 #include "areachartitem_p.h"
49 #include "scatterchartitem_p.h"
49 #include "scatterchartitem_p.h"
50 #include "piechartitem_p.h"
50 #include "piechartitem_p.h"
51 #include "splinechartitem_p.h"
51 #include "splinechartitem_p.h"
52
52
53 //themes
53 //themes
54 #include "chartthemesystem_p.h"
54 #include "chartthemesystem_p.h"
55 #include "chartthemelight_p.h"
55 #include "chartthemelight_p.h"
56 #include "chartthemebluecerulean_p.h"
56 #include "chartthemebluecerulean_p.h"
57 #include "chartthemedark_p.h"
57 #include "chartthemedark_p.h"
58 #include "chartthemebrownsand_p.h"
58 #include "chartthemebrownsand_p.h"
59 #include "chartthemebluencs_p.h"
59 #include "chartthemebluencs_p.h"
60 #include "chartthemehighcontrast_p.h"
60 #include "chartthemehighcontrast_p.h"
61 #include "chartthemeblueicy_p.h"
61 #include "chartthemeblueicy_p.h"
62
62
63 QTCOMMERCIALCHART_BEGIN_NAMESPACE
63 QTCOMMERCIALCHART_BEGIN_NAMESPACE
64
64
65 ChartTheme::ChartTheme(QChart::ChartTheme id) :
65 ChartTheme::ChartTheme(QChart::ChartTheme id) :
66 m_masterFont(QFont("arial", 14)),
66 m_masterFont(QFont("arial", 14)),
67 m_labelFont(QFont("arial", 10)),
67 m_labelFont(QFont("arial", 10)),
68 m_labelBrush(QColor(QRgb(0x000000))),
68 m_labelBrush(QColor(QRgb(0x000000))),
69 m_axisLinePen(QPen(QRgb(0x000000))),
69 m_axisLinePen(QPen(QRgb(0x000000))),
70 m_backgroundShadesPen(Qt::NoPen),
70 m_backgroundShadesPen(Qt::NoPen),
71 m_backgroundShadesBrush(Qt::NoBrush),
71 m_backgroundShadesBrush(Qt::NoBrush),
72 m_backgroundShades(BackgroundShadesNone),
72 m_backgroundShades(BackgroundShadesNone),
73 m_backgroundDropShadowEnabled(false),
73 m_backgroundDropShadowEnabled(false),
74 m_gridLinePen(QPen(QRgb(0x000000))),
74 m_gridLinePen(QPen(QRgb(0x000000))),
75 m_force(false)
75 m_force(false)
76 {
76 {
77 m_id = id;
77 m_id = id;
78 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
78 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
79 }
79 }
80
80
81
81
82 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
82 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
83 {
83 {
84 switch(theme) {
84 switch(theme) {
85 case QChart::ChartThemeLight:
85 case QChart::ChartThemeLight:
86 return new ChartThemeLight();
86 return new ChartThemeLight();
87 case QChart::ChartThemeBlueCerulean:
87 case QChart::ChartThemeBlueCerulean:
88 return new ChartThemeBlueCerulean();
88 return new ChartThemeBlueCerulean();
89 case QChart::ChartThemeDark:
89 case QChart::ChartThemeDark:
90 return new ChartThemeDark();
90 return new ChartThemeDark();
91 case QChart::ChartThemeBrownSand:
91 case QChart::ChartThemeBrownSand:
92 return new ChartThemeBrownSand();
92 return new ChartThemeBrownSand();
93 case QChart::ChartThemeBlueNcs:
93 case QChart::ChartThemeBlueNcs:
94 return new ChartThemeBlueNcs();
94 return new ChartThemeBlueNcs();
95 case QChart::ChartThemeHighContrast:
95 case QChart::ChartThemeHighContrast:
96 return new ChartThemeHighContrast();
96 return new ChartThemeHighContrast();
97 case QChart::ChartThemeBlueIcy:
97 case QChart::ChartThemeBlueIcy:
98 return new ChartThemeBlueIcy();
98 return new ChartThemeBlueIcy();
99 default:
99 default:
100 return new ChartThemeSystem();
100 return new ChartThemeSystem();
101 }
101 }
102 }
102 }
103
103
104 void ChartTheme::decorate(QChart *chart)
104 void ChartTheme::decorate(QChart *chart)
105 {
105 {
106 QBrush brush;
106 QBrush brush;
107
107
108 if(brush == chart->backgroundBrush() || m_force)
108 if(m_force || brush == chart->backgroundBrush())
109 chart->setBackgroundBrush(m_chartBackgroundGradient);
109 chart->setBackgroundBrush(m_chartBackgroundGradient);
110 chart->setTitleFont(m_masterFont);
110 chart->setTitleFont(m_masterFont);
111 chart->setTitleBrush(m_labelBrush);
111 chart->setTitleBrush(m_labelBrush);
112 chart->setDropShadowEnabled(m_backgroundDropShadowEnabled);
112 chart->setDropShadowEnabled(m_backgroundDropShadowEnabled);
113 }
113 }
114
114
115 void ChartTheme::decorate(QLegend *legend)
115 void ChartTheme::decorate(QLegend *legend)
116 {
116 {
117 QPen pen;
117 QPen pen;
118 QBrush brush;
118 QBrush brush;
119 QFont font;
119 QFont font;
120
120
121 if (pen == legend->pen() || m_force)
121 if (m_force || pen == legend->pen())
122 legend->setPen(m_axisLinePen);
122 legend->setPen(m_axisLinePen);
123
123
124 if (brush == legend->brush() || m_force)
124 if (m_force || brush == legend->brush())
125 legend->setBrush(m_chartBackgroundGradient);
125 legend->setBrush(m_chartBackgroundGradient);
126
126
127 if (font == legend->font() || m_force)
127 if (m_force || font == legend->font())
128 legend->setFont(m_labelFont);
128 legend->setFont(m_labelFont);
129
129
130 if (brush == legend->labelBrush() || m_force)
130 if (m_force || brush == legend->labelBrush())
131 legend->setLabelBrush(m_labelBrush);
131 legend->setLabelBrush(m_labelBrush);
132 }
132 }
133
133
134 void ChartTheme::decorate(QAreaSeries *series, int index)
134 void ChartTheme::decorate(QAreaSeries *series, int index)
135 {
135 {
136 QPen pen;
136 QPen pen;
137 QBrush brush;
137 QBrush brush;
138
138
139 if (pen == series->pen() || m_force){
139 if (m_force || pen == series->pen()){
140 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
140 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
141 pen.setWidthF(2);
141 pen.setWidthF(2);
142 series->setPen(pen);
142 series->setPen(pen);
143 }
143 }
144
144
145 if (brush == series->brush() || m_force) {
145 if (m_force || brush == series->brush()) {
146 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
146 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
147 series->setBrush(brush);
147 series->setBrush(brush);
148 }
148 }
149 }
149 }
150
150
151
151
152 void ChartTheme::decorate(QLineSeries *series,int index)
152 void ChartTheme::decorate(QLineSeries *series,int index)
153 {
153 {
154 QPen pen;
154 QPen pen;
155 if(pen == series->pen() || m_force ){
155 if(m_force || pen == series->pen()){
156 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
156 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
157 pen.setWidthF(2);
157 pen.setWidthF(2);
158 series->setPen(pen);
158 series->setPen(pen);
159 }
159 }
160 }
160 }
161
161
162 void ChartTheme::decorate(QAbstractBarSeries *series, int index)
162 void ChartTheme::decorate(QAbstractBarSeries *series, int index)
163 {
163 {
164 QBrush brush;
164 QBrush brush;
165 QPen pen;
165 QPen pen;
166 QList<QBarSet *> sets = series->barSets();
166 QList<QBarSet *> sets = series->barSets();
167
167
168 qreal takeAtPos = 0.5;
168 qreal takeAtPos = 0.5;
169 qreal step = 0.2;
169 qreal step = 0.2;
170 if (sets.count() > 1 ) {
170 if (sets.count() > 1 ) {
171 step = 1.0 / (qreal) sets.count();
171 step = 1.0 / (qreal) sets.count();
172 if (sets.count() % m_seriesGradients.count())
172 if (sets.count() % m_seriesGradients.count())
173 step *= m_seriesGradients.count();
173 step *= m_seriesGradients.count();
174 else
174 else
175 step *= (m_seriesGradients.count() - 1);
175 step *= (m_seriesGradients.count() - 1);
176 }
176 }
177
177
178 for (int i(0); i < sets.count(); i++) {
178 for (int i(0); i < sets.count(); i++) {
179 int colorIndex = (index + i) % m_seriesGradients.count();
179 int colorIndex = (index + i) % m_seriesGradients.count();
180 if (i > 0 && i % m_seriesGradients.count() == 0) {
180 if (i > 0 && i % m_seriesGradients.count() == 0) {
181 // There is no dedicated base color for each sets, generate more colors
181 // There is no dedicated base color for each sets, generate more colors
182 takeAtPos += step;
182 takeAtPos += step;
183 if (takeAtPos == 1.0)
183 if (takeAtPos == 1.0)
184 takeAtPos += step;
184 takeAtPos += step;
185 takeAtPos -= (int) takeAtPos;
185 takeAtPos -= (int) takeAtPos;
186 }
186 }
187 if (brush == sets.at(i)->brush() || m_force )
187 if (m_force || brush == sets.at(i)->brush())
188 sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos));
188 sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos));
189
189
190 // Pick label color from the opposite end of the gradient.
190 // Pick label color from the opposite end of the gradient.
191 // 0.3 as a boundary seems to work well.
191 // 0.3 as a boundary seems to work well.
192 if (brush == sets.at(i)->labelBrush() || m_force) {
192 if (m_force || brush == sets.at(i)->labelBrush()) {
193 if (takeAtPos < 0.3)
193 if (takeAtPos < 0.3)
194 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1));
194 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1));
195 else
195 else
196 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0));
196 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0));
197 }
197 }
198
198
199 if (pen == sets.at(i)->pen() || m_force) {
199 if (m_force || pen == sets.at(i)->pen()) {
200 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
200 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
201 sets.at(i)->setPen(c);
201 sets.at(i)->setPen(c);
202 }
202 }
203 }
203 }
204 }
204 }
205
205
206 void ChartTheme::decorate(QScatterSeries *series, int index)
206 void ChartTheme::decorate(QScatterSeries *series, int index)
207 {
207 {
208 QPen pen;
208 QPen pen;
209 QBrush brush;
209 QBrush brush;
210
210
211 if (pen == series->pen() || m_force) {
211 if (m_force || pen == series->pen()) {
212 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
212 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
213 pen.setWidthF(2);
213 pen.setWidthF(2);
214 series->setPen(pen);
214 series->setPen(pen);
215 }
215 }
216
216
217 if (brush == series->brush() || m_force) {
217 if (m_force || brush == series->brush()) {
218 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
218 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
219 series->setBrush(brush);
219 series->setBrush(brush);
220 }
220 }
221 }
221 }
222
222
223 void ChartTheme::decorate(QPieSeries *series, int index)
223 void ChartTheme::decorate(QPieSeries *series, int index)
224 {
224 {
225
225
226 for (int i(0); i < series->slices().count(); i++) {
226 for (int i(0); i < series->slices().count(); i++) {
227
227
228 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
228 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
229
229
230 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
230 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
231 qreal pos = (qreal) (i + 1) / (qreal) series->count();
231 qreal pos = (qreal) (i + 1) / (qreal) series->count();
232 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
232 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
233
233
234 QPieSlice *s = series->slices().at(i);
234 QPieSlice *s = series->slices().at(i);
235 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
235 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
236
236
237 if (d->m_data.m_slicePen.isThemed() || m_force)
237 if (m_force || d->m_data.m_slicePen.isThemed())
238 d->setPen(penColor, true);
238 d->setPen(penColor, true);
239
239
240 if (d->m_data.m_sliceBrush.isThemed() || m_force)
240 if (m_force || d->m_data.m_sliceBrush.isThemed())
241 d->setBrush(brushColor, true);
241 d->setBrush(brushColor, true);
242
242
243 if (d->m_data.m_labelBrush.isThemed() || m_force)
243 if (m_force || d->m_data.m_labelBrush.isThemed())
244 d->setLabelBrush(m_labelBrush.color(), true);
244 d->setLabelBrush(m_labelBrush.color(), true);
245
245
246 if (d->m_data.m_labelFont.isThemed() || m_force)
246 if (m_force || d->m_data.m_labelFont.isThemed())
247 d->setLabelFont(m_labelFont, true);
247 d->setLabelFont(m_labelFont, true);
248 }
248 }
249 }
249 }
250
250
251 void ChartTheme::decorate(QSplineSeries *series, int index)
251 void ChartTheme::decorate(QSplineSeries *series, int index)
252 {
252 {
253 QPen pen;
253 QPen pen;
254 if(pen == series->pen() || m_force){
254 if(m_force || pen == series->pen()){
255 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
255 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
256 pen.setWidthF(2);
256 pen.setWidthF(2);
257 series->setPen(pen);
257 series->setPen(pen);
258 }
258 }
259 }
259 }
260
260
261 void ChartTheme::decorate(QAbstractAxis *axis)
261 void ChartTheme::decorate(QAbstractAxis *axis)
262 {
262 {
263 QPen pen;
263 QPen pen;
264 QBrush brush;
264 QBrush brush;
265 QFont font;
265 QFont font;
266
266
267 bool axisX = axis->orientation()== Qt::Horizontal;
267 bool axisX = axis->orientation()== Qt::Horizontal;
268
268
269 if (axis->isArrowVisible()) {
269 if (axis->isArrowVisible()) {
270
270
271 if(brush == axis->labelsBrush() || m_force){
271 if(m_force || brush == axis->labelsBrush()){
272 axis->setLabelsBrush(m_labelBrush);
272 axis->setLabelsBrush(m_labelBrush);
273 }
273 }
274 if(pen == axis->labelsPen() || m_force){
274 if(m_force || pen == axis->labelsPen()){
275 axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
275 axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
276 }
276 }
277
277
278
278
279 if (axis->shadesVisible() || m_force) {
279 if (m_force || axis->shadesVisible()) {
280
280
281 if(brush == axis->shadesBrush() || m_force){
281 if(m_force || brush == axis->shadesBrush()){
282 axis->setShadesBrush(m_backgroundShadesBrush);
282 axis->setShadesBrush(m_backgroundShadesBrush);
283 }
283 }
284
284
285 if(pen == axis->shadesPen() || m_force){
285 if(m_force || pen == axis->shadesPen()){
286 axis->setShadesPen(m_backgroundShadesPen);
286 axis->setShadesPen(m_backgroundShadesPen);
287 }
287 }
288
288
289 if( m_force && (m_backgroundShades == BackgroundShadesBoth
289 if( m_force && (m_backgroundShades == BackgroundShadesBoth
290 || (m_backgroundShades == BackgroundShadesVertical && axisX)
290 || (m_backgroundShades == BackgroundShadesVertical && axisX)
291 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){
291 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){
292 axis->setShadesVisible(true);
292 axis->setShadesVisible(true);
293
293
294 }
294 }
295 }
295 }
296
296
297 if(pen == axis->axisPen() || m_force){
297 if(m_force || pen == axis->axisPen()){
298 axis->setAxisPen(m_axisLinePen);
298 axis->setAxisPen(m_axisLinePen);
299 }
299 }
300
300
301 if(pen == axis->gridLinePen() || m_force){
301 if(m_force || pen == axis->gridLinePen()){
302 axis->setGridLinePen(m_gridLinePen);
302 axis->setGridLinePen(m_gridLinePen);
303 }
303 }
304
304
305 if(font == axis->labelsFont() || m_force){
305 if(m_force || font == axis->labelsFont()){
306 axis->setLabelsFont(m_labelFont);
306 axis->setLabelsFont(m_labelFont);
307 }
307 }
308 }
308 }
309 }
309 }
310
310
311 void ChartTheme::generateSeriesGradients()
311 void ChartTheme::generateSeriesGradients()
312 {
312 {
313 // Generate gradients in HSV color space
313 // Generate gradients in HSV color space
314 foreach (const QColor& color, m_seriesColors) {
314 foreach (const QColor& color, m_seriesColors) {
315 QLinearGradient g;
315 QLinearGradient g;
316 qreal h = color.hsvHueF();
316 qreal h = color.hsvHueF();
317 qreal s = color.hsvSaturationF();
317 qreal s = color.hsvSaturationF();
318
318
319 // TODO: tune the algorithm to give nice results with most base colors defined in
319 // TODO: tune the algorithm to give nice results with most base colors defined in
320 // most themes. The rest of the gradients we can define manually in theme specific
320 // most themes. The rest of the gradients we can define manually in theme specific
321 // implementation.
321 // implementation.
322 QColor start = color;
322 QColor start = color;
323 start.setHsvF(h, 0.0, 1.0);
323 start.setHsvF(h, 0.0, 1.0);
324 g.setColorAt(0.0, start);
324 g.setColorAt(0.0, start);
325
325
326 g.setColorAt(0.5, color);
326 g.setColorAt(0.5, color);
327
327
328 QColor end = color;
328 QColor end = color;
329 end.setHsvF(h, s, 0.25);
329 end.setHsvF(h, s, 0.25);
330 g.setColorAt(1.0, end);
330 g.setColorAt(1.0, end);
331
331
332 m_seriesGradients << g;
332 m_seriesGradients << g;
333 }
333 }
334 }
334 }
335
335
336
336
337 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
337 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
338 {
338 {
339 Q_ASSERT(pos >= 0.0 && pos <= 1.0);
339 Q_ASSERT(pos >= 0.0 && pos <= 1.0);
340 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
340 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
341 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
341 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
342 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
342 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
343 QColor c;
343 QColor c;
344 c.setRgbF(r, g, b);
344 c.setRgbF(r, g, b);
345 return c;
345 return c;
346 }
346 }
347
347
348 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
348 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
349 {
349 {
350 Q_ASSERT(pos >= 0 && pos <= 1.0);
350 Q_ASSERT(pos >= 0 && pos <= 1.0);
351
351
352 QGradientStops stops = gradient.stops();
352 QGradientStops stops = gradient.stops();
353 int count = stops.count();
353 int count = stops.count();
354
354
355 // find previous stop relative to position
355 // find previous stop relative to position
356 QGradientStop prev = stops.first();
356 QGradientStop prev = stops.first();
357 for (int i = 0; i < count; i++) {
357 for (int i = 0; i < count; i++) {
358 QGradientStop stop = stops.at(i);
358 QGradientStop stop = stops.at(i);
359 if (pos > stop.first)
359 if (pos > stop.first)
360 prev = stop;
360 prev = stop;
361
361
362 // given position is actually a stop position?
362 // given position is actually a stop position?
363 if (pos == stop.first) {
363 if (pos == stop.first) {
364 //qDebug() << "stop color" << pos;
364 //qDebug() << "stop color" << pos;
365 return stop.second;
365 return stop.second;
366 }
366 }
367 }
367 }
368
368
369 // find next stop relative to position
369 // find next stop relative to position
370 QGradientStop next = stops.last();
370 QGradientStop next = stops.last();
371 for (int i = count - 1; i >= 0; i--) {
371 for (int i = count - 1; i >= 0; i--) {
372 QGradientStop stop = stops.at(i);
372 QGradientStop stop = stops.at(i);
373 if (pos < stop.first)
373 if (pos < stop.first)
374 next = stop;
374 next = stop;
375 }
375 }
376
376
377 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
377 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
378
378
379 qreal range = next.first - prev.first;
379 qreal range = next.first - prev.first;
380 qreal posDelta = pos - prev.first;
380 qreal posDelta = pos - prev.first;
381 qreal relativePos = posDelta / range;
381 qreal relativePos = posDelta / range;
382
382
383 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
383 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
384
384
385 return colorAt(prev.second, next.second, relativePos);
385 return colorAt(prev.second, next.second, relativePos);
386 }
386 }
387
387
388 void ChartTheme::setForced(bool enabled)
388 void ChartTheme::setForced(bool enabled)
389 {
389 {
390 m_force=enabled;
390 m_force = enabled;
391 }
391 }
392
392
393 QTCOMMERCIALCHART_END_NAMESPACE
393 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now