pieslice.cpp
203 lines
| 5.4 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r353 | #include "pieslice_p.h" | ||
Jani Honkonen
|
r568 | #include "piechartitem_p.h" | ||
Jani Honkonen
|
r157 | #include "qpieseries.h" | ||
Jani Honkonen
|
r203 | #include "qpieslice.h" | ||
Tero Ahola
|
r490 | #include "chartpresenter_p.h" | ||
Tero Ahola
|
r51 | #include <QPainter> | ||
#include <QDebug> | ||||
Jani Honkonen
|
r157 | #include <qmath.h> | ||
Jani Honkonen
|
r163 | #include <QGraphicsSceneEvent> | ||
Jani Honkonen
|
r174 | #include <QTime> | ||
Tero Ahola
|
r51 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Jani Honkonen
|
r454 | #define PI 3.14159265 // TODO: is this defined in some header? | ||
Jani Honkonen
|
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); | ||||
} | ||||
Jani Honkonen
|
r203 | PieSlice::PieSlice(QGraphicsItem* parent) | ||
Jani Honkonen
|
r174 | :QGraphicsObject(parent), | ||
Jani Honkonen
|
r289 | m_isExploded(false), | ||
Jani Honkonen
|
r454 | m_explodeDistanceFactor(0), | ||
m_labelVisible(false), | ||||
m_labelArmLengthFactor(0) | ||||
Tero Ahola
|
r51 | { | ||
setAcceptHoverEvents(true); | ||||
Jani Honkonen
|
r142 | setAcceptedMouseButtons(Qt::LeftButton); | ||
Tero Ahola
|
r490 | setZValue(ChartPresenter::PieSeriesZValue); | ||
Tero Ahola
|
r51 | } | ||
PieSlice::~PieSlice() | ||||
{ | ||||
Jani Honkonen
|
r203 | |||
Tero Ahola
|
r51 | } | ||
QRectF PieSlice::boundingRect() const | ||||
{ | ||||
Jani Honkonen
|
r618 | return m_slicePath.boundingRect().united(m_labelTextRect); | ||
Tero Ahola
|
r51 | } | ||
QPainterPath PieSlice::shape() const | ||||
{ | ||||
Jani Honkonen
|
r454 | // Don't include the label and label arm. | ||
// This is used to detect a mouse clicks. We do not want clicks from label. | ||||
Jani Honkonen
|
r437 | return m_slicePath; | ||
Tero Ahola
|
r51 | } | ||
Jani Honkonen
|
r142 | void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) | ||
Tero Ahola
|
r51 | { | ||
Jani Honkonen
|
r454 | painter->setClipRect(parentItem()->boundingRect()); | ||
Jani Honkonen
|
r437 | painter->save(); | ||
Jani Honkonen
|
r469 | painter->setPen(m_slicePen); | ||
painter->setBrush(m_sliceBrush); | ||||
Jani Honkonen
|
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); | ||||
} | ||||
Tero Ahola
|
r51 | } | ||
Jani Honkonen
|
r174 | void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) | ||
Tero Ahola
|
r51 | { | ||
Jani Honkonen
|
r203 | emit hoverEnter(); | ||
Jani Honkonen
|
r157 | } | ||
Jani Honkonen
|
r163 | void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) | ||
Jani Honkonen
|
r157 | { | ||
Jani Honkonen
|
r203 | emit hoverLeave(); | ||
Tero Ahola
|
r51 | } | ||
Jani Honkonen
|
r163 | void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r203 | emit clicked(); | ||
} | ||||
Jani Honkonen
|
r566 | void PieSlice::setLayout(PieSliceLayout layout) | ||
Jani Honkonen
|
r203 | { | ||
Jani Honkonen
|
r566 | m_layout = layout; | ||
Jani Honkonen
|
r157 | } | ||
Jani Honkonen
|
r203 | void PieSlice::updateGeometry() | ||
Jani Honkonen
|
r157 | { | ||
Jani Honkonen
|
r566 | if (m_layout.m_radius <= 0) | ||
Jani Honkonen
|
r289 | return; | ||
Jani Honkonen
|
r157 | prepareGeometryChange(); | ||
Jani Honkonen
|
r437 | // update slice path | ||
qreal centerAngle; | ||||
QPointF armStart; | ||||
Jani Honkonen
|
r618 | m_slicePath = slicePath(m_layout.m_center, m_layout.m_radius, m_layout.m_startAngle, m_layout.m_angleSpan, ¢erAngle, &armStart); | ||
Jani Honkonen
|
r157 | |||
Jani Honkonen
|
r437 | // update text rect | ||
m_labelTextRect = labelTextRect(m_labelFont, m_labelText); | ||||
Jani Honkonen
|
r157 | |||
Jani Honkonen
|
r437 | // update label arm path | ||
QPointF labelTextStart; | ||||
Jani Honkonen
|
r566 | m_labelArmPath = labelArmPath(armStart, centerAngle, m_layout.m_radius * m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); | ||
Jani Honkonen
|
r157 | |||
Jani Honkonen
|
r437 | // update text position | ||
m_labelTextRect.moveBottomLeft(labelTextStart); | ||||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r454 | //qDebug() << "PieSlice::updateGeometry" << m_labelText << boundingRect() << m_startAngle << m_startAngle + m_angleSpan; | ||
Jani Honkonen
|
r203 | } | ||
void PieSlice::updateData(const QPieSlice* sliceData) | ||||
Jani Honkonen
|
r157 | { | ||
Jani Honkonen
|
r203 | // TODO: compare what has changes to avoid unneccesary geometry updates | ||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r203 | m_isExploded = sliceData->isExploded(); | ||
Jani Honkonen
|
r454 | m_explodeDistanceFactor = sliceData->explodeDistanceFactor(); | ||
Jani Honkonen
|
r469 | m_slicePen = sliceData->slicePen(); | ||
m_sliceBrush = sliceData->sliceBrush(); | ||||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r437 | m_labelVisible = sliceData->isLabelVisible(); | ||
m_labelText = sliceData->label(); | ||||
m_labelFont = sliceData->labelFont(); | ||||
Jani Honkonen
|
r454 | m_labelArmLengthFactor = sliceData->labelArmLengthFactor(); | ||
Jani Honkonen
|
r469 | m_labelArmPen = sliceData->labelArmPen(); | ||
Jani Honkonen
|
r566 | } | ||
Jani Honkonen
|
r209 | |||
Jani Honkonen
|
r566 | QPointF PieSlice::sliceCenter(QPointF point, qreal radius, QPieSlice *slice) | ||
{ | ||||
if (slice->isExploded()) { | ||||
qreal centerAngle = slice->startAngle() + (slice->m_angleSpan/2); | ||||
qreal len = radius * slice->explodeDistanceFactor(); | ||||
qreal dx = qSin(centerAngle*(PI/180)) * len; | ||||
qreal dy = -qCos(centerAngle*(PI/180)) * len; | ||||
point += QPointF(dx, dy); | ||||
} | ||||
return point; | ||||
Jani Honkonen
|
r157 | } | ||
Jani Honkonen
|
r566 | QPainterPath PieSlice::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal* centerAngle, QPointF* armStart) | ||
Jani Honkonen
|
r437 | { | ||
// calculate center angle | ||||
*centerAngle = startAngle + (angleSpan/2); | ||||
// calculate slice rectangle | ||||
QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2); | ||||
// 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; | ||||
Jani Honkonen
|
r566 | *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP); | ||
Jani Honkonen
|
r437 | |||
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); | ||||
} | ||||
Jani Honkonen
|
r353 | #include "moc_pieslice_p.cpp" | ||
Jani Honkonen
|
r174 | |||
Tero Ahola
|
r51 | QTCOMMERCIALCHART_END_NAMESPACE | ||