diff --git a/examples/stackedbarchartdrilldown/main.cpp b/examples/stackedbarchartdrilldown/main.cpp index 664471e..4559af6 100644 --- a/examples/stackedbarchartdrilldown/main.cpp +++ b/examples/stackedbarchartdrilldown/main.cpp @@ -88,7 +88,7 @@ int main(int argc, char *argv[]) // Get the drilldown series from season series and add crop to it. seasonSeries->drilldownSeries(month)->appendBarSet(weeklyCrop); seasonSeries->drilldownSeries(month)->setToolTipEnabled(true); - *monthlyCrop << weeklyCrop->total(); + *monthlyCrop << weeklyCrop->sum(); } seasonSeries->appendBarSet(monthlyCrop); } diff --git a/src/barchart/barchart.pri b/src/barchart/barchart.pri index 220bc47..ac25d9e 100644 --- a/src/barchart/barchart.pri +++ b/src/barchart/barchart.pri @@ -19,7 +19,9 @@ PRIVATE_HEADERS += \ $$PWD/barchartitem_p.h \ $$PWD/percentbarchartitem_p.h \ $$PWD/stackedbarchartitem_p.h \ - $$PWD/barlabel_p.h + $$PWD/barlabel_p.h \ + $$PWD/qbarsetprivate_p.h \ + $$PWD/qbarseriesprivate_p.h PUBLIC_HEADERS += \ $$PWD/qbarseries.h \ diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index 683bc80..5db1334 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -22,11 +22,320 @@ #include "qbarseries.h" #include "qbarset.h" #include "barchartmodel_p.h" +#include "qbarseriesprivate_p.h" +#include "qbarsetprivate_p.h" #include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE +QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *parent) : QObject(parent), + q_ptr(parent), + m_internalModel(new BarChartModel(categories,this)) +{ + m_model = 0; + m_mapCategories = -1; + m_mapBarBottom = -1; + m_mapBarTop = -1; + m_mapFirst = 0; + m_mapCount = 0; + m_mapOrientation = Qt::Vertical; +} + +void QBarSeriesPrivate::appendBarSet(QBarSet *set) +{ + m_internalModel->appendBarSet(set); +// connect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons))); +// connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged())); + emit restructuredBars(); +} + +void QBarSeriesPrivate::removeBarSet(QBarSet *set) +{ +// disconnect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons))); + m_internalModel->removeBarSet(set); + emit restructuredBars(); +} +void QBarSeriesPrivate::appendBarSets(QList sets) +{ + foreach (QBarSet* barset, sets) { + m_internalModel->appendBarSet(barset); + connect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons))); + connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged())); + } + emit restructuredBars(); +} + +void QBarSeriesPrivate::removeBarSets(QList sets) +{ + foreach (QBarSet* barset, sets) { + disconnect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons))); + m_internalModel->removeBarSet(barset); + } + emit restructuredBars(); +} + +void QBarSeriesPrivate::insertBarSet(int i, QBarSet *set) +{ + m_internalModel->insertBarSet(i, set); + // emit barsetChanged(); +} + +void QBarSeriesPrivate::insertCategory(int i, QString category) +{ + m_internalModel->insertCategory(i, category); +} + +void QBarSeriesPrivate::removeCategory(int i) +{ + m_internalModel->removeCategory(i); +} + +int QBarSeriesPrivate::barsetCount() const +{ + // if(m_model) + // return m_mapBarTop - m_mapBarBottom; + // else + return m_internalModel->barsetCount(); +} + +int QBarSeriesPrivate::categoryCount() const +{ + return m_internalModel->categoryCount(); +} + +QList QBarSeriesPrivate::barSets() const +{ + return m_internalModel->barSets(); +} + +QBarSet* QBarSeriesPrivate::barsetAt(int index) +{ + return m_internalModel->barsetAt(index); +} + +QString QBarSeriesPrivate::categoryName(int category) +{ + return m_internalModel->categoryName(category); +} + +void QBarSeriesPrivate::setToolTipEnabled(bool enabled) +{ + // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled. + if (enabled) { + for (int i=0; ibarsetCount(); i++) { + QBarSet *set = m_internalModel->barsetAt(i); + connect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString))); + } + } else { + for (int i=0; ibarsetCount(); i++) { + QBarSet *set = m_internalModel->barsetAt(i); + disconnect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString))); + } + } +} + +void QBarSeriesPrivate::barsetClicked(QString category, Qt::MouseButtons button) +{ + emit clicked(qobject_cast(sender()), category, button); +} + +qreal QBarSeriesPrivate::min() +{ + return m_internalModel->min(); +} + +qreal QBarSeriesPrivate::max() +{ + return m_internalModel->max(); +} + +qreal QBarSeriesPrivate::valueAt(int set, int category) +{ + return m_internalModel->valueAt(set, category); +} + +qreal QBarSeriesPrivate::percentageAt(int set, int category) +{ + return m_internalModel->percentageAt(set, category); +} + +qreal QBarSeriesPrivate::categorySum(int category) +{ + return m_internalModel->categorySum(category); +} + +qreal QBarSeriesPrivate::absoluteCategorySum(int category) +{ + return m_internalModel->absoluteCategorySum(category); +} + +qreal QBarSeriesPrivate::maxCategorySum() +{ + return m_internalModel->maxCategorySum(); +} + +BarChartModel& QBarSeriesPrivate::modelInternal() +{ + return *m_internalModel; +} + +bool QBarSeriesPrivate::setModel(QAbstractItemModel *model) +{ + // disconnect signals from old model + if(m_model) + { + disconnect(m_model, 0, this, 0); + m_mapCategories = -1; + m_mapBarBottom = -1; + m_mapBarTop = -1; + m_mapFirst = 0; + m_mapCount = 0; + m_mapOrientation = Qt::Vertical; + } + + // set new model + if(model) + { + m_model = model; + return true; + } + else + { + m_model = 0; + return false; + } +} + +void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation) +{ + if (!m_model) + return; + + m_mapCategories = categories; + m_mapBarBottom = bottomBoundry; + m_mapBarTop = topBoundry; +// m_mapFirst = 1; + m_mapOrientation = orientation; + + // connect the signals + if (m_mapOrientation == Qt::Vertical) { + m_mapCount = m_model->rowCount() - m_mapFirst; + connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), + this, SLOT(modelUpdated(QModelIndex, QModelIndex))); + connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), + this, SLOT(modelDataAdded(QModelIndex,int,int))); + connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), + this, SLOT(modelDataRemoved(QModelIndex,int,int))); + } else { + m_mapCount = m_model->columnCount() - m_mapFirst; + connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), + this, SLOT(modelUpdated(QModelIndex, QModelIndex))); + connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), + this, SLOT(modelDataAdded(QModelIndex,int,int))); + connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), + this, SLOT(modelDataRemoved(QModelIndex,int,int))); + } + + // create the initial bars + delete m_internalModel; + if (m_mapOrientation == Qt::Vertical) { + QStringList categories; + for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++) + categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString(); + m_internalModel = new BarChartModel(categories, this); + + for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) { + QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1)); + for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++) + *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble(); + appendBarSet(barSet); + } + } else { + QStringList categories; + for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++) + categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString(); + m_internalModel = new BarChartModel(categories, this); + + for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) { + QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1)); + for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++) + *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble(); + appendBarSet(barSet); + } + } +} + +void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) +{ + Q_UNUSED(bottomRight) + + if (m_mapOrientation == Qt::Vertical) + { + // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries + if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount) + barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble()); + } + else + { + // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries + if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount) + barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble()); + } +} + +void QBarSeriesPrivate::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/) +{ + if (m_mapOrientation == Qt::Vertical) { + insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1)); + for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) { + barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble()); + } + } else { + insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1)); + for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) { + barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble()); + } + } + emit restructuredBars(); +} + +void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end) +{ + Q_UNUSED(parent) + Q_UNUSED(end) + + removeCategory(start - m_mapFirst); + for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) + { + barsetAt(i)->removeValue(start - m_mapFirst); + } + emit restructuredBars(); +} + +void QBarSeriesPrivate::barsetChanged() +{ + emit updatedBars(); +} + +QBarCategories QBarSeriesPrivate::categories() const +{ + QBarCategories categories; + int count = m_internalModel->categoryCount(); + for (int i=1; i <= count; i++) { + categories.insert(i, m_internalModel->categoryName(i - 1)); + } + return categories; +} + +void QBarSeriesPrivate::setLabelsVisible(bool visible) +{ + foreach (QBarSet* s, barSets()) { + s->setLabelsVisible(visible); + } +} + + /*! \class QBarSeries \brief part of QtCommercial chart API. @@ -56,15 +365,16 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QBarSeries is QObject which is a child of a \a parent. */ QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) : QSeries(parent), - m_internalModel(new BarChartModel(categories, this)) + d_ptr(new QBarSeriesPrivate(categories, this)) +// m_internalModel(new BarChartModel(categories, this)) { - m_model = 0; - m_mapCategories = -1; - m_mapBarBottom = -1; - m_mapBarTop = -1; - m_mapFirst = 0; - m_mapCount = 0; - m_mapOrientation = Qt::Vertical; +// m_model = 0; +// m_mapCategories = -1; +// m_mapBarBottom = -1; +// m_mapBarTop = -1; +// m_mapFirst = 0; +// m_mapCount = 0; +// m_mapOrientation = Qt::Vertical; } /*! @@ -74,10 +384,16 @@ QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) : QSeries(par */ void QBarSeries::appendBarSet(QBarSet *set) { + Q_D(QBarSeries); + connect(&QBarSetPrivate::pimpl(set), SIGNAL(clicked(QString,Qt::MouseButtons)), d_ptr, SLOT(barsetClicked(QString,Qt::MouseButtons))); + connect(&QBarSetPrivate::pimpl(set), SIGNAL(valueChanged()), d_ptr, SLOT(barsetChanged())); + d->appendBarSet(set); + /* m_internalModel->appendBarSet(set); connect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons))); connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged())); emit restructuredBars(); + */ } /*! @@ -87,9 +403,14 @@ void QBarSeries::appendBarSet(QBarSet *set) */ void QBarSeries::removeBarSet(QBarSet *set) { + Q_D(QBarSeries); + disconnect(&QBarSetPrivate::pimpl(set), SIGNAL(clicked(QString,Qt::MouseButtons)), d_ptr, SLOT(barsetClicked(QString,Qt::MouseButtons))); + d->removeBarSet(set); + /* disconnect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons))); m_internalModel->removeBarSet(set); emit restructuredBars(); + */ } /*! @@ -99,12 +420,16 @@ void QBarSeries::removeBarSet(QBarSet *set) */ void QBarSeries::appendBarSets(QList sets) { + Q_D(QBarSeries); + d->appendBarSets(sets); + /* foreach (QBarSet* barset, sets) { m_internalModel->appendBarSet(barset); connect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons))); connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged())); } emit restructuredBars(); + */ } /*! @@ -114,11 +439,15 @@ void QBarSeries::appendBarSets(QList sets) */ void QBarSeries::removeBarSets(QList sets) { + Q_D(QBarSeries); + d->removeBarSets(sets); + /* foreach (QBarSet* barset, sets) { disconnect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons))); m_internalModel->removeBarSet(barset); } emit restructuredBars(); + */ } /*! @@ -127,8 +456,12 @@ void QBarSeries::removeBarSets(QList sets) */ void QBarSeries::insertBarSet(int i, QBarSet *set) { + Q_D(QBarSeries); + d->insertBarSet(i,set); + /* m_internalModel->insertBarSet(i, set); // emit barsetChanged(); + */ } /*! @@ -138,7 +471,9 @@ void QBarSeries::insertBarSet(int i, QBarSet *set) */ void QBarSeries::insertCategory(int i, QString category) { - m_internalModel->insertCategory(i, category); + Q_D(QBarSeries); + d->insertCategory(i,category); + //m_internalModel->insertCategory(i, category); } /*! @@ -147,7 +482,9 @@ void QBarSeries::insertCategory(int i, QString category) */ void QBarSeries::removeCategory(int i) { - m_internalModel->removeCategory(i); + Q_D(QBarSeries); + d->removeCategory(i); + //m_internalModel->removeCategory(i); } /*! @@ -155,10 +492,14 @@ void QBarSeries::removeCategory(int i) */ int QBarSeries::barsetCount() const { + Q_D(const QBarSeries); + return d->barsetCount(); + /* // if(m_model) // return m_mapBarTop - m_mapBarBottom; // else return m_internalModel->barsetCount(); + */ } /*! @@ -166,7 +507,9 @@ int QBarSeries::barsetCount() const */ int QBarSeries::categoryCount() const { - return m_internalModel->categoryCount(); + Q_D(const QBarSeries); + return d->categoryCount(); +// return m_internalModel->categoryCount(); } /*! @@ -174,7 +517,9 @@ int QBarSeries::categoryCount() const */ QList QBarSeries::barSets() const { - return m_internalModel->barSets(); + Q_D(const QBarSeries); + return d->barSets(); +// return m_internalModel->barSets(); } /*! @@ -182,7 +527,9 @@ QList QBarSeries::barSets() const */ QBarSet* QBarSeries::barsetAt(int index) { - return m_internalModel->barsetAt(index); + Q_D(QBarSeries); + return d->barsetAt(index); +// return m_internalModel->barsetAt(index); } /*! @@ -190,7 +537,9 @@ QBarSet* QBarSeries::barsetAt(int index) */ QString QBarSeries::categoryName(int category) { - return m_internalModel->categoryName(category); + Q_D(QBarSeries); + return d->categoryName(category); +// return m_internalModel->categoryName(category); } /*! @@ -200,6 +549,9 @@ QString QBarSeries::categoryName(int category) */ void QBarSeries::setToolTipEnabled(bool enabled) { + Q_D(QBarSeries); + d->setToolTipEnabled(enabled); + /* // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled. if (enabled) { for (int i=0; ibarsetCount(); i++) { @@ -212,6 +564,7 @@ void QBarSeries::setToolTipEnabled(bool enabled) disconnect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString))); } } + */ } @@ -220,7 +573,9 @@ void QBarSeries::setToolTipEnabled(bool enabled) */ void QBarSeries::barsetClicked(QString category, Qt::MouseButtons button) { - emit clicked(qobject_cast(sender()), category, button); + Q_D(QBarSeries); + d->barsetClicked(category,button); +// emit clicked(qobject_cast(sender()), category, button); } /*! @@ -228,7 +583,9 @@ void QBarSeries::barsetClicked(QString category, Qt::MouseButtons button) */ qreal QBarSeries::min() { - return m_internalModel->min(); + Q_D(QBarSeries); + return d->min(); + //return m_internalModel->min(); } /*! @@ -236,7 +593,9 @@ qreal QBarSeries::min() */ qreal QBarSeries::max() { - return m_internalModel->max(); + Q_D(QBarSeries); + return d->max(); +// return m_internalModel->max(); } /*! @@ -244,7 +603,9 @@ qreal QBarSeries::max() */ qreal QBarSeries::valueAt(int set, int category) { - return m_internalModel->valueAt(set, category); + Q_D(QBarSeries); + return d->valueAt(set,category); +// return m_internalModel->valueAt(set, category); } /*! @@ -252,7 +613,9 @@ qreal QBarSeries::valueAt(int set, int category) */ qreal QBarSeries::percentageAt(int set, int category) { - return m_internalModel->percentageAt(set, category); + Q_D(QBarSeries); + return d->percentageAt(set,category); +// return m_internalModel->percentageAt(set, category); } /*! @@ -260,7 +623,9 @@ qreal QBarSeries::percentageAt(int set, int category) */ qreal QBarSeries::categorySum(int category) { - return m_internalModel->categorySum(category); + Q_D(QBarSeries); + return d->categorySum(category); +// return m_internalModel->categorySum(category); } /*! @@ -268,7 +633,9 @@ qreal QBarSeries::categorySum(int category) */ qreal QBarSeries::absoluteCategorySum(int category) { - return m_internalModel->absoluteCategorySum(category); + Q_D(QBarSeries); + return d->absoluteCategorySum(category); +// return m_internalModel->absoluteCategorySum(category); } /*! @@ -276,7 +643,9 @@ qreal QBarSeries::absoluteCategorySum(int category) */ qreal QBarSeries::maxCategorySum() { - return m_internalModel->maxCategorySum(); + Q_D(QBarSeries); + return d->maxCategorySum(); +// return m_internalModel->maxCategorySum(); } /*! @@ -284,7 +653,9 @@ qreal QBarSeries::maxCategorySum() */ BarChartModel& QBarSeries::modelInternal() { - return *m_internalModel; + Q_D(QBarSeries); + return d->modelInternal(); +// return *m_internalModel; } /*! @@ -293,6 +664,9 @@ BarChartModel& QBarSeries::modelInternal() */ bool QBarSeries::setModel(QAbstractItemModel *model) { + Q_D(QBarSeries); + return d->setModel(model); + /* // disconnect signals from old model if(m_model) { @@ -316,6 +690,7 @@ bool QBarSeries::setModel(QAbstractItemModel *model) m_model = 0; return false; } + */ } /*! @@ -326,8 +701,11 @@ bool QBarSeries::setModel(QAbstractItemModel *model) All the columns/rows inbetween those two values are also used as data for bar sets. The \a orientation paramater specifies whether the data is in columns or in rows. */ -void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation) +void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation) { + Q_D(QBarSeries); + d->setModelMapping(categories,bottomBoundary,topBoundary,orientation); +/* if (!m_model) return; @@ -383,6 +761,7 @@ void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBound appendBarSet(barSet); } } +*/ } /*! @@ -390,6 +769,9 @@ void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBound */ void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) { + Q_D(QBarSeries); + d->modelUpdated(topLeft,bottomRight); + /* Q_UNUSED(bottomRight) if (m_mapOrientation == Qt::Vertical) @@ -404,13 +786,17 @@ void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount) barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble()); } + */ } /*! \internal */ -void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/) +void QBarSeries::modelDataAdded(QModelIndex parent, int start, int end) { + Q_D(QBarSeries); + d->modelDataAdded(parent,start,end); +/* if (m_mapOrientation == Qt::Vertical) { insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1)); for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) { @@ -423,6 +809,7 @@ void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/) } } emit restructuredBars(); +*/ } /*! @@ -430,6 +817,9 @@ void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/) */ void QBarSeries::modelDataRemoved(QModelIndex parent, int start, int end) { + Q_D(QBarSeries); + d->modelDataRemoved(parent,start,end); +/* Q_UNUSED(parent) Q_UNUSED(end) @@ -439,21 +829,28 @@ void QBarSeries::modelDataRemoved(QModelIndex parent, int start, int end) barsetAt(i)->removeValue(start - m_mapFirst); } emit restructuredBars(); +*/ } void QBarSeries::barsetChanged() { - emit updatedBars(); + Q_D(QBarSeries); + d->barsetChanged(); +// emit updatedBars(); } QBarCategories QBarSeries::categories() const { + Q_D(const QBarSeries); + return d->categories(); +/* QBarCategories categories; int count = m_internalModel->categoryCount(); for (int i=1; i <= count; i++) { categories.insert(i, m_internalModel->categoryName(i - 1)); } return categories; +*/ } /*! @@ -461,12 +858,17 @@ QBarCategories QBarSeries::categories() const */ void QBarSeries::setLabelsVisible(bool visible) { + Q_D(QBarSeries); + d->setLabelsVisible(visible); +/* foreach (QBarSet* s, barSets()) { s->setLabelsVisible(visible); } +*/ } #include "moc_qbarseries.cpp" +#include "moc_qbarseriesprivate_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarseries.h b/src/barchart/qbarseries.h index d2b7fe3..020e5d2 100644 --- a/src/barchart/qbarseries.h +++ b/src/barchart/qbarseries.h @@ -33,6 +33,7 @@ typedef QStringList QBarCategories; class QBarSet; class BarChartModel; class BarCategory; +class QBarSeriesPrivate; // Container for series class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries @@ -58,7 +59,7 @@ public: void setLabelsVisible(bool visible = true); bool setModel(QAbstractItemModel *model); - void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical); + void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical); public: // TODO: Functions below this are not part of api and will be moved @@ -85,7 +86,7 @@ Q_SIGNALS: // TODO: internal signals, these to private implementation. // TODO: TO PIMPL ---> - void showToolTip(QPoint pos, QString tip); +// void showToolTip(QPoint pos, QString tip); // <--- TO PIMPL public Q_SLOTS: @@ -103,6 +104,11 @@ private Q_SLOTS: void barsetChanged(); protected: + QBarSeriesPrivate * const d_ptr; + Q_DECLARE_PRIVATE(QBarSeries) + Q_DISABLE_COPY(QBarSeries) + +/* BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good. // QAbstractItemModel* m_model; @@ -112,6 +118,7 @@ protected: int m_mapFirst; int m_mapCount; Qt::Orientation m_mapOrientation; +*/ }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarseriesprivate_p.h b/src/barchart/qbarseriesprivate_p.h new file mode 100644 index 0000000..8fe5065 --- /dev/null +++ b/src/barchart/qbarseriesprivate_p.h @@ -0,0 +1,96 @@ +#ifndef QBARSERIESPRIVATE_P_H +#define QBARSERIESPRIVATE_P_H + +#include "qbarseries.h" +#include +#include + +class QModelIndex; + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +typedef QStringList QBarCategories; + +class QBarSeries; +class QBarSet; +class BarChartModel; +class BarCategory; + +// Container for series +class QBarSeriesPrivate : public QObject +{ + Q_OBJECT + Q_DECLARE_PUBLIC(QBarSeries) +public: + QBarSeriesPrivate(QStringList categories, QBarSeries *parent); + + virtual QSeries::QSeriesType type() const { return QSeries::SeriesTypeBar; } + + void appendBarSet(QBarSet *set); // Takes ownership of set + void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set + void appendBarSets(QList sets); + void removeBarSets(QList sets); + void insertBarSet(int i, QBarSet *set); + void insertCategory(int i, QString category); + void removeCategory(int i); + int barsetCount() const; + int categoryCount() const; + QList barSets() const; + QBarCategories categories() const; + + void setLabelsVisible(bool visible = true); + + bool setModel(QAbstractItemModel *model); + void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical); + + QBarSet* barsetAt(int index); + QString categoryName(int category); + qreal min(); + qreal max(); + qreal valueAt(int set, int category); + qreal percentageAt(int set, int category); + qreal categorySum(int category); + qreal absoluteCategorySum(int category); + qreal maxCategorySum(); + BarChartModel& modelInternal(); + + static QBarSeriesPrivate &data(QBarSeries *barseries) + { + Q_ASSERT(barseries); + return *barseries->d_ptr; + } + +Q_SIGNALS: + void clicked(QBarSet *barset, QString category, Qt::MouseButtons button); // Up to user of api, what to do with these signals + void selected(); + void updatedBars(); + void restructuredBars(); + void showToolTip(QPoint pos, QString tip); + +public Q_SLOTS: + void setToolTipEnabled(bool enabled = true); // enables tooltips + void barsetClicked(QString category, Qt::MouseButtons button); + +private Q_SLOTS: + // slots for updating bars when data in model changes + void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); + void modelDataAdded(QModelIndex parent, int start, int end); + void modelDataRemoved(QModelIndex parent, int start, int end); + void barsetChanged(); + +protected: + QBarSeries * const q_ptr; + + BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good. + QAbstractItemModel* m_model; + int m_mapCategories; + int m_mapBarBottom; + int m_mapBarTop; + int m_mapFirst; + int m_mapCount; + Qt::Orientation m_mapOrientation; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QBARSERIESPRIVATE_P_H diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index 74ac9ff..972a2e3 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -19,11 +19,157 @@ ****************************************************************************/ #include "qbarset.h" -#include +#include "qbarsetprivate_p.h" +//#include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE + +QBarSetPrivate::QBarSetPrivate(QString name, QBarSet *parent) : QObject(parent), + q_ptr(parent), + m_name(name), + m_labelsVisible(false) +{ + +} + +QBarSetPrivate::~QBarSetPrivate() +{ + +} + +void QBarSetPrivate::setName(QString name) +{ + m_name = name; +} + +QString QBarSetPrivate::name() const +{ + return m_name; +} + +QBarSetPrivate& QBarSetPrivate::operator << (const qreal &value) +{ + m_values.append(value); + emit structureChanged(); + return *this; +} + +void QBarSetPrivate::insertValue(int i, qreal value) +{ + m_values.insert(i, value); +} + +void QBarSetPrivate::removeValue(int i) +{ + m_values.removeAt(i); +} + +int QBarSetPrivate::count() const +{ + return m_values.count(); +} + +qreal QBarSetPrivate::valueAt(int index) const +{ + return m_values.at(index); +} + +void QBarSetPrivate::setValue(int index, qreal value) +{ + m_values.replace(index,value); + emit valueChanged(); +} + +qreal QBarSetPrivate::sum() const +{ + qreal sum(0); + for (int i=0; i < m_values.count(); i++) { + sum += m_values.at(i); + } + return sum; +} + +void QBarSetPrivate::setPen(const QPen &pen) +{ + m_pen = pen; + emit valueChanged(); +} + +QPen QBarSetPrivate::pen() const +{ + return m_pen; +} + +void QBarSetPrivate::setBrush(const QBrush &brush) +{ + m_brush = brush; + emit valueChanged(); +} + +QBrush QBarSetPrivate::brush() const +{ + return m_brush; +} + +void QBarSetPrivate::setLabelPen(const QPen &pen) +{ + m_labelPen = pen; + emit valueChanged(); +} + +QPen QBarSetPrivate::labelPen() const +{ + return m_labelPen; +} + +void QBarSetPrivate::setLabelBrush(const QBrush &brush) +{ + m_labelBrush = brush; + emit valueChanged(); +} + +QBrush QBarSetPrivate::labelBrush() const +{ + return m_labelBrush; +} + +void QBarSetPrivate::setLabelFont(const QFont &font) +{ + m_labelFont = font; + emit valueChanged(); +} + +QFont QBarSetPrivate::labelFont() const +{ + return m_labelFont; +} + +void QBarSetPrivate::setLabelsVisible(bool visible) +{ + m_labelsVisible = visible; + emit labelsVisibleChanged(visible); +} + +bool QBarSetPrivate::labelsVisible() const +{ + return m_labelsVisible; +} + +void QBarSetPrivate::barHoverEnterEvent(QPoint pos) +{ + emit showToolTip(pos, m_name); + emit hoverEnter(pos); +} + +void QBarSetPrivate::barHoverLeaveEvent() +{ + // Emit signal to user of charts + emit hoverLeave(); +} + + /*! \class QBarSet \brief part of QtCommercial chart API. @@ -66,8 +212,9 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ QBarSet::QBarSet(QString name, QObject *parent) : QObject(parent) - ,m_name(name) - ,m_labelsVisible(false) + ,d_ptr(new QBarSetPrivate(name,this)) +// ,m_name(name) +// ,m_labelsVisible(false) { } @@ -76,7 +223,8 @@ QBarSet::QBarSet(QString name, QObject *parent) */ void QBarSet::setName(QString name) { - m_name = name; + Q_D(QBarSet); + d->setName(name); } /*! @@ -84,7 +232,8 @@ void QBarSet::setName(QString name) */ QString QBarSet::name() const { - return m_name; + Q_D(const QBarSet); + return d->name(); } /*! @@ -92,9 +241,13 @@ QString QBarSet::name() const */ QBarSet& QBarSet::operator << (const qreal &value) { - m_values.append(value); - emit structureChanged(); + Q_D(QBarSet); + d->operator <<(value); return *this; + +// m_values.append(value); +// emit structureChanged(); +// return *this; } /*! @@ -104,7 +257,9 @@ QBarSet& QBarSet::operator << (const qreal &value) */ void QBarSet::insertValue(int i, qreal value) { - m_values.insert(i, value); + Q_D(QBarSet); + d->insertValue(i,value); +// m_values.insert(i, value); } /*! @@ -113,7 +268,9 @@ void QBarSet::insertValue(int i, qreal value) */ void QBarSet::removeValue(int i) { - m_values.removeAt(i); + Q_D(QBarSet); + d->removeValue(i); +// m_values.removeAt(i); } /*! @@ -121,7 +278,9 @@ void QBarSet::removeValue(int i) */ int QBarSet::count() const { - return m_values.count(); + Q_D(const QBarSet); + return d->count(); +// return m_values.count(); } /*! @@ -129,7 +288,9 @@ int QBarSet::count() const */ qreal QBarSet::valueAt(int index) const { - return m_values.at(index); + Q_D(const QBarSet); + return d->valueAt(index); +// return m_values.at(index); } /*! @@ -137,20 +298,26 @@ qreal QBarSet::valueAt(int index) const */ void QBarSet::setValue(int index, qreal value) { - m_values.replace(index,value); - emit valueChanged(); + Q_D(QBarSet); + d->setValue(index,value); +// m_values.replace(index,value); +// emit valueChanged(); } /*! - Returns total sum of all values in barset. + Returns sum of all values in barset. */ -qreal QBarSet::total() const +qreal QBarSet::sum() const { + Q_D(const QBarSet); + return d->sum(); +/* qreal total(0); for (int i=0; i < m_values.count(); i++) { total += m_values.at(i); } return total; +*/ } /*! @@ -158,8 +325,10 @@ qreal QBarSet::total() const */ void QBarSet::setPen(const QPen &pen) { - m_pen = pen; - emit valueChanged(); + Q_D(QBarSet); + d->setPen(pen); +// m_pen = pen; +// emit valueChanged(); } /*! @@ -167,7 +336,9 @@ void QBarSet::setPen(const QPen &pen) */ QPen QBarSet::pen() const { - return m_pen; + Q_D(const QBarSet); + return d->pen(); +// return m_pen; } /*! @@ -175,8 +346,10 @@ QPen QBarSet::pen() const */ void QBarSet::setBrush(const QBrush &brush) { - m_brush = brush; - emit valueChanged(); + Q_D(QBarSet); + d->setBrush(brush); +// m_brush = brush; +// emit valueChanged(); } /*! @@ -184,7 +357,9 @@ void QBarSet::setBrush(const QBrush &brush) */ QBrush QBarSet::brush() const { - return m_brush; + Q_D(const QBarSet); + return d->brush(); +// return m_brush; } /*! @@ -192,8 +367,10 @@ QBrush QBarSet::brush() const */ void QBarSet::setLabelPen(const QPen &pen) { - m_labelPen = pen; - emit valueChanged(); + Q_D(QBarSet); + d->setLabelPen(pen); +// m_labelPen = pen; +// emit valueChanged(); } /*! @@ -201,7 +378,9 @@ void QBarSet::setLabelPen(const QPen &pen) */ QPen QBarSet::labelPen() const { - return m_labelPen; + Q_D(const QBarSet); + return d->labelPen(); +// return m_labelPen; } /*! @@ -209,8 +388,10 @@ QPen QBarSet::labelPen() const */ void QBarSet::setLabelBrush(const QBrush &brush) { - m_labelBrush = brush; - emit valueChanged(); + Q_D(QBarSet); + d->setLabelBrush(brush); +// m_labelBrush = brush; +// emit valueChanged(); } /*! @@ -218,7 +399,9 @@ void QBarSet::setLabelBrush(const QBrush &brush) */ QBrush QBarSet::labelBrush() const { - return m_labelBrush; + Q_D(const QBarSet); + return d->labelBrush(); +// return m_labelBrush; } /*! @@ -226,8 +409,10 @@ QBrush QBarSet::labelBrush() const */ void QBarSet::setLabelFont(const QFont &font) { - m_labelFont = font; - emit valueChanged(); + Q_D(QBarSet); + d->setLabelFont(font); +// m_labelFont = font; +// emit valueChanged(); } /*! @@ -235,7 +420,9 @@ void QBarSet::setLabelFont(const QFont &font) */ QFont QBarSet::labelFont() const { - return m_labelFont; + Q_D(const QBarSet); + return d->labelFont(); +// return m_labelFont; } /*! @@ -244,8 +431,10 @@ QFont QBarSet::labelFont() const void QBarSet::setLabelsVisible(bool visible) { - m_labelsVisible = visible; - emit labelsVisibleChanged(visible); + Q_D(QBarSet); + d->setLabelsVisible(visible); +// m_labelsVisible = visible; +// emit labelsVisibleChanged(visible); } /*! @@ -253,27 +442,26 @@ void QBarSet::setLabelsVisible(bool visible) */ bool QBarSet::labelsVisible() const { - return m_labelsVisible; + Q_D(const QBarSet); + return d->labelsVisible(); +// return m_labelsVisible; } -/*! - \internal \a pos -*/ +/* void QBarSet::barHoverEnterEvent(QPoint pos) { emit showToolTip(pos, m_name); emit hoverEnter(pos); } - -/*! - \internal */ +/* void QBarSet::barHoverLeaveEvent() { // Emit signal to user of charts emit hoverLeave(); } - +*/ #include "moc_qbarset.cpp" +#include "moc_qbarsetprivate_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 15dfae3..f0df1ca 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -27,6 +27,7 @@ #include QTCOMMERCIALCHART_BEGIN_NAMESPACE +class QBarSetPrivate; class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject { @@ -39,16 +40,10 @@ public: QBarSet& operator << (const qreal &value); // appends new value to set void insertValue(int i, qreal value); void removeValue(int i); - - // TODO: remove indices eventually. Use as internal? int count() const; // count of values in set - qreal valueAt(int index) const; // for modifying individual values + qreal valueAt(int index) const; // for modifying individual values void setValue(int index, qreal value); // setter for individual value - qreal total() const; // total values in the set - - // TODO: - //qreal value(QString category); - //void setValue(QString category, qreal value); + qreal sum() const; // sum of all values in the set void setPen(const QPen &pen); QPen pen() const; @@ -72,23 +67,26 @@ Q_SIGNALS: void clicked(QString category, Qt::MouseButtons button); // Clicked and hover signals exposed to user // TODO: TO PIMPL ---> - void structureChanged(); - void valueChanged(); - void hoverEnter(QPoint pos); - void hoverLeave(); - void showToolTip(QPoint pos, QString tip); // Private signal - void labelsVisibleChanged(bool visible); +// void structureChanged(); +// void valueChanged(); +// void hoverEnter(QPoint pos); +// void hoverLeave(); +// void showToolTip(QPoint pos, QString tip); // Private signal +// void labelsVisibleChanged(bool visible); // <--- TO PIMPL public Q_SLOTS: // These are for internal communication // TODO: TO PIMPL ---> - void barHoverEnterEvent(QPoint pos); - void barHoverLeaveEvent(); +// void barHoverEnterEvent(QPoint pos); +// void barHoverLeaveEvent(); // <--- TO PIMPL private: - + QBarSetPrivate * const d_ptr; + Q_DECLARE_PRIVATE(QBarSet) + Q_DISABLE_COPY(QBarSet) +/* QString m_name; QList m_values; // TODO: replace with map (category, value) QMap m_mappedValues; @@ -98,6 +96,7 @@ private: QBrush m_labelBrush; QFont m_labelFont; bool m_labelsVisible; +*/ }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarsetprivate_p.h b/src/barchart/qbarsetprivate_p.h new file mode 100644 index 0000000..2819337 --- /dev/null +++ b/src/barchart/qbarsetprivate_p.h @@ -0,0 +1,80 @@ +#ifndef QBARSETPRIVATE_P_H +#define QBARSETPRIVATE_P_H + +#include "qbarset.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QBarSetPrivate : public QObject +{ + Q_OBJECT + Q_DECLARE_PUBLIC(QBarSet) + +public: + QBarSetPrivate(QString name, QBarSet *parent); + ~QBarSetPrivate(); + + void setName(QString name); + QString name() const; + QBarSetPrivate& operator << (const qreal &value); // appends new value to set + void insertValue(int i, qreal value); + void removeValue(int i); + int count() const; + qreal valueAt(int index) const; + void setValue(int index, qreal value); + qreal sum() const; + + void setPen(const QPen &pen); + QPen pen() const; + + void setBrush(const QBrush &brush); + QBrush brush() const; + + void setLabelPen(const QPen &pen); + QPen labelPen() const; + + void setLabelBrush(const QBrush &brush); + QBrush labelBrush() const; + + void setLabelFont(const QFont &font); + QFont labelFont() const; + + void setLabelsVisible(bool visible = true); + bool labelsVisible() const; + + static QBarSetPrivate &pimpl(QBarSet *barset) + { + Q_ASSERT(barset); + return *barset->d_ptr; + } + +Q_SIGNALS: + void clicked(QString category, Qt::MouseButtons button); + void structureChanged(); + void valueChanged(); + void hoverEnter(QPoint pos); + void hoverLeave(); + void showToolTip(QPoint pos, QString tip); + void labelsVisibleChanged(bool visible); + +public Q_SLOTS: + void barHoverEnterEvent(QPoint pos); + void barHoverLeaveEvent(); + +public: + QBarSet * const q_ptr; + + QString m_name; + QList m_values; // TODO: replace with map (category, value) + QMap m_mappedValues; + QPen m_pen; + QBrush m_brush; + QPen m_labelPen; + QBrush m_labelBrush; + QFont m_labelFont; + bool m_labelsVisible; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QBARSETPRIVATE_P_H