From afa09ce7f03149b2a377d955491a32c376e1a8dc 2012-03-01 06:54:42 From: Jani Honkonen Date: 2012-03-01 06:54:42 Subject: [PATCH] Pie now actually withs the given rectangle and does not go over --- diff --git a/src/piechart/piepresenter.cpp b/src/piechart/piepresenter.cpp index bd6cbd1..9be832e 100644 --- a/src/piechart/piepresenter.cpp +++ b/src/piechart/piepresenter.cpp @@ -123,12 +123,22 @@ void PiePresenter::updateGeometry() foreach (QPieSlice* s, m_series->m_slices) { // calculate the farthest point in the slice from the pie center + + // the arm qreal centerAngle = s->m_startAngle + (s->m_angleSpan / 2); - qreal len = pieRadius + s->labelArmLength() + s->explodeDistance(); + qreal len = pieRadius + PIESLICE_LABEL_GAP + s->labelArmLength() + s->explodeDistance(); QPointF dp(qSin(centerAngle*(PI/180)) * len, -qCos(centerAngle*(PI/180)) * len); QPointF p = pieRect.center() + dp; - // TODO: consider the label text + // the label text + QFontMetricsF fm(s->labelFont()); + QRectF labelRect = fm.boundingRect(s->label()); + if (centerAngle < 90 || centerAngle > 270) + p += QPointF(0, -labelRect.height()); + if (centerAngle < 180) + p += QPointF(labelRect.width(), 0); + else + p += QPointF(-labelRect.width(), 0); // calculate how much the radius must get smaller to fit that point in the base rectangle qreal dt = m_rect.top() - p.y(); diff --git a/src/piechart/pieslice.cpp b/src/piechart/pieslice.cpp index cdb07d3..756783e 100644 --- a/src/piechart/pieslice.cpp +++ b/src/piechart/pieslice.cpp @@ -100,7 +100,7 @@ void PieSlice::updateGeometry() // update label position qreal radius = rect.height() / 2; - QPointF edgeCenter = rect.center() + offset(centerAngle, radius + 5); + QPointF edgeCenter = rect.center() + offset(centerAngle, radius + PIESLICE_LABEL_GAP); m_slicelabel->setArmStartPoint(edgeCenter); m_slicelabel->setArmAngle(centerAngle); m_slicelabel->updateGeometry(); diff --git a/src/piechart/pieslice_p.h b/src/piechart/pieslice_p.h index fd0660e..f6623d6 100644 --- a/src/piechart/pieslice_p.h +++ b/src/piechart/pieslice_p.h @@ -9,6 +9,8 @@ #include #include +#define PIESLICE_LABEL_GAP 5 + QTCOMMERCIALCHART_BEGIN_NAMESPACE class PiePresenter; class PieSliceLabel;