qpieslice.cpp
408 lines
| 8.5 KiB
| text/x-c
|
CppLexer
|
r203 | #include "qpieslice.h" | ||
|
r669 | #include "qpiesliceprivate_p.h" | ||
|
r618 | #include "qpieseries.h" | ||
|
r669 | #include "qpieseriesprivate_p.h" | ||
|
r203 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r314 | /*! | ||
\class QPieSlice | ||||
|
r339 | \brief Defines a slice in pie series. | ||
|
r314 | |||
|
r320 | Holds all the data of a single slice in a QPieSeries and provides the means | ||
to modify slice data and customize the visual appearance of the slice. | ||||
It also provides the means to customize user interaction with the slice by | ||||
providing signals for clicking and hover events. | ||||
|
r314 | */ | ||
/*! | ||||
\property QPieSlice::label | ||||
Label of the slice. | ||||
*/ | ||||
/*! | ||||
\property QPieSlice::value | ||||
Value of the slice. | ||||
*/ | ||||
/*! | ||||
Constructs an empty slice with a \a parent. | ||||
|
r320 | |||
|
r314 | Note that QPieSeries takes ownership of the slice when it is set/added. | ||
|
r320 | |||
|
r361 | \sa QPieSeries::replace(), QPieSeries::add() | ||
|
r314 | */ | ||
|
r203 | QPieSlice::QPieSlice(QObject *parent) | ||
:QObject(parent), | ||||
|
r669 | d_ptr(new QPieSlicePrivate(this)) | ||
|
r203 | { | ||
} | ||||
|
r314 | /*! | ||
Constructs an empty slice with given \a value, \a label and a \a parent. | ||||
Note that QPieSeries takes ownership of the slice when it is set/added. | ||||
|
r361 | \sa QPieSeries::replace(), QPieSeries::add() | ||
|
r314 | */ | ||
QPieSlice::QPieSlice(qreal value, QString label, QObject *parent) | ||||
|
r203 | :QObject(parent), | ||
|
r669 | d_ptr(new QPieSlicePrivate(this)) | ||
|
r203 | { | ||
|
r669 | Q_D(QPieSlice); | ||
d->m_data.m_value = value; | ||||
d->m_data.m_labelText = label; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
Destroys the slice. | ||||
User should not delete the slice if it has been added to the series. | ||||
*/ | ||||
|
r203 | QPieSlice::~QPieSlice() | ||
{ | ||||
|
r669 | delete d_ptr; | ||
|
r203 | } | ||
|
r314 | /*! | ||
Gets the value of the slice. | ||||
Note that all values in the series | ||||
\sa setValue() | ||||
*/ | ||||
|
r203 | qreal QPieSlice::value() const | ||
{ | ||||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_value; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
Gets the label of the slice. | ||||
\sa setLabel() | ||||
*/ | ||||
|
r203 | QString QPieSlice::label() const | ||
{ | ||||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_labelText; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
Returns true if label is set as visible. | ||||
\sa setLabelVisible() | ||||
*/ | ||||
|
r203 | bool QPieSlice::isLabelVisible() const | ||
{ | ||||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_labelVisible; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
Returns true if slice is exloded from the pie. | ||||
|
r454 | \sa setExploded(), setExplodeDistanceFactor() | ||
|
r314 | */ | ||
|
r203 | bool QPieSlice::isExploded() const | ||
{ | ||||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_isExploded; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
|
r454 | Returns the explode distance factor. | ||
The factor is relative to pie radius. For example: | ||||
1.0 means the distance is the same as the radius. | ||||
0.5 means the distance is half of the radius. | ||||
Default value is 0.15. | ||||
\sa setExplodeDistanceFactor() | ||||
|
r314 | */ | ||
|
r454 | qreal QPieSlice::explodeDistanceFactor() const | ||
|
r289 | { | ||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_explodeDistanceFactor; | ||||
|
r289 | } | ||
|
r314 | /*! | ||
Returns the percentage of this slice compared to all slices in the same series. | ||||
|
r386 | The returned value ranges from 0 to 1.0. | ||
|
r320 | |||
|
r314 | Updated internally after the slice is added to the series. | ||
*/ | ||||
|
r203 | qreal QPieSlice::percentage() const | ||
{ | ||||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_percentage; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
Returns the starting angle of this slice in the series it belongs to. | ||||
|
r320 | |||
Full pie is 360 degrees where 0 degrees is at 12 a'clock. | ||||
|
r314 | Updated internally after the slice is added to the series. | ||
*/ | ||||
|
r355 | qreal QPieSlice::startAngle() const | ||
|
r203 | { | ||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_startAngle; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
|
r355 | Returns the end angle of this slice in the series it belongs to. | ||
|
r320 | |||
Full pie is 360 degrees where 0 degrees is at 12 a'clock. | ||||
|
r314 | Updated internally after the slice is added to the series. | ||
*/ | ||||
|
r355 | qreal QPieSlice::endAngle() const | ||
|
r203 | { | ||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_startAngle + d->m_data.m_angleSpan; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
Returns the pen used to draw this slice. | ||||
|
r469 | \sa setSlicePen() | ||
|
r314 | */ | ||
|
r469 | QPen QPieSlice::slicePen() const | ||
|
r203 | { | ||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_slicePen; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
Returns the brush used to draw this slice. | ||||
|
r469 | \sa setSliceBrush() | ||
|
r314 | */ | ||
|
r469 | QBrush QPieSlice::sliceBrush() const | ||
|
r203 | { | ||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_sliceBrush; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
|
r469 | Returns the pen used to draw label arm in this slice. | ||
\sa setLabelArmPen() | ||||
|
r314 | */ | ||
|
r469 | QPen QPieSlice::labelArmPen() const | ||
|
r203 | { | ||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_labelArmPen; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
Returns the font used to draw label in this slice. | ||||
\sa setLabelFont() | ||||
*/ | ||||
|
r203 | QFont QPieSlice::labelFont() const | ||
{ | ||||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_labelFont; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
|
r454 | Gets the label arm lenght factor. | ||
The factor is relative to pie radius. For example: | ||||
1.0 means the length is the same as the radius. | ||||
0.5 means the length is half of the radius. | ||||
Default value is 0.15 | ||||
\sa setLabelArmLengthFactor() | ||||
|
r314 | */ | ||
|
r454 | qreal QPieSlice::labelArmLengthFactor() const | ||
|
r203 | { | ||
|
r669 | Q_D(const QPieSlice); | ||
return d->m_data.m_labelArmLengthFactor; | ||||
|
r203 | } | ||
|
r314 | /*! | ||
\fn void QPieSlice::clicked() | ||||
This signal is emitted when user has clicked the slice. | ||||
\sa QPieSeries::clicked() | ||||
*/ | ||||
/*! | ||||
\fn void QPieSlice::hoverEnter() | ||||
This signal is emitted when user has hovered over the slice. | ||||
\sa QPieSeries::hoverEnter() | ||||
*/ | ||||
/*! | ||||
\fn void QPieSlice::hoverLeave() | ||||
This signal is emitted when user has hovered away from the slice. | ||||
\sa QPieSeries::hoverLeave() | ||||
*/ | ||||
/*! | ||||
\fn void QPieSlice::changed() | ||||
This signal emitted when something has changed in the slice. | ||||
\sa QPieSeries::changed() | ||||
*/ | ||||
/*! | ||||
|
r454 | Sets the \a value of this slice. | ||
|
r314 | \sa value() | ||
*/ | ||||
|
r203 | void QPieSlice::setValue(qreal value) | ||
{ | ||||
|
r669 | Q_D(QPieSlice); | ||
if (d->m_data.m_value != value) { | ||||
d->m_data.m_value = value; | ||||
|
r618 | |||
QPieSeries *series = qobject_cast<QPieSeries*>(parent()); | ||||
if (series) | ||||
|
r669 | series->d_ptr->updateDerivativeData(); // will emit changed() | ||
|
r618 | else | ||
emit changed(); | ||||
|
r203 | } | ||
} | ||||
|
r314 | /*! | ||
Sets the \a label of the slice. | ||||
\sa label() | ||||
*/ | ||||
|
r203 | void QPieSlice::setLabel(QString label) | ||
{ | ||||
|
r669 | Q_D(QPieSlice); | ||
if (d->m_data.m_labelText != label) { | ||||
d->m_data.m_labelText = label; | ||||
|
r203 | emit changed(); | ||
} | ||||
} | ||||
|
r314 | /*! | ||
Sets the label \a visible in this slice. | ||||
\sa isLabelVisible(), QPieSeries::setLabelsVisible() | ||||
*/ | ||||
|
r203 | void QPieSlice::setLabelVisible(bool visible) | ||
{ | ||||
|
r669 | Q_D(QPieSlice); | ||
if (d->m_data.m_labelVisible != visible) { | ||||
d->m_data.m_labelVisible = visible; | ||||
|
r203 | emit changed(); | ||
} | ||||
} | ||||
|
r314 | /*! | ||
Sets this slice \a exploded. | ||||
|
r454 | \sa isExploded(), explodeDistanceFactor() | ||
|
r314 | */ | ||
|
r203 | void QPieSlice::setExploded(bool exploded) | ||
{ | ||||
|
r669 | Q_D(QPieSlice); | ||
if (d->m_data.m_isExploded != exploded) { | ||||
d->m_data.m_isExploded = exploded; | ||||
|
r203 | emit changed(); | ||
} | ||||
} | ||||
|
r314 | /*! | ||
|
r454 | Sets the explode distance \a factor. | ||
The factor is relative to pie radius. For example: | ||||
1.0 means the distance is the same as the radius. | ||||
0.5 means the distance is half of the radius. | ||||
Default value is 0.15 | ||||
\sa explodeDistanceFactor() | ||||
|
r314 | */ | ||
|
r454 | void QPieSlice::setExplodeDistanceFactor(qreal factor) | ||
|
r289 | { | ||
|
r669 | Q_D(QPieSlice); | ||
if (d->m_data.m_explodeDistanceFactor != factor) { | ||||
d->m_data.m_explodeDistanceFactor = factor; | ||||
|
r289 | emit changed(); | ||
} | ||||
} | ||||
|
r314 | /*! | ||
Sets the \a pen used to draw this slice. | ||||
Note that applying a theme will override this. | ||||
|
r469 | \sa slicePen() | ||
|
r314 | */ | ||
|
r469 | void QPieSlice::setSlicePen(const QPen &pen) | ||
|
r203 | { | ||
|
r669 | Q_D(QPieSlice); | ||
if (d->m_data.m_slicePen != pen) { | ||||
d->m_data.m_slicePen = pen; | ||||
|
r203 | emit changed(); | ||
} | ||||
} | ||||
|
r314 | /*! | ||
Sets the \a brush used to draw this slice. | ||||
Note that applying a theme will override this. | ||||
|
r469 | \sa sliceBrush() | ||
|
r314 | */ | ||
|
r469 | void QPieSlice::setSliceBrush(const QBrush &brush) | ||
|
r203 | { | ||
|
r669 | Q_D(QPieSlice); | ||
if (d->m_data.m_sliceBrush != brush) { | ||||
d->m_data.m_sliceBrush = brush; | ||||
|
r203 | emit changed(); | ||
} | ||||
} | ||||
|
r314 | /*! | ||
|
r469 | Sets the \a pen used to draw the label arm in this slice. | ||
|
r314 | Note that applying a theme will override this. | ||
|
r469 | \sa labelArmPen() | ||
|
r314 | */ | ||
|
r469 | void QPieSlice::setLabelArmPen(const QPen &pen) | ||
|
r203 | { | ||
|
r669 | Q_D(QPieSlice); | ||
if (d->m_data.m_labelArmPen != pen) { | ||||
d->m_data.m_labelArmPen = pen; | ||||
|
r203 | emit changed(); | ||
} | ||||
} | ||||
|
r314 | /*! | ||
Sets the \a font used to draw the label in this slice. | ||||
Note that applying a theme will override this. | ||||
\sa labelFont() | ||||
*/ | ||||
|
r469 | void QPieSlice::setLabelFont(const QFont &font) | ||
|
r203 | { | ||
|
r669 | Q_D(QPieSlice); | ||
if (d->m_data.m_labelFont != font) { | ||||
d->m_data.m_labelFont = font; | ||||
|
r203 | emit changed(); | ||
} | ||||
} | ||||
|
r314 | /*! | ||
|
r454 | Sets the label arm lenght \a factor. | ||
The factor is relative to pie radius. For example: | ||||
1.0 means the length is the same as the radius. | ||||
0.5 means the length is half of the radius. | ||||
Default value is 0.15 | ||||
\sa labelArmLengthFactor() | ||||
|
r314 | */ | ||
|
r454 | void QPieSlice::setLabelArmLengthFactor(qreal factor) | ||
|
r203 | { | ||
|
r669 | Q_D(QPieSlice); | ||
if (d->m_data.m_labelArmLengthFactor != factor) { | ||||
d->m_data.m_labelArmLengthFactor = factor; | ||||
|
r203 | emit changed(); | ||
} | ||||
} | ||||
#include "moc_qpieslice.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||