##// END OF EJS Templates
Fix pie slice label rotation...
Titta Heikkala -
r2629:6244acef2135
parent child
Show More
@@ -1,308 +1,309
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Enterprise Charts Add-on.
7 ** This file is part of the Qt Enterprise Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 ** accordance with the Qt Enterprise License Agreement provided with the
11 ** accordance with the Qt Enterprise License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "piesliceitem_p.h"
21 #include "piesliceitem_p.h"
22 #include "piechartitem_p.h"
22 #include "piechartitem_p.h"
23 #include "qpieseries.h"
23 #include "qpieseries.h"
24 #include "qpieslice.h"
24 #include "qpieslice.h"
25 #include "chartpresenter_p.h"
25 #include "chartpresenter_p.h"
26 #include <QPainter>
26 #include <QPainter>
27 #include <qmath.h>
27 #include <qmath.h>
28 #include <QGraphicsSceneEvent>
28 #include <QGraphicsSceneEvent>
29 #include <QTime>
29 #include <QTime>
30 #include <QTextDocument>
30 #include <QTextDocument>
31 #include <QDebug>
31 #include <QDebug>
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 QPointF offset(qreal angle, qreal length)
35 QPointF offset(qreal angle, qreal length)
36 {
36 {
37 qreal dx = qSin(angle * (M_PI / 180)) * length;
37 qreal dx = qSin(angle * (M_PI / 180)) * length;
38 qreal dy = qCos(angle * (M_PI / 180)) * length;
38 qreal dy = qCos(angle * (M_PI / 180)) * length;
39 return QPointF(dx, -dy);
39 return QPointF(dx, -dy);
40 }
40 }
41
41
42 PieSliceItem::PieSliceItem(QGraphicsItem *parent)
42 PieSliceItem::PieSliceItem(QGraphicsItem *parent)
43 : QGraphicsObject(parent),
43 : QGraphicsObject(parent),
44 m_hovered(false)
44 m_hovered(false)
45 {
45 {
46 setAcceptHoverEvents(true);
46 setAcceptHoverEvents(true);
47 setAcceptedMouseButtons(Qt::MouseButtonMask);
47 setAcceptedMouseButtons(Qt::MouseButtonMask);
48 setZValue(ChartPresenter::PieSeriesZValue);
48 setZValue(ChartPresenter::PieSeriesZValue);
49 m_labelItem = new QGraphicsTextItem(this);
49 m_labelItem = new QGraphicsTextItem(this);
50 m_labelItem->document()->setDocumentMargin(1.0);
50 m_labelItem->document()->setDocumentMargin(1.0);
51 }
51 }
52
52
53 PieSliceItem::~PieSliceItem()
53 PieSliceItem::~PieSliceItem()
54 {
54 {
55 // If user is hovering over the slice and it gets destroyed we do
55 // If user is hovering over the slice and it gets destroyed we do
56 // not get a hover leave event. So we must emit the signal here.
56 // not get a hover leave event. So we must emit the signal here.
57 if (m_hovered)
57 if (m_hovered)
58 emit hovered(false);
58 emit hovered(false);
59 }
59 }
60
60
61 QRectF PieSliceItem::boundingRect() const
61 QRectF PieSliceItem::boundingRect() const
62 {
62 {
63 return m_boundingRect;
63 return m_boundingRect;
64 }
64 }
65
65
66 QPainterPath PieSliceItem::shape() const
66 QPainterPath PieSliceItem::shape() const
67 {
67 {
68 // Don't include the label and label arm.
68 // Don't include the label and label arm.
69 // This is used to detect a mouse clicks. We do not want clicks from label.
69 // This is used to detect a mouse clicks. We do not want clicks from label.
70 return m_slicePath;
70 return m_slicePath;
71 }
71 }
72
72
73 void PieSliceItem::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
73 void PieSliceItem::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
74 {
74 {
75 painter->save();
75 painter->save();
76 painter->setClipRect(parentItem()->boundingRect());
76 painter->setClipRect(parentItem()->boundingRect());
77 painter->setPen(m_data.m_slicePen);
77 painter->setPen(m_data.m_slicePen);
78 painter->setBrush(m_data.m_sliceBrush);
78 painter->setBrush(m_data.m_sliceBrush);
79 painter->drawPath(m_slicePath);
79 painter->drawPath(m_slicePath);
80 painter->restore();
80 painter->restore();
81
81
82 if (m_data.m_isLabelVisible) {
82 if (m_data.m_isLabelVisible) {
83 painter->save();
83 painter->save();
84
84
85 // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead
85 // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead
86 painter->setBrush(m_data.m_labelBrush);
86 painter->setBrush(m_data.m_labelBrush);
87
87
88 if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
88 if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
89 painter->setClipRect(parentItem()->boundingRect());
89 painter->setClipRect(parentItem()->boundingRect());
90 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
90 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
91 }
91 }
92
92
93 painter->restore();
93 painter->restore();
94 }
94 }
95 }
95 }
96
96
97 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent * /*event*/)
97 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent * /*event*/)
98 {
98 {
99 m_hovered = true;
99 m_hovered = true;
100 emit hovered(true);
100 emit hovered(true);
101 }
101 }
102
102
103 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * /*event*/)
103 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * /*event*/)
104 {
104 {
105 m_hovered = false;
105 m_hovered = false;
106 emit hovered(false);
106 emit hovered(false);
107 }
107 }
108
108
109 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
109 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
110 {
110 {
111 emit clicked(event->buttons());
111 emit clicked(event->buttons());
112 QGraphicsItem::mousePressEvent(event);
112 QGraphicsItem::mousePressEvent(event);
113 }
113 }
114
114
115 void PieSliceItem::setLayout(const PieSliceData &sliceData)
115 void PieSliceItem::setLayout(const PieSliceData &sliceData)
116 {
116 {
117 m_data = sliceData;
117 m_data = sliceData;
118 updateGeometry();
118 updateGeometry();
119 update();
119 update();
120 }
120 }
121
121
122 void PieSliceItem::updateGeometry()
122 void PieSliceItem::updateGeometry()
123 {
123 {
124 if (m_data.m_radius <= 0)
124 if (m_data.m_radius <= 0)
125 return;
125 return;
126
126
127 prepareGeometryChange();
127 prepareGeometryChange();
128
128
129 // slice path
129 // slice path
130 qreal centerAngle;
130 qreal centerAngle;
131 QPointF armStart;
131 QPointF armStart;
132 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
132 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
133
133
134 m_labelItem->setVisible(m_data.m_isLabelVisible);
134 m_labelItem->setVisible(m_data.m_isLabelVisible);
135
135
136 if (m_data.m_isLabelVisible) {
136 if (m_data.m_isLabelVisible) {
137 // text rect
137 // text rect
138 QFontMetricsF fm(m_data.m_labelFont);
138 QFontMetricsF fm(m_data.m_labelFont);
139 m_labelTextRect = ChartPresenter::textBoundingRect(m_data.m_labelFont,
139 m_labelTextRect = ChartPresenter::textBoundingRect(m_data.m_labelFont,
140 m_data.m_labelText,
140 m_data.m_labelText,
141 0);
141 0);
142
142
143 QString label(m_data.m_labelText);
143 QString label(m_data.m_labelText);
144 m_labelItem->setDefaultTextColor(m_data.m_labelBrush.color());
144 m_labelItem->setDefaultTextColor(m_data.m_labelBrush.color());
145 m_labelItem->setFont(m_data.m_labelFont);
145 m_labelItem->setFont(m_data.m_labelFont);
146
146
147 // text position
147 // text position
148 if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
148 if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
149 setFlag(QGraphicsItem::ItemClipsChildrenToShape, false);
149 setFlag(QGraphicsItem::ItemClipsChildrenToShape, false);
150
150
151 // label arm path
151 // label arm path
152 QPointF labelTextStart;
152 QPointF labelTextStart;
153 m_labelArmPath = labelArmPath(armStart, centerAngle,
153 m_labelArmPath = labelArmPath(armStart, centerAngle,
154 m_data.m_radius * m_data.m_labelArmLengthFactor,
154 m_data.m_radius * m_data.m_labelArmLengthFactor,
155 m_labelTextRect.width(), &labelTextStart);
155 m_labelTextRect.width(), &labelTextStart);
156
156
157 m_labelTextRect.moveBottomLeft(labelTextStart);
157 m_labelTextRect.moveBottomLeft(labelTextStart);
158 if (m_labelTextRect.left() < 0)
158 if (m_labelTextRect.left() < 0)
159 m_labelTextRect.setLeft(0);
159 m_labelTextRect.setLeft(0);
160 else if (m_labelTextRect.left() < parentItem()->boundingRect().left())
160 else if (m_labelTextRect.left() < parentItem()->boundingRect().left())
161 m_labelTextRect.setLeft(parentItem()->boundingRect().left());
161 m_labelTextRect.setLeft(parentItem()->boundingRect().left());
162 if (m_labelTextRect.right() > parentItem()->boundingRect().right())
162 if (m_labelTextRect.right() > parentItem()->boundingRect().right())
163 m_labelTextRect.setRight(parentItem()->boundingRect().right());
163 m_labelTextRect.setRight(parentItem()->boundingRect().right());
164
164
165 if (fm.width(m_data.m_labelText) > m_labelTextRect.width()) {
165 if (fm.width(m_data.m_labelText) > m_labelTextRect.width()) {
166 label = ChartPresenter::truncatedText(m_data.m_labelFont, m_data.m_labelText,
166 label = ChartPresenter::truncatedText(m_data.m_labelFont, m_data.m_labelText,
167 qreal(0.0), m_labelTextRect.width(),
167 qreal(0.0), m_labelTextRect.width(),
168 m_labelTextRect.height(), m_labelTextRect);
168 m_labelTextRect.height(), m_labelTextRect);
169 m_labelArmPath = labelArmPath(armStart, centerAngle,
169 m_labelArmPath = labelArmPath(armStart, centerAngle,
170 m_data.m_radius * m_data.m_labelArmLengthFactor,
170 m_data.m_radius * m_data.m_labelArmLengthFactor,
171 m_labelTextRect.width(), &labelTextStart);
171 m_labelTextRect.width(), &labelTextStart);
172
172
173 m_labelTextRect.moveBottomLeft(labelTextStart);
173 m_labelTextRect.moveBottomLeft(labelTextStart);
174 }
174 }
175 m_labelItem->setTextWidth(m_labelTextRect.width()
175 m_labelItem->setTextWidth(m_labelTextRect.width()
176 + m_labelItem->document()->documentMargin());
176 + m_labelItem->document()->documentMargin());
177 m_labelItem->setHtml(label);
177 m_labelItem->setHtml(label);
178 m_labelItem->setRotation(0);
178 m_labelItem->setPos(m_labelTextRect.x(), m_labelTextRect.y() + 1.0);
179 m_labelItem->setPos(m_labelTextRect.x(), m_labelTextRect.y() + 1.0);
179 } else {
180 } else {
180 // label inside
181 // label inside
181 setFlag(QGraphicsItem::ItemClipsChildrenToShape);
182 setFlag(QGraphicsItem::ItemClipsChildrenToShape);
182 m_labelItem->setTextWidth(m_labelTextRect.width()
183 m_labelItem->setTextWidth(m_labelTextRect.width()
183 + m_labelItem->document()->documentMargin());
184 + m_labelItem->document()->documentMargin());
184 m_labelItem->setHtml(label);
185 m_labelItem->setHtml(label);
185
186
186 QPointF textCenter;
187 QPointF textCenter;
187 if (m_data.m_holeRadius > 0) {
188 if (m_data.m_holeRadius > 0) {
188 textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius
189 textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius
189 + (m_data.m_radius
190 + (m_data.m_radius
190 - m_data.m_holeRadius) / 2);
191 - m_data.m_holeRadius) / 2);
191 } else {
192 } else {
192 textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
193 textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
193 }
194 }
194 m_labelItem->setPos(textCenter.x() - m_labelItem->boundingRect().width() / 2,
195 m_labelItem->setPos(textCenter.x() - m_labelItem->boundingRect().width() / 2,
195 textCenter.y() - fm.height() / 2);
196 textCenter.y() - fm.height() / 2);
196
197
197 QPointF labelCenter = m_labelItem->boundingRect().center();
198 QPointF labelCenter = m_labelItem->boundingRect().center();
198 m_labelItem->setTransformOriginPoint(labelCenter);
199 m_labelItem->setTransformOriginPoint(labelCenter);
199
200
200 if (m_data.m_labelPosition == QPieSlice::LabelInsideTangential) {
201 if (m_data.m_labelPosition == QPieSlice::LabelInsideTangential) {
201 m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2);
202 m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2);
202 } else if (m_data.m_labelPosition == QPieSlice::LabelInsideNormal) {
203 } else if (m_data.m_labelPosition == QPieSlice::LabelInsideNormal) {
203 if (m_data.m_startAngle + m_data.m_angleSpan / 2 < 180)
204 if (m_data.m_startAngle + m_data.m_angleSpan / 2 < 180)
204 m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 - 90);
205 m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 - 90);
205 else
206 else
206 m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 + 90);
207 m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 + 90);
207 } else {
208 } else {
208 m_labelItem->setRotation(0);
209 m_labelItem->setRotation(0);
209 }
210 }
210 }
211 }
211 }
212 }
212
213
213 // bounding rect
214 // bounding rect
214 if (m_data.m_isLabelVisible)
215 if (m_data.m_isLabelVisible)
215 m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
216 m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
216 else
217 else
217 m_boundingRect = m_slicePath.boundingRect();
218 m_boundingRect = m_slicePath.boundingRect();
218
219
219 // Inflate bounding rect by 2/3 pen width to make sure it encompasses whole slice also for thick pens
220 // Inflate bounding rect by 2/3 pen width to make sure it encompasses whole slice also for thick pens
220 // and miter joins.
221 // and miter joins.
221 int penWidth = (m_data.m_slicePen.width() * 2) / 3;
222 int penWidth = (m_data.m_slicePen.width() * 2) / 3;
222 m_boundingRect = m_boundingRect.adjusted(-penWidth, -penWidth, penWidth, penWidth);
223 m_boundingRect = m_boundingRect.adjusted(-penWidth, -penWidth, penWidth, penWidth);
223 }
224 }
224
225
225 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
226 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
226 {
227 {
227 if (slice->isExploded()) {
228 if (slice->isExploded()) {
228 qreal centerAngle = slice->startAngle() + (slice->angleSpan() / 2);
229 qreal centerAngle = slice->startAngle() + (slice->angleSpan() / 2);
229 qreal len = radius * slice->explodeDistanceFactor();
230 qreal len = radius * slice->explodeDistanceFactor();
230 point += offset(centerAngle, len);
231 point += offset(centerAngle, len);
231 }
232 }
232 return point;
233 return point;
233 }
234 }
234
235
235 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF *armStart)
236 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF *armStart)
236 {
237 {
237 // calculate center angle
238 // calculate center angle
238 *centerAngle = startAngle + (angleSpan / 2);
239 *centerAngle = startAngle + (angleSpan / 2);
239
240
240 // calculate slice rectangle
241 // calculate slice rectangle
241 QRectF rect(center.x() - radius, center.y() - radius, radius * 2, radius * 2);
242 QRectF rect(center.x() - radius, center.y() - radius, radius * 2, radius * 2);
242
243
243 // slice path
244 // slice path
244 QPainterPath path;
245 QPainterPath path;
245 if (m_data.m_holeRadius > 0) {
246 if (m_data.m_holeRadius > 0) {
246 QRectF insideRect(center.x() - m_data.m_holeRadius, center.y() - m_data.m_holeRadius, m_data.m_holeRadius * 2, m_data.m_holeRadius * 2);
247 QRectF insideRect(center.x() - m_data.m_holeRadius, center.y() - m_data.m_holeRadius, m_data.m_holeRadius * 2, m_data.m_holeRadius * 2);
247 path.arcMoveTo(rect, -startAngle + 90);
248 path.arcMoveTo(rect, -startAngle + 90);
248 path.arcTo(rect, -startAngle + 90, -angleSpan);
249 path.arcTo(rect, -startAngle + 90, -angleSpan);
249 path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan);
250 path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan);
250 path.closeSubpath();
251 path.closeSubpath();
251 } else {
252 } else {
252 path.moveTo(rect.center());
253 path.moveTo(rect.center());
253 path.arcTo(rect, -startAngle + 90, -angleSpan);
254 path.arcTo(rect, -startAngle + 90, -angleSpan);
254 path.closeSubpath();
255 path.closeSubpath();
255 }
256 }
256
257
257 // calculate label arm start point
258 // calculate label arm start point
258 *armStart = center;
259 *armStart = center;
259 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
260 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
260
261
261 return path;
262 return path;
262 }
263 }
263
264
264 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
265 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
265 {
266 {
266 // Normalize the angle to 0-360 range
267 // Normalize the angle to 0-360 range
267 // NOTE: We are using int here on purpose. Depenging on platform and hardware
268 // NOTE: We are using int here on purpose. Depenging on platform and hardware
268 // qreal can be a double, float or something the user gives to the Qt configure
269 // qreal can be a double, float or something the user gives to the Qt configure
269 // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float
270 // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float
270 // but there are fmod() and fmodf() functions for that. So instead of some #ifdef
271 // but there are fmod() and fmodf() functions for that. So instead of some #ifdef
271 // that might break we just use int. Precision for this is just fine for our needs.
272 // that might break we just use int. Precision for this is just fine for our needs.
272 int normalized = angle * 10.0;
273 int normalized = angle * 10.0;
273 normalized = normalized % 3600;
274 normalized = normalized % 3600;
274 if (normalized < 0)
275 if (normalized < 0)
275 normalized += 3600;
276 normalized += 3600;
276 angle = (qreal) normalized / 10.0;
277 angle = (qreal) normalized / 10.0;
277
278
278 // prevent label arm pointing straight down because it will look bad
279 // prevent label arm pointing straight down because it will look bad
279 if (angle < 180 && angle > 170)
280 if (angle < 180 && angle > 170)
280 angle = 170;
281 angle = 170;
281 if (angle > 180 && angle < 190)
282 if (angle > 180 && angle < 190)
282 angle = 190;
283 angle = 190;
283
284
284 // line from slice to label
285 // line from slice to label
285 QPointF parm1 = start + offset(angle, length);
286 QPointF parm1 = start + offset(angle, length);
286
287
287 // line to underline the label
288 // line to underline the label
288 QPointF parm2 = parm1;
289 QPointF parm2 = parm1;
289 if (angle < 180) { // arm swings the other way on the left side
290 if (angle < 180) { // arm swings the other way on the left side
290 parm2 += QPointF(textWidth, 0);
291 parm2 += QPointF(textWidth, 0);
291 *textStart = parm1;
292 *textStart = parm1;
292 } else {
293 } else {
293 parm2 += QPointF(-textWidth, 0);
294 parm2 += QPointF(-textWidth, 0);
294 *textStart = parm2;
295 *textStart = parm2;
295 }
296 }
296
297
297 QPainterPath path;
298 QPainterPath path;
298 path.moveTo(start);
299 path.moveTo(start);
299 path.lineTo(parm1);
300 path.lineTo(parm1);
300 path.lineTo(parm2);
301 path.lineTo(parm2);
301
302
302 return path;
303 return path;
303 }
304 }
304
305
305 #include "moc_piesliceitem_p.cpp"
306 #include "moc_piesliceitem_p.cpp"
306
307
307 QTCOMMERCIALCHART_END_NAMESPACE
308 QTCOMMERCIALCHART_END_NAMESPACE
308
309
General Comments 0
You need to be logged in to leave comments. Login now