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