From 8959911f7807e392a82e475cc6a7bee618c11d7e 2012-03-06 14:44:25 From: sauimone Date: 2012-03-06 14:44:25 Subject: [PATCH] Fixed layout for barcharts --- diff --git a/src/barchart/barpresenter.cpp b/src/barchart/barpresenter.cpp index 0309969..2524f62 100644 --- a/src/barchart/barpresenter.cpp +++ b/src/barchart/barpresenter.cpp @@ -10,7 +10,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE BarPresenter::BarPresenter(QBarSeries *series, QGraphicsItem *parent) : BarPresenterBase(series, parent) { - mBarDefaultWidth = 5; + mBarWidth = 5; } void BarPresenter::layoutChanged() @@ -27,8 +27,7 @@ void BarPresenter::layoutChanged() return; } - // TODO: better way to auto-layout? - // Use reals for accurancy (we might get some compiler warnings... :) + // Use temporary qreals for accurancy (we might get some compiler warnings... :) int categoryCount = mSeries->categoryCount(); int setCount = mSeries->barsetCount(); @@ -36,39 +35,39 @@ void BarPresenter::layoutChanged() qreal tH = mHeight; qreal tM = mSeries->max(); qreal scale = (tH/tM); - qreal tC = categoryCount+1; - qreal xStepPerSet = (tW/tC); + qreal tC = categoryCount + 1; + mBarWidth = tW / ((categoryCount * setCount) + tC); + qreal xStepPerCategory = (tW/tC) + mBarWidth; - // Scaling. int itemIndex(0); int labelIndex(0); for (int category=0; category < categoryCount; category++) { - qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2)); + qreal xPos = xStepPerCategory * category; qreal yPos = mHeight; for (int set = 0; set < setCount; set++) { qreal barHeight = mSeries->valueAt(set,category) * scale; Bar* bar = mBars.at(itemIndex); // TODO: width settable per bar? - bar->resize(mBarDefaultWidth, barHeight); + bar->resize(mBarWidth, barHeight); bar->setBrush(mSeries->barsetAt(set)->brush()); bar->setPos(xPos, yPos-barHeight); itemIndex++; - xPos += mBarDefaultWidth; + xPos += mBarWidth; } // TODO: Layout for labels, remove magic number - xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2)); + xPos = xStepPerCategory * category + mBarWidth/2; BarLabel* label = mLabels.at(labelIndex); - label->setPos(xPos, mHeight + 20); + label->setPos(xPos, mHeight - 20); labelIndex++; } // Position floating values itemIndex = 0; for (int category=0; category < mSeries->categoryCount(); category++) { - qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2)); + qreal xPos = xStepPerCategory * category + mBarWidth/2; qreal yPos = mHeight; for (int set=0; set < mSeries->barsetCount(); set++) { qreal barHeight = mSeries->valueAt(set,category) * scale; @@ -86,10 +85,9 @@ void BarPresenter::layoutChanged() } itemIndex++; - xPos += mBarDefaultWidth; + xPos += mBarWidth; } } - mLayoutDirty = true; } #include "moc_barpresenter_p.cpp" diff --git a/src/barchart/barpresenterbase.cpp b/src/barchart/barpresenterbase.cpp index 106a458..4940db6 100644 --- a/src/barchart/barpresenterbase.cpp +++ b/src/barchart/barpresenterbase.cpp @@ -12,14 +12,12 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent) : ChartItem(parent) - ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready + ,mBarWidth(20) // TODO: remove hard coding, when we have layout code ready ,mLayoutSet(false) - ,mLayoutDirty(true) ,mSeparatorsEnabled(false) ,mSeries(series) { connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); -// connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool))); dataChanged(); } @@ -34,12 +32,9 @@ void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem * qDebug() << "BarPresenterBase::paint called without layout set. Aborting."; return; } -// if (mLayoutDirty) { - // Layout or data has changed. Need to redraw. - foreach(QGraphicsItem* i, childItems()) { - i->paint(painter,option,widget); - } -// } + foreach(QGraphicsItem* i, childItems()) { + i->paint(painter,option,widget); + } } QRectF BarPresenterBase::boundingRect() const @@ -49,13 +44,12 @@ QRectF BarPresenterBase::boundingRect() const void BarPresenterBase::setBarWidth( int w ) { - mBarDefaultWidth = w; + mBarWidth = w; } void BarPresenterBase::dataChanged() { // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? -// qDebug() << "datachanged"; // Delete old bars foreach (QGraphicsItem* item, childItems()) { delete item; @@ -110,9 +104,6 @@ void BarPresenterBase::dataChanged() connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); } } - - // TODO: if (autolayout) { layoutChanged() } or something - mLayoutDirty = true; } //handlers diff --git a/src/barchart/barpresenterbase_p.h b/src/barchart/barpresenterbase_p.h index 90abdbc..4a22852 100644 --- a/src/barchart/barpresenterbase_p.h +++ b/src/barchart/barpresenterbase_p.h @@ -49,17 +49,13 @@ protected: // TODO: consider these. int mHeight; // Layout spesific int mWidth; - int mBarDefaultWidth; + qreal mBarWidth; bool mLayoutSet; // True, if component has been laid out. - bool mLayoutDirty; - bool mSeparatorsEnabled; - // Owned - QBarSeries* mSeries; - // Not owned. + QBarSeries* mSeries; QList mBars; QList mLabels; QList mSeparators; diff --git a/src/barchart/barvalue.cpp b/src/barchart/barvalue.cpp index 7715da2..dbe83fc 100644 --- a/src/barchart/barvalue.cpp +++ b/src/barchart/barvalue.cpp @@ -21,12 +21,12 @@ QString BarValue::valueString() return mValueString; } -void BarValue::setPen(const QPen& pen) +void BarValue::setPen(const QPen pen) { mPen = pen; } -const QPen& BarValue::pen() +QPen BarValue::pen() const { return mPen; } diff --git a/src/barchart/barvalue_p.h b/src/barchart/barvalue_p.h index 471f999..f4c1844 100644 --- a/src/barchart/barvalue_p.h +++ b/src/barchart/barvalue_p.h @@ -20,8 +20,8 @@ public: void setValueString(QString str); QString valueString(); - void setPen(const QPen& pen); - const QPen& pen(); + void setPen(const QPen pen); + QPen pen() const; void resize(qreal w, qreal h); void setPos(qreal x, qreal y); diff --git a/src/barchart/percentbarpresenter.cpp b/src/barchart/percentbarpresenter.cpp index 6fb873b..0e4bdf3 100644 --- a/src/barchart/percentbarpresenter.cpp +++ b/src/barchart/percentbarpresenter.cpp @@ -29,17 +29,17 @@ void PercentBarPresenter::layoutChanged() return; } - // TODO: better way to auto-layout - // Use reals for accurancy (we might get some compiler warnings... :) - int count = mSeries->categoryCount(); - int itemIndex(0); - int labelIndex(0); + // Use temporary qreals for accurancy (we might get some compiler warnings... :) qreal tW = mWidth; - qreal tC = count+1; + qreal tC = mSeries->categoryCount() + 1; + qreal cC = mSeries->categoryCount() * 2 + 1; + mBarWidth = tW / cC; qreal xStep = (tW/tC); - qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); + qreal xPos = ((tW/tC) - mBarWidth / 2); qreal h = mHeight; + int itemIndex(0); + int labelIndex(0); for (int category = 0; category < mSeries->categoryCount(); category++) { qreal colSum = mSeries->categorySum(category); qreal scale = (h / colSum); @@ -49,7 +49,7 @@ void PercentBarPresenter::layoutChanged() Bar* bar = mBars.at(itemIndex); // TODO: width settable per bar? - bar->resize(mBarDefaultWidth, barHeight); + bar->resize(mBarWidth, barHeight); bar->setBrush(mSeries->barsetAt(set)->brush()); bar->setPos(xPos, yPos-barHeight); itemIndex++; @@ -74,7 +74,7 @@ void PercentBarPresenter::layoutChanged() // Position floating values itemIndex = 0; - xPos = ((tW/tC) - mBarDefaultWidth / 2); + xPos = ((tW/tC) - mBarWidth / 2); for (int category=0; category < mSeries->categoryCount(); category++) { qreal yPos = h; qreal colSum = mSeries->categorySum(category); @@ -103,8 +103,6 @@ void PercentBarPresenter::layoutChanged() } xPos += xStep; } - - mLayoutDirty = true; } #include "moc_percentbarpresenter_p.cpp" diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index e86c8d9..803c2e6 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -122,7 +122,7 @@ qreal QBarSet::total() /*! Sets pen for set. Bars of this set are drawn using \a pen */ -void QBarSet::setPen(QPen pen) +void QBarSet::setPen(const QPen pen) { mPen = pen; } @@ -130,7 +130,7 @@ void QBarSet::setPen(QPen pen) /*! Returns pen of the set. */ -QPen QBarSet::pen() +QPen QBarSet::pen() const { return mPen; } @@ -138,7 +138,7 @@ QPen QBarSet::pen() /*! Sets brush for the set. Bars of this set are drawn using \a brush */ -void QBarSet::setBrush(QBrush brush) +void QBarSet::setBrush(const QBrush brush) { mBrush = brush; } @@ -146,7 +146,7 @@ void QBarSet::setBrush(QBrush brush) /*! Returns brush of the set. */ -QBrush QBarSet::brush() +QBrush QBarSet::brush() const { return mBrush; } diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 7ab5665..4b656c8 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -27,11 +27,11 @@ public: //qreal value(QString category); //void setValue(QString category, qreal value); - void setPen(QPen pen); - QPen pen(); + void setPen(const QPen pen); + QPen pen() const; - void setBrush(QBrush brush); - QBrush brush(); + void setBrush(const QBrush brush); + QBrush brush() const; Q_SIGNALS: void clicked(QString category); // Clicked and hover signals exposed to user diff --git a/src/barchart/stackedbarpresenter.cpp b/src/barchart/stackedbarpresenter.cpp index 6588a15..692db65 100644 --- a/src/barchart/stackedbarpresenter.cpp +++ b/src/barchart/stackedbarpresenter.cpp @@ -39,27 +39,26 @@ void StackedBarPresenter::layoutChanged() return; } - // TODO: better way to auto-layout - // Use reals for accurancy (we might get some compiler warnings... :) - // TODO: use temp variable for category count... + // Use temporary qreals for accurancy (we might get some compiler warnings... :) qreal maxSum = mSeries->maxCategorySum(); qreal h = mHeight; qreal scale = (h / maxSum); - - int itemIndex(0); - int labelIndex(0); qreal tW = mWidth; qreal tC = mSeries->categoryCount() + 1; + qreal cC = mSeries->categoryCount() * 2 + 1; + mBarWidth = tW / cC; qreal xStep = (tW/tC); - qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); + qreal xPos = ((tW/tC) - mBarWidth / 2); + int itemIndex(0); + int labelIndex(0); for (int category = 0; category < mSeries->categoryCount(); category++) { qreal yPos = h; for (int set=0; set < mSeries->barsetCount(); set++) { qreal barHeight = mSeries->valueAt(set, category) * scale; Bar* bar = mBars.at(itemIndex); - bar->resize(mBarDefaultWidth, barHeight); + bar->resize(mBarWidth, barHeight); bar->setBrush(mSeries->barsetAt(set)->brush()); bar->setPos(xPos, yPos-barHeight); itemIndex++; @@ -84,7 +83,7 @@ void StackedBarPresenter::layoutChanged() // Position floating values itemIndex = 0; - xPos = ((tW/tC) - mBarDefaultWidth / 2); + xPos = ((tW/tC) - mBarWidth / 2); for (int category=0; category < mSeries->categoryCount(); category++) { qreal yPos = h; for (int set=0; set < mSeries->barsetCount(); set++) { @@ -107,8 +106,6 @@ void StackedBarPresenter::layoutChanged() } xPos += xStep; } - - mLayoutDirty = true; } #include "moc_stackedbarpresenter_p.cpp"