@@ -1,75 +1,74 | |||||
1 | #include "pieslicelabel.h" |
|
1 | #include "pieslicelabel.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->setRenderHint(QPainter::Antialiasing); |
|
21 | painter->setRenderHint(QPainter::Antialiasing); | |
22 |
|
22 | |||
23 | painter->setPen(m_pen); |
|
23 | painter->setPen(m_pen); | |
24 | painter->drawPath(m_armPath); |
|
24 | painter->drawPath(m_armPath); | |
25 |
|
25 | |||
26 | // TODO: do we need a pen for text? |
|
26 | // TODO: do we need a pen for text? | |
27 | QFont font; |
|
27 | painter->setFont(m_font); | |
28 | painter->setFont(font); |
|
|||
29 | painter->drawText(m_textRect.bottomLeft(), m_text); |
|
28 | painter->drawText(m_textRect.bottomLeft(), m_text); | |
30 |
|
29 | |||
31 | //qDebug() << "PieSliceLabel::paint" << m_text << m_textRect; |
|
30 | //qDebug() << "PieSliceLabel::paint" << m_text << m_textRect; | |
32 | } |
|
31 | } | |
33 |
|
32 | |||
34 | void PieSliceLabel::updateGeometry() |
|
33 | void PieSliceLabel::updateGeometry() | |
35 | { |
|
34 | { | |
36 | prepareGeometryChange(); |
|
35 | prepareGeometryChange(); | |
37 |
|
36 | |||
38 | // calculate text size |
|
37 | // calculate text size | |
39 | QFontMetricsF fm(m_font); |
|
38 | QFontMetricsF fm(m_font); | |
40 | QRectF textRect = fm.boundingRect(m_text); |
|
39 | QRectF textRect = fm.boundingRect(m_text); | |
41 |
|
40 | |||
42 | // calculate path for arm and text start point |
|
41 | // calculate path for arm and text start point | |
43 | qreal dx = qSin(m_armAngle*(PI/180)) * m_armLength; |
|
42 | qreal dx = qSin(m_armAngle*(PI/180)) * m_armLength; | |
44 | qreal dy = -qCos(m_armAngle*(PI/180)) * m_armLength; |
|
43 | qreal dy = -qCos(m_armAngle*(PI/180)) * m_armLength; | |
45 | QPointF parm1 = m_armStartPoint + QPointF(dx, dy); |
|
44 | QPointF parm1 = m_armStartPoint + QPointF(dx, dy); | |
46 |
|
45 | |||
47 | // calculate horizontal arm and text position |
|
46 | // calculate horizontal arm and text position | |
48 | QPointF parm2 = parm1; |
|
47 | QPointF parm2 = parm1; | |
49 | textRect.moveBottomLeft(parm1); |
|
48 | textRect.moveBottomLeft(parm1); | |
50 | if (m_armAngle < 180) { // arm swings the other way on the left side |
|
49 | if (m_armAngle < 180) { // arm swings the other way on the left side | |
51 | parm2 += QPointF(m_textRect.width(), 0); |
|
50 | parm2 += QPointF(m_textRect.width(), 0); | |
52 | } else { |
|
51 | } else { | |
53 | parm2 += QPointF(-m_textRect.width(),0); |
|
52 | parm2 += QPointF(-m_textRect.width(),0); | |
54 | textRect.moveBottomLeft(parm2); |
|
53 | textRect.moveBottomLeft(parm2); | |
55 | } |
|
54 | } | |
56 |
|
55 | |||
57 | // add a little offset to text so that it does not touch the arm |
|
56 | // add a little offset to text so that it does not touch the arm | |
58 | qreal yOffset = m_pen.widthF() ? m_pen.widthF() : 2; |
|
57 | qreal yOffset = m_pen.widthF() ? m_pen.widthF() : 2; | |
59 | textRect.translate(0, -yOffset); |
|
58 | textRect.translate(0, -yOffset); | |
60 |
|
59 | |||
61 | // update arm path |
|
60 | // update arm path | |
62 | QPainterPath path; |
|
61 | QPainterPath path; | |
63 | path.moveTo(m_armStartPoint); |
|
62 | path.moveTo(m_armStartPoint); | |
64 | path.lineTo(parm1); |
|
63 | path.lineTo(parm1); | |
65 | path.lineTo(parm2); |
|
64 | path.lineTo(parm2); | |
66 |
|
65 | |||
67 | // update paths & rects |
|
66 | // update paths & rects | |
68 | m_armPath = path; |
|
67 | m_armPath = path; | |
69 | m_textRect = textRect; |
|
68 | m_textRect = textRect; | |
70 | m_rect = path.boundingRect().united(m_textRect); |
|
69 | m_rect = path.boundingRect().united(m_textRect); | |
71 |
|
70 | |||
72 | //qDebug() << "PieSliceLabel::updateGeometry" << m_text << m_armStartPoint << m_armLength << m_armAngle << m_textRect; |
|
71 | //qDebug() << "PieSliceLabel::updateGeometry" << m_text << m_armStartPoint << m_armLength << m_armAngle << m_textRect; | |
73 | } |
|
72 | } | |
74 |
|
73 | |||
75 | QTCOMMERCIALCHART_END_NAMESPACE |
|
74 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now