pieslice.cpp
122 lines
| 2.9 KiB
| text/x-c
|
CppLexer
Tero Ahola
|
r51 | #include "pieslice.h" | ||
Jani Honkonen
|
r146 | #include "piepresenter.h" | ||
Jani Honkonen
|
r157 | #include "qpieseries.h" | ||
Tero Ahola
|
r51 | #include <QPainter> | ||
#include <QDebug> | ||||
Jani Honkonen
|
r157 | #include <qmath.h> | ||
Jani Honkonen
|
r163 | #include <QGraphicsSceneEvent> | ||
Tero Ahola
|
r51 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Jani Honkonen
|
r157 | #define PI 3.14159265 | ||
#define EXPLODE_OFFSET 20 | ||||
QPointF offset(qreal angle, qreal length) | ||||
{ | ||||
qreal dx = qSin(angle*(PI/180)) * length; | ||||
qreal dy = qCos(angle*(PI/180)) * length; | ||||
return QPointF(dx, -dy); | ||||
} | ||||
PieSlice::PieSlice(PiePresenter *presenter, int seriesIndex, qreal startAngle, qreal span) | ||||
:QGraphicsItem(presenter), | ||||
m_label(new QGraphicsTextItem(this)), | ||||
Jani Honkonen
|
r142 | m_seriesIndex(seriesIndex), | ||
Tero Ahola
|
r51 | m_startAngle(startAngle), | ||
Jani Honkonen
|
r142 | m_span(span) | ||
Tero Ahola
|
r51 | { | ||
Jani Honkonen
|
r157 | Q_ASSERT(presenter); | ||
Tero Ahola
|
r51 | setAcceptHoverEvents(true); | ||
Jani Honkonen
|
r142 | setAcceptedMouseButtons(Qt::LeftButton); | ||
Jani Honkonen
|
r157 | updateGeometry(); | ||
// TODO: use themes | ||||
m_pen = QPen(Qt::black); | ||||
m_brush = QBrush(sliceData().m_color); | ||||
Tero Ahola
|
r51 | } | ||
PieSlice::~PieSlice() | ||||
{ | ||||
} | ||||
QRectF PieSlice::boundingRect() const | ||||
{ | ||||
Jani Honkonen
|
r157 | return m_rect; | ||
Tero Ahola
|
r51 | } | ||
QPainterPath PieSlice::shape() const | ||||
{ | ||||
Jani Honkonen
|
r157 | return m_path; | ||
Tero Ahola
|
r51 | } | ||
Jani Honkonen
|
r142 | void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) | ||
Tero Ahola
|
r51 | { | ||
Tero Ahola
|
r77 | painter->setRenderHint(QPainter::Antialiasing); | ||
Jani Honkonen
|
r142 | |||
Jani Honkonen
|
r157 | // TODO: themes | ||
painter->setPen(m_pen); | ||||
painter->setBrush(m_brush); | ||||
painter->drawPath(m_path); | ||||
Jani Honkonen
|
r142 | |||
// Draw the label | ||||
// TODO: do this better | ||||
Jani Honkonen
|
r157 | //QTextItem text; | ||
painter->drawText(m_center, sliceData().m_label); | ||||
Tero Ahola
|
r51 | } | ||
Jani Honkonen
|
r163 | void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* event) | ||
Tero Ahola
|
r51 | { | ||
Jani Honkonen
|
r157 | m_brush = QBrush(sliceData().m_color.lighter()); | ||
update(); | ||||
} | ||||
Jani Honkonen
|
r163 | void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) | ||
Jani Honkonen
|
r157 | { | ||
m_brush = QBrush(sliceData().m_color); | ||||
update(); | ||||
Tero Ahola
|
r51 | } | ||
Jani Honkonen
|
r163 | void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r157 | QPieSlice data = sliceData(); | ||
data.m_isExploded = !data.m_isExploded; | ||||
(static_cast<PiePresenter*>(parentItem()))->m_pieSeries->update(m_seriesIndex, data); | ||||
} | ||||
void PieSlice::updateGeometry() | ||||
{ | ||||
prepareGeometryChange(); | ||||
PiePresenter *presenter = static_cast<PiePresenter*>(parentItem()); | ||||
QRectF rect = presenter->pieRect(); | ||||
rect.adjust(EXPLODE_OFFSET, EXPLODE_OFFSET, -EXPLODE_OFFSET ,-EXPLODE_OFFSET); | ||||
qreal centerAngle = m_startAngle + (m_span/2); | ||||
if (presenter->m_pieSeries->slice(m_seriesIndex).m_isExploded) { | ||||
QPointF d = offset((centerAngle), EXPLODE_OFFSET); | ||||
rect.translate(d.x(), d.y()); | ||||
} | ||||
qreal angle = (-m_startAngle) + (90); | ||||
qreal span = -m_span; | ||||
QPainterPath path; | ||||
path.moveTo(rect.center()); | ||||
path.arcTo(rect, angle, span); | ||||
// TODO: draw the shape so that it might have a hole in the center | ||||
m_path = path; | ||||
m_rect = path.boundingRect(); | ||||
qreal radius = rect.height() / 2; | ||||
m_center = rect.center() + offset(centerAngle, radius / 2); | ||||
Jani Honkonen
|
r142 | } | ||
Jani Honkonen
|
r157 | |||
QPieSlice PieSlice::sliceData() | ||||
{ | ||||
return (static_cast<PiePresenter*>(parentItem()))->m_pieSeries->slice(m_seriesIndex); | ||||
} | ||||
Tero Ahola
|
r51 | QTCOMMERCIALCHART_END_NAMESPACE | ||