piepresenter.cpp
158 lines
| 4.4 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r353 | #include "piepresenter_p.h" | ||
#include "pieslice_p.h" | ||||
Jani Honkonen
|
r203 | #include "qpieslice.h" | ||
Jani Honkonen
|
r289 | #include "qpieseries.h" | ||
Tero Ahola
|
r490 | #include "chartpresenter_p.h" | ||
Jani Honkonen
|
r142 | #include <QDebug> | ||
Jani Honkonen
|
r437 | #include <QPainter> | ||
Jani Honkonen
|
r289 | |||
Jani Honkonen
|
r142 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Jani Honkonen
|
r174 | PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) | ||
:ChartItem(parent), | ||||
m_series(series) | ||||
Jani Honkonen
|
r142 | { | ||
Q_ASSERT(series); | ||||
Jani Honkonen
|
r174 | connect(series, SIGNAL(changed(const QPieSeries::ChangeSet&)), this, SLOT(handleSeriesChanged(const QPieSeries::ChangeSet&))); | ||
Jani Honkonen
|
r498 | connect(series, SIGNAL(piePositionChanged()), this, SLOT(updateGeometry())); | ||
connect(series, SIGNAL(pieSizeChanged()), this, SLOT(updateGeometry())); | ||||
Jani Honkonen
|
r289 | |||
if (m_series->count()) { | ||||
QPieSeries::ChangeSet changeSet; | ||||
changeSet.appendAdded(m_series->m_slices); | ||||
handleSeriesChanged(changeSet); | ||||
} | ||||
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
|
r142 | } | ||
Jani Honkonen
|
r146 | PiePresenter::~PiePresenter() | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r174 | // slices deleted automatically through QGraphicsItem | ||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r289 | void PiePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r174 | // TODO: paint shadows for all components | ||
// - get paths from items & merge & offset and draw with shadow color? | ||||
Jani Honkonen
|
r437 | //painter->setBrush(QBrush(Qt::red)); | ||
//painter->drawRect(m_debugRect); | ||||
Jani Honkonen
|
r174 | } | ||
void PiePresenter::handleSeriesChanged(const QPieSeries::ChangeSet& changeSet) | ||||
{ | ||||
Jani Honkonen
|
r203 | //qDebug() << "PiePresenter::handleSeriesChanged()"; | ||
//qDebug() << " added : " << changeSet.added(); | ||||
//qDebug() << " changed: " << changeSet.changed(); | ||||
//qDebug() << " removed: " << changeSet.removed(); | ||||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r289 | foreach (QPieSlice* s, changeSet.added()) | ||
addSlice(s); | ||||
foreach (QPieSlice* s, changeSet.changed()) | ||||
updateSlice(s); | ||||
Jani Honkonen
|
r142 | |||
Jani Honkonen
|
r203 | foreach (QPieSlice* s, changeSet.removed()) | ||
deleteSlice(s); | ||||
Jani Honkonen
|
r142 | |||
Jani Honkonen
|
r289 | // every change possibly changes the actual pie size | ||
updateGeometry(); | ||||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r203 | void PiePresenter::handleDomainChanged(const Domain& domain) | ||
{ | ||||
// TODO | ||||
} | ||||
void PiePresenter::handleGeometryChanged(const QRectF& rect) | ||||
{ | ||||
m_rect = rect; | ||||
Jani Honkonen
|
r289 | prepareGeometryChange(); | ||
Jani Honkonen
|
r203 | updateGeometry(); | ||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r157 | void PiePresenter::updateGeometry() | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r289 | if (!m_rect.isValid() || m_rect.isEmpty()) | ||
return; | ||||
Jani Honkonen
|
r157 | |||
Jani Honkonen
|
r454 | // find pie center coordinates | ||
QPointF center; | ||||
Jani Honkonen
|
r498 | center.setX(m_rect.left() + (m_rect.width() * m_series->pieHorizontalPosition())); | ||
center.setY(m_rect.top() + (m_rect.height() * m_series->pieVerticalPosition())); | ||||
Jani Honkonen
|
r437 | |||
Jani Honkonen
|
r454 | // find maximum radius for pie | ||
qreal radius = m_rect.height() / 2; | ||||
if (m_rect.width() < m_rect.height()) | ||||
radius = m_rect.width() / 2; | ||||
Jani Honkonen
|
r437 | |||
Jani Honkonen
|
r454 | // apply size factor | ||
Jani Honkonen
|
r498 | radius *= m_series->pieSize(); | ||
Jani Honkonen
|
r437 | |||
Jani Honkonen
|
r289 | // update slices | ||
Jani Honkonen
|
r454 | if (m_pieCenter != center || m_pieRadius != radius) { | ||
m_pieCenter = center; | ||||
m_pieRadius = radius; | ||||
//qDebug() << "PiePresenter::updateGeometry()" << m_rect << m_pieCenter << m_pieRadius; | ||||
Jani Honkonen
|
r203 | foreach (PieSlice* s, m_slices.values()) { | ||
Jani Honkonen
|
r454 | s->setPieCenterAndRadius(center, radius); | ||
Jani Honkonen
|
r203 | s->updateGeometry(); | ||
Jani Honkonen
|
r289 | s->update(); | ||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r174 | } | ||
Jani Honkonen
|
r437 | |||
update(); | ||||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r203 | void PiePresenter::addSlice(QPieSlice* sliceData) | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r203 | //qDebug() << "PiePresenter::addSlice()" << sliceData; | ||
Jani Honkonen
|
r142 | |||
Jani Honkonen
|
r203 | if (m_slices.keys().contains(sliceData)) { | ||
Jani Honkonen
|
r289 | Q_ASSERT(0); // TODO: how to handle this nicely? | ||
Jani Honkonen
|
r174 | return; | ||
} | ||||
// create slice | ||||
Jani Honkonen
|
r203 | PieSlice *slice = new PieSlice(this); | ||
Jani Honkonen
|
r454 | slice->setPieCenterAndRadius(m_pieCenter, m_pieRadius); | ||
Jani Honkonen
|
r203 | slice->updateData(sliceData); | ||
Jani Honkonen
|
r209 | slice->updateGeometry(); | ||
slice->update(); | ||||
Jani Honkonen
|
r203 | m_slices.insert(sliceData, slice); | ||
// connect signals | ||||
connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked())); | ||||
connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter())); | ||||
connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave())); | ||||
Jani Honkonen
|
r174 | } | ||
Jani Honkonen
|
r289 | void PiePresenter::updateSlice(QPieSlice* sliceData) | ||
{ | ||||
//qDebug() << "PiePresenter::updateSlice()" << sliceData; | ||||
if (!m_slices.contains(sliceData)) { | ||||
Q_ASSERT(0); // TODO: how to handle this nicely? | ||||
return; | ||||
} | ||||
m_slices[sliceData]->updateData(sliceData); | ||||
} | ||||
Jani Honkonen
|
r203 | void PiePresenter::deleteSlice(QPieSlice* sliceData) | ||
Jani Honkonen
|
r174 | { | ||
Jani Honkonen
|
r203 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; | ||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r289 | if (!m_slices.contains(sliceData)) { | ||
Q_ASSERT(0); // TODO: how to handle this nicely? | ||||
return; | ||||
Jani Honkonen
|
r174 | } | ||
Jani Honkonen
|
r289 | |||
delete m_slices.take(sliceData); | ||||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r353 | #include "moc_piepresenter_p.cpp" | ||
Jani Honkonen
|
r142 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||