##// END OF EJS Templates
More documentation for pie
More documentation for pie

File last commit:

r212:70a8e655a143
r320:832b68ef6fc6
Show More
pieslicelabel.cpp
72 lines | 1.9 KiB | text/x-c | CppLexer
Jani Honkonen
Refactoring piechart API (and internals)
r174 #include "pieslicelabel.h"
#include <QPainter>
#include <qmath.h>
Jani Honkonen
Implementing slice label
r181 #include <QGraphicsTextItem>
Jani Honkonen
Fix issue with pie label not drawn correctly when first created
r209 #include <QDebug>
Jani Honkonen
Refactoring piechart API (and internals)
r174
QTCOMMERCIALCHART_BEGIN_NAMESPACE
#define PI 3.14159265
PieSliceLabel::PieSliceLabel(QGraphicsItem* parent)
Jani Honkonen
Fix issue with pie label not drawn correctly when first created
r209 :QGraphicsItem(parent),
m_armAngle(0),
m_armLength(0)
Jani Honkonen
Refactoring piechart API (and internals)
r174 {
Jani Honkonen
Fix issue with pie label not drawn correctly when first created
r209
Jani Honkonen
Refactoring piechart API (and internals)
r174 }
void PieSliceLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
{
painter->setPen(m_pen);
Jani Honkonen
Implementing slice label
r181 painter->drawPath(m_armPath);
// TODO: do we need a pen for text?
Jani Honkonen
Oops this was not supposed to be here...
r211 painter->setFont(m_font);
Jani Honkonen
Adjusting slice label
r210 painter->drawText(m_textRect.bottomLeft(), m_text);
Jani Honkonen
Fix issue with pie label not drawn correctly when first created
r209
//qDebug() << "PieSliceLabel::paint" << m_text << m_textRect;
Jani Honkonen
Refactoring piechart API (and internals)
r174 }
Jani Honkonen
Implementing slice label
r181 void PieSliceLabel::updateGeometry()
Jani Honkonen
Refactoring piechart API (and internals)
r174 {
prepareGeometryChange();
Jani Honkonen
Implementing slice label
r181 // calculate text size
QFontMetricsF fm(m_font);
QRectF textRect = fm.boundingRect(m_text);
Jani Honkonen
Refactoring piechart API (and internals)
r174
Jani Honkonen
Implementing slice label
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
Refactoring piechart API (and internals)
r174
Jani Honkonen
Implementing slice label
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
Refactoring piechart API (and internals)
r174 } else {
Jani Honkonen
Implementing slice label
r181 parm2 += QPointF(-m_textRect.width(),0);
textRect.moveBottomLeft(parm2);
Jani Honkonen
Refactoring piechart API (and internals)
r174 }
Jani Honkonen
Adjusting slice label
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
Implementing slice label
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
Fix issue with pie label not drawn correctly when first created
r209
//qDebug() << "PieSliceLabel::updateGeometry" << m_text << m_armStartPoint << m_armLength << m_armAngle << m_textRect;
Jani Honkonen
Refactoring piechart API (and internals)
r174 }
QTCOMMERCIALCHART_END_NAMESPACE