qbarset.cpp
639 lines
| 15.4 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r169 | #include "qbarset.h" | ||
|
r938 | #include "qbarset_p.h" | ||
|
r169 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r313 | /*! | ||
\class QBarSet | ||||
\brief part of QtCommercial chart API. | ||||
QBarSet represents one set of bars. Set of bars contains one data value for each category. | ||||
First value of set is assumed to belong to first category, second to second category and so on. | ||||
If set has fewer values than there are categories, then the missing values are assumed to be | ||||
at the end of set. For missing values in middle of a set, numerical value of zero is used. | ||||
|
r325 | \mainclass | ||
|
r1208 | \sa QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries | ||
|
r313 | */ | ||
|
r1489 | /*! | ||
\qmlclass BarSet QBarSet | ||||
BarSet represents one set of bars. Set of bars contains one data value for each category. | ||||
First value of set is assumed to belong to first category, second to second category and so on. | ||||
If set has fewer values than there are categories, then the missing values are assumed to be | ||||
at the end of set. For missing values in middle of a set, numerical value of zero is used. | ||||
\sa BarSeries, GroupedBarSeries, StackedBarSeries, PercentBarSeries | ||||
*/ | ||||
|
r313 | |||
|
r1361 | /*! | ||
|
r1429 | \property QBarSet::label | ||
|
r1489 | Defines the label of the barSet. | ||
*/ | ||||
/*! | ||||
\qmlproperty string BarSet::label | ||||
Defines the label of the barSet. | ||||
|
r1361 | */ | ||
/*! | ||||
\property QBarSet::pen | ||||
\brief Defines the pen used by the barSet. | ||||
*/ | ||||
/*! | ||||
\property QBarSet::brush | ||||
\brief Defines the brush used by the barSet. | ||||
*/ | ||||
/*! | ||||
\property QBarSet::labelBrush | ||||
\brief Defines the brush used by the barSet's label. | ||||
*/ | ||||
/*! | ||||
\property QBarSet::labelFont | ||||
\brief Defines the font used by the barSet's label. | ||||
*/ | ||||
|
r1465 | /*! | ||
\property QBarSet::color | ||||
|
r1489 | The fill (brush) color of the bar set. | ||
*/ | ||||
/*! | ||||
\qmlproperty color BarSet::color | ||||
The fill (brush) color of the bar set. | ||||
|
r1465 | */ | ||
/*! | ||||
\property QBarSet::borderColor | ||||
|
r1489 | The line (pen) color of the bar set. | ||
*/ | ||||
/*! | ||||
\qmlproperty color BarSet::borderColor | ||||
The line (pen) color of the bar set. | ||||
|
r1465 | */ | ||
/*! | ||||
\property QBarSet::labelColor | ||||
|
r1489 | The text (label) color of the bar set. | ||
*/ | ||||
/*! | ||||
\qmlproperty color BarSet::labelColor | ||||
The text (label) color of the bar set. | ||||
|
r1465 | */ | ||
|
r1490 | /*! | ||
\fn void QBarSet::clicked(int index) | ||||
The signal is emitted if the user clicks with a mouse on top of barset. | ||||
Clicked bar inside set is indexed by \a index | ||||
*/ | ||||
/*! | ||||
\fn void QBarSet::hovered(bool status) | ||||
The signal is emitted if mouse is hovered on top of barset. | ||||
Parameter \a status is true, if mouse entered on top of barset, false if mouse left from top of barset. | ||||
*/ | ||||
|
r1361 | /*! | ||
|
r1429 | \fn void QBarSet::labelChanged() | ||
This signal is emitted when the label of the barSet has changed. | ||||
\sa label | ||||
|
r1361 | */ | ||
/*! | ||||
\fn void QBarSet::penChanged() | ||||
This signal is emitted when the pen of the barSet has changed. | ||||
\sa pen | ||||
*/ | ||||
/*! | ||||
\fn void QBarSet::brushChanged() | ||||
This signal is emitted when the brush of the barSet has changed. | ||||
\sa brush | ||||
*/ | ||||
/*! | ||||
\fn void QBarSet::labelBrushChanged() | ||||
This signal is emitted when the brush of the barSet's label has changed. | ||||
\sa labelBrush | ||||
*/ | ||||
/*! | ||||
\fn void QBarSet::labelFontChanged() | ||||
This signal is emitted when the font of the barSet's label has changed. | ||||
\sa labelBrush | ||||
*/ | ||||
|
r1465 | /*! | ||
\fn void QBarSet::colorChanged(QColor) | ||||
This signal is emitted when the fill (brush) color of the set has changed to \a color. | ||||
*/ | ||||
|
r1489 | /*! | ||
\qmlsignal BarSet::onColorChanged(color color) | ||||
This signal is emitted when the fill (brush) color of the set has changed to \a color. | ||||
*/ | ||||
|
r1465 | |||
/*! | ||||
\fn void QBarSet::borderColorChanged(QColor) | ||||
This signal is emitted when the line (pen) color of the set has changed to \a color. | ||||
*/ | ||||
|
r1489 | /*! | ||
\qmlsignal BarSet::onBorderColorChanged(color color) | ||||
This signal is emitted when the line (pen) color of the set has changed to \a color. | ||||
*/ | ||||
|
r1465 | |||
/*! | ||||
\fn void QBarSet::labelColorChanged(QColor) | ||||
This signal is emitted when the text (label) color of the set has changed to \a color. | ||||
*/ | ||||
|
r1489 | /*! | ||
\qmlsignal BarSet::onLabelColorChanged(color color) | ||||
This signal is emitted when the text (label) color of the set has changed to \a color. | ||||
*/ | ||||
|
r1465 | |||
|
r1361 | /*! | ||
\fn void QBarSet::valuesAdded(int index, int count) | ||||
This signal is emitted when new values have been added to the set. | ||||
Parameter \a index indicates the position of the first inserted value. | ||||
Parameter \a count is the number of iserted values. | ||||
\sa append(), insert() | ||||
*/ | ||||
|
r1489 | /*! | ||
\qmlsignal BarSet::onValuesAdded(int index, int count) | ||||
This signal is emitted when new values have been added to the set. | ||||
Parameter \a index indicates the position of the first inserted value. | ||||
Parameter \a count is the number of iserted values. | ||||
*/ | ||||
|
r1361 | |||
/*! | ||||
\fn void QBarSet::valuesRemoved(int index, int count) | ||||
This signal is emitted values have been removed from the set. | ||||
Parameter \a index indicates the position of the first removed value. | ||||
Parameter \a count is the number of removed values. | ||||
\sa remove() | ||||
*/ | ||||
|
r1489 | /*! | ||
\qmlsignal BarSet::onValuesRemoved(int index, int count) | ||||
This signal is emitted values have been removed from the set. | ||||
Parameter \a index indicates the position of the first removed value. | ||||
Parameter \a count is the number of removed values. | ||||
*/ | ||||
|
r1361 | |||
/*! | ||||
\fn void QBarSet::valueChanged(int index) | ||||
This signal is emitted values the value in the set has been modified. | ||||
Parameter \a index indicates the position of the modified value. | ||||
\sa at() | ||||
*/ | ||||
|
r1489 | /*! | ||
\qmlsignal BarSet::onValueChanged(int index) | ||||
This signal is emitted values the value in the set has been modified. | ||||
Parameter \a index indicates the position of the modified value. | ||||
*/ | ||||
|
r1361 | |||
|
r313 | /*! | ||
|
r1429 | Constructs QBarSet with a label of \a label and with parent of \a parent | ||
|
r313 | */ | ||
|
r1429 | QBarSet::QBarSet(const QString label, QObject *parent) | ||
|
r276 | : QObject(parent) | ||
|
r1429 | ,d_ptr(new QBarSetPrivate(label,this)) | ||
|
r169 | { | ||
} | ||||
|
r980 | /*! | ||
Destroys the barset | ||||
*/ | ||||
|
r944 | QBarSet::~QBarSet() | ||
{ | ||||
|
r980 | // NOTE: d_ptr destroyed by QObject | ||
|
r944 | } | ||
|
r313 | /*! | ||
|
r1429 | Sets new \a label for set. | ||
|
r313 | */ | ||
|
r1429 | void QBarSet::setLabel(const QString label) | ||
|
r169 | { | ||
|
r1429 | d_ptr->m_label = label; | ||
emit labelChanged(); | ||||
|
r169 | } | ||
|
r313 | |||
/*! | ||||
|
r1429 | Returns label of the set. | ||
|
r313 | */ | ||
|
r1429 | QString QBarSet::label() const | ||
|
r171 | { | ||
|
r1429 | return d_ptr->m_label; | ||
|
r171 | } | ||
|
r169 | |||
|
r1208 | /*! | ||
Appends a point to set. Parameter \a value x coordinate defines the | ||||
position in x-axis and y coordinate defines the height of bar. | ||||
Depending on presentation (QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries) | ||||
the x values are used or ignored. | ||||
*/ | ||||
|
r1167 | void QBarSet::append(const QPointF value) | ||
{ | ||||
|
r1353 | int index = d_ptr->m_values.count(); | ||
d_ptr->append(value); | ||||
emit valuesAdded(index, 1); | ||||
|
r1167 | } | ||
|
r1208 | /*! | ||
|
r1243 | Appends a list of \a values to set. Works like append with single point. | ||
|
r1208 | \sa append() | ||
*/ | ||||
|
r1500 | void QBarSet::append(const QList<QPointF> &values) | ||
|
r1167 | { | ||
|
r1353 | int index = d_ptr->m_values.count(); | ||
d_ptr->append(values); | ||||
emit valuesAdded(index, values.count()); | ||||
|
r1167 | } | ||
|
r313 | /*! | ||
|
r1208 | Appends new value \a value to the end of set. Internally the value is converted to QPointF, | ||
with x coordinate being the index of appended value and y coordinate is the value. | ||||
|
r313 | */ | ||
|
r993 | void QBarSet::append(const qreal value) | ||
|
r169 | { | ||
|
r1353 | // Convert to QPointF and use other append(QPointF) method. | ||
|
r1167 | append(QPointF(d_ptr->m_values.count(), value)); | ||
} | ||||
|
r1208 | /*! | ||
|
r1361 | Appends a list of reals to set. Works like append with single real value. The \a values in list | ||
|
r1208 | are converted to QPointF, where x coordinate is the index of point and y coordinate is the value. | ||
\sa append() | ||||
*/ | ||||
|
r1500 | void QBarSet::append(const QList<qreal> &values) | ||
|
r1167 | { | ||
int index = d_ptr->m_values.count(); | ||||
|
r1353 | d_ptr->append(values); | ||
emit valuesAdded(index, values.count()); | ||||
|
r993 | } | ||
/*! | ||||
|
r1208 | Convinience operator. Same as append, with real \a value. | ||
\sa append() | ||||
|
r993 | */ | ||
QBarSet& QBarSet::operator << (const qreal &value) | ||||
{ | ||||
append(value); | ||||
|
r169 | return *this; | ||
} | ||||
|
r1208 | /*! | ||
Convinience operator. Same as append, with QPointF \a value. | ||||
\sa append() | ||||
*/ | ||||
|
r1167 | QBarSet& QBarSet::operator << (const QPointF &value) | ||
{ | ||||
append(value); | ||||
return *this; | ||||
} | ||||
|
r901 | /*! | ||
|
r993 | Inserts new \a value on the \a index position. | ||
The value that is currently at this postion is moved to postion index + 1 | ||||
\sa remove() | ||||
|
r901 | */ | ||
|
r993 | void QBarSet::insert(const int index, const qreal value) | ||
|
r662 | { | ||
|
r1353 | d_ptr->insert(index, value); | ||
emit valuesAdded(index,1); | ||||
} | ||||
/*! | ||||
Inserts new \a value on the \a index position. | ||||
The value that is currently at this postion is moved to postion index + 1 | ||||
\sa remove() | ||||
*/ | ||||
void QBarSet::insert(const int index, const QPointF value) | ||||
{ | ||||
d_ptr->insert(index,value); | ||||
emit valuesAdded(index,1); | ||||
|
r662 | } | ||
|
r901 | /*! | ||
|
r1361 | Removes \a count number of values from the set starting at \a index. | ||
Returns true if remove operation was succesfull. | ||||
|
r993 | \sa insert() | ||
|
r901 | */ | ||
|
r1353 | bool QBarSet::remove(const int index, const int count) | ||
|
r663 | { | ||
|
r1353 | bool success = d_ptr->remove(index,count); | ||
if (success) { | ||||
emit valuesRemoved(index,count); | ||||
} | ||||
return success; | ||||
|
r663 | } | ||
|
r313 | /*! | ||
|
r993 | Sets a new value \a value to set, indexed by \a index | ||
|
r313 | */ | ||
|
r993 | void QBarSet::replace(const int index, const qreal value) | ||
|
r171 | { | ||
|
r1353 | d_ptr->replace(index,value); | ||
emit valueChanged(index); | ||||
} | ||||
/*! | ||||
Sets a new value \a value to set, indexed by \a index | ||||
*/ | ||||
void QBarSet::replace(const int index, const QPointF value) | ||||
{ | ||||
d_ptr->replace(index,value); | ||||
emit valueChanged(index); | ||||
|
r171 | } | ||
|
r313 | /*! | ||
|
r1208 | Returns value of set indexed by \a index. Note that all appended values are stored internally as QPointF. | ||
The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value | ||||
of the QPointF (if appended with QPointF append). | ||||
If the index is out of bounds QPointF(0, 0.0) is returned. | ||||
|
r313 | */ | ||
|
r1167 | QPointF QBarSet::at(const int index) const | ||
|
r171 | { | ||
|
r1168 | if (index < 0 || index >= d_ptr->m_values.count()) { | ||
return QPointF(index, 0.0); | ||||
} | ||||
|
r1162 | |||
|
r938 | return d_ptr->m_values.at(index); | ||
|
r171 | } | ||
|
r313 | /*! | ||
|
r1208 | Returns value of set indexed by \a index. ote that all appended values are stored internally as QPointF. | ||
The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value | ||||
of the QPointF (if appended with QPointF append). | ||||
|
r313 | */ | ||
|
r1167 | QPointF QBarSet::operator [](const int index) const | ||
|
r183 | { | ||
|
r993 | return d_ptr->m_values.at(index); | ||
} | ||||
/*! | ||||
Returns count of values in set. | ||||
*/ | ||||
int QBarSet::count() const | ||||
{ | ||||
return d_ptr->m_values.count(); | ||||
|
r183 | } | ||
|
r492 | /*! | ||
|
r1208 | Returns sum of all values in barset. The sum is sum of y coordinates in the QPointF representation. | ||
|
r492 | */ | ||
|
r934 | qreal QBarSet::sum() const | ||
|
r438 | { | ||
qreal total(0); | ||||
|
r938 | for (int i=0; i < d_ptr->m_values.count(); i++) { | ||
|
r1167 | //total += d_ptr->m_values.at(i); | ||
total += d_ptr->m_values.at(i).y(); | ||||
|
r438 | } | ||
return total; | ||||
} | ||||
|
r313 | /*! | ||
Sets pen for set. Bars of this set are drawn using \a pen | ||||
*/ | ||||
|
r763 | void QBarSet::setPen(const QPen &pen) | ||
|
r214 | { | ||
|
r938 | if(d_ptr->m_pen!=pen){ | ||
d_ptr->m_pen = pen; | ||||
|
r1008 | emit d_ptr->updatedBars(); | ||
|
r1353 | emit penChanged(); | ||
|
r938 | } | ||
|
r214 | } | ||
|
r313 | /*! | ||
Returns pen of the set. | ||||
*/ | ||||
|
r473 | QPen QBarSet::pen() const | ||
|
r214 | { | ||
|
r938 | return d_ptr->m_pen; | ||
|
r214 | } | ||
|
r313 | /*! | ||
Sets brush for the set. Bars of this set are drawn using \a brush | ||||
*/ | ||||
|
r763 | void QBarSet::setBrush(const QBrush &brush) | ||
|
r214 | { | ||
|
r938 | if(d_ptr->m_brush!=brush){ | ||
d_ptr->m_brush = brush; | ||||
|
r1008 | emit d_ptr->updatedBars(); | ||
|
r1353 | emit brushChanged(); | ||
|
r938 | } | ||
|
r214 | } | ||
|
r313 | /*! | ||
Returns brush of the set. | ||||
*/ | ||||
|
r473 | QBrush QBarSet::brush() const | ||
|
r214 | { | ||
|
r938 | return d_ptr->m_brush; | ||
|
r214 | } | ||
|
r820 | /*! | ||
|
r839 | Sets \a brush of the values that are drawn on top of this barset | ||
|
r820 | */ | ||
void QBarSet::setLabelBrush(const QBrush &brush) | ||||
{ | ||||
|
r938 | if(d_ptr->m_labelBrush!=brush){ | ||
d_ptr->m_labelBrush = brush; | ||||
|
r1008 | emit d_ptr->updatedBars(); | ||
|
r1353 | emit labelBrushChanged(); | ||
|
r938 | } | ||
|
r820 | } | ||
/*! | ||||
Returns brush of the values that are drawn on top of this barset | ||||
*/ | ||||
QBrush QBarSet::labelBrush() const | ||||
{ | ||||
|
r938 | return d_ptr->m_labelBrush; | ||
|
r820 | } | ||
/*! | ||||
|
r839 | Sets the \a font for values that are drawn on top of this barset | ||
|
r820 | */ | ||
void QBarSet::setLabelFont(const QFont &font) | ||||
{ | ||||
|
r938 | if(d_ptr->m_labelFont!=font) { | ||
d_ptr->m_labelFont = font; | ||||
|
r1008 | emit d_ptr->updatedBars(); | ||
|
r1353 | emit labelFontChanged(); | ||
|
r938 | } | ||
|
r512 | } | ||
/*! | ||||
|
r1493 | Returns the pen for values that are drawn on top of this barset | ||
|
r512 | */ | ||
|
r820 | QFont QBarSet::labelFont() const | ||
|
r512 | { | ||
|
r938 | return d_ptr->m_labelFont; | ||
|
r512 | } | ||
|
r1493 | /*! | ||
Returns the color of the brush of barset. | ||||
*/ | ||||
|
r1465 | QColor QBarSet::color() | ||
{ | ||||
return brush().color(); | ||||
} | ||||
|
r1493 | /*! | ||
Sets the \a color of brush for this barset | ||||
*/ | ||||
|
r1465 | void QBarSet::setColor(QColor color) | ||
{ | ||||
QBrush b = brush(); | ||||
if (b.color() != color) { | ||||
b.setColor(color); | ||||
setBrush(b); | ||||
emit colorChanged(color); | ||||
} | ||||
} | ||||
|
r1493 | /*! | ||
Returns the color of pen of this barset | ||||
*/ | ||||
|
r1465 | QColor QBarSet::borderColor() | ||
{ | ||||
return pen().color(); | ||||
} | ||||
|
r1493 | /*! | ||
Sets the color of pen for this barset | ||||
*/ | ||||
|
r1465 | void QBarSet::setBorderColor(QColor color) | ||
{ | ||||
QPen p = pen(); | ||||
if (p.color() != color) { | ||||
p.setColor(color); | ||||
setPen(p); | ||||
emit borderColorChanged(color); | ||||
} | ||||
} | ||||
|
r1493 | /*! | ||
Returns the color of labels of this barset | ||||
*/ | ||||
|
r1465 | QColor QBarSet::labelColor() | ||
{ | ||||
return labelBrush().color(); | ||||
} | ||||
|
r1493 | /*! | ||
Sets the color of labels for this barset | ||||
*/ | ||||
|
r1465 | void QBarSet::setLabelColor(QColor color) | ||
{ | ||||
QBrush b = labelBrush(); | ||||
if (b.color() != color) { | ||||
b.setColor(color); | ||||
setLabelBrush(b); | ||||
emit labelColorChanged(color); | ||||
} | ||||
} | ||||
|
r938 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
r1429 | QBarSetPrivate::QBarSetPrivate(const QString label, QBarSet *parent) : QObject(parent), | ||
|
r938 | q_ptr(parent), | ||
|
r1429 | m_label(label) | ||
|
r938 | { | ||
} | ||||
QBarSetPrivate::~QBarSetPrivate() | ||||
{ | ||||
} | ||||
|
r1017 | |||
|
r1353 | void QBarSetPrivate::append(QPointF value) | ||
{ | ||||
m_values.append(value); | ||||
emit restructuredBars(); | ||||
} | ||||
void QBarSetPrivate::append(QList<QPointF> values) | ||||
{ | ||||
for (int i=0; i<values.count(); i++) { | ||||
m_values.append(values.at(i)); | ||||
} | ||||
emit restructuredBars(); | ||||
} | ||||
void QBarSetPrivate::append(QList<qreal> values) | ||||
{ | ||||
int index = m_values.count(); | ||||
for (int i=0; i<values.count(); i++) { | ||||
m_values.append(QPointF(index,values.at(i))); | ||||
index++; | ||||
} | ||||
emit restructuredBars(); | ||||
} | ||||
void QBarSetPrivate::insert(const int index, const qreal value) | ||||
{ | ||||
m_values.insert(index, QPointF(index, value)); | ||||
emit restructuredBars(); | ||||
} | ||||
void QBarSetPrivate::insert(const int index, const QPointF value) | ||||
{ | ||||
m_values.insert(index, value); | ||||
emit restructuredBars(); | ||||
} | ||||
bool QBarSetPrivate::remove(const int index, const int count) | ||||
{ | ||||
if ((index + count) > m_values.count()) { | ||||
// cant remove more values than there are | ||||
return false; | ||||
} | ||||
int c = count; | ||||
while (c > 0) { | ||||
m_values.removeAt(index); | ||||
c--; | ||||
} | ||||
emit restructuredBars(); | ||||
return true; | ||||
} | ||||
void QBarSetPrivate::replace(const int index, const qreal value) | ||||
{ | ||||
m_values.replace(index,QPointF(index,value)); | ||||
emit updatedBars(); | ||||
} | ||||
void QBarSetPrivate::replace(const int index, const QPointF value) | ||||
{ | ||||
m_values.replace(index,value); | ||||
emit updatedBars(); | ||||
} | ||||
|
r239 | #include "moc_qbarset.cpp" | ||
|
r938 | #include "moc_qbarset_p.cpp" | ||
|
r737 | |||
|
r169 | QTCOMMERCIALCHART_END_NAMESPACE | ||