From a6e87336a4362457b4c6ac4b4316be8025896000 2012-04-12 10:15:45 From: sauimone Date: 2012-04-12 10:15:45 Subject: [PATCH] barchart: removed old model --- diff --git a/examples/stackedbarchartdrilldown/drilldownchart.cpp b/examples/stackedbarchartdrilldown/drilldownchart.cpp index 90e75d3..87ce145 100644 --- a/examples/stackedbarchartdrilldown/drilldownchart.cpp +++ b/examples/stackedbarchartdrilldown/drilldownchart.cpp @@ -31,8 +31,9 @@ DrilldownChart::DrilldownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) void DrilldownChart::changeSeries(QAbstractSeries *series) { - if (m_currentSeries) + if (m_currentSeries) { removeSeries(m_currentSeries); + } m_currentSeries = series; addSeries(series); setTitle(series->name()); diff --git a/src/barchart/barchart.pri b/src/barchart/barchart.pri index fed2ba2..34619ad 100644 --- a/src/barchart/barchart.pri +++ b/src/barchart/barchart.pri @@ -3,7 +3,6 @@ DEPENDPATH += $$PWD SOURCES += \ $$PWD/bar.cpp \ - $$PWD/barchartmodel.cpp \ $$PWD/barchartitem.cpp \ $$PWD/percentbarchartitem.cpp \ $$PWD/qbarseries.cpp \ @@ -15,7 +14,6 @@ SOURCES += \ PRIVATE_HEADERS += \ $$PWD/bar_p.h \ - $$PWD/barchartmodel_p.h \ $$PWD/barchartitem_p.h \ $$PWD/percentbarchartitem_p.h \ $$PWD/stackedbarchartitem_p.h \ diff --git a/src/barchart/barchartitem_p.h b/src/barchart/barchartitem_p.h index d720b3b..755c0dc 100644 --- a/src/barchart/barchartitem_p.h +++ b/src/barchart/barchartitem_p.h @@ -47,7 +47,6 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QRectF boundingRect() const; - // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes virtual QVector calculateLayout(); @@ -65,7 +64,6 @@ public Q_SLOTS: protected: - // TODO: consider these. qreal m_domainMinX; qreal m_domainMaxX; qreal m_domainMinY; diff --git a/src/barchart/barchartmodel.cpp b/src/barchart/barchartmodel.cpp deleted file mode 100644 index 538c679..0000000 --- a/src/barchart/barchartmodel.cpp +++ /dev/null @@ -1,198 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Digia Plc -** All rights reserved. -** For any questions to Digia, please use contact form at http://qt.digia.com -** -** This file is part of the Qt Commercial Charts Add-on. -** -** $QT_BEGIN_LICENSE$ -** Licensees holding valid Qt Commercial licenses may use this file in -** accordance with the Qt Commercial License Agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. -** -** If you have questions regarding the use of this file, please use -** contact form at http://qt.digia.com -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "barchartmodel_p.h" -#include "qbarset.h" -#include -#include -#include - -QTCOMMERCIALCHART_BEGIN_NAMESPACE - -BarChartModel::BarChartModel(QStringList categories, QObject *parent) : QObject(parent), - m_category(categories) -{ -} - -QStringList BarChartModel::category() -{ - return m_category; -} - -void BarChartModel::appendBarSet(QBarSet *set) -{ - m_dataModel.append(set); -} - -void BarChartModel::removeBarSet(QBarSet *set) -{ - if (m_dataModel.contains(set)) { - m_dataModel.removeOne(set); - } -} - -void BarChartModel::insertBarSet(int i, QBarSet *set) -{ - m_dataModel.insert(i, set); -} - -void BarChartModel::insertCategory(int i, QString category) -{ - m_category.insert(i, category); -} - -void BarChartModel::removeCategory(int i) -{ - m_category.removeAt(i); -} - -QBarSet* BarChartModel::barsetAt(int index) const -{ - return m_dataModel.at(index); -} - -QList BarChartModel::barSets() const -{ - return m_dataModel; -} - -int BarChartModel::barsetCount() const -{ - return m_dataModel.count(); -} - -int BarChartModel::categoryCount() const -{ - return m_category.count(); -} - -qreal BarChartModel::min() const -{ - Q_ASSERT(m_dataModel.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. - qreal min = INT_MAX; - - for (int i = 0; i < m_dataModel.count(); i++) { - int itemCount = m_dataModel.at(i)->count(); - for (int j = 0; j < itemCount; j++) { - qreal temp = m_dataModel.at(i)->at(j); - if (temp < min) - min = temp; - } - } - return min; -} - -qreal BarChartModel::max() const -{ - if (m_dataModel.count() == 0) return 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. - qreal max = INT_MIN; - - for (int i = 0; i < m_dataModel.count(); i++) { - int itemCount = m_dataModel.at(i)->count(); - for (int j = 0; j < itemCount; j++) { - qreal temp = m_dataModel.at(i)->at(j); - if (temp > max) - max = temp; - } - } - - return max; -} - -qreal BarChartModel::valueAt(int set, int category) const -{ - if ((set < 0) || (set >= m_dataModel.count())) { - // No set, no value. - return 0; - } else if ((category < 0) || (category >= m_dataModel.at(set)->count())) { - // No category, no value. - return 0; - } - - return m_dataModel.at(set)->at(category); -} - -qreal BarChartModel::percentageAt(int set, int category) const -{ - if ((set < 0) || (set >= m_dataModel.count())) { - // No set, no value. - return 0; - } else if ((category < 0) || (category >= m_dataModel.at(set)->count())) { - // No category, no value. - return 0; - } - - qreal value = m_dataModel.at(set)->at(category); - qreal total = categorySum(category); - if ( qFuzzyCompare(total, 0) ) - return 0; - - return value / total; -} - -qreal BarChartModel::categorySum(int category) const -{ - qreal sum(0); - int count = m_dataModel.count(); // Count sets - - for (int set = 0; set < count; set++) { - if (category < m_dataModel.at(set)->count()) - sum += m_dataModel.at(set)->at(category); - } - return sum; -} - -qreal BarChartModel::absoluteCategorySum(int category) const -{ - qreal sum(0); - int count = m_dataModel.count(); // Count sets - - for (int set = 0; set < count; set++) { - if (category < m_dataModel.at(set)->count()) - sum += qAbs(m_dataModel.at(set)->at(category)); - } - return sum; -} - -qreal BarChartModel::maxCategorySum() const -{ - qreal max = INT_MIN; - int count = categoryCount(); - - for (int col = 0; col < count; col++) { - qreal sum = categorySum(col); - if (sum > max) - max = sum; - } - return max; -} - -QString BarChartModel::categoryName(int category) -{ - return m_category.at(category); -} - -#include "moc_barchartmodel_p.cpp" - -QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barchartmodel_p.h b/src/barchart/barchartmodel_p.h deleted file mode 100644 index 74454dc..0000000 --- a/src/barchart/barchartmodel_p.h +++ /dev/null @@ -1,78 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Digia Plc -** All rights reserved. -** For any questions to Digia, please use contact form at http://qt.digia.com -** -** This file is part of the Qt Commercial Charts Add-on. -** -** $QT_BEGIN_LICENSE$ -** Licensees holding valid Qt Commercial licenses may use this file in -** accordance with the Qt Commercial License Agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. -** -** If you have questions regarding the use of this file, please use -** contact form at http://qt.digia.com -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef BARCHARTMODEL_H -#define BARCHARTMODEL_H - -#include -#include -#include "qchartglobal.h" -#include - -QTCOMMERCIALCHART_BEGIN_NAMESPACE - -// Model for bar chart. Internal class. -// TODO: Implement as QAbstractItemModel? - -class QBarSet; - -class BarChartModel : public QObject //, public QAbstractItemModel -{ - Q_OBJECT -public: - explicit BarChartModel(QStringList categories, QObject *parent = 0); - - QStringList category(); - void appendBarSet(QBarSet *set); - void removeBarSet(QBarSet *set); - void insertBarSet(int i, QBarSet *set); - void insertCategory(int i, QString category); - void removeCategory(int i); - QBarSet *barsetAt(int index) const; - QList barSets() const; - - int barsetCount() const; // Number of sets in model - int categoryCount() const; // Number of categories - - qreal max() const; // Maximum value of all sets - qreal min() const; // Minimum value of all sets - qreal valueAt(int set, int category) const; - qreal percentageAt(int set, int category) const; - - qreal categorySum(int category) const; - qreal absoluteCategorySum(int category) const; - qreal maxCategorySum() const; // returns maximum sum of sets in all categories. - - QString categoryName(int category); - -Q_SIGNALS: - void modelUpdated(); - -public Q_SLOTS: - -private: - - QList m_dataModel; - QStringList m_category; -}; - -QTCOMMERCIALCHART_END_NAMESPACE - -#endif // BARCHARTMODEL_H diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index 8a5f637..bc5886a 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -22,7 +22,6 @@ #include "qbarseries_p.h" #include "qbarset.h" #include "qbarset_p.h" -#include "barchartmodel_p.h" #include "domain_p.h" #include "legendmarker_p.h" #include "chartdataset_p.h" @@ -112,7 +111,7 @@ QAbstractSeries::QSeriesType QBarSeries::type() const void QBarSeries::appendBarSet(QBarSet *set) { Q_D(QBarSeries); - d->m_internalModel->appendBarSet(set); + d->m_barSets.append(set); QObject::connect(set->d_ptr.data(), SIGNAL(valueChanged()), d, SLOT(barsetChanged())); emit d->restructuredBars(); } @@ -125,8 +124,10 @@ void QBarSeries::appendBarSet(QBarSet *set) void QBarSeries::removeBarSet(QBarSet *set) { Q_D(QBarSeries); - d->m_internalModel->removeBarSet(set); - emit d->restructuredBars(); + if (d->m_barSets.contains(set)) { + d->m_barSets.removeOne(set); + emit d->restructuredBars(); + } } /*! @@ -138,11 +139,10 @@ void QBarSeries::appendBarSets(QList sets) { Q_D(QBarSeries); foreach (QBarSet* barset, sets) { - d->m_internalModel->appendBarSet(barset); + d->m_barSets.append(barset); QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged())); } emit d->restructuredBars(); - } /*! @@ -155,7 +155,9 @@ void QBarSeries::removeBarSets(QList sets) Q_D(QBarSeries); foreach (QBarSet* barset, sets) { - d->m_internalModel->removeBarSet(barset); + if (d->m_barSets.contains(barset)) { + d->m_barSets.removeOne(barset); + } } emit d->restructuredBars(); } @@ -167,7 +169,7 @@ void QBarSeries::removeBarSets(QList sets) void QBarSeries::insertBarSet(int i, QBarSet *set) { Q_D(QBarSeries); - d->m_internalModel->insertBarSet(i, set); + d->m_barSets.insert(i,set); emit d->barsetChanged(); } @@ -177,7 +179,7 @@ void QBarSeries::insertBarSet(int i, QBarSet *set) int QBarSeries::barsetCount() const { Q_D(const QBarSeries); - return d->m_internalModel->barsetCount(); + return d->m_barSets.count(); } /*! @@ -186,7 +188,7 @@ int QBarSeries::barsetCount() const int QBarSeries::categoryCount() const { Q_D(const QBarSeries); - return d->m_internalModel->categoryCount(); + return d->m_categories.count(); } /*! @@ -195,7 +197,7 @@ int QBarSeries::categoryCount() const QList QBarSeries::barSets() const { Q_D(const QBarSeries); - return d->m_internalModel->barSets(); + return d->m_barSets; } /*! @@ -228,14 +230,7 @@ void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoun QBarCategories QBarSeries::categories() const { Q_D(const QBarSeries); - - QBarCategories categories; - int count = d->m_internalModel->categoryCount(); - for (int i=1; i <= count; i++) { - categories.insert(i, d->m_internalModel->categoryName(i - 1)); - } - return categories; - + return d->m_categories; } /*! @@ -252,7 +247,7 @@ void QBarSeries::setLabelsVisible(bool visible) QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) : QAbstractSeriesPrivate(q), - m_internalModel(new BarChartModel(categories,this)), + m_categories(categories), m_model(0), m_mapCategories(-1), m_mapBarBottom(-1), @@ -263,52 +258,115 @@ QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) : QBarSet* QBarSeriesPrivate::barsetAt(int index) { - return m_internalModel->barsetAt(index); + return m_barSets.at(index); } QString QBarSeriesPrivate::categoryName(int category) { - return m_internalModel->categoryName(category); + return m_categories.at(category); } qreal QBarSeriesPrivate::min() { - return m_internalModel->min(); + if (m_barSets.count() <= 0) { + return 0; + } + qreal min = INT_MAX; + + for (int i = 0; i < m_barSets.count(); i++) { + int categoryCount = m_barSets.at(i)->count(); + for (int j = 0; j < categoryCount; j++) { + qreal temp = m_barSets.at(i)->at(j); + if (temp < min) + min = temp; + } + } + return min; } qreal QBarSeriesPrivate::max() { - return m_internalModel->max(); + if (m_barSets.count() <= 0) { + return 0; + } + qreal max = INT_MIN; + + for (int i = 0; i < m_barSets.count(); i++) { + int categoryCount = m_barSets.at(i)->count(); + for (int j = 0; j < categoryCount; j++) { + qreal temp = m_barSets.at(i)->at(j); + if (temp > max) + max = temp; + } + } + + return max; } qreal QBarSeriesPrivate::valueAt(int set, int category) { - return m_internalModel->valueAt(set, category); + if ((set < 0) || (set >= m_barSets.count())) { + // No set, no value. + return 0; + } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { + // No category, no value. + return 0; + } + + return m_barSets.at(set)->at(category); } qreal QBarSeriesPrivate::percentageAt(int set, int category) { - return m_internalModel->percentageAt(set, category); + if ((set < 0) || (set >= m_barSets.count())) { + // No set, no value. + return 0; + } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { + // No category, no value. + return 0; + } + + qreal value = m_barSets.at(set)->at(category); + qreal sum = categorySum(category); + if ( qFuzzyIsNull(sum) ) { + return 0; + } + + return value / sum; } qreal QBarSeriesPrivate::categorySum(int category) { - return m_internalModel->categorySum(category); + qreal sum(0); + int count = m_barSets.count(); // Count sets + for (int set = 0; set < count; set++) { + if (category < m_barSets.at(set)->count()) + sum += m_barSets.at(set)->at(category); + } + return sum; } qreal QBarSeriesPrivate::absoluteCategorySum(int category) { - return m_internalModel->absoluteCategorySum(category); + qreal sum(0); + int count = m_barSets.count(); // Count sets + for (int set = 0; set < count; set++) { + if (category < m_barSets.at(set)->count()) + sum += qAbs(m_barSets.at(set)->at(category)); + } + return sum; } qreal QBarSeriesPrivate::maxCategorySum() { - return m_internalModel->maxCategorySum(); -} - -BarChartModel& QBarSeriesPrivate::modelInternal() -{ - return *m_internalModel; + qreal max = INT_MIN; + int count = m_categories.count(); + for (int i = 0; i < count; i++) { + qreal sum = categorySum(i); + if (sum > max) + max = sum; + } + return max; } bool QBarSeriesPrivate::setModel(QAbstractItemModel *model) @@ -353,12 +411,11 @@ void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int t this, SLOT(modelUpdated(QModelIndex,QModelIndex))); // create the initial bars - delete m_internalModel; + m_categories.clear(); if (m_mapOrientation == Qt::Vertical) { - QStringList categories; - for (int k = 0; k < m_model->rowCount(); k++) - categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString(); - m_internalModel = new BarChartModel(categories, this); + for (int k = 0; k < m_model->rowCount(); k++) { + m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString(); + } for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) { QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1)); @@ -367,10 +424,9 @@ void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int t q->appendBarSet(barSet); } } else { - QStringList categories; - for (int k = 0; k < m_model->columnCount(); k++) - categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString(); - m_internalModel = new BarChartModel(categories, this); + for (int k = 0; k < m_model->columnCount(); k++) { + m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString(); + } for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) { QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1)); @@ -413,7 +469,7 @@ void QBarSeriesPrivate::scaleDomain(Domain& domain) int tickXCount(domain.tickXCount()); int tickYCount(domain.tickYCount()); - qreal x = m_internalModel->categoryCount(); + qreal x = m_categories.count(); qreal y = max(); minX = qMin(minX, x); minY = qMin(minY, y); diff --git a/src/barchart/qbarseries.h b/src/barchart/qbarseries.h index b02239d..c9ecfcd 100644 --- a/src/barchart/qbarseries.h +++ b/src/barchart/qbarseries.h @@ -31,7 +31,6 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE typedef QStringList QBarCategories; class QBarSet; -class BarChartModel; class BarCategory; class QBarSeriesPrivate; diff --git a/src/barchart/qbarseries_p.h b/src/barchart/qbarseries_p.h index 5eef36d..c3a0b8e 100644 --- a/src/barchart/qbarseries_p.h +++ b/src/barchart/qbarseries_p.h @@ -33,7 +33,6 @@ public: qreal categorySum(int category); qreal absoluteCategorySum(int category); qreal maxCategorySum(); - BarChartModel& modelInternal(); Q_SIGNALS: void clicked(QBarSet *barset, QString category, Qt::MouseButtons button); @@ -47,7 +46,9 @@ private Q_SLOTS: void barsetChanged(); protected: - BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good. + QList m_barSets; + QBarCategories m_categories; + QAbstractItemModel* m_model; int m_mapCategories; int m_mapBarBottom; diff --git a/src/barchart/qstackedbarseries.cpp b/src/barchart/qstackedbarseries.cpp index cbef863..11af3b9 100644 --- a/src/barchart/qstackedbarseries.cpp +++ b/src/barchart/qstackedbarseries.cpp @@ -21,7 +21,6 @@ #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" @@ -79,7 +78,7 @@ void QStackedBarSeriesPrivate::scaleDomain(Domain& domain) int tickXCount(domain.tickXCount()); int tickYCount(domain.tickYCount()); - qreal x = m_internalModel->categoryCount(); + qreal x = m_categories.count(); qreal y = maxCategorySum(); minX = qMin(minX, x); minY = qMin(minY, y);