qboxset.cpp
493 lines
| 11.9 KiB
| text/x-c
|
CppLexer
Mika Salmela
|
r2486 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2013 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "qboxset.h" | ||||
#include "qboxset_p.h" | ||||
#include "charthelpers_p.h" | ||||
Mika Salmela
|
r2508 | #include <QDebug> //TODO: remove on release | ||
Mika Salmela
|
r2486 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
/*! | ||||
Mika Salmela
|
r2517 | \class QBoxSet | ||
\brief Building block for box-and-whiskers chart | ||||
QBoxSet represents one box-and-whiskers item. It takes fives values to create a graphical representation | ||||
of range and three medians. There's two type of methods to give the values. The first one is with constructor | ||||
or append type of methods (append and operator <<). In these the values have to be given in order lower extreme, | ||||
lower quartile, median, upper quartile and upper extre. Second method is to create an empty QBoxSet instance and | ||||
give the values using own methods. | ||||
\mainclass | ||||
\sa QBoxPlotSeries | ||||
*/ | ||||
/*! | ||||
\qmlclass BoxSet QBoxSet | ||||
BoxSet represents one box-and-whiskers item. It takes fives values to create a graphical representation | ||||
of range and three medians. There's two type of methods to give the values. The first one is with constructor | ||||
or append type of methods (append and operator <<). In these the values have to be given in order lower extreme, | ||||
lower quartile, median, upper quartile and upper extre. Second method is to create an empty BoxSet instance and | ||||
give the values using own methods. | ||||
\sa BoxPlotSeries | ||||
*/ | ||||
/*! | ||||
\property QBoxSet::pen | ||||
\brief Defines the pen used by the box-and-whiskers set. | ||||
*/ | ||||
/*! | ||||
\property QBoxSet::brush | ||||
\brief Defines the brush used by the box-and-whiskers set. | ||||
*/ | ||||
/*! | ||||
\property QBoxSet::color | ||||
The fill (brush) color of the box-and-whiskers set. | ||||
*/ | ||||
/*! | ||||
\property QBoxSet::borderColor | ||||
The line (pen) color of the box-and-whiskers set. | ||||
*/ | ||||
/*! | ||||
\fn void QBoxSet::clicked() | ||||
The signal is emitted if the user clicks with a mouse on top of box-and-whisker item. | ||||
*/ | ||||
/*! | ||||
\fn void QBoxSet::hovered(bool status) | ||||
The signal is emitted if mouse is hovered on top of box-and-whisker item. | ||||
Parameter \a status is true, if mouse entered on top of item, false if mouse left from top of item. | ||||
*/ | ||||
/*! | ||||
\fn void QBoxSet::penChanged() | ||||
This signal is emitted when the pen of the box-and-whisker item has changed. | ||||
\sa pen | ||||
*/ | ||||
/*! | ||||
\fn void QBoxSet::brushChanged() | ||||
This signal is emitted when the brush of the box-and-whisker item has changed. | ||||
\sa brush | ||||
*/ | ||||
/*! | ||||
\fn void QBoxSet::colorChanged(QColor) | ||||
This signal is emitted when the fill (brush) color of the box-and-whisker item has changed to \a color. | ||||
*/ | ||||
/*! | ||||
\fn void QBoxSet::valuesAdded(int index, int count) | ||||
This signal is emitted when new values have been added to the box-and-whisker item. | ||||
Parameter \a index indicates the position of the first inserted value. | ||||
Parameter \a count is the number of inserted values. | ||||
\sa append(), insert() | ||||
*/ | ||||
/*! | ||||
\fn void QBoxSet::valueChanged(int index) | ||||
This signal is emitted values the value in the box-and-whisker item has been modified. | ||||
Parameter \a index indicates the position of the modified value. | ||||
\sa at() | ||||
*/ | ||||
/*! | ||||
\fn void QBoxSet::borderColorChanged(QColor) | ||||
This signal is emitted when the line (pen) color of the box-and-whisker item has changed to \a color. | ||||
*/ | ||||
/*! | ||||
Constructs QBoxSet with optional \a label and parent of \a parent | ||||
Mika Salmela
|
r2486 | */ | ||
Mika Salmela
|
r2513 | QBoxSet::QBoxSet(const QString label, QObject *parent) | ||
Mika Salmela
|
r2486 | : QObject(parent), | ||
Mika Salmela
|
r2513 | d_ptr(new QBoxSetPrivate(label, this)) | ||
Mika Salmela
|
r2486 | { | ||
} | ||||
Mika Salmela
|
r2517 | /*! | ||
Constructs QBoxSet with given ordered values. \a le for lower extreme, \a lq for lower quartile, \a m for median, | ||||
\a uq for upper quartile and \a ue for upper quartile. \a label and \a parent are optional. | ||||
*/ | ||||
Mika Salmela
|
r2518 | QBoxSet::QBoxSet(const qreal le, const qreal lq, const qreal m, const qreal uq, const qreal ue, const QString label, QObject *parent) | ||
Mika Salmela
|
r2486 | : QObject(parent), | ||
Mika Salmela
|
r2513 | d_ptr(new QBoxSetPrivate(label, this)) | ||
Mika Salmela
|
r2486 | { | ||
Mika Salmela
|
r2517 | d_ptr->append(le); | ||
d_ptr->append(lq); | ||||
d_ptr->append(m); | ||||
d_ptr->append(uq); | ||||
d_ptr->append(ue); | ||||
Mika Salmela
|
r2486 | } | ||
/*! | ||||
Destroys the boxset | ||||
*/ | ||||
QBoxSet::~QBoxSet() | ||||
{ | ||||
// NOTE: d_ptr destroyed by QObject | ||||
} | ||||
/*! | ||||
Appends new value \a value to the end of set. | ||||
*/ | ||||
void QBoxSet::append(const qreal value) | ||||
{ | ||||
Mika Salmela
|
r2508 | //int index = d_ptr->m_values.count(); | ||
Mika Salmela
|
r2486 | d_ptr->append(value); | ||
Mika Salmela
|
r2508 | emit valuesAdded(d_ptr->m_valuesCount, 1); | ||
Mika Salmela
|
r2486 | } | ||
/*! | ||||
Appends a list of reals to set. Works like append with single real value. The \a values in list | ||||
are appended to end of boxset | ||||
\sa append() | ||||
*/ | ||||
void QBoxSet::append(const QList<qreal> &values) | ||||
{ | ||||
Mika Salmela
|
r2508 | //int index = d_ptr->m_values.count(); | ||
Mika Salmela
|
r2486 | d_ptr->append(values); | ||
Mika Salmela
|
r2508 | emit valuesAdded(d_ptr->m_valuesCount, values.count()); | ||
Mika Salmela
|
r2486 | } | ||
/*! | ||||
Sets new value \a value as the lower extreme for the set. | ||||
*/ | ||||
void QBoxSet::setLowerExtreme(const qreal value) | ||||
{ | ||||
d_ptr->replace(QBoxSetPrivate::PosLowerExtreme, value); | ||||
Mika Salmela
|
r2508 | emit d_ptr->restructuredBox(); | ||
Mika Salmela
|
r2486 | emit valueChanged(QBoxSetPrivate::PosLowerExtreme); | ||
} | ||||
/*! | ||||
Returns the lower extreme value of the set. | ||||
*/ | ||||
qreal QBoxSet::lowerExtreme() | ||||
{ | ||||
Mika Salmela
|
r2508 | return d_ptr->m_values[QBoxSetPrivate::PosLowerExtreme]; | ||
Mika Salmela
|
r2486 | } | ||
/*! | ||||
Sets new value \a value as the lower quartile for the set. | ||||
*/ | ||||
void QBoxSet::setLowerQuartile(const qreal value) | ||||
{ | ||||
d_ptr->replace(QBoxSetPrivate::PosLowerQuartile, value); | ||||
Mika Salmela
|
r2508 | emit d_ptr->restructuredBox(); | ||
Mika Salmela
|
r2486 | emit valueChanged(QBoxSetPrivate::PosLowerQuartile); | ||
} | ||||
/*! | ||||
Returns the lower quartile value of the set. | ||||
*/ | ||||
qreal QBoxSet::lowerQuartile() | ||||
{ | ||||
Mika Salmela
|
r2508 | return d_ptr->m_values[QBoxSetPrivate::PosLowerQuartile]; | ||
Mika Salmela
|
r2486 | } | ||
/*! | ||||
Sets new value \a value as the median for the set. | ||||
*/ | ||||
void QBoxSet::setMedian(const qreal value) | ||||
{ | ||||
d_ptr->replace(QBoxSetPrivate::PosMedian, value); | ||||
Mika Salmela
|
r2508 | emit d_ptr->restructuredBox(); | ||
Mika Salmela
|
r2486 | emit valueChanged(QBoxSetPrivate::PosMedian); | ||
} | ||||
/*! | ||||
Returns the median value of the set. | ||||
*/ | ||||
qreal QBoxSet::median() | ||||
{ | ||||
Mika Salmela
|
r2508 | return d_ptr->m_values[QBoxSetPrivate::PosMedian]; | ||
Mika Salmela
|
r2486 | } | ||
/*! | ||||
Sets new value \a value as the upper quartile for the set. | ||||
*/ | ||||
void QBoxSet::setUpperQuartile(const qreal value) | ||||
{ | ||||
d_ptr->replace(QBoxSetPrivate::PosUpperQuartile, value); | ||||
Mika Salmela
|
r2508 | emit d_ptr->restructuredBox(); | ||
Mika Salmela
|
r2486 | emit valueChanged(QBoxSetPrivate::PosUpperQuartile); | ||
} | ||||
/*! | ||||
Returns the upper quartile value of the set. | ||||
*/ | ||||
qreal QBoxSet::upperQuartile() | ||||
{ | ||||
Mika Salmela
|
r2508 | return d_ptr->m_values[QBoxSetPrivate::PosUpperQuartile]; | ||
Mika Salmela
|
r2486 | } | ||
/*! | ||||
Sets new value \a value as the upper extreme for the set. | ||||
*/ | ||||
void QBoxSet::setUpperExtreme(const qreal value) | ||||
{ | ||||
Mika Salmela
|
r2508 | d_ptr->replace(QBoxSetPrivate::PosUpperExtreme, value); | ||
emit d_ptr->restructuredBox(); | ||||
emit valueChanged(QBoxSetPrivate::PosUpperExtreme); | ||||
Mika Salmela
|
r2486 | } | ||
/*! | ||||
Returns the upper extreme value of the set. | ||||
*/ | ||||
qreal QBoxSet::upperExtreme() | ||||
{ | ||||
Mika Salmela
|
r2508 | return d_ptr->m_values[QBoxSetPrivate::PosUpperExtreme]; | ||
Mika Salmela
|
r2486 | } | ||
Mika Salmela
|
r2513 | /*! | ||
Sets new \a label for set. | ||||
*/ | ||||
void QBoxSet::setLabel(const QString label) | ||||
{ | ||||
d_ptr->m_label = label; | ||||
} | ||||
/*! | ||||
Returns label of the set. | ||||
*/ | ||||
QString QBoxSet::label() const | ||||
{ | ||||
return d_ptr->m_label; | ||||
} | ||||
Mika Salmela
|
r2486 | /*! | ||
Convenience operator. Same as append, with real \a value. | ||||
\sa append() | ||||
*/ | ||||
QBoxSet &QBoxSet::operator << (const qreal &value) | ||||
{ | ||||
append(value); | ||||
return *this; | ||||
} | ||||
/*! | ||||
Inserts new \a value on the \a index position. | ||||
The value that is currently at this postion is moved to postion index + 1 | ||||
*/ | ||||
void QBoxSet::insert(const int index, const qreal value) | ||||
{ | ||||
d_ptr->insert(index, value); | ||||
emit valuesAdded(index, 1); | ||||
} | ||||
/*! | ||||
Sets a new value \a value to set, indexed by \a index | ||||
*/ | ||||
void QBoxSet::replace(const int index, const qreal value) | ||||
{ | ||||
Mika Salmela
|
r2508 | if (index >= 0 && index < 5) { | ||
Mika Salmela
|
r2486 | d_ptr->replace(index, value); | ||
emit valueChanged(index); | ||||
} | ||||
} | ||||
/*! | ||||
Returns value of set indexed by \a index. | ||||
If the index is out of bounds 0.0 is returned. | ||||
*/ | ||||
qreal QBoxSet::at(const int index) const | ||||
{ | ||||
Mika Salmela
|
r2508 | if (index < 0 || index >= 5) | ||
Mika Salmela
|
r2486 | return 0; | ||
Mika Salmela
|
r2508 | return d_ptr->m_values[index]; | ||
Mika Salmela
|
r2486 | } | ||
/*! | ||||
Returns value of set indexed by \a index. | ||||
If the index is out of bounds 0.0 is returned. | ||||
*/ | ||||
qreal QBoxSet::operator [](const int index) const | ||||
{ | ||||
return at(index); | ||||
} | ||||
/*! | ||||
Returns count of values in set. | ||||
*/ | ||||
int QBoxSet::count() const | ||||
{ | ||||
Mika Salmela
|
r2508 | return d_ptr->m_valuesCount; | ||
Mika Salmela
|
r2486 | } | ||
/*! | ||||
Sets pen for set. Boxes of this set are drawn using \a pen | ||||
*/ | ||||
void QBoxSet::setPen(const QPen &pen) | ||||
{ | ||||
if (d_ptr->m_pen != pen) { | ||||
d_ptr->m_pen = pen; | ||||
emit d_ptr->updatedBox(); | ||||
emit penChanged(); | ||||
} | ||||
} | ||||
/*! | ||||
Returns pen of the set. | ||||
*/ | ||||
QPen QBoxSet::pen() const | ||||
{ | ||||
return d_ptr->m_pen; | ||||
} | ||||
/*! | ||||
Sets brush for the set. Boxes of this set are drawn using \a brush | ||||
*/ | ||||
void QBoxSet::setBrush(const QBrush &brush) | ||||
{ | ||||
if (d_ptr->m_brush != brush) { | ||||
d_ptr->m_brush = brush; | ||||
emit d_ptr->updatedBox(); | ||||
emit brushChanged(); | ||||
} | ||||
} | ||||
/*! | ||||
Returns brush of the set. | ||||
*/ | ||||
QBrush QBoxSet::brush() const | ||||
{ | ||||
return d_ptr->m_brush; | ||||
} | ||||
/*! | ||||
Returns the color of the brush of boxset. | ||||
*/ | ||||
QColor QBoxSet::color() | ||||
{ | ||||
return brush().color(); | ||||
} | ||||
/*! | ||||
Sets the \a color of brush for this boxset | ||||
*/ | ||||
void QBoxSet::setColor(QColor color) | ||||
{ | ||||
QBrush b = brush(); | ||||
if ((b.color() != color) || (b.style() == Qt::NoBrush)) { | ||||
b.setColor(color); | ||||
if (b.style() == Qt::NoBrush) { | ||||
// Set tyle to Qt::SolidPattern. (Default is Qt::NoBrush) | ||||
// This prevents theme to override color defined in QML side: | ||||
// BoxSet { label: "Bob"; color:"red"; values: [1,2,3] } | ||||
// The color must be obeyed, since user wanted it. | ||||
b.setStyle(Qt::SolidPattern); | ||||
} | ||||
setBrush(b); | ||||
emit colorChanged(color); | ||||
} | ||||
} | ||||
/*! | ||||
Returns the color of pen of this boxset | ||||
*/ | ||||
QColor QBoxSet::borderColor() | ||||
{ | ||||
return pen().color(); | ||||
} | ||||
/*! | ||||
Sets the color of pen for this boxset | ||||
*/ | ||||
void QBoxSet::setBorderColor(QColor color) | ||||
{ | ||||
QPen p = pen(); | ||||
if (p.color() != color) { | ||||
p.setColor(color); | ||||
setPen(p); | ||||
emit borderColorChanged(color); | ||||
} | ||||
} | ||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
Mika Salmela
|
r2513 | QBoxSetPrivate::QBoxSetPrivate(const QString label, QBoxSet *parent) : QObject(parent), | ||
Mika Salmela
|
r2486 | q_ptr(parent), | ||
Mika Salmela
|
r2513 | m_label(label), | ||
Mika Salmela
|
r2508 | m_valuesCount(5), | ||
m_appendCount(0), | ||||
Mika Salmela
|
r2486 | m_pen(QPen(Qt::NoPen)), | ||
m_brush(QBrush(Qt::NoBrush)) | ||||
{ | ||||
Mika Salmela
|
r2508 | m_values = new qreal[m_valuesCount]; | ||
Mika Salmela
|
r2486 | } | ||
QBoxSetPrivate::~QBoxSetPrivate() | ||||
{ | ||||
} | ||||
void QBoxSetPrivate::append(qreal value) | ||||
{ | ||||
Mika Salmela
|
r2508 | if (isValidValue(value) && m_appendCount < m_valuesCount) { | ||
m_values[m_appendCount++] = value; | ||||
Mika Salmela
|
r2486 | emit restructuredBox(); | ||
} | ||||
} | ||||
void QBoxSetPrivate::append(QList<qreal> values) | ||||
{ | ||||
for (int i = 0; i < values.count(); i++) { | ||||
Mika Salmela
|
r2508 | if (isValidValue(values.at(i)) && m_appendCount < m_valuesCount) | ||
m_values[m_appendCount++] = values.at(i); | ||||
Mika Salmela
|
r2486 | } | ||
emit restructuredBox(); | ||||
} | ||||
void QBoxSetPrivate::insert(const int index, const qreal value) | ||||
{ | ||||
if (isValidValue(value)) { | ||||
Mika Salmela
|
r2508 | for (int i = 4; i > index; i--) | ||
m_values[i] = m_values[i - 1]; | ||||
m_values[index] = value; | ||||
Mika Salmela
|
r2486 | emit restructuredBox(); | ||
} | ||||
} | ||||
void QBoxSetPrivate::replace(const int index, const qreal value) | ||||
{ | ||||
Mika Salmela
|
r2508 | m_values[index] = value; | ||
Mika Salmela
|
r2486 | emit updatedLayout(); | ||
} | ||||
qreal QBoxSetPrivate::value(const int index) | ||||
{ | ||||
Mika Salmela
|
r2508 | if (index < 0 || index >= m_valuesCount) | ||
Mika Salmela
|
r2486 | return 0; | ||
Mika Salmela
|
r2508 | return m_values[index]; | ||
Mika Salmela
|
r2486 | } | ||
#include "moc_qboxset.cpp" | ||||
#include "moc_qboxset_p.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||