src/ui/canvas.cpp

Thu, 27 Feb 2020 14:38:48 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 27 Feb 2020 14:38:48 +0200
changeset 59
60650929dc82
parent 58
b7841cd31fb7
child 61
4585d8d7a7ec
permissions
-rw-r--r--

fixed testing of whether screenToModelCoordinates's result value is behind the camera

#include <QMouseEvent>
#include <QPainter>
#include "canvas.h"

Canvas::Canvas(
	Model* model,
	DocumentManager* documents,
	const ldraw::ColorTable& colorTable,
	QWidget* parent) :
	PartRenderer{model, documents, colorTable, parent}
{
	this->setMouseTracking(true);
}

void Canvas::handleSelectionChange(const QSet<ldraw::Id>& selectedIds, const QSet<ldraw::Id>& deselectedIds)
{
	Q_ASSERT(not selectedIds.contains(ldraw::NULL_ID));
	this->selection.subtract(deselectedIds);
	this->selection.unite(selectedIds);
	this->compiler->setSelectedObjects(this->selection);
	this->update();
}

void Canvas::mouseMoveEvent(QMouseEvent* event)
{
	const ldraw::Id id = this->pick(event->pos());
	this->highlighted = id;
	this->totalMouseMove += (event->pos() - this->lastMousePosition).manhattanLength();
	this->lastMousePosition = event->pos();
	this->worldPosition = this->screenToModelCoordinates(this->lastMousePosition, geom::XY);
	if (this->worldPosition.has_value())
	{
		this->worldPosition = glm::round(*this->worldPosition);
	}
	if (this->worldPosition.has_value())
	{
		this->newStatusText("Position: (%1, %2, %3)"_q
			.arg(this->worldPosition->x)
			.arg(this->worldPosition->y)
			.arg(this->worldPosition->z));
	}
	else
	{
		this->newStatusText("Position: <none>"_q);
	}
	PartRenderer::mouseMoveEvent(event);
}

void Canvas::mousePressEvent(QMouseEvent* event)
{
	this->totalMouseMove = 0;
	this->lastMousePosition = event->pos();
	PartRenderer::mousePressEvent(event);
}

void Canvas::mouseReleaseEvent(QMouseEvent* event)
{
	if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0)
	{
		if (this->highlighted == ldraw::NULL_ID)
		{
			this->selection = {};
		}
		else
		{
			this->selection = {this->highlighted};
		}
		this->compiler->setSelectedObjects(this->selection);
		emit selectionChanged(this->selection);
		this->update();
	}
	PartRenderer::mouseReleaseEvent(event);
}

void Canvas::paintGL()
{
	PartRenderer::paintGL();
	if (this->worldPosition.has_value())
	{
		QPainter painter{this};
		painter.setRenderHint(QPainter::Antialiasing);
		painter.setPen(Qt::black);
		painter.setBrush(Qt::green);
		painter.drawEllipse(this->modelToScreenCoordinates(*this->worldPosition), 10, 10);
	}
}

mercurial