From 1f789ed92d7bfed26f84a655b089e9cb95f672e5 2012-01-25 15:10:28 From: sauimone Date: 2012-01-25 15:10:28 Subject: [PATCH] Improved bar chart series --- diff --git a/src/barchart/barchartseries.cpp b/src/barchart/barchartseries.cpp index 89b1e60..b3cc8a5 100644 --- a/src/barchart/barchartseries.cpp +++ b/src/barchart/barchartseries.cpp @@ -1,3 +1,4 @@ +#include #include "barchartseries.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -6,23 +7,29 @@ BarChartSeries::BarChartSeries(QObject *parent) { } -bool BarChartSeries::setData(QList data) +bool BarChartSeries::setData(QAbstractItemModel* model) { - mData = data; - return true; + mModel = model; } + int BarChartSeries::min() { - Q_ASSERT(mData.count() > 0); + Q_ASSERT(mModel->rowCount() > 0); + Q_ASSERT(mModel->columnCount() > 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 = mData.at(0); + QModelIndex modelIndex = mModel->index(0,0); + int min = mModel->data(modelIndex).toInt(); - for (int i=0; i rowCount(); i++) { + for(int j=0; jcolumnCount(); j++) { + modelIndex = mModel->index(i,j); + int temp = mModel->data(modelIndex).toInt(); + if (temp < min) { + min = temp; + } } } return min; @@ -30,26 +37,54 @@ int BarChartSeries::min() int BarChartSeries::max() { - Q_ASSERT(mData.count() > 0); + Q_ASSERT(mModel->rowCount() > 0); + Q_ASSERT(mModel->columnCount() > 0); - int max = mData.at(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. + QModelIndex modelIndex = mModel->index(0,0); + int max = mModel->data(modelIndex).toInt(); - for (int i=0; i max) { - max = mData.at(i); + for (int i=0; i rowCount(); i++) { + for(int j=0; jcolumnCount(); j++) { + modelIndex = mModel->index(i,j); + int temp = mModel->data(modelIndex).toInt(); + if (temp > max) { + max = temp; + } } } return max; } -int BarChartSeries::count() + +int BarChartSeries::countSeries() +{ + return mModel->rowCount(); +} + +int BarChartSeries::countItemsInSeries() { - return mData.count(); + return mModel->columnCount(); } -int BarChartSeries::valueAt(int i) +int BarChartSeries::countTotalItems() { - return mData.at(i); + return mModel->rowCount() * mModel->columnCount(); } +int BarChartSeries::valueAt(int series, int item) +{ + QModelIndex index = mModel->index(series,item); + return mModel->data(index).toInt(); +} + + +void BarChartSeries::chartSizeChanged(QRectF rect) +{ + qDebug() << "barchart size changed:" << rect; +} + +#include "moc_barchartseries.cpp" + QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barchartseries.h b/src/barchart/barchartseries.h index 6a4045e..5950ab4 100644 --- a/src/barchart/barchartseries.h +++ b/src/barchart/barchartseries.h @@ -2,6 +2,8 @@ #define BARCHARTSERIES_H #include +#include +#include #include "qchartseries.h" #include "qchartglobal.h" @@ -10,28 +12,32 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE // Container for series class QTCOMMERCIALCHART_EXPORT BarChartSeries : public QChartSeries { - // TODO: -// Q_OBJECT + Q_OBJECT public: BarChartSeries(QObject* parent=0); // from QChartSeries virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } - virtual bool setData(QList data); - virtual bool setData(QList data) {return false;} - virtual bool setData(QList x, QList y) {return false;} - + // TODO: This as dataModel instead of n different setters. (data model itself can accept lists and whatnot) + virtual bool setData(QAbstractItemModel* model); // Methods to find out minimum and maximum values of data int min(); int max(); - int count(); - int valueAt(int i); + int countSeries(); + int countItemsInSeries(); // Count items in one series. + int countTotalItems(); + int valueAt(int series, int item); + +public Q_SLOTS: + + void chartSizeChanged(QRectF rect); private: - QList mData; + //QList mData; + QAbstractItemModel* mModel; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/bargroup.cpp b/src/barchart/bargroup.cpp index 9e50cf8..949130d 100644 --- a/src/barchart/bargroup.cpp +++ b/src/barchart/bargroup.cpp @@ -4,11 +4,17 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE +// TODO: singleton? +//BarGroup* BarGroup::mBarGroupInstance = NULL; + +//BarGroup::BarGroup(QGraphicsItem *parent) : +// QGraphicsItem(parent) +// ,mSeries(series) BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : - QGraphicsItem(parent) - ,mSeries(series) - ,mLayoutSet(false) - ,mLayoutDirty(true) + QGraphicsItem(parent) + ,mSeries(series) + ,mLayoutSet(false) + ,mLayoutDirty(true) { dataChanged(); } @@ -68,7 +74,8 @@ void BarGroup::dataChanged() } // Create new graphic items for bars - for (int i=0; i (childItems().at(i)); - - bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar - bar->setColor(mColor); - bar->setPos(i*posStep+startPos, 0); + int itemIndex(0); + for (int series = 0; series < count; series++) { + for (int item=0; item < count; item++) { + int barHeight = mSeries.valueAt(series, item) * mHeight / mMax; + Bar* bar = reinterpret_cast (childItems().at(itemIndex)); + + bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar + bar->setColor(mColor); + bar->setPos(itemIndex*posStep+startPos, 0); + itemIndex++; + } } mLayoutDirty = true; } diff --git a/src/barchart/bargroup.h b/src/barchart/bargroup.h index 85d0a19..78a55b3 100644 --- a/src/barchart/bargroup.h +++ b/src/barchart/bargroup.h @@ -1,7 +1,7 @@ #ifndef QBARCHART_H #define QBARCHART_H -#include +#include #include "bar.h" //#include "qbarchartview.h" @@ -10,11 +10,25 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE // TODO: Better name for this? The function of this class is to know where each bar in series is laid out. -// Class has knowledge of single series of data (owns vs reference?) +//class BarGroup : public QGraphicsItemGroup + class BarGroup : public QGraphicsItem { +/* // TODO: implement as singleton? +private: + static BarGroup* mBarGroupInstance; public: + static BarGroup* instance() + { + if (mBarGroupInstance == NULL) { + mBarGroupInstance = new BarGroup(); + } + return mBarGroupInstance; + } +private: +*/ +public: explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0); // Layout "api" diff --git a/src/qchart.cpp b/src/qchart.cpp index c16e0f0..bb73d17 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -84,11 +84,9 @@ void QChart::addSeries(QChartSeries* series) qDebug() << "barSeries added"; BarChartSeries* barSeries = static_cast(series); + connect(this, SIGNAL(sizeChanged(QRectF)), + barSeries, SLOT(chartSizeChanged(QRectF))); - // Who owns the series? - BarGroup* group = new BarGroup(*barSeries, this); - scene()->addItem(group); - m_BarGroupItems.append(group); // If we need to access group later break; } case QChartSeries::SeriesTypeScatter: { diff --git a/src/qchart.h b/src/qchart.h index a2a85dc..55c82e2 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -78,7 +78,6 @@ private: int m_plotDataIndex; int m_marginSize; QList m_themeColors; - QList m_BarGroupItems; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartseries.cpp b/src/qchartseries.cpp index a087b8d..b728300 100644 --- a/src/qchartseries.cpp +++ b/src/qchartseries.cpp @@ -8,6 +8,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QChartSeries* QChartSeries::create(QChartSeriesType type, QObject* parent) { + qDebug() << "QChartSeries::create"; // TODO: Other types switch (type) { case QChartSeries::SeriesTypeLine: { @@ -29,4 +30,5 @@ QChartSeries* QChartSeries::create(QChartSeriesType type, QObject* parent) } } +#include "moc_qchartseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartseries.h b/src/qchartseries.h index 71e2037..86d033c 100644 --- a/src/qchartseries.h +++ b/src/qchartseries.h @@ -3,13 +3,13 @@ #include "qchartglobal.h" #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE class QTCOMMERCIALCHART_EXPORT QChartSeries : public QObject { - //TODO: - //Q_OBJECT + Q_OBJECT public: enum QChartSeriesType { SeriesTypeLine = 0, @@ -35,6 +35,10 @@ public: virtual bool setData(QList data) { return false; } virtual bool setData(QList data) { return false; } virtual bool setData(QList x, QList y){ return false; } + + // Prototype for data model. TODO: remove the other setData methods and use something like this for now? + virtual bool setData(QAbstractItemModel* model) { return false; } + }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index 4b327e2..9f9a918 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -303,7 +303,7 @@ void MainWidget::yMaxChanged(int value) void MainWidget::changeChartTheme(int themeIndex) { qDebug() << "changeChartTheme: " << themeIndex; - m_chartWidget->setTheme((QChart::ChartTheme) themeIndex); +// m_chartWidget->setTheme((QChart::ChartTheme) themeIndex); } void MainWidget::setPieSizeFactor(double size)