From c9f8f8ef7809ca8b48a6c10434111e49d1e338f4 2012-03-20 11:08:29 From: Marek Rosa Date: 2012-03-20 11:08:29 Subject: [PATCH] Adding data to BarSet through model added --- diff --git a/src/barchart/barchartmodel.cpp b/src/barchart/barchartmodel.cpp index a252543..89a6dfe 100644 --- a/src/barchart/barchartmodel.cpp +++ b/src/barchart/barchartmodel.cpp @@ -29,6 +29,16 @@ void BarChartModel::removeBarSet(QBarSet *set) } } +void BarChartModel::insertBarSet(int i, QBarSet *set) +{ + mDataModel.insert(i, set); +} + +void BarChartModel::insertCategory(int i, QString category) +{ + mCategory.insert(i, category); +} + QBarSet* BarChartModel::setAt(int index) { return mDataModel.at(index); diff --git a/src/barchart/barchartmodel_p.h b/src/barchart/barchartmodel_p.h index 7659d95..bdef70f 100644 --- a/src/barchart/barchartmodel_p.h +++ b/src/barchart/barchartmodel_p.h @@ -22,6 +22,8 @@ public: QStringList category(); void addBarSet(QBarSet *set); void removeBarSet(QBarSet *set); + void insertBarSet(int i, QBarSet *set); + void insertCategory(int i, QString category); QBarSet *setAt(int index); QList barSets(); diff --git a/src/barchart/barpresenter.cpp b/src/barchart/barpresenter.cpp index 340b4ae..ef59061 100644 --- a/src/barchart/barpresenter.cpp +++ b/src/barchart/barpresenter.cpp @@ -10,6 +10,7 @@ BarPresenter::BarPresenter(QBarSeries *series, QChart *parent) : BarPresenterBase(series, parent) { connect(series, SIGNAL(updatedBars()), this, SLOT(layoutChanged())); + connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int))); } void BarPresenter::layoutChanged() diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index b6460f8..79d8fea 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -69,15 +69,26 @@ void QBarSeries::removeBarSet(QBarSet *set) mModel->removeBarSet(set); } +void QBarSeries::insertBarSet(int i, QBarSet *set) +{ + mModel->insertBarSet(i, set); + // emit barsetChanged(); +} + +void QBarSeries::insertCategory(int i, QString category) +{ + mModel->insertCategory(i, category); +} + /*! Returns number of sets in series. */ int QBarSeries::barsetCount() { -// if(m_model) -// return m_mapBarTop - m_mapBarBottom; -// else - return mModel->barsetCount(); + // if(m_model) + // return m_mapBarTop - m_mapBarBottom; + // else + return mModel->barsetCount(); } /*! @@ -299,21 +310,37 @@ void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) { if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop) barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); -// else if (topLeft.column() == m_mapCategories) -// slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); + // else if (topLeft.column() == m_mapCategories) + // slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); } else { if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop) barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); -// else if (topLeft.row() == m_mapCategories) -// slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); + // else if (topLeft.row() == m_mapCategories) + // slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); } } -void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int /*start*/, int /*end*/) +void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/) { - // + if (m_mapOrientation == Qt::Vertical) + { + insertCategory(start, QString("Row: %1").arg(start + 1)); + for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) + { + barsetAt(i)->insertValue(start, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble()); + } + } + else + { + insertCategory(start, QString("Column: %1").arg(start + 1)); + for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) + { + barsetAt(i)->insertValue(start, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble()); + } + } + emit restructuredBar(1); } void QBarSeries::modelDataRemoved(QModelIndex /*parent*/, int /*start*/, int /*end*/) diff --git a/src/barchart/qbarseries.h b/src/barchart/qbarseries.h index f4cdebd..9a0e33c 100644 --- a/src/barchart/qbarseries.h +++ b/src/barchart/qbarseries.h @@ -21,6 +21,8 @@ public: void addBarSet(QBarSet *set); // Takes ownership of set void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set + void insertBarSet(int i, QBarSet *set); + void insertCategory(int i, QString category); int barsetCount(); int categoryCount(); QList barSets(); @@ -51,6 +53,7 @@ signals: // void updatedBars(); + void restructuredBar(int); // TODO: internal signals, these to private implementation. // TODO: TO PIMPL ---> diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index d98de32..3c579f3 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -86,6 +86,11 @@ QBarSet& QBarSet::operator << (const qreal &value) return *this; } +void QBarSet::insertValue(int i, qreal value) +{ + mValues.insert(i, value); +} + /*! Returns count of values in set. */ diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 5c1ef93..ce0af59 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -16,6 +16,7 @@ public: void setName(QString name); QString name(); QBarSet& operator << (const qreal &value); // appends new value to set + void insertValue(int i, qreal value); // TODO: remove indices eventually. Use as internal? int count(); // count of values in set