piechartitem.cpp
208 lines
| 7.0 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
|
r568 | #include "piechartitem_p.h" | ||
Jani Honkonen
|
r673 | #include "piesliceitem_p.h" | ||
Jani Honkonen
|
r203 | #include "qpieslice.h" | ||
Jani Honkonen
|
r1274 | #include "qpieslice_p.h" | ||
Jani Honkonen
|
r289 | #include "qpieseries.h" | ||
Jani Honkonen
|
r1009 | #include "qpieseries_p.h" | ||
Tero Ahola
|
r490 | #include "chartpresenter_p.h" | ||
Jani Honkonen
|
r659 | #include "chartdataset_p.h" | ||
Jani Honkonen
|
r618 | #include "chartanimator_p.h" | ||
Jani Honkonen
|
r437 | #include <QPainter> | ||
Jani Honkonen
|
r621 | #include <QTimer> | ||
Jani Honkonen
|
r142 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Michal Klocek
|
r677 | PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter) | ||
:ChartItem(presenter), | ||||
m_series(series) | ||||
Jani Honkonen
|
r142 | { | ||
Q_ASSERT(series); | ||||
Jani Honkonen
|
r1009 | |||
Jani Honkonen
|
r1213 | connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>))); | ||
connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>))); | ||||
Jani Honkonen
|
r1255 | connect(series, SIGNAL(horizontalPositionChanged()), this, SLOT(updateLayout())); | ||
connect(series, SIGNAL(verticalPositionChanged()), this, SLOT(updateLayout())); | ||||
connect(series, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout())); | ||||
Jani Honkonen
|
r1274 | connect(QPieSeriesPrivate::fromSeries(series), SIGNAL(calculatedDataChanged()), this, SLOT(updateLayout())); | ||
Jani Honkonen
|
r621 | |||
Tero Ahola
|
r490 | // Note: the following does not affect as long as the item does not have anything to paint | ||
setZValue(ChartPresenter::PieSeriesZValue); | ||||
Jani Honkonen
|
r1238 | |||
// Note: will not create slice items until we have a proper rectangle to draw on. | ||||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r568 | PieChartItem::~PieChartItem() | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r174 | // slices deleted automatically through QGraphicsItem | ||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r1074 | void PieChartItem::handleGeometryChanged(const QRectF& rect) | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r1074 | prepareGeometryChange(); | ||
m_rect = rect; | ||||
updateLayout(); | ||||
Jani Honkonen
|
r1238 | |||
// This is for delayed initialization of the slice items during startup. | ||||
// It ensures that startup animation originates from the correct position. | ||||
if (m_sliceItems.isEmpty()) | ||||
handleSlicesAdded(m_series->slices()); | ||||
Jani Honkonen
|
r174 | } | ||
Jani Honkonen
|
r1191 | void PieChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) | ||
{ | ||||
Q_UNUSED(minX); | ||||
Q_UNUSED(maxX); | ||||
Q_UNUSED(minY); | ||||
Q_UNUSED(maxY); | ||||
// does not apply to pie | ||||
} | ||||
void PieChartItem::rangeXChanged(qreal min, qreal max, int tickXCount) | ||||
{ | ||||
Q_UNUSED(min); | ||||
Q_UNUSED(max); | ||||
Q_UNUSED(tickXCount); | ||||
// does not apply to pie | ||||
} | ||||
void PieChartItem::rangeYChanged(qreal min, qreal max, int tickYCount) | ||||
{ | ||||
Q_UNUSED(min); | ||||
Q_UNUSED(max); | ||||
Q_UNUSED(tickYCount); | ||||
// does not apply to pie | ||||
} | ||||
Jani Honkonen
|
r1074 | void PieChartItem::updateLayout() | ||
Jani Honkonen
|
r621 | { | ||
Jani Honkonen
|
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())); | ||||
Jani Honkonen
|
r634 | |||
Jani Honkonen
|
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; | ||||
// apply size factor | ||||
m_pieRadius *= m_series->pieSize(); | ||||
// set layouts for existing slice items | ||||
foreach (QPieSlice* slice, m_series->slices()) { | ||||
PieSliceItem *sliceItem = m_sliceItems.value(slice); | ||||
if (sliceItem) { | ||||
PieSliceData sliceData = updateSliceGeometry(slice); | ||||
if (animator()) | ||||
animator()->updateAnimation(this, sliceItem, sliceData); | ||||
else | ||||
sliceItem->setLayout(sliceData); | ||||
} | ||||
} | ||||
update(); | ||||
} | ||||
void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices) | ||||
{ | ||||
Jani Honkonen
|
r1238 | // delay creating slice items until there is a proper rectangle | ||
if (!m_rect.isValid() && m_sliceItems.isEmpty()) | ||||
return; | ||||
Michal Klocek
|
r943 | presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); | ||
Jani Honkonen
|
r659 | |||
Jani Honkonen
|
r1074 | bool startupAnimation = m_sliceItems.isEmpty(); | ||
Jani Honkonen
|
r621 | |||
Jani Honkonen
|
r1074 | foreach (QPieSlice *slice, slices) { | ||
PieSliceItem* sliceItem = new PieSliceItem(this); | ||||
m_sliceItems.insert(slice, sliceItem); | ||||
Jani Honkonen
|
r1224 | |||
Jani Honkonen
|
r1274 | // Note: do need to connect to slice valueChanged() etc. | ||
// This is handled through calculatedDataChanged signal. | ||||
Jani Honkonen
|
r1224 | connect(slice, SIGNAL(labelChanged()), this, SLOT(handleSliceChanged())); | ||
Jani Honkonen
|
r1274 | 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())); | ||||
Tero Ahola
|
r1307 | connect(slice, SIGNAL(labelBrushChanged()), this, SLOT(handleSliceChanged())); | ||
Jani Honkonen
|
r1274 | connect(slice, SIGNAL(labelFontChanged()), this, SLOT(handleSliceChanged())); | ||
connect(slice, SIGNAL(labelArmLengthFactorChanged()), this, SLOT(handleSliceChanged())); | ||||
connect(slice, SIGNAL(explodeDistanceFactorChanged()), this, SLOT(handleSliceChanged())); | ||||
Jani Honkonen
|
r1074 | connect(sliceItem, SIGNAL(clicked(Qt::MouseButtons)), slice, SIGNAL(clicked())); | ||
connect(sliceItem, SIGNAL(hovered(bool)), slice, SIGNAL(hovered(bool))); | ||||
Jani Honkonen
|
r621 | |||
Jani Honkonen
|
r1074 | PieSliceData sliceData = updateSliceGeometry(slice); | ||
Michal Klocek
|
r679 | if (animator()) | ||
Jani Honkonen
|
r1074 | animator()->addAnimation(this, sliceItem, sliceData, startupAnimation); | ||
Jani Honkonen
|
r621 | else | ||
Jani Honkonen
|
r1074 | sliceItem->setLayout(sliceData); | ||
Jani Honkonen
|
r621 | } | ||
} | ||||
void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices) | ||||
{ | ||||
Michal Klocek
|
r943 | presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); | ||
Jani Honkonen
|
r659 | |||
Jani Honkonen
|
r1053 | foreach (QPieSlice *slice, slices) { | ||
Jani Honkonen
|
r1301 | |||
Jani Honkonen
|
r1074 | PieSliceItem *sliceItem = m_sliceItems.value(slice); | ||
Jani Honkonen
|
r1301 | |||
// this can happen if you call append() & remove() in a row so that PieSliceItem is not even created | ||||
if (!sliceItem) | ||||
continue; | ||||
Jani Honkonen
|
r1074 | m_sliceItems.remove(slice); | ||
Jani Honkonen
|
r1053 | |||
Michal Klocek
|
r677 | if (animator()) | ||
Jani Honkonen
|
r1074 | animator()->removeAnimation(this, sliceItem); // animator deletes the PieSliceItem | ||
Jani Honkonen
|
r621 | else | ||
Jani Honkonen
|
r1074 | delete sliceItem; | ||
Jani Honkonen
|
r621 | } | ||
} | ||||
Jani Honkonen
|
r568 | void PieChartItem::handleSliceChanged() | ||
Jani Honkonen
|
r566 | { | ||
Jani Honkonen
|
r618 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | ||
Jani Honkonen
|
r1074 | Q_ASSERT(m_sliceItems.contains(slice)); | ||
Jani Honkonen
|
r437 | |||
Jani Honkonen
|
r1074 | PieSliceItem *sliceItem = m_sliceItems.value(slice); | ||
PieSliceData sliceData = updateSliceGeometry(slice); | ||||
if (animator()) | ||||
animator()->updateAnimation(this, sliceItem, sliceData); | ||||
else | ||||
sliceItem->setLayout(sliceData); | ||||
Jani Honkonen
|
r437 | |||
Jani Honkonen
|
r1074 | update(); | ||
Jani Honkonen
|
r621 | } | ||
Jani Honkonen
|
r437 | |||
Jani Honkonen
|
r1074 | PieSliceData PieChartItem::updateSliceGeometry(QPieSlice *slice) | ||
Jani Honkonen
|
r621 | { | ||
Jani Honkonen
|
r1274 | PieSliceData &sliceData = QPieSlicePrivate::fromSlice(slice)->m_data; | ||
Jani Honkonen
|
r673 | sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice); | ||
Jani Honkonen
|
r668 | sliceData.m_radius = m_pieRadius; | ||
return sliceData; | ||||
Jani Honkonen
|
r621 | } | ||
Jani Honkonen
|
r568 | #include "moc_piechartitem_p.cpp" | ||
Jani Honkonen
|
r142 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||