piesliceitem.cpp
205 lines
| 5.9 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r673 | #include "piesliceitem_p.h" | ||
|
r568 | #include "piechartitem_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); | ||||
} | ||||
|
r673 | PieSliceItem::PieSliceItem(QGraphicsItem* parent) | ||
|
r668 | :QGraphicsObject(parent) | ||
|
r51 | { | ||
setAcceptHoverEvents(true); | ||||
|
r707 | setAcceptedMouseButtons(Qt::MouseButtonMask); | ||
|
r490 | setZValue(ChartPresenter::PieSeriesZValue); | ||
|
r51 | } | ||
|
r673 | PieSliceItem::~PieSliceItem() | ||
|
r51 | { | ||
|
r203 | |||
|
r51 | } | ||
|
r673 | QRectF PieSliceItem::boundingRect() const | ||
|
r51 | { | ||
|
r621 | return m_boundingRect; | ||
|
r51 | } | ||
|
r673 | QPainterPath PieSliceItem::shape() const | ||
|
r51 | { | ||
|
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 | } | ||
|
r673 | void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) | ||
|
r51 | { | ||
|
r437 | painter->save(); | ||
|
r789 | painter->setClipRect(parentItem()->boundingRect()); | ||
|
r669 | painter->setPen(m_data.m_slicePen); | ||
painter->setBrush(m_data.m_sliceBrush); | ||||
|
r437 | painter->drawPath(m_slicePath); | ||
painter->restore(); | ||||
|
r691 | if (m_data.m_isLabelVisible) { | ||
|
r905 | painter->setClipRect(parentItem()->boundingRect()); | ||
|
r714 | painter->setPen(m_data.m_labelPen); | ||
|
r437 | painter->drawPath(m_labelArmPath); | ||
|
r714 | // the pen color will affect the font color as well | ||
|
r668 | painter->setFont(m_data.m_labelFont); | ||
painter->drawText(m_labelTextRect.bottomLeft(), m_data.m_labelText); | ||||
|
r437 | } | ||
|
r51 | } | ||
|
r673 | void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) | ||
|
r51 | { | ||
|
r952 | emit hoverEnter(); // TODO: refactor emit hover(somebooleanvalue) | ||
|
r157 | } | ||
|
r673 | void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) | ||
|
r157 | { | ||
|
r952 | emit hoverLeave(); // TODO: refactor emit hover(somebooleanvalue) | ||
|
r51 | } | ||
|
r707 | void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
|
r142 | { | ||
|
r707 | emit clicked(event->buttons()); | ||
|
r203 | } | ||
|
r673 | void PieSliceItem::setSliceData(PieSliceData sliceData) | ||
|
r203 | { | ||
|
r668 | m_data = sliceData; | ||
|
r157 | } | ||
|
r673 | void PieSliceItem::updateGeometry() | ||
|
r157 | { | ||
|
r668 | if (m_data.m_radius <= 0) | ||
|
r289 | return; | ||
|
r157 | prepareGeometryChange(); | ||
|
r437 | // update slice path | ||
qreal centerAngle; | ||||
QPointF armStart; | ||||
|
r668 | m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, ¢erAngle, &armStart); | ||
|
r157 | |||
|
r437 | // update text rect | ||
|
r668 | m_labelTextRect = labelTextRect(m_data.m_labelFont, m_data.m_labelText); | ||
|
r157 | |||
|
r437 | // update label arm path | ||
QPointF labelTextStart; | ||||
|
r668 | m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); | ||
|
r157 | |||
|
r437 | // update text position | ||
m_labelTextRect.moveBottomLeft(labelTextStart); | ||||
|
r174 | |||
|
r621 | // update bounding rect | ||
|
r718 | if (m_data.m_isLabelVisible) | ||
m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect); | ||||
else | ||||
m_boundingRect = m_slicePath.boundingRect(); | ||||
|
r203 | } | ||
|
r673 | QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice) | ||
|
r566 | { | ||
if (slice->isExploded()) { | ||||
|
r669 | qreal centerAngle = slice->startAngle() + ((slice->endAngle() - slice->startAngle())/2); | ||
|
r566 | 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; | ||||
|
r157 | } | ||
|
r673 | QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal* centerAngle, QPointF* armStart) | ||
|
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; | ||||
|
r566 | *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP); | ||
|
r437 | |||
return path; | ||||
} | ||||
|
r673 | QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF* textStart) | ||
|
r437 | { | ||
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; | ||||
} | ||||
|
r673 | QRectF PieSliceItem::labelTextRect(QFont font, QString text) | ||
|
r437 | { | ||
QFontMetricsF fm(font); | ||||
return fm.boundingRect(text); | ||||
} | ||||
|
r673 | #include "moc_piesliceitem_p.cpp" | ||
|
r174 | |||
|
r51 | QTCOMMERCIALCHART_END_NAMESPACE | ||