From 45734e367adb824ad4d2f62357221f250920f200 2012-02-15 11:01:20 From: sauimone Date: 2012-02-15 11:01:20 Subject: [PATCH] brush support for bargroups --- diff --git a/src/barchart/bar.cpp b/src/barchart/bar.cpp index 8e2d066..7be89a3 100644 --- a/src/barchart/bar.cpp +++ b/src/barchart/bar.cpp @@ -27,6 +27,7 @@ void Bar::setColor( QColor col ) { mColor = col; } + void Bar::setPos(qreal x, qreal y) { // qDebug() << "Bar::setpos" << x << y; @@ -34,14 +35,25 @@ void Bar::setPos(qreal x, qreal y) mYpos = y; } +void Bar::setPen(QPen pen) +{ + mPen = pen; +} + +void Bar::setBrush(QBrush brush) +{ + mBrush = brush; +} + void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { if (0 == mHeight) { return; } // TODO: accept brush instead of color - QBrush brush(mColor); - painter->setBrush(brush); + painter->setBrush(mBrush); +// QBrush brush(mColor); +// painter->setBrush(brush); // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. int x0 = mXpos; diff --git a/src/barchart/bar_p.h b/src/barchart/bar_p.h index 1a40209..013c01d 100644 --- a/src/barchart/bar_p.h +++ b/src/barchart/bar_p.h @@ -4,6 +4,8 @@ #include "chartitem_p.h" #include "qchartglobal.h" #include +#include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -17,9 +19,11 @@ public: // from ChartItem void setSize(const QSizeF &size); // Layout Stuff - void resize( qreal w, qreal h ); // Size of bar. - void setColor( QColor col ); // Color of bar + void resize(qreal w, qreal h); // Size of bar. void setPos(qreal x, qreal y); + void setPen(QPen pen); + void setBrush(QBrush brush); + void setColor( QColor col); // deprecated public: // From QGraphicsItem @@ -35,6 +39,9 @@ private: qreal mYpos; QColor mColor; + QBrush mBrush; + QPen mPen; + }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/bargroup.cpp b/src/barchart/bargroup.cpp index 1222f23..4d74e59 100644 --- a/src/barchart/bargroup.cpp +++ b/src/barchart/bargroup.cpp @@ -29,36 +29,39 @@ void BarGroup::layoutChanged() // TODO: better way to auto-layout? // Use reals for accurancy (we might get some compiler warnings... :) int itemCount = mModel.countCategories(); - int seriesCount = mModel.countSets(); + int setCount = mModel.countSets(); qreal tW = mWidth; qreal tH = mHeight; qreal tM = mModel.max(); qreal scale = (tH/tM); qreal tC = itemCount+1; - qreal xStepPerSeries = (tW/tC); + qreal xStepPerSet = (tW/tC); // Scaling. int itemIndex(0); - int labelIndex = itemCount * seriesCount; + int labelIndex = itemCount * setCount; for (int item=0; item < itemCount; item++) { - qreal xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2)); + qreal xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); qreal yPos = mHeight; - for (int series = 0; series < seriesCount; series++) { - qreal barHeight = mModel.valueAt(series, item) * scale; + for (int set = 0; set < setCount; set++) { + qreal barHeight = mModel.valueAt(set, item) * scale; Bar* bar = reinterpret_cast (childItems().at(itemIndex)); // TODO: width settable per bar? bar->resize(mBarDefaultWidth, barHeight); - bar->setColor(mColors.at(series)); - bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); + bar->setBrush(mBrushes.at(set)); +// bar->setPen(mModel.barSet(set).pen()); +// bar->setColor(mColors.at(set)); +// bar->setPen(); + bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight); itemIndex++; xPos += mBarDefaultWidth; } // TODO: Layout for labels, remove magic number - xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2)); + xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); BarLabel* label = reinterpret_cast (childItems().at(labelIndex)); label->setPos(xPos, mHeight + 20); labelIndex++; diff --git a/src/barchart/bargroupbase.cpp b/src/barchart/bargroupbase.cpp index 2690fd5..65ca41b 100644 --- a/src/barchart/bargroupbase.cpp +++ b/src/barchart/bargroupbase.cpp @@ -46,7 +46,7 @@ void BarGroupBase::setBarWidth( int w ) { mBarDefaultWidth = w; } - +/* int BarGroupBase::addColor( QColor color ) { // qDebug() << "BarGroupBase::addColor"; @@ -54,12 +54,24 @@ int BarGroupBase::addColor( QColor color ) mColors.append(color); return colorIndex; } - +*/ +/* void BarGroupBase::resetColors() { // qDebug() << "BarGroupBase::resetColors"; mColors.clear(); } +*/ +void BarGroupBase::resetBrushes() +{ + mBrushes.clear(); +} + +void BarGroupBase::addBrush(QBrush brush) +{ + mBrushes.append(brush); +} + void BarGroupBase::dataChanged() { diff --git a/src/barchart/bargroupbase.h b/src/barchart/bargroupbase.h index 0a81ffa..bb49f7a 100644 --- a/src/barchart/bargroupbase.h +++ b/src/barchart/bargroupbase.h @@ -3,6 +3,8 @@ #include "chartitem_p.h" #include "barchartmodel_p.h" +#include +#include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -23,8 +25,18 @@ public: // TODO: these may change with layout awarness. void setBarWidth( int w ); - int addColor( QColor color ); - void resetColors(); +// int addColor( QColor color ); +// void resetColors(); + + void resetBrushes(); + void addBrush(QBrush brush); + + void setPen(QPen pen); + QPen pen(); + + void setBrush(QBrush brush); + QBrush brush(); + // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes @@ -49,6 +61,8 @@ protected: bool mSeparatorsVisible; BarChartModel& mModel; + QPen mPen; + QList mBrushes; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/percentbargroup.cpp b/src/barchart/percentbargroup.cpp index 1391089..a1c280e 100644 --- a/src/barchart/percentbargroup.cpp +++ b/src/barchart/percentbargroup.cpp @@ -38,18 +38,20 @@ void PercentBarGroup::layoutChanged() qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); int labelIndex = mModel.countCategories() * mModel.countSets(); - for (int column = 0; column < mModel.countCategories(); column++) { - qreal colSum = mModel.categorySum(column); + for (int category = 0; category < mModel.countCategories(); category++) { + qreal colSum = mModel.categorySum(category); qreal h = mHeight; qreal scale = (h / colSum); qreal yPos = h; - for (int row=0; row < mModel.countSets(); row++) { - qreal barHeight = mModel.valueAt(row, column) * scale; + for (int set=0; set < mModel.countSets(); set++) { + qreal barHeight = mModel.valueAt(set, category) * scale; Bar* bar = reinterpret_cast (childItems().at(itemIndex)); // TODO: width settable per bar? bar->resize(mBarDefaultWidth, barHeight); - bar->setColor(mColors.at(row)); + bar->setBrush(mBrushes.at(set)); +// bar->setBrush(mBrush); +// bar->setColor(mColors.at(set)); bar->setPos(xPos, yPos-barHeight); itemIndex++; yPos -= barHeight; diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index 727d838..f09d8eb 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -31,6 +31,11 @@ qreal QBarSet::valueAt(int index) return mValues.at(index); } +void QBarSet::setValue(int index, qreal value) +{ + mValues.replace(index,value); +} + //TODO?: //#include "moc_qbarset.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 9541047..39d15af 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -2,6 +2,8 @@ #define QBARSET_H #include "qchartglobal.h" +#include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -13,18 +15,16 @@ public: void setName(QString name); QString name(); - QBarSet& operator << (const qreal &value); + QBarSet& operator << (const qreal &value); // appends new value to set - //TODO: What is the way to set a single value to n:th item? Is there need for such functionality? - - int count(); - qreal valueAt(int index); + int count(); // count of values in set + qreal valueAt(int index); // for modifying individual values + void setValue(int index, qreal value); // private: QString mName; QList mValues; - }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/stackedbargroup.cpp b/src/barchart/stackedbargroup.cpp index c6b0d9a..c4cb71d 100644 --- a/src/barchart/stackedbargroup.cpp +++ b/src/barchart/stackedbargroup.cpp @@ -35,7 +35,7 @@ void StackedBarGroup::layoutChanged() // TODO: better way to auto-layout // Use reals for accurancy (we might get some compiler warnings... :) - // TODO: use temp variable for column count... + // TODO: use temp variable for category count... qreal maxSum = mModel.maxCategorySum(); qreal h = mHeight; qreal scale = (h / maxSum); @@ -47,14 +47,16 @@ void StackedBarGroup::layoutChanged() qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); int labelIndex = mModel.countSets() * mModel.countCategories(); - for (int column = 0; column < mModel.countCategories(); column++) { + for (int category = 0; category < mModel.countCategories(); category++) { qreal yPos = h; - for (int row=0; row < mModel.countSets(); row++) { - qreal barHeight = mModel.valueAt(row, column) * scale; + for (int set=0; set < mModel.countSets(); set++) { + qreal barHeight = mModel.valueAt(set, category) * scale; Bar* bar = reinterpret_cast (childItems().at(itemIndex)); bar->resize(mBarDefaultWidth, barHeight); - bar->setColor(mColors.at(row)); + bar->setBrush(mBrushes.at(set)); +// bar->setBrush(mBrush); +// bar->setColor(mColors.at(set)); bar->setPos(xPos, yPos-barHeight); itemIndex++; yPos -= barHeight; diff --git a/src/charttheme.cpp b/src/charttheme.cpp index 5472231..39fdd44 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -131,41 +131,47 @@ void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int coun void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count) { - item->resetColors(); + // TODO: better way to descide series color and remove hard coded colors. + item->resetBrushes(); for (int i=0; iaddColor(m_seriesColor.at(i)); + QBrush brush(m_seriesColor.at(i)); + item->addBrush(brush); } - item->addColor(QColor(255,0,0,128)); - item->addColor(QColor(255,255,0,128)); - item->addColor(QColor(0,255,0,128)); - item->addColor(QColor(0,0,255,128)); - item->addColor(QColor(255,128,0,128)); + 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) { - item->resetColors(); - for (int i=0; i< m_seriesColor.count(); i++) { - item->addColor(m_seriesColor.at(i)); + // TODO: better way to descide series color and remove hard coded colors. + item->resetBrushes(); + for (int i=0; iaddBrush(brush); } - item->addColor(QColor(255,0,0,128)); - item->addColor(QColor(255,255,0,128)); - item->addColor(QColor(0,255,0,128)); - item->addColor(QColor(0,0,255,128)); - item->addColor(QColor(255,128,0,128)); + 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) { - item->resetColors(); - for (int i=0; i< m_seriesColor.count(); i++) { - item->addColor(m_seriesColor.at(i)); + // TODO: better way to descide series color and remove hard coded colors. + item->resetBrushes(); + for (int i=0; iaddBrush(brush); } - item->addColor(QColor(255,0,0,128)); - item->addColor(QColor(255,255,0,128)); - item->addColor(QColor(0,255,0,128)); - item->addColor(QColor(0,0,255,128)); - item->addColor(QColor(255,128,0,128)); + 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)