added right click support for draw tool

Fri, 30 Jul 2021 01:28:39 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 30 Jul 2021 01:28:39 +0300
changeset 121
000781318c36
parent 120
8c9fff699241
child 122
b54b350dff5d

added right click support for draw tool

src/document.cpp file | annotate | diff | comparison | revisions
src/document.h file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.h file | annotate | diff | comparison | revisions
src/tools/basetool.h file | annotate | diff | comparison | revisions
src/tools/drawtool.cpp file | annotate | diff | comparison | revisions
src/tools/drawtool.h file | annotate | diff | comparison | revisions
src/tools/selecttool.cpp file | annotate | diff | comparison | revisions
src/tools/selecttool.h file | annotate | diff | comparison | revisions
src/ui/canvas.cpp file | annotate | diff | comparison | revisions
src/ui/canvas.h file | annotate | diff | comparison | revisions
--- a/src/document.cpp	Wed Jul 28 13:22:51 2021 +0300
+++ b/src/document.cpp	Fri Jul 30 01:28:39 2021 +0300
@@ -73,13 +73,13 @@
 		this->selectionChanged(resolve(this->ui.listView->selectionModel()->selection()));
 	});
 	connect(this->model, &Model::dataChanged, this->renderer, qOverload<>(&Canvas::update));
-	connect(this->renderer, &Canvas::mouseClick, this, [this](Canvas* canvas)
+	connect(this->renderer, &Canvas::mouseClick, this, [this](Canvas* canvas, QMouseEvent* event)
 	{
-		Q_EMIT this->mouseClick(this, canvas);
+		Q_EMIT this->mouseClick(this, canvas, event);
 	});
-	connect(this->renderer, &Canvas::mouseMove, this, [this](Canvas* canvas)
+	connect(this->renderer, &Canvas::mouseMove, this, [this](Canvas* canvas, QMouseEvent* event)
 	{
-		Q_EMIT this->mouseMove(this, canvas);
+		Q_EMIT this->mouseMove(this, canvas, event);
 	});
 	connect(&this->vertexMap, &VertexMap::verticesChanged, [&]()
 	{
--- a/src/document.h	Wed Jul 28 13:22:51 2021 +0300
+++ b/src/document.h	Fri Jul 30 01:28:39 2021 +0300
@@ -48,8 +48,8 @@
 Q_SIGNALS:
 	void newStatusText(const QString& newStatusText);
 	void splitterChanged();
-	void mouseClick(Document* document, Canvas* canvas);
-	void mouseMove(Document* document, Canvas* canvas);
+	void mouseClick(Document* document, Canvas* canvas, QMouseEvent* event);
+	void mouseMove(Document* document, Canvas* canvas, QMouseEvent* event);
 private:
 	void selectionChanged(const QSet<ldraw::id_t>& newSelection);
 	Model* model;
--- a/src/mainwindow.cpp	Wed Jul 28 13:22:51 2021 +0300
+++ b/src/mainwindow.cpp	Fri Jul 30 01:28:39 2021 +0300
@@ -424,11 +424,11 @@
 	Q_UNUSED(event)
 }
 
-void MainWindow::canvasMouseReleased(Document* document, Canvas* canvas)
+void MainWindow::canvasMouseReleased(Document* document, Canvas* canvas, QMouseEvent* event)
 {
 	if (this->selectedTool != nullptr)
 	{
-		this->selectedTool->mouseClick(document, canvas);
+		this->selectedTool->mouseClick(document, canvas, event);
 	}
 }
 
@@ -437,11 +437,11 @@
 	Q_UNUSED(event)
 }
 
-void MainWindow::canvasMouseMoved(Document* document, Canvas* canvas)
+void MainWindow::canvasMouseMoved(Document* document, Canvas* canvas, QMouseEvent* event)
 {
 	if (this->selectedTool != nullptr)
 	{
-		this->selectedTool->mouseMove(document, canvas);
+		this->selectedTool->mouseMove(document, canvas, event);
 	}
 }
 
--- a/src/mainwindow.h	Wed Jul 28 13:22:51 2021 +0300
+++ b/src/mainwindow.h	Fri Jul 30 01:28:39 2021 +0300
@@ -78,8 +78,8 @@
 	void selectTool(BaseTool* tool);
 private Q_SLOTS:
 	void canvasMousePressed(QMouseEvent* event);
-	void canvasMouseReleased(Document *document, Canvas *canvas);
+	void canvasMouseReleased(Document *document, Canvas *canvas, QMouseEvent *event);
 	void canvasMouseDoubleClicked(QMouseEvent* event);
-	void canvasMouseMoved(Document *document, Canvas *canvas);
+	void canvasMouseMoved(Document *document, Canvas *canvas, QMouseEvent *event);
 	void canvasKeyReleased(QKeyEvent*);
 };
--- a/src/tools/basetool.h	Wed Jul 28 13:22:51 2021 +0300
+++ b/src/tools/basetool.h	Fri Jul 30 01:28:39 2021 +0300
@@ -14,10 +14,9 @@
 
 	virtual QString name() const = 0;
 	virtual QString toolTip() const = 0;
-	virtual bool mousePressed(QMouseEvent*) { return false; }
-	virtual bool mouseClick(Document*, Canvas*) { return false; }
-	virtual bool mouseDoubleClicked(QMouseEvent*) { return false; }
-	virtual bool mouseMove(Document*, Canvas*) { return false; }
+	virtual bool mouseClick(Document*, Canvas*, QMouseEvent*) { return false; }
+	virtual bool mouseDoubleClicked(QMouseEvent*, QMouseEvent*) { return false; }
+	virtual bool mouseMove(Document*, Canvas*, QMouseEvent*) { return false; }
 	virtual bool keyReleased(QKeyEvent*) { return false; }
 	virtual void reset() {}
 	virtual void overpaint(Canvas*, QPainter*) const {}
--- a/src/tools/drawtool.cpp	Wed Jul 28 13:22:51 2021 +0300
+++ b/src/tools/drawtool.cpp	Fri Jul 30 01:28:39 2021 +0300
@@ -26,46 +26,65 @@
 	return result;
 }
 
-bool DrawTool::mouseClick(Document* document, Canvas* canvas)
+bool DrawTool::mouseClick(Document* document, Canvas* canvas, QMouseEvent* event)
 {
-	const auto& worldPosition = canvas->getWorldPosition();
-	if (worldPosition.has_value())
+	if (event->button() == Qt::LeftButton)
 	{
-		const glm::vec3& pos = worldPosition.value();
-		const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);};
-		if (any(this->polygon, isCloseToPos))
+		const auto& worldPosition = canvas->getWorldPosition();
+		if (worldPosition.has_value())
 		{
-			this->closeShape(document);
-		}
-		else
-		{
-			this->polygon.push_back(pos);
-			if (this->polygon.size() == 4)
+			const glm::vec3& pos = worldPosition.value();
+			const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);};
+			if (any(this->polygon, isCloseToPos))
 			{
 				this->closeShape(document);
 			}
+			else
+			{
+				this->polygon.push_back(pos);
+				if (this->polygon.size() == 4)
+				{
+					this->closeShape(document);
+				}
+			}
 		}
+		this->previewPolygon = this->polygon;
+		return true;
 	}
-	this->previewPolygon = this->polygon;
-	return true;
+	else if (event->button() == Qt::RightButton and this->polygon.size() > 0)
+	{
+		this->polygon.erase(this->polygon.end() - 1);
+		this->updatePreviewPolygon();
+		return true;
+	}
+	else
+	{
+		return false;
+	}
 }
 
-bool DrawTool::mouseMove(Document* document, Canvas* canvas)
+bool DrawTool::mouseMove(Document* document, Canvas* canvas, QMouseEvent *event)
 {
 	static_cast<void>(document);
+	static_cast<void>(event);
 	const auto& worldPosition = canvas->getWorldPosition();
 	if (worldPosition.has_value())
 	{
 		this->previewPoint = worldPosition.value();
 		if (this->polygon.size() < 4)
 		{
-			this->previewPolygon.resize(this->polygon.size() + 1);
-			this->previewPolygon.back() = this->previewPoint;
+			this->updatePreviewPolygon();
 		}
 	}
 	return false;
 }
 
+void DrawTool::updatePreviewPolygon()
+{
+	this->previewPolygon.resize(this->polygon.size() + 1);
+	this->previewPolygon.back() = this->previewPoint;
+}
+
 void DrawTool::reset()
 {
 	this->polygon.clear();
--- a/src/tools/drawtool.h	Wed Jul 28 13:22:51 2021 +0300
+++ b/src/tools/drawtool.h	Fri Jul 30 01:28:39 2021 +0300
@@ -10,8 +10,8 @@
 
 	QString name() const override;
 	QString toolTip() const override;
-	bool mouseClick(Document* document, Canvas* canvas) override;
-	bool mouseMove(Document* document, Canvas* canvas) override;
+	bool mouseClick(Document* document, Canvas* canvas, QMouseEvent* event) override;
+	bool mouseMove(Document* document, Canvas* canvas, QMouseEvent* event) override;
 	void reset() override;
 	void overpaint(Canvas*, QPainter*) const override;
 private:
@@ -19,4 +19,5 @@
 	std::vector<glm::vec3> polygon;
 	std::vector<glm::vec3> previewPolygon;
 	glm::vec3 previewPoint;
+	void updatePreviewPolygon();
 };
--- a/src/tools/selecttool.cpp	Wed Jul 28 13:22:51 2021 +0300
+++ b/src/tools/selecttool.cpp	Fri Jul 30 01:28:39 2021 +0300
@@ -15,14 +15,21 @@
 	return result;
 }
 
-bool SelectTool::mouseClick(Document* document, Canvas* canvas)
+bool SelectTool::mouseClick(Document* document, Canvas* canvas, QMouseEvent* event)
 {
-	static_cast<void>(document);
-	const ldraw::id_t highlighted = canvas->getHighlightedObject();
-	canvas->clearSelection();
-	if (highlighted != ldraw::NULL_ID)
+	if (event->button() == Qt::LeftButton)
 	{
-		canvas->addToSelection(highlighted);
+		static_cast<void>(document);
+		const ldraw::id_t highlighted = canvas->getHighlightedObject();
+		canvas->clearSelection();
+		if (highlighted != ldraw::NULL_ID)
+		{
+			canvas->addToSelection(highlighted);
+		}
+		return true;
 	}
-	return true;
+	else
+	{
+		return false;
+	}
 }
--- a/src/tools/selecttool.h	Wed Jul 28 13:22:51 2021 +0300
+++ b/src/tools/selecttool.h	Fri Jul 30 01:28:39 2021 +0300
@@ -10,5 +10,5 @@
 
 	QString name() const override;
 	QString toolTip() const override;
-	bool mouseClick(Document*, Canvas*) override;
+	bool mouseClick(Document*, Canvas*, QMouseEvent*) override;
 };
--- a/src/ui/canvas.cpp	Wed Jul 28 13:22:51 2021 +0300
+++ b/src/ui/canvas.cpp	Fri Jul 30 01:28:39 2021 +0300
@@ -86,8 +86,9 @@
 		this->updateGridMatrix();
 		this->update();
 	}
-	Q_EMIT this->mouseMove(this);
+	Q_EMIT this->mouseMove(this, event);
 	PartRenderer::mouseMoveEvent(event);
+	this->update();
 }
 
 void Canvas::mousePressEvent(QMouseEvent* event)
@@ -101,9 +102,10 @@
 {
 	if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0)
 	{
-		Q_EMIT this->mouseClick(this);
+		Q_EMIT this->mouseClick(this, event);
 	}
 	PartRenderer::mouseReleaseEvent(event);
+	this->update();
 }
 
 void Canvas::initializeGL()
--- a/src/ui/canvas.h	Wed Jul 28 13:22:51 2021 +0300
+++ b/src/ui/canvas.h	Fri Jul 30 01:28:39 2021 +0300
@@ -35,8 +35,8 @@
 Q_SIGNALS:
 	void newStatusText(const QString& newStatusText);
 	void selectionChanged(const QSet<ldraw::id_t>& newSelection);
-	void mouseClick(Canvas* canvas);
-	void mouseMove(Canvas* canvas);
+	void mouseClick(Canvas* canvas, QMouseEvent* event);
+	void mouseMove(Canvas* canvas, QMouseEvent* event);
 private:
 	void updateGridMatrix();
 	glm::vec3 cameraVector() const;

mercurial