piepresenter.cpp
158 lines
| 4.8 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r142 | |||
Jani Honkonen
|
r146 | #include "piepresenter.h" | ||
Jani Honkonen
|
r142 | #include "pieslice.h" | ||
Jani Honkonen
|
r203 | #include "qpieslice.h" | ||
Jani Honkonen
|
r142 | #include <QDebug> | ||
Jani Honkonen
|
r163 | #include <QTime> | ||
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
|
r163 | connect(series, SIGNAL(sizeFactorChanged()), this, SLOT(updateGeometry())); | ||
connect(series, SIGNAL(positionChanged()), this, SLOT(updateGeometry())); | ||||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r146 | PiePresenter::~PiePresenter() | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r174 | // slices deleted automatically through QGraphicsItem | ||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r174 | void PiePresenter::paint(QPainter *, 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? | ||||
} | ||||
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 | |||
// ignore changeset when there are no visual slices | ||||
// changeset might not be valid about the added slices | ||||
if (m_slices.count() == 0) { | ||||
Jani Honkonen
|
r203 | foreach (QPieSlice* s, m_series->m_slices) | ||
addSlice(s); | ||||
Jani Honkonen
|
r174 | return; | ||
Jani Honkonen
|
r163 | } | ||
Jani Honkonen
|
r142 | |||
Jani Honkonen
|
r203 | foreach (QPieSlice* s, changeSet.removed()) | ||
deleteSlice(s); | ||||
Jani Honkonen
|
r142 | |||
Jani Honkonen
|
r203 | foreach (QPieSlice* s, changeSet.added()) | ||
addSlice(s); | ||||
} | ||||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r203 | void PiePresenter::handleDomainChanged(const Domain& domain) | ||
{ | ||||
// TODO | ||||
} | ||||
void PiePresenter::handleGeometryChanged(const QRectF& rect) | ||||
{ | ||||
m_rect = rect; | ||||
updateGeometry(); | ||||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r157 | void PiePresenter::updateGeometry() | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r157 | prepareGeometryChange(); | ||
Jani Honkonen
|
r203 | QRectF pieRect = m_rect; | ||
Jani Honkonen
|
r142 | |||
Jani Honkonen
|
r203 | if (pieRect.width() < pieRect.height()) { | ||
pieRect.setWidth(pieRect.width() * m_series->sizeFactor()); | ||||
pieRect.setHeight(pieRect.width()); | ||||
pieRect.moveCenter(m_rect.center()); | ||||
Jani Honkonen
|
r142 | } else { | ||
Jani Honkonen
|
r203 | pieRect.setHeight(pieRect.height() * m_series->sizeFactor()); | ||
pieRect.setWidth(pieRect.height()); | ||||
pieRect.moveCenter(m_rect.center()); | ||||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r174 | switch (m_series->position()) { | ||
Jani Honkonen
|
r142 | case QPieSeries::PiePositionTopLeft: { | ||
Jani Honkonen
|
r203 | pieRect.setHeight(pieRect.height() / 2); | ||
pieRect.setWidth(pieRect.height()); | ||||
pieRect.moveCenter(QPointF(m_rect.center().x() / 2, m_rect.center().y() / 2)); | ||||
Jani Honkonen
|
r142 | break; | ||
} | ||||
case QPieSeries::PiePositionTopRight: { | ||||
Jani Honkonen
|
r203 | pieRect.setHeight(pieRect.height() / 2); | ||
pieRect.setWidth(pieRect.height()); | ||||
pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, m_rect.center().y() / 2)); | ||||
Jani Honkonen
|
r142 | break; | ||
} | ||||
case QPieSeries::PiePositionBottomLeft: { | ||||
Jani Honkonen
|
r203 | pieRect.setHeight(pieRect.height() / 2); | ||
pieRect.setWidth(pieRect.height()); | ||||
pieRect.moveCenter(QPointF(m_rect.center().x() / 2, (m_rect.center().y() / 2) * 3)); | ||||
Jani Honkonen
|
r142 | break; | ||
} | ||||
case QPieSeries::PiePositionBottomRight: { | ||||
Jani Honkonen
|
r203 | pieRect.setHeight(pieRect.height() / 2); | ||
pieRect.setWidth(pieRect.height()); | ||||
pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, (m_rect.center().y() / 2) * 3)); | ||||
Jani Honkonen
|
r142 | break; | ||
} | ||||
default: | ||||
break; | ||||
} | ||||
Jani Honkonen
|
r203 | if (m_pieRect != pieRect) { | ||
m_pieRect = pieRect; | ||||
//qDebug() << "PiePresenter::updateGeometry()" << m_pieRect; | ||||
foreach (PieSlice* s, m_slices.values()) { | ||||
s->setPieRect(m_pieRect); | ||||
s->updateGeometry(); | ||||
} | ||||
Jani Honkonen
|
r174 | } | ||
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)) { | ||
//qWarning() << "PiePresenter::addSlice(): slice already exists!" << sliceData; | ||||
Q_ASSERT(0); | ||||
Jani Honkonen
|
r174 | return; | ||
} | ||||
// create slice | ||||
Jani Honkonen
|
r203 | PieSlice *slice = new PieSlice(this); | ||
Jani Honkonen
|
r208 | slice->setPieRect(m_pieRect); | ||
Jani Honkonen
|
r203 | slice->updateData(sliceData); | ||
Jani Honkonen
|
r209 | slice->updateGeometry(); | ||
slice->update(); | ||||
Jani Honkonen
|
r203 | m_slices.insert(sliceData, slice); | ||
// connect signals | ||||
connect(sliceData, SIGNAL(changed()), slice, SLOT(handleSliceDataChanged())); | ||||
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
|
r203 | void PiePresenter::deleteSlice(QPieSlice* sliceData) | ||
Jani Honkonen
|
r174 | { | ||
Jani Honkonen
|
r203 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; | ||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r203 | if (m_slices.contains(sliceData)) | ||
delete m_slices.take(sliceData); | ||||
Jani Honkonen
|
r174 | else { | ||
Jani Honkonen
|
r203 | // nothing to remove | ||
Q_ASSERT(0); // TODO: remove before release | ||||
Jani Honkonen
|
r174 | } | ||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r146 | #include "moc_piepresenter.cpp" | ||
Jani Honkonen
|
r142 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||