qpieseries.cpp
738 lines
| 19.4 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
Jani Honkonen
|
r142 | #include "qpieseries.h" | ||
Michal Klocek
|
r938 | #include "qpieseries_p.h" | ||
Jani Honkonen
|
r818 | #include "qpieslice.h" | ||
#include "pieslicedata_p.h" | ||||
Michal Klocek
|
r943 | #include "chartdataset_p.h" | ||
#include "charttheme_p.h" | ||||
#include "chartanimator_p.h" | ||||
Michal Klocek
|
r950 | #include "legendmarker_p.h" | ||
Marek Rosa
|
r862 | #include <QAbstractItemModel> | ||
Jani Honkonen
|
r142 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Jani Honkonen
|
r320 | /*! | ||
\class QPieSeries | ||||
\brief Pie series API for QtCommercial Charts | ||||
Jani Honkonen
|
r952 | The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects. | ||
Jani Honkonen
|
r320 | The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices. | ||
Jani Honkonen
|
r386 | The actual slice size is determined by that relative value. | ||
Jani Honkonen
|
r320 | |||
Jani Honkonen
|
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. | ||||
Jani Honkonen
|
r320 | This can be done by setting a starting angle and angle span to the series. | ||
Jani Honkonen
|
r952 | Full pie is 360 degrees where 0 is at 12 a'clock. | ||
Jani Honkonen
|
r320 | */ | ||
Marek Rosa
|
r924 | /*! | ||
Jani Honkonen
|
r928 | \property QPieSeries::horizontalPosition | ||
Jani Honkonen
|
r932 | \brief Defines the horizontal position of the pie. | ||
Marek Rosa
|
r924 | |||
Jani Honkonen
|
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). | ||||
Marek Rosa
|
r924 | */ | ||
Jani Honkonen
|
r928 | /*! | ||
\property QPieSeries::verticalPosition | ||||
Jani Honkonen
|
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 | ||||
Jani Honkonen
|
r928 | |||
Jani Honkonen
|
r932 | Default value is 0.5 (center). | ||
Jani Honkonen
|
r928 | */ | ||
/*! | ||||
\property QPieSeries::size | ||||
Jani Honkonen
|
r932 | \brief Defines the pie size. | ||
Jani Honkonen
|
r928 | |||
Jani Honkonen
|
r932 | The value is a relative value to the chart rectangle where: | ||
\list | ||||
\o 0.0 is the minumum size (pie not drawn). | ||||
\o 1.0 is the maximum size that can fit the chart. | ||||
\endlist | ||||
Default value is 0.7. | ||||
Jani Honkonen
|
r928 | */ | ||
/*! | ||||
\property QPieSeries::startAngle | ||||
Jani Honkonen
|
r932 | \brief Defines the starting angle of the pie. | ||
Jani Honkonen
|
r928 | |||
Jani Honkonen
|
r932 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. | ||
Jani Honkonen
|
r928 | |||
Jani Honkonen
|
r932 | Default is value is 0. | ||
*/ | ||||
Jani Honkonen
|
r928 | |||
/*! | ||||
\property QPieSeries::endAngle | ||||
Jani Honkonen
|
r932 | \brief Defines the ending angle of the pie. | ||
Jani Honkonen
|
r928 | |||
Jani Honkonen
|
r932 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. | ||
Jani Honkonen
|
r928 | |||
Jani Honkonen
|
r932 | Default is value is 360. | ||
*/ | ||||
Jani Honkonen
|
r928 | |||
Marek Rosa
|
r924 | |||
Jani Honkonen
|
r314 | /*! | ||
Constructs a series object which is a child of \a parent. | ||||
*/ | ||||
Jani Honkonen
|
r203 | QPieSeries::QPieSeries(QObject *parent) : | ||
Michal Klocek
|
r938 | QSeries(*new QPieSeriesPrivate(this),parent) | ||
Jani Honkonen
|
r203 | { | ||
Jani Honkonen
|
r157 | |||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r933 | Destroys the series and its slices. | ||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | QPieSeries::~QPieSeries() | ||
{ | ||||
Jani Honkonen
|
r669 | // NOTE: d_prt destroyed by QObject | ||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r320 | Returns QChartSeries::SeriesTypePie. | ||
Jani Honkonen
|
r314 | */ | ||
Michal Klocek
|
r360 | QSeries::QSeriesType QPieSeries::type() const | ||
Jani Honkonen
|
r314 | { | ||
Michal Klocek
|
r360 | return QSeries::SeriesTypePie; | ||
Jani Honkonen
|
r314 | } | ||
/*! | ||||
Jani Honkonen
|
r354 | Sets an array of \a slices to the series replacing the existing slices. | ||
Jani Honkonen
|
r314 | Slice ownership is passed to the series. | ||
*/ | ||||
Jani Honkonen
|
r354 | void QPieSeries::replace(QList<QPieSlice*> slices) | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r203 | clear(); | ||
Jani Honkonen
|
r796 | append(slices); | ||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r163 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r933 | Appends an array of \a slices to the series. | ||
Jani Honkonen
|
r314 | Slice ownership is passed to the series. | ||
*/ | ||||
Jani Honkonen
|
r796 | void QPieSeries::append(QList<QPieSlice*> slices) | ||
Jani Honkonen
|
r203 | { | ||
Jani Honkonen
|
r669 | Q_D(QPieSeries); | ||
Jani Honkonen
|
r203 | foreach (QPieSlice* s, slices) { | ||
s->setParent(this); | ||||
Jani Honkonen
|
r669 | d->m_slices << s; | ||
Jani Honkonen
|
r174 | } | ||
Jani Honkonen
|
r157 | |||
Jani Honkonen
|
r669 | d->updateDerivativeData(); | ||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r203 | foreach (QPieSlice* s, slices) { | ||
Jani Honkonen
|
r669 | connect(s, SIGNAL(changed()), d, SLOT(sliceChanged())); | ||
Jani Honkonen
|
r707 | connect(s, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons))); | ||
Jani Honkonen
|
r669 | connect(s, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter())); | ||
connect(s, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave())); | ||||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r621 | emit added(slices); | ||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r933 | Appends a single \a slice to the series. | ||
Jani Honkonen
|
r314 | Slice ownership is passed to the series. | ||
*/ | ||||
Jani Honkonen
|
r796 | void QPieSeries::append(QPieSlice* slice) | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r796 | append(QList<QPieSlice*>() << slice); | ||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r415 | /*! | ||
Jani Honkonen
|
r933 | Appends a single \a slice to the series and returns a reference to the series. | ||
Jani Honkonen
|
r415 | Slice ownership is passed to the series. | ||
*/ | ||||
Jani Honkonen
|
r406 | QPieSeries& QPieSeries::operator << (QPieSlice* slice) | ||
{ | ||||
Jani Honkonen
|
r796 | append(slice); | ||
Jani Honkonen
|
r406 | return *this; | ||
} | ||||
Jani Honkonen
|
r314 | |||
/*! | ||||
Jani Honkonen
|
r809 | Appends a single slice to the series with give \a value and \a name. | ||
Jani Honkonen
|
r314 | Slice ownership is passed to the series. | ||
*/ | ||||
Jani Honkonen
|
r796 | QPieSlice* QPieSeries::append(qreal value, QString name) | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r203 | QPieSlice* slice = new QPieSlice(value, name); | ||
Jani Honkonen
|
r796 | append(slice); | ||
Jani Honkonen
|
r203 | return slice; | ||
} | ||||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r809 | /*! | ||
Inserts a single \a slice to the series before the slice at \a index position. | ||||
Slice ownership is passed to the series. | ||||
*/ | ||||
void QPieSeries::insert(int index, QPieSlice* slice) | ||||
Marek Rosa
|
r604 | { | ||
Jani Honkonen
|
r669 | Q_D(QPieSeries); | ||
Jani Honkonen
|
r809 | Q_ASSERT(index <= d->m_slices.count()); | ||
Marek Rosa
|
r604 | slice->setParent(this); | ||
Jani Honkonen
|
r809 | d->m_slices.insert(index, slice); | ||
Marek Rosa
|
r604 | |||
Jani Honkonen
|
r669 | d->updateDerivativeData(); | ||
Marek Rosa
|
r604 | |||
Jani Honkonen
|
r669 | connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged())); | ||
Jani Honkonen
|
r819 | connect(slice, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons))); | ||
Jani Honkonen
|
r669 | connect(slice, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter())); | ||
connect(slice, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave())); | ||||
Marek Rosa
|
r604 | |||
Jani Honkonen
|
r621 | emit added(QList<QPieSlice*>() << slice); | ||
Marek Rosa
|
r604 | } | ||
Jani Honkonen
|
r314 | /*! | ||
Removes a single \a slice from the series and deletes the slice. | ||||
Jani Honkonen
|
r320 | |||
Jani Honkonen
|
r952 | Do not reference the pointer after this call. | ||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | void QPieSeries::remove(QPieSlice* slice) | ||
{ | ||||
Jani Honkonen
|
r669 | Q_D(QPieSeries); | ||
if (!d->m_slices.removeOne(slice)) { | ||||
Jani Honkonen
|
r289 | Q_ASSERT(0); // TODO: how should this be reported? | ||
Jani Honkonen
|
r203 | return; | ||
} | ||||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r669 | d->updateDerivativeData(); | ||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r621 | emit removed(QList<QPieSlice*>() << slice); | ||
Jani Honkonen
|
r203 | delete slice; | ||
Jani Honkonen
|
r823 | slice = 0; | ||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r314 | /*! | ||
Clears all slices from the series. | ||||
*/ | ||||
Jani Honkonen
|
r203 | void QPieSeries::clear() | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r669 | Q_D(QPieSeries); | ||
if (d->m_slices.count() == 0) | ||||
Jani Honkonen
|
r203 | return; | ||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r669 | QList<QPieSlice*> slices = d->m_slices; | ||
foreach (QPieSlice* s, d->m_slices) { | ||||
d->m_slices.removeOne(s); | ||||
Jani Honkonen
|
r203 | delete s; | ||
} | ||||
Jani Honkonen
|
r566 | |||
Jani Honkonen
|
r669 | d->updateDerivativeData(); | ||
Jani Honkonen
|
r621 | |||
emit removed(slices); | ||||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r933 | returns the number of the slices in this series. | ||
Jani Honkonen
|
r314 | */ | ||
int QPieSeries::count() const | ||||
{ | ||||
Jani Honkonen
|
r669 | Q_D(const QPieSeries); | ||
return d->m_slices.count(); | ||||
Jani Honkonen
|
r314 | } | ||
Jani Honkonen
|
r621 | /*! | ||
Returns true is the series is empty. | ||||
*/ | ||||
bool QPieSeries::isEmpty() const | ||||
{ | ||||
Jani Honkonen
|
r669 | Q_D(const QPieSeries); | ||
return d->m_slices.isEmpty(); | ||||
Jani Honkonen
|
r621 | } | ||
Jani Honkonen
|
r314 | /*! | ||
Returns a list of slices that belong to this series. | ||||
*/ | ||||
QList<QPieSlice*> QPieSeries::slices() const | ||||
{ | ||||
Jani Honkonen
|
r669 | Q_D(const QPieSeries); | ||
return d->m_slices; | ||||
Jani Honkonen
|
r314 | } | ||
Tero Ahola
|
r884 | void QPieSeries::setHorizontalPosition(qreal relativePosition) | ||
{ | ||||
Q_D(QPieSeries); | ||||
if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0)) | ||||
emit piePositionChanged(); | ||||
} | ||||
Jani Honkonen
|
r454 | |||
Tero Ahola
|
r884 | void QPieSeries::setVerticalPosition(qreal relativePosition) | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r669 | Q_D(QPieSeries); | ||
Tero Ahola
|
r884 | if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0)) | ||
Jani Honkonen
|
r621 | emit piePositionChanged(); | ||
Jani Honkonen
|
r142 | } | ||
Tero Ahola
|
r884 | qreal QPieSeries::horizontalPosition() const | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r669 | Q_D(const QPieSeries); | ||
return d->m_pieRelativeHorPos; | ||||
Jani Honkonen
|
r142 | } | ||
Tero Ahola
|
r884 | qreal QPieSeries::verticalPosition() const | ||
Jani Honkonen
|
r314 | { | ||
Jani Honkonen
|
r669 | Q_D(const QPieSeries); | ||
return d->m_pieRelativeVerPos; | ||||
Jani Honkonen
|
r314 | } | ||
Jani Honkonen
|
r498 | void QPieSeries::setPieSize(qreal relativeSize) | ||
Jani Honkonen
|
r437 | { | ||
Jani Honkonen
|
r669 | Q_D(QPieSeries); | ||
Tero Ahola
|
r884 | if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0)) | ||
Jani Honkonen
|
r621 | emit pieSizeChanged(); | ||
Jani Honkonen
|
r437 | } | ||
Jani Honkonen
|
r314 | |||
Jani Honkonen
|
r498 | qreal QPieSeries::pieSize() const | ||
Jani Honkonen
|
r203 | { | ||
Jani Honkonen
|
r669 | Q_D(const QPieSeries); | ||
return d->m_pieRelativeSize; | ||||
Jani Honkonen
|
r437 | } | ||
Jani Honkonen
|
r498 | void QPieSeries::setPieStartAngle(qreal angle) | ||
Jani Honkonen
|
r437 | { | ||
Jani Honkonen
|
r669 | Q_D(QPieSeries); | ||
Tero Ahola
|
r884 | if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle)) | ||
Jani Honkonen
|
r669 | d->updateDerivativeData(); | ||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r498 | qreal QPieSeries::pieStartAngle() const | ||
Jani Honkonen
|
r437 | { | ||
Jani Honkonen
|
r669 | Q_D(const QPieSeries); | ||
return d->m_pieStartAngle; | ||||
Jani Honkonen
|
r437 | } | ||
Jani Honkonen
|
r454 | /*! | ||
Sets the end angle of the pie. | ||||
Full pie is 360 degrees where 0 degrees is at 12 a'clock. | ||||
Jani Honkonen
|
r498 | \a angle must be greater than start angle. | ||
Jani Honkonen
|
r454 | |||
Jani Honkonen
|
r498 | \sa pieEndAngle(), pieStartAngle(), setPieStartAngle() | ||
Jani Honkonen
|
r454 | */ | ||
Jani Honkonen
|
r498 | void QPieSeries::setPieEndAngle(qreal angle) | ||
Jani Honkonen
|
r437 | { | ||
Jani Honkonen
|
r669 | Q_D(QPieSeries); | ||
Jani Honkonen
|
r765 | |||
Tero Ahola
|
r884 | if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle)) | ||
Jani Honkonen
|
r669 | d->updateDerivativeData(); | ||
Jani Honkonen
|
r437 | } | ||
Jani Honkonen
|
r454 | /*! | ||
Returns the end angle of the pie. | ||||
Full pie is 360 degrees where 0 degrees is at 12 a'clock. | ||||
Jani Honkonen
|
r498 | \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle() | ||
Jani Honkonen
|
r454 | */ | ||
Jani Honkonen
|
r498 | qreal QPieSeries::pieEndAngle() const | ||
Jani Honkonen
|
r437 | { | ||
Jani Honkonen
|
r669 | Q_D(const QPieSeries); | ||
return d->m_pieEndAngle; | ||||
Jani Honkonen
|
r437 | } | ||
Jani Honkonen
|
r314 | /*! | ||
Sets the all the slice labels \a visible or invisible. | ||||
\sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible() | ||||
*/ | ||||
Jani Honkonen
|
r203 | void QPieSeries::setLabelsVisible(bool visible) | ||
{ | ||||
Jani Honkonen
|
r669 | Q_D(QPieSeries); | ||
foreach (QPieSlice* s, d->m_slices) | ||||
Jani Honkonen
|
r203 | s->setLabelVisible(visible); | ||
} | ||||
Jani Honkonen
|
r406 | /*! | ||
Returns the sum of all slice values in this series. | ||||
Jani Honkonen
|
r952 | \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage() | ||
Jani Honkonen
|
r406 | */ | ||
Jani Honkonen
|
r939 | qreal QPieSeries::sum() const | ||
Jani Honkonen
|
r406 | { | ||
Jani Honkonen
|
r669 | Q_D(const QPieSeries); | ||
Jani Honkonen
|
r939 | return d->m_sum; | ||
Jani Honkonen
|
r406 | } | ||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r809 | \fn void QPieSeries::clicked(QPieSlice* slice, Qt::MouseButtons buttons) | ||
Jani Honkonen
|
r314 | |||
Jani Honkonen
|
r809 | This signal is emitted when a \a slice has been clicked with mouse \a buttons. | ||
Jani Honkonen
|
r314 | |||
\sa QPieSlice::clicked() | ||||
*/ | ||||
/*! | ||||
\fn void QPieSeries::hoverEnter(QPieSlice* slice) | ||||
This signal is emitted when user has hovered over a \a slice. | ||||
\sa QPieSlice::hoverEnter() | ||||
*/ | ||||
/*! | ||||
\fn void QPieSeries::hoverLeave(QPieSlice* slice) | ||||
This signal is emitted when user has hovered away from a \a slice. | ||||
\sa QPieSlice::hoverLeave() | ||||
*/ | ||||
Jani Honkonen
|
r809 | /*! | ||
\fn void QPieSeries::added(QList<QPieSlice*> slices) | ||||
This signal is emitted when \a slices has been added to the series. | ||||
\sa append(), insert() | ||||
*/ | ||||
/*! | ||||
\fn void QPieSeries::removed(QList<QPieSlice*> slices) | ||||
This signal is emitted when \a slices has been removed from the series. | ||||
\sa remove(), clear() | ||||
*/ | ||||
/*! | ||||
\fn void QPieSeries::piePositionChanged() | ||||
This signal is emitted when pie position has changed. | ||||
Marek Rosa
|
r909 | \sa verticalPosition(), setVerticalPosition(), horizontalPosition(), setHorizontalPosition() | ||
Jani Honkonen
|
r809 | */ | ||
/*! | ||||
\fn void QPieSeries::pieSizeChanged() | ||||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r809 | This signal is emitted when pie size has changed. | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r809 | \sa pieSize(), setPieSize() | ||
*/ | ||||
Jani Honkonen
|
r174 | |||
Marek Rosa
|
r879 | /*! | ||
\fn bool QPieSeries::setModel(QAbstractItemModel *model) | ||||
Sets the \a model to be used as a data source | ||||
*/ | ||||
Marek Rosa
|
r597 | bool QPieSeries::setModel(QAbstractItemModel* model) | ||
{ | ||||
Jani Honkonen
|
r669 | Q_D(QPieSeries); | ||
Marek Rosa
|
r597 | // disconnect signals from old model | ||
Michal Klocek
|
r938 | if(d->m_model) | ||
Marek Rosa
|
r597 | { | ||
Michal Klocek
|
r938 | disconnect(d->m_model, 0, this, 0); | ||
Jani Honkonen
|
r669 | d->m_mapValues = -1; | ||
d->m_mapLabels = -1; | ||||
d->m_mapOrientation = Qt::Vertical; | ||||
Marek Rosa
|
r597 | } | ||
Marek Rosa
|
r630 | // set new model | ||
Marek Rosa
|
r597 | if(model) | ||
{ | ||||
Michal Klocek
|
r938 | d->m_model = model; | ||
Marek Rosa
|
r630 | return true; | ||
} | ||||
else | ||||
{ | ||||
Michal Klocek
|
r938 | d->m_model = 0; | ||
Marek Rosa
|
r630 | return false; | ||
Marek Rosa
|
r597 | } | ||
} | ||||
Marek Rosa
|
r900 | /*! | ||
\fn bool QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation) | ||||
Sets column/row specified by \a modelValuesLine to be used as a list of pie slice values for the pie. | ||||
Parameter \a modelValuesLine indicates the column/row where the values for the pie slices are located in the model. | ||||
Parameter \a modelLabelsLine indicates the column/row where the labels for the pie slices are located in the model. | ||||
The \a orientation paramater specifies whether the data is in columns or in rows. | ||||
*/ | ||||
Marek Rosa
|
r597 | void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation) | ||
{ | ||||
Jani Honkonen
|
r669 | Q_D(QPieSeries); | ||
Michal Klocek
|
r938 | if (d->m_model == 0) | ||
Marek Rosa
|
r630 | return; | ||
Jani Honkonen
|
r669 | |||
d->m_mapValues = modelValuesLine; | ||||
d->m_mapLabels = modelLabelsLine; | ||||
d->m_mapOrientation = orientation; | ||||
Marek Rosa
|
r597 | |||
Marek Rosa
|
r630 | // connect the signals | ||
Jani Honkonen
|
r669 | if (d->m_mapOrientation == Qt::Vertical) { | ||
Michal Klocek
|
r938 | connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex))); | ||
connect(d->m_model, SIGNAL(rowsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int))); | ||||
connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int))); | ||||
Jani Honkonen
|
r669 | } else { | ||
Michal Klocek
|
r938 | connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex))); | ||
connect(d->m_model, SIGNAL(columnsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int))); | ||||
connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int))); | ||||
Marek Rosa
|
r630 | } | ||
Marek Rosa
|
r597 | |||
Marek Rosa
|
r630 | // create the initial slices set | ||
Jani Honkonen
|
r669 | if (d->m_mapOrientation == Qt::Vertical) { | ||
Michal Klocek
|
r938 | for (int i = 0; i < d->m_model->rowCount(); i++) | ||
append(d->m_model->data(d->m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString()); | ||||
Jani Honkonen
|
r669 | } else { | ||
Michal Klocek
|
r938 | for (int i = 0; i < d->m_model->columnCount(); i++) | ||
append(d->m_model->data(d->m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString()); | ||||
Marek Rosa
|
r604 | } | ||
Marek Rosa
|
r597 | } | ||
Michal Klocek
|
r938 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) | ||||
:QSeriesPrivate(parent), | ||||
m_pieRelativeHorPos(0.5), | ||||
m_pieRelativeVerPos(0.5), | ||||
m_pieRelativeSize(0.7), | ||||
m_pieStartAngle(0), | ||||
m_pieEndAngle(360), | ||||
Jani Honkonen
|
r939 | m_sum(0), | ||
Michal Klocek
|
r938 | m_mapValues(0), | ||
m_mapLabels(0), | ||||
m_mapOrientation(Qt::Horizontal) | ||||
{ | ||||
} | ||||
QPieSeriesPrivate::~QPieSeriesPrivate() | ||||
{ | ||||
} | ||||
void QPieSeriesPrivate::updateDerivativeData() | ||||
{ | ||||
Jani Honkonen
|
r939 | m_sum = 0; | ||
Michal Klocek
|
r938 | |||
// nothing to do? | ||||
if (m_slices.count() == 0) | ||||
return; | ||||
Jani Honkonen
|
r939 | // calculate sum of all slices | ||
Michal Klocek
|
r938 | foreach (QPieSlice* s, m_slices) | ||
Jani Honkonen
|
r939 | m_sum += s->value(); | ||
Michal Klocek
|
r938 | |||
// nothing to show.. | ||||
Jani Honkonen
|
r939 | if (qFuzzyIsNull(m_sum)) | ||
Michal Klocek
|
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); | ||||
Jani Honkonen
|
r939 | data.m_percentage = s->value() / m_sum; | ||
Michal Klocek
|
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); | ||||
} | ||||
void QPieSeriesPrivate::sliceChanged() | ||||
{ | ||||
Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender()))); | ||||
updateDerivativeData(); | ||||
} | ||||
void QPieSeriesPrivate::sliceClicked(Qt::MouseButtons buttons) | ||||
{ | ||||
QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | ||||
Q_ASSERT(m_slices.contains(slice)); | ||||
Q_Q(QPieSeries); | ||||
emit q->clicked(slice, buttons); | ||||
} | ||||
void QPieSeriesPrivate::sliceHoverEnter() | ||||
{ | ||||
QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | ||||
Q_ASSERT(m_slices.contains(slice)); | ||||
Q_Q(QPieSeries); | ||||
emit q->hoverEnter(slice); | ||||
} | ||||
void QPieSeriesPrivate::sliceHoverLeave() | ||||
{ | ||||
QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | ||||
Q_ASSERT(m_slices.contains(slice)); | ||||
Q_Q(QPieSeries); | ||||
emit q->hoverLeave(slice); | ||||
} | ||||
void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) | ||||
{ | ||||
Q_UNUSED(bottomRight) | ||||
if (m_mapOrientation == Qt::Vertical) | ||||
{ | ||||
if (topLeft.column() == m_mapValues) | ||||
if (m_mapValues == m_mapLabels) | ||||
{ | ||||
m_slices.at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); | ||||
m_slices.at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | ||||
} | ||||
else | ||||
{ | ||||
m_slices.at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); | ||||
} | ||||
else if (topLeft.column() == m_mapLabels) | ||||
m_slices.at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | ||||
} | ||||
else | ||||
{ | ||||
if (topLeft.row() == m_mapValues) | ||||
if (m_mapValues == m_mapLabels) | ||||
{ | ||||
m_slices.at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); | ||||
m_slices.at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | ||||
} | ||||
else | ||||
{ | ||||
m_slices.at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); | ||||
} | ||||
else if (topLeft.row() == m_mapLabels) | ||||
m_slices.at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | ||||
} | ||||
} | ||||
void QPieSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end) | ||||
{ | ||||
Q_UNUSED(parent) | ||||
Q_UNUSED(end) | ||||
Q_Q(QPieSeries); | ||||
QPieSlice* newSlice = new QPieSlice; | ||||
newSlice->setLabelVisible(true); | ||||
if (m_mapOrientation == Qt::Vertical) | ||||
{ | ||||
newSlice->setValue(m_model->data(m_model->index(start, m_mapValues), Qt::DisplayRole).toDouble()); | ||||
newSlice->setLabel(m_model->data(m_model->index(start, m_mapLabels), Qt::DisplayRole).toString()); | ||||
} | ||||
else | ||||
{ | ||||
newSlice->setValue(m_model->data(m_model->index(m_mapValues, start), Qt::DisplayRole).toDouble()); | ||||
newSlice->setLabel(m_model->data(m_model->index(m_mapLabels, start), Qt::DisplayRole).toString()); | ||||
} | ||||
q->insert(start, newSlice); | ||||
} | ||||
void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end) | ||||
{ | ||||
Q_UNUSED(parent) | ||||
Q_UNUSED(end) | ||||
Q_Q(QPieSeries); | ||||
q->remove(m_slices.at(start)); | ||||
} | ||||
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; | ||||
} | ||||
Michal Klocek
|
r943 | void QPieSeriesPrivate::scaleDomain(Domain& domain) | ||
{ | ||||
Q_UNUSED(domain); | ||||
#ifndef QT_NO_DEBUG | ||||
qWarning() << __FILE__<<__FUNCTION__<<"not implemented"; | ||||
#endif | ||||
} | ||||
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; | ||||
} | ||||
Michal Klocek
|
r938 | |||
Michal Klocek
|
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; | ||||
} | ||||
Jani Honkonen
|
r142 | #include "moc_qpieseries.cpp" | ||
Michal Klocek
|
r938 | #include "moc_qpieseries_p.cpp" | ||
Jani Honkonen
|
r142 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||