piechartitem.cpp
228 lines
| 7.8 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
|
r2688 | ** Copyright (C) 2014 Digia Plc | ||
|
r794 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
|
r2574 | ** This file is part of the Qt Enterprise Charts Add-on. | ||
|
r794 | ** | ||
** $QT_BEGIN_LICENSE$ | ||||
|
r2574 | ** Licensees holding valid Qt Enterprise licenses may use this file in | ||
** accordance with the Qt Enterprise License Agreement provided with the | ||||
|
r794 | ** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r2714 | #include <private/piechartitem_p.h> | ||
#include <private/piesliceitem_p.h> | ||||
#include <QtCharts/QPieSlice> | ||||
#include <private/qpieslice_p.h> | ||||
#include <QtCharts/QPieSeries> | ||||
#include <private/qpieseries_p.h> | ||||
#include <private/chartpresenter_p.h> | ||||
#include <private/chartdataset_p.h> | ||||
#include <private/pieanimation_p.h> | ||||
#include <QtGui/QPainter> | ||||
#include <QtCore/QTimer> | ||||
|
r142 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r142 | |||
|
r2273 | PieChartItem::PieChartItem(QPieSeries *series, QGraphicsItem* item) | ||
: ChartItem(series->d_func(),item), | ||||
|
r2097 | m_series(series), | ||
m_animation(0) | ||||
|
r142 | { | ||
Q_ASSERT(series); | ||||
|
r1009 | |||
|
r1482 | QPieSeriesPrivate *p = QPieSeriesPrivate::fromSeries(series); | ||
|
r1456 | connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); | ||
|
r2067 | connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged())); | ||
|
r1213 | connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>))); | ||
connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>))); | ||||
|
r1482 | connect(p, SIGNAL(horizontalPositionChanged()), this, SLOT(updateLayout())); | ||
connect(p, SIGNAL(verticalPositionChanged()), this, SLOT(updateLayout())); | ||||
connect(p, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout())); | ||||
connect(p, SIGNAL(calculatedDataChanged()), this, SLOT(updateLayout())); | ||||
|
r621 | |||
|
r490 | // Note: the following does not affect as long as the item does not have anything to paint | ||
setZValue(ChartPresenter::PieSeriesZValue); | ||||
|
r1238 | |||
// Note: will not create slice items until we have a proper rectangle to draw on. | ||||
|
r142 | } | ||
|
r568 | PieChartItem::~PieChartItem() | ||
|
r142 | { | ||
|
r174 | // slices deleted automatically through QGraphicsItem | ||
|
r2423 | if (m_series) { | ||
m_series->disconnect(this); | ||||
QPieSeriesPrivate::fromSeries(m_series)->disconnect(this); | ||||
} | ||||
foreach (QPieSlice *slice, m_sliceItems.keys()) { | ||||
slice->disconnect(this); | ||||
QPieSlicePrivate::fromSlice(slice)->disconnect(this); | ||||
} | ||||
|
r142 | } | ||
|
r2104 | void PieChartItem::setAnimation(PieAnimation *animation) | ||
|
r1735 | { | ||
|
r2097 | m_animation = animation; | ||
|
r1735 | } | ||
|
r2104 | ChartAnimation *PieChartItem::animation() const | ||
|
r1735 | { | ||
return m_animation; | ||||
} | ||||
|
r1815 | void PieChartItem::handleDomainUpdated() | ||
{ | ||||
|
r2273 | QRectF rect(QPointF(0,0),domain()->size()); | ||
if(m_rect!=rect){ | ||||
prepareGeometryChange(); | ||||
m_rect = rect; | ||||
updateLayout(); | ||||
if (m_sliceItems.isEmpty()) | ||||
handleSlicesAdded(m_series->slices()); | ||||
} | ||||
|
r1191 | } | ||
|
r1074 | void PieChartItem::updateLayout() | ||
|
r621 | { | ||
|
r1074 | // find pie center coordinates | ||
m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->horizontalPosition())); | ||||
m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->verticalPosition())); | ||||
|
r634 | |||
|
r1074 | // find maximum radius for pie | ||
m_pieRadius = m_rect.height() / 2; | ||||
if (m_rect.width() < m_rect.height()) | ||||
m_pieRadius = m_rect.width() / 2; | ||||
|
r1838 | m_holeSize = m_pieRadius; | ||
|
r1074 | // apply size factor | ||
m_pieRadius *= m_series->pieSize(); | ||||
|
r1838 | m_holeSize *= m_series->holeSize(); | ||
|
r1074 | |||
// set layouts for existing slice items | ||||
|
r2097 | foreach (QPieSlice *slice, m_series->slices()) { | ||
|
r1074 | PieSliceItem *sliceItem = m_sliceItems.value(slice); | ||
if (sliceItem) { | ||||
PieSliceData sliceData = updateSliceGeometry(slice); | ||||
|
r2097 | if (m_animation) | ||
|
r1735 | presenter()->startAnimation(m_animation->updateValue(sliceItem, sliceData)); | ||
|
r1074 | else | ||
sliceItem->setLayout(sliceData); | ||||
} | ||||
} | ||||
update(); | ||||
} | ||||
|
r2104 | void PieChartItem::handleSlicesAdded(QList<QPieSlice *> slices) | ||
|
r1074 | { | ||
|
r1238 | // delay creating slice items until there is a proper rectangle | ||
if (!m_rect.isValid() && m_sliceItems.isEmpty()) | ||||
return; | ||||
|
r2273 | themeManager()->updateSeries(m_series); | ||
|
r659 | |||
|
r1074 | bool startupAnimation = m_sliceItems.isEmpty(); | ||
|
r621 | |||
|
r2104 | foreach(QPieSlice * slice, slices) { | ||
PieSliceItem *sliceItem = new PieSliceItem(this); | ||||
|
r1074 | m_sliceItems.insert(slice, sliceItem); | ||
|
r1224 | |||
|
r1494 | // Note: no need to connect to slice valueChanged() etc. | ||
|
r1274 | // This is handled through calculatedDataChanged signal. | ||
|
r1224 | connect(slice, SIGNAL(labelChanged()), this, SLOT(handleSliceChanged())); | ||
|
r1274 | connect(slice, SIGNAL(labelVisibleChanged()), this, SLOT(handleSliceChanged())); | ||
connect(slice, SIGNAL(penChanged()), this, SLOT(handleSliceChanged())); | ||||
connect(slice, SIGNAL(brushChanged()), this, SLOT(handleSliceChanged())); | ||||
|
r1307 | connect(slice, SIGNAL(labelBrushChanged()), this, SLOT(handleSliceChanged())); | ||
|
r1274 | connect(slice, SIGNAL(labelFontChanged()), this, SLOT(handleSliceChanged())); | ||
|
r1494 | |||
QPieSlicePrivate *p = QPieSlicePrivate::fromSlice(slice); | ||||
connect(p, SIGNAL(labelPositionChanged()), this, SLOT(handleSliceChanged())); | ||||
connect(p, SIGNAL(explodedChanged()), this, SLOT(handleSliceChanged())); | ||||
connect(p, SIGNAL(labelArmLengthFactorChanged()), this, SLOT(handleSliceChanged())); | ||||
connect(p, SIGNAL(explodeDistanceFactorChanged()), this, SLOT(handleSliceChanged())); | ||||
|
r1274 | |||
|
r1074 | connect(sliceItem, SIGNAL(clicked(Qt::MouseButtons)), slice, SIGNAL(clicked())); | ||
connect(sliceItem, SIGNAL(hovered(bool)), slice, SIGNAL(hovered(bool))); | ||||
|
r621 | |||
|
r1074 | PieSliceData sliceData = updateSliceGeometry(slice); | ||
|
r1735 | if (m_animation) | ||
presenter()->startAnimation(m_animation->addSlice(sliceItem, sliceData, startupAnimation)); | ||||
|
r621 | else | ||
|
r1074 | sliceItem->setLayout(sliceData); | ||
|
r621 | } | ||
} | ||||
|
r2104 | void PieChartItem::handleSlicesRemoved(QList<QPieSlice *> slices) | ||
|
r621 | { | ||
|
r2273 | themeManager()->updateSeries(m_series); | ||
|
r659 | |||
|
r1053 | foreach (QPieSlice *slice, slices) { | ||
|
r1301 | |||
|
r1074 | PieSliceItem *sliceItem = m_sliceItems.value(slice); | ||
|
r1301 | |||
// this can happen if you call append() & remove() in a row so that PieSliceItem is not even created | ||||
if (!sliceItem) | ||||
continue; | ||||
|
r1074 | m_sliceItems.remove(slice); | ||
|
r2084 | slice->disconnect(this); | ||
QPieSlicePrivate::fromSlice(slice)->disconnect(this); | ||||
|
r1053 | |||
|
r1735 | if (m_animation) | ||
presenter()->startAnimation(m_animation->removeSlice(sliceItem)); // animator deletes the PieSliceItem | ||||
|
r621 | else | ||
|
r1074 | delete sliceItem; | ||
|
r621 | } | ||
} | ||||
|
r568 | void PieChartItem::handleSliceChanged() | ||
|
r566 | { | ||
|
r2104 | QPieSlice *slice = qobject_cast<QPieSlice *>(sender()); | ||
|
r1494 | if (!slice) { | ||
|
r2104 | QPieSlicePrivate *slicep = qobject_cast<QPieSlicePrivate *>(sender()); | ||
|
r1494 | slice = slicep->q_ptr; | ||
} | ||||
|
r1074 | Q_ASSERT(m_sliceItems.contains(slice)); | ||
|
r437 | |||
|
r1074 | PieSliceItem *sliceItem = m_sliceItems.value(slice); | ||
PieSliceData sliceData = updateSliceGeometry(slice); | ||||
|
r1735 | if (m_animation) | ||
presenter()->startAnimation(m_animation->updateValue(sliceItem, sliceData)); | ||||
|
r1074 | else | ||
sliceItem->setLayout(sliceData); | ||||
|
r437 | |||
|
r1074 | update(); | ||
|
r621 | } | ||
|
r437 | |||
|
r1456 | void PieChartItem::handleSeriesVisibleChanged() | ||
{ | ||||
setVisible(m_series->isVisible()); | ||||
} | ||||
|
r2067 | void PieChartItem::handleOpacityChanged() | ||
{ | ||||
setOpacity(m_series->opacity()); | ||||
} | ||||
|
r1074 | PieSliceData PieChartItem::updateSliceGeometry(QPieSlice *slice) | ||
|
r621 | { | ||
|
r1274 | PieSliceData &sliceData = QPieSlicePrivate::fromSlice(slice)->m_data; | ||
|
r673 | sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice); | ||
|
r668 | sliceData.m_radius = m_pieRadius; | ||
|
r1838 | sliceData.m_holeRadius = m_holeSize; | ||
|
r668 | return sliceData; | ||
|
r621 | } | ||
|
r568 | #include "moc_piechartitem_p.cpp" | ||
|
r142 | |||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||