diff --git a/src/barchart/bar.cpp b/src/barchart/bar.cpp index da6ce29..615314b 100644 --- a/src/barchart/bar.cpp +++ b/src/barchart/bar.cpp @@ -5,8 +5,9 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Bar::Bar(QGraphicsItem *parent) - : ChartItem(parent) + : QGraphicsObject(parent) { + setAcceptedMouseButtons(Qt::LeftButton); } void Bar::setSize(const QSizeF& size) @@ -18,7 +19,6 @@ void Bar::setSize(const QSizeF& size) void Bar::resize( qreal w, qreal h ) { -// qDebug() << "bar::resize" << w << h; mWidth = w; mHeight = h; } @@ -30,7 +30,6 @@ void Bar::setColor( QColor col ) void Bar::setPos(qreal x, qreal y) { - // qDebug() << "Bar::setpos" << x << y; mXpos = x; mYpos = y; } @@ -50,10 +49,7 @@ void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidg if (0 == mHeight) { return; } - // TODO: accept brush instead of color 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; @@ -71,6 +67,11 @@ QRectF Bar::boundingRect() const return r; } -//#include "moc_bar_p.cpp" +void Bar::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) +{ + emit clicked(); +} + +#include "moc_bar_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/bar_p.h b/src/barchart/bar_p.h index 013c01d..6b2c13a 100644 --- a/src/barchart/bar_p.h +++ b/src/barchart/bar_p.h @@ -1,17 +1,17 @@ #ifndef BAR_H #define BAR_H -#include "chartitem_p.h" #include "qchartglobal.h" -#include +#include #include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE // Single bar item of chart -class Bar : public ChartItem +class Bar : public QGraphicsObject { + Q_OBJECT public: Bar(QGraphicsItem *parent=0); @@ -30,6 +30,10 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); QRectF boundingRect() const; + void mousePressEvent(QGraphicsSceneMouseEvent *event); + +Q_SIGNALS: + void clicked(); private: diff --git a/src/barchart/barpresenter.cpp b/src/barchart/barpresenter.cpp index f23f4fb..d66b7d9 100644 --- a/src/barchart/barpresenter.cpp +++ b/src/barchart/barpresenter.cpp @@ -14,7 +14,6 @@ BarPresenter::BarPresenter(BarChartModel& model, QGraphicsItem *parent) : void BarPresenter::layoutChanged() { -// qDebug() << "BarGroup::layoutChanged"; // Scale bars to new layout // Layout for bars: if (mModel.countSets() <= 0) { @@ -23,7 +22,7 @@ void BarPresenter::layoutChanged() } if (childItems().count() == 0) { - qDebug() << "WARNING: BarGroup::layoutChanged called before graphics items are created!"; + qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!"; return; } @@ -41,14 +40,14 @@ void BarPresenter::layoutChanged() // Scaling. int itemIndex(0); - int labelIndex = itemCount * setCount; + int labelIndex(0); for (int item=0; item < itemCount; item++) { qreal xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); qreal yPos = mHeight; for (int set = 0; set < setCount; set++) { qreal barHeight = mModel.valueAt(set, item) * scale; - Bar* bar = reinterpret_cast (childItems().at(itemIndex)); + Bar* bar = mBars.at(itemIndex); // TODO: width settable per bar? bar->resize(mBarDefaultWidth, barHeight); @@ -60,7 +59,7 @@ void BarPresenter::layoutChanged() // TODO: Layout for labels, remove magic number xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); - BarLabel* label = reinterpret_cast (childItems().at(labelIndex)); + BarLabel* label = mLabels.at(labelIndex); label->setPos(xPos, mHeight + 20); labelIndex++; } diff --git a/src/barchart/barpresenter.h b/src/barchart/barpresenter.h index 036f6ef..197c0ab 100644 --- a/src/barchart/barpresenter.h +++ b/src/barchart/barpresenter.h @@ -7,8 +7,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -// Base class for bar groups - +// Presenter for parallel bars. Grouping of bars is done on category basis. class BarPresenter : public BarPresenterBase { public: diff --git a/src/barchart/barpresenterbase.cpp b/src/barchart/barpresenterbase.cpp index 6d8c36f..06c7328 100644 --- a/src/barchart/barpresenterbase.cpp +++ b/src/barchart/barpresenterbase.cpp @@ -25,9 +25,8 @@ void BarPresenterBase::setSeparatorsVisible(bool visible) void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { -// qDebug() << "BarGroupBase::paint" << childItems().count(); if (!mLayoutSet) { - qDebug() << "BarGroupBase::paint called without layout set. Aborting."; + qDebug() << "BarPresenterBase::paint called without layout set. Aborting."; return; } // if (mLayoutDirty) { @@ -51,19 +50,24 @@ void BarPresenterBase::setBarWidth( int 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; } + mBars.clear(); + mLabels.clear(); + mSeparators.clear(); + // Create new graphic items for bars for (int s=0; sset(mModel.label(i)); childItems().append(label); + mLabels.append(label); } count = mModel.countCategories() - 1; // There is one less separator than columns @@ -79,6 +84,7 @@ void BarPresenterBase::dataChanged() Separator* sep = new Separator(this); sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme childItems().append(sep); + mSeparators.append(sep); } // TODO: if (autolayout) { layoutChanged() } or something @@ -89,13 +95,13 @@ void BarPresenterBase::dataChanged() void BarPresenterBase::handleModelChanged(int index) { -// qDebug() << "BarGroupBase::handleModelChanged" << index; +// qDebug() << "BarPresenterBase::handleModelChanged" << index; dataChanged(); } void BarPresenterBase::handleDomainChanged(const Domain& domain) { -// qDebug() << "BarGroupBase::handleDomainChanged"; +// qDebug() << "BarPresenterBase::handleDomainChanged"; // TODO: Figure out the use case for this. // Affects the size of visible item, so layout is changed. // layoutChanged(); diff --git a/src/barchart/barpresenterbase.h b/src/barchart/barpresenterbase.h index bb8dc8f..0d39f8a 100644 --- a/src/barchart/barpresenterbase.h +++ b/src/barchart/barpresenterbase.h @@ -9,7 +9,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -// Base Class for bar groups. Common implemantation of different groups. Not to be instantiated. +class Bar; +class BarLabel; +class Separator; + +// Common implemantation of different presenters. Not to be instantiated. class BarPresenterBase : public QObject, public ChartItem { Q_OBJECT @@ -51,10 +55,14 @@ protected: bool mLayoutSet; // True, if component has been laid out. bool mLayoutDirty; - QList mColors; // List of colors for series for now bool mSeparatorsVisible; BarChartModel& mModel; + // Not owned. + QList mBars; + QList mLabels; + QList mSeparators; + QPen mPen; }; diff --git a/src/barchart/percentbarpresenter.cpp b/src/barchart/percentbarpresenter.cpp index 20431d4..049644d 100644 --- a/src/barchart/percentbarpresenter.cpp +++ b/src/barchart/percentbarpresenter.cpp @@ -15,7 +15,6 @@ PercentBarPresenter::PercentBarPresenter(BarChartModel& model, QGraphicsItem *pa void PercentBarPresenter::layoutChanged() { -// qDebug() << "PercentBarGroup::layoutChanged"; // Scale bars to new layout // Layout for bars: if (mModel.countSets() <= 0) { @@ -25,7 +24,7 @@ void PercentBarPresenter::layoutChanged() } if (childItems().count() == 0) { - qDebug() << "WARNING: PercentBarGroup::layoutChanged called before graphics items are created!"; + qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!"; return; } @@ -33,11 +32,11 @@ void PercentBarPresenter::layoutChanged() // Use reals for accurancy (we might get some compiler warnings... :) int count = mModel.countCategories(); int itemIndex(0); + int labelIndex(0); qreal tW = mWidth; qreal tC = count+1; qreal xStep = (tW/tC); qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); - int labelIndex = mModel.countCategories() * mModel.countSets(); for (int category = 0; category < mModel.countCategories(); category++) { qreal colSum = mModel.categorySum(category); @@ -46,7 +45,7 @@ void PercentBarPresenter::layoutChanged() qreal yPos = h; for (int set=0; set < mModel.countSets(); set++) { qreal barHeight = mModel.valueAt(set, category) * scale; - Bar* bar = reinterpret_cast (childItems().at(itemIndex)); + Bar* bar = mBars.at(itemIndex); // TODO: width settable per bar? bar->resize(mBarDefaultWidth, barHeight); @@ -57,17 +56,17 @@ void PercentBarPresenter::layoutChanged() } // TODO: Layout for labels, remove magic number - BarLabel* label = reinterpret_cast (childItems().at(labelIndex)); + BarLabel* label = mLabels.at(labelIndex); label->setPos(xPos, mHeight + 20); labelIndex++; xPos += xStep; } // Position separators - int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? - xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. + int separatorIndex(0); + xPos = xStep + xStep/2; for (int s=0; s < mModel.countCategories() - 1; s++) { - Separator* sep = reinterpret_cast (childItems().at(separatorIndex)); + Separator* sep = mSeparators.at(separatorIndex); sep->setPos(xPos,0); sep->setSize(QSizeF(1,mHeight)); xPos += xStep; diff --git a/src/barchart/stackedbarpresenter.cpp b/src/barchart/stackedbarpresenter.cpp index 74fdcdc..4c73fb4 100644 --- a/src/barchart/stackedbarpresenter.cpp +++ b/src/barchart/stackedbarpresenter.cpp @@ -14,7 +14,6 @@ StackedBarPresenter::StackedBarPresenter(BarChartModel& model, QGraphicsItem *pa void StackedBarPresenter::layoutChanged() { -// qDebug() << "StackedBarGroup::layoutChanged"; // Scale bars to new layout // Layout for bars: if (mModel.countSets() <= 0) { @@ -30,7 +29,7 @@ void StackedBarPresenter::layoutChanged() } if (childItems().count() == 0) { - qDebug() << "WARNING: StackedBarGroup::layoutChanged called before graphics items are created!"; + qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!"; return; } @@ -42,17 +41,17 @@ void StackedBarPresenter::layoutChanged() qreal scale = (h / maxSum); int itemIndex(0); + int labelIndex(0); qreal tW = mWidth; qreal tC = mModel.countCategories() + 1; qreal xStep = (tW/tC); qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); - int labelIndex = mModel.countSets() * mModel.countCategories(); for (int category = 0; category < mModel.countCategories(); category++) { qreal yPos = h; for (int set=0; set < mModel.countSets(); set++) { qreal barHeight = mModel.valueAt(set, category) * scale; - Bar* bar = reinterpret_cast (childItems().at(itemIndex)); + Bar* bar = mBars.at(itemIndex); bar->resize(mBarDefaultWidth, barHeight); bar->setBrush(mModel.setAt(set).brush()); @@ -62,17 +61,17 @@ void StackedBarPresenter::layoutChanged() } // TODO: Layout for labels, remove magic number - BarLabel* label = reinterpret_cast (childItems().at(labelIndex)); + BarLabel* label = mLabels.at(labelIndex); label->setPos(xPos, mHeight + 20); labelIndex++; xPos += xStep; } // Position separators - int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? - xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. + int separatorIndex(0); + xPos = xStep + xStep/2; for (int s=0; s < mModel.countCategories() - 1; s++) { - Separator* sep = reinterpret_cast (childItems().at(separatorIndex)); + Separator* sep = mSeparators.at(separatorIndex); sep->setPos(xPos,0); sep->setSize(QSizeF(1,mHeight)); xPos += xStep; diff --git a/src/barchart/stackedbarpresenter.h b/src/barchart/stackedbarpresenter.h index fc79acc..1db1be0 100644 --- a/src/barchart/stackedbarpresenter.h +++ b/src/barchart/stackedbarpresenter.h @@ -1,5 +1,5 @@ -#ifndef STACKEDBARGROUP_H -#define STACKEDBARGROUP_H +#ifndef STACKEDBARPRESENTER_H +#define STACKEDBARPRESENTER_H #include "barpresenterbase.h" #include "qstackedbarchartseries.h" @@ -13,7 +13,7 @@ public: StackedBarPresenter(BarChartModel& model, QGraphicsItem *parent = 0); private: - // From BarGroupBase + // From BarPresenterBase void layoutChanged(); // layout has changed -> need to recalculate bar sizes private: @@ -23,4 +23,4 @@ private: QTCOMMERCIALCHART_END_NAMESPACE -#endif // STACKEDBARGROUP_H +#endif // STACKEDBARPRESENTER_H