##// END OF EJS Templates
Implementing slice label
Jani Honkonen -
r181:f52f5b858a82
parent child
Show More
@@ -109,7 +109,11 void PieSlice::updateGeometry(QRectF rect, qreal startAngle, qreal span)
109 109 // update label position
110 110 qreal radius = rect.height() / 2;
111 111 QPointF edgeCenter = rect.center() + offset(centerAngle, radius + 5);
112 m_slicelabel->updateGeometry(edgeCenter, centerAngle, 50);
112
113 m_slicelabel->setArmStartPoint(edgeCenter);
114 m_slicelabel->setArmAngle(centerAngle);
115 m_slicelabel->setArmLength(50);
116 m_slicelabel->updateGeometry();
113 117
114 118 //qDebug() << "PieSlice::updateGeometry" << m_rect;
115 119 }
@@ -125,8 +129,11 void PieSlice::updateData()
125 129
126 130 update();
127 131
128 m_slicelabel->setLabel(m_data.label());
129 132 m_slicelabel->setVisible(m_data.isLabelVisible());
133 m_slicelabel->setText(m_data.label());
134 //m_slicelabel->setPen(m_data.labelPen());
135 //m_slicelabel->setFont(m_data.labelFont());
136 m_slicelabel->updateGeometry(); // text size & font modifies the geometry
130 137 m_slicelabel->update();
131 138 }
132 139
@@ -1,6 +1,7
1 1 #include "pieslicelabel.h"
2 2 #include <QPainter>
3 3 #include <qmath.h>
4 #include <QGraphicsTextItem>
4 5
5 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 7
@@ -9,68 +10,55 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 10 PieSliceLabel::PieSliceLabel(QGraphicsItem* parent)
10 11 :QGraphicsItem(parent)
11 12 {
13 // set defaults
12 14 m_pen = QPen(Qt::black);
13 15 }
14 16
15 PieSliceLabel::~PieSliceLabel()
16 {
17
18 }
19
20 QRectF PieSliceLabel::boundingRect() const
21 {
22 return m_rect;
23 }
24
25 QPainterPath PieSliceLabel::shape() const
26 {
27 return m_path;
28 }
29
30 17 void PieSliceLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
31 18 {
32 19 painter->setRenderHint(QPainter::Antialiasing);
20
33 21 painter->setPen(m_pen);
34 painter->drawPath(m_path);
22 painter->drawPath(m_armPath);
23
24 // TODO: do we need a pen for text?
25 painter->setFont(m_font);
26 painter->drawText(m_textRect, m_text);
35 27 }
36 28
37 void PieSliceLabel::updateGeometry(const QPointF& startPoint, qreal armAngle, qreal armLength)
29 void PieSliceLabel::updateGeometry()
38 30 {
39 31 prepareGeometryChange();
40 32
41 QPainterPath path;
42 path.moveTo(startPoint);
33 // calculate text size
34 QFontMetricsF fm(m_font);
35 QRectF textRect = fm.boundingRect(m_text);
43 36
44 // draw arm
45 qreal dx = qSin(armAngle*(PI/180)) * armLength;
46 qreal dy = -qCos(armAngle*(PI/180)) * armLength;
47 QPointF p1 = startPoint + QPointF(dx, dy);
48 path.lineTo(p1);
37 // calculate path for arm and text start point
38 qreal dx = qSin(m_armAngle*(PI/180)) * m_armLength;
39 qreal dy = -qCos(m_armAngle*(PI/180)) * m_armLength;
40 QPointF parm1 = m_armStartPoint + QPointF(dx, dy);
49 41
50 QPointF p2 = p1;
51 QPointF pt = p1;
52 if (armAngle < 180) {
53 p2 += QPointF(50, 0);
42 // calculate horizontal arm and text position
43 QPointF parm2 = parm1;
44 textRect.moveBottomLeft(parm1);
45 if (m_armAngle < 180) { // arm swings the other way on the left side
46 parm2 += QPointF(m_textRect.width(), 0);
54 47 } else {
55 p2 += QPointF(-50,0);
56 pt = p2;
48 parm2 += QPointF(-m_textRect.width(),0);
49 textRect.moveBottomLeft(parm2);
57 50 }
58 path.lineTo(p2);
59
60 QFont font;
61 pt += QPointF(0,-2);
62 path.addText(pt, font, m_label);
63
64 m_path = path;
65 m_rect = path.boundingRect();
66 }
67 51
68 void PieSliceLabel::setLabel(QString label)
69 {
70 m_label = label;
71 // TODO: animation?
52 // update arm path
53 QPainterPath path;
54 path.moveTo(m_armStartPoint);
55 path.lineTo(parm1);
56 path.lineTo(parm2);
57
58 // update paths & rects
59 m_armPath = path;
60 m_textRect = textRect;
61 m_rect = path.boundingRect().united(m_textRect);
72 62 }
73 63
74
75
76 64 QTCOMMERCIALCHART_END_NAMESPACE
@@ -4,31 +4,52
4 4 #include "qchartglobal.h"
5 5 #include <QGraphicsItem>
6 6 #include <QPen>
7 #include <QFontMetricsF>
7 8
9 class QGraphicsTextItem;
8 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 11
10 12 class PieSliceLabel : public QGraphicsItem
11 13 {
12 14 public:
13 15 PieSliceLabel(QGraphicsItem* parent = 0);
14 ~PieSliceLabel();
16 ~PieSliceLabel() {};
15 17
16 18 public: // from QGraphicsItem
17 QRectF boundingRect() const;
18 QPainterPath shape() const;
19 QRectF boundingRect() const { return m_rect; }
19 20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
20 21
21 22 public:
22 void updateGeometry(const QPointF& startPoint, qreal armAngle, qreal armLength);
23 void setLabel(QString label);
24 QString label() const {return m_label;}
25 void setPen(QPen pen);
23 void updateGeometry();
24
25 void setArmStartPoint(QPointF point) { m_armStartPoint = point; }
26 QPointF armStartPoint() const { return m_armStartPoint; }
27
28 void setArmAngle(qreal angle) { m_armAngle = angle; }
29 qreal armAngle() const { return m_armAngle; }
30
31 void setArmLength(qreal len) { m_armLength = len; }
32 qreal armLength() const { return m_armLength; }
33
34 void setText(QString text) { m_text = text; }
35 QString text() const { return m_text; }
36
37 void setPen(QPen pen) { m_pen = pen; }
38 QPen pen() const { return m_pen; }
39
40 void setFont(QFont font) { m_font = font; }
41 QFont font() const { return m_font; }
26 42
27 43 private:
28 QString m_label;
29 QPainterPath m_path;
44 QPointF m_armStartPoint;
45 qreal m_armAngle;
46 qreal m_armLength;
47 QString m_text;
30 48 QRectF m_rect;
49 QPainterPath m_armPath;
50 QRectF m_textRect;
31 51 QPen m_pen;
52 QFont m_font;
32 53 };
33 54
34 55 QTCOMMERCIALCHART_END_NAMESPACE
@@ -35,6 +35,14 public:
35 35 void setLabelVisible(bool visible) { m_isLabelVisible = visible; }
36 36 bool isLabelVisible() const { return m_isLabelVisible; }
37 37
38 // TODO:
39 //void setLabelPen(QPen pen) {};
40 //QPen labelPen() const {};
41 //void setLabelFont(QFont font);
42 //QFont labelFont() const;
43 //void setLabelArmLenght(qreal len) {};
44 //qreal labelArmLenght() const {};
45
38 46 void setExploded(bool exploded) { m_isExploded = exploded; }
39 47 bool isExploded() const { return m_isExploded; }
40 48
@@ -109,16 +117,12 public:
109 117
110 118 // TODO: sorting?
111 119
112 // TODO: convenience functions
113 //void updateValue(int sliceIndex, qreal value);
114 //void updateLabel(int sliceIndex, QString label);
115 //void updateColor(int sliceIndex, QColor color);
116 //void updateExploded(int slizeIndex, bool exploded);
117
118 // TODO: customization
119 // set/get pen/brush
120 // - for label
121 // - for whole pie/slice
120 // TODO: convenience functions?
121 //void setValue(QPieSliceId id, qreal value);
122 //void setLabel(QPieSliceId id, QString label);
123 //void setPen(QPieSliceId id, QPen pen);
124 //void setBrush(QPieSliceId id, QBrush brush);
125 //void setExploded(QPieSliceId id, bool exploded);
122 126
123 127 void setSizeFactor(qreal sizeFactor);
124 128 qreal sizeFactor() const { return m_sizeFactor; }
@@ -130,9 +134,11 Q_SIGNALS:
130 134 void changed(const QPieSeries::ChangeSet& changeSet);
131 135 void sizeFactorChanged();
132 136 void positionChanged();
137
138 // TODO:
133 139 //void sliceClicked(QPieSliceId id);
134 //void sliceHoverEnter(QPieSliceId id);
135 //void sliceHoverLeave(QPieSliceId id);
140 // ?? void sliceHoverEnter(QPieSliceId id);
141 // ?? void sliceHoverLeave(QPieSliceId id);
136 142
137 143 private:
138 144 QPieSliceId generateSliceId();
General Comments 0
You need to be logged in to leave comments. Login now