pieslice.cpp
135 lines
| 3.3 KiB
| text/x-c
|
CppLexer
|
r51 | #include "pieslice.h" | ||
|
r174 | #include "pieslicelabel.h" | ||
|
r146 | #include "piepresenter.h" | ||
|
r157 | #include "qpieseries.h" | ||
|
r203 | #include "qpieslice.h" | ||
|
r51 | #include <QPainter> | ||
#include <QDebug> | ||||
|
r157 | #include <qmath.h> | ||
|
r163 | #include <QGraphicsSceneEvent> | ||
|
r174 | #include <QTime> | ||
|
r51 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r157 | QPointF offset(qreal angle, qreal length) | ||
{ | ||||
qreal dx = qSin(angle*(PI/180)) * length; | ||||
qreal dy = qCos(angle*(PI/180)) * length; | ||||
return QPointF(dx, -dy); | ||||
} | ||||
|
r203 | PieSlice::PieSlice(QGraphicsItem* parent) | ||
|
r174 | :QGraphicsObject(parent), | ||
m_slicelabel(new PieSliceLabel(this)), | ||||
|
r203 | m_angle(0), | ||
|
r289 | m_angleSpan(0), | ||
m_isExploded(false), | ||||
m_explodeDistance(0) | ||||
|
r51 | { | ||
setAcceptHoverEvents(true); | ||||
|
r142 | setAcceptedMouseButtons(Qt::LeftButton); | ||
|
r51 | } | ||
PieSlice::~PieSlice() | ||||
{ | ||||
|
r203 | |||
|
r51 | } | ||
QRectF PieSlice::boundingRect() const | ||||
{ | ||||
|
r203 | return m_path.boundingRect(); | ||
|
r51 | } | ||
QPainterPath PieSlice::shape() const | ||||
{ | ||||
|
r157 | return m_path; | ||
|
r51 | } | ||
|
r142 | void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) | ||
|
r51 | { | ||
|
r203 | painter->setPen(m_pen); | ||
painter->setBrush(m_brush); | ||||
|
r157 | painter->drawPath(m_path); | ||
|
r51 | } | ||
|
r174 | void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) | ||
|
r51 | { | ||
|
r203 | emit hoverEnter(); | ||
|
r157 | } | ||
|
r163 | void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) | ||
|
r157 | { | ||
|
r203 | emit hoverLeave(); | ||
|
r51 | } | ||
|
r163 | void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) | ||
|
r142 | { | ||
|
r203 | emit clicked(); | ||
} | ||||
void PieSlice::setPieRect(QRectF rect) | ||||
{ | ||||
m_pieRect = rect; | ||||
|
r157 | } | ||
|
r203 | void PieSlice::updateGeometry() | ||
|
r157 | { | ||
|
r289 | if (!m_pieRect.isValid() || m_pieRect.isEmpty()) | ||
return; | ||||
|
r157 | prepareGeometryChange(); | ||
|
r174 | // calculate center angle | ||
|
r289 | qreal centerAngle = m_angle + (m_angleSpan/2); | ||
|
r157 | |||
|
r174 | // adjust rect for exploding | ||
|
r203 | QRectF rect = m_pieRect; | ||
if (m_isExploded) { | ||||
|
r289 | qreal dx = qSin(centerAngle*(PI/180)) * m_explodeDistance; | ||
qreal dy = -qCos(centerAngle*(PI/180)) * m_explodeDistance; | ||||
rect.translate(dx, dy); | ||||
|
r157 | } | ||
|
r174 | // update slice path | ||
// TODO: draw the shape so that it might have a hole in the center | ||||
|
r157 | QPainterPath path; | ||
path.moveTo(rect.center()); | ||||
|
r289 | path.arcTo(rect, -m_angle + 90, -m_angleSpan); | ||
|
r203 | path.closeSubpath(); | ||
|
r157 | m_path = path; | ||
|
r174 | // update label position | ||
|
r157 | qreal radius = rect.height() / 2; | ||
|
r174 | QPointF edgeCenter = rect.center() + offset(centerAngle, radius + 5); | ||
|
r181 | m_slicelabel->setArmStartPoint(edgeCenter); | ||
m_slicelabel->setArmAngle(centerAngle); | ||||
m_slicelabel->updateGeometry(); | ||||
|
r289 | m_slicelabel->update(); | ||
|
r174 | |||
|
r203 | //qDebug() << "PieSlice::updateGeometry" << m_slicelabel->text() << boundingRect() << m_angle << m_span; | ||
} | ||||
void PieSlice::updateData(const QPieSlice* sliceData) | ||||
|
r157 | { | ||
|
r203 | // TODO: compare what has changes to avoid unneccesary geometry updates | ||
|
r174 | |||
|
r203 | m_angle = sliceData->angle(); | ||
|
r289 | m_angleSpan = sliceData->angleSpan(); | ||
|
r203 | m_isExploded = sliceData->isExploded(); | ||
|
r289 | m_explodeDistance = sliceData->explodeDistance(); // TODO: expose to public API | ||
|
r203 | m_pen = sliceData->pen(); | ||
m_brush = sliceData->brush(); | ||||
|
r174 | |||
|
r203 | m_slicelabel->setVisible(sliceData->isLabelVisible()); | ||
m_slicelabel->setText(sliceData->label()); | ||||
m_slicelabel->setPen(sliceData->labelPen()); | ||||
m_slicelabel->setFont(sliceData->labelFont()); | ||||
|
r289 | m_slicelabel->setArmLength(sliceData->labelArmLength()); | ||
|
r209 | |||
updateGeometry(); | ||||
update(); | ||||
|
r157 | } | ||
|
r174 | #include "moc_pieslice.cpp" | ||
|
r51 | QTCOMMERCIALCHART_END_NAMESPACE | ||