piesliceitem.cpp
284 lines
| 9.5 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> | ||
|
r157 | #include <qmath.h> | ||
|
r163 | #include <QGraphicsSceneEvent> | ||
|
r174 | #include <QTime> | ||
|
r1297 | #include <QDebug> | ||
|
r51 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r157 | QPointF offset(qreal angle, qreal length) | ||
{ | ||||
|
r1327 | qreal dx = qSin(angle*(M_PI/180)) * length; | ||
qreal dy = qCos(angle*(M_PI/180)) * length; | ||||
|
r157 | return QPointF(dx, -dy); | ||
} | ||||
|
r673 | PieSliceItem::PieSliceItem(QGraphicsItem* parent) | ||
|
r1083 | :QGraphicsObject(parent), | ||
|
r1670 | m_hovered(false) | ||
|
r51 | { | ||
setAcceptHoverEvents(true); | ||||
|
r707 | setAcceptedMouseButtons(Qt::MouseButtonMask); | ||
|
r490 | setZValue(ChartPresenter::PieSeriesZValue); | ||
|
r51 | } | ||
|
r673 | PieSliceItem::~PieSliceItem() | ||
|
r51 | { | ||
|
r1083 | // If user is hovering over the slice and it gets destroyed we do | ||
// not get a hover leave event. So we must emit the signal here. | ||||
if (m_hovered) | ||||
emit hovered(false); | ||||
|
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) { | ||
|
r1297 | painter->save(); | ||
|
r1450 | |||
|
r1307 | // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead | ||
// Also, the drawText actually uses the pen color for the text color (unlike QGraphicsSimpleTextItem) | ||||
painter->setPen(m_data.m_labelBrush.color()); | ||||
|
r1450 | painter->setBrush(m_data.m_labelBrush); | ||
|
r668 | painter->setFont(m_data.m_labelFont); | ||
|
r1712 | |||
switch (m_data.m_labelPosition) | ||||
{ | ||||
case QPieSlice::LabelOutside: | ||||
|
r1450 | painter->setClipRect(parentItem()->boundingRect()); | ||
painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color()); | ||||
|
r1694 | painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText); | ||
|
r1712 | break; | ||
|
r1759 | case QPieSlice::LabelInsideHorizontal: | ||
|
r1450 | painter->setClipPath(m_slicePath); | ||
|
r1693 | painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText); | ||
|
r1712 | break; | ||
case QPieSlice::LabelInsideTangential: | ||||
painter->setClipPath(m_slicePath); | ||||
painter->translate(m_labelTextRect.center()); | ||||
painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2); | ||||
painter->drawText(-m_labelTextRect.width() / 2, -m_labelTextRect.height() / 2, m_labelTextRect.width(), m_labelTextRect.height(), Qt::AlignCenter, m_data.m_labelText); | ||||
break; | ||||
case QPieSlice::LabelInsideNormal: | ||||
painter->setClipPath(m_slicePath); | ||||
painter->translate(m_labelTextRect.center()); | ||||
if (m_data.m_startAngle + m_data.m_angleSpan / 2 < 180) | ||||
painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2 - 90); | ||||
else | ||||
painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2 + 90); | ||||
painter->drawText(-m_labelTextRect.width() / 2, -m_labelTextRect.height() / 2, m_labelTextRect.width(), m_labelTextRect.height(), Qt::AlignCenter, m_data.m_labelText); | ||||
break; | ||||
|
r1450 | } | ||
|
r1297 | painter->restore(); | ||
|
r437 | } | ||
|
r51 | } | ||
|
r673 | void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) | ||
|
r51 | { | ||
|
r1083 | m_hovered = true; | ||
|
r1009 | emit hovered(true); | ||
|
r157 | } | ||
|
r673 | void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) | ||
|
r157 | { | ||
|
r1083 | m_hovered = false; | ||
|
r1009 | emit hovered(false); | ||
|
r51 | } | ||
|
r707 | void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
|
r142 | { | ||
|
r707 | emit clicked(event->buttons()); | ||
|
r1747 | QGraphicsItem::mousePressEvent(event); | ||
|
r203 | } | ||
|
r1074 | void PieSliceItem::setLayout(const PieSliceData &sliceData) | ||
|
r203 | { | ||
|
r668 | m_data = sliceData; | ||
|
r1074 | updateGeometry(); | ||
update(); | ||||
|
r157 | } | ||
|
r673 | void PieSliceItem::updateGeometry() | ||
|
r157 | { | ||
|
r668 | if (m_data.m_radius <= 0) | ||
|
r289 | return; | ||
|
r157 | prepareGeometryChange(); | ||
|
r1450 | // slice path | ||
|
r437 | 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 | |||
|
r1450 | // text rect | ||
QFontMetricsF fm(m_data.m_labelFont); | ||||
m_labelTextRect = QRectF(0, 0, fm.width(m_data.m_labelText), fm.height()); | ||||
|
r157 | |||
|
r1450 | // label arm path | ||
|
r437 | QPointF labelTextStart; | ||
|
r668 | m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); | ||
|
r157 | |||
|
r1450 | // text position | ||
|
r1712 | switch (m_data.m_labelPosition) | ||
{ | ||||
case QPieSlice::LabelOutside: | ||||
|
r1450 | m_labelTextRect.moveBottomLeft(labelTextStart); | ||
|
r1712 | break; | ||
|
r1759 | case QPieSlice::LabelInsideHorizontal: | ||
|
r1841 | case QPieSlice::LabelInsideTangential:{ | ||
QPointF textCenter; | ||||
if (m_data.m_holeRadius > 0) | ||||
textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius + (m_data.m_radius - m_data.m_holeRadius) / 2); | ||||
else | ||||
textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); | ||||
m_labelTextRect.moveCenter(textCenter); | ||||
break; | ||||
} | ||||
|
r1712 | case QPieSlice::LabelInsideNormal:{ | ||
|
r1841 | // TODO: align the label text to the slice arc insead of half the radius | ||
|
r1712 | QPointF textCenter; | ||
|
r1838 | if (m_data.m_holeRadius > 0) | ||
textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius + (m_data.m_radius - m_data.m_holeRadius) / 2); | ||||
|
r1712 | else | ||
textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); | ||||
m_labelTextRect.moveCenter(textCenter); | ||||
break; | ||||
} | ||||
|
r1450 | } | ||
|
r174 | |||
|
r1450 | // 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()) { | ||||
|
r1274 | qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2); | ||
|
r566 | qreal len = radius * slice->explodeDistanceFactor(); | ||
|
r1327 | point += offset(centerAngle, len); | ||
|
r566 | } | ||
return point; | ||||
|
r157 | } | ||
|
r972 | 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 | ||||
QPainterPath path; | ||||
|
r1838 | if (m_data.m_holeRadius > 0) { | ||
QRectF insideRect(center.x() - m_data.m_holeRadius, center.y()-m_data.m_holeRadius, m_data.m_holeRadius*2, m_data.m_holeRadius*2); | ||||
|
r1670 | path.arcMoveTo(rect, -startAngle + 90); | ||
path.arcTo(rect, -startAngle + 90, -angleSpan); | ||||
path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan); | ||||
path.closeSubpath(); | ||||
} else { | ||||
path.moveTo(rect.center()); | ||||
path.arcTo(rect, -startAngle + 90, -angleSpan); | ||||
path.closeSubpath(); | ||||
} | ||||
|
r437 | |||
// calculate label arm start point | ||||
*armStart = center; | ||||
|
r566 | *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP); | ||
|
r437 | |||
return path; | ||||
} | ||||
|
r972 | QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart) | ||
|
r437 | { | ||
|
r1327 | // Normalize the angle to 0-360 range | ||
// NOTE: We are using int here on purpose. Depenging on platform and hardware | ||||
// qreal can be a double, float or something the user gives to the Qt configure | ||||
// (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float | ||||
// but there are fmod() and fmodf() functions for that. So instead of some #ifdef | ||||
// that might break we just use int. Precision for this is just fine for our needs. | ||||
int normalized = angle * 10.0; | ||||
normalized = normalized % 3600; | ||||
if (normalized < 0) | ||||
normalized += 3600; | ||||
angle = (qreal) normalized / 10.0; | ||||
|
r972 | // prevent label arm pointing straight down because it will look bad | ||
if (angle < 180 && angle > 170) | ||||
angle = 170; | ||||
if (angle > 180 && angle < 190) | ||||
angle = 190; | ||||
// line from slice to label | ||||
|
r1327 | QPointF parm1 = start + offset(angle, length); | ||
|
r437 | |||
|
r972 | // line to underline the label | ||
|
r437 | QPointF parm2 = parm1; | ||
if (angle < 180) { // arm swings the other way on the left side | ||||
|
r1670 | parm2 += QPointF(textWidth, 0); | ||
*textStart = parm1; | ||||
|
r437 | } | ||
else { | ||||
|
r1670 | parm2 += QPointF(-textWidth,0); | ||
*textStart = parm2; | ||||
|
r437 | } | ||
QPainterPath path; | ||||
path.moveTo(start); | ||||
path.lineTo(parm1); | ||||
path.lineTo(parm2); | ||||
return path; | ||||
} | ||||
|
r673 | #include "moc_piesliceitem_p.cpp" | ||
|
r174 | |||
|
r51 | QTCOMMERCIALCHART_END_NAMESPACE | ||
|
r1693 | |||