aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/painter.cpp
blob: 74f140ed8814b89f797c7a82f43973023f82e909 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "ui/painter.h"

#include "debugtools.h"

#include <cmath>


Painter::Painter(QWidget *parent) :
    QWidget(parent),
    _font("Latin Modern Roman"),
    _fontMetrics(_font)
{
    // default font size
    _font.setPixelSize(15);
}

Painter::~Painter()
{

}

void Painter::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    if (_structogram == nullptr) {
        return;
    }

    if (*_structogram == nullptr) {
        return;
    }

    QPainter qp(this);
    qp.setFont(_font);

    // TODO default values should not be magic numbers
    QRect rect(50, 50, 600, 00);
    for (auto it = (*_structogram)->begin(); it != (*_structogram)->end(); ++it) {
        drawStatement(qp, *it, rect);
        rect.setY(rect.y() + rect.height());
    }
}

void Painter::drawStatement(QPainter &qp, samb::Statement &statement, const QRect &rect)
{
    QRect textRect(rect.x() + _margin,
                   rect.y() + _margin,
                   rect.width() - 2*_margin,
                   rect.height() - 2*_margin);

    int textHeight;
    if (textRect.width() < _fontMetrics.width(statement.text())) {
        textHeight = (_fontMetrics.height() + _fontMetrics.lineSpacing()) *
                        (int) ceil(_fontMetrics.width(statement.text()) / (double) textRect.width());
    } else {
        textHeight = _fontMetrics.height();
    }

    if (textRect.height() < textHeight) {
        debug_msg("painter error, textRect.height() < height");
    }

    qp.drawRect(rect);
    qp.drawText(textRect, Qt::AlignVCenter, statement.text());
}