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