src/widgets/patternviewer.cpp

Thu, 29 Mar 2018 12:10:54 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 29 Mar 2018 12:10:54 +0300
branch
experimental
changeset 1363
b725b7fb63a5
permissions
-rw-r--r--

started work on the pattern editor

#include <QPainter>
#include <QPaintEvent>
#include "patterneditor.h"
#include "patternviewer.h"

/*
 * Construct a pattern viewer for an existing pattern editor.
 */
PatternViewer::PatternViewer(PatternEditor* parent) :
	QWidget {parent},
	editor {parent}
{
	this->transformation.scale(4, 4);
}

/*
 * Unfortunately, Qt doesn't provide an easy way to translate a floating point rectangle
 * into a floating point polygon. So here's a manual implementation.
 */
static QPolygonF transformRect(const QRectF& rect, const QTransform& transform)
{
	QPolygonF transformed;
	for (const QPointF& point : {
		rect.topLeft(),
		rect.topRight(),
		rect.bottomRight(),
		rect.bottomLeft()
	}) {
		transformed.append(transform.map(point));
	}
	return transformed;
}

/*
 * Renders the pattern.
 */
void PatternViewer::paintEvent(QPaintEvent* event)
{
	const Pattern& pattern = this->editor->pattern;
	static const QPixmap viewerBackground {":/data/pattern-background.png"};
	static const QPixmap canvasBackground {":/data/transparent-background.png"};
	QPainter painter {this};
	painter.drawTiledPixmap(this->rect(), viewerBackground);
	painter.setBrush(canvasBackground);
	QRectF canvasRect = QRectF {{0.0f, 0.0f}, pattern.canvasSize};
	painter.drawPolygon(::transformRect(canvasRect, this->transformation));
	painter.setTransform(this->transformation);
	event->accept();
}

mercurial