#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); }