From 6018938e3db4f5859b33b0bd6e6d5f7836454fc2 2012-03-20 12:00:50 From: sauimone Date: 2012-03-20 12:00:50 Subject: [PATCH] combined barpresenterbase and barpresenter. renamed barchartpresenters to barchartitems --- diff --git a/src/barchart/barchart.pri b/src/barchart/barchart.pri index 7216025..79fd795 100644 --- a/src/barchart/barchart.pri +++ b/src/barchart/barchart.pri @@ -4,23 +4,21 @@ DEPENDPATH += $$PWD SOURCES += \ $$PWD/bar.cpp \ $$PWD/barchartmodel.cpp \ - $$PWD/barpresenter.cpp \ - $$PWD/barpresenterbase.cpp \ - $$PWD/percentbarpresenter.cpp \ + $$PWD/barchartitem.cpp \ + $$PWD/percentbarchartitem.cpp \ $$PWD/qbarseries.cpp \ $$PWD/qbarset.cpp \ $$PWD/qpercentbarseries.cpp \ $$PWD/qstackedbarseries.cpp \ - $$PWD/stackedbarpresenter.cpp \ + $$PWD/stackedbarchartitem.cpp \ $$PWD/barvalue.cpp PRIVATE_HEADERS += \ $$PWD/bar_p.h \ $$PWD/barchartmodel_p.h \ - $$PWD/barpresenter_p.h \ - $$PWD/barpresenterbase_p.h \ - $$PWD/percentbarpresenter_p.h \ - $$PWD/stackedbarpresenter_p.h \ + $$PWD/barchartitem_p.h \ + $$PWD/percentbarchartitem_p.h \ + $$PWD/stackedbarchartitem_p.h \ $$PWD/barvalue_p.h PUBLIC_HEADERS += \ diff --git a/src/barchart/barpresenterbase.cpp b/src/barchart/barchartitem.cpp similarity index 62% rename from src/barchart/barpresenterbase.cpp rename to src/barchart/barchartitem.cpp index 7d58909..6932438 100644 --- a/src/barchart/barpresenterbase.cpp +++ b/src/barchart/barchartitem.cpp @@ -1,4 +1,4 @@ -#include "barpresenterbase_p.h" +#include "barchartitem_p.h" #include "bar_p.h" #include "barvalue_p.h" #include "qbarset.h" @@ -12,7 +12,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent) : +BarChartItem::BarChartItem(QBarSeries *series, QChart *parent) : ChartItem(parent), mHeight(0), mWidth(0), @@ -21,20 +21,21 @@ BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent) : mChart(parent) { connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); + connect(series, SIGNAL(updatedBars()), this, SLOT(layoutChanged())); setZValue(ChartPresenter::BarSeriesZValue); initAxisLabels(); dataChanged(); } -BarPresenterBase::~BarPresenterBase() +BarChartItem::~BarChartItem() { disconnect(this,SLOT(showToolTip(QPoint,QString))); } -void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { if (!mLayoutSet) { - qDebug() << "BarPresenterBase::paint called without layout set. Aborting."; + qDebug() << "BarChartItem::paint called without layout set. Aborting."; return; } foreach(QGraphicsItem* i, childItems()) { @@ -42,12 +43,12 @@ void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem * } } -QRectF BarPresenterBase::boundingRect() const +QRectF BarChartItem::boundingRect() const { return QRectF(0, 0, mWidth, mHeight); } -void BarPresenterBase::dataChanged() +void BarChartItem::dataChanged() { // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? // Delete old bars @@ -85,7 +86,79 @@ void BarPresenterBase::dataChanged() } } -void BarPresenterBase::initAxisLabels() +void BarChartItem::layoutChanged() +{ + // Scale bars to new layout + // Layout for bars: + if (mSeries->barsetCount() <= 0) { + qDebug() << "No sets in model!"; + return; + } + + if (childItems().count() == 0) { + qDebug() << "WARNING: BarChartitem::layoutChanged called before graphics items are created!"; + return; + } + + // Use temporary qreals for accurancy (we might get some compiler warnings... :) + int categoryCount = mSeries->categoryCount(); + int setCount = mSeries->barsetCount(); + + qreal tW = mWidth; + qreal tH = mHeight; + qreal tM = mSeries->max(); + qreal scale = (tH/tM); + qreal tC = categoryCount + 1; + qreal categoryWidth = tW/tC; + mBarWidth = categoryWidth / (setCount+1); + + int itemIndex(0); + for (int category=0; category < categoryCount; category++) { + qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2; + 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(mBarWidth, barHeight); + bar->setPen(mSeries->barsetAt(set)->pen()); + bar->setBrush(mSeries->barsetAt(set)->brush()); + bar->setPos(xPos, yPos-barHeight); + itemIndex++; + xPos += mBarWidth; + } + } + + // Position floating values + itemIndex = 0; + for (int category=0; category < mSeries->categoryCount(); category++) { + qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth; + qreal yPos = mHeight; + for (int set=0; set < mSeries->barsetCount(); set++) { + qreal barHeight = mSeries->valueAt(set,category) * scale; + BarValue* value = mFloatingValues.at(itemIndex); + + QBarSet* barSet = mSeries->barsetAt(set); + value->resize(100,50); // TODO: proper layout for this. + value->setPos(xPos, yPos-barHeight/2); + value->setPen(barSet->floatingValuePen()); + + if (mSeries->valueAt(set,category) != 0) { + value->setValueString(QString::number(mSeries->valueAt(set,category))); + } else { + value->setValueString(QString("")); + } + + itemIndex++; + xPos += mBarWidth; + } + } + update(); +} + + +void BarChartItem::initAxisLabels() { int count = mSeries->categoryCount(); if (0 == count) { @@ -113,13 +186,13 @@ void BarPresenterBase::initAxisLabels() //handlers -void BarPresenterBase::handleModelChanged(int index) +void BarChartItem::handleModelChanged(int index) { Q_UNUSED(index) dataChanged(); } -void BarPresenterBase::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) +void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) { // TODO: Q_UNUSED(minX) @@ -146,7 +219,7 @@ void BarPresenterBase::handleDomainChanged(qreal minX, qreal maxX, qreal minY, q */ } -void BarPresenterBase::handleGeometryChanged(const QRectF& rect) +void BarChartItem::handleGeometryChanged(const QRectF& rect) { mWidth = rect.width(); mHeight = rect.height(); @@ -155,12 +228,12 @@ void BarPresenterBase::handleGeometryChanged(const QRectF& rect) setPos(rect.topLeft()); } -void BarPresenterBase::showToolTip(QPoint pos, QString tip) +void BarChartItem::showToolTip(QPoint pos, QString tip) { // TODO: cool tooltip instead of default QToolTip::showText(pos,tip); } -#include "moc_barpresenterbase_p.cpp" +#include "moc_barchartitem_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barpresenterbase_p.h b/src/barchart/barchartitem_p.h similarity index 82% rename from src/barchart/barpresenterbase_p.h rename to src/barchart/barchartitem_p.h index c21dc1e..6aeed0f 100644 --- a/src/barchart/barpresenterbase_p.h +++ b/src/barchart/barchartitem_p.h @@ -1,5 +1,5 @@ -#ifndef BARPRESENTERBASE_H -#define BARPRESENTERBASE_H +#ifndef BARCHARTITEM_H +#define BARCHARTITEM_H #include "chartitem_p.h" #include "qbarseries.h" @@ -14,14 +14,13 @@ class BarValue; class QChartAxisCategories; class QChart; -// Common implemantation of different presenters. Not to be instantiated. -// TODO: combine this with BarPresenter and derive other presenters from it? -class BarPresenterBase : public QObject, public ChartItem +// Common implemantation of different presenters. +class BarChartItem : public QObject, public ChartItem { Q_OBJECT public: - BarPresenterBase(QBarSeries *series, QChart *parent = 0); - virtual ~BarPresenterBase(); + BarChartItem(QBarSeries *series, QChart *parent = 0); + virtual ~BarChartItem(); public: // From QGraphicsItem @@ -31,7 +30,7 @@ public: // 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 private slots: - virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes + virtual void layoutChanged(); // layout has changed -> need to recalculate bar sizes protected: void initAxisLabels(); @@ -62,4 +61,4 @@ protected: QTCOMMERCIALCHART_END_NAMESPACE -#endif // BARPRESENTERBASE_H +#endif // BARCHARTITEM_H diff --git a/src/barchart/barpresenter.cpp b/src/barchart/barpresenter.cpp deleted file mode 100644 index ef59061..0000000 --- a/src/barchart/barpresenter.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include "barpresenter_p.h" -#include "bar_p.h" -#include "barvalue_p.h" -#include "qbarset.h" -#include - -QTCOMMERCIALCHART_BEGIN_NAMESPACE - -BarPresenter::BarPresenter(QBarSeries *series, QChart *parent) : - BarPresenterBase(series, parent) -{ - connect(series, SIGNAL(updatedBars()), this, SLOT(layoutChanged())); - connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int))); -} - -void BarPresenter::layoutChanged() -{ - // Scale bars to new layout - // Layout for bars: - if (mSeries->barsetCount() <= 0) { - qDebug() << "No sets in model!"; - return; - } - - if (childItems().count() == 0) { - qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!"; - return; - } - - // Use temporary qreals for accurancy (we might get some compiler warnings... :) - int categoryCount = mSeries->categoryCount(); - int setCount = mSeries->barsetCount(); - - qreal tW = mWidth; - qreal tH = mHeight; - qreal tM = mSeries->max(); - qreal scale = (tH/tM); - qreal tC = categoryCount + 1; - qreal categoryWidth = tW/tC; - mBarWidth = categoryWidth / (setCount+1); - - int itemIndex(0); - for (int category=0; category < categoryCount; category++) { - qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2; - 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(mBarWidth, barHeight); - bar->setPen(mSeries->barsetAt(set)->pen()); - bar->setBrush(mSeries->barsetAt(set)->brush()); - bar->setPos(xPos, yPos-barHeight); - itemIndex++; - xPos += mBarWidth; - } - } - - // Position floating values - itemIndex = 0; - for (int category=0; category < mSeries->categoryCount(); category++) { - qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth; - qreal yPos = mHeight; - for (int set=0; set < mSeries->barsetCount(); set++) { - qreal barHeight = mSeries->valueAt(set,category) * scale; - BarValue* value = mFloatingValues.at(itemIndex); - - QBarSet* barSet = mSeries->barsetAt(set); - value->resize(100,50); // TODO: proper layout for this. - value->setPos(xPos, yPos-barHeight/2); - value->setPen(barSet->floatingValuePen()); - - if (mSeries->valueAt(set,category) != 0) { - value->setValueString(QString::number(mSeries->valueAt(set,category))); - } else { - value->setValueString(QString("")); - } - - itemIndex++; - xPos += mBarWidth; - } - } - update(); -} - -#include "moc_barpresenter_p.cpp" - -QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/percentbarpresenter.cpp b/src/barchart/percentbarchartitem.cpp similarity index 92% rename from src/barchart/percentbarpresenter.cpp rename to src/barchart/percentbarchartitem.cpp index d55f986..3ead549 100644 --- a/src/barchart/percentbarpresenter.cpp +++ b/src/barchart/percentbarchartitem.cpp @@ -1,4 +1,4 @@ -#include "percentbarpresenter_p.h" +#include "percentbarchartitem_p.h" #include "bar_p.h" #include "barvalue_p.h" #include "qbarset.h" @@ -7,12 +7,12 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QChart *parent) : - BarPresenterBase(series, parent) +PercentBarChartItem::PercentBarChartItem(QBarSeries *series, QChart *parent) : + BarChartItem(series, parent) { } -void PercentBarPresenter::layoutChanged() +void PercentBarChartItem::layoutChanged() { // Scale bars to new layout // Layout for bars: @@ -23,7 +23,7 @@ void PercentBarPresenter::layoutChanged() } if (childItems().count() == 0) { - qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!"; + qDebug() << "WARNING: PercentBarChartItem::layoutChanged called before graphics items are created!"; return; } @@ -89,6 +89,6 @@ void PercentBarPresenter::layoutChanged() } } -#include "moc_percentbarpresenter_p.cpp" +#include "moc_percentbarchartitem_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barpresenter_p.h b/src/barchart/percentbarchartitem_p.h similarity index 61% rename from src/barchart/barpresenter_p.h rename to src/barchart/percentbarchartitem_p.h index 418aa8f..46e68c4 100644 --- a/src/barchart/barpresenter_p.h +++ b/src/barchart/percentbarchartitem_p.h @@ -1,30 +1,28 @@ -#ifndef BARPRESENTER_H -#define BARPRESENTER_H +#ifndef PERCENTBARCHARTITEM_H +#define PERCENTBARCHARTITEM_H -#include "qchartglobal.h" -#include "barpresenterbase_p.h" +#include "barchartitem_p.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE class QBarSeries; -// Presenter for parallel bars. Grouping of bars is done on category basis. -class BarPresenter : public BarPresenterBase +class PercentBarChartItem : public BarChartItem { Q_OBJECT public: - explicit BarPresenter(QBarSeries *series, QChart *parent = 0); + PercentBarChartItem(QBarSeries *series, QChart *parent = 0); private: - // From BarPresenterBase void layoutChanged(); // layout has changed -> need to recalculate bar sizes private: + // Data }; QTCOMMERCIALCHART_END_NAMESPACE -#endif // BARPRESENTER_H +#endif // PERCENTBARCHARTITEM_H diff --git a/src/barchart/percentbarpresenter_p.h b/src/barchart/percentbarpresenter_p.h deleted file mode 100644 index 8eeb455..0000000 --- a/src/barchart/percentbarpresenter_p.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef PERCENTBARPRESENTER_H -#define PERCENTBARPRESENTER_H - -#include "chartitem_p.h" -#include "bar_p.h" -#include "qpercentbarseries.h" -#include "barpresenterbase_p.h" -#include - -QTCOMMERCIALCHART_BEGIN_NAMESPACE - -class PercentBarPresenter : public BarPresenterBase -{ - Q_OBJECT -public: - PercentBarPresenter(QBarSeries *series, QChart *parent = 0); - -private: - - void layoutChanged(); // layout has changed -> need to recalculate bar sizes - -private: - - // Data -}; - -QTCOMMERCIALCHART_END_NAMESPACE - -#endif // PERCENTBARPRESENTER_H diff --git a/src/barchart/stackedbarpresenter.cpp b/src/barchart/stackedbarchartitem.cpp similarity index 91% rename from src/barchart/stackedbarpresenter.cpp rename to src/barchart/stackedbarchartitem.cpp index f498146..986f240 100644 --- a/src/barchart/stackedbarpresenter.cpp +++ b/src/barchart/stackedbarchartitem.cpp @@ -1,4 +1,4 @@ -#include "stackedbarpresenter_p.h" +#include "stackedbarchartitem_p.h" #include "bar_p.h" #include "barvalue_p.h" #include "qbarset.h" @@ -6,17 +6,17 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QChart *parent) : - BarPresenterBase(series,parent) +StackedBarChartItem::StackedBarChartItem(QBarSeries *series, QChart *parent) : + BarChartItem(series,parent) { } -StackedBarPresenter::~StackedBarPresenter() +StackedBarChartItem::~StackedBarChartItem() { } -void StackedBarPresenter::layoutChanged() +void StackedBarChartItem::layoutChanged() { // Scale bars to new layout // Layout for bars: @@ -33,7 +33,7 @@ void StackedBarPresenter::layoutChanged() } if (childItems().count() == 0) { - qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!"; + qDebug() << "WARNING: StackedBarChartItem::layoutChanged called before graphics items are created!"; return; } @@ -92,6 +92,6 @@ void StackedBarPresenter::layoutChanged() } } -#include "moc_stackedbarpresenter_p.cpp" +#include "moc_stackedbarchartitem_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/stackedbarpresenter_p.h b/src/barchart/stackedbarchartitem_p.h similarity index 60% rename from src/barchart/stackedbarpresenter_p.h rename to src/barchart/stackedbarchartitem_p.h index 9faadbb..a035b42 100644 --- a/src/barchart/stackedbarpresenter_p.h +++ b/src/barchart/stackedbarchartitem_p.h @@ -1,21 +1,21 @@ -#ifndef STACKEDBARPRESENTER_H -#define STACKEDBARPRESENTER_H +#ifndef STACKEDBARCHARTITEM_H +#define STACKEDBARCHARTITEM_H -#include "barpresenterbase_p.h" +#include "barchartitem_p.h" #include "qstackedbarseries.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class StackedBarPresenter : public BarPresenterBase +class StackedBarChartItem : public BarChartItem { Q_OBJECT public: - StackedBarPresenter(QBarSeries *series, QChart *parent = 0); - ~StackedBarPresenter(); + StackedBarChartItem(QBarSeries *series, QChart *parent = 0); + ~StackedBarChartItem(); private: - // From BarPresenterBase + // From BarChartItem void layoutChanged(); // layout has changed -> need to recalculate bar sizes private: @@ -25,4 +25,4 @@ private: QTCOMMERCIALCHART_END_NAMESPACE -#endif // STACKEDBARPRESENTER_H +#endif // STACKEDBARCHARTITEM_H diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 52436dc..d401542 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -16,9 +16,9 @@ //items #include "axisitem_p.h" #include "areachartitem_p.h" -#include "barpresenter_p.h" -#include "stackedbarpresenter_p.h" -#include "percentbarpresenter_p.h" +#include "barchartitem_p.h" +#include "stackedbarchartitem_p.h" +#include "percentbarchartitem_p.h" #include "linechartitem_p.h" #include "piechartitem_p.h" #include "scatterchartitem_p.h" @@ -160,7 +160,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) case QSeries::SeriesTypeBar: { QBarSeries* barSeries = static_cast(series); - BarPresenter* bar = new BarPresenter(barSeries,m_chart); + BarChartItem* bar = new BarChartItem(barSeries,m_chart); if(m_options.testFlag(QChart::SeriesAnimations)) { // m_animator->addAnimation(bar); } @@ -173,7 +173,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) case QSeries::SeriesTypeStackedBar: { QStackedBarSeries* stackedBarSeries = static_cast(series); - StackedBarPresenter* bar = new StackedBarPresenter(stackedBarSeries,m_chart); + StackedBarChartItem* bar = new StackedBarChartItem(stackedBarSeries,m_chart); if(m_options.testFlag(QChart::SeriesAnimations)) { // m_animator->addAnimation(bar); } @@ -186,7 +186,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) case QSeries::SeriesTypePercentBar: { QPercentBarSeries* percentBarSeries = static_cast(series); - PercentBarPresenter* bar = new PercentBarPresenter(percentBarSeries,m_chart); + PercentBarChartItem* bar = new PercentBarChartItem(percentBarSeries,m_chart); if(m_options.testFlag(QChart::SeriesAnimations)) { // m_animator->addAnimation(bar); } diff --git a/src/charttheme.cpp b/src/charttheme.cpp index a99eb07..4a9c33f 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -19,9 +19,9 @@ //items #include "axisitem_p.h" -#include "barpresenter_p.h" -#include "stackedbarpresenter_p.h" -#include "percentbarpresenter_p.h" +#include "barchartitem_p.h" +#include "stackedbarchartitem_p.h" +#include "percentbarchartitem_p.h" #include "linechartitem_p.h" #include "areachartitem_p.h" #include "scatterchartitem_p.h" diff --git a/src/charttheme_p.h b/src/charttheme_p.h index ee77ed4..4b295ea 100644 --- a/src/charttheme_p.h +++ b/src/charttheme_p.h @@ -12,12 +12,12 @@ class ChartItem; class QSeries; class LineChartItem; class QLineSeries; -class BarPresenter; +class BarChartItem; class QBarSeries; -class StackedBarPresenter; +class StackedBarChartItem; class QStackedBarSeries; class QPercentBarSeries; -class PercentBarPresenter; +class PercentBarChartItem; class QScatterSeries; class ScatterChartItem; class PieChartItem; diff --git a/src/qchart.h b/src/qchart.h index b0f3425..77f9c6d 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -14,7 +14,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class AxisItem; class QSeries; class PlotDomain; -class BarPresenter; +class BarChartItem; class QChartAxis; class ChartTheme; class ChartItem;