src/ui/canvas.cpp

Sat, 08 Feb 2020 00:08:57 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 08 Feb 2020 00:08:57 +0200
changeset 52
eee644f88e93
parent 51
1a9eac27698d
child 55
cb81ecb5fb23
permissions
-rw-r--r--

avoid having the null id in the selection

#include <QMouseEvent>
#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->newStatusText("Selected: %1"_q.arg(id.value));
	this->highlighted = id;
	this->totalMouseMove += (event->pos() - this->lastMousePosition).manhattanLength();
	this->lastMousePosition = event->pos();
	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);
}

mercurial