qpieslice.cpp
536 lines
| 11.1 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
|
r203 | #include "qpieslice.h" | ||
Jani Honkonen
|
r1274 | #include "qpieslice_p.h" | ||
Jani Honkonen
|
r203 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Jani Honkonen
|
r314 | /*! | ||
\class QPieSlice | ||||
Jani Honkonen
|
r339 | \brief Defines a slice in pie series. | ||
Jani Honkonen
|
r314 | |||
Jani Honkonen
|
r952 | This object defines the properties of a single slice in a QPieSeries. | ||
Jani Honkonen
|
r320 | |||
Jani Honkonen
|
r952 | In addition to the obvious value and label properties the user can also control | ||
the visual appearance of a slice. By modifying the visual appearance also means that | ||||
Jani Honkonen
|
r1277 | the user is overriding the default appearance set by the theme. | ||
Jani Honkonen
|
r952 | |||
Jani Honkonen
|
r1277 | Note that if the user has customized slices and theme is changed all customizations will be lost. | ||
To enable user interaction with the pie some basic signals are provided about clicking and hovering. | ||||
Jani Honkonen
|
r314 | */ | ||
/*! | ||||
\property QPieSlice::label | ||||
Label of the slice. | ||||
Jani Honkonen
|
r1277 | |||
\sa labelVisible, labelPen, labelFont, labelArmLengthFactor | ||||
*/ | ||||
/*! | ||||
\fn void QPieSlice::labelChanged() | ||||
This signal emitted when the slice label has been changed. | ||||
\sa label | ||||
Jani Honkonen
|
r314 | */ | ||
/*! | ||||
\property QPieSlice::value | ||||
Value of the slice. | ||||
Jani Honkonen
|
r952 | |||
Jani Honkonen
|
r1280 | Note that if users sets a negative value it is converted to a positive value. | ||
Jani Honkonen
|
r952 | \sa percentage(), QPieSeries::sum() | ||
Jani Honkonen
|
r314 | */ | ||
/*! | ||||
Jani Honkonen
|
r1277 | \fn void QPieSlice::valueChanged() | ||
Jani Honkonen
|
r320 | |||
Jani Honkonen
|
r1277 | This signal is emitted when the slice value changes. | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r1277 | \sa value | ||
*/ | ||||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \property QPieSlice::labelVisible | ||
Defienes the visibility of the slice label. | ||||
Default is not visible. | ||||
\sa label, labelPen, labelFont, labelArmLengthFactor | ||||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \fn void QPieSlice::labelVisibleChanged() | ||
Jani Honkonen
|
r1274 | |||
Jani Honkonen
|
r1277 | This signal emitted when visibility of the slice label has changed. | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r1277 | \sa labelVisible | ||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \property QPieSlice::exploded | ||
Defines if the slice is exploded from the pie. | ||||
\sa explodeDistanceFactor | ||||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \fn void QPieSlice::explodedChanged() | ||
This signal is emitted the the slice has been exploded from the pie or is returned back to the pie. | ||||
\sa exploded | ||||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \property QPieSlice::pen | ||
Pen used to draw the slice border. | ||||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \fn void QPieSlice::penChanged() | ||
Jani Honkonen
|
r454 | |||
Jani Honkonen
|
r1277 | This signal is emitted when the pen of the slice has changed. | ||
\sa pen | ||||
*/ | ||||
Jani Honkonen
|
r454 | |||
Jani Honkonen
|
r1277 | /*! | ||
\property QPieSlice::brush | ||||
Jani Honkonen
|
r454 | |||
Jani Honkonen
|
r1277 | Brush used to draw the slice. | ||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r289 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \fn void QPieSlice::brushChanged() | ||
Jani Honkonen
|
r320 | |||
Jani Honkonen
|
r1277 | This signal is emitted when the brush of the slice has changed. | ||
Jani Honkonen
|
r952 | |||
Jani Honkonen
|
r1277 | \sa brush | ||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \property QPieSlice::labelPen | ||
Jani Honkonen
|
r320 | |||
Jani Honkonen
|
r1277 | Pen used to draw label and label arm of the slice. | ||
Jani Honkonen
|
r320 | |||
Jani Honkonen
|
r1277 | \sa label, labelVisible, labelFont, labelArmLengthFactor | ||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \fn void QPieSlice::labelPenChanged() | ||
Jani Honkonen
|
r320 | |||
Jani Honkonen
|
r1277 | This signal is emitted when the label pen of the slice has changed. | ||
Jani Honkonen
|
r320 | |||
Jani Honkonen
|
r1277 | \sa labelPen | ||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \property QPieSlice::labelFont | ||
Font used for drawing label text. | ||||
\sa label, labelVisible, labelArmLengthFactor | ||||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \fn void QPieSlice::labelFontChanged() | ||
This signal is emitted when the label font of the slice has changed. | ||||
\sa labelFont | ||||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \property QPieSlice::labelArmLengthFactor | ||
Defines the length of the label arm. | ||||
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 label, labelVisible, labelPen, labelFont | ||||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \fn void QPieSlice::labelArmLengthFactorChanged() | ||
This signal is emitted when the label arm factor of the slice has changed. | ||||
\sa labelArmLengthFactor | ||||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | \property QPieSlice::explodeDistanceFactor | ||
When the slice is exploded this factor defines how far the slice is exploded away from the pie. | ||||
Jani Honkonen
|
r454 | |||
The factor is relative to pie radius. For example: | ||||
Jani Honkonen
|
r1277 | 1.0 means the distance is the same as the radius. | ||
0.5 means the distance is half of the radius. | ||||
Jani Honkonen
|
r454 | |||
Default value is 0.15 | ||||
Jani Honkonen
|
r1277 | \sa exploded | ||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r203 | |||
Jani Honkonen
|
r1224 | /*! | ||
Jani Honkonen
|
r1277 | \fn void QPieSlice::explodeDistanceFactorChanged() | ||
Jani Honkonen
|
r1224 | |||
Jani Honkonen
|
r1277 | This signal is emitted when the explode distance factor of the slice has changed. | ||
Jani Honkonen
|
r1224 | |||
Jani Honkonen
|
r1277 | \sa explodeDistanceFactor | ||
Jani Honkonen
|
r1224 | */ | ||
/*! | ||||
Jani Honkonen
|
r1277 | \property QPieSlice::percentage | ||
Jani Honkonen
|
r1224 | |||
Jani Honkonen
|
r1277 | Percentage of the slice compared to the sum of all slices in the series. | ||
The actual value ranges from 0.0 to 1.0. | ||||
Updated automatically once the slice is added to the series. | ||||
\sa value, QPieSeries::sum | ||||
*/ | ||||
/*! | ||||
\fn void QPieSlice::percentageChanged() | ||||
This signal is emitted when the percentage of the slice has changed. | ||||
\sa percentage | ||||
*/ | ||||
/*! | ||||
\property QPieSlice::startAngle | ||||
Defines the starting angle of this slice in the series it belongs to. | ||||
Jani Honkonen
|
r1224 | |||
Jani Honkonen
|
r1277 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. | ||
Updated automatically once the slice is added to the series. | ||||
Jani Honkonen
|
r1224 | */ | ||
/*! | ||||
Jani Honkonen
|
r1277 | \fn void QPieSlice::startAngleChanged() | ||
This signal is emitted when the starting angle f the slice has changed. | ||||
\sa startAngle | ||||
*/ | ||||
Jani Honkonen
|
r1224 | |||
Jani Honkonen
|
r1277 | /*! | ||
\property QPieSlice::angleSpan | ||||
Span of the slice in degrees. | ||||
Full pie is 360 degrees where 0 degrees is at 12 a'clock. | ||||
Jani Honkonen
|
r1224 | |||
Jani Honkonen
|
r1277 | Updated automatically once the slice is added to the series. | ||
Jani Honkonen
|
r1224 | */ | ||
/*! | ||||
Jani Honkonen
|
r1277 | \fn void QPieSlice::angleSpanChanged() | ||
Jani Honkonen
|
r1224 | |||
Jani Honkonen
|
r1277 | This signal is emitted when the angle span of the slice has changed. | ||
Jani Honkonen
|
r1224 | |||
Jani Honkonen
|
r1277 | \sa angleSpan | ||
Jani Honkonen
|
r1224 | */ | ||
Jani Honkonen
|
r1277 | |||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1009 | \fn void QPieSlice::clicked() | ||
Jani Honkonen
|
r314 | |||
This signal is emitted when user has clicked the slice. | ||||
\sa QPieSeries::clicked() | ||||
*/ | ||||
/*! | ||||
Jani Honkonen
|
r1009 | \fn void QPieSlice::hovered(bool state) | ||
Jani Honkonen
|
r314 | |||
Jani Honkonen
|
r1009 | This signal is emitted when user has hovered over or away from the slice. | ||
Jani Honkonen
|
r314 | |||
Jani Honkonen
|
r1009 | \a state is true when user has hovered over the slice and false when hover has moved away from the slice. | ||
Jani Honkonen
|
r314 | |||
Jani Honkonen
|
r1009 | \sa QPieSeries::hovered() | ||
Jani Honkonen
|
r314 | */ | ||
/*! | ||||
Jani Honkonen
|
r1277 | Constructs an empty slice with a \a parent. | ||
\sa QPieSeries::append(), QPieSeries::insert() | ||||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r1277 | QPieSlice::QPieSlice(QObject *parent) | ||
:QObject(parent), | ||||
d_ptr(new QPieSlicePrivate(this)) | ||||
Jani Honkonen
|
r203 | { | ||
Jani Honkonen
|
r1277 | |||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r314 | /*! | ||
Jani Honkonen
|
r1277 | Constructs an empty slice with given \a value, \a label and a \a parent. | ||
\sa QPieSeries::append(), QPieSeries::insert() | ||||
Jani Honkonen
|
r314 | */ | ||
Jani Honkonen
|
r1277 | QPieSlice::QPieSlice(QString label, qreal value, QObject *parent) | ||
:QObject(parent), | ||||
d_ptr(new QPieSlicePrivate(this)) | ||||
{ | ||||
Jani Honkonen
|
r1280 | setValue(value); | ||
setLabel(label); | ||||
Jani Honkonen
|
r1277 | } | ||
/*! | ||||
Destroys the slice. | ||||
User should not delete the slice if it has been added to the series. | ||||
*/ | ||||
QPieSlice::~QPieSlice() | ||||
{ | ||||
} | ||||
Jani Honkonen
|
r203 | void QPieSlice::setLabel(QString label) | ||
{ | ||||
Jani Honkonen
|
r1274 | if (d_ptr->m_data.m_labelText != label) { | ||
d_ptr->m_data.m_labelText = label; | ||||
Jani Honkonen
|
r1224 | emit labelChanged(); | ||
Jani Honkonen
|
r203 | } | ||
} | ||||
Jani Honkonen
|
r1277 | QString QPieSlice::label() const | ||
{ | ||||
return d_ptr->m_data.m_labelText; | ||||
} | ||||
void QPieSlice::setValue(qreal value) | ||||
{ | ||||
Jani Honkonen
|
r1280 | value = qAbs(value); // negative values not allowed | ||
Jani Honkonen
|
r1277 | if (!qFuzzyIsNull(d_ptr->m_data.m_value - value)) { | ||
d_ptr->m_data.m_value = value; | ||||
emit valueChanged(); | ||||
} | ||||
} | ||||
qreal QPieSlice::value() const | ||||
{ | ||||
return d_ptr->m_data.m_value; | ||||
} | ||||
Jani Honkonen
|
r203 | void QPieSlice::setLabelVisible(bool visible) | ||
{ | ||||
Jani Honkonen
|
r1274 | if (d_ptr->m_data.m_isLabelVisible != visible) { | ||
d_ptr->m_data.m_isLabelVisible = visible; | ||||
emit labelVisibleChanged(); | ||||
Jani Honkonen
|
r203 | } | ||
} | ||||
Jani Honkonen
|
r1277 | bool QPieSlice::isLabelVisible() const | ||
{ | ||||
return d_ptr->m_data.m_isLabelVisible; | ||||
} | ||||
Jani Honkonen
|
r952 | |||
Jani Honkonen
|
r203 | void QPieSlice::setExploded(bool exploded) | ||
{ | ||||
Jani Honkonen
|
r1274 | if (d_ptr->m_data.m_isExploded != exploded) { | ||
d_ptr->m_data.m_isExploded = exploded; | ||||
emit explodedChanged(); | ||||
Jani Honkonen
|
r203 | } | ||
} | ||||
Jani Honkonen
|
r1277 | bool QPieSlice::isExploded() const | ||
Jani Honkonen
|
r289 | { | ||
Jani Honkonen
|
r1277 | return d_ptr->m_data.m_isExploded; | ||
Jani Honkonen
|
r289 | } | ||
Jani Honkonen
|
r756 | void QPieSlice::setPen(const QPen &pen) | ||
Jani Honkonen
|
r203 | { | ||
Jani Honkonen
|
r1274 | d_ptr->setPen(pen, false); | ||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r1277 | QPen QPieSlice::pen() const | ||
{ | ||||
return d_ptr->m_data.m_slicePen; | ||||
} | ||||
Jani Honkonen
|
r952 | |||
Jani Honkonen
|
r756 | void QPieSlice::setBrush(const QBrush &brush) | ||
Jani Honkonen
|
r203 | { | ||
Jani Honkonen
|
r1274 | d_ptr->setBrush(brush, false); | ||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r1277 | QBrush QPieSlice::brush() const | ||
{ | ||||
return d_ptr->m_data.m_sliceBrush; | ||||
} | ||||
Jani Honkonen
|
r952 | |||
Jani Honkonen
|
r714 | void QPieSlice::setLabelPen(const QPen &pen) | ||
Jani Honkonen
|
r203 | { | ||
Jani Honkonen
|
r1274 | d_ptr->setLabelPen(pen, false); | ||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r1277 | QPen QPieSlice::labelPen() const | ||
{ | ||||
return d_ptr->m_data.m_labelPen; | ||||
} | ||||
Jani Honkonen
|
r952 | |||
Jani Honkonen
|
r469 | void QPieSlice::setLabelFont(const QFont &font) | ||
Jani Honkonen
|
r203 | { | ||
Jani Honkonen
|
r1274 | d_ptr->setLabelFont(font, false); | ||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r1277 | QFont QPieSlice::labelFont() const | ||
{ | ||||
return d_ptr->m_data.m_labelFont; | ||||
} | ||||
Jani Honkonen
|
r454 | |||
void QPieSlice::setLabelArmLengthFactor(qreal factor) | ||||
Jani Honkonen
|
r203 | { | ||
Jani Honkonen
|
r1274 | if (!qFuzzyIsNull(d_ptr->m_data.m_labelArmLengthFactor - factor)) { | ||
d_ptr->m_data.m_labelArmLengthFactor = factor; | ||||
emit labelArmLengthFactorChanged(); | ||||
} | ||||
} | ||||
Jani Honkonen
|
r1277 | qreal QPieSlice::labelArmLengthFactor() const | ||
{ | ||||
return d_ptr->m_data.m_labelArmLengthFactor; | ||||
} | ||||
void QPieSlice::setExplodeDistanceFactor(qreal factor) | ||||
{ | ||||
if (!qFuzzyIsNull(d_ptr->m_data.m_explodeDistanceFactor - factor)) { | ||||
d_ptr->m_data.m_explodeDistanceFactor = factor; | ||||
emit explodeDistanceFactorChanged(); | ||||
} | ||||
} | ||||
qreal QPieSlice::explodeDistanceFactor() const | ||||
{ | ||||
return d_ptr->m_data.m_explodeDistanceFactor; | ||||
} | ||||
qreal QPieSlice::percentage() const | ||||
{ | ||||
return d_ptr->m_data.m_percentage; | ||||
} | ||||
qreal QPieSlice::startAngle() const | ||||
{ | ||||
return d_ptr->m_data.m_startAngle; | ||||
} | ||||
qreal QPieSlice::angleSpan() const | ||||
{ | ||||
return d_ptr->m_data.m_angleSpan; | ||||
} | ||||
Jani Honkonen
|
r1274 | QPieSlicePrivate::QPieSlicePrivate(QPieSlice *parent) | ||
:QObject(parent), | ||||
q_ptr(parent) | ||||
{ | ||||
} | ||||
QPieSlicePrivate::~QPieSlicePrivate() | ||||
{ | ||||
} | ||||
QPieSlicePrivate *QPieSlicePrivate::fromSlice(QPieSlice *slice) | ||||
{ | ||||
return slice->d_func(); | ||||
} | ||||
void QPieSlicePrivate::setPen(const QPen &pen, bool themed) | ||||
{ | ||||
if (m_data.m_slicePen != pen) { | ||||
m_data.m_slicePen = pen; | ||||
m_data.m_slicePen.setThemed(themed); | ||||
emit q_ptr->penChanged(); | ||||
} | ||||
} | ||||
void QPieSlicePrivate::setBrush(const QBrush &brush, bool themed) | ||||
{ | ||||
if (m_data.m_sliceBrush != brush) { | ||||
m_data.m_sliceBrush = brush; | ||||
m_data.m_sliceBrush.setThemed(themed); | ||||
emit q_ptr->brushChanged(); | ||||
} | ||||
} | ||||
void QPieSlicePrivate::setLabelPen(const QPen &pen, bool themed) | ||||
{ | ||||
if (m_data.m_labelPen != pen) { | ||||
m_data.m_labelPen = pen; | ||||
m_data.m_labelPen.setThemed(themed); | ||||
emit q_ptr->labelPenChanged(); | ||||
} | ||||
} | ||||
void QPieSlicePrivate::setLabelFont(const QFont &font, bool themed) | ||||
{ | ||||
if (m_data.m_labelFont != font) { | ||||
m_data.m_labelFont = font; | ||||
m_data.m_labelFont.setThemed(themed); | ||||
emit q_ptr->labelFontChanged(); | ||||
} | ||||
} | ||||
void QPieSlicePrivate::setPercentage(qreal percentage) | ||||
{ | ||||
if (!qFuzzyIsNull(m_data.m_percentage - percentage)) { | ||||
m_data.m_percentage = percentage; | ||||
emit q_ptr->percentageChanged(); | ||||
} | ||||
} | ||||
void QPieSlicePrivate::setStartAngle(qreal angle) | ||||
{ | ||||
if (!qFuzzyIsNull(m_data.m_startAngle - angle)) { | ||||
m_data.m_startAngle = angle; | ||||
emit q_ptr->startAngleChanged(); | ||||
} | ||||
} | ||||
void QPieSlicePrivate::setAngleSpan(qreal span) | ||||
{ | ||||
if (!qFuzzyIsNull(m_data.m_angleSpan - span)) { | ||||
m_data.m_angleSpan = span; | ||||
emit q_ptr->angleSpanChanged(); | ||||
Jani Honkonen
|
r203 | } | ||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||
Tero Ahola
|
r837 | |||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
#include "moc_qpieslice.cpp" | ||||
Jani Honkonen
|
r1274 | #include "moc_qpieslice_p.cpp" | ||