From 95a77193fc29d78e9d53d557354b4f850c7a6724 2012-04-10 13:09:53 From: sauimone Date: 2012-04-10 13:09:53 Subject: [PATCH] barchart PIMPL part 2. Cleaning up leftovers in public api --- diff --git a/src/barchart/barchartitem.cpp b/src/barchart/barchartitem.cpp index cf7e6f2..e5fb50b 100644 --- a/src/barchart/barchartitem.cpp +++ b/src/barchart/barchartitem.cpp @@ -22,7 +22,9 @@ #include "bar_p.h" #include "barlabel_p.h" #include "qbarset.h" +#include "qbarset_p.h" #include "qbarseries.h" +#include "qbarseries_p.h" #include "qchart.h" #include "qchartaxis.h" #include "qchartaxiscategories.h" @@ -40,9 +42,9 @@ BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : m_series(series) { setFlag(ItemClipsChildrenToShape); - connect(series, SIGNAL(showToolTip(QPoint,QString)), this, SLOT(showToolTip(QPoint,QString))); - connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); - connect(series, SIGNAL(restructuredBars()), this, SLOT(handleModelChanged())); + connect(series->d_func(), SIGNAL(showToolTip(QPoint,QString)), this, SLOT(showToolTip(QPoint,QString))); + connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); + connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged())); setZValue(ChartPresenter::BarSeriesZValue); dataChanged(); } @@ -77,14 +79,14 @@ void BarChartItem::dataChanged() // Create new graphic items for bars for (int c = 0; c < m_series->categoryCount(); c++) { - QString category = m_series->categoryName(c); + QString category = m_series->d_func()->categoryName(c); for (int s = 0; s < m_series->barsetCount(); s++) { - QBarSet *set = m_series->barsetAt(s); + QBarSet *set = m_series->d_func()->barsetAt(s); Bar *bar = new Bar(category,this); m_bars.append(bar); connect(bar, SIGNAL(clicked(QString,Qt::MouseButtons)), set, SIGNAL(clicked(QString,Qt::MouseButtons))); - connect(bar, SIGNAL(hoverEntered(QPoint)), set, SLOT(barHoverEnterEvent(QPoint))); - connect(bar, SIGNAL(hoverLeaved()), set, SLOT(barHoverLeaveEvent())); + connect(bar, SIGNAL(hoverEntered(QPoint)), set->d_ptr.data(), SLOT(barHoverEnterEvent(QPoint))); + connect(bar, SIGNAL(hoverLeaved()), set->d_ptr.data(), SLOT(barHoverLeaveEvent())); m_layout.append(QRectF(0, 0, 0, 0)); } } @@ -92,7 +94,7 @@ void BarChartItem::dataChanged() // Create labels for (int category = 0; category < m_series->categoryCount(); category++) { for (int s = 0; s < m_series->barsetCount(); s++) { - QBarSet *set = m_series->barsetAt(s); + QBarSet *set = m_series->d_func()->barsetAt(s); BarLabel *value = new BarLabel(*set, this); m_labels.append(value); connect(set,SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool))); @@ -121,7 +123,7 @@ QVector BarChartItem::calculateLayout() qreal xPos = categoryWidth * category + barWidth / 2; qreal yPos = height + scale * m_domainMinY; for (int set = 0; set < setCount; set++) { - QBarSet* barSet = m_series->barsetAt(set); + QBarSet* barSet = m_series->d_func()->barsetAt(set); qreal barHeight = barSet->valueAt(category) * scale; Bar* bar = m_bars.at(itemIndex); diff --git a/src/barchart/percentbarchartitem.cpp b/src/barchart/percentbarchartitem.cpp index ad764ec..9f30272 100644 --- a/src/barchart/percentbarchartitem.cpp +++ b/src/barchart/percentbarchartitem.cpp @@ -21,6 +21,7 @@ #include "percentbarchartitem_p.h" #include "bar_p.h" #include "barlabel_p.h" +#include "qbarseries_p.h" #include "qbarset.h" #include @@ -49,11 +50,11 @@ QVector PercentBarChartItem::calculateLayout() int itemIndex(0); for (int category = 0; category < categoryCount; category++) { - qreal colSum = m_series->categorySum(category); + qreal colSum = m_series->d_func()->categorySum(category); qreal percentage = (100 / colSum); qreal yPos = height + domainScale * m_domainMinY; for (int set=0; set < m_series->barsetCount(); set++) { - QBarSet* barSet = m_series->barsetAt(set); + QBarSet* barSet = m_series->d_func()->barsetAt(set); qreal barHeight = barSet->valueAt(category) * percentage * domainScale; Bar* bar = m_bars.at(itemIndex); bar->setPen(barSet->pen()); @@ -63,8 +64,8 @@ QVector PercentBarChartItem::calculateLayout() BarLabel* label = m_labels.at(itemIndex); - if (!qFuzzyIsNull(m_series->valueAt(set,category))) { - int p = m_series->percentageAt(set,category) * 100; + if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) { + int p = m_series->d_func()->percentageAt(set,category) * 100; QString vString(QString::number(p)); vString.truncate(3); vString.append("%"); diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index b30cfb8..9797ead 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -176,12 +176,6 @@ void QBarSeries::removeCategory(int i) int QBarSeries::barsetCount() const { Q_D(const QBarSeries); - /* - // if(m_model) - // return m_mapBarTop - m_mapBarBottom; - // else - - */ return d->m_internalModel->barsetCount(); } @@ -204,26 +198,6 @@ QList QBarSeries::barSets() const } /*! - \internal \a index -*/ -QBarSet* QBarSeries::barsetAt(int index) -{ - Q_D(QBarSeries); - return d->barsetAt(index); -// return m_internalModel->barsetAt(index); -} - -/*! - \internal \a category -*/ -QString QBarSeries::categoryName(int category) -{ - Q_D(QBarSeries); - return d->categoryName(category); -// return m_internalModel->categoryName(category); -} - -/*! Enables or disables tooltip depending on parameter \a enabled. Tooltip shows the name of set, when mouse is hovering on top of bar. Calling without parameter \a enabled, enables the tooltip @@ -232,111 +206,6 @@ 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++) { - 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))); - } - } - */ -} - - -/*! - \internal \a category -*/ -void QBarSeries::barsetClicked(QString category, Qt::MouseButtons button) -{ - Q_D(QBarSeries); - d->barsetClicked(category,button); -// emit clicked(qobject_cast(sender()), category, button); -} - -/*! - \internal -*/ -qreal QBarSeries::min() -{ - Q_D(QBarSeries); - return d->min(); - //return m_internalModel->min(); -} - -/*! - \internal -*/ -qreal QBarSeries::max() -{ - Q_D(QBarSeries); - return d->max(); -// return m_internalModel->max(); -} - -/*! - \internal \a set \a category -*/ -qreal QBarSeries::valueAt(int set, int category) -{ - Q_D(QBarSeries); - return d->valueAt(set,category); -// return m_internalModel->valueAt(set, category); -} - -/*! - \internal \a set \a category -*/ -qreal QBarSeries::percentageAt(int set, int category) -{ - Q_D(QBarSeries); - return d->percentageAt(set,category); -// return m_internalModel->percentageAt(set, category); -} - -/*! - \internal \a category -*/ -qreal QBarSeries::categorySum(int category) -{ - Q_D(QBarSeries); - return d->categorySum(category); -// return m_internalModel->categorySum(category); -} - -/*! - \internal \a category -*/ -qreal QBarSeries::absoluteCategorySum(int category) -{ - Q_D(QBarSeries); - return d->absoluteCategorySum(category); -// return m_internalModel->absoluteCategorySum(category); -} - -/*! - \internal -*/ -qreal QBarSeries::maxCategorySum() -{ - Q_D(QBarSeries); - return d->maxCategorySum(); -// return m_internalModel->maxCategorySum(); -} - -/*! - \internal -*/ -BarChartModel& QBarSeries::modelInternal() -{ - Q_D(QBarSeries); - return d->modelInternal(); -// return *m_internalModel; } /*! @@ -347,31 +216,6 @@ bool QBarSeries::setModel(QAbstractItemModel *model) { Q_D(QBarSeries); return d->setModel(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; - } - */ } /*! @@ -386,138 +230,6 @@ void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoun { Q_D(QBarSeries); d->setModelMapping(categories,bottomBoundary,topBoundary,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); - } - } -*/ -} - -/*! - \internal -*/ -void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) -{ - Q_D(QBarSeries); - d->modelUpdated(topLeft,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()); - } - */ -} - -/*! - \internal -*/ -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++) { - 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(); -*/ -} - -/*! - \internal -*/ -void QBarSeries::modelDataRemoved(QModelIndex parent, int start, int end) -{ - Q_D(QBarSeries); - d->modelDataRemoved(parent,start,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 QBarSeries::barsetChanged() -{ - Q_D(QBarSeries); - d->barsetChanged(); -// emit updatedBars(); } QBarCategories QBarSeries::categories() const @@ -773,7 +485,6 @@ void QBarSeriesPrivate::barsetChanged() void QBarSeriesPrivate::scaleDomain(Domain& domain) { - Q_Q(QBarSeries); qreal minX(domain.minX()); qreal minY(domain.minY()); qreal maxX(domain.maxX()); @@ -781,8 +492,8 @@ void QBarSeriesPrivate::scaleDomain(Domain& domain) int tickXCount(domain.tickXCount()); int tickYCount(domain.tickYCount()); - qreal x = q->categoryCount(); - qreal y = q->max(); + qreal x = m_internalModel->categoryCount(); + qreal y = max(); minX = qMin(minX, x); minY = qMin(minY, y); maxX = qMax(maxX, x); diff --git a/src/barchart/qbarseries.h b/src/barchart/qbarseries.h index a003596..eb4dda1 100644 --- a/src/barchart/qbarseries.h +++ b/src/barchart/qbarseries.h @@ -61,65 +61,21 @@ public: bool setModel(QAbstractItemModel *model); 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 - // to private implementation, when we start using it - // TODO: TO PIMPL ---> - 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(); - // <--- TO PIMPL - protected: explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0); Q_SIGNALS: - void clicked(QBarSet *barset, QString category, Qt::MouseButtons button); // Up to user of api, what to do with these signals + void clicked(QBarSet *barset, QString category, Qt::MouseButtons button); void selected(); - // - void updatedBars(); - void restructuredBars(); - - // TODO: internal signals, these to private implementation. - // TODO: TO PIMPL ---> -// void showToolTip(QPoint pos, QString tip); - // <--- TO PIMPL public Q_SLOTS: void setToolTipEnabled(bool enabled = true); // enables tooltips - // TODO: TO PIMPL ---> - void barsetClicked(QString category, Qt::MouseButtons button); - // <--- TO PIMPL - -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: Q_DECLARE_PRIVATE(QBarSeries) - -/* - 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; -*/ + friend class BarChartItem; + friend class PercentBarChartItem; + friend class StackedBarChartItem; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index a4b0c5e..d10100c 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -73,6 +73,7 @@ private: Q_DISABLE_COPY(QBarSet) friend class QBarSeries; friend class BarLegendMarker; + friend class BarChartItem; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qstackedbarseries.cpp b/src/barchart/qstackedbarseries.cpp index 88edbfe..dc51787 100644 --- a/src/barchart/qstackedbarseries.cpp +++ b/src/barchart/qstackedbarseries.cpp @@ -21,6 +21,7 @@ #include "qstackedbarseries.h" #include "qstackedbarseries_p.h" #include "stackedbarchartitem_p.h" +#include "barchartmodel_p.h" #include "chartdataset_p.h" #include "charttheme_p.h" #include "chartanimator_p.h" @@ -69,7 +70,6 @@ QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(QBarCategories categories, QS void QStackedBarSeriesPrivate::scaleDomain(Domain& domain) { - Q_Q(QStackedBarSeries); qreal minX(domain.minX()); qreal minY(domain.minY()); qreal maxX(domain.maxX()); @@ -77,8 +77,8 @@ void QStackedBarSeriesPrivate::scaleDomain(Domain& domain) int tickXCount(domain.tickXCount()); int tickYCount(domain.tickYCount()); - qreal x = q->categoryCount(); - qreal y = q->maxCategorySum(); + qreal x = m_internalModel->categoryCount(); + qreal y = maxCategorySum(); minX = qMin(minX, x); minY = qMin(minY, y); maxX = qMax(maxX, x); diff --git a/src/barchart/stackedbarchartitem.cpp b/src/barchart/stackedbarchartitem.cpp index 2a11cde..4389f3a 100644 --- a/src/barchart/stackedbarchartitem.cpp +++ b/src/barchart/stackedbarchartitem.cpp @@ -21,6 +21,8 @@ #include "stackedbarchartitem_p.h" #include "bar_p.h" #include "barlabel_p.h" +#include "qbarset_p.h" +#include "qbarseries_p.h" #include "qbarset.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -49,7 +51,7 @@ QVector StackedBarChartItem::calculateLayout() for (int category = 0; category < categotyCount; category++) { qreal yPos = height + scale * m_domainMinY; for (int set=0; set < m_series->barsetCount(); set++) { - QBarSet* barSet = m_series->barsetAt(set); + QBarSet* barSet = m_series->d_func()->barsetAt(set); qreal barHeight = barSet->valueAt(category) * scale; Bar* bar = m_bars.at(itemIndex); diff --git a/test/tablemodelchart/tablewidget.cpp b/test/tablemodelchart/tablewidget.cpp index 7675532..6a4db6e 100644 --- a/test/tablemodelchart/tablewidget.cpp +++ b/test/tablemodelchart/tablewidget.cpp @@ -314,8 +314,9 @@ void TableWidget::updateChartType(bool toggle) barSeries->setModelMapping(5, 2, 4, Qt::Vertical); barSeries->setToolTipEnabled(true); m_chart->addSeries(barSeries); - for (int i = 0; i < barSeries->barsetCount(); i++) { - seriesColorHex = "#" + QString::number(barSeries->barsetAt(i)->brush().color().rgb(), 16).right(6).toUpper(); + QList barsets = barSeries->barSets(); + for (int i = 0; i < barsets.count(); i++) { + seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper(); m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000)); } }