pieslicelabel.cpp
72 lines
| 1.9 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r174 | #include "pieslicelabel.h" | ||
#include <QPainter> | ||||
#include <qmath.h> | ||||
Jani Honkonen
|
r181 | #include <QGraphicsTextItem> | ||
Jani Honkonen
|
r209 | #include <QDebug> | ||
Jani Honkonen
|
r174 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
#define PI 3.14159265 | ||||
PieSliceLabel::PieSliceLabel(QGraphicsItem* parent) | ||||
Jani Honkonen
|
r209 | :QGraphicsItem(parent), | ||
m_armAngle(0), | ||||
m_armLength(0) | ||||
Jani Honkonen
|
r174 | { | ||
Jani Honkonen
|
r209 | |||
Jani Honkonen
|
r174 | } | ||
void PieSliceLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) | ||||
{ | ||||
painter->setPen(m_pen); | ||||
Jani Honkonen
|
r181 | painter->drawPath(m_armPath); | ||
// TODO: do we need a pen for text? | ||||
Jani Honkonen
|
r211 | painter->setFont(m_font); | ||
Jani Honkonen
|
r210 | painter->drawText(m_textRect.bottomLeft(), m_text); | ||
Jani Honkonen
|
r209 | |||
//qDebug() << "PieSliceLabel::paint" << m_text << m_textRect; | ||||
Jani Honkonen
|
r174 | } | ||
Jani Honkonen
|
r181 | void PieSliceLabel::updateGeometry() | ||
Jani Honkonen
|
r174 | { | ||
prepareGeometryChange(); | ||||
Jani Honkonen
|
r181 | // calculate text size | ||
QFontMetricsF fm(m_font); | ||||
QRectF textRect = fm.boundingRect(m_text); | ||||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r181 | // calculate path for arm and text start point | ||
qreal dx = qSin(m_armAngle*(PI/180)) * m_armLength; | ||||
qreal dy = -qCos(m_armAngle*(PI/180)) * m_armLength; | ||||
QPointF parm1 = m_armStartPoint + QPointF(dx, dy); | ||||
Jani Honkonen
|
r174 | |||
Jani Honkonen
|
r181 | // calculate horizontal arm and text position | ||
QPointF parm2 = parm1; | ||||
textRect.moveBottomLeft(parm1); | ||||
if (m_armAngle < 180) { // arm swings the other way on the left side | ||||
parm2 += QPointF(m_textRect.width(), 0); | ||||
Jani Honkonen
|
r174 | } else { | ||
Jani Honkonen
|
r181 | parm2 += QPointF(-m_textRect.width(),0); | ||
textRect.moveBottomLeft(parm2); | ||||
Jani Honkonen
|
r174 | } | ||
Jani Honkonen
|
r210 | // add a little offset to text so that it does not touch the arm | ||
qreal yOffset = m_pen.widthF() ? m_pen.widthF() : 2; | ||||
textRect.translate(0, -yOffset); | ||||
Jani Honkonen
|
r181 | // update arm path | ||
QPainterPath path; | ||||
path.moveTo(m_armStartPoint); | ||||
path.lineTo(parm1); | ||||
path.lineTo(parm2); | ||||
// update paths & rects | ||||
m_armPath = path; | ||||
m_textRect = textRect; | ||||
m_rect = path.boundingRect().united(m_textRect); | ||||
Jani Honkonen
|
r209 | |||
//qDebug() << "PieSliceLabel::updateGeometry" << m_text << m_armStartPoint << m_armLength << m_armAngle << m_textRect; | ||||
Jani Honkonen
|
r174 | } | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||