@@ -27,6 +27,7 | |||||
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 <QDebug> |
|
31 | #include <QDebug> | |
31 |
|
32 | |||
32 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
33 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
@@ -45,6 +46,8 PieSliceItem::PieSliceItem(QGraphicsItem *parent) | |||||
45 | setAcceptHoverEvents(true); |
|
46 | setAcceptHoverEvents(true); | |
46 | setAcceptedMouseButtons(Qt::MouseButtonMask); |
|
47 | setAcceptedMouseButtons(Qt::MouseButtonMask); | |
47 | setZValue(ChartPresenter::PieSeriesZValue); |
|
48 | setZValue(ChartPresenter::PieSeriesZValue); | |
|
49 | m_labelItem = new QGraphicsTextItem(this); | |||
|
50 | m_labelItem->document()->setDocumentMargin(1.0); | |||
48 | } |
|
51 | } | |
49 |
|
52 | |||
50 | PieSliceItem::~PieSliceItem() |
|
53 | PieSliceItem::~PieSliceItem() | |
@@ -80,47 +83,11 void PieSliceItem::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*o | |||||
80 | painter->save(); |
|
83 | painter->save(); | |
81 |
|
84 | |||
82 | // 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 | |
83 | // Also, the drawText actually uses the pen color for the text color (unlike QGraphicsSimpleTextItem) |
|
|||
84 | painter->setPen(m_data.m_labelBrush.color()); |
|
|||
85 | painter->setBrush(m_data.m_labelBrush); |
|
86 | painter->setBrush(m_data.m_labelBrush); | |
86 | painter->setFont(m_data.m_labelFont); |
|
|||
87 |
|
87 | |||
88 | QFontMetricsF fm(m_data.m_labelFont); |
|
88 | if (m_data.m_labelPosition == QPieSlice::LabelOutside) { | |
89 | QString label = m_data.m_labelText; |
|
|||
90 | QRectF labelBoundingRect; |
|
|||
91 |
|
||||
92 | switch (m_data.m_labelPosition) { |
|
|||
93 | case QPieSlice::LabelOutside: |
|
|||
94 | painter->setClipRect(parentItem()->boundingRect()); |
|
89 | painter->setClipRect(parentItem()->boundingRect()); | |
95 | painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color()); |
|
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 | painter->restore(); |
|
93 | painter->restore(); | |
@@ -164,41 +131,79 void PieSliceItem::updateGeometry() | |||||
164 | QPointF armStart; |
|
131 | QPointF armStart; | |
165 | 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); | |
166 |
|
133 | |||
|
134 | if (m_data.m_isLabelVisible) { | |||
167 | // text rect |
|
135 | // text rect | |
168 | QFontMetricsF fm(m_data.m_labelFont); |
|
136 | QFontMetricsF fm(m_data.m_labelFont); | |
169 | m_labelTextRect = QRectF(0, 0, fm.width(m_data.m_labelText), fm.height()); |
|
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); | |||
170 |
|
149 | |||
171 | // label arm path |
|
150 | // label arm path | |
172 | QPointF labelTextStart; |
|
151 | QPointF labelTextStart; | |
173 | m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); |
|
152 | m_labelArmPath = labelArmPath(armStart, centerAngle, | |
|
153 | m_data.m_radius * m_data.m_labelArmLengthFactor, | |||
|
154 | m_labelTextRect.width(), &labelTextStart); | |||
174 |
|
155 | |||
175 | // text position |
|
|||
176 | switch (m_data.m_labelPosition) { |
|
|||
177 | case QPieSlice::LabelOutside: |
|
|||
178 | m_labelTextRect.moveBottomLeft(labelTextStart); |
|
156 | m_labelTextRect.moveBottomLeft(labelTextStart); | |
179 | if (m_labelTextRect.left() < 0) |
|
157 | if (m_labelTextRect.left() < 0) | |
180 | m_labelTextRect.setLeft(0); |
|
158 | m_labelTextRect.setLeft(0); | |
|
159 | else if (m_labelTextRect.left() < parentItem()->boundingRect().left()) | |||
|
160 | m_labelTextRect.setLeft(parentItem()->boundingRect().left()); | |||
181 | if (m_labelTextRect.right() > parentItem()->boundingRect().right()) |
|
161 | if (m_labelTextRect.right() > parentItem()->boundingRect().right()) | |
182 | m_labelTextRect.setRight(parentItem()->boundingRect().right()); |
|
162 | m_labelTextRect.setRight(parentItem()->boundingRect().right()); | |
183 | break; |
|
163 | ||
184 | case QPieSlice::LabelInsideHorizontal: |
|
164 | if (fm.width(m_data.m_labelText) > m_labelTextRect.width()) { | |
185 | case QPieSlice::LabelInsideTangential: { |
|
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 | ||||
186 | QPointF textCenter; |
|
185 | QPointF textCenter; | |
187 | if (m_data.m_holeRadius > 0) |
|
186 | if (m_data.m_holeRadius > 0) { | |
188 |
textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius |
|
187 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius | |
189 | else |
|
188 | + (m_data.m_radius | |
|
189 | - m_data.m_holeRadius) / 2); | |||
|
190 | } else { | |||
190 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); |
|
191 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); | |
191 | m_labelTextRect.moveCenter(textCenter); |
|
|||
192 | break; |
|
|||
193 | } |
|
192 | } | |
194 | case QPieSlice::LabelInsideNormal: { |
|
193 | m_labelItem->setPos(textCenter.x() - m_labelItem->boundingRect().width() / 2, | |
195 | QPointF textCenter; |
|
194 | textCenter.y() - fm.height() / 2); | |
196 | if (m_data.m_holeRadius > 0) |
|
195 | ||
197 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_holeRadius + (m_data.m_radius - m_data.m_holeRadius) / 2); |
|
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); | |||
198 | else |
|
204 | else | |
199 | textCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2); |
|
205 | m_labelItem->setRotation(m_data.m_startAngle + m_data.m_angleSpan / 2 + 90); | |
200 | m_labelTextRect.moveCenter(textCenter); |
|
206 | } | |
201 | break; |
|
|||
202 | } |
|
207 | } | |
203 | } |
|
208 | } | |
204 |
|
209 |
@@ -81,6 +81,7 private: | |||||
81 | QPainterPath m_labelArmPath; |
|
81 | QPainterPath m_labelArmPath; | |
82 | QRectF m_labelTextRect; |
|
82 | QRectF m_labelTextRect; | |
83 | bool m_hovered; |
|
83 | bool m_hovered; | |
|
84 | QGraphicsTextItem *m_labelItem; | |||
84 |
|
85 | |||
85 | friend class PieSliceAnimation; |
|
86 | friend class PieSliceAnimation; | |
86 | }; |
|
87 | }; |
General Comments 0
You need to be logged in to leave comments.
Login now