From 7ea534dd1c8bf72200a999cae554d842d9035ba9 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 1 Jan 2018 18:44:35 +0100 Subject: New StatementDialog, rename diagram classes Other changes: - Use raw pointer instead of smart pointers to manage resources - Initial Painter implementation --- src/ui/painter.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) (limited to 'src/ui/painter.cpp') diff --git a/src/ui/painter.cpp b/src/ui/painter.cpp index b04e389..74f140e 100644 --- a/src/ui/painter.cpp +++ b/src/ui/painter.cpp @@ -1,14 +1,66 @@ #include "ui/painter.h" -#include "ui_painter.h" + +#include "debugtools.h" + +#include + Painter::Painter(QWidget *parent) : QWidget(parent), - _ui(new Ui::Painter) + _font("Latin Modern Roman"), + _fontMetrics(_font) { - _ui->setupUi(this); + // default font size + _font.setPixelSize(15); } Painter::~Painter() { - delete _ui; + +} + +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()); } -- cgit v1.2.1