qpieseries.cpp
148 lines
| 4.1 KiB
| text/x-c
|
CppLexer
/ src / qpieseries.cpp
|
r51 | #include "qpieseries.h" | ||
#include "pieslice.h" | ||||
#include <QGraphicsObject> | ||||
#include <QDebug> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r61 | QPieSeries::QPieSeries(QGraphicsObject *parent) : | ||
|
r51 | QChartSeries(parent), | ||
|
r77 | m_sizeFactor(1.0), | ||
m_position(PiePositionMaximized) | ||||
|
r51 | { | ||
|
r61 | } | ||
QPieSeries::~QPieSeries() | ||||
{ | ||||
while (m_slices.count()) | ||||
delete m_slices.takeLast(); | ||||
} | ||||
bool QPieSeries::setData(QList<qreal> data) | ||||
{ | ||||
m_data = data; | ||||
|
r54 | // Create slices | ||
|
r51 | qreal fullPie = 360; | ||
qreal total = 0; | ||||
|
r61 | foreach (qreal value, m_data) | ||
|
r51 | total += value; | ||
|
r61 | QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent()); | ||
|
r51 | Q_ASSERT(parentItem); | ||
|
r60 | m_chartSize = parentItem->boundingRect(); | ||
|
r51 | qreal angle = 0; | ||
|
r54 | // TODO: no need to create new slices in case size changed; we should re-use the existing ones | ||
|
r61 | foreach (qreal value, m_data) { | ||
|
r51 | qreal span = value / total * fullPie; | ||
|
r75 | PieSlice *slice = new PieSlice(QColor(), angle, span, parentItem->boundingRect()); | ||
|
r51 | slice->setParentItem(parentItem); | ||
m_slices.append(slice); | ||||
angle += span; | ||||
} | ||||
|
r60 | |||
resizeSlices(m_chartSize); | ||||
|
r61 | return true; | ||
|
r54 | } | ||
|
r75 | void QPieSeries::setSliceColor(int index, QColor color) | ||
{ | ||||
if (index >= 0 && index < m_slices.count()) | ||||
m_slices.at(index)->m_color = color; | ||||
} | ||||
QColor QPieSeries::sliceColor(int index) | ||||
{ | ||||
if (index >= 0 && index < m_slices.count()) | ||||
return m_slices.at(index)->m_color; | ||||
else | ||||
return QColor(); | ||||
} | ||||
int QPieSeries::sliceCount() | ||||
{ | ||||
return m_slices.count(); | ||||
} | ||||
|
r60 | void QPieSeries::chartSizeChanged(QRectF chartRect) | ||
|
r54 | { | ||
|
r60 | // TODO: allow user setting the size? | ||
// TODO: allow user defining the margins? | ||||
m_chartSize = chartRect; | ||||
resizeSlices(m_chartSize); | ||||
} | ||||
void QPieSeries::resizeSlices(QRectF rect) | ||||
{ | ||||
QRectF tempRect = rect; | ||||
if (tempRect.width() < tempRect.height()) { | ||||
tempRect.setWidth(tempRect.width() * m_sizeFactor); | ||||
tempRect.setHeight(tempRect.width()); | ||||
tempRect.moveCenter(rect.center()); | ||||
} else { | ||||
tempRect.setHeight(tempRect.height() * m_sizeFactor); | ||||
tempRect.setWidth(tempRect.height()); | ||||
tempRect.moveCenter(rect.center()); | ||||
} | ||||
|
r77 | switch (m_position) { | ||
case PiePositionTopLeft: { | ||||
tempRect.setHeight(tempRect.height() / 2); | ||||
tempRect.setWidth(tempRect.height()); | ||||
tempRect.moveCenter(QPointF(rect.center().x() / 2, rect.center().y() / 2)); | ||||
break; | ||||
} | ||||
case PiePositionTopRight: { | ||||
tempRect.setHeight(tempRect.height() / 2); | ||||
tempRect.setWidth(tempRect.height()); | ||||
tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, rect.center().y() / 2)); | ||||
break; | ||||
} | ||||
case PiePositionBottomLeft: { | ||||
tempRect.setHeight(tempRect.height() / 2); | ||||
tempRect.setWidth(tempRect.height()); | ||||
tempRect.moveCenter(QPointF(rect.center().x() / 2, (rect.center().y() / 2) * 3)); | ||||
break; | ||||
} | ||||
case PiePositionBottomRight: { | ||||
tempRect.setHeight(tempRect.height() / 2); | ||||
tempRect.setWidth(tempRect.height()); | ||||
tempRect.moveCenter(QPointF((rect.center().x() / 2) * 3, (rect.center().y() / 2) * 3)); | ||||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
|
r60 | foreach (PieSlice *slice, m_slices) | ||
slice->m_rect = tempRect; | ||||
} | ||||
void QPieSeries::setSizeFactor(qreal factor) | ||||
{ | ||||
if (factor > 0.0) | ||||
m_sizeFactor = factor; | ||||
resizeSlices(m_chartSize); | ||||
// Initiate update via the parent graphics item | ||||
// TODO: potential issue: what if this function is called from the parent context? | ||||
QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent()); | ||||
Q_ASSERT(parentItem); | ||||
parentItem->update(); | ||||
|
r54 | } | ||
|
r77 | void QPieSeries::setPosition(PiePosition position) | ||
{ | ||||
m_position = position; | ||||
resizeSlices(m_chartSize); | ||||
// Initiate update via the parent graphics item | ||||
// TODO: potential issue: what if this function is called from the parent context? | ||||
QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent()); | ||||
Q_ASSERT(parentItem); | ||||
parentItem->update(); | ||||
} | ||||
|
r51 | #include "moc_qpieseries.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||