##// END OF EJS Templates
Fix...
Mika Salmela -
r2518:04911b229610
parent child
Show More
@@ -1,493 +1,493
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qboxset.h"
22 22 #include "qboxset_p.h"
23 23 #include "charthelpers_p.h"
24 24
25 25 #include <QDebug> //TODO: remove on release
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 /*!
30 30 \class QBoxSet
31 31 \brief Building block for box-and-whiskers chart
32 32
33 33 QBoxSet represents one box-and-whiskers item. It takes fives values to create a graphical representation
34 34 of range and three medians. There's two type of methods to give the values. The first one is with constructor
35 35 or append type of methods (append and operator <<). In these the values have to be given in order lower extreme,
36 36 lower quartile, median, upper quartile and upper extre. Second method is to create an empty QBoxSet instance and
37 37 give the values using own methods.
38 38
39 39 \mainclass
40 40
41 41 \sa QBoxPlotSeries
42 42 */
43 43 /*!
44 44 \qmlclass BoxSet QBoxSet
45 45
46 46 BoxSet represents one box-and-whiskers item. It takes fives values to create a graphical representation
47 47 of range and three medians. There's two type of methods to give the values. The first one is with constructor
48 48 or append type of methods (append and operator <<). In these the values have to be given in order lower extreme,
49 49 lower quartile, median, upper quartile and upper extre. Second method is to create an empty BoxSet instance and
50 50 give the values using own methods.
51 51 \sa BoxPlotSeries
52 52 */
53 53
54 54 /*!
55 55 \property QBoxSet::pen
56 56 \brief Defines the pen used by the box-and-whiskers set.
57 57 */
58 58
59 59 /*!
60 60 \property QBoxSet::brush
61 61 \brief Defines the brush used by the box-and-whiskers set.
62 62 */
63 63
64 64 /*!
65 65 \property QBoxSet::color
66 66 The fill (brush) color of the box-and-whiskers set.
67 67 */
68 68
69 69 /*!
70 70 \property QBoxSet::borderColor
71 71 The line (pen) color of the box-and-whiskers set.
72 72 */
73 73
74 74 /*!
75 75 \fn void QBoxSet::clicked()
76 76 The signal is emitted if the user clicks with a mouse on top of box-and-whisker item.
77 77 */
78 78
79 79 /*!
80 80 \fn void QBoxSet::hovered(bool status)
81 81
82 82 The signal is emitted if mouse is hovered on top of box-and-whisker item.
83 83 Parameter \a status is true, if mouse entered on top of item, false if mouse left from top of item.
84 84 */
85 85
86 86 /*!
87 87 \fn void QBoxSet::penChanged()
88 88 This signal is emitted when the pen of the box-and-whisker item has changed.
89 89 \sa pen
90 90 */
91 91
92 92 /*!
93 93 \fn void QBoxSet::brushChanged()
94 94 This signal is emitted when the brush of the box-and-whisker item has changed.
95 95 \sa brush
96 96 */
97 97
98 98 /*!
99 99 \fn void QBoxSet::colorChanged(QColor)
100 100 This signal is emitted when the fill (brush) color of the box-and-whisker item has changed to \a color.
101 101 */
102 102
103 103 /*!
104 104 \fn void QBoxSet::valuesAdded(int index, int count)
105 105 This signal is emitted when new values have been added to the box-and-whisker item.
106 106 Parameter \a index indicates the position of the first inserted value.
107 107 Parameter \a count is the number of inserted values.
108 108 \sa append(), insert()
109 109 */
110 110
111 111 /*!
112 112 \fn void QBoxSet::valueChanged(int index)
113 113 This signal is emitted values the value in the box-and-whisker item has been modified.
114 114 Parameter \a index indicates the position of the modified value.
115 115 \sa at()
116 116 */
117 117
118 118 /*!
119 119 \fn void QBoxSet::borderColorChanged(QColor)
120 120 This signal is emitted when the line (pen) color of the box-and-whisker item has changed to \a color.
121 121 */
122 122
123 123 /*!
124 124 Constructs QBoxSet with optional \a label and parent of \a parent
125 125 */
126 126 QBoxSet::QBoxSet(const QString label, QObject *parent)
127 127 : QObject(parent),
128 128 d_ptr(new QBoxSetPrivate(label, this))
129 129 {
130 130 }
131 131
132 132 /*!
133 133 Constructs QBoxSet with given ordered values. \a le for lower extreme, \a lq for lower quartile, \a m for median,
134 134 \a uq for upper quartile and \a ue for upper quartile. \a label and \a parent are optional.
135 135 */
136 QBoxSet::QBoxSet(const qreal le, const qreal lq, const qreal m, const qreal uq, const qreal ue, const QString label = "", QObject *parent)
136 QBoxSet::QBoxSet(const qreal le, const qreal lq, const qreal m, const qreal uq, const qreal ue, const QString label, QObject *parent)
137 137 : QObject(parent),
138 138 d_ptr(new QBoxSetPrivate(label, this))
139 139 {
140 140 d_ptr->append(le);
141 141 d_ptr->append(lq);
142 142 d_ptr->append(m);
143 143 d_ptr->append(uq);
144 144 d_ptr->append(ue);
145 145 }
146 146
147 147 /*!
148 148 Destroys the boxset
149 149 */
150 150 QBoxSet::~QBoxSet()
151 151 {
152 152 // NOTE: d_ptr destroyed by QObject
153 153 }
154 154
155 155 /*!
156 156 Appends new value \a value to the end of set.
157 157 */
158 158 void QBoxSet::append(const qreal value)
159 159 {
160 160 //int index = d_ptr->m_values.count();
161 161 d_ptr->append(value);
162 162
163 163 emit valuesAdded(d_ptr->m_valuesCount, 1);
164 164 }
165 165
166 166 /*!
167 167 Appends a list of reals to set. Works like append with single real value. The \a values in list
168 168 are appended to end of boxset
169 169 \sa append()
170 170 */
171 171 void QBoxSet::append(const QList<qreal> &values)
172 172 {
173 173 //int index = d_ptr->m_values.count();
174 174 d_ptr->append(values);
175 175 emit valuesAdded(d_ptr->m_valuesCount, values.count());
176 176 }
177 177
178 178 /*!
179 179 Sets new value \a value as the lower extreme for the set.
180 180 */
181 181 void QBoxSet::setLowerExtreme(const qreal value)
182 182 {
183 183 d_ptr->replace(QBoxSetPrivate::PosLowerExtreme, value);
184 184 emit d_ptr->restructuredBox();
185 185 emit valueChanged(QBoxSetPrivate::PosLowerExtreme);
186 186 }
187 187
188 188 /*!
189 189 Returns the lower extreme value of the set.
190 190 */
191 191 qreal QBoxSet::lowerExtreme()
192 192 {
193 193 return d_ptr->m_values[QBoxSetPrivate::PosLowerExtreme];
194 194 }
195 195
196 196 /*!
197 197 Sets new value \a value as the lower quartile for the set.
198 198 */
199 199 void QBoxSet::setLowerQuartile(const qreal value)
200 200 {
201 201 d_ptr->replace(QBoxSetPrivate::PosLowerQuartile, value);
202 202 emit d_ptr->restructuredBox();
203 203 emit valueChanged(QBoxSetPrivate::PosLowerQuartile);
204 204 }
205 205
206 206 /*!
207 207 Returns the lower quartile value of the set.
208 208 */
209 209 qreal QBoxSet::lowerQuartile()
210 210 {
211 211 return d_ptr->m_values[QBoxSetPrivate::PosLowerQuartile];
212 212 }
213 213
214 214 /*!
215 215 Sets new value \a value as the median for the set.
216 216 */
217 217 void QBoxSet::setMedian(const qreal value)
218 218 {
219 219 d_ptr->replace(QBoxSetPrivate::PosMedian, value);
220 220 emit d_ptr->restructuredBox();
221 221 emit valueChanged(QBoxSetPrivate::PosMedian);
222 222 }
223 223
224 224 /*!
225 225 Returns the median value of the set.
226 226 */
227 227 qreal QBoxSet::median()
228 228 {
229 229 return d_ptr->m_values[QBoxSetPrivate::PosMedian];
230 230 }
231 231
232 232 /*!
233 233 Sets new value \a value as the upper quartile for the set.
234 234 */
235 235 void QBoxSet::setUpperQuartile(const qreal value)
236 236 {
237 237 d_ptr->replace(QBoxSetPrivate::PosUpperQuartile, value);
238 238 emit d_ptr->restructuredBox();
239 239 emit valueChanged(QBoxSetPrivate::PosUpperQuartile);
240 240 }
241 241
242 242 /*!
243 243 Returns the upper quartile value of the set.
244 244 */
245 245 qreal QBoxSet::upperQuartile()
246 246 {
247 247 return d_ptr->m_values[QBoxSetPrivate::PosUpperQuartile];
248 248 }
249 249
250 250 /*!
251 251 Sets new value \a value as the upper extreme for the set.
252 252 */
253 253 void QBoxSet::setUpperExtreme(const qreal value)
254 254 {
255 255 d_ptr->replace(QBoxSetPrivate::PosUpperExtreme, value);
256 256 emit d_ptr->restructuredBox();
257 257 emit valueChanged(QBoxSetPrivate::PosUpperExtreme);
258 258 }
259 259
260 260 /*!
261 261 Returns the upper extreme value of the set.
262 262 */
263 263 qreal QBoxSet::upperExtreme()
264 264 {
265 265 return d_ptr->m_values[QBoxSetPrivate::PosUpperExtreme];
266 266 }
267 267
268 268 /*!
269 269 Sets new \a label for set.
270 270 */
271 271 void QBoxSet::setLabel(const QString label)
272 272 {
273 273 d_ptr->m_label = label;
274 274 }
275 275
276 276 /*!
277 277 Returns label of the set.
278 278 */
279 279 QString QBoxSet::label() const
280 280 {
281 281 return d_ptr->m_label;
282 282 }
283 283
284 284 /*!
285 285 Convenience operator. Same as append, with real \a value.
286 286 \sa append()
287 287 */
288 288 QBoxSet &QBoxSet::operator << (const qreal &value)
289 289 {
290 290 append(value);
291 291 return *this;
292 292 }
293 293
294 294 /*!
295 295 Inserts new \a value on the \a index position.
296 296 The value that is currently at this postion is moved to postion index + 1
297 297 */
298 298 void QBoxSet::insert(const int index, const qreal value)
299 299 {
300 300 d_ptr->insert(index, value);
301 301 emit valuesAdded(index, 1);
302 302 }
303 303
304 304 /*!
305 305 Sets a new value \a value to set, indexed by \a index
306 306 */
307 307 void QBoxSet::replace(const int index, const qreal value)
308 308 {
309 309 if (index >= 0 && index < 5) {
310 310 d_ptr->replace(index, value);
311 311 emit valueChanged(index);
312 312 }
313 313 }
314 314
315 315
316 316 /*!
317 317 Returns value of set indexed by \a index.
318 318 If the index is out of bounds 0.0 is returned.
319 319 */
320 320 qreal QBoxSet::at(const int index) const
321 321 {
322 322 if (index < 0 || index >= 5)
323 323 return 0;
324 324 return d_ptr->m_values[index];
325 325 }
326 326
327 327 /*!
328 328 Returns value of set indexed by \a index.
329 329 If the index is out of bounds 0.0 is returned.
330 330 */
331 331 qreal QBoxSet::operator [](const int index) const
332 332 {
333 333 return at(index);
334 334 }
335 335
336 336 /*!
337 337 Returns count of values in set.
338 338 */
339 339 int QBoxSet::count() const
340 340 {
341 341 return d_ptr->m_valuesCount;
342 342 }
343 343
344 344 /*!
345 345 Sets pen for set. Boxes of this set are drawn using \a pen
346 346 */
347 347 void QBoxSet::setPen(const QPen &pen)
348 348 {
349 349 if (d_ptr->m_pen != pen) {
350 350 d_ptr->m_pen = pen;
351 351 emit d_ptr->updatedBox();
352 352 emit penChanged();
353 353 }
354 354 }
355 355
356 356 /*!
357 357 Returns pen of the set.
358 358 */
359 359 QPen QBoxSet::pen() const
360 360 {
361 361 return d_ptr->m_pen;
362 362 }
363 363
364 364 /*!
365 365 Sets brush for the set. Boxes of this set are drawn using \a brush
366 366 */
367 367 void QBoxSet::setBrush(const QBrush &brush)
368 368 {
369 369 if (d_ptr->m_brush != brush) {
370 370 d_ptr->m_brush = brush;
371 371 emit d_ptr->updatedBox();
372 372 emit brushChanged();
373 373 }
374 374 }
375 375
376 376 /*!
377 377 Returns brush of the set.
378 378 */
379 379 QBrush QBoxSet::brush() const
380 380 {
381 381 return d_ptr->m_brush;
382 382 }
383 383
384 384 /*!
385 385 Returns the color of the brush of boxset.
386 386 */
387 387 QColor QBoxSet::color()
388 388 {
389 389 return brush().color();
390 390 }
391 391
392 392 /*!
393 393 Sets the \a color of brush for this boxset
394 394 */
395 395 void QBoxSet::setColor(QColor color)
396 396 {
397 397 QBrush b = brush();
398 398 if ((b.color() != color) || (b.style() == Qt::NoBrush)) {
399 399 b.setColor(color);
400 400 if (b.style() == Qt::NoBrush) {
401 401 // Set tyle to Qt::SolidPattern. (Default is Qt::NoBrush)
402 402 // This prevents theme to override color defined in QML side:
403 403 // BoxSet { label: "Bob"; color:"red"; values: [1,2,3] }
404 404 // The color must be obeyed, since user wanted it.
405 405 b.setStyle(Qt::SolidPattern);
406 406 }
407 407 setBrush(b);
408 408 emit colorChanged(color);
409 409 }
410 410 }
411 411
412 412 /*!
413 413 Returns the color of pen of this boxset
414 414 */
415 415 QColor QBoxSet::borderColor()
416 416 {
417 417 return pen().color();
418 418 }
419 419
420 420 /*!
421 421 Sets the color of pen for this boxset
422 422 */
423 423 void QBoxSet::setBorderColor(QColor color)
424 424 {
425 425 QPen p = pen();
426 426 if (p.color() != color) {
427 427 p.setColor(color);
428 428 setPen(p);
429 429 emit borderColorChanged(color);
430 430 }
431 431 }
432 432
433 433 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
434 434
435 435 QBoxSetPrivate::QBoxSetPrivate(const QString label, QBoxSet *parent) : QObject(parent),
436 436 q_ptr(parent),
437 437 m_label(label),
438 438 m_valuesCount(5),
439 439 m_appendCount(0),
440 440 m_pen(QPen(Qt::NoPen)),
441 441 m_brush(QBrush(Qt::NoBrush))
442 442 {
443 443 m_values = new qreal[m_valuesCount];
444 444 }
445 445
446 446 QBoxSetPrivate::~QBoxSetPrivate()
447 447 {
448 448 }
449 449
450 450 void QBoxSetPrivate::append(qreal value)
451 451 {
452 452 if (isValidValue(value) && m_appendCount < m_valuesCount) {
453 453 m_values[m_appendCount++] = value;
454 454 emit restructuredBox();
455 455 }
456 456 }
457 457
458 458 void QBoxSetPrivate::append(QList<qreal> values)
459 459 {
460 460 for (int i = 0; i < values.count(); i++) {
461 461 if (isValidValue(values.at(i)) && m_appendCount < m_valuesCount)
462 462 m_values[m_appendCount++] = values.at(i);
463 463 }
464 464 emit restructuredBox();
465 465 }
466 466
467 467 void QBoxSetPrivate::insert(const int index, const qreal value)
468 468 {
469 469 if (isValidValue(value)) {
470 470 for (int i = 4; i > index; i--)
471 471 m_values[i] = m_values[i - 1];
472 472 m_values[index] = value;
473 473 emit restructuredBox();
474 474 }
475 475 }
476 476
477 477 void QBoxSetPrivate::replace(const int index, const qreal value)
478 478 {
479 479 m_values[index] = value;
480 480 emit updatedLayout();
481 481 }
482 482
483 483 qreal QBoxSetPrivate::value(const int index)
484 484 {
485 485 if (index < 0 || index >= m_valuesCount)
486 486 return 0;
487 487 return m_values[index];
488 488 }
489 489
490 490 #include "moc_qboxset.cpp"
491 491 #include "moc_qboxset_p.cpp"
492 492
493 493 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now