@@ -27,6 +27,7 | |||
|
27 | 27 | #include <qmath.h> |
|
28 | 28 | #include <QGraphicsSceneEvent> |
|
29 | 29 | #include <QTime> |
|
30 | #include <QTextDocument> | |
|
30 | 31 | #include <QDebug> |
|
31 | 32 | |
|
32 | 33 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
@@ -45,6 +46,8 PieSliceItem::PieSliceItem(QGraphicsItem *parent) | |||
|
45 | 46 | setAcceptHoverEvents(true); |
|
46 | 47 | setAcceptedMouseButtons(Qt::MouseButtonMask); |
|
47 | 48 | setZValue(ChartPresenter::PieSeriesZValue); |
|
49 | m_labelItem = new QGraphicsTextItem(this); | |
|
50 | m_labelItem->document()->setDocumentMargin(1.0); | |
|
48 | 51 | } |
|
49 | 52 | |
|
50 | 53 | PieSliceItem::~PieSliceItem() |
@@ -80,47 +83,11 void PieSliceItem::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*o | |||
|
80 | 83 | painter->save(); |
|
81 | 84 | |
|
82 | 85 | // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead |
|
83 | // Also, the drawText actually uses the pen color for the text color (unlike QGraphicsSimpleTextItem) | |
|
84 | painter->setPen(m_data.m_labelBrush.color()); | |
|
85 | 86 | painter->setBrush(m_data.m_labelBrush); |
|
86 | painter->setFont(m_data.m_labelFont); | |
|
87 | 87 | |
|
88 | QFontMetricsF fm(m_data.m_labelFont); | |
|
89 | QString label = m_data.m_labelText; | |
|
90 | QRectF labelBoundingRect; | |
|
91 | ||
|
92 | switch (m_data.m_labelPosition) { | |
|
93 | case QPieSlice::LabelOutside: | |
|
88 | if (m_data.m_labelPosition == QPieSlice::LabelOutside) { | |
|
94 | 89 | painter->setClipRect(parentItem()->boundingRect()); |
|
95 | 90 | painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color()); |
|
96 | if (fm.width(m_data.m_labelText) > m_labelTextRect.width()) { | |
|
97 | // Only one line label text is supported currently. | |
|
98 | // The height for the label is set one pixel over the font metrics. | |
|
99 | label = ChartPresenter::truncatedText(m_data.m_labelFont, m_data.m_labelText, | |
|
100 | qreal(0.0), m_labelTextRect.width(), | |
|
101 | fm.height() + 1.0, labelBoundingRect); | |
|
102 | } | |
|
103 | painter->drawText(m_labelTextRect, Qt::AlignCenter, label); | |
|
104 | break; | |
|
105 | case QPieSlice::LabelInsideHorizontal: | |
|
106 | painter->setClipPath(m_slicePath); | |
|
107 | painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText); | |
|
108 | break; | |
|
109 | case QPieSlice::LabelInsideTangential: | |
|
110 | painter->setClipPath(m_slicePath); | |
|
111 | painter->translate(m_labelTextRect.center()); | |
|
112 | painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2); | |
|
113 | painter->drawText(-m_labelTextRect.width() / 2, -m_labelTextRect.height() / 2, m_labelTextRect.width(), m_labelTextRect.height(), Qt::AlignCenter, m_data.m_labelText); | |
|
114 | break; | |
|
115 | case QPieSlice::LabelInsideNormal: | |
|
116 | painter->setClipPath(m_slicePath); | |
|
117 | painter->translate(m_labelTextRect.center()); | |
|
118 | if (m_data.m_startAngle + m_data.m_angleSpan / 2 < 180) | |
|
119 | painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2 - 90); | |
|
120 | else | |
|
121 | painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2 + 90); | |
|
122 | painter->drawText(-m_labelTextRect.width() / 2, -m_labelTextRect.height() / 2, m_labelTextRect.width(), m_labelTextRect.height(), Qt::AlignCenter, m_data.m_labelText); | |
|
123 | break; | |
|
124 | 91 | } |
|
125 | 92 | |
|
126 | 93 | painter->restore(); |
@@ -164,42 +131,80 void PieSliceItem::updateGeometry() | |||
|
164 | 131 | QPointF armStart; |
|
165 | 132 | m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, ¢erAngle, &armStart); |
|
166 | 133 | |
|
167 | // text rect | |
|
168 | QFontMetricsF fm(m_data.m_labelFont); | |
|
169 | m_labelTextRect = QRectF(0, 0, fm.width(m_data.m_labelText), fm.height()); | |
|
170 | ||
|
171 | // label arm path | |
|
172 | QPointF labelTextStart; | |
|
173 | m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); | |
|
174 | ||
|
175 | // text position | |
|
176 | switch (m_data.m_labelPosition) { | |
|
177 | case QPieSlice::LabelOutside: | |
|
178 | m_labelTextRect.moveBottomLeft(labelTextStart); | |
|
179 | if (m_labelTextRect.left() < 0) | |
|
180 | m_labelTextRect.setLeft(0); | |
|
181 | if (m_labelTextRect.right() > parentItem()->boundingRect().right()) | |
|
182 | m_labelTextRect.setRight(parentItem()->boundingRect().right()); | |
|
183 | break; | |
|
184 | case QPieSlice::LabelInsideHorizontal: | |
|
185 | case QPieSlice::LabelInsideTangential: { | |
|
186 | QPointF textCenter; | |
|
187 | if (m_data.m_holeRadius > 0) | |
|
188 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius + (m_data.m_radius - m_data.m_holeRadius) / 2); | |
|
189 | else | |
|
190 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); | |
|
191 |
m_labelTextRect. |
|
|
192 | break; | |
|
193 | } | |
|
194 | case QPieSlice::LabelInsideNormal: { | |
|
195 | QPointF textCenter; | |
|
196 | if (m_data.m_holeRadius > 0) | |
|
197 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius + (m_data.m_radius - m_data.m_holeRadius) / 2); | |
|
198 | else | |
|
199 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); | |
|
200 | m_labelTextRect.moveCenter(textCenter); | |
|
201 | break; | |
|
202 | } | |
|
134 | if (m_data.m_isLabelVisible) { | |
|
135 | // text rect | |
|
136 | QFontMetricsF fm(m_data.m_labelFont); | |
|
137 | m_labelTextRect = ChartPresenter::textBoundingRect(m_data.m_labelFont, | |
|
138 | m_data.m_labelText, | |
|
139 | 0); | |
|
140 | ||
|
141 | QString label(m_data.m_labelText); | |
|
142 | m_labelItem->setVisible(m_data.m_isLabelVisible); | |
|
143 | m_labelItem->setDefaultTextColor(m_data.m_labelBrush.color()); | |
|
144 | m_labelItem->setFont(m_data.m_labelFont); | |
|
145 | ||
|
146 | // text position | |
|
147 | if (m_data.m_labelPosition == QPieSlice::LabelOutside) { | |
|
148 | setFlag(QGraphicsItem::ItemClipsChildrenToShape, false); | |
|
149 | ||
|
150 | // label arm path | |
|
151 | QPointF labelTextStart; | |
|
152 | m_labelArmPath = labelArmPath(armStart, centerAngle, | |
|
153 | m_data.m_radius * m_data.m_labelArmLengthFactor, | |
|
154 | m_labelTextRect.width(), &labelTextStart); | |
|
155 | ||
|
156 | m_labelTextRect.moveBottomLeft(labelTextStart); | |
|
157 | if (m_labelTextRect.left() < 0) | |
|
158 | m_labelTextRect.setLeft(0); | |
|
159 | else if (m_labelTextRect.left() < parentItem()->boundingRect().left()) | |
|
160 | m_labelTextRect.setLeft(parentItem()->boundingRect().left()); | |
|
161 | if (m_labelTextRect.right() > parentItem()->boundingRect().right()) | |
|
162 | m_labelTextRect.setRight(parentItem()->boundingRect().right()); | |
|
163 | ||
|
164 | if (fm.width(m_data.m_labelText) > m_labelTextRect.width()) { | |
|
165 | label = ChartPresenter::truncatedText(m_data.m_labelFont, m_data.m_labelText, | |
|
166 | qreal(0.0), m_labelTextRect.width(), | |
|
167 | m_labelTextRect.height(), m_labelTextRect); | |
|
168 | m_labelArmPath = labelArmPath(armStart, centerAngle, | |
|
169 | m_data.m_radius * m_data.m_labelArmLengthFactor, | |
|
170 | m_labelTextRect.width(), &labelTextStart); | |
|
171 | ||
|
172 | m_labelTextRect.moveBottomLeft(labelTextStart); | |
|
173 | } | |
|
174 | m_labelItem->setTextWidth(m_labelTextRect.width() | |
|
175 | + m_labelItem->document()->documentMargin()); | |
|
176 | m_labelItem->setHtml(label); | |
|
177 | m_labelItem->setPos(m_labelTextRect.x(), m_labelTextRect.y() + 1.0); | |
|
178 | } else { | |
|
179 | // label inside | |
|
180 | setFlag(QGraphicsItem::ItemClipsChildrenToShape); | |
|
181 | m_labelItem->setTextWidth(m_labelTextRect.width() | |
|
182 | + m_labelItem->document()->documentMargin()); | |
|
183 | m_labelItem->setHtml(label); | |
|
184 | ||
|
185 | QPointF textCenter; | |
|
186 | if (m_data.m_holeRadius > 0) { | |
|
187 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius | |
|
188 | + (m_data.m_radius | |
|
189 | - m_data.m_holeRadius) / 2); | |
|
190 | } else { | |
|
191 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); | |
|
192 | } | |
|
193 | m_labelItem->setPos(textCenter.x() - m_labelItem->boundingRect().width() / 2, | |
|
194 | textCenter.y() - fm.height() / 2); | |
|
195 | ||
|
196 | QPointF labelCenter = m_labelItem->boundingRect().center(); | |
|
197 | m_labelItem->setTransformOriginPoint(labelCenter); | |
|
198 | ||
|
199 | if (m_data.m_labelPosition == QPieSlice::LabelInsideTangential) { | |
|
200 | m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2); | |
|
201 | } else if (m_data.m_labelPosition == QPieSlice::LabelInsideNormal) { | |
|
202 | if (m_data.m_startAngle + m_data.m_angleSpan / 2 < 180) | |
|
203 | m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 - 90); | |
|
204 | else | |
|
205 | m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 + 90); | |
|
206 | } | |
|
207 | } | |
|
203 | 208 | } |
|
204 | 209 | |
|
205 | 210 | // bounding rect |
General Comments 0
You need to be logged in to leave comments.
Login now