pieslice.cpp
210 lines
| 5.6 KiB
| text/x-c
|
CppLexer
|
r353 | #include "pieslice_p.h" | ||
#include "piepresenter_p.h" | ||||
|
r157 | #include "qpieseries.h" | ||
|
r203 | #include "qpieslice.h" | ||
|
r490 | #include "chartpresenter_p.h" | ||
|
r51 | #include <QPainter> | ||
#include <QDebug> | ||||
|
r157 | #include <qmath.h> | ||
|
r163 | #include <QGraphicsSceneEvent> | ||
|
r174 | #include <QTime> | ||
|
r51 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r454 | #define PI 3.14159265 // TODO: is this defined in some header? | ||
|
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), | ||
|
r454 | m_pieRadius(0), | ||
|
r355 | m_startAngle(0), | ||
|
r289 | m_angleSpan(0), | ||
m_isExploded(false), | ||||
|
r454 | m_explodeDistanceFactor(0), | ||
m_labelVisible(false), | ||||
m_labelArmLengthFactor(0) | ||||
|
r51 | { | ||
setAcceptHoverEvents(true); | ||||
|
r142 | setAcceptedMouseButtons(Qt::LeftButton); | ||
|
r490 | setZValue(ChartPresenter::PieSeriesZValue); | ||
|
r51 | } | ||
PieSlice::~PieSlice() | ||||
{ | ||||
|
r203 | |||
|
r51 | } | ||
QRectF PieSlice::boundingRect() const | ||||
{ | ||||
|
r437 | return m_slicePath.boundingRect(); | ||
|
r51 | } | ||
QPainterPath PieSlice::shape() const | ||||
{ | ||||
|
r454 | // Don't include the label and label arm. | ||
// This is used to detect a mouse clicks. We do not want clicks from label. | ||||
|
r437 | return m_slicePath; | ||
|
r51 | } | ||
|
r142 | void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) | ||
|
r51 | { | ||
|
r454 | painter->setClipRect(parentItem()->boundingRect()); | ||
|
r437 | painter->save(); | ||
|
r469 | painter->setPen(m_slicePen); | ||
painter->setBrush(m_sliceBrush); | ||||
|
r437 | painter->drawPath(m_slicePath); | ||
painter->restore(); | ||||
if (m_labelVisible) { | ||||
painter->save(); | ||||
painter->setPen(m_labelArmPen); | ||||
painter->drawPath(m_labelArmPath); | ||||
painter->restore(); | ||||
painter->setFont(m_labelFont); | ||||
painter->drawText(m_labelTextRect.bottomLeft(), m_labelText); | ||||
} | ||||
|
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(); | ||
} | ||||
|
r454 | void PieSlice::setPieCenterAndRadius(QPointF center, qreal radius) | ||
|
r203 | { | ||
|
r454 | m_pieCenter = center; | ||
m_pieRadius = radius; | ||||
|
r157 | } | ||
|
r203 | void PieSlice::updateGeometry() | ||
|
r157 | { | ||
|
r454 | if (m_pieRadius <= 0) | ||
|
r289 | return; | ||
|
r157 | prepareGeometryChange(); | ||
|
r437 | // update slice path | ||
qreal centerAngle; | ||||
QPointF armStart; | ||||
|
r454 | m_slicePath = slicePath(m_pieCenter, m_pieRadius, m_startAngle, m_angleSpan, m_isExploded, m_pieRadius * m_explodeDistanceFactor, ¢erAngle, &armStart); | ||
|
r157 | |||
|
r437 | // update text rect | ||
m_labelTextRect = labelTextRect(m_labelFont, m_labelText); | ||||
|
r157 | |||
|
r437 | // update label arm path | ||
QPointF labelTextStart; | ||||
|
r454 | m_labelArmPath = labelArmPath(armStart, centerAngle, m_pieRadius * m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); | ||
|
r157 | |||
|
r437 | // update text position | ||
m_labelTextRect.moveBottomLeft(labelTextStart); | ||||
|
r174 | |||
|
r454 | //qDebug() << "PieSlice::updateGeometry" << m_labelText << boundingRect() << m_startAngle << m_startAngle + m_angleSpan; | ||
|
r203 | } | ||
void PieSlice::updateData(const QPieSlice* sliceData) | ||||
|
r157 | { | ||
|
r203 | // TODO: compare what has changes to avoid unneccesary geometry updates | ||
|
r174 | |||
|
r355 | m_startAngle = sliceData->startAngle(); | ||
m_angleSpan = sliceData->m_angleSpan; | ||||
|
r203 | m_isExploded = sliceData->isExploded(); | ||
|
r454 | m_explodeDistanceFactor = sliceData->explodeDistanceFactor(); | ||
|
r469 | m_slicePen = sliceData->slicePen(); | ||
m_sliceBrush = sliceData->sliceBrush(); | ||||
|
r174 | |||
|
r437 | m_labelVisible = sliceData->isLabelVisible(); | ||
m_labelText = sliceData->label(); | ||||
m_labelFont = sliceData->labelFont(); | ||||
|
r454 | m_labelArmLengthFactor = sliceData->labelArmLengthFactor(); | ||
|
r469 | m_labelArmPen = sliceData->labelArmPen(); | ||
|
r209 | |||
updateGeometry(); | ||||
update(); | ||||
|
r157 | } | ||
|
r437 | QPainterPath PieSlice::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, bool exploded, qreal explodeDistance, qreal* centerAngle, QPointF* armStart) | ||
{ | ||||
// calculate center angle | ||||
*centerAngle = startAngle + (angleSpan/2); | ||||
// calculate slice rectangle | ||||
QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2); | ||||
// adjust rect for exploding | ||||
if (exploded) { | ||||
qreal dx = qSin(*centerAngle*(PI/180)) * explodeDistance; | ||||
qreal dy = -qCos(*centerAngle*(PI/180)) * explodeDistance; | ||||
rect.translate(dx, dy); | ||||
} | ||||
// slice path | ||||
// TODO: draw the shape so that it might have a hole in the center | ||||
QPainterPath path; | ||||
path.moveTo(rect.center()); | ||||
path.arcTo(rect, -startAngle + 90, -angleSpan); | ||||
path.closeSubpath(); | ||||
// calculate label arm start point | ||||
*armStart = center; | ||||
if (exploded) | ||||
*armStart += offset(*centerAngle, explodeDistance + radius + PIESLICE_LABEL_GAP); | ||||
else | ||||
*armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP); | ||||
return path; | ||||
} | ||||
QPainterPath PieSlice::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF* textStart) | ||||
{ | ||||
qreal dx = qSin(angle*(PI/180)) * length; | ||||
qreal dy = -qCos(angle*(PI/180)) * length; | ||||
QPointF parm1 = start + QPointF(dx, dy); | ||||
QPointF parm2 = parm1; | ||||
if (angle < 180) { // arm swings the other way on the left side | ||||
parm2 += QPointF(textWidth, 0); | ||||
*textStart = parm1; | ||||
} | ||||
else { | ||||
parm2 += QPointF(-textWidth,0); | ||||
*textStart = parm2; | ||||
} | ||||
// elevate the text position a bit so that it does not hit the line | ||||
*textStart += QPointF(0, -5); | ||||
QPainterPath path; | ||||
path.moveTo(start); | ||||
path.lineTo(parm1); | ||||
path.lineTo(parm2); | ||||
return path; | ||||
} | ||||
QRectF PieSlice::labelTextRect(QFont font, QString text) | ||||
{ | ||||
QFontMetricsF fm(font); | ||||
return fm.boundingRect(text); | ||||
} | ||||
|
r353 | #include "moc_pieslice_p.cpp" | ||
|
r174 | |||
|
r51 | QTCOMMERCIALCHART_END_NAMESPACE | ||