##// END OF EJS Templates
Fix issue with pie slice label not drawn correctly initially
Jani Honkonen -
r409:34a3f20a9cf1
parent child
Show More
@@ -1,72 +1,72
1 #include "pieslicelabel_p.h"
1 #include "pieslicelabel_p.h"
2 #include <QPainter>
2 #include <QPainter>
3 #include <qmath.h>
3 #include <qmath.h>
4 #include <QGraphicsTextItem>
4 #include <QGraphicsTextItem>
5 #include <QDebug>
5 #include <QDebug>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 #define PI 3.14159265
9 #define PI 3.14159265
10
10
11 PieSliceLabel::PieSliceLabel(QGraphicsItem* parent)
11 PieSliceLabel::PieSliceLabel(QGraphicsItem* parent)
12 :QGraphicsItem(parent),
12 :QGraphicsItem(parent),
13 m_armAngle(0),
13 m_armAngle(0),
14 m_armLength(0)
14 m_armLength(0)
15 {
15 {
16
16
17 }
17 }
18
18
19 void PieSliceLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
19 void PieSliceLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
20 {
20 {
21 painter->setPen(m_pen);
21 painter->setPen(m_pen);
22 painter->drawPath(m_armPath);
22 painter->drawPath(m_armPath);
23
23
24 // TODO: do we need a pen for text?
24 // TODO: do we need a pen for text?
25 painter->setFont(m_font);
25 painter->setFont(m_font);
26 painter->drawText(m_textRect.bottomLeft(), m_text);
26 painter->drawText(m_textRect.bottomLeft(), m_text);
27
27
28 //qDebug() << "PieSliceLabel::paint" << m_text << m_textRect;
28 //qDebug() << "PieSliceLabel::paint" << m_text << m_textRect;
29 }
29 }
30
30
31 void PieSliceLabel::updateGeometry()
31 void PieSliceLabel::updateGeometry()
32 {
32 {
33 prepareGeometryChange();
33 prepareGeometryChange();
34
34
35 // calculate text size
35 // calculate text size
36 QFontMetricsF fm(m_font);
36 QFontMetricsF fm(m_font);
37 QRectF textRect = fm.boundingRect(m_text);
37 QRectF textRect = fm.boundingRect(m_text);
38
38
39 // calculate path for arm and text start point
39 // calculate path for arm and text start point
40 qreal dx = qSin(m_armAngle*(PI/180)) * m_armLength;
40 qreal dx = qSin(m_armAngle*(PI/180)) * m_armLength;
41 qreal dy = -qCos(m_armAngle*(PI/180)) * m_armLength;
41 qreal dy = -qCos(m_armAngle*(PI/180)) * m_armLength;
42 QPointF parm1 = m_armStartPoint + QPointF(dx, dy);
42 QPointF parm1 = m_armStartPoint + QPointF(dx, dy);
43
43
44 // calculate horizontal arm and text position
44 // calculate horizontal arm and text position
45 QPointF parm2 = parm1;
45 QPointF parm2 = parm1;
46 textRect.moveBottomLeft(parm1);
46 textRect.moveBottomLeft(parm1);
47 if (m_armAngle < 180) { // arm swings the other way on the left side
47 if (m_armAngle < 180) { // arm swings the other way on the left side
48 parm2 += QPointF(m_textRect.width(), 0);
48 parm2 += QPointF(textRect.width(), 0);
49 } else {
49 } else {
50 parm2 += QPointF(-m_textRect.width(),0);
50 parm2 += QPointF(-textRect.width(),0);
51 textRect.moveBottomLeft(parm2);
51 textRect.moveBottomLeft(parm2);
52 }
52 }
53
53
54 // add a little offset to text so that it does not touch the arm
54 // add a little offset to text so that it does not touch the arm
55 qreal yOffset = m_pen.widthF() ? m_pen.widthF() : 2;
55 qreal yOffset = m_pen.widthF() ? m_pen.widthF() : 2;
56 textRect.translate(0, -yOffset);
56 textRect.translate(0, -yOffset);
57
57
58 // update arm path
58 // update arm path
59 QPainterPath path;
59 QPainterPath path;
60 path.moveTo(m_armStartPoint);
60 path.moveTo(m_armStartPoint);
61 path.lineTo(parm1);
61 path.lineTo(parm1);
62 path.lineTo(parm2);
62 path.lineTo(parm2);
63
63
64 // update paths & rects
64 // update paths & rects
65 m_armPath = path;
65 m_armPath = path;
66 m_textRect = textRect;
66 m_textRect = textRect;
67 m_rect = path.boundingRect().united(m_textRect);
67 m_rect = path.boundingRect().united(m_textRect);
68
68
69 //qDebug() << "PieSliceLabel::updateGeometry" << m_text << m_armStartPoint << m_armLength << m_armAngle << m_textRect;
69 qDebug() << "PieSliceLabel::updateGeometry" << m_text << m_armStartPoint << m_armLength << m_armAngle << m_textRect;
70 }
70 }
71
71
72 QTCOMMERCIALCHART_END_NAMESPACE
72 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now