From cc2daab2560deb7d68898130ca839a36f6ba5ee5 2012-02-09 15:30:59 From: sauimone Date: 2012-02-09 15:30:59 Subject: [PATCH] model delegate for bar series. updated examples --- diff --git a/example/barchart/main.cpp b/example/barchart/main.cpp index fe2556f..8e9474d 100644 --- a/example/barchart/main.cpp +++ b/example/barchart/main.cpp @@ -14,67 +14,22 @@ int main(int argc, char *argv[]) BarChartSeries* series0 = new BarChartSeries(); // Create some test data to chart - QStandardItemModel dataModel(5,10); - QModelIndex index; - index = dataModel.index(0,0); - - // Series 0 - dataModel.setData(dataModel.index(0,0),1); - dataModel.setData(dataModel.index(0,1),2); - dataModel.setData(dataModel.index(0,2),3); - dataModel.setData(dataModel.index(0,3),4); - dataModel.setData(dataModel.index(0,4),5); - dataModel.setData(dataModel.index(0,5),6); - dataModel.setData(dataModel.index(0,6),7); - dataModel.setData(dataModel.index(0,7),8); - dataModel.setData(dataModel.index(0,8),9); - dataModel.setData(dataModel.index(0,9),10); - - // Series 1, some other items missing - dataModel.setData(dataModel.index(1,0),5); - dataModel.setData(dataModel.index(1,3),4); - dataModel.setData(dataModel.index(1,5),7); - dataModel.setData(dataModel.index(1,6),8); - dataModel.setData(dataModel.index(1,8),9); - dataModel.setData(dataModel.index(1,9),9); - - // Series 2 - dataModel.setData(dataModel.index(2,0),3); - dataModel.setData(dataModel.index(2,1),5); - dataModel.setData(dataModel.index(2,2),8); - dataModel.setData(dataModel.index(2,3),13); - dataModel.setData(dataModel.index(2,4),8); - dataModel.setData(dataModel.index(2,5),5); - dataModel.setData(dataModel.index(2,6),3); - dataModel.setData(dataModel.index(2,7),2); - dataModel.setData(dataModel.index(2,8),1); - dataModel.setData(dataModel.index(2,9),1); - - // Series 3 - dataModel.setData(dataModel.index(3,0),5); - dataModel.setData(dataModel.index(3,1),6); - dataModel.setData(dataModel.index(3,2),7); - dataModel.setData(dataModel.index(3,3),3); - dataModel.setData(dataModel.index(3,4),4); - dataModel.setData(dataModel.index(3,5),5); - dataModel.setData(dataModel.index(3,6),8); - dataModel.setData(dataModel.index(3,7),9); - dataModel.setData(dataModel.index(3,8),10); - dataModel.setData(dataModel.index(3,9),5); - - // Series 4 - dataModel.setData(dataModel.index(4,0),9); - dataModel.setData(dataModel.index(4,1),7); - dataModel.setData(dataModel.index(4,2),5); - dataModel.setData(dataModel.index(4,3),3); - dataModel.setData(dataModel.index(4,4),1); - dataModel.setData(dataModel.index(4,5),2); - dataModel.setData(dataModel.index(4,6),4); - dataModel.setData(dataModel.index(4,7),6); - dataModel.setData(dataModel.index(4,8),8); - dataModel.setData(dataModel.index(4,9),10); - - series0->setData(&dataModel); + QList data0; + data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10; + QList data1; + data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0; + QList data2; + data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1; + QList data3; + data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5; + QList data4; + data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10; + + series0->addData(data0); + series0->addData(data1); + series0->addData(data2); + series0->addData(data3); + series0->addData(data4); ChartWidget* chartWidget = new ChartWidget(&window); chartWidget->addSeries(series0); diff --git a/example/percentbarchart/main.cpp b/example/percentbarchart/main.cpp index 056eb91..7c791ff 100644 --- a/example/percentbarchart/main.cpp +++ b/example/percentbarchart/main.cpp @@ -14,67 +14,22 @@ int main(int argc, char *argv[]) PercentBarChartSeries* series0 = new PercentBarChartSeries(); // Create some test data to chart - QStandardItemModel dataModel(5,10); - QModelIndex index; - index = dataModel.index(0,0); - - // Series 0 - dataModel.setData(dataModel.index(0,0),1); - dataModel.setData(dataModel.index(0,1),2); - dataModel.setData(dataModel.index(0,2),3); - dataModel.setData(dataModel.index(0,3),4); - dataModel.setData(dataModel.index(0,4),5); - dataModel.setData(dataModel.index(0,5),6); - dataModel.setData(dataModel.index(0,6),7); - dataModel.setData(dataModel.index(0,7),8); - dataModel.setData(dataModel.index(0,8),9); - dataModel.setData(dataModel.index(0,9),10); - - // Series 1, some other items missing - dataModel.setData(dataModel.index(1,0),5); - dataModel.setData(dataModel.index(1,3),4); - dataModel.setData(dataModel.index(1,5),7); - dataModel.setData(dataModel.index(1,6),8); - dataModel.setData(dataModel.index(1,8),9); - dataModel.setData(dataModel.index(1,9),9); - - // Series 2 - dataModel.setData(dataModel.index(2,0),3); - dataModel.setData(dataModel.index(2,1),5); - dataModel.setData(dataModel.index(2,2),8); - dataModel.setData(dataModel.index(2,3),13); - dataModel.setData(dataModel.index(2,4),8); - dataModel.setData(dataModel.index(2,5),5); - dataModel.setData(dataModel.index(2,6),3); - dataModel.setData(dataModel.index(2,7),2); - dataModel.setData(dataModel.index(2,8),1); - dataModel.setData(dataModel.index(2,9),1); - - // Series 3 - dataModel.setData(dataModel.index(3,0),5); - dataModel.setData(dataModel.index(3,1),6); - dataModel.setData(dataModel.index(3,2),7); - dataModel.setData(dataModel.index(3,3),3); - dataModel.setData(dataModel.index(3,4),4); - dataModel.setData(dataModel.index(3,5),5); - dataModel.setData(dataModel.index(3,6),8); - dataModel.setData(dataModel.index(3,7),9); - dataModel.setData(dataModel.index(3,8),10); - dataModel.setData(dataModel.index(3,9),5); - - // Series 4 - dataModel.setData(dataModel.index(4,0),9); - dataModel.setData(dataModel.index(4,1),7); - dataModel.setData(dataModel.index(4,2),5); - dataModel.setData(dataModel.index(4,3),3); - dataModel.setData(dataModel.index(4,4),1); - dataModel.setData(dataModel.index(4,5),2); - dataModel.setData(dataModel.index(4,6),4); - dataModel.setData(dataModel.index(4,7),6); - dataModel.setData(dataModel.index(4,8),8); - dataModel.setData(dataModel.index(4,9),10); - - series0->setData(&dataModel); + QList data0; + data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10; + QList data1; + data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0; + QList data2; + data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1; + QList data3; + data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5; + QList data4; + data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10; + + series0->addData(data0); + series0->addData(data1); + series0->addData(data2); + series0->addData(data3); + series0->addData(data4); ChartWidget* chartWidget = new ChartWidget(&window); chartWidget->addSeries(series0); diff --git a/example/stackedbarchart/main.cpp b/example/stackedbarchart/main.cpp index 6f16dbd..66f99ab 100644 --- a/example/stackedbarchart/main.cpp +++ b/example/stackedbarchart/main.cpp @@ -14,67 +14,22 @@ int main(int argc, char *argv[]) StackedBarChartSeries* series0 = new StackedBarChartSeries(); // Create some test data to chart - QStandardItemModel dataModel(5,10); - QModelIndex index; - index = dataModel.index(0,0); - - // Series 0 - dataModel.setData(dataModel.index(0,0),1); - dataModel.setData(dataModel.index(0,1),2); - dataModel.setData(dataModel.index(0,2),3); - dataModel.setData(dataModel.index(0,3),4); - dataModel.setData(dataModel.index(0,4),5); - dataModel.setData(dataModel.index(0,5),6); - dataModel.setData(dataModel.index(0,6),7); - dataModel.setData(dataModel.index(0,7),8); - dataModel.setData(dataModel.index(0,8),9); - dataModel.setData(dataModel.index(0,9),10); - - // Series 1, some other items missing - dataModel.setData(dataModel.index(1,0),5); - dataModel.setData(dataModel.index(1,3),4); - dataModel.setData(dataModel.index(1,5),7); - dataModel.setData(dataModel.index(1,6),8); - dataModel.setData(dataModel.index(1,8),9); - dataModel.setData(dataModel.index(1,9),9); - - // Series 2 - dataModel.setData(dataModel.index(2,0),3); - dataModel.setData(dataModel.index(2,1),5); - dataModel.setData(dataModel.index(2,2),8); - dataModel.setData(dataModel.index(2,3),13); - dataModel.setData(dataModel.index(2,4),8); - dataModel.setData(dataModel.index(2,5),5); - dataModel.setData(dataModel.index(2,6),3); - dataModel.setData(dataModel.index(2,7),2); - dataModel.setData(dataModel.index(2,8),1); - dataModel.setData(dataModel.index(2,9),1); - - // Series 3 - dataModel.setData(dataModel.index(3,0),5); - dataModel.setData(dataModel.index(3,1),6); - dataModel.setData(dataModel.index(3,2),7); - dataModel.setData(dataModel.index(3,3),3); - dataModel.setData(dataModel.index(3,4),4); - dataModel.setData(dataModel.index(3,5),5); - dataModel.setData(dataModel.index(3,6),8); - dataModel.setData(dataModel.index(3,7),9); - dataModel.setData(dataModel.index(3,8),10); - dataModel.setData(dataModel.index(3,9),5); - - // Series 4 - dataModel.setData(dataModel.index(4,0),9); - dataModel.setData(dataModel.index(4,1),7); - dataModel.setData(dataModel.index(4,2),5); - dataModel.setData(dataModel.index(4,3),3); - dataModel.setData(dataModel.index(4,4),1); - dataModel.setData(dataModel.index(4,5),2); - dataModel.setData(dataModel.index(4,6),4); - dataModel.setData(dataModel.index(4,7),6); - dataModel.setData(dataModel.index(4,8),8); - dataModel.setData(dataModel.index(4,9),10); - - series0->setData(&dataModel); + QList data0; + data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10; + QList data1; + data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0; + QList data2; + data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1; + QList data3; + data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5; + QList data4; + data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10; + + series0->addData(data0); + series0->addData(data1); + series0->addData(data2); + series0->addData(data3); + series0->addData(data4); ChartWidget* chartWidget = new ChartWidget(&window); chartWidget->addSeries(series0); diff --git a/src/barchart/barchartmodel.cpp b/src/barchart/barchartmodel.cpp index cc1e612..62453fc 100644 --- a/src/barchart/barchartmodel.cpp +++ b/src/barchart/barchartmodel.cpp @@ -1,40 +1,54 @@ #include +#include +#include #include "barchartmodel_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE BarChartModel::BarChartModel(QObject *parent) : QObject(parent) + ,mRunningId(1) { } -void BarChartModel::addSeries(BarChartSeriesBase& series) +BarChartModel::~BarChartModel() { - mSeries.append(&series); - emit modelUpdated(); + qDebug() << "BarChartModel::~BarChartModel"; + foreach (DataContainer* c, mDataModel) { + delete c; + } } -void BarChartModel::removeSeries(BarChartSeriesBase& series) +int BarChartModel::addData(QList data) { - int i = mSeries.indexOf(&series); - if (-1 == i) { - return; + qDebug() << "BarChartModel::addData" << data.count(); + DataContainer* c = new DataContainer(data,mRunningId); + mDataModel.append(c); + mRunningId++; + return mRunningId-1; +} + +void BarChartModel::removeData(int id) +{ + foreach(DataContainer* c, mDataModel) { + if (c->mId == id) { + mDataModel.removeOne(c); + delete c; + } } - mSeries.removeAt(i); - emit modelUpdated(); } -int BarChartModel::countSeries() +int BarChartModel::countRows() { - return mSeries.count(); + return mDataModel.count(); } -int BarChartModel::countItemsInSeries() +int BarChartModel::countColumns() { int count(0); - for (int i=0; icountValues(); + int temp = mDataModel.at(i)->countColumns(); if (temp > count) { count = temp; } @@ -44,21 +58,26 @@ int BarChartModel::countItemsInSeries() int BarChartModel::countTotalItems() { - int total = mSeries.count() * countItemsInSeries(); + int total = mDataModel.count() * countColumns(); + qDebug() << "BarChartModel::countTotalItems datamodel count" << mDataModel.count(); + qDebug() << "BarChartModel::countTotalItems countColumns count" << countColumns(); return total; } int BarChartModel::min() { - Q_ASSERT(mSeries.count() > 0); + Q_ASSERT(mDataModel.count() > 0); // TODO: make min and max members and update them when data changes. // This is slower since they are checked every time, even if data is same since previous call. int min = INT_MAX; - for (int i=0; i min(); - if (temp < min) { - min = temp; + for (int i=0; i countColumns(); + for (int j=0; jvalueAt(j); + if (temp < min) { + min = temp; + } } } return min; @@ -66,32 +85,63 @@ int BarChartModel::min() int BarChartModel::max() { - Q_ASSERT(mSeries.count() > 0); + Q_ASSERT(mDataModel.count() > 0); // TODO: make min and max members and update them when data changes. // This is slower since they are checked every time, even if data is same since previous call. int max = INT_MIN; - for (int i=0; i min(); - if (temp > max) { - max = temp; + for (int i=0; i countColumns(); + for (int j=0; jvalueAt(j); + if (temp > max) { + max = temp; + } } } + return max; } qreal BarChartModel::valueAt(int series, int item) { - if ((series < 0) || (series >= mSeries.count())) { + if ((series < 0) || (series >= mDataModel.count())) { // No series, no value. return 0; - } else if ((item < 0) || (item >= mSeries.at(series)->countValues())) { + } else if ((item < 0) || (item >= mDataModel.at(series)->countColumns())) { // No item, no value. return 0; } - return mSeries.at(series)->valueAt(item); + return mDataModel.at(series)->valueAt(item); +} + +qreal BarChartModel::columnSum(int column) +{ + int sum(0); + int count = mDataModel.count(); // Count rows + + for (int row = 0; row < count; row++) { + if (column < mDataModel.at(row)->countColumns()) { + sum += mDataModel.at(row)->valueAt(column); + } + } + return sum; +} + +qreal BarChartModel::maxColumnSum() +{ + int max = INT_MIN; + int count = countColumns(); + + for (int col=0; col max) { + max = sum; + } + } + return max; } #include "moc_barchartmodel_p.cpp" diff --git a/src/barchart/barchartmodel_p.h b/src/barchart/barchartmodel_p.h index 808c74c..82b1527 100644 --- a/src/barchart/barchartmodel_p.h +++ b/src/barchart/barchartmodel_p.h @@ -2,11 +2,11 @@ #define BARCHARTMODEL_H #include -#include "barchartseries.h" +#include "qchartglobal.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE -// Model for bar chart. +// Model for bar chart. Internal class. // TODO: Implement as QAbstractItemModel? class BarChartModel : public QObject //, public QAbstractItemModel @@ -14,19 +14,24 @@ class BarChartModel : public QObject //, public QAbstractItemModel Q_OBJECT public: explicit BarChartModel(QObject *parent = 0); + ~BarChartModel(); - // Takes reference, Series are owned by QChart or model? - void addSeries(BarChartSeriesBase& series); - void removeSeries(BarChartSeriesBase& series); + // Adds data to model. returns id. + int addData(QList data); + void removeData(int id); - int countSeries(); // Number of series in model - int countItemsInSeries(); // Maximum number of items in series + int countRows(); // Number of series in model + int countColumns(); // Maximum number of items in series int countTotalItems(); // Total items in all series. Includes empty items. + // TODO: qreal these int max(); // Maximum value of all series int min(); // Minimum value of all series qreal valueAt(int series, int item); + qreal columnSum(int column); + qreal maxColumnSum(); // returns maximum sum of items in all columns. + signals: void modelUpdated(); @@ -34,10 +39,23 @@ public slots: private: - // Data - QList mSeries; + // Little helper class. + class DataContainer { + public: + DataContainer(QList data, int id) : mId(id), mData(data) {} + int countColumns() { return mData.count(); } + qreal valueAt(int item) { return mData.at(item); } + + int mId; // TODO: Is this needed? + private: + QList mData; + }; + + // Owned. N series. each has a list of values. + QList mDataModel; + int mRunningId; + int mMaxColumns; // longest series in datamodel - BarChartModel* mSingle; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barchartseries.h b/src/barchart/barchartseries.h index fd58706..5e85940 100644 --- a/src/barchart/barchartseries.h +++ b/src/barchart/barchartseries.h @@ -21,7 +21,6 @@ public: private: - QAbstractItemModel* mModel; BarGroup* mBarGroup; }; diff --git a/src/barchart/barchartseriesbase.cpp b/src/barchart/barchartseriesbase.cpp index ed4a841..48012fe 100644 --- a/src/barchart/barchartseriesbase.cpp +++ b/src/barchart/barchartseriesbase.cpp @@ -2,13 +2,15 @@ #include #include "barchartseriesbase.h" #include "bargroup.h" +#include "barchartmodel_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE BarChartSeriesBase::BarChartSeriesBase(QObject *parent) : QChartSeries(parent) - ,mData(0) + ,mModel(*(new BarChartModel(this))) // TODO: is this ok? { + qDebug() << "BarChartSeriesBase::BarChartSeriesBase"; } /* bool BarChartSeriesBase::setModel(QAbstractItemModel* model) @@ -17,11 +19,13 @@ bool BarChartSeriesBase::setModel(QAbstractItemModel* model) return true; } */ +/* bool BarChartSeriesBase::setData(QList& data) { mData = &data; return true; } +*/ /* int BarChartSeriesBase::min() { @@ -111,10 +115,22 @@ int BarChartSeriesBase::columnSum(int column) return sum; } */ -qreal BarChartSeriesBase::min() + +int BarChartSeriesBase::addData(QList data) { - Q_ASSERT(mData != 0); + qDebug() << "BarChartSeriesBase::addData"; + return mModel.addData(data); +} +void BarChartSeriesBase::removeData(int id) +{ + mModel.removeData(id); +} + +qreal BarChartSeriesBase::min() +{ + return mModel.min(); + /* Delegated to model int count = mData->count(); int min = INT_MAX; @@ -124,12 +140,13 @@ qreal BarChartSeriesBase::min() } } return min; + */ } qreal BarChartSeriesBase::max() { - Q_ASSERT(mData != 0); - + return mModel.max(); + /* Delegated to model int count = mData->count(); int max = INT_MIN; @@ -139,18 +156,30 @@ qreal BarChartSeriesBase::max() } } return max; + */ +} + +int BarChartSeriesBase::countColumns() +{ + return mModel.countColumns(); +// return mData->count(); +} + +qreal BarChartSeriesBase::valueAt(int series, int item) +{ + return mModel.valueAt(series,item); +// return mData->at(item); } -int BarChartSeriesBase::countValues() +qreal BarChartSeriesBase::maxColumnSum() { - Q_ASSERT(mData != 0); - return mData->count(); + qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel.maxColumnSum(); + return mModel.maxColumnSum(); } -qreal BarChartSeriesBase::valueAt(int item) +BarChartModel& BarChartSeriesBase::model() { - Q_ASSERT(mData != 0); - return mData->at(item); + return mModel; } #include "moc_barchartseriesbase.cpp" diff --git a/src/barchart/barchartseriesbase.h b/src/barchart/barchartseriesbase.h index be3d807..b0c60ae 100644 --- a/src/barchart/barchartseriesbase.h +++ b/src/barchart/barchartseriesbase.h @@ -6,10 +6,11 @@ #include "qchartseries.h" #include "qchartglobal.h" -class BarGroupBase; - QTCOMMERCIALCHART_BEGIN_NAMESPACE +class BarGroupBase; +class BarChartModel; + // Container for series class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries { @@ -21,36 +22,29 @@ public: // from QChartSeries virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; } - // TODO: Better data model? -// virtual bool setModel(QAbstractItemModel* model); - virtual bool setData(QList& data); - - // Methods to find out minimum and maximum values of data -// int min(); // TODO: remove -// int max(); // TODO: remove -// int maxColumnSum(); // TODO: move to model. returns maximum sum of items in all columns. - -// int countRows(); // TODO: remove. -// int countColumns(); // TODO: remove. Count items in one series. -// int countTotalItems(); // TODO: move to model -// int valueAt(int row, int column); // TODO: move to model - -// int columnSum(int column); // TODO: move to model + // TODO: << operator for convinience + // Returns id for vector. + int addData(QList data); + void removeData(int id); qreal min(); qreal max(); - int countValues(); - qreal valueAt(int item); + int countColumns(); // Count items in one series. + qreal valueAt(int series, int item); + qreal maxColumnSum(); + + BarChartModel& model(); + +signals: + void changed(int index); public Q_SLOTS: private: - QAbstractItemModel* mModel; + BarChartModel& mModel; BarGroupBase* mBarGroup; - QList* mData; - }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/bargroup.cpp b/src/barchart/bargroup.cpp index f52955b..a77da51 100644 --- a/src/barchart/bargroup.cpp +++ b/src/barchart/bargroup.cpp @@ -13,22 +13,17 @@ BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : void BarGroup::layoutChanged() { + qDebug() << "BarGroup::layoutChanged"; // Scale bars to new layout // Layout for bars: - /* - if (mSeries.countRows() <= 0) { - // Nothing to do. - return; - } -*/ - if (mModel.countSeries() <= 0) { + if (mModel.countRows() <= 0) { return; } // TODO: better way to auto-layout? // Use reals for accurancy (we might get some compiler warnings... :) - int itemCount = mModel.countItemsInSeries(); - int seriesCount = mModel.countSeries(); + int itemCount = mModel.countColumns(); + int seriesCount = mModel.countRows(); qreal tW = mWidth; qreal tH = mHeight; diff --git a/src/barchart/bargroupbase.cpp b/src/barchart/bargroupbase.cpp index d691732..390864b 100644 --- a/src/barchart/bargroupbase.cpp +++ b/src/barchart/bargroupbase.cpp @@ -9,15 +9,13 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent) : ChartItem(parent) -// ,mSeries(series) ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready ,mLayoutSet(false) ,mLayoutDirty(true) ,mTheme(0) ,mSeparatorsVisible(true) + ,mModel(series.model()) { - mModel.addSeries(series); - dataChanged(); } void BarGroupBase::setSeparatorsVisible(bool visible) @@ -27,16 +25,17 @@ void BarGroupBase::setSeparatorsVisible(bool visible) void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { + qDebug() << "BarGroupBase::paint" << childItems().count(); if (!mLayoutSet) { qDebug() << "BarGroupBase::paint called without layout set. Aborting."; return; } - if (mLayoutDirty) { +// if (mLayoutDirty) { // Layout or data has changed. Need to redraw. foreach(QGraphicsItem* i, childItems()) { i->paint(painter,option,widget); } - } +// } } QRectF BarGroupBase::boundingRect() const @@ -70,11 +69,6 @@ void BarGroupBase::dataChanged() { qDebug() << "BarGroupBase::dataChanged"; - // Find out maximum and minimum of all series. - // TODO: is this actually needed? -// mMax = mModel.max(); -// mMin = mModel.min(); - // Delete old bars foreach (QGraphicsItem* item, childItems()) { delete item; @@ -88,7 +82,7 @@ void BarGroupBase::dataChanged() } // TODO: labels from series. This creates just some example labels - int count = mModel.countItemsInSeries(); // mSeries.countColumns(); + int count = mModel.countColumns(); // mSeries.countColumns(); for (int i=0; isetColor(QColor(255,0,0,255)); // TODO: color for separations from theme @@ -111,12 +105,14 @@ void BarGroupBase::dataChanged() void BarGroupBase::handleModelChanged(int index) { - qDebug() << "BarGroupBase::handleModelChanged"; + qDebug() << "BarGroupBase::handleModelChanged PUUH" << index; + dataChanged(); } void BarGroupBase::handleDomainChanged(const Domain& domain) { - qDebug() << "BarGroupBase::handleModelChanged"; + qDebug() << "BarGroupBase::handleDomainChanged"; + dataChanged(); } void BarGroupBase::handleGeometryChanged(const QRectF& rect) diff --git a/src/barchart/bargroupbase.h b/src/barchart/bargroupbase.h index a2c9a97..bb73044 100644 --- a/src/barchart/bargroupbase.h +++ b/src/barchart/bargroupbase.h @@ -17,7 +17,6 @@ class BarGroupBase : public QObject, public ChartItem Q_OBJECT public: BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0); -// BarGroupBase(BarChartModel& model, QGraphicsItem *parent = 0); void setSeparatorsVisible(bool visible = true); public: // From ChartItem @@ -47,8 +46,6 @@ protected slots: protected: - //BarChartSeriesBase& mSeries; - // TODO: consider these. int mMin; // Min and max values of data. (updated when data is changed, used when drawing) int mMax; @@ -65,7 +62,7 @@ protected: ChartTheme* mTheme; bool mSeparatorsVisible; - BarChartModel mModel; + BarChartModel& mModel; }; diff --git a/src/barchart/percentbarchartseries.h b/src/barchart/percentbarchartseries.h index f8e32ee..ce2d25c 100644 --- a/src/barchart/percentbarchartseries.h +++ b/src/barchart/percentbarchartseries.h @@ -22,7 +22,6 @@ public Q_SLOTS: private: - QAbstractItemModel* mModel; PercentBarGroup* mPercentBarGroup; }; diff --git a/src/barchart/percentbargroup.cpp b/src/barchart/percentbargroup.cpp index 94beb34..9eba91e 100644 --- a/src/barchart/percentbargroup.cpp +++ b/src/barchart/percentbargroup.cpp @@ -14,32 +14,31 @@ PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *p void PercentBarGroup::layoutChanged() { - /* // Scale bars to new layout // Layout for bars: - if (mSeries.countRows() <= 0) { + if (mModel.countRows() <= 0) { // Nothing to do. return; } // TODO: better way to auto-layout // Use reals for accurancy (we might get some compiler warnings... :) - int count = mSeries.countColumns(); + int count = mModel.countColumns(); int itemIndex(0); qreal tW = mWidth; qreal tC = count+1; qreal xStep = (tW/tC); // qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); - int labelIndex = mSeries.countColumns() * mSeries.countRows(); + int labelIndex = mModel.countColumns() * mModel.countRows(); - for (int column = 0; column < mSeries.countColumns(); column++) { - qreal colSum = mSeries.columnSum(column); + for (int column = 0; column < mModel.countColumns(); column++) { + qreal colSum = mModel.columnSum(column); qreal h = mHeight; qreal scale = (h / colSum); qreal yPos = h; - for (int row=0; row < mSeries.countRows(); row++) { - qreal barHeight = mSeries.valueAt(row, column) * scale; + for (int row=0; row < mModel.countRows(); row++) { + qreal barHeight = mModel.valueAt(row, column) * scale; Bar* bar = reinterpret_cast (childItems().at(itemIndex)); // TODO: width settable per bar? @@ -60,14 +59,14 @@ void PercentBarGroup::layoutChanged() // 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. - for (int s=0; s < mSeries.countColumns() - 1; s++) { + for (int s=0; s < mModel.countColumns() - 1; s++) { Separator* sep = reinterpret_cast (childItems().at(separatorIndex)); sep->setPos(xPos,0); sep->setSize(QSizeF(1,mHeight)); xPos += xStep; separatorIndex++; } -*/ + mLayoutDirty = true; } diff --git a/src/barchart/separator.cpp b/src/barchart/separator.cpp index 0f2135e..d280d55 100644 --- a/src/barchart/separator.cpp +++ b/src/barchart/separator.cpp @@ -28,7 +28,7 @@ void Separator::setSize(const QSizeF &size) void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - qDebug() << "separator::paint" << boundingRect(); +// qDebug() << "separator::paint" << boundingRect(); QPen pen(mColor); painter->setPen(pen); painter->drawLine(mXpos,mYpos,mXpos,mHeight); diff --git a/src/barchart/stackedbarchartseries.h b/src/barchart/stackedbarchartseries.h index b34d3b9..680c5a4 100644 --- a/src/barchart/stackedbarchartseries.h +++ b/src/barchart/stackedbarchartseries.h @@ -22,7 +22,6 @@ public Q_SLOTS: private: - QAbstractItemModel* mModel; StackedBarGroup* mStackedBarGroup; }; diff --git a/src/barchart/stackedbargroup.cpp b/src/barchart/stackedbargroup.cpp index 9accb73..82db63e 100644 --- a/src/barchart/stackedbargroup.cpp +++ b/src/barchart/stackedbargroup.cpp @@ -3,7 +3,7 @@ #include "barlabel_p.h" #include "separator_p.h" #include -#include +//#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -14,15 +14,15 @@ StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *p void StackedBarGroup::layoutChanged() { -/* + // Scale bars to new layout // Layout for bars: - if (mSeries.countRows() <= 0) { + if (mModel.countRows() <= 0) { // Nothing to do. return; } - if (mSeries.countColumns() == 0) { + if (mModel.countColumns() == 0) { // Nothing to do return; } @@ -30,21 +30,21 @@ 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... - qreal maxSum = mSeries.maxColumnSum(); + qreal maxSum = mModel.maxColumnSum(); qreal h = mHeight; qreal scale = (h / maxSum); int itemIndex(0); qreal tW = mWidth; - qreal tC = mSeries.countColumns() + 1; + qreal tC = mModel.countColumns() + 1; qreal xStep = (tW/tC); qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); - int labelIndex = mSeries.countColumns() * mSeries.countRows(); + int labelIndex = mModel.countRows() * mModel.countColumns(); - for (int column = 0; column < mSeries.countColumns(); column++) { + for (int column = 0; column < mModel.countColumns(); column++) { qreal yPos = h; - for (int row=0; row < mSeries.countRows(); row++) { - qreal barHeight = mSeries.valueAt(row, column) * scale; + for (int row=0; row < mModel.countRows(); row++) { + qreal barHeight = mModel.valueAt(row, column) * scale; Bar* bar = reinterpret_cast (childItems().at(itemIndex)); // TODO: width settable per bar? @@ -68,14 +68,14 @@ void StackedBarGroup::layoutChanged() // 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. - for (int s=0; s < mSeries.countColumns() - 1; s++) { + for (int s=0; s < mModel.countColumns() - 1; s++) { Separator* sep = reinterpret_cast (childItems().at(separatorIndex)); sep->setPos(xPos,0); sep->setSize(QSizeF(1,mHeight)); xPos += xStep; separatorIndex++; } -*/ + mLayoutDirty = true; } diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index 76fbcf3..78469dc 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -54,38 +54,33 @@ void ChartDataSet::addSeries(QChartSeries* series) case QChartSeries::SeriesTypeBar: { BarChartSeries* barSeries = static_cast(series); - qreal x = barSeries->countValues(); + qreal x = barSeries->countColumns(); qreal y = barSeries->max(); domain.m_minX = qMin(domain.m_minX,x); domain.m_minY = qMin(domain.m_minY,y); domain.m_maxX = qMax(domain.m_maxX,x); domain.m_maxY = qMax(domain.m_maxY,y); - } break; case QChartSeries::SeriesTypeStackedBar: { StackedBarChartSeries* stackedBarSeries = static_cast(series); -/* qreal x = stackedBarSeries->countColumns(); qreal y = stackedBarSeries->maxColumnSum(); domain.m_minX = qMin(domain.m_minX,x); domain.m_minY = qMin(domain.m_minY,y); domain.m_maxX = qMax(domain.m_maxX,x); domain.m_maxY = qMax(domain.m_maxY,y); -*/ } break; case QChartSeries::SeriesTypePercentBar: { PercentBarChartSeries* percentBarSeries = static_cast(series); -/* qreal x = percentBarSeries->countColumns(); domain.m_minX = qMin(domain.m_minX,x); domain.m_minY = 0; domain.m_maxX = qMax(domain.m_maxX,x); domain.m_maxY = 100; -*/ } break;