diff options
Diffstat (limited to 'src/charts/PieLegend.cpp')
-rw-r--r-- | src/charts/PieLegend.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/charts/PieLegend.cpp b/src/charts/PieLegend.cpp new file mode 100644 index 0000000..5faabf4 --- /dev/null +++ b/src/charts/PieLegend.cpp @@ -0,0 +1,48 @@ +#include "PieLegend.h" +#include "PieChartScene.h" + +PieLegend::PieLegend(const QPointF& pos, const PieChartScene* scene): + scene(scene) +{ + setPos(pos); + addLabels(); +} + +QRectF PieLegend::boundingRect() const +{ + return QRectF(-Width / 2, -Width / 2, Width, Width); +} + +void PieLegend::addLabels() +{ + for(int i = 0; i < scene->getDataSet().size(); i++) + addLabel(i, getLabelPos(i)); +} + +QPointF PieLegend::getLabelPos(int index) const +{ + return boundingRect().topLeft() + + QPointF(SquareSide / 2, SquareSide / 2) + + index * QPointF(0, SquareSide + LabelSpacing); +} + +void PieLegend::addLabel(int index, const QPointF& pos) +{ + QRectF squareRect(-SquareSide / 2., -SquareSide / 2., + SquareSide, SquareSide); + QGraphicsRectItem* squareItem = new QGraphicsRectItem(squareRect, this); + squareItem->setBrush(QColor(scene->getColors()[index])); + squareItem->setPos(pos); + + QGraphicsSimpleTextItem* textItem = + new QGraphicsSimpleTextItem(scene->getDataSet()[index].label, this); + textItem->setPos(getTextPos(pos, textItem)); +} + +QPointF PieLegend::getTextPos(const QPointF& squarePos, + const QGraphicsSimpleTextItem* textItem) const +{ + return squarePos + + QPointF(SquareSide / 2. + LabelTextSpacing, + -textItem->boundingRect().height() / 2); +} |