From 9c11726517faf7cbc1f295abef66595df48df4d2 2012-05-29 07:51:21 From: Jani Honkonen Date: 2012-05-29 07:51:21 Subject: [PATCH] pie: added everything as a property in QPieSlice This also caused some internal refactoring. --- diff --git a/examples/piechartdrilldown/drilldownslice.cpp b/examples/piechartdrilldown/drilldownslice.cpp index ed99cc5..852873d 100644 --- a/examples/piechartdrilldown/drilldownslice.cpp +++ b/examples/piechartdrilldown/drilldownslice.cpp @@ -29,7 +29,7 @@ DrilldownSlice::DrilldownSlice(qreal value, QString prefix, QAbstractSeries* dri setValue(value); updateLabel(); setLabelFont(QFont("Arial", 8)); - connect(this, SIGNAL(calculatedDataChanged()), this, SLOT(updateLabel())); + connect(this, SIGNAL(percentageChanged()), this, SLOT(updateLabel())); connect(this, SIGNAL(hovered(bool)), this, SLOT(showHighlight(bool))); } diff --git a/src/charttheme.cpp b/src/charttheme.cpp index a8150e5..7c31b2f 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -36,6 +36,7 @@ #include "qscatterseries.h" #include "qpieseries.h" #include "qpieslice.h" +#include "qpieslice_p.h" #include "qsplineseries.h" //items @@ -225,32 +226,19 @@ void ChartTheme::decorate(QPieSeries *series, int index) QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); QPieSlice *s = series->slices().at(i); - PieSliceData data = PieSliceData::fromSlice(s); + QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s); - if (data.m_slicePen.isThemed() || m_force) { - data.m_slicePen = penColor; - data.m_slicePen.setThemed(true); - } - - if (data.m_sliceBrush.isThemed() || m_force) { - data.m_sliceBrush = brushColor; - data.m_sliceBrush.setThemed(true); - } + if (d->m_data.m_slicePen.isThemed() || m_force) + d->setPen(penColor, true); - if (data.m_labelPen.isThemed() || m_force) { - data.m_labelPen = QPen(m_titleBrush.color()); - data.m_labelPen.setThemed(true); - } + if (d->m_data.m_sliceBrush.isThemed() || m_force) + d->setBrush(brushColor, true); - if (data.m_labelFont.isThemed() || m_force) { - data.m_labelFont = m_labelFont; - data.m_labelFont.setThemed(true); - } + if (d->m_data.m_labelPen.isThemed() || m_force) + d->setLabelPen(QPen(m_titleBrush.color()), true); - if (PieSliceData::fromSlice(s) != data) { - PieSliceData::fromSlice(s) = data; - emit PieSliceData::emitAppearanceChanged(s); - } + if (d->m_data.m_labelFont.isThemed() || m_force) + d->setLabelFont(m_labelFont, true); } } diff --git a/src/legend/legendmarker.cpp b/src/legend/legendmarker.cpp index 4fe0680..07a99bb 100644 --- a/src/legend/legendmarker.cpp +++ b/src/legend/legendmarker.cpp @@ -156,7 +156,7 @@ PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend m_pieslice(pieslice) { QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated())); - QObject::connect(pieslice, SIGNAL(appearanceChanged()), this, SLOT(updated())); + QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated())); updated(); } diff --git a/src/piechart/piechart.pri b/src/piechart/piechart.pri index ae163e4..acb7061 100644 --- a/src/piechart/piechart.pri +++ b/src/piechart/piechart.pri @@ -14,6 +14,7 @@ PRIVATE_HEADERS += \ $$PWD/pieslicedata_p.h \ $$PWD/piechartitem_p.h \ $$PWD/piesliceitem_p.h \ + $$PWD/qpieslice_p.h \ $$PWD/qpieseries_p.h \ $$PWD/qpiemodelmapper_p.h @@ -23,3 +24,4 @@ PUBLIC_HEADERS += \ $$PWD/qpiemodelmapper.h \ $$PWD/qvpiemodelmapper.h \ $$PWD/qhpiemodelmapper.h + diff --git a/src/piechart/piechartitem.cpp b/src/piechart/piechartitem.cpp index b9c6e81..ac57ab9 100644 --- a/src/piechart/piechartitem.cpp +++ b/src/piechart/piechartitem.cpp @@ -21,6 +21,7 @@ #include "piechartitem_p.h" #include "piesliceitem_p.h" #include "qpieslice.h" +#include "qpieslice_p.h" #include "qpieseries.h" #include "qpieseries_p.h" #include "chartpresenter_p.h" @@ -42,6 +43,7 @@ PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter) connect(series, SIGNAL(horizontalPositionChanged()), this, SLOT(updateLayout())); connect(series, SIGNAL(verticalPositionChanged()), this, SLOT(updateLayout())); connect(series, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout())); + connect(QPieSeriesPrivate::fromSeries(series), SIGNAL(calculatedDataChanged()), this, SLOT(updateLayout())); // Note: the following does not affect as long as the item does not have anything to paint setZValue(ChartPresenter::PieSeriesZValue); @@ -134,11 +136,18 @@ void PieChartItem::handleSlicesAdded(QList slices) PieSliceItem* sliceItem = new PieSliceItem(this); m_sliceItems.insert(slice, sliceItem); - // note: do need to connect to slice valueChanged(). calculatedDataChanged() is enough. - // to update the slice. - connect(slice, SIGNAL(calculatedDataChanged()), this, SLOT(handleSliceChanged())); + // Note: do need to connect to slice valueChanged() etc. + // This is handled through calculatedDataChanged signal. connect(slice, SIGNAL(labelChanged()), this, SLOT(handleSliceChanged())); - connect(slice, SIGNAL(appearanceChanged()), this, SLOT(handleSliceChanged())); + connect(slice, SIGNAL(labelVisibleChanged()), this, SLOT(handleSliceChanged())); + connect(slice, SIGNAL(explodedChanged()), this, SLOT(handleSliceChanged())); + connect(slice, SIGNAL(penChanged()), this, SLOT(handleSliceChanged())); + connect(slice, SIGNAL(brushChanged()), this, SLOT(handleSliceChanged())); + connect(slice, SIGNAL(labelPenChanged()), this, SLOT(handleSliceChanged())); + connect(slice, SIGNAL(labelFontChanged()), this, SLOT(handleSliceChanged())); + connect(slice, SIGNAL(labelArmLengthFactorChanged()), this, SLOT(handleSliceChanged())); + connect(slice, SIGNAL(explodeDistanceFactorChanged()), this, SLOT(handleSliceChanged())); + connect(sliceItem, SIGNAL(clicked(Qt::MouseButtons)), slice, SIGNAL(clicked())); connect(sliceItem, SIGNAL(hovered(bool)), slice, SIGNAL(hovered(bool))); @@ -183,7 +192,7 @@ void PieChartItem::handleSliceChanged() PieSliceData PieChartItem::updateSliceGeometry(QPieSlice *slice) { - PieSliceData &sliceData = PieSliceData::fromSlice(slice); + PieSliceData &sliceData = QPieSlicePrivate::fromSlice(slice)->m_data; sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice); sliceData.m_radius = m_pieRadius; return sliceData; diff --git a/src/piechart/pieslicedata_p.h b/src/piechart/pieslicedata_p.h index 3e9c9af..45b39e2 100644 --- a/src/piechart/pieslicedata_p.h +++ b/src/piechart/pieslicedata_p.h @@ -104,24 +104,6 @@ public: return false; } - static void emitAppearanceChanged(QPieSlice *slice) - { - Q_ASSERT(slice); - emit slice->appearanceChanged(); - } - - static void emitCalculatedDataChanged(QPieSlice *slice) - { - Q_ASSERT(slice); - emit slice->calculatedDataChanged(); - } - - static PieSliceData &fromSlice(QPieSlice *slice) - { - Q_ASSERT(slice); - return *slice->d; - } - qreal m_value; Themed m_slicePen; diff --git a/src/piechart/piesliceitem.cpp b/src/piechart/piesliceitem.cpp index 94f076b..83d7e25 100644 --- a/src/piechart/piesliceitem.cpp +++ b/src/piechart/piesliceitem.cpp @@ -143,7 +143,7 @@ void PieSliceItem::updateGeometry() QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice) { if (slice->isExploded()) { - qreal centerAngle = slice->startAngle() + ((slice->endAngle() - slice->startAngle())/2); + qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2); qreal len = radius * slice->explodeDistanceFactor(); qreal dx = qSin(centerAngle*(PI/180)) * len; qreal dy = -qCos(centerAngle*(PI/180)) * len; diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index e262d37..9e02a3a 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -21,6 +21,7 @@ #include "qpieseries.h" #include "qpieseries_p.h" #include "qpieslice.h" +#include "qpieslice_p.h" #include "pieslicedata_p.h" #include "chartdataset_p.h" #include "charttheme_p.h" @@ -173,7 +174,7 @@ bool QPieSeries::append(QList slices) d->updateDerivativeData(); foreach (QPieSlice* s, slices) { - connect(s, SIGNAL(valueChanged()), d, SLOT(sliceChanged())); + connect(s, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged())); connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked())); connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool))); } @@ -238,7 +239,7 @@ bool QPieSeries::insert(int index, QPieSlice* slice) d->updateDerivativeData(); - connect(slice, SIGNAL(valueChanged()), d, SLOT(sliceChanged())); + connect(slice, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged())); connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked())); connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool))); @@ -531,30 +532,23 @@ void QPieSeriesPrivate::updateDerivativeData() qreal pieSpan = m_pieEndAngle - m_pieStartAngle; QVector changed; foreach (QPieSlice* s, m_slices) { - - PieSliceData data = PieSliceData::fromSlice(s); - data.m_percentage = s->value() / m_sum; - data.m_angleSpan = pieSpan * data.m_percentage; - data.m_startAngle = sliceAngle; - sliceAngle += data.m_angleSpan; - - if (PieSliceData::fromSlice(s) != data) { - PieSliceData::fromSlice(s) = data; - changed << s; - } + QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s); + d->setPercentage(s->value() / m_sum); + d->setStartAngle(sliceAngle); + d->setAngleSpan(pieSpan * s->percentage()); + sliceAngle += s->angleSpan(); } - // emit signals - foreach (QPieSlice* s, changed) - PieSliceData::emitCalculatedDataChanged(s); + + emit calculatedDataChanged(); } -QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series) +QPieSeriesPrivate* QPieSeriesPrivate::fromSeries(QPieSeries *series) { - return series.d_func(); + return series->d_func(); } -void QPieSeriesPrivate::sliceChanged() +void QPieSeriesPrivate::sliceValueChanged() { Q_ASSERT(m_slices.contains(qobject_cast(sender()))); updateDerivativeData(); diff --git a/src/piechart/qpieseries_p.h b/src/piechart/qpieseries_p.h index 2a37731..a3b0ef5 100644 --- a/src/piechart/qpieseries_p.h +++ b/src/piechart/qpieseries_p.h @@ -41,14 +41,17 @@ public: void updateDerivativeData(); - static QPieSeriesPrivate* seriesData(QPieSeries &series); + static QPieSeriesPrivate* fromSeries(QPieSeries *series); + +signals: + void calculatedDataChanged(); public Q_SLOTS: - void sliceChanged(); + void sliceValueChanged(); void sliceClicked(); void sliceHovered(bool state); -public: +private: QList m_slices; qreal m_pieRelativeHorPos; qreal m_pieRelativeVerPos; diff --git a/src/piechart/qpieslice.cpp b/src/piechart/qpieslice.cpp index 7c0308d..5615dfb 100644 --- a/src/piechart/qpieslice.cpp +++ b/src/piechart/qpieslice.cpp @@ -19,7 +19,7 @@ ****************************************************************************/ #include "qpieslice.h" -#include "pieslicedata_p.h" +#include "qpieslice_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -59,7 +59,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ QPieSlice::QPieSlice(QObject *parent) :QObject(parent), - d(new PieSliceData()) + d_ptr(new QPieSlicePrivate(this)) { } @@ -70,10 +70,10 @@ QPieSlice::QPieSlice(QObject *parent) */ QPieSlice::QPieSlice(QString label, qreal value, QObject *parent) :QObject(parent), - d(new PieSliceData()) + d_ptr(new QPieSlicePrivate(this)) { - d->m_value = value; - d->m_labelText = label; + d_ptr->m_data.m_value = value; + d_ptr->m_data.m_labelText = label; } /*! @@ -82,7 +82,7 @@ QPieSlice::QPieSlice(QString label, qreal value, QObject *parent) */ QPieSlice::~QPieSlice() { - delete d; + } /*! @@ -92,7 +92,7 @@ QPieSlice::~QPieSlice() */ qreal QPieSlice::value() const { - return d->m_value; + return d_ptr->m_data.m_value; } /*! @@ -101,7 +101,7 @@ qreal QPieSlice::value() const */ QString QPieSlice::label() const { - return d->m_labelText; + return d_ptr->m_data.m_labelText; } /*! @@ -110,7 +110,7 @@ QString QPieSlice::label() const */ bool QPieSlice::isLabelVisible() const { - return d->m_isLabelVisible; + return d_ptr->m_data.m_isLabelVisible; } /*! @@ -119,7 +119,7 @@ bool QPieSlice::isLabelVisible() const */ bool QPieSlice::isExploded() const { - return d->m_isExploded; + return d_ptr->m_data.m_isExploded; } /*! @@ -135,7 +135,7 @@ bool QPieSlice::isExploded() const */ qreal QPieSlice::explodeDistanceFactor() const { - return d->m_explodeDistanceFactor; + return d_ptr->m_data.m_explodeDistanceFactor; } /*! @@ -148,7 +148,7 @@ qreal QPieSlice::explodeDistanceFactor() const */ qreal QPieSlice::percentage() const { - return d->m_percentage; + return d_ptr->m_data.m_percentage; } /*! @@ -160,7 +160,7 @@ qreal QPieSlice::percentage() const */ qreal QPieSlice::startAngle() const { - return d->m_startAngle; + return d_ptr->m_data.m_startAngle; } /*! @@ -170,9 +170,9 @@ qreal QPieSlice::startAngle() const Updated internally after the slice is added to the series. */ -qreal QPieSlice::endAngle() const +qreal QPieSlice::angleSpan() const { - return d->m_startAngle + d->m_angleSpan; + return d_ptr->m_data.m_angleSpan; } /*! @@ -181,7 +181,7 @@ qreal QPieSlice::endAngle() const */ QPen QPieSlice::pen() const { - return d->m_slicePen; + return d_ptr->m_data.m_slicePen; } /*! @@ -190,7 +190,7 @@ QPen QPieSlice::pen() const */ QBrush QPieSlice::brush() const { - return d->m_sliceBrush; + return d_ptr->m_data.m_sliceBrush; } /*! @@ -199,7 +199,7 @@ QBrush QPieSlice::brush() const */ QPen QPieSlice::labelPen() const { - return d->m_labelPen; + return d_ptr->m_data.m_labelPen; } /*! @@ -208,7 +208,7 @@ QPen QPieSlice::labelPen() const */ QFont QPieSlice::labelFont() const { - return d->m_labelFont; + return d_ptr->m_data.m_labelFont; } /*! @@ -224,7 +224,7 @@ QFont QPieSlice::labelFont() const */ qreal QPieSlice::labelArmLengthFactor() const { - return d->m_labelArmLengthFactor; + return d_ptr->m_data.m_labelArmLengthFactor; } /*! @@ -283,8 +283,8 @@ qreal QPieSlice::labelArmLengthFactor() const */ void QPieSlice::setValue(qreal value) { - if (!qFuzzyIsNull(d->m_value - value)) { - d->m_value = value; + if (!qFuzzyIsNull(d_ptr->m_data.m_value - value)) { + d_ptr->m_data.m_value = value; emit valueChanged(); } } @@ -295,8 +295,8 @@ void QPieSlice::setValue(qreal value) */ void QPieSlice::setLabel(QString label) { - if (d->m_labelText != label) { - d->m_labelText = label; + if (d_ptr->m_data.m_labelText != label) { + d_ptr->m_data.m_labelText = label; emit labelChanged(); } } @@ -307,9 +307,9 @@ void QPieSlice::setLabel(QString label) */ void QPieSlice::setLabelVisible(bool visible) { - if (d->m_isLabelVisible != visible) { - d->m_isLabelVisible = visible; - emit appearanceChanged(); + if (d_ptr->m_data.m_isLabelVisible != visible) { + d_ptr->m_data.m_isLabelVisible = visible; + emit labelVisibleChanged(); } } @@ -322,9 +322,9 @@ void QPieSlice::setLabelVisible(bool visible) */ void QPieSlice::setExploded(bool exploded) { - if (d->m_isExploded != exploded) { - d->m_isExploded = exploded; - emit appearanceChanged(); + if (d_ptr->m_data.m_isExploded != exploded) { + d_ptr->m_data.m_isExploded = exploded; + emit explodedChanged(); } } @@ -341,9 +341,9 @@ void QPieSlice::setExploded(bool exploded) */ void QPieSlice::setExplodeDistanceFactor(qreal factor) { - if (!qFuzzyIsNull(d->m_explodeDistanceFactor - factor)) { - d->m_explodeDistanceFactor = factor; - emit appearanceChanged(); + if (!qFuzzyIsNull(d_ptr->m_data.m_explodeDistanceFactor - factor)) { + d_ptr->m_data.m_explodeDistanceFactor = factor; + emit explodeDistanceFactorChanged(); } } @@ -356,11 +356,7 @@ void QPieSlice::setExplodeDistanceFactor(qreal factor) */ void QPieSlice::setPen(const QPen &pen) { - if (d->m_slicePen != pen) { - d->m_slicePen = pen; - d->m_slicePen.setThemed(false); - emit appearanceChanged(); - } + d_ptr->setPen(pen, false); } /*! @@ -372,11 +368,7 @@ void QPieSlice::setPen(const QPen &pen) */ void QPieSlice::setBrush(const QBrush &brush) { - if (d->m_sliceBrush != brush) { - d->m_sliceBrush = brush; - d->m_sliceBrush.setThemed(false); - emit appearanceChanged(); - } + d_ptr->setBrush(brush, false); } /*! @@ -388,11 +380,7 @@ void QPieSlice::setBrush(const QBrush &brush) */ void QPieSlice::setLabelPen(const QPen &pen) { - if (d->m_labelPen != pen) { - d->m_labelPen = pen; - d->m_labelPen.setThemed(false); - emit appearanceChanged(); - } + d_ptr->setLabelPen(pen, false); } /*! @@ -404,11 +392,7 @@ void QPieSlice::setLabelPen(const QPen &pen) */ void QPieSlice::setLabelFont(const QFont &font) { - if (d->m_labelFont != font) { - d->m_labelFont = font; - d->m_labelFont.setThemed(false); - emit appearanceChanged(); - } + d_ptr->setLabelFont(font, false); } /*! @@ -424,9 +408,86 @@ void QPieSlice::setLabelFont(const QFont &font) */ void QPieSlice::setLabelArmLengthFactor(qreal factor) { - if (!qFuzzyIsNull(d->m_labelArmLengthFactor - factor)) { - d->m_labelArmLengthFactor = factor; - emit appearanceChanged(); + if (!qFuzzyIsNull(d_ptr->m_data.m_labelArmLengthFactor - factor)) { + d_ptr->m_data.m_labelArmLengthFactor = factor; + emit labelArmLengthFactorChanged(); + } +} + +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(); } } @@ -434,3 +495,4 @@ QTCOMMERCIALCHART_END_NAMESPACE QTCOMMERCIALCHART_USE_NAMESPACE #include "moc_qpieslice.cpp" +#include "moc_qpieslice_p.cpp" diff --git a/src/piechart/qpieslice.h b/src/piechart/qpieslice.h index e8e1196..ba3a7df 100644 --- a/src/piechart/qpieslice.h +++ b/src/piechart/qpieslice.h @@ -28,20 +28,24 @@ #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -class PieSliceData; +class QPieSlicePrivate; class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject { Q_OBJECT Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged) Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged) - Q_PROPERTY(bool labelVisible READ isLabelVisible WRITE setLabelVisible) - Q_PROPERTY(bool exploded READ isExploded WRITE setExploded) + Q_PROPERTY(bool labelVisible READ isLabelVisible WRITE setLabelVisible NOTIFY labelVisibleChanged) + Q_PROPERTY(bool exploded READ isExploded WRITE setExploded NOTIFY explodedChanged) + Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged) + Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged) + Q_PROPERTY(QPen labelPen READ labelPen WRITE setLabelPen NOTIFY labelPenChanged) + Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont NOTIFY labelFontChanged) Q_PROPERTY(qreal LabelArmLengthFactor READ labelArmLengthFactor WRITE setLabelArmLengthFactor) Q_PROPERTY(qreal explodeDistanceFactor READ explodeDistanceFactor WRITE setExplodeDistanceFactor) - Q_PROPERTY(qreal percentage READ percentage NOTIFY calculatedDataChanged) - Q_PROPERTY(qreal startAngle READ startAngle NOTIFY calculatedDataChanged) - Q_PROPERTY(qreal endAngle READ endAngle NOTIFY calculatedDataChanged) + Q_PROPERTY(qreal percentage READ percentage NOTIFY percentageChanged) + Q_PROPERTY(qreal startAngle READ startAngle NOTIFY startAngleChanged) + Q_PROPERTY(qreal angleSpan READ angleSpan NOTIFY angleSpanChanged) public: explicit QPieSlice(QObject *parent = 0); @@ -54,45 +58,54 @@ public: void setValue(qreal value); qreal value() const; + void setLabelVisible(bool visible = true); + bool isLabelVisible() const; + + void setExploded(bool exploded = true); + bool isExploded() const; + void setPen(const QPen &pen); QPen pen() const; void setBrush(const QBrush &brush); QBrush brush() const; - void setLabelVisible(bool visible = true); - bool isLabelVisible() const; - - void setLabelArmLengthFactor(qreal factor); - qreal labelArmLengthFactor() const; - void setLabelPen(const QPen &pen); QPen labelPen() const; void setLabelFont(const QFont &font); QFont labelFont() const; - void setExploded(bool exploded = true); - bool isExploded() const; + void setLabelArmLengthFactor(qreal factor); + qreal labelArmLengthFactor() const; void setExplodeDistanceFactor(qreal factor); qreal explodeDistanceFactor() const; qreal percentage() const; qreal startAngle() const; - qreal endAngle() const; + qreal angleSpan() const; Q_SIGNALS: void labelChanged(); void valueChanged(); - void appearanceChanged(); - void calculatedDataChanged(); + void labelVisibleChanged(); + void explodedChanged(); + void penChanged(); + void brushChanged(); + void labelPenChanged(); + void labelFontChanged(); + void labelArmLengthFactorChanged(); + void explodeDistanceFactorChanged(); + void percentageChanged(); + void startAngleChanged(); + void angleSpanChanged(); void clicked(); void hovered(bool state); private: - friend class PieSliceData; - PieSliceData * const d; + QPieSlicePrivate * const d_ptr; + Q_DECLARE_PRIVATE(QPieSlice) Q_DISABLE_COPY(QPieSlice) }; diff --git a/src/piechart/qpieslice_p.h b/src/piechart/qpieslice_p.h new file mode 100644 index 0000000..f9c690f --- /dev/null +++ b/src/piechart/qpieslice_p.h @@ -0,0 +1,43 @@ +#ifndef QPIESLICE_P_H +#define QPIESLICE_P_H + +#include +#include "qpieslice.h" +#include "pieslicedata_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QPieSlicePrivate : public QObject +{ + Q_OBJECT + +public: + QPieSlicePrivate(QPieSlice *parent); + ~QPieSlicePrivate(); + + static QPieSlicePrivate* fromSlice(QPieSlice *slice); + + void setPen(const QPen &pen, bool themed); + void setBrush(const QBrush &brush, bool themed); + void setLabelPen(const QPen &pen, bool themed); + void setLabelFont(const QFont &font, bool themed); + + void setPercentage(qreal percentage); + void setStartAngle(qreal angle); + void setAngleSpan(qreal span); + +private: + PieSliceData m_data; + +private: + friend class QPieSeriesPrivate; + friend class ChartTheme; + friend class PieChartItem; + + QPieSlice * const q_ptr; + Q_DECLARE_PUBLIC(QPieSlice) +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QPIESLICE_P_H diff --git a/tests/auto/qpieseries/tst_qpieseries.cpp b/tests/auto/qpieseries/tst_qpieseries.cpp index d63eb8a..0b3f81f 100644 --- a/tests/auto/qpieseries/tst_qpieseries.cpp +++ b/tests/auto/qpieseries/tst_qpieseries.cpp @@ -260,14 +260,18 @@ void tst_qpieseries::calculatedValues() QPieSeries s; QPieSlice *slice1 = new QPieSlice("slice 1", 1); - QSignalSpy calculatedDataSpy(slice1, SIGNAL(calculatedDataChanged())); + QSignalSpy percentageSpy(slice1, SIGNAL(percentageChanged())); + QSignalSpy startAngleSpy(slice1, SIGNAL(startAngleChanged())); + QSignalSpy angleSpanSpy(slice1, SIGNAL(angleSpanChanged())); // add a slice s.append(slice1); verifyCalculatedData(s, &ok); if (!ok) return; - QCOMPARE(calculatedDataSpy.count(), 1); + QCOMPARE(percentageSpy.count(), 1); + QCOMPARE(startAngleSpy.count(), 0); + QCOMPARE(angleSpanSpy.count(), 1); // add some more slices QList list; @@ -277,21 +281,27 @@ void tst_qpieseries::calculatedValues() verifyCalculatedData(s, &ok); if (!ok) return; - QCOMPARE(calculatedDataSpy.count(), 2); + QCOMPARE(percentageSpy.count(), 2); + QCOMPARE(startAngleSpy.count(), 0); + QCOMPARE(angleSpanSpy.count(), 2); // remove a slice s.remove(list.first()); // remove slice 2 verifyCalculatedData(s, &ok); if (!ok) return; - QCOMPARE(calculatedDataSpy.count(), 3); + QCOMPARE(percentageSpy.count(), 3); + QCOMPARE(startAngleSpy.count(), 0); + QCOMPARE(angleSpanSpy.count(), 3); // insert a slice s.insert(0, new QPieSlice("Slice 4", 4)); verifyCalculatedData(s, &ok); if (!ok) return; - QCOMPARE(calculatedDataSpy.count(), 4); + QCOMPARE(percentageSpy.count(), 4); + QCOMPARE(startAngleSpy.count(), 1); + QCOMPARE(angleSpanSpy.count(), 4); // modify pie angles s.setPieStartAngle(-90); @@ -299,11 +309,16 @@ void tst_qpieseries::calculatedValues() verifyCalculatedData(s, &ok); if (!ok) return; - QCOMPARE(calculatedDataSpy.count(), 6); + QCOMPARE(percentageSpy.count(), 4); + QCOMPARE(startAngleSpy.count(), 3); + QCOMPARE(angleSpanSpy.count(), 6); // clear all s.clear(); verifyCalculatedData(s, &ok); + QCOMPARE(percentageSpy.count(), 4); + QCOMPARE(startAngleSpy.count(), 3); + QCOMPARE(angleSpanSpy.count(), 6); } void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok) @@ -321,13 +336,13 @@ void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok) qreal ratio = slice->value() / sum; qreal sliceSpan = pieAngleSpan * ratio; QCOMPARE(slice->startAngle(), startAngle); - QCOMPARE(slice->endAngle(), startAngle + sliceSpan); + QCOMPARE(slice->angleSpan(), sliceSpan); QCOMPARE(slice->percentage(), ratio); startAngle += sliceSpan; } if (!series.isEmpty()) - QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle()); + QCOMPARE(series.slices().last()->startAngle() + series.slices().last()->angleSpan(), series.pieEndAngle()); *ok = true; } diff --git a/tests/auto/qpieslice/tst_qpieslice.cpp b/tests/auto/qpieslice/tst_qpieslice.cpp index 5391e6c..b90008b 100644 --- a/tests/auto/qpieslice/tst_qpieslice.cpp +++ b/tests/auto/qpieslice/tst_qpieslice.cpp @@ -85,7 +85,7 @@ void tst_qpieslice::construction() QCOMPARE(slice1.explodeDistanceFactor(), 0.15); // default value QCOMPARE(slice1.percentage(), 0.0); QCOMPARE(slice1.startAngle(), 0.0); - QCOMPARE(slice1.endAngle(), 0.0); + QCOMPARE(slice1.angleSpan(), 0.0); // value and label params QPieSlice slice2("foobar", 1.0); @@ -101,7 +101,7 @@ void tst_qpieslice::construction() QCOMPARE(slice2.explodeDistanceFactor(), 0.15); // default value QCOMPARE(slice2.percentage(), 0.0); QCOMPARE(slice2.startAngle(), 0.0); - QCOMPARE(slice2.endAngle(), 0.0); + QCOMPARE(slice2.angleSpan(), 0.0); } void tst_qpieslice::changedSignals() @@ -110,8 +110,15 @@ void tst_qpieslice::changedSignals() QSignalSpy valueSpy(&slice, SIGNAL(valueChanged())); QSignalSpy labelSpy(&slice, SIGNAL(labelChanged())); - QSignalSpy appearanceSpy(&slice, SIGNAL(appearanceChanged())); - // calculatedDataChanged signal is tested at tst_qpieseries::calculatedValues() + QSignalSpy explodedSpy(&slice, SIGNAL(explodedChanged())); + QSignalSpy penSpy(&slice, SIGNAL(penChanged())); + QSignalSpy brushSpy(&slice, SIGNAL(brushChanged())); + QSignalSpy labelPenSpy(&slice, SIGNAL(labelPenChanged())); + QSignalSpy labelFontSpy(&slice, SIGNAL(labelFontChanged())); + QSignalSpy labelArmLengthFactorSpy(&slice, SIGNAL(labelArmLengthFactorChanged())); + QSignalSpy explodeDistanceFactorSpy(&slice, SIGNAL(explodeDistanceFactorChanged())); + + // percentageChanged(), startAngleChanged() and angleSpanChanged() signals tested at tst_qpieseries::calculatedValues() // set everything twice to see we do not get unnecessary signals slice.setValue(1); @@ -137,7 +144,13 @@ void tst_qpieslice::changedSignals() TRY_COMPARE(valueSpy.count(), 1); TRY_COMPARE(labelSpy.count(), 1); - TRY_COMPARE(appearanceSpy.count(), 8); + TRY_COMPARE(explodedSpy.count(), 1); + TRY_COMPARE(penSpy.count(), 1); + TRY_COMPARE(brushSpy.count(), 1); + TRY_COMPARE(labelPenSpy.count(), 1); + TRY_COMPARE(labelFontSpy.count(), 1); + TRY_COMPARE(labelArmLengthFactorSpy.count(), 1); + TRY_COMPARE(explodeDistanceFactorSpy.count(), 1); } void tst_qpieslice::customize()