piesliceitem.cpp
230 lines
| 6.9 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
Jani Honkonen
|
r673 | #include "piesliceitem_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> | ||
Jani Honkonen
|
r157 | #include <qmath.h> | ||
Jani Honkonen
|
r163 | #include <QGraphicsSceneEvent> | ||
Jani Honkonen
|
r174 | #include <QTime> | ||
Tero Ahola
|
r1297 | #include <QDebug> | ||
Tero Ahola
|
r51 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Jani Honkonen
|
r157 | QPointF offset(qreal angle, qreal length) | ||
{ | ||||
Jani Honkonen
|
r1327 | qreal dx = qSin(angle*(M_PI/180)) * length; | ||
qreal dy = qCos(angle*(M_PI/180)) * length; | ||||
Jani Honkonen
|
r157 | return QPointF(dx, -dy); | ||
} | ||||
Jani Honkonen
|
r673 | PieSliceItem::PieSliceItem(QGraphicsItem* parent) | ||
Jani Honkonen
|
r1083 | :QGraphicsObject(parent), | ||
m_hovered(false) | ||||
Tero Ahola
|
r51 | { | ||
setAcceptHoverEvents(true); | ||||
Jani Honkonen
|
r707 | setAcceptedMouseButtons(Qt::MouseButtonMask); | ||
Tero Ahola
|
r490 | setZValue(ChartPresenter::PieSeriesZValue); | ||
Tero Ahola
|
r51 | } | ||
Jani Honkonen
|
r673 | PieSliceItem::~PieSliceItem() | ||
Tero Ahola
|
r51 | { | ||
Jani Honkonen
|
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); | ||||
Tero Ahola
|
r51 | } | ||
Jani Honkonen
|
r673 | QRectF PieSliceItem::boundingRect() const | ||
Tero Ahola
|
r51 | { | ||
Jani Honkonen
|
r621 | return m_boundingRect; | ||
Tero Ahola
|
r51 | } | ||
Jani Honkonen
|
r673 | QPainterPath PieSliceItem::shape() const | ||
Tero Ahola
|
r51 | { | ||
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
|
r673 | void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) | ||
Tero Ahola
|
r51 | { | ||
Jani Honkonen
|
r437 | painter->save(); | ||
Tero Ahola
|
r789 | painter->setClipRect(parentItem()->boundingRect()); | ||
Jani Honkonen
|
r669 | painter->setPen(m_data.m_slicePen); | ||
painter->setBrush(m_data.m_sliceBrush); | ||||
Jani Honkonen
|
r437 | painter->drawPath(m_slicePath); | ||
painter->restore(); | ||||
Jani Honkonen
|
r691 | if (m_data.m_isLabelVisible) { | ||
Tero Ahola
|
r1297 | painter->save(); | ||
Jani Honkonen
|
r905 | painter->setClipRect(parentItem()->boundingRect()); | ||
Tero Ahola
|
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()); | ||||
Jani Honkonen
|
r437 | painter->drawPath(m_labelArmPath); | ||
Jani Honkonen
|
r668 | painter->setFont(m_data.m_labelFont); | ||
painter->drawText(m_labelTextRect.bottomLeft(), m_data.m_labelText); | ||||
Tero Ahola
|
r1297 | painter->restore(); | ||
Jani Honkonen
|
r437 | } | ||
Tero Ahola
|
r51 | } | ||
Jani Honkonen
|
r673 | void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) | ||
Tero Ahola
|
r51 | { | ||
Jani Honkonen
|
r1083 | m_hovered = true; | ||
Jani Honkonen
|
r1009 | emit hovered(true); | ||
Jani Honkonen
|
r157 | } | ||
Jani Honkonen
|
r673 | void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) | ||
Jani Honkonen
|
r157 | { | ||
Jani Honkonen
|
r1083 | m_hovered = false; | ||
Jani Honkonen
|
r1009 | emit hovered(false); | ||
Tero Ahola
|
r51 | } | ||
Jani Honkonen
|
r707 | void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
Jani Honkonen
|
r142 | { | ||
Jani Honkonen
|
r707 | emit clicked(event->buttons()); | ||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r1074 | void PieSliceItem::setLayout(const PieSliceData &sliceData) | ||
Jani Honkonen
|
r203 | { | ||
Jani Honkonen
|
r668 | m_data = sliceData; | ||
Jani Honkonen
|
r1074 | updateGeometry(); | ||
update(); | ||||
Jani Honkonen
|
r157 | } | ||
Jani Honkonen
|
r673 | void PieSliceItem::updateGeometry() | ||
Jani Honkonen
|
r157 | { | ||
Jani Honkonen
|
r668 | if (m_data.m_radius <= 0) | ||
Jani Honkonen
|
r289 | return; | ||
Jani Honkonen
|
r157 | prepareGeometryChange(); | ||
Jani Honkonen
|
r437 | // update slice path | ||
qreal centerAngle; | ||||
QPointF armStart; | ||||
Jani Honkonen
|
r668 | m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, ¢erAngle, &armStart); | ||
Jani Honkonen
|
r157 | |||
Jani Honkonen
|
r437 | // update text rect | ||
Jani Honkonen
|
r668 | m_labelTextRect = labelTextRect(m_data.m_labelFont, m_data.m_labelText); | ||
Jani Honkonen
|
r157 | |||
Jani Honkonen
|
r437 | // update label arm path | ||
QPointF labelTextStart; | ||||
Jani Honkonen
|
r668 | m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); | ||
Jani Honkonen
|
r157 | |||
Jani Honkonen
|
r437 | // update text position | ||
m_labelTextRect.moveBottomLeft(labelTextStart); | ||||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r621 | // update bounding rect | ||
Jani Honkonen
|
r718 | if (m_data.m_isLabelVisible) | ||
m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect); | ||||
else | ||||
m_boundingRect = m_slicePath.boundingRect(); | ||||
Jani Honkonen
|
r203 | } | ||
Jani Honkonen
|
r673 | QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice) | ||
Jani Honkonen
|
r566 | { | ||
if (slice->isExploded()) { | ||||
Jani Honkonen
|
r1274 | qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2); | ||
Jani Honkonen
|
r566 | qreal len = radius * slice->explodeDistanceFactor(); | ||
Jani Honkonen
|
r1327 | point += offset(centerAngle, len); | ||
Jani Honkonen
|
r566 | } | ||
return point; | ||||
Jani Honkonen
|
r157 | } | ||
Jani Honkonen
|
r972 | QPainterPath PieSliceItem::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; | ||||
} | ||||
Jani Honkonen
|
r972 | QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart) | ||
Jani Honkonen
|
r437 | { | ||
Jani Honkonen
|
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; | ||||
Jani Honkonen
|
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 | ||||
Jani Honkonen
|
r1327 | QPointF parm1 = start + offset(angle, length); | ||
Jani Honkonen
|
r437 | |||
Jani Honkonen
|
r972 | // line to underline the label | ||
Jani Honkonen
|
r437 | 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 | ||||
Jani Honkonen
|
r972 | *textStart += QPointF(0, -3); | ||
Jani Honkonen
|
r437 | |||
QPainterPath path; | ||||
path.moveTo(start); | ||||
path.lineTo(parm1); | ||||
path.lineTo(parm2); | ||||
return path; | ||||
} | ||||
Jani Honkonen
|
r673 | QRectF PieSliceItem::labelTextRect(QFont font, QString text) | ||
Jani Honkonen
|
r437 | { | ||
QFontMetricsF fm(font); | ||||
return fm.boundingRect(text); | ||||
} | ||||
Jani Honkonen
|
r673 | #include "moc_piesliceitem_p.cpp" | ||
Jani Honkonen
|
r174 | |||
Tero Ahola
|
r51 | QTCOMMERCIALCHART_END_NAMESPACE | ||