@@ -1,313 +1,320 | |||||
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, ¢erAngle, &armStart); |
|
132 | m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, ¢erAngle, &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 | m_labelTextRect = ChartPresenter::textBoundingRect(m_data.m_labelFont, |
|
138 | m_labelTextRect = ChartPresenter::textBoundingRect(m_data.m_labelFont, | |
139 | m_data.m_labelText, |
|
139 | m_data.m_labelText, | |
140 | 0); |
|
140 | 0); | |
141 |
|
141 | |||
142 | QString label(m_data.m_labelText); |
|
142 | QString label(m_data.m_labelText); | |
143 | m_labelItem->setDefaultTextColor(m_data.m_labelBrush.color()); |
|
143 | m_labelItem->setDefaultTextColor(m_data.m_labelBrush.color()); | |
144 | m_labelItem->setFont(m_data.m_labelFont); |
|
144 | m_labelItem->setFont(m_data.m_labelFont); | |
145 |
|
145 | |||
146 | // text position |
|
146 | // text position | |
147 | if (m_data.m_labelPosition == QPieSlice::LabelOutside) { |
|
147 | if (m_data.m_labelPosition == QPieSlice::LabelOutside) { | |
148 | setFlag(QGraphicsItem::ItemClipsChildrenToShape, false); |
|
148 | setFlag(QGraphicsItem::ItemClipsChildrenToShape, false); | |
149 |
|
149 | |||
150 | // label arm path |
|
150 | // label arm path | |
151 | QPointF labelTextStart; |
|
151 | QPointF labelTextStart; | |
152 | m_labelArmPath = labelArmPath(armStart, centerAngle, |
|
152 | m_labelArmPath = labelArmPath(armStart, centerAngle, | |
153 | m_data.m_radius * m_data.m_labelArmLengthFactor, |
|
153 | m_data.m_radius * m_data.m_labelArmLengthFactor, | |
154 | m_labelTextRect.width(), &labelTextStart); |
|
154 | m_labelTextRect.width(), &labelTextStart); | |
155 |
|
155 | |||
156 | m_labelTextRect.moveBottomLeft(labelTextStart); |
|
156 | m_labelTextRect.moveBottomLeft(labelTextStart); | |
157 | if (m_labelTextRect.left() < 0) |
|
157 | if (m_labelTextRect.left() < 0) | |
158 | m_labelTextRect.setLeft(0); |
|
158 | m_labelTextRect.setLeft(0); | |
159 | else if (m_labelTextRect.left() < parentItem()->boundingRect().left()) |
|
159 | else if (m_labelTextRect.left() < parentItem()->boundingRect().left()) | |
160 | m_labelTextRect.setLeft(parentItem()->boundingRect().left()); |
|
160 | m_labelTextRect.setLeft(parentItem()->boundingRect().left()); | |
161 | if (m_labelTextRect.right() > parentItem()->boundingRect().right()) |
|
161 | if (m_labelTextRect.right() > parentItem()->boundingRect().right()) | |
162 | m_labelTextRect.setRight(parentItem()->boundingRect().right()); |
|
162 | m_labelTextRect.setRight(parentItem()->boundingRect().right()); | |
163 |
|
163 | |||
164 | label = ChartPresenter::truncatedText(m_data.m_labelFont, m_data.m_labelText, |
|
164 | label = ChartPresenter::truncatedText(m_data.m_labelFont, m_data.m_labelText, | |
165 | qreal(0.0), m_labelTextRect.width(), |
|
165 | qreal(0.0), m_labelTextRect.width(), | |
166 | m_labelTextRect.height(), m_labelTextRect); |
|
166 | m_labelTextRect.height(), m_labelTextRect); | |
167 | m_labelArmPath = labelArmPath(armStart, centerAngle, |
|
167 | m_labelArmPath = labelArmPath(armStart, centerAngle, | |
168 | m_data.m_radius * m_data.m_labelArmLengthFactor, |
|
168 | m_data.m_radius * m_data.m_labelArmLengthFactor, | |
169 | m_labelTextRect.width(), &labelTextStart); |
|
169 | m_labelTextRect.width(), &labelTextStart); | |
170 | m_labelTextRect.moveBottomLeft(labelTextStart); |
|
170 | m_labelTextRect.moveBottomLeft(labelTextStart); | |
171 |
|
171 | |||
172 | m_labelItem->setTextWidth(m_labelTextRect.width() |
|
172 | m_labelItem->setTextWidth(m_labelTextRect.width() | |
173 | + m_labelItem->document()->documentMargin()); |
|
173 | + m_labelItem->document()->documentMargin()); | |
174 | m_labelItem->setHtml(label); |
|
174 | m_labelItem->setHtml(label); | |
175 | m_labelItem->setRotation(0); |
|
175 | m_labelItem->setRotation(0); | |
176 | m_labelItem->setPos(m_labelTextRect.x(), m_labelTextRect.y() + 1.0); |
|
176 | m_labelItem->setPos(m_labelTextRect.x(), m_labelTextRect.y() + 1.0); | |
177 | } else { |
|
177 | } else { | |
178 | // label inside |
|
178 | // label inside | |
179 | setFlag(QGraphicsItem::ItemClipsChildrenToShape); |
|
179 | setFlag(QGraphicsItem::ItemClipsChildrenToShape); | |
180 | m_labelItem->setTextWidth(m_labelTextRect.width() |
|
180 | m_labelItem->setTextWidth(m_labelTextRect.width() | |
181 | + m_labelItem->document()->documentMargin()); |
|
181 | + m_labelItem->document()->documentMargin()); | |
182 | m_labelItem->setHtml(label); |
|
182 | m_labelItem->setHtml(label); | |
183 |
|
183 | |||
184 | QPointF textCenter; |
|
184 | QPointF textCenter; | |
185 | if (m_data.m_holeRadius > 0) { |
|
185 | if (m_data.m_holeRadius > 0) { | |
186 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius |
|
186 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius | |
187 | + (m_data.m_radius |
|
187 | + (m_data.m_radius | |
188 | - m_data.m_holeRadius) / 2); |
|
188 | - m_data.m_holeRadius) / 2); | |
189 | } else { |
|
189 | } else { | |
190 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); |
|
190 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); | |
191 | } |
|
191 | } | |
192 | m_labelItem->setPos(textCenter.x() - m_labelItem->boundingRect().width() / 2, |
|
192 | m_labelItem->setPos(textCenter.x() - m_labelItem->boundingRect().width() / 2, | |
193 | textCenter.y() - m_labelTextRect.height() / 2); |
|
193 | textCenter.y() - m_labelTextRect.height() / 2); | |
194 |
|
194 | |||
195 | QPointF labelCenter = m_labelItem->boundingRect().center(); |
|
195 | QPointF labelCenter = m_labelItem->boundingRect().center(); | |
196 | m_labelItem->setTransformOriginPoint(labelCenter); |
|
196 | m_labelItem->setTransformOriginPoint(labelCenter); | |
197 |
|
197 | |||
198 | if (m_data.m_labelPosition == QPieSlice::LabelInsideTangential) { |
|
198 | if (m_data.m_labelPosition == QPieSlice::LabelInsideTangential) { | |
199 | m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2); |
|
199 | m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2); | |
200 | } else if (m_data.m_labelPosition == QPieSlice::LabelInsideNormal) { |
|
200 | } else if (m_data.m_labelPosition == QPieSlice::LabelInsideNormal) { | |
201 | if (m_data.m_startAngle + m_data.m_angleSpan / 2 < 180) |
|
201 | if (m_data.m_startAngle + m_data.m_angleSpan / 2 < 180) | |
202 | m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 - 90); |
|
202 | m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 - 90); | |
203 | else |
|
203 | else | |
204 | m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 + 90); |
|
204 | m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 + 90); | |
205 | } else { |
|
205 | } else { | |
206 | m_labelItem->setRotation(0); |
|
206 | m_labelItem->setRotation(0); | |
207 | } |
|
207 | } | |
208 | } |
|
208 | } | |
209 | // Hide label if it's outside the bounding rect of parent item |
|
209 | // Hide label if it's outside the bounding rect of parent item | |
210 | QRectF labelRect(m_labelItem->boundingRect()); |
|
210 | QRectF labelRect(m_labelItem->boundingRect()); | |
211 | labelRect.moveTopLeft(m_labelItem->pos()); |
|
211 | labelRect.moveTopLeft(m_labelItem->pos()); | |
212 |
if (parentItem()->boundingRect(). |
|
212 | if ((parentItem()->boundingRect().left() | |
|
213 | < (labelRect.left() + m_labelItem->document()->documentMargin() + 1.0)) | |||
|
214 | && (parentItem()->boundingRect().right() | |||
|
215 | > (labelRect.right() - m_labelItem->document()->documentMargin() - 1.0)) | |||
|
216 | && (parentItem()->boundingRect().top() | |||
|
217 | < (labelRect.top() + m_labelItem->document()->documentMargin() + 1.0)) | |||
|
218 | && (parentItem()->boundingRect().bottom() | |||
|
219 | > (labelRect.bottom() - m_labelItem->document()->documentMargin() - 1.0))) | |||
213 | m_labelItem->show(); |
|
220 | m_labelItem->show(); | |
214 | else |
|
221 | else | |
215 | m_labelItem->hide(); |
|
222 | m_labelItem->hide(); | |
216 | } |
|
223 | } | |
217 |
|
224 | |||
218 | // bounding rect |
|
225 | // bounding rect | |
219 | if (m_data.m_isLabelVisible) |
|
226 | if (m_data.m_isLabelVisible) | |
220 | m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect); |
|
227 | m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect); | |
221 | else |
|
228 | else | |
222 | m_boundingRect = m_slicePath.boundingRect(); |
|
229 | m_boundingRect = m_slicePath.boundingRect(); | |
223 |
|
230 | |||
224 | // Inflate bounding rect by 2/3 pen width to make sure it encompasses whole slice also for thick pens |
|
231 | // Inflate bounding rect by 2/3 pen width to make sure it encompasses whole slice also for thick pens | |
225 | // and miter joins. |
|
232 | // and miter joins. | |
226 | int penWidth = (m_data.m_slicePen.width() * 2) / 3; |
|
233 | int penWidth = (m_data.m_slicePen.width() * 2) / 3; | |
227 | m_boundingRect = m_boundingRect.adjusted(-penWidth, -penWidth, penWidth, penWidth); |
|
234 | m_boundingRect = m_boundingRect.adjusted(-penWidth, -penWidth, penWidth, penWidth); | |
228 | } |
|
235 | } | |
229 |
|
236 | |||
230 | QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice) |
|
237 | QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice) | |
231 | { |
|
238 | { | |
232 | if (slice->isExploded()) { |
|
239 | if (slice->isExploded()) { | |
233 | qreal centerAngle = slice->startAngle() + (slice->angleSpan() / 2); |
|
240 | qreal centerAngle = slice->startAngle() + (slice->angleSpan() / 2); | |
234 | qreal len = radius * slice->explodeDistanceFactor(); |
|
241 | qreal len = radius * slice->explodeDistanceFactor(); | |
235 | point += offset(centerAngle, len); |
|
242 | point += offset(centerAngle, len); | |
236 | } |
|
243 | } | |
237 | return point; |
|
244 | return point; | |
238 | } |
|
245 | } | |
239 |
|
246 | |||
240 | QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF *armStart) |
|
247 | QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF *armStart) | |
241 | { |
|
248 | { | |
242 | // calculate center angle |
|
249 | // calculate center angle | |
243 | *centerAngle = startAngle + (angleSpan / 2); |
|
250 | *centerAngle = startAngle + (angleSpan / 2); | |
244 |
|
251 | |||
245 | // calculate slice rectangle |
|
252 | // calculate slice rectangle | |
246 | QRectF rect(center.x() - radius, center.y() - radius, radius * 2, radius * 2); |
|
253 | QRectF rect(center.x() - radius, center.y() - radius, radius * 2, radius * 2); | |
247 |
|
254 | |||
248 | // slice path |
|
255 | // slice path | |
249 | QPainterPath path; |
|
256 | QPainterPath path; | |
250 | if (m_data.m_holeRadius > 0) { |
|
257 | if (m_data.m_holeRadius > 0) { | |
251 | QRectF insideRect(center.x() - m_data.m_holeRadius, center.y() - m_data.m_holeRadius, m_data.m_holeRadius * 2, m_data.m_holeRadius * 2); |
|
258 | QRectF insideRect(center.x() - m_data.m_holeRadius, center.y() - m_data.m_holeRadius, m_data.m_holeRadius * 2, m_data.m_holeRadius * 2); | |
252 | path.arcMoveTo(rect, -startAngle + 90); |
|
259 | path.arcMoveTo(rect, -startAngle + 90); | |
253 | path.arcTo(rect, -startAngle + 90, -angleSpan); |
|
260 | path.arcTo(rect, -startAngle + 90, -angleSpan); | |
254 | path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan); |
|
261 | path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan); | |
255 | path.closeSubpath(); |
|
262 | path.closeSubpath(); | |
256 | } else { |
|
263 | } else { | |
257 | path.moveTo(rect.center()); |
|
264 | path.moveTo(rect.center()); | |
258 | path.arcTo(rect, -startAngle + 90, -angleSpan); |
|
265 | path.arcTo(rect, -startAngle + 90, -angleSpan); | |
259 | path.closeSubpath(); |
|
266 | path.closeSubpath(); | |
260 | } |
|
267 | } | |
261 |
|
268 | |||
262 | // calculate label arm start point |
|
269 | // calculate label arm start point | |
263 | *armStart = center; |
|
270 | *armStart = center; | |
264 | *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP); |
|
271 | *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP); | |
265 |
|
272 | |||
266 | return path; |
|
273 | return path; | |
267 | } |
|
274 | } | |
268 |
|
275 | |||
269 | QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart) |
|
276 | QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart) | |
270 | { |
|
277 | { | |
271 | // Normalize the angle to 0-360 range |
|
278 | // Normalize the angle to 0-360 range | |
272 | // NOTE: We are using int here on purpose. Depenging on platform and hardware |
|
279 | // NOTE: We are using int here on purpose. Depenging on platform and hardware | |
273 | // qreal can be a double, float or something the user gives to the Qt configure |
|
280 | // qreal can be a double, float or something the user gives to the Qt configure | |
274 | // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float |
|
281 | // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float | |
275 | // but there are fmod() and fmodf() functions for that. So instead of some #ifdef |
|
282 | // but there are fmod() and fmodf() functions for that. So instead of some #ifdef | |
276 | // that might break we just use int. Precision for this is just fine for our needs. |
|
283 | // that might break we just use int. Precision for this is just fine for our needs. | |
277 | int normalized = angle * 10.0; |
|
284 | int normalized = angle * 10.0; | |
278 | normalized = normalized % 3600; |
|
285 | normalized = normalized % 3600; | |
279 | if (normalized < 0) |
|
286 | if (normalized < 0) | |
280 | normalized += 3600; |
|
287 | normalized += 3600; | |
281 | angle = (qreal) normalized / 10.0; |
|
288 | angle = (qreal) normalized / 10.0; | |
282 |
|
289 | |||
283 | // prevent label arm pointing straight down because it will look bad |
|
290 | // prevent label arm pointing straight down because it will look bad | |
284 | if (angle < 180 && angle > 170) |
|
291 | if (angle < 180 && angle > 170) | |
285 | angle = 170; |
|
292 | angle = 170; | |
286 | if (angle > 180 && angle < 190) |
|
293 | if (angle > 180 && angle < 190) | |
287 | angle = 190; |
|
294 | angle = 190; | |
288 |
|
295 | |||
289 | // line from slice to label |
|
296 | // line from slice to label | |
290 | QPointF parm1 = start + offset(angle, length); |
|
297 | QPointF parm1 = start + offset(angle, length); | |
291 |
|
298 | |||
292 | // line to underline the label |
|
299 | // line to underline the label | |
293 | QPointF parm2 = parm1; |
|
300 | QPointF parm2 = parm1; | |
294 | if (angle < 180) { // arm swings the other way on the left side |
|
301 | if (angle < 180) { // arm swings the other way on the left side | |
295 | parm2 += QPointF(textWidth, 0); |
|
302 | parm2 += QPointF(textWidth, 0); | |
296 | *textStart = parm1; |
|
303 | *textStart = parm1; | |
297 | } else { |
|
304 | } else { | |
298 | parm2 += QPointF(-textWidth, 0); |
|
305 | parm2 += QPointF(-textWidth, 0); | |
299 | *textStart = parm2; |
|
306 | *textStart = parm2; | |
300 | } |
|
307 | } | |
301 |
|
308 | |||
302 | QPainterPath path; |
|
309 | QPainterPath path; | |
303 | path.moveTo(start); |
|
310 | path.moveTo(start); | |
304 | path.lineTo(parm1); |
|
311 | path.lineTo(parm1); | |
305 | path.lineTo(parm2); |
|
312 | path.lineTo(parm2); | |
306 |
|
313 | |||
307 | return path; |
|
314 | return path; | |
308 | } |
|
315 | } | |
309 |
|
316 | |||
310 | #include "moc_piesliceitem_p.cpp" |
|
317 | #include "moc_piesliceitem_p.cpp" | |
311 |
|
318 | |||
312 | QTCOMMERCIALCHART_END_NAMESPACE |
|
319 | QTCOMMERCIALCHART_END_NAMESPACE | |
313 |
|
320 |
General Comments 0
You need to be logged in to leave comments.
Login now