From 84a6aaeda27a85a637a140b64006239b65207b29 2012-02-20 09:28:36 From: sauimone Date: 2012-02-20 09:28:36 Subject: [PATCH] Added pen & brush to QBarSet --- diff --git a/src/barchart/barchartmodel.cpp b/src/barchart/barchartmodel.cpp index 2e430d6..97a661e 100644 --- a/src/barchart/barchartmodel.cpp +++ b/src/barchart/barchartmodel.cpp @@ -33,10 +33,30 @@ void BarChartModel::removeBarSet(QBarSet *set) { if (mDataModel.contains(set)) { mDataModel.removeOne(set); - delete set; } } +QBarSet* BarChartModel::nextSet(bool getFirst) +{ + if (getFirst) { + mCurrentSet = 0; + } + + if ((mDataModel.count() <= 0) || (mDataModel.count() <= mCurrentSet)) { + return 0; + } + + QBarSet* set = mDataModel.at(mCurrentSet); + mCurrentSet++; + return set; +} + + +QBarSet& BarChartModel::setAt(int index) +{ + return *mDataModel.at(index); +} + int BarChartModel::countSets() { return mDataModel.count(); diff --git a/src/barchart/barchartmodel_p.h b/src/barchart/barchartmodel_p.h index 0f03f73..f19ad08 100644 --- a/src/barchart/barchartmodel_p.h +++ b/src/barchart/barchartmodel_p.h @@ -22,6 +22,9 @@ public: QBarCategory& category(); void addBarSet(QBarSet *set); void removeBarSet(QBarSet *set); + QBarSet* nextSet(bool getFirst); + + QBarSet& setAt(int index); // Internal int countSets(); // Number of sets in model int countCategories(); // Number of categories @@ -46,6 +49,8 @@ private: QList mDataModel; QBarCategory* mCategory; // Owned + int mCurrentSet; + }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barchartseries.cpp b/src/barchart/barchartseries.cpp index 7b8f359..8bb6f16 100644 --- a/src/barchart/barchartseries.cpp +++ b/src/barchart/barchartseries.cpp @@ -23,6 +23,16 @@ void BarChartSeries::removeBarSet(QBarSet *set) mModel->removeBarSet(set); } +int BarChartSeries::countSets() +{ + return mModel->countSets(); +} + +QBarSet* BarChartSeries::nextSet(bool getFirst) +{ + return mModel->nextSet(getFirst); +} + int BarChartSeries::countCategories() { return mModel->countCategories(); diff --git a/src/barchart/barchartseries.h b/src/barchart/barchartseries.h index f0f1aa6..c8a3c15 100644 --- a/src/barchart/barchartseries.h +++ b/src/barchart/barchartseries.h @@ -21,8 +21,10 @@ public: virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } - void addBarSet(QBarSet *set); // Takes ownership - void removeBarSet(QBarSet *set); // Also deletes the set, if set is owned. + void addBarSet(QBarSet *set); // Takes ownership of set + void removeBarSet(QBarSet *set); // Removes set, releases ownership. + int countSets(); + QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true //TODO: //QList legend(); // Returns legend of series (ie. names of all sets in series) diff --git a/src/barchart/bargroup.cpp b/src/barchart/bargroup.cpp index 4d74e59..5db14da 100644 --- a/src/barchart/bargroup.cpp +++ b/src/barchart/bargroup.cpp @@ -1,6 +1,7 @@ #include "bargroup.h" #include "bar_p.h" #include "barlabel_p.h" +#include "qbarset.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -51,10 +52,7 @@ void BarGroup::layoutChanged() // TODO: width settable per bar? bar->resize(mBarDefaultWidth, barHeight); - bar->setBrush(mBrushes.at(set)); -// bar->setPen(mModel.barSet(set).pen()); -// bar->setColor(mColors.at(set)); -// bar->setPen(); + bar->setBrush(mModel.setAt(set).brush()); bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight); itemIndex++; xPos += mBarDefaultWidth; diff --git a/src/barchart/bargroupbase.cpp b/src/barchart/bargroupbase.cpp index 65ca41b..233a84f 100644 --- a/src/barchart/bargroupbase.cpp +++ b/src/barchart/bargroupbase.cpp @@ -47,21 +47,6 @@ void BarGroupBase::setBarWidth( int w ) mBarDefaultWidth = w; } /* -int BarGroupBase::addColor( QColor color ) -{ -// qDebug() << "BarGroupBase::addColor"; - int colorIndex = mColors.count(); - mColors.append(color); - return colorIndex; -} -*/ -/* -void BarGroupBase::resetColors() -{ -// qDebug() << "BarGroupBase::resetColors"; - mColors.clear(); -} -*/ void BarGroupBase::resetBrushes() { mBrushes.clear(); @@ -71,8 +56,7 @@ void BarGroupBase::addBrush(QBrush brush) { mBrushes.append(brush); } - - +*/ void BarGroupBase::dataChanged() { // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? diff --git a/src/barchart/bargroupbase.h b/src/barchart/bargroupbase.h index bb49f7a..67dfe40 100644 --- a/src/barchart/bargroupbase.h +++ b/src/barchart/bargroupbase.h @@ -25,11 +25,9 @@ public: // TODO: these may change with layout awarness. void setBarWidth( int w ); -// int addColor( QColor color ); -// void resetColors(); - void resetBrushes(); - void addBrush(QBrush brush); +// void resetBrushes(); +// void addBrush(QBrush brush); void setPen(QPen pen); QPen pen(); @@ -62,7 +60,7 @@ protected: BarChartModel& mModel; QPen mPen; - QList mBrushes; +// QList mBrushes; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/percentbarchartseries.cpp b/src/barchart/percentbarchartseries.cpp index 8edf70d..66ba990 100644 --- a/src/barchart/percentbarchartseries.cpp +++ b/src/barchart/percentbarchartseries.cpp @@ -25,6 +25,16 @@ void PercentBarChartSeries::removeBarSet(QBarSet *set) mModel->removeBarSet(set); } +int PercentBarChartSeries::countSets() +{ + return mModel->countSets(); +} + +QBarSet* PercentBarChartSeries::nextSet(bool getFirst) +{ + return mModel->nextSet(getFirst); +} + int PercentBarChartSeries::countCategories() { return mModel->countCategories(); diff --git a/src/barchart/percentbarchartseries.h b/src/barchart/percentbarchartseries.h index 3536946..b583db3 100644 --- a/src/barchart/percentbarchartseries.h +++ b/src/barchart/percentbarchartseries.h @@ -23,6 +23,8 @@ public: void addBarSet(QBarSet *set); // Takes ownership void removeBarSet(QBarSet *set); // Also deletes the set, if set is owned. + int countSets(); + QBarSet* nextSet(bool first=false); // Returns first set, if called with true //TODO: //QList legend(); // Returns legend of series (ie. names of all sets in series) diff --git a/src/barchart/percentbargroup.cpp b/src/barchart/percentbargroup.cpp index a1c280e..a875b27 100644 --- a/src/barchart/percentbargroup.cpp +++ b/src/barchart/percentbargroup.cpp @@ -2,6 +2,7 @@ #include "bar_p.h" #include "barlabel_p.h" #include "separator_p.h" +#include "qbarset.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -49,9 +50,7 @@ void PercentBarGroup::layoutChanged() // TODO: width settable per bar? bar->resize(mBarDefaultWidth, barHeight); - bar->setBrush(mBrushes.at(set)); -// bar->setBrush(mBrush); -// bar->setColor(mColors.at(set)); + bar->setBrush(mModel.setAt(set).brush()); bar->setPos(xPos, yPos-barHeight); itemIndex++; yPos -= barHeight; diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index f09d8eb..ac603a3 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -36,6 +36,28 @@ void QBarSet::setValue(int index, qreal value) mValues.replace(index,value); } +void QBarSet::setPen(const QPen& pen) +{ + mPen = pen; +} + +const QPen& QBarSet::pen() const +{ + return mPen; +} + +void QBarSet::setBrush(const QBrush& brush) +{ + mBrush = brush; +} + +const QBrush& QBarSet::brush() const +{ + return mBrush; +} + + + //TODO?: //#include "moc_qbarset.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 39d15af..89f240a 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -21,10 +21,18 @@ public: qreal valueAt(int index); // for modifying individual values void setValue(int index, qreal value); // + void setPen(const QPen& pen); + const QPen& pen() const; + + void setBrush(const QBrush& brush); + const QBrush& brush() const; + private: QString mName; QList mValues; + QPen mPen; + QBrush mBrush; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/stackedbarchartseries.cpp b/src/barchart/stackedbarchartseries.cpp index 05986b4..8093d24 100644 --- a/src/barchart/stackedbarchartseries.cpp +++ b/src/barchart/stackedbarchartseries.cpp @@ -23,6 +23,16 @@ void StackedBarChartSeries::removeBarSet(QBarSet *set) mModel->removeBarSet(set); } +int StackedBarChartSeries::countSets() +{ + return mModel->countSets(); +} + +QBarSet* StackedBarChartSeries::nextSet(bool getFirst) +{ + return mModel->nextSet(getFirst); +} + int StackedBarChartSeries::countCategories() { return mModel->countCategories(); diff --git a/src/barchart/stackedbarchartseries.h b/src/barchart/stackedbarchartseries.h index c2f4d18..a428071 100644 --- a/src/barchart/stackedbarchartseries.h +++ b/src/barchart/stackedbarchartseries.h @@ -23,6 +23,8 @@ public: void addBarSet(QBarSet *set); // Takes ownership void removeBarSet(QBarSet *set); // Also deletes the set, if set is owned. + int countSets(); + QBarSet* nextSet(bool first=false); // Returns first set, if called with true //TODO: //QList legend(); // Returns legend of series (ie. names of all sets in series) diff --git a/src/barchart/stackedbargroup.cpp b/src/barchart/stackedbargroup.cpp index c4cb71d..aaa668f 100644 --- a/src/barchart/stackedbargroup.cpp +++ b/src/barchart/stackedbargroup.cpp @@ -2,6 +2,7 @@ #include "bar_p.h" #include "barlabel_p.h" #include "separator_p.h" +#include "qbarset.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -54,9 +55,7 @@ void StackedBarGroup::layoutChanged() Bar* bar = reinterpret_cast (childItems().at(itemIndex)); bar->resize(mBarDefaultWidth, barHeight); - bar->setBrush(mBrushes.at(set)); -// bar->setBrush(mBrush); -// bar->setColor(mColors.at(set)); + bar->setBrush(mModel.setAt(set).brush()); bar->setPos(xPos, yPos-barHeight); itemIndex++; yPos -= barHeight; diff --git a/src/charttheme.cpp b/src/charttheme.cpp index 8d3b5d3..da6dc3a 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -4,6 +4,7 @@ //series +#include "qbarset.h" #include "barchartseries.h" #include "stackedbarchartseries.h" #include "percentbarchartseries.h" @@ -140,47 +141,23 @@ void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int coun void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count) { - // TODO: better way to descide series color and remove hard coded colors. - item->resetBrushes(); - for (int i=0; iaddBrush(brush); + for (int i=0; icountSets(); i++) { + series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); } - item->addBrush(QBrush(QColor(255,0,0,128))); - item->addBrush(QBrush(QColor(255,255,0,128))); - item->addBrush(QBrush(QColor(0,255,0,128))); - item->addBrush(QBrush(QColor(0,0,255,128))); - item->addBrush(QBrush(QColor(255,128,0,128))); } void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count) { - // TODO: better way to descide series color and remove hard coded colors. - item->resetBrushes(); - for (int i=0; iaddBrush(brush); + for (int i=0; icountSets(); i++) { + series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); } - item->addBrush(QBrush(QColor(255,0,0,128))); - item->addBrush(QBrush(QColor(255,255,0,128))); - item->addBrush(QBrush(QColor(0,255,0,128))); - item->addBrush(QBrush(QColor(0,0,255,128))); - item->addBrush(QBrush(QColor(255,128,0,128))); } void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count) { - // TODO: better way to descide series color and remove hard coded colors. - item->resetBrushes(); - for (int i=0; iaddBrush(brush); + for (int i=0; icountSets(); i++) { + series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); } - item->addBrush(QBrush(QColor(255,0,0,128))); - item->addBrush(QBrush(QColor(255,255,0,128))); - item->addBrush(QBrush(QColor(0,255,0,128))); - item->addBrush(QBrush(QColor(0,0,255,128))); - item->addBrush(QBrush(QColor(255,128,0,128))); } void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)