piepresenter.cpp
113 lines
| 3.3 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r142 | |||
Jani Honkonen
|
r146 | #include "piepresenter.h" | ||
Jani Honkonen
|
r142 | #include "pieslice.h" | ||
#include <QDebug> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Jani Honkonen
|
r146 | PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) : | ||
Jani Honkonen
|
r142 | ChartItem(parent), | ||
m_pieSeries(series) | ||||
{ | ||||
Q_ASSERT(parent); | ||||
Q_ASSERT(series); | ||||
m_rect = parentItem()->boundingRect(); | ||||
setAcceptHoverEvents(true); | ||||
} | ||||
Jani Honkonen
|
r146 | PiePresenter::~PiePresenter() | ||
Jani Honkonen
|
r142 | { | ||
while (m_slices.count()) | ||||
delete m_slices.takeLast(); | ||||
} | ||||
Jani Honkonen
|
r157 | void PiePresenter::handleSeriesChanged(const PieChangeSet& changeSet) | ||
Jani Honkonen
|
r142 | { | ||
const qreal fullPie = 360; | ||||
qreal total = 0; | ||||
// calculate total | ||||
foreach (QPieSlice sliceData, m_pieSeries->slices()) | ||||
total += sliceData.m_value; | ||||
// TODO: no need to create new slices in case size changed; we should re-use the existing ones | ||||
while (m_slices.count()) | ||||
delete m_slices.takeLast(); | ||||
// create slices | ||||
qreal angle = 0; | ||||
for (int i=0; i<m_pieSeries->count(); i++) { | ||||
QPieSlice sliceData = m_pieSeries->slice(i); | ||||
qreal span = sliceData.m_value / total * fullPie; | ||||
PieSlice *slice = new PieSlice(this, i, angle, span); | ||||
m_slices.append(slice); | ||||
angle += span; | ||||
} | ||||
} | ||||
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
|
r157 | m_pieRect.setWidth(m_pieRect.width() * m_pieSeries->sizeFactor()); | ||
Jani Honkonen
|
r142 | m_pieRect.setHeight(m_pieRect.width()); | ||
m_pieRect.moveCenter(m_rect.center()); | ||||
} else { | ||||
Jani Honkonen
|
r157 | m_pieRect.setHeight(m_pieRect.height() * m_pieSeries->sizeFactor()); | ||
Jani Honkonen
|
r142 | m_pieRect.setWidth(m_pieRect.height()); | ||
m_pieRect.moveCenter(m_rect.center()); | ||||
} | ||||
Jani Honkonen
|
r157 | switch (m_pieSeries->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; | ||||
} | ||||
qDebug() << "presentation rect:" << m_rect; | ||||
qDebug() << "pie rect:" << m_pieRect; | ||||
Jani Honkonen
|
r157 | foreach (PieSlice *slice, m_slices) | ||
slice->updateGeometry(); | ||||
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
|
r142 | } | ||
Jani Honkonen
|
r146 | #include "moc_piepresenter.cpp" | ||
Jani Honkonen
|
r142 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||