qbarseries.cpp
407 lines
| 11.5 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
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 | */ | ||
|
r762 | QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) : QSeries(parent), | ||
|
r763 | m_internalModel(new BarChartModel(categories, this)) | ||
|
r71 | { | ||
|
r764 | m_model = 0; | ||
|
r528 | m_mapCategories = -1; | ||
|
r609 | m_mapBarBottom = -1; | ||
m_mapBarTop = -1; | ||||
|
r734 | m_mapFirst = 0; | ||
m_mapCount = 0; | ||||
|
r630 | m_mapOrientation = Qt::Vertical; | ||
|
r71 | } | ||
|
r313 | /*! | ||
|
r425 | Adds a set of bars to series. Takes ownership of \a set. | ||
|
r812 | Connects the clicked(QString, Qt::MouseButtons) signal | ||
|
r425 | of \a set to this series | ||
|
r313 | */ | ||
|
r776 | void QBarSeries::appendBarSet(QBarSet *set) | ||
|
r171 | { | ||
|
r776 | m_internalModel->appendBarSet(set); | ||
|
r812 | connect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons))); | ||
|
r655 | connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged())); | ||
|
r681 | emit updatedBars(); | ||
|
r171 | } | ||
|
r313 | /*! | ||
Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set. | ||||
|
r812 | Disconnects the clicked(QString, Qt::MouseButtons) signal | ||
|
r425 | of \a set from this series | ||
|
r313 | */ | ||
|
r338 | void QBarSeries::removeBarSet(QBarSet *set) | ||
|
r171 | { | ||
|
r812 | disconnect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons))); | ||
|
r763 | m_internalModel->removeBarSet(set); | ||
|
r681 | emit updatedBars(); | ||
|
r172 | } | ||
|
r662 | void QBarSeries::insertBarSet(int i, QBarSet *set) | ||
{ | ||||
|
r763 | m_internalModel->insertBarSet(i, set); | ||
|
r662 | // emit barsetChanged(); | ||
} | ||||
void QBarSeries::insertCategory(int i, QString category) | ||||
{ | ||||
|
r763 | m_internalModel->insertCategory(i, category); | ||
|
r662 | } | ||
|
r663 | void QBarSeries::removeCategory(int i) | ||
{ | ||||
|
r763 | m_internalModel->removeCategory(i); | ||
|
r663 | } | ||
|
r313 | /*! | ||
Returns number of sets in series. | ||||
*/ | ||||
|
r776 | int QBarSeries::barsetCount() const | ||
|
r214 | { | ||
|
r662 | // if(m_model) | ||
// return m_mapBarTop - m_mapBarBottom; | ||||
// else | ||||
|
r763 | return m_internalModel->barsetCount(); | ||
|
r214 | } | ||
|
r323 | /*! | ||
Returns number of categories in series | ||||
*/ | ||||
|
r776 | int QBarSeries::categoryCount() const | ||
|
r323 | { | ||
|
r763 | return m_internalModel->categoryCount(); | ||
|
r323 | } | ||
|
r313 | /*! | ||
|
r357 | Returns a list of sets in series. Keeps ownership of sets. | ||
*/ | ||||
|
r776 | QList<QBarSet*> QBarSeries::barSets() const | ||
|
r214 | { | ||
|
r763 | return m_internalModel->barSets(); | ||
|
r214 | } | ||
|
r313 | /*! | ||
|
r357 | \internal \a index | ||
|
r313 | */ | ||
|
r357 | QBarSet* QBarSeries::barsetAt(int index) | ||
|
r288 | { | ||
|
r776 | return m_internalModel->barsetAt(index); | ||
|
r288 | } | ||
|
r313 | /*! | ||
|
r319 | \internal \a category | ||
|
r313 | */ | ||
|
r412 | QString QBarSeries::categoryName(int category) | ||
|
r288 | { | ||
|
r763 | return m_internalModel->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) { | ||
|
r763 | for (int i=0; i<m_internalModel->barsetCount(); i++) { | ||
|
r776 | QBarSet *set = m_internalModel->barsetAt(i); | ||
|
r737 | connect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString))); | ||
|
r296 | } | ||
} else { | ||||
|
r763 | for (int i=0; i<m_internalModel->barsetCount(); i++) { | ||
|
r776 | QBarSet *set = m_internalModel->barsetAt(i); | ||
|
r737 | disconnect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString))); | ||
|
r296 | } | ||
} | ||||
|
r288 | } | ||
|
r425 | |||
/*! | ||||
\internal \a category | ||||
*/ | ||||
|
r812 | void QBarSeries::barsetClicked(QString category, Qt::MouseButtons button) | ||
|
r425 | { | ||
|
r812 | emit clicked(qobject_cast<QBarSet*>(sender()), category, button); | ||
|
r425 | } | ||
|
r313 | /*! | ||
|
r319 | \internal | ||
|
r313 | */ | ||
|
r338 | qreal QBarSeries::min() | ||
|
r172 | { | ||
|
r763 | return m_internalModel->min(); | ||
|
r172 | } | ||
|
r313 | /*! | ||
|
r319 | \internal | ||
|
r313 | */ | ||
|
r338 | qreal QBarSeries::max() | ||
|
r172 | { | ||
|
r763 | return m_internalModel->max(); | ||
|
r171 | } | ||
|
r313 | /*! | ||
|
r319 | \internal \a set \a category | ||
|
r313 | */ | ||
|
r338 | qreal QBarSeries::valueAt(int set, int category) | ||
|
r172 | { | ||
|
r763 | return m_internalModel->valueAt(set, category); | ||
|
r172 | } | ||
|
r313 | /*! | ||
|
r319 | \internal \a set \a category | ||
|
r313 | */ | ||
|
r338 | qreal QBarSeries::percentageAt(int set, int category) | ||
|
r288 | { | ||
|
r763 | return m_internalModel->percentageAt(set, category); | ||
|
r288 | } | ||
|
r313 | /*! | ||
|
r319 | \internal \a category | ||
|
r313 | */ | ||
|
r338 | qreal QBarSeries::categorySum(int category) | ||
|
r288 | { | ||
|
r763 | return m_internalModel->categorySum(category); | ||
|
r288 | } | ||
|
r313 | /*! | ||
|
r319 | \internal | ||
|
r313 | */ | ||
|
r338 | qreal QBarSeries::maxCategorySum() | ||
|
r172 | { | ||
|
r763 | return m_internalModel->maxCategorySum(); | ||
|
r172 | } | ||
|
r313 | /*! | ||
|
r319 | \internal | ||
|
r313 | */ | ||
|
r338 | BarChartModel& QBarSeries::model() | ||
|
r172 | { | ||
|
r763 | return *m_internalModel; | ||
|
r172 | } | ||
|
r737 | bool QBarSeries::setModel(QAbstractItemModel *model) | ||
|
r527 | { | ||
|
r630 | // disconnect signals from old model | ||
if(m_model) | ||||
{ | ||||
disconnect(m_model, 0, this, 0); | ||||
m_mapCategories = -1; | ||||
m_mapBarBottom = -1; | ||||
m_mapBarTop = -1; | ||||
|
r734 | m_mapFirst = 0; | ||
m_mapCount = 0; | ||||
|
r630 | m_mapOrientation = Qt::Vertical; | ||
} | ||||
// set new model | ||||
if(model) | ||||
{ | ||||
m_model = model; | ||||
return true; | ||||
} | ||||
else | ||||
{ | ||||
|
r764 | m_model = 0; | ||
|
r630 | return false; | ||
} | ||||
|
r527 | } | ||
|
r611 | // TODO | ||
|
r630 | void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation) | ||
|
r527 | { | ||
|
r764 | if (!m_model) | ||
|
r630 | return; | ||
|
r737 | |||
|
r630 | m_mapCategories = categories; | ||
m_mapBarBottom = bottomBoundry; | ||||
m_mapBarTop = topBoundry; | ||||
|
r735 | // m_mapFirst = 1; | ||
|
r630 | m_mapOrientation = orientation; | ||
// connect the signals | ||||
|
r737 | if (m_mapOrientation == Qt::Vertical) { | ||
|
r735 | m_mapCount = m_model->rowCount() - m_mapFirst; | ||
|
r737 | 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 { | ||||
|
r735 | m_mapCount = m_model->columnCount() - m_mapFirst; | ||
|
r737 | 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))); | ||||
|
r630 | } | ||
// create the initial bars | ||||
|
r763 | delete m_internalModel; | ||
|
r737 | if (m_mapOrientation == Qt::Vertical) { | ||
|
r630 | QStringList categories; | ||
|
r734 | for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++) | ||
|
r650 | categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString(); | ||
|
r763 | m_internalModel = new BarChartModel(categories, this); | ||
|
r630 | |||
|
r737 | for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) { | ||
|
r650 | QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1)); | ||
|
r734 | for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++) | ||
|
r650 | *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble(); | ||
|
r776 | appendBarSet(barSet); | ||
|
r630 | } | ||
|
r737 | } else { | ||
|
r630 | QStringList categories; | ||
|
r734 | for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++) | ||
|
r650 | categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString(); | ||
|
r763 | m_internalModel = new BarChartModel(categories, this); | ||
|
r630 | |||
|
r737 | for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) { | ||
|
r650 | QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1)); | ||
|
r734 | for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++) | ||
|
r650 | *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble(); | ||
|
r776 | appendBarSet(barSet); | ||
|
r630 | } | ||
} | ||||
|
r527 | } | ||
|
r734 | void QBarSeries::setModelMappingShift(int first, int count) | ||
{ | ||||
m_mapFirst = first; | ||||
m_mapCount = count; | ||||
} | ||||
|
r630 | void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) | ||
{ | ||||
Q_UNUSED(bottomRight) | ||||
if (m_mapOrientation == Qt::Vertical) | ||||
{ | ||||
|
r734 | // 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()); | ||||
|
r630 | } | ||
else | ||||
{ | ||||
|
r734 | // 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()); | ||||
|
r630 | } | ||
} | ||||
|
r662 | void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/) | ||
|
r630 | { | ||
|
r737 | if (m_mapOrientation == Qt::Vertical) { | ||
|
r734 | insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1)); | ||
|
r737 | for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) { | ||
|
r734 | barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble()); | ||
|
r662 | } | ||
|
r737 | } else { | ||
|
r734 | insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1)); | ||
|
r737 | for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) { | ||
|
r734 | barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble()); | ||
|
r662 | } | ||
} | ||||
emit restructuredBar(1); | ||||
|
r630 | } | ||
|
r737 | void QBarSeries::modelDataRemoved(QModelIndex parent, int start, int end) | ||
|
r630 | { | ||
|
r737 | Q_UNUSED(parent) | ||
Q_UNUSED(end) | ||||
|
r734 | removeCategory(start - m_mapFirst); | ||
|
r663 | for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) | ||
{ | ||||
|
r734 | barsetAt(i)->removeValue(start - m_mapFirst); | ||
|
r663 | } | ||
emit restructuredBar(1); | ||||
|
r630 | } | ||
void QBarSeries::barsetChanged() | ||||
|
r527 | { | ||
|
r655 | emit updatedBars(); | ||
|
r527 | } | ||
|
r703 | QBarCategories QBarSeries::categories() const | ||
{ | ||||
QBarCategories categories; | ||||
|
r763 | int count = m_internalModel->categoryCount(); | ||
|
r737 | for (int i=1; i <= count; i++) { | ||
|
r763 | categories.insert(i, m_internalModel->categoryName(i - 1)); | ||
|
r703 | } | ||
return categories; | ||||
} | ||||
|
r839 | /*! | ||
Sets the visibility of labels in series to \a visible | ||||
*/ | ||||
|
r820 | void QBarSeries::setLabelsVisible(bool visible) | ||
|
r813 | { | ||
foreach (QBarSet* s, barSets()) { | ||||
|
r820 | s->setLabelsVisible(visible); | ||
|
r813 | } | ||
} | ||||
|
r338 | #include "moc_qbarseries.cpp" | ||
|
r71 | |||
|
r56 | QTCOMMERCIALCHART_END_NAMESPACE | ||