qpieseries.cpp
816 lines
| 23.9 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r142 | #include "qpieseries.h" | ||
|
r938 | #include "qpieseries_p.h" | ||
|
r818 | #include "qpieslice.h" | ||
#include "pieslicedata_p.h" | ||||
|
r943 | #include "chartdataset_p.h" | ||
#include "charttheme_p.h" | ||||
#include "chartanimator_p.h" | ||||
|
r950 | #include "legendmarker_p.h" | ||
|
r1204 | #include <QAbstractItemModel> | ||
|
r1164 | #include "qpiemodelmapper.h" | ||
|
r142 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r320 | /*! | ||
\class QPieSeries | ||||
\brief Pie series API for QtCommercial Charts | ||||
|
r952 | The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects. | ||
|
r320 | The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices. | ||
|
r386 | The actual slice size is determined by that relative value. | ||
|
r320 | |||
|
r952 | Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0 | ||
These relate to the actual chart rectangle. | ||||
By default the pie is defined as a full pie but it can also be a partial pie. | ||||
|
r320 | This can be done by setting a starting angle and angle span to the series. | ||
|
r952 | Full pie is 360 degrees where 0 is at 12 a'clock. | ||
|
r995 | |||
See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart. | ||||
\image examples_piechart.png | ||||
|
r320 | */ | ||
|
r924 | /*! | ||
|
r928 | \property QPieSeries::horizontalPosition | ||
|
r932 | \brief Defines the horizontal position of the pie. | ||
|
r924 | |||
|
r932 | The value is a relative value to the chart rectangle where: | ||
\list | ||||
\o 0.0 is the absolute left. | ||||
\o 1.0 is the absolute right. | ||||
\endlist | ||||
Default value is 0.5 (center). | ||||
|
r924 | */ | ||
|
r928 | /*! | ||
\property QPieSeries::verticalPosition | ||||
|
r932 | \brief Defines the vertical position of the pie. | ||
The value is a relative value to the chart rectangle where: | ||||
\list | ||||
\o 0.0 is the absolute top. | ||||
\o 1.0 is the absolute bottom. | ||||
\endlist | ||||
|
r928 | |||
|
r932 | Default value is 0.5 (center). | ||
|
r928 | */ | ||
/*! | ||||
\property QPieSeries::size | ||||
|
r932 | \brief Defines the pie size. | ||
|
r928 | |||
|
r932 | The value is a relative value to the chart rectangle where: | ||
\list | ||||
|
r974 | \o 0.0 is the minimum size (pie not drawn). | ||
|
r932 | \o 1.0 is the maximum size that can fit the chart. | ||
\endlist | ||||
Default value is 0.7. | ||||
|
r928 | */ | ||
/*! | ||||
\property QPieSeries::startAngle | ||||
|
r932 | \brief Defines the starting angle of the pie. | ||
|
r928 | |||
|
r932 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. | ||
|
r928 | |||
|
r932 | Default is value is 0. | ||
*/ | ||||
|
r928 | |||
/*! | ||||
\property QPieSeries::endAngle | ||||
|
r932 | \brief Defines the ending angle of the pie. | ||
|
r928 | |||
|
r932 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. | ||
|
r928 | |||
|
r932 | Default is value is 360. | ||
*/ | ||||
|
r928 | |||
|
r924 | |||
|
r314 | /*! | ||
Constructs a series object which is a child of \a parent. | ||||
*/ | ||||
|
r203 | QPieSeries::QPieSeries(QObject *parent) : | ||
|
r988 | QAbstractSeries(*new QPieSeriesPrivate(this),parent) | ||
|
r203 | { | ||
|
r157 | |||
|
r203 | } | ||
|
r174 | |||
|
r314 | /*! | ||
|
r933 | Destroys the series and its slices. | ||
|
r314 | */ | ||
|
r203 | QPieSeries::~QPieSeries() | ||
{ | ||||
|
r669 | // NOTE: d_prt destroyed by QObject | ||
|
r203 | } | ||
|
r314 | /*! | ||
|
r320 | Returns QChartSeries::SeriesTypePie. | ||
|
r314 | */ | ||
|
r1107 | QAbstractSeries::SeriesType QPieSeries::type() const | ||
|
r314 | { | ||
|
r988 | return QAbstractSeries::SeriesTypePie; | ||
|
r314 | } | ||
/*! | ||||
|
r933 | Appends an array of \a slices to the series. | ||
|
r314 | Slice ownership is passed to the series. | ||
*/ | ||||
|
r1104 | bool QPieSeries::append(QList<QPieSlice*> slices) | ||
|
r203 | { | ||
|
r669 | Q_D(QPieSeries); | ||
|
r1104 | if (slices.count() == 0) | ||
return false; | ||||
foreach (QPieSlice* s, slices) { | ||||
if (!s || d->m_slices.contains(s)) | ||||
return false; | ||||
} | ||||
|
r203 | foreach (QPieSlice* s, slices) { | ||
s->setParent(this); | ||||
|
r669 | d->m_slices << s; | ||
|
r174 | } | ||
|
r157 | |||
|
r669 | d->updateDerivativeData(); | ||
|
r174 | |||
|
r203 | foreach (QPieSlice* s, slices) { | ||
|
r669 | connect(s, SIGNAL(changed()), d, SLOT(sliceChanged())); | ||
|
r1009 | connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked())); | ||
connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool))); | ||||
|
r203 | } | ||
|
r1213 | emit added(slices); | ||
|
r1104 | |||
return true; | ||||
|
r142 | } | ||
|
r314 | /*! | ||
|
r933 | Appends a single \a slice to the series. | ||
|
r314 | Slice ownership is passed to the series. | ||
*/ | ||||
|
r1104 | bool QPieSeries::append(QPieSlice* slice) | ||
|
r142 | { | ||
|
r1104 | return append(QList<QPieSlice*>() << slice); | ||
|
r142 | } | ||
|
r415 | /*! | ||
|
r933 | Appends a single \a slice to the series and returns a reference to the series. | ||
|
r415 | Slice ownership is passed to the series. | ||
*/ | ||||
|
r406 | QPieSeries& QPieSeries::operator << (QPieSlice* slice) | ||
{ | ||||
|
r796 | append(slice); | ||
|
r406 | return *this; | ||
} | ||||
|
r314 | |||
/*! | ||||
|
r1206 | Appends a single slice to the series with give \a value and \a label. | ||
|
r314 | Slice ownership is passed to the series. | ||
*/ | ||||
|
r1206 | QPieSlice* QPieSeries::append(QString label, qreal value) | ||
|
r142 | { | ||
|
r1206 | QPieSlice* slice = new QPieSlice(label, value); | ||
|
r796 | append(slice); | ||
|
r203 | return slice; | ||
} | ||||
|
r174 | |||
|
r809 | /*! | ||
Inserts a single \a slice to the series before the slice at \a index position. | ||||
Slice ownership is passed to the series. | ||||
*/ | ||||
|
r1104 | bool QPieSeries::insert(int index, QPieSlice* slice) | ||
|
r604 | { | ||
|
r669 | Q_D(QPieSeries); | ||
|
r1104 | |||
if (index < 0 || index > d->m_slices.count()) | ||||
return false; | ||||
if (!slice || d->m_slices.contains(slice)) | ||||
return false; | ||||
|
r1204 | slice->setParent(this); | ||
d->m_slices.insert(index, slice); | ||||
|
r1195 | |||
|
r1204 | d->updateDerivativeData(); | ||
|
r604 | |||
|
r1204 | connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged())); | ||
connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked())); | ||||
connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool))); | ||||
|
r604 | |||
|
r1213 | emit added(QList<QPieSlice*>() << slice); | ||
|
r1104 | |||
return true; | ||||
|
r604 | } | ||
|
r314 | /*! | ||
Removes a single \a slice from the series and deletes the slice. | ||||
|
r320 | |||
|
r952 | Do not reference the pointer after this call. | ||
|
r314 | */ | ||
|
r1104 | bool QPieSeries::remove(QPieSlice* slice) | ||
|
r203 | { | ||
|
r669 | Q_D(QPieSeries); | ||
|
r1104 | |||
if (!d->m_slices.removeOne(slice)) | ||||
return false; | ||||
|
r174 | |||
|
r669 | d->updateDerivativeData(); | ||
|
r174 | |||
|
r1213 | emit removed(QList<QPieSlice*>() << slice); | ||
|
r621 | |||
|
r203 | delete slice; | ||
|
r823 | slice = 0; | ||
|
r1104 | |||
return true; | ||||
|
r142 | } | ||
|
r314 | /*! | ||
Clears all slices from the series. | ||||
*/ | ||||
|
r203 | void QPieSeries::clear() | ||
|
r142 | { | ||
|
r669 | Q_D(QPieSeries); | ||
|
r1204 | if (d->m_slices.count() == 0) | ||
|
r203 | return; | ||
|
r174 | |||
|
r1204 | QList<QPieSlice*> slices = d->m_slices; | ||
foreach (QPieSlice* s, d->m_slices) { | ||||
d->m_slices.removeOne(s); | ||||
delete s; | ||||
} | ||||
|
r566 | |||
|
r1204 | d->updateDerivativeData(); | ||
|
r621 | |||
|
r1213 | emit removed(slices); | ||
|
r142 | } | ||
|
r314 | /*! | ||
|
r933 | returns the number of the slices in this series. | ||
|
r314 | */ | ||
int QPieSeries::count() const | ||||
{ | ||||
|
r669 | Q_D(const QPieSeries); | ||
|
r1204 | return d->m_slices.count(); | ||
|
r314 | } | ||
|
r621 | /*! | ||
Returns true is the series is empty. | ||||
*/ | ||||
bool QPieSeries::isEmpty() const | ||||
{ | ||||
|
r669 | Q_D(const QPieSeries); | ||
return d->m_slices.isEmpty(); | ||||
|
r621 | } | ||
|
r314 | /*! | ||
Returns a list of slices that belong to this series. | ||||
*/ | ||||
QList<QPieSlice*> QPieSeries::slices() const | ||||
{ | ||||
|
r669 | Q_D(const QPieSeries); | ||
return d->m_slices; | ||||
|
r314 | } | ||
|
r884 | void QPieSeries::setHorizontalPosition(qreal relativePosition) | ||
{ | ||||
Q_D(QPieSeries); | ||||
if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0)) | ||||
|
r1009 | emit d->piePositionChanged(); | ||
|
r884 | } | ||
|
r454 | |||
|
r884 | void QPieSeries::setVerticalPosition(qreal relativePosition) | ||
|
r142 | { | ||
|
r669 | Q_D(QPieSeries); | ||
|
r884 | if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0)) | ||
|
r1009 | emit d->piePositionChanged(); | ||
|
r142 | } | ||
|
r884 | qreal QPieSeries::horizontalPosition() const | ||
|
r142 | { | ||
|
r669 | Q_D(const QPieSeries); | ||
return d->m_pieRelativeHorPos; | ||||
|
r142 | } | ||
|
r884 | qreal QPieSeries::verticalPosition() const | ||
|
r314 | { | ||
|
r669 | Q_D(const QPieSeries); | ||
return d->m_pieRelativeVerPos; | ||||
|
r314 | } | ||
|
r498 | void QPieSeries::setPieSize(qreal relativeSize) | ||
|
r437 | { | ||
|
r669 | Q_D(QPieSeries); | ||
|
r884 | if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0)) | ||
|
r1009 | emit d->pieSizeChanged(); | ||
|
r437 | } | ||
|
r314 | |||
|
r498 | qreal QPieSeries::pieSize() const | ||
|
r203 | { | ||
|
r669 | Q_D(const QPieSeries); | ||
return d->m_pieRelativeSize; | ||||
|
r437 | } | ||
|
r498 | void QPieSeries::setPieStartAngle(qreal angle) | ||
|
r437 | { | ||
|
r669 | Q_D(QPieSeries); | ||
|
r1207 | if (qFuzzyIsNull(d->m_pieStartAngle - angle)) | ||
return; | ||||
d->m_pieStartAngle = angle; | ||||
d->updateDerivativeData(); | ||||
|
r203 | } | ||
|
r498 | qreal QPieSeries::pieStartAngle() const | ||
|
r437 | { | ||
|
r669 | Q_D(const QPieSeries); | ||
return d->m_pieStartAngle; | ||||
|
r437 | } | ||
|
r454 | /*! | ||
Sets the end angle of the pie. | ||||
Full pie is 360 degrees where 0 degrees is at 12 a'clock. | ||||
|
r498 | \a angle must be greater than start angle. | ||
|
r454 | |||
|
r498 | \sa pieEndAngle(), pieStartAngle(), setPieStartAngle() | ||
|
r454 | */ | ||
|
r498 | void QPieSeries::setPieEndAngle(qreal angle) | ||
|
r437 | { | ||
|
r669 | Q_D(QPieSeries); | ||
|
r1207 | if (qFuzzyIsNull(d->m_pieEndAngle - angle)) | ||
return; | ||||
d->m_pieEndAngle = angle; | ||||
d->updateDerivativeData(); | ||||
|
r437 | } | ||
|
r454 | /*! | ||
Returns the end angle of the pie. | ||||
Full pie is 360 degrees where 0 degrees is at 12 a'clock. | ||||
|
r498 | \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle() | ||
|
r454 | */ | ||
|
r498 | qreal QPieSeries::pieEndAngle() const | ||
|
r437 | { | ||
|
r669 | Q_D(const QPieSeries); | ||
return d->m_pieEndAngle; | ||||
|
r437 | } | ||
|
r314 | /*! | ||
Sets the all the slice labels \a visible or invisible. | ||||
\sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible() | ||||
*/ | ||||
|
r203 | void QPieSeries::setLabelsVisible(bool visible) | ||
{ | ||||
|
r669 | Q_D(QPieSeries); | ||
foreach (QPieSlice* s, d->m_slices) | ||||
|
r203 | s->setLabelVisible(visible); | ||
} | ||||
|
r406 | /*! | ||
Returns the sum of all slice values in this series. | ||||
|
r952 | \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage() | ||
|
r406 | */ | ||
|
r939 | qreal QPieSeries::sum() const | ||
|
r406 | { | ||
|
r669 | Q_D(const QPieSeries); | ||
|
r939 | return d->m_sum; | ||
|
r406 | } | ||
|
r314 | /*! | ||
|
r1009 | \fn void QPieSeries::clicked(QPieSlice* slice) | ||
|
r314 | |||
|
r1009 | This signal is emitted when a \a slice has been clicked. | ||
|
r314 | |||
\sa QPieSlice::clicked() | ||||
*/ | ||||
/*! | ||||
|
r1009 | \fn void QPieSeries::hovered(QPieSlice* slice, bool state) | ||
|
r314 | |||
|
r1009 | This signal is emitted when user has hovered over or away from the \a slice. | ||
|
r314 | |||
|
r1009 | \a state is true when user has hovered over the slice and false when hover has moved away from the slice. | ||
|
r809 | |||
|
r1009 | \sa QPieSlice::hovered() | ||
|
r809 | */ | ||
|
r174 | |||
|
r879 | /*! | ||
\fn bool QPieSeries::setModel(QAbstractItemModel *model) | ||||
Sets the \a model to be used as a data source | ||||
*/ | ||||
|
r1164 | void QPieSeries::setModel(QAbstractItemModel* model) | ||
|
r597 | { | ||
|
r669 | Q_D(QPieSeries); | ||
|
r1204 | // disconnect signals from old model | ||
if(d->m_model) | ||||
{ | ||||
disconnect(d->m_model, 0, this, 0); | ||||
} | ||||
|
r597 | |||
|
r630 | // set new model | ||
|
r597 | if(model) | ||
{ | ||||
|
r938 | d->m_model = model; | ||
|
r1166 | // connect signals from the model | ||
connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex))); | ||||
connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int))); | ||||
connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int))); | ||||
connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int))); | ||||
connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int))); | ||||
|
r1164 | if (d->m_mapper) | ||
|
r1166 | d->initializePieFromModel(); | ||
|
r630 | } | ||
|
r1204 | else | ||
{ | ||||
d->m_model = 0; | ||||
} | ||||
} | ||||
void QPieSeries::setModelMapper(QPieModelMapper *mapper) | ||||
{ | ||||
Q_D(QPieSeries); | ||||
// disconnect signals from old mapper | ||||
if (d->m_mapper) { | ||||
QObject::disconnect(d->m_mapper, 0, this, 0); | ||||
} | ||||
if (mapper) { | ||||
d->m_mapper = mapper; | ||||
// connect the signal from the mapper | ||||
connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel())); | ||||
if (d->m_model) | ||||
d->initializePieFromModel(); | ||||
} else { | ||||
d->m_mapper = 0; | ||||
} | ||||
} | ||||
|
r1056 | |||
|
r1164 | QPieModelMapper* QPieSeries::modelMapper() const | ||
|
r1056 | { | ||
|
r1164 | Q_D(const QPieSeries); | ||
return d->m_mapper; | ||||
|
r597 | } | ||
|
r938 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
r988 | QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) : | ||
QAbstractSeriesPrivate(parent), | ||||
|
r938 | m_pieRelativeHorPos(0.5), | ||
m_pieRelativeVerPos(0.5), | ||||
m_pieRelativeSize(0.7), | ||||
m_pieStartAngle(0), | ||||
m_pieEndAngle(360), | ||||
|
r939 | m_sum(0), | ||
|
r1204 | m_mapper(0) | ||
|
r938 | { | ||
} | ||||
QPieSeriesPrivate::~QPieSeriesPrivate() | ||||
{ | ||||
} | ||||
void QPieSeriesPrivate::updateDerivativeData() | ||||
{ | ||||
|
r939 | m_sum = 0; | ||
|
r938 | |||
// nothing to do? | ||||
if (m_slices.count() == 0) | ||||
return; | ||||
|
r939 | // calculate sum of all slices | ||
|
r938 | foreach (QPieSlice* s, m_slices) | ||
|
r939 | m_sum += s->value(); | ||
|
r938 | |||
// nothing to show.. | ||||
|
r939 | if (qFuzzyIsNull(m_sum)) | ||
|
r938 | return; | ||
// update slice attributes | ||||
qreal sliceAngle = m_pieStartAngle; | ||||
qreal pieSpan = m_pieEndAngle - m_pieStartAngle; | ||||
QVector<QPieSlice*> changed; | ||||
foreach (QPieSlice* s, m_slices) { | ||||
PieSliceData data = PieSliceData::data(s); | ||||
|
r939 | data.m_percentage = s->value() / m_sum; | ||
|
r938 | data.m_angleSpan = pieSpan * data.m_percentage; | ||
data.m_startAngle = sliceAngle; | ||||
sliceAngle += data.m_angleSpan; | ||||
if (PieSliceData::data(s) != data) { | ||||
PieSliceData::data(s) = data; | ||||
changed << s; | ||||
} | ||||
} | ||||
// emit signals | ||||
foreach (QPieSlice* s, changed) | ||||
PieSliceData::data(s).emitChangedSignal(s); | ||||
} | ||||
|
r1009 | QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series) | ||
|
r938 | { | ||
|
r1009 | return series.d_func(); | ||
|
r938 | } | ||
|
r1009 | void QPieSeriesPrivate::sliceChanged() | ||
|
r938 | { | ||
|
r1009 | Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender()))); | ||
updateDerivativeData(); | ||||
|
r938 | } | ||
|
r1009 | void QPieSeriesPrivate::sliceClicked() | ||
|
r938 | { | ||
QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | ||||
Q_ASSERT(m_slices.contains(slice)); | ||||
Q_Q(QPieSeries); | ||||
|
r1009 | emit q->clicked(slice); | ||
|
r938 | } | ||
|
r1009 | void QPieSeriesPrivate::sliceHovered(bool state) | ||
|
r938 | { | ||
QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | ||||
Q_ASSERT(m_slices.contains(slice)); | ||||
Q_Q(QPieSeries); | ||||
|
r1009 | emit q->hovered(slice, state); | ||
|
r938 | } | ||
void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) | ||||
{ | ||||
|
r1166 | if (m_mapper) { | ||
for (int row = topLeft.row(); row <= bottomRight.row(); row++) { | ||||
for (int column = topLeft.column(); column <= bottomRight.column(); column++) { | ||||
if (m_mapper->orientation() == Qt::Vertical) | ||||
{ | ||||
if ( topLeft.row() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.row() < m_mapper->first() + m_mapper->count())) { | ||||
if (topLeft.column() == m_mapper->mapValues()) | ||||
m_slices.at(topLeft.row() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); | ||||
if (topLeft.column() == m_mapper->mapLabels()) | ||||
m_slices.at(topLeft.row() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | ||||
} | ||||
|
r1056 | } | ||
|
r1166 | else | ||
{ | ||||
if (topLeft.column() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.column() < m_mapper->first() + m_mapper->count())) { | ||||
if (topLeft.row() == m_mapper->mapValues()) | ||||
m_slices.at(topLeft.column() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); | ||||
if (topLeft.row() == m_mapper->mapLabels()) | ||||
m_slices.at(topLeft.column() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | ||||
} | ||||
|
r1056 | } | ||
|
r938 | } | ||
|
r1056 | } | ||
|
r938 | } | ||
|
r1056 | } | ||
|
r1081 | void QPieSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end) | ||
|
r1056 | { | ||
Q_UNUSED(parent); | ||||
|
r1166 | if (m_mapper) { | ||
if (m_mapper->orientation() == Qt::Vertical) | ||||
insertData(start, end); | ||||
else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie | ||||
initializePieFromModel(); | ||||
} | ||||
|
r1081 | } | ||
void QPieSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) | ||||
{ | ||||
Q_UNUSED(parent); | ||||
|
r1166 | if (m_mapper) { | ||
if (m_mapper->orientation() == Qt::Vertical) | ||||
removeData(start, end); | ||||
else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie | ||||
initializePieFromModel(); | ||||
} | ||||
|
r1081 | } | ||
void QPieSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) | ||||
{ | ||||
Q_UNUSED(parent); | ||||
|
r1166 | if (m_mapper) { | ||
if (m_mapper->orientation() == Qt::Horizontal) | ||||
insertData(start, end); | ||||
else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie | ||||
initializePieFromModel(); | ||||
} | ||||
|
r1081 | } | ||
void QPieSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) | ||||
{ | ||||
Q_UNUSED(parent); | ||||
|
r1166 | if (m_mapper) { | ||
if (m_mapper->orientation() == Qt::Horizontal) | ||||
removeData(start, end); | ||||
else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie | ||||
initializePieFromModel(); | ||||
} | ||||
|
r1081 | } | ||
void QPieSeriesPrivate::insertData(int start, int end) | ||||
{ | ||||
|
r1056 | Q_Q(QPieSeries); | ||
|
r1166 | if (m_mapper) { | ||
if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) { | ||||
return; | ||||
} else { | ||||
int addedCount = end - start + 1; | ||||
if (m_mapper->count() != -1 && addedCount > m_mapper->count()) | ||||
addedCount = m_mapper->count(); | ||||
int first = qMax(start, m_mapper->first()); | ||||
int last = qMin(first + addedCount - 1, m_mapper->orientation() == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1); | ||||
for (int i = first; i <= last; i++) { | ||||
QPieSlice *slice = new QPieSlice; | ||||
if (m_mapper->orientation() == Qt::Vertical) { | ||||
slice->setValue(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble()); | ||||
slice->setLabel(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString()); | ||||
} else { | ||||
slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble()); | ||||
slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString()); | ||||
} | ||||
slice->setLabelVisible(); | ||||
|
r1204 | q->insert(i - m_mapper->first(), slice); | ||
|
r938 | } | ||
|
r1166 | if (m_mapper->count() != -1 && m_slices.size() > m_mapper->count()) | ||
for (int i = m_slices.size() - 1; i >= m_mapper->count(); i--) | ||||
|
r1204 | q->remove(q->slices().at(i)); | ||
|
r1056 | } | ||
} | ||||
} | ||||
|
r1081 | void QPieSeriesPrivate::removeData(int start, int end) | ||
|
r1056 | { | ||
Q_Q(QPieSeries); | ||||
|
r1166 | if (m_mapper) { | ||
int removedCount = end - start + 1; | ||||
if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) { | ||||
return; | ||||
} else { | ||||
int toRemove = qMin(m_slices.size(), removedCount); // first find how many items can actually be removed | ||||
int first = qMax(start, m_mapper->first()); // get the index of the first item that will be removed. | ||||
int last = qMin(first + toRemove - 1, m_slices.size() + m_mapper->first() - 1); // get the index of the last item that will be removed. | ||||
for (int i = last; i >= first; i--) | ||||
|
r1204 | q->remove(q->slices().at(i - m_mapper->first())); | ||
|
r1166 | |||
if (m_mapper->count() != -1) { | ||||
int itemsAvailable; // check how many are available to be added | ||||
if (m_mapper->orientation() == Qt::Vertical) | ||||
itemsAvailable = m_model->rowCount() - m_mapper->first() - m_slices.size(); | ||||
else | ||||
itemsAvailable = m_model->columnCount() - m_mapper->first() - m_slices.size(); | ||||
int toBeAdded = qMin(itemsAvailable, m_mapper->count() - m_slices.size()); // add not more items than there is space left to be filled. | ||||
int currentSize = m_slices.size(); | ||||
if (toBeAdded > 0) | ||||
for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) { | ||||
QPieSlice *slice = new QPieSlice; | ||||
if (m_mapper->orientation() == Qt::Vertical) { | ||||
slice->setValue(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapValues()), Qt::DisplayRole).toDouble()); | ||||
slice->setLabel(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapLabels()), Qt::DisplayRole).toString()); | ||||
} else { | ||||
slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i + m_mapper->first()), Qt::DisplayRole).toDouble()); | ||||
slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i + m_mapper->first()), Qt::DisplayRole).toString()); | ||||
} | ||||
slice->setLabelVisible(); | ||||
|
r1204 | q->insert(i, slice); | ||
|
r1063 | } | ||
|
r1166 | } | ||
|
r1056 | } | ||
} | ||||
} | ||||
void QPieSeriesPrivate::initializePieFromModel() | ||||
{ | ||||
Q_Q(QPieSeries); | ||||
|
r1173 | |||
|
r1056 | // clear current content | ||
|
r1204 | q->clear(); | ||
|
r1056 | |||
|
r1184 | if (m_model == 0 || m_mapper == 0) | ||
return; | ||||
|
r1180 | // check if mappings are set | ||
if (m_mapper->mapValues() == -1 || m_mapper->mapLabels() == -1) | ||||
return; | ||||
|
r1056 | // create the initial slices set | ||
|
r1164 | if (m_mapper->orientation() == Qt::Vertical) { | ||
if (m_mapper->mapValues() >= m_model->columnCount() || m_mapper->mapLabels() >= m_model->columnCount()) | ||||
|
r1086 | return; // mapped columns are not existing | ||
|
r1056 | int sliceCount = 0; | ||
|
r1164 | if(m_mapper->count() == -1) | ||
sliceCount = m_model->rowCount() - m_mapper->first(); | ||||
|
r1056 | else | ||
|
r1164 | sliceCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first()); | ||
for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++) | ||||
|
r1206 | q->append(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString(), m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble()); | ||
|
r1056 | } else { | ||
|
r1164 | if (m_mapper->mapValues() >= m_model->rowCount() || m_mapper->mapLabels() >= m_model->rowCount()) | ||
|
r1086 | return; // mapped columns are not existing | ||
|
r1056 | int sliceCount = 0; | ||
|
r1164 | if(m_mapper->count() == -1) | ||
sliceCount = m_model->columnCount() - m_mapper->first(); | ||||
|
r1056 | else | ||
|
r1164 | sliceCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first()); | ||
for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++) | ||||
|
r1206 | q->append(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString(), m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble()); | ||
|
r938 | } | ||
|
r1081 | q->setLabelsVisible(true); | ||
|
r938 | } | ||
bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min) | ||||
{ | ||||
// Remove rounding errors | ||||
qreal roundedValue = newValue; | ||||
if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue)) | ||||
roundedValue = 0.0; | ||||
else if (qFuzzyCompare(newValue, max)) | ||||
roundedValue = max; | ||||
else if (qFuzzyCompare(newValue, min)) | ||||
roundedValue = min; | ||||
// Check if the position is valid after removing the rounding errors | ||||
if (roundedValue < min || roundedValue > max) { | ||||
qWarning("QPieSeries: Illegal value"); | ||||
return false; | ||||
} | ||||
if (!qFuzzyIsNull(value - roundedValue)) { | ||||
value = roundedValue; | ||||
return true; | ||||
} | ||||
// The change was so small it is considered a rounding error | ||||
return false; | ||||
} | ||||
|
r943 | void QPieSeriesPrivate::scaleDomain(Domain& domain) | ||
{ | ||||
Q_UNUSED(domain); | ||||
|
r1191 | // does not apply to pie | ||
|
r943 | } | ||
Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter) | ||||
{ | ||||
Q_Q(QPieSeries); | ||||
PieChartItem* pie = new PieChartItem(q,presenter); | ||||
if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { | ||||
presenter->animator()->addAnimation(pie); | ||||
} | ||||
presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); | ||||
return pie; | ||||
} | ||||
|
r938 | |||
|
r950 | QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend) | ||
{ | ||||
Q_Q(QPieSeries); | ||||
QList<LegendMarker*> markers; | ||||
foreach(QPieSlice* slice, q->slices()) { | ||||
PieLegendMarker* marker = new PieLegendMarker(q,slice,legend); | ||||
markers << marker; | ||||
} | ||||
return markers; | ||||
} | ||||
|
r142 | #include "moc_qpieseries.cpp" | ||
|
r938 | #include "moc_qpieseries_p.cpp" | ||
|
r142 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||