piepresenter.cpp
170 lines
| 4.9 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r142 | |||
Jani Honkonen
|
r146 | #include "piepresenter.h" | ||
Jani Honkonen
|
r142 | #include "pieslice.h" | ||
#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) | ||||
{ | ||||
qDebug() << "PiePresenter::handleSeriesChanged()"; | ||||
qDebug() << " added : " << changeSet.m_added; | ||||
qDebug() << " changed: " << changeSet.m_changed; | ||||
qDebug() << " removed: " << changeSet.m_removed; | ||||
// ignore changeset when there are no visual slices | ||||
// changeset might not be valid about the added slices | ||||
if (m_slices.count() == 0) { | ||||
foreach (QPieSliceId id, m_series->m_slices.keys()) | ||||
addSlice(id); | ||||
return; | ||||
Jani Honkonen
|
r163 | } | ||
Jani Honkonen
|
r142 | |||
Jani Honkonen
|
r174 | foreach (QPieSliceId id, changeSet.m_removed) | ||
deleteSlice(id); | ||||
Jani Honkonen
|
r142 | |||
Jani Honkonen
|
r174 | foreach (QPieSliceId id, changeSet.m_changed) | ||
updateSlice(id); | ||||
foreach (QPieSliceId id, changeSet.m_added) | ||||
addSlice(id); | ||||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r157 | void PiePresenter::updateGeometry() | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r157 | prepareGeometryChange(); | ||
Jani Honkonen
|
r142 | m_pieRect = m_rect; | ||
if (m_pieRect.width() < m_pieRect.height()) { | ||||
Jani Honkonen
|
r174 | m_pieRect.setWidth(m_pieRect.width() * m_series->sizeFactor()); | ||
Jani Honkonen
|
r142 | m_pieRect.setHeight(m_pieRect.width()); | ||
m_pieRect.moveCenter(m_rect.center()); | ||||
} else { | ||||
Jani Honkonen
|
r174 | m_pieRect.setHeight(m_pieRect.height() * m_series->sizeFactor()); | ||
Jani Honkonen
|
r142 | m_pieRect.setWidth(m_pieRect.height()); | ||
m_pieRect.moveCenter(m_rect.center()); | ||||
} | ||||
Jani Honkonen
|
r174 | switch (m_series->position()) { | ||
Jani Honkonen
|
r142 | case QPieSeries::PiePositionTopLeft: { | ||
m_pieRect.setHeight(m_pieRect.height() / 2); | ||||
m_pieRect.setWidth(m_pieRect.height()); | ||||
m_pieRect.moveCenter(QPointF(m_rect.center().x() / 2, m_rect.center().y() / 2)); | ||||
break; | ||||
} | ||||
case QPieSeries::PiePositionTopRight: { | ||||
m_pieRect.setHeight(m_pieRect.height() / 2); | ||||
m_pieRect.setWidth(m_pieRect.height()); | ||||
m_pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, m_rect.center().y() / 2)); | ||||
break; | ||||
} | ||||
case QPieSeries::PiePositionBottomLeft: { | ||||
m_pieRect.setHeight(m_pieRect.height() / 2); | ||||
m_pieRect.setWidth(m_pieRect.height()); | ||||
m_pieRect.moveCenter(QPointF(m_rect.center().x() / 2, (m_rect.center().y() / 2) * 3)); | ||||
break; | ||||
} | ||||
case QPieSeries::PiePositionBottomRight: { | ||||
m_pieRect.setHeight(m_pieRect.height() / 2); | ||||
m_pieRect.setWidth(m_pieRect.height()); | ||||
m_pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, (m_rect.center().y() / 2) * 3)); | ||||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
Jani Honkonen
|
r174 | // update slice geometry | ||
const qreal fullPie = 360; | ||||
qreal angle = 0; | ||||
foreach (QPieSliceId id, m_slices.keys()) { | ||||
qreal span = fullPie * m_series->slice(id).percentage(); | ||||
m_slices[id]->updateGeometry(m_pieRect, angle, span); | ||||
angle += span; | ||||
} | ||||
//qDebug() << "PiePresenter::updateGeometry" << m_rect << m_pieRect; | ||||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r146 | void PiePresenter::handleDomainChanged(const Domain& domain) | ||
Jani Honkonen
|
r142 | { | ||
// TODO | ||||
} | ||||
Jani Honkonen
|
r146 | void PiePresenter::handleGeometryChanged(const QRectF& rect) | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r157 | m_rect = rect; | ||
updateGeometry(); | ||||
Jani Honkonen
|
r174 | } | ||
Jani Honkonen
|
r157 | |||
Jani Honkonen
|
r174 | void PiePresenter::addSlice(QPieSliceId id) | ||
{ | ||||
qDebug() << "PiePresenter::addSlice()" << id; | ||||
if (m_slices.contains(id)) { | ||||
qWarning() << "PiePresenter::addSlice(): slice already exists!" << id; | ||||
updateSlice(id); | ||||
return; | ||||
} | ||||
// create slice | ||||
PieSlice *slice = new PieSlice(id, m_series, this); | ||||
m_slices.insert(id, slice); | ||||
updateGeometry(); | ||||
} | ||||
void PiePresenter::updateSlice(QPieSliceId id) | ||||
{ | ||||
qDebug() << "PiePresenter::updateSlice()" << id; | ||||
// TODO: animation | ||||
if (m_slices.contains(id)) | ||||
m_slices.value(id)->updateData(); | ||||
else { | ||||
qWarning() << "PiePresenter::updateSlice(): slice does not exist!" << id; | ||||
addSlice(id); | ||||
} | ||||
updateGeometry(); | ||||
} | ||||
void PiePresenter::deleteSlice(QPieSliceId id) | ||||
{ | ||||
qDebug() << "PiePresenter::deleteSlice()" << id; | ||||
// TODO: animation | ||||
if (m_slices.contains(id)) | ||||
delete m_slices.take(id); | ||||
else | ||||
qWarning() << "PiePresenter::deleteSlice(): slice does not exist!" << id; | ||||
updateGeometry(); | ||||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r146 | #include "moc_piepresenter.cpp" | ||
Jani Honkonen
|
r142 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||