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