diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index cf42bdf..a168881 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -124,7 +124,13 @@ qreal QBarSeries::barMargin() const bool QBarSeries::append(QBarSet *set) { Q_D(QBarSeries); - return d->append(set); + bool success = d->append(set); + if (success) { + QList sets; + sets.append(set); + emit barsetsAdded(sets); + } + return success; } /*! @@ -134,7 +140,13 @@ bool QBarSeries::append(QBarSet *set) bool QBarSeries::remove(QBarSet *set) { Q_D(QBarSeries); - return d->remove(set); + bool success = d->remove(set); + if (success) { + QList sets; + sets.append(set); + emit barsetsRemoved(sets); + } + return success; } /*! @@ -146,7 +158,11 @@ bool QBarSeries::remove(QBarSet *set) bool QBarSeries::append(QList sets) { Q_D(QBarSeries); - return d->append(sets); + bool success = d->append(sets); + if (success) { + emit barsetsAdded(sets); + } + return success; } /*! @@ -155,7 +171,11 @@ bool QBarSeries::append(QList sets) bool QBarSeries::remove(QList sets) { Q_D(QBarSeries); - return d->remove(sets); + bool success = d->remove(sets); + if (success) { + emit barsetsRemoved(sets); + } + return success; } void QBarSeries::clear() @@ -189,8 +209,8 @@ void QBarSeries::setLabelsVisible(bool visible) { Q_D(QBarSeries); if (d->m_labelsVisible != visible) { - d->m_labelsVisible = visible; - emit d->labelsVisibleChanged(visible); + d->setLabelsVisible(visible); + emit labelsVisibleChanged(); } } @@ -287,15 +307,14 @@ QBarSet* QBarSeriesPrivate::barsetAt(int index) void QBarSeriesPrivate::setVisible(bool visible) { - if (m_visible != visible) { - m_visible = visible; - emit updatedBars(); - } + m_visible = visible; + emit updatedBars(); } -bool QBarSeriesPrivate::isVisible() const +void QBarSeriesPrivate::setLabelsVisible(bool visible) { - return m_visible; + m_labelsVisible = visible; + emit labelsVisibleChanged(visible); } QString QBarSeriesPrivate::categoryName(int category) @@ -518,23 +537,28 @@ bool QBarSeriesPrivate::append(QList sets) bool QBarSeriesPrivate::remove(QList sets) { Q_Q(QBarSeries); - bool setsRemoved = false; foreach (QBarSet* set, sets) { - if (m_barSets.contains(set)) { - m_barSets.removeOne(set); - QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); - QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); - setsRemoved = true; + if ((set == 0) || (!m_barSets.contains(set))) { + // Fail if any of the sets is null or is not in series + return false; + } + if (sets.count(set) != 1) { + // Also fail if same set is more than once in given list. + return false; } } - if (setsRemoved) { - if (m_dataset) { - m_dataset->updateSeries(q); // this notifies legend - } - emit restructuredBars(); // this notifies barchartitem + foreach (QBarSet* set, sets) { + m_barSets.removeOne(set); + QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); + QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); } - return setsRemoved; + + if (m_dataset) { + m_dataset->updateSeries(q); // this notifies legend + } + emit restructuredBars(); // this notifies barchartitem + return true; } #include "moc_qbarseries.cpp" diff --git a/src/barchart/qbarseries.h b/src/barchart/qbarseries.h index afad19e..091d66a 100644 --- a/src/barchart/qbarseries.h +++ b/src/barchart/qbarseries.h @@ -63,6 +63,11 @@ protected: Q_SIGNALS: void clicked(QBarSet *barset, int index); void hovered(QBarSet* barset, bool status); + void visibleChanged(); + void labelsVisibleChanged(); + + void barsetsAdded(QList sets); + void barsetsRemoved(QList sets); protected: Q_DECLARE_PRIVATE(QBarSeries) diff --git a/src/barchart/qbarseries_p.h b/src/barchart/qbarseries_p.h index d562018..a9926a4 100644 --- a/src/barchart/qbarseries_p.h +++ b/src/barchart/qbarseries_p.h @@ -55,7 +55,7 @@ public: qreal barMargin() const; void setVisible(bool visible); - bool isVisible() const; + void setLabelsVisible(bool visible); void scaleDomain(Domain& domain); Chart* createGraphics(ChartPresenter* presenter); diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index 119d8c4..ee2fa53 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -60,6 +60,7 @@ QBarSet::~QBarSet() void QBarSet::setName(const QString name) { d_ptr->m_name = name; + emit nameChanged(); } /*! @@ -78,8 +79,9 @@ QString QBarSet::name() const */ void QBarSet::append(const QPointF value) { - d_ptr->m_values.append(value); - emit d_ptr->restructuredBars(); + int index = d_ptr->m_values.count(); + d_ptr->append(value); + emit valuesAdded(index, 1); } /*! @@ -88,10 +90,9 @@ void QBarSet::append(const QPointF value) */ void QBarSet::append(const QList values) { - for (int i=0; im_values.append(values.at(i)); - } - emit d_ptr->restructuredBars(); + int index = d_ptr->m_values.count(); + d_ptr->append(values); + emit valuesAdded(index, values.count()); } /*! @@ -100,6 +101,7 @@ void QBarSet::append(const QList values) */ void QBarSet::append(const qreal value) { + // Convert to QPointF and use other append(QPointF) method. append(QPointF(d_ptr->m_values.count(), value)); } @@ -111,11 +113,8 @@ void QBarSet::append(const qreal value) void QBarSet::append(const QList values) { int index = d_ptr->m_values.count(); - for (int i=0; im_values.append(QPointF(index,values.at(i))); - index++; - } - emit d_ptr->restructuredBars(); + d_ptr->append(values); + emit valuesAdded(index, values.count()); } /*! @@ -145,18 +144,32 @@ QBarSet& QBarSet::operator << (const QPointF &value) */ void QBarSet::insert(const int index, const qreal value) { - d_ptr->m_values.insert(index, QPointF(index, value)); -// emit d_ptr->updatedBars(); + 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); } /*! Removes the value specified by \a index \sa insert() */ -void QBarSet::remove(const int index) +bool QBarSet::remove(const int index, const int count) { - d_ptr->m_values.removeAt(index); -// emit d_ptr->updatedBars(); + bool success = d_ptr->remove(index,count); + if (success) { + emit valuesRemoved(index,count); + } + return success; } /*! @@ -164,8 +177,17 @@ void QBarSet::remove(const int index) */ void QBarSet::replace(const int index, const qreal value) { - d_ptr->m_values.replace(index,QPointF(index,value)); - emit d_ptr->updatedBars(); + 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); } /*! @@ -222,6 +244,7 @@ void QBarSet::setPen(const QPen &pen) if(d_ptr->m_pen!=pen){ d_ptr->m_pen = pen; emit d_ptr->updatedBars(); + emit penChanged(); } } @@ -241,6 +264,7 @@ void QBarSet::setBrush(const QBrush &brush) if(d_ptr->m_brush!=brush){ d_ptr->m_brush = brush; emit d_ptr->updatedBars(); + emit brushChanged(); } } @@ -260,6 +284,7 @@ void QBarSet::setLabelBrush(const QBrush &brush) if(d_ptr->m_labelBrush!=brush){ d_ptr->m_labelBrush = brush; emit d_ptr->updatedBars(); + emit labelBrushChanged(); } } @@ -279,6 +304,7 @@ void QBarSet::setLabelFont(const QFont &font) if(d_ptr->m_labelFont!=font) { d_ptr->m_labelFont = font; emit d_ptr->updatedBars(); + emit labelFontChanged(); } } @@ -303,6 +329,69 @@ QBarSetPrivate::~QBarSetPrivate() { } +void QBarSetPrivate::append(QPointF value) +{ + m_values.append(value); + emit restructuredBars(); +} + +void QBarSetPrivate::append(QList values) +{ + for (int i=0; i values) +{ + int index = m_values.count(); + for (int i=0; i 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(); +} + #include "moc_qbarset.cpp" #include "moc_qbarset_p.cpp" diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 8fad337..6591fae 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -54,8 +54,10 @@ public: QBarSet& operator << (const QPointF &value); void insert(const int index, const qreal value); - void remove(const int index); + void insert(const int index, const QPointF value); + bool remove(const int index, const int count = 1); void replace(const int index, const qreal value); + void replace(const int index, const QPointF value); QPointF at(const int index) const; QPointF operator [] (const int index) const; int count() const; @@ -73,6 +75,18 @@ public: void setLabelFont(const QFont &font); QFont labelFont() const; +Q_SIGNALS: + void nameChanged(); + void penChanged(); + void brushChanged(); + void labelChanged(); + void labelBrushChanged(); + void labelFontChanged(); + + void valuesAdded(int index, int count); + void valuesRemoved(int index, int count); + void valueChanged(int index); + private: QScopedPointer d_ptr; Q_DISABLE_COPY(QBarSet) diff --git a/src/barchart/qbarset_p.h b/src/barchart/qbarset_p.h index 4fa3aa0..49a128e 100644 --- a/src/barchart/qbarset_p.h +++ b/src/barchart/qbarset_p.h @@ -46,6 +46,17 @@ public: QBarSetPrivate(const QString name, QBarSet *parent); ~QBarSetPrivate(); + void append(QPointF value); + void append(QList values); + void append(QList values); + + void insert(const int index, const qreal value); + void insert(const int index, const QPointF value); + bool remove(const int index, const int count); + + void replace(const int index, const qreal value); + void replace(const int index, const QPointF value); + Q_SIGNALS: void restructuredBars(); void updatedBars(); diff --git a/tests/auto/qbarset/tst_qbarset.cpp b/tests/auto/qbarset/tst_qbarset.cpp index 46b2ae5..c146185 100644 --- a/tests/auto/qbarset/tst_qbarset.cpp +++ b/tests/auto/qbarset/tst_qbarset.cpp @@ -108,8 +108,10 @@ void tst_QBarSet::name() QFETCH(QString, name); QFETCH(QString, result); + QSignalSpy nameSpy(m_barset,SIGNAL(nameChanged())); m_barset->setName(name); QCOMPARE(m_barset->name(), result); + QVERIFY(nameSpy.count() == 1); } void tst_QBarSet::append_data() @@ -128,6 +130,8 @@ void tst_QBarSet::append() QCOMPARE(m_barset->count(), 0); QVERIFY(qFuzzyIsNull(m_barset->sum())); + QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int))); + qreal sum(0.0); qreal value(0.0); @@ -140,6 +144,8 @@ void tst_QBarSet::append() QCOMPARE(m_barset->count(), count); QVERIFY(qFuzzyCompare(m_barset->sum(), sum)); + + QVERIFY(valueSpy.count() == count); } void tst_QBarSet::appendOperator_data() @@ -154,6 +160,8 @@ void tst_QBarSet::appendOperator() QCOMPARE(m_barset->count(), 0); QVERIFY(qFuzzyIsNull(m_barset->sum())); + QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int))); + qreal sum(0.0); qreal value(0.0); @@ -166,6 +174,8 @@ void tst_QBarSet::appendOperator() QCOMPARE(m_barset->count(), count); QVERIFY(qFuzzyCompare(m_barset->sum(), sum)); + QVERIFY(valueSpy.count() == count); + } void tst_QBarSet::insert_data() @@ -176,6 +186,7 @@ void tst_QBarSet::insert() { QCOMPARE(m_barset->count(), 0); QVERIFY(qFuzzyIsNull(m_barset->sum())); + QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int))); m_barset->insert(0, 1.0); // 1.0 QCOMPARE(m_barset->at(0).y(), 1.0); @@ -194,6 +205,7 @@ void tst_QBarSet::insert() QCOMPARE(m_barset->at(2).y(), 1.0); QCOMPARE(m_barset->count(), 3); QVERIFY(qFuzzyCompare(m_barset->sum(), 6.0)); + QVERIFY(valueSpy.count() == 3); } void tst_QBarSet::remove_data() @@ -205,6 +217,8 @@ void tst_QBarSet::remove() QCOMPARE(m_barset->count(), 0); QVERIFY(qFuzzyIsNull(m_barset->sum())); + QSignalSpy valueSpy(m_barset,SIGNAL(valuesRemoved(int,int))); + m_barset->append(1.0); m_barset->append(2.0); m_barset->append(3.0); @@ -225,6 +239,8 @@ void tst_QBarSet::remove() QCOMPARE(m_barset->at(1).y(), 4.0); QCOMPARE(m_barset->count(), 2); QCOMPARE(m_barset->sum(), 6.0); + + QVERIFY(valueSpy.count() == 2); } void tst_QBarSet::replace_data() @@ -236,6 +252,7 @@ void tst_QBarSet::replace() { QCOMPARE(m_barset->count(), 0); QVERIFY(qFuzzyIsNull(m_barset->sum())); + QSignalSpy valueSpy(m_barset,SIGNAL(valueChanged(int))); m_barset->append(1.0); m_barset->append(2.0); @@ -257,6 +274,8 @@ void tst_QBarSet::replace() QCOMPARE(m_barset->at(1).y(), 2.0); QCOMPARE(m_barset->at(2).y(), 3.0); QCOMPARE(m_barset->at(3).y(), 6.0); + + QVERIFY(valueSpy.count() == 2); } void tst_QBarSet::at_data()