piechartitem.cpp
228 lines
| 7.7 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r794 | /**************************************************************************** | ||
** | ||||
Miikka Heikkinen
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
Jani Honkonen
|
r794 | ** 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" | ||
Michal Klocek
|
r1735 | #include "pieanimation_p.h" | ||
Jani Honkonen
|
r437 | #include <QPainter> | ||
Jani Honkonen
|
r621 | #include <QTimer> | ||
Jani Honkonen
|
r142 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Michal Klocek
|
r2273 | PieChartItem::PieChartItem(QPieSeries *series, QGraphicsItem* item) | ||
: ChartItem(series->d_func(),item), | ||||
Jani Honkonen
|
r2097 | m_series(series), | ||
m_animation(0) | ||||
Jani Honkonen
|
r142 | { | ||
Q_ASSERT(series); | ||||
Jani Honkonen
|
r1009 | |||
Tero Ahola
|
r1482 | QPieSeriesPrivate *p = QPieSeriesPrivate::fromSeries(series); | ||
Jani Honkonen
|
r1456 | connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); | ||
Tero Ahola
|
r2067 | connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged())); | ||
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*>))); | ||||
Tero Ahola
|
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())); | ||||
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 | ||
Miikka Heikkinen
|
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); | ||||
} | ||||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r2104 | void PieChartItem::setAnimation(PieAnimation *animation) | ||
Michal Klocek
|
r1735 | { | ||
Jani Honkonen
|
r2097 | m_animation = animation; | ||
Michal Klocek
|
r1735 | } | ||
Jani Honkonen
|
r2104 | ChartAnimation *PieChartItem::animation() const | ||
Michal Klocek
|
r1735 | { | ||
return m_animation; | ||||
} | ||||
Jani Honkonen
|
r1815 | void PieChartItem::handleDomainUpdated() | ||
{ | ||||
Michal Klocek
|
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()); | ||||
} | ||||
Jani Honkonen
|
r1191 | } | ||
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; | ||||
Marek Rosa
|
r1838 | m_holeSize = m_pieRadius; | ||
Jani Honkonen
|
r1074 | // apply size factor | ||
m_pieRadius *= m_series->pieSize(); | ||||
Marek Rosa
|
r1838 | m_holeSize *= m_series->holeSize(); | ||
Jani Honkonen
|
r1074 | |||
// set layouts for existing slice items | ||||
Jani Honkonen
|
r2097 | foreach (QPieSlice *slice, m_series->slices()) { | ||
Jani Honkonen
|
r1074 | PieSliceItem *sliceItem = m_sliceItems.value(slice); | ||
if (sliceItem) { | ||||
PieSliceData sliceData = updateSliceGeometry(slice); | ||||
Jani Honkonen
|
r2097 | if (m_animation) | ||
Michal Klocek
|
r1735 | presenter()->startAnimation(m_animation->updateValue(sliceItem, sliceData)); | ||
Jani Honkonen
|
r1074 | else | ||
sliceItem->setLayout(sliceData); | ||||
} | ||||
} | ||||
update(); | ||||
} | ||||
Jani Honkonen
|
r2104 | void PieChartItem::handleSlicesAdded(QList<QPieSlice *> slices) | ||
Jani Honkonen
|
r1074 | { | ||
Jani Honkonen
|
r1238 | // delay creating slice items until there is a proper rectangle | ||
if (!m_rect.isValid() && m_sliceItems.isEmpty()) | ||||
return; | ||||
Michal Klocek
|
r2273 | themeManager()->updateSeries(m_series); | ||
Jani Honkonen
|
r659 | |||
Jani Honkonen
|
r1074 | bool startupAnimation = m_sliceItems.isEmpty(); | ||
Jani Honkonen
|
r621 | |||
Jani Honkonen
|
r2104 | foreach(QPieSlice * slice, slices) { | ||
PieSliceItem *sliceItem = new PieSliceItem(this); | ||||
Jani Honkonen
|
r1074 | m_sliceItems.insert(slice, sliceItem); | ||
Jani Honkonen
|
r1224 | |||
Tero Ahola
|
r1494 | // Note: no need to connect to slice valueChanged() etc. | ||
Jani Honkonen
|
r1274 | // 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(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())); | ||
Tero Ahola
|
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())); | ||||
Jani Honkonen
|
r1274 | |||
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
|
r1735 | if (m_animation) | ||
presenter()->startAnimation(m_animation->addSlice(sliceItem, sliceData, startupAnimation)); | ||||
Jani Honkonen
|
r621 | else | ||
Jani Honkonen
|
r1074 | sliceItem->setLayout(sliceData); | ||
Jani Honkonen
|
r621 | } | ||
} | ||||
Jani Honkonen
|
r2104 | void PieChartItem::handleSlicesRemoved(QList<QPieSlice *> slices) | ||
Jani Honkonen
|
r621 | { | ||
Michal Klocek
|
r2273 | themeManager()->updateSeries(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
|
r2084 | slice->disconnect(this); | ||
QPieSlicePrivate::fromSlice(slice)->disconnect(this); | ||||
Jani Honkonen
|
r1053 | |||
Michal Klocek
|
r1735 | if (m_animation) | ||
presenter()->startAnimation(m_animation->removeSlice(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
|
r2104 | QPieSlice *slice = qobject_cast<QPieSlice *>(sender()); | ||
Tero Ahola
|
r1494 | if (!slice) { | ||
Jani Honkonen
|
r2104 | QPieSlicePrivate *slicep = qobject_cast<QPieSlicePrivate *>(sender()); | ||
Tero Ahola
|
r1494 | slice = slicep->q_ptr; | ||
} | ||||
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); | ||||
Michal Klocek
|
r1735 | if (m_animation) | ||
presenter()->startAnimation(m_animation->updateValue(sliceItem, sliceData)); | ||||
Jani Honkonen
|
r1074 | else | ||
sliceItem->setLayout(sliceData); | ||||
Jani Honkonen
|
r437 | |||
Jani Honkonen
|
r1074 | update(); | ||
Jani Honkonen
|
r621 | } | ||
Jani Honkonen
|
r437 | |||
Jani Honkonen
|
r1456 | void PieChartItem::handleSeriesVisibleChanged() | ||
{ | ||||
setVisible(m_series->isVisible()); | ||||
} | ||||
Tero Ahola
|
r2067 | void PieChartItem::handleOpacityChanged() | ||
{ | ||||
setOpacity(m_series->opacity()); | ||||
} | ||||
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; | ||
Marek Rosa
|
r1838 | sliceData.m_holeRadius = m_holeSize; | ||
Jani Honkonen
|
r668 | return sliceData; | ||
Jani Honkonen
|
r621 | } | ||
Jani Honkonen
|
r568 | #include "moc_piechartitem_p.cpp" | ||
Jani Honkonen
|
r142 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||