qbarseries.cpp
358 lines
| 9.4 KiB
| text/x-c
|
CppLexer
|
r71 | #include <QDebug> | ||
|
r338 | #include "qbarseries.h" | ||
|
r172 | #include "qbarset.h" | ||
#include "barchartmodel_p.h" | ||||
|
r126 | |||
|
r56 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r313 | /*! | ||
|
r338 | \class QBarSeries | ||
|
r313 | \brief part of QtCommercial chart API. | ||
|
r338 | QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible | ||
QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined | ||||
|
r377 | by QStringList. | ||
|
r313 | |||
|
r319 | \mainclass | ||
|
r377 | \sa QBarSet, QStackedBarSeries, QPercentBarSeries | ||
|
r313 | */ | ||
/*! | ||||
|
r360 | \fn virtual QSeriesType QBarSeries::type() const | ||
|
r313 | \brief Returns type of series. | ||
|
r360 | \sa QSeries, QSeriesType | ||
|
r313 | */ | ||
|
r425 | |||
|
r313 | /*! | ||
|
r338 | \fn void QBarSeries::showToolTip(QPoint pos, QString tip) | ||
|
r319 | \brief \internal \a pos \a tip | ||
|
r313 | */ | ||
/*! | ||||
|
r387 | Constructs empty QBarSeries. Parameter \a categories defines the categories for chart. | ||
|
r338 | QBarSeries is QObject which is a child of a \a parent. | ||
|
r313 | */ | ||
|
r377 | QBarSeries::QBarSeries(QStringList categories, QObject *parent) | ||
|
r360 | : QSeries(parent) | ||
|
r377 | ,mModel(new BarChartModel(categories, this)) | ||
|
r71 | { | ||
|
r528 | m_model = NULL; | ||
m_mapCategories = -1; | ||||
|
r609 | m_mapBarBottom = -1; | ||
m_mapBarTop = -1; | ||||
|
r630 | m_mapOrientation = Qt::Vertical; | ||
|
r71 | } | ||
|
r313 | /*! | ||
|
r425 | Adds a set of bars to series. Takes ownership of \a set. | ||
Connects the clicked(QString) and rightClicked(QString) signals | ||||
of \a set to this series | ||||
|
r313 | */ | ||
|
r338 | void QBarSeries::addBarSet(QBarSet *set) | ||
|
r171 | { | ||
|
r172 | mModel->addBarSet(set); | ||
|
r425 | connect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString))); | ||
connect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString))); | ||||
|
r655 | connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged())); | ||
|
r171 | } | ||
|
r313 | /*! | ||
Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set. | ||||
|
r425 | Disconnects the clicked(QString) and rightClicked(QString) signals | ||
of \a set from this series | ||||
|
r313 | */ | ||
|
r338 | void QBarSeries::removeBarSet(QBarSet *set) | ||
|
r171 | { | ||
|
r425 | disconnect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString))); | ||
disconnect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString))); | ||||
|
r172 | mModel->removeBarSet(set); | ||
} | ||||
|
r662 | void QBarSeries::insertBarSet(int i, QBarSet *set) | ||
{ | ||||
mModel->insertBarSet(i, set); | ||||
// emit barsetChanged(); | ||||
} | ||||
void QBarSeries::insertCategory(int i, QString category) | ||||
{ | ||||
mModel->insertCategory(i, category); | ||||
} | ||||
|
r313 | /*! | ||
Returns number of sets in series. | ||||
*/ | ||||
|
r366 | int QBarSeries::barsetCount() | ||
|
r214 | { | ||
|
r662 | // if(m_model) | ||
// return m_mapBarTop - m_mapBarBottom; | ||||
// else | ||||
return mModel->barsetCount(); | ||||
|
r214 | } | ||
|
r323 | /*! | ||
Returns number of categories in series | ||||
*/ | ||||
|
r366 | int QBarSeries::categoryCount() | ||
|
r323 | { | ||
|
r387 | return mModel->categoryCount(); | ||
|
r323 | } | ||
|
r313 | /*! | ||
|
r357 | Returns a list of sets in series. Keeps ownership of sets. | ||
*/ | ||||
QList<QBarSet*> QBarSeries::barSets() | ||||
|
r214 | { | ||
|
r357 | return mModel->barSets(); | ||
|
r214 | } | ||
|
r313 | /*! | ||
|
r357 | \internal \a index | ||
|
r313 | */ | ||
|
r357 | QBarSet* QBarSeries::barsetAt(int index) | ||
|
r288 | { | ||
return mModel->setAt(index); | ||||
} | ||||
|
r313 | /*! | ||
|
r319 | \internal \a category | ||
|
r313 | */ | ||
|
r412 | QString QBarSeries::categoryName(int category) | ||
|
r288 | { | ||
|
r412 | return mModel->categoryName(category); | ||
|
r288 | } | ||
|
r313 | /*! | ||
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 | ||||
*/ | ||||
|
r357 | void QBarSeries::setToolTipEnabled(bool enabled) | ||
|
r280 | { | ||
|
r487 | // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled. | ||
|
r296 | if (enabled) { | ||
|
r387 | for (int i=0; i<mModel->barsetCount(); i++) { | ||
|
r296 | QBarSet *set = mModel->setAt(i); | ||
connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); | ||||
} | ||||
} else { | ||||
|
r387 | for (int i=0; i<mModel->barsetCount(); i++) { | ||
|
r296 | QBarSet *set = mModel->setAt(i); | ||
disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); | ||||
} | ||||
} | ||||
|
r288 | } | ||
|
r425 | |||
/*! | ||||
\internal \a category | ||||
*/ | ||||
void QBarSeries::barsetClicked(QString category) | ||||
{ | ||||
emit clicked(qobject_cast<QBarSet*>(sender()), category); | ||||
} | ||||
/*! | ||||
\internal \a category | ||||
*/ | ||||
void QBarSeries::barsetRightClicked(QString category) | ||||
{ | ||||
emit rightClicked(qobject_cast<QBarSet*>(sender()), category); | ||||
} | ||||
|
r313 | /*! | ||
|
r319 | \internal | ||
|
r313 | */ | ||
|
r338 | qreal QBarSeries::min() | ||
|
r172 | { | ||
return mModel->min(); | ||||
} | ||||
|
r313 | /*! | ||
|
r319 | \internal | ||
|
r313 | */ | ||
|
r338 | qreal QBarSeries::max() | ||
|
r172 | { | ||
return mModel->max(); | ||||
|
r171 | } | ||
|
r313 | /*! | ||
|
r319 | \internal \a set \a category | ||
|
r313 | */ | ||
|
r338 | qreal QBarSeries::valueAt(int set, int category) | ||
|
r172 | { | ||
return mModel->valueAt(set,category); | ||||
} | ||||
|
r313 | /*! | ||
|
r319 | \internal \a set \a category | ||
|
r313 | */ | ||
|
r338 | qreal QBarSeries::percentageAt(int set, int category) | ||
|
r288 | { | ||
return mModel->percentageAt(set,category); | ||||
} | ||||
|
r313 | /*! | ||
|
r319 | \internal \a category | ||
|
r313 | */ | ||
|
r338 | qreal QBarSeries::categorySum(int category) | ||
|
r288 | { | ||
return mModel->categorySum(category); | ||||
} | ||||
|
r313 | /*! | ||
|
r319 | \internal | ||
|
r313 | */ | ||
|
r338 | qreal QBarSeries::maxCategorySum() | ||
|
r172 | { | ||
return mModel->maxCategorySum(); | ||||
} | ||||
|
r313 | /*! | ||
|
r319 | \internal | ||
|
r313 | */ | ||
|
r338 | BarChartModel& QBarSeries::model() | ||
|
r172 | { | ||
return *mModel; | ||||
} | ||||
|
r527 | bool QBarSeries::setModel(QAbstractItemModel* model) | ||
{ | ||||
|
r630 | // disconnect signals from old model | ||
if(m_model) | ||||
{ | ||||
disconnect(m_model, 0, this, 0); | ||||
m_mapCategories = -1; | ||||
m_mapBarBottom = -1; | ||||
m_mapBarTop = -1; | ||||
m_mapOrientation = Qt::Vertical; | ||||
} | ||||
// set new model | ||||
if(model) | ||||
{ | ||||
m_model = model; | ||||
return true; | ||||
} | ||||
else | ||||
{ | ||||
m_model = NULL; | ||||
return false; | ||||
} | ||||
|
r527 | } | ||
|
r611 | // TODO | ||
|
r630 | void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation) | ||
|
r527 | { | ||
|
r630 | if (m_model == NULL) | ||
return; | ||||
m_mapCategories = categories; | ||||
m_mapBarBottom = bottomBoundry; | ||||
m_mapBarTop = topBoundry; | ||||
m_mapOrientation = orientation; | ||||
// connect the signals | ||||
if (m_mapOrientation == Qt::Vertical) | ||||
{ | ||||
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 | ||||
{ | ||||
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 mModel; | ||||
if (m_mapOrientation == Qt::Vertical) | ||||
{ | ||||
QStringList categories; | ||||
|
r650 | for (int k = 0; k < m_model->rowCount(); k++) | ||
categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString(); | ||||
|
r630 | mModel = new BarChartModel(categories, this); | ||
for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) | ||||
{ | ||||
|
r650 | QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1)); | ||
for(int m = 0; m < m_model->rowCount(); m++) | ||||
*barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble(); | ||||
|
r630 | addBarSet(barSet); | ||
} | ||||
} | ||||
else | ||||
{ | ||||
QStringList categories; | ||||
|
r650 | for (int k = 0; k < m_model->columnCount(); k++) | ||
categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString(); | ||||
|
r630 | mModel = new BarChartModel(categories, this); | ||
for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) | ||||
{ | ||||
|
r650 | QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1)); | ||
for(int m = 0; m < m_model->columnCount(); m++) | ||||
*barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble(); | ||||
|
r630 | addBarSet(barSet); | ||
} | ||||
} | ||||
|
r527 | } | ||
|
r630 | void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) | ||
{ | ||||
Q_UNUSED(bottomRight) | ||||
if (m_mapOrientation == Qt::Vertical) | ||||
{ | ||||
if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop) | ||||
|
r650 | barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); | ||
|
r662 | // else if (topLeft.column() == m_mapCategories) | ||
// slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | ||||
|
r630 | } | ||
else | ||||
{ | ||||
if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop) | ||||
|
r650 | barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); | ||
|
r662 | // else if (topLeft.row() == m_mapCategories) | ||
// slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | ||||
|
r630 | } | ||
} | ||||
|
r662 | void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/) | ||
|
r630 | { | ||
|
r662 | 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); | ||||
|
r630 | } | ||
void QBarSeries::modelDataRemoved(QModelIndex /*parent*/, int /*start*/, int /*end*/) | ||||
{ | ||||
// | ||||
} | ||||
void QBarSeries::barsetChanged() | ||||
|
r527 | { | ||
|
r655 | emit updatedBars(); | ||
|
r527 | } | ||
|
r338 | #include "moc_qbarseries.cpp" | ||
|
r71 | |||
|
r56 | QTCOMMERCIALCHART_END_NAMESPACE | ||