summaryrefslogtreecommitdiff
path: root/src/charts/PieLegend.cpp
blob: 5faabf4d448ad0ae99a52d840ae8620cfd64dcd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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);
}