From f8e9430063264df110b449856bd953f3bd288211 2012-04-11 14:46:31 From: sauimone Date: 2012-04-11 14:46:31 Subject: [PATCH] barset: unified insert/set/remove methods --- diff --git a/src/barchart/barchartitem.cpp b/src/barchart/barchartitem.cpp index 1131801..87a5627 100644 --- a/src/barchart/barchartitem.cpp +++ b/src/barchart/barchartitem.cpp @@ -122,7 +122,7 @@ QVector BarChartItem::calculateLayout() for (int set = 0; set < setCount; set++) { QBarSet* barSet = m_series->d_func()->barsetAt(set); - qreal barHeight = barSet->valueAt(category) * scale; + qreal barHeight = barSet->at(category) * scale; Bar* bar = m_bars.at(itemIndex); QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); @@ -132,8 +132,8 @@ QVector BarChartItem::calculateLayout() BarLabel* label = m_labels.at(itemIndex); - if (!qFuzzyIsNull(barSet->valueAt(category))) { - label->setText(QString::number(barSet->valueAt(category))); + if (!qFuzzyIsNull(barSet->at(category))) { + label->setText(QString::number(barSet->at(category))); } else { label->setText(QString("")); } diff --git a/src/barchart/barchartmodel.cpp b/src/barchart/barchartmodel.cpp index d6a42a5..538c679 100644 --- a/src/barchart/barchartmodel.cpp +++ b/src/barchart/barchartmodel.cpp @@ -93,7 +93,7 @@ qreal BarChartModel::min() const for (int i = 0; i < m_dataModel.count(); i++) { int itemCount = m_dataModel.at(i)->count(); for (int j = 0; j < itemCount; j++) { - qreal temp = m_dataModel.at(i)->valueAt(j); + qreal temp = m_dataModel.at(i)->at(j); if (temp < min) min = temp; } @@ -111,7 +111,7 @@ qreal BarChartModel::max() const for (int i = 0; i < m_dataModel.count(); i++) { int itemCount = m_dataModel.at(i)->count(); for (int j = 0; j < itemCount; j++) { - qreal temp = m_dataModel.at(i)->valueAt(j); + qreal temp = m_dataModel.at(i)->at(j); if (temp > max) max = temp; } @@ -130,7 +130,7 @@ qreal BarChartModel::valueAt(int set, int category) const return 0; } - return m_dataModel.at(set)->valueAt(category); + return m_dataModel.at(set)->at(category); } qreal BarChartModel::percentageAt(int set, int category) const @@ -143,7 +143,7 @@ qreal BarChartModel::percentageAt(int set, int category) const return 0; } - qreal value = m_dataModel.at(set)->valueAt(category); + qreal value = m_dataModel.at(set)->at(category); qreal total = categorySum(category); if ( qFuzzyCompare(total, 0) ) return 0; @@ -158,7 +158,7 @@ qreal BarChartModel::categorySum(int category) const for (int set = 0; set < count; set++) { if (category < m_dataModel.at(set)->count()) - sum += m_dataModel.at(set)->valueAt(category); + sum += m_dataModel.at(set)->at(category); } return sum; } @@ -170,7 +170,7 @@ qreal BarChartModel::absoluteCategorySum(int category) const for (int set = 0; set < count; set++) { if (category < m_dataModel.at(set)->count()) - sum += qAbs(m_dataModel.at(set)->valueAt(category)); + sum += qAbs(m_dataModel.at(set)->at(category)); } return sum; } diff --git a/src/barchart/percentbarchartitem.cpp b/src/barchart/percentbarchartitem.cpp index 540adeb..3b3061c 100644 --- a/src/barchart/percentbarchartitem.cpp +++ b/src/barchart/percentbarchartitem.cpp @@ -55,7 +55,7 @@ QVector PercentBarChartItem::calculateLayout() qreal yPos = height + domainScale * m_domainMinY + geometry().topLeft().y(); for (int set=0; set < m_series->barsetCount(); set++) { QBarSet* barSet = m_series->d_func()->barsetAt(set); - qreal barHeight = barSet->valueAt(category) * percentage * domainScale; + qreal barHeight = barSet->at(category) * percentage * domainScale; Bar* bar = m_bars.at(itemIndex); bar->setPen(barSet->pen()); bar->setBrush(barSet->brush()); diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index 1e9361a..f299cde 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -387,13 +387,13 @@ void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRigh { // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop) - barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); + barsetAt(topLeft.column() - m_mapBarBottom)->replace(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); } else { // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop) - barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); + barsetAt(topLeft.row() - m_mapBarBottom)->replace(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); } } diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index ff979fa..f90f532 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -60,7 +60,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE /*! Constructs QBarSet with a name of \a name and with parent of \a parent */ -QBarSet::QBarSet(QString name, QObject *parent) +QBarSet::QBarSet(const QString name, QObject *parent) : QObject(parent) ,d_ptr(new QBarSetPrivate(name,this)) { @@ -77,7 +77,7 @@ QBarSet::~QBarSet() /*! Sets new \a name for set. */ -void QBarSet::setName(QString name) +void QBarSet::setName(const QString name) { d_ptr->m_name = name; } @@ -93,55 +93,71 @@ QString QBarSet::name() const /*! Appends new value \a value to the end of set. */ -QBarSet& QBarSet::operator << (const qreal &value) +void QBarSet::append(const qreal value) { d_ptr->m_values.append(value); emit d_ptr->structureChanged(); +} + +/*! + Appends new value \a value to the end of set. +*/ +QBarSet& QBarSet::operator << (const qreal &value) +{ + append(value); return *this; } /*! - Inserts new \a value on the \a i position. - The value that is currently at this postion is moved to postion i + 1 - \sa removeValue() + 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::insertValue(int i, qreal value) +void QBarSet::insert(const int index, const qreal value) { - d_ptr->m_values.insert(i, value); + d_ptr->m_values.insert(index, value); } /*! - Removes the value specified by \a i - \sa insertValue() + Removes the value specified by \a index + \sa insert() */ -void QBarSet::removeValue(int i) +void QBarSet::remove(const int index) { - d_ptr->m_values.removeAt(i); + d_ptr->m_values.removeAt(index); } /*! - Returns count of values in set. + Sets a new value \a value to set, indexed by \a index */ -int QBarSet::count() const +void QBarSet::replace(const int index, const qreal value) { - return d_ptr->m_values.count(); + d_ptr->m_values.replace(index,value); + emit d_ptr->valueChanged(); } /*! Returns value of set indexed by \a index */ -qreal QBarSet::valueAt(int index) const +qreal QBarSet::at(const int index) const { return d_ptr->m_values.at(index); } /*! - Sets a new value \a value to set, indexed by \a index + Returns value of set indexed by \a index */ -void QBarSet::setValue(int index, qreal value) +qreal QBarSet::operator [] (int index) const { - d_ptr->m_values.replace(index,value); - emit d_ptr->valueChanged(); + return d_ptr->m_values.at(index); +} + +/*! + Returns count of values in set. +*/ +int QBarSet::count() const +{ + return d_ptr->m_values.count(); } /*! @@ -274,7 +290,7 @@ bool QBarSet::labelsVisible() const //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -QBarSetPrivate::QBarSetPrivate(QString name, QBarSet *parent) : QObject(parent), +QBarSetPrivate::QBarSetPrivate(const QString name, QBarSet *parent) : QObject(parent), q_ptr(parent), m_name(name), m_labelsVisible(false) diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index fc83a8a..8aa3f25 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -34,18 +34,20 @@ class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject Q_OBJECT public: - explicit QBarSet(QString name, QObject *parent = 0); + explicit QBarSet(const QString name, QObject *parent = 0); virtual ~QBarSet(); - void setName(QString name); + void setName(const QString name); QString name() const; - QBarSet& operator << (const qreal &value); // appends new value to set - void insertValue(int i, qreal value); - void removeValue(int i); - int count() const; // count of values in set - qreal valueAt(int index) const; // for modifying individual values - void setValue(int index, qreal value); // setter for individual value - qreal sum() const; // sum of all values in the set + void append(const qreal value); + QBarSet& operator << (const qreal &value); + void insert(const int index, const qreal value); + void remove(const int index); + void replace(const int index, const qreal value); + qreal at(const int index) const; + qreal operator [] (int index) const; + int count() const; + qreal sum() const; void setPen(const QPen &pen); QPen pen() const; diff --git a/src/barchart/qbarset_p.h b/src/barchart/qbarset_p.h index 3f051f8..36999ac 100644 --- a/src/barchart/qbarset_p.h +++ b/src/barchart/qbarset_p.h @@ -10,7 +10,7 @@ class QBarSetPrivate : public QObject Q_OBJECT public: - QBarSetPrivate(QString name, QBarSet *parent); + QBarSetPrivate(const QString name, QBarSet *parent); ~QBarSetPrivate(); Q_SIGNALS: diff --git a/src/barchart/stackedbarchartitem.cpp b/src/barchart/stackedbarchartitem.cpp index 7a5a345..e4192ee 100644 --- a/src/barchart/stackedbarchartitem.cpp +++ b/src/barchart/stackedbarchartitem.cpp @@ -53,7 +53,7 @@ QVector StackedBarChartItem::calculateLayout() for (int set=0; set < m_series->barsetCount(); set++) { QBarSet* barSet = m_series->d_func()->barsetAt(set); - qreal barHeight = barSet->valueAt(category) * scale; + qreal barHeight = barSet->at(category) * scale; Bar* bar = m_bars.at(itemIndex); bar->setPen(barSet->pen()); bar->setBrush(barSet->brush()); @@ -62,8 +62,8 @@ QVector StackedBarChartItem::calculateLayout() BarLabel* label = m_labels.at(itemIndex); - if (!qFuzzyIsNull(barSet->valueAt(category))) { - label->setText(QString::number(barSet->valueAt(category))); + if (!qFuzzyIsNull(barSet->at(category))) { + label->setText(QString::number(barSet->at(category))); } else { label->setText(QString("")); }