src/document.cpp

Thu, 09 Jun 2022 13:32:55 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Thu, 09 Jun 2022 13:32:55 +0300
changeset 210
232e7634cc8a
parent 205
1a4342d80de7
child 214
8e1fe64ce4e3
permissions
-rw-r--r--

more refactoring, dosn't build yet

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2020 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QMouseEvent>
#include <QMessageBox>
#include <QVBoxLayout>
#include "document.h"
#include "model.h"
#include "ui/objecteditor.h"

EditorTabWidget::EditorTabWidget(
	Model* model,
	DocumentManager* documents,
	const ColorTable& colorTable,
	QWidget* parent) :
	QWidget{parent},
	colorTable{colorTable},
	canvas{new Canvas{model, this, documents, colorTable, this}},
	model{model},
	vertexMap{model}
{
	this->setMouseTracking(true);
	connect(this->canvas, &Canvas::mouseClick, this, &EditorTabWidget::canvasMouseClick);
	connect(this->canvas, &Canvas::mouseMove, this, &EditorTabWidget::canvasMouseMove);
	connect(this->canvas, &Canvas::newStatusText, this, &EditorTabWidget::newStatusText);
	connect(this->model, &Model::dataChanged, this->canvas, qOverload<>(&Canvas::update));
	connect(&this->vertexMap, &VertexMap::verticesChanged, [&]()
	{
		this->canvas->rebuildVertices(this);
	});
	this->canvas->drawState = &this->drawState;
	QVBoxLayout* layout = new QVBoxLayout{this};
	layout->addWidget(this->canvas);
}

EditorTabWidget::~EditorTabWidget()
{
}

void EditorTabWidget::applyToVertices(VertexMap::ApplyFunction fn) const
{
	this->vertexMap.apply(fn);
}

void EditorTabWidget::setEditMode(EditingMode mode)
{
	this->drawState.mode = mode;
}

void updatePreviewPolygon(DrawState* drawState)
{
	drawState->previewPolygon = drawState->polygon;
	drawState->previewPolygon.resize(drawState->polygon.size() + 1);
	drawState->previewPolygon.back() = drawState->previewPoint;
	if (drawState->previewPolygon.size() > 2)
	{
		drawState->isconcave = not isConvex(drawState->previewPolygon);
	}
}

void removeLastPoint(DrawState* drawState)
{
	if (drawState->polygon.size() > 0)
	{
		drawState->polygon.erase(drawState->polygon.end() - 1);
		updatePreviewPolygon(drawState);
	}
}

bool isCloseToExistingPoints(const std::vector<glm::vec3>& points, const glm::vec3 &pos)
{
	return any(points, std::bind(isclose, std::placeholders::_1, pos));
}

void EditorTabWidget::canvasMouseClick(QMouseEvent *event)
{
	switch(this->drawState.mode)
	{
	case SelectMode:
		if (event->button() == Qt::LeftButton)
		{
			const ModelId highlighted = this->canvas->getHighlightedObject();
			QSet<ModelId> selected;
			if (highlighted != ModelId{0}) {
				selected.insert(highlighted);
			}
			//this->select(selected);
			event->accept();
		}
		break;
	case DrawMode:
		if (event->button() == Qt::LeftButton and this->canvas->worldPosition.has_value())
		{
			const glm::vec3& pos = this->canvas->worldPosition.value();
			if (isCloseToExistingPoints(this->drawState.polygon, pos))
			{
				this->closeShape();
			}
			else
			{
				this->drawState.polygon.push_back(pos);
				updatePreviewPolygon(&this->drawState);
			}
			event->accept();
		}
		else if (true
			and event->button() == Qt::RightButton
			and this->drawState.polygon.size() > 0
		) {
			this->drawState.polygon.erase(this->drawState.polygon.end() - 1);
			updatePreviewPolygon(&this->drawState);
			event->accept();
		}
		break;
	}
}

void EditorTabWidget::canvasMouseMove(QMouseEvent *event)
{
	switch(this->drawState.mode)
	{
	case SelectMode:
		break;
	case DrawMode:
		if (this->canvas->worldPosition.has_value())
		{
			this->drawState.previewPoint = this->canvas->worldPosition.value();
			updatePreviewPolygon(&this->drawState);
			this->update();
		}
		event->accept();
		break;
	}
}
/*

void EditorTabWidget::select(const QSet<ModelId> &selected)
{
	QItemSelectionModel* selectionModel = this->ui.listView->selectionModel();
	QItemSelection itemSelection;
	for (const ModelId id : selected)
	{
		const std::optional<int> row = this->model->find(id);
		if (row.has_value())
		{
			const QModelIndex qindex = this->model->index(*row);
			itemSelection.select(qindex, qindex);
		}
	}
	selectionModel->select(itemSelection, QItemSelectionModel::ClearAndSelect);
}
*/
const QSet<ModelId> EditorTabWidget::selectedObjects() const
{
	return this->canvas->selectedObjects();
}

EditingMode EditorTabWidget::currentEditingMode() const
{
	return this->drawState.mode;
}

void EditorTabWidget::closeShape()
{
	if (this->drawState.polygon.size() >= 2 and this->drawState.polygon.size() <= 4)
	{
		switch (this->drawState.polygon.size())
		{
		case 2:
			Q_EMIT this->modelAction(AppendToModel{
				.newElement = Colored<LineSegment>{
					LineSegment{
						.p1 = this->drawState.polygon[0],
						.p2 = this->drawState.polygon[1],
					},
					EDGE_COLOR,
				}
			});
			break;
		case 3:
			Q_EMIT this->modelAction(AppendToModel{
				.newElement = Colored<Triangle>{
					Triangle{
						.p1 = this->drawState.polygon[0],
						.p2 = this->drawState.polygon[1],
						.p3 = this->drawState.polygon[2],
					},
					MAIN_COLOR,
				}
			});
			break;
		case 4:
			Q_EMIT this->modelAction(AppendToModel{
				.newElement = Colored<Quadrilateral>{
					Quadrilateral{
						.p1 = this->drawState.polygon[0],
						.p2 = this->drawState.polygon[1],
						.p3 = this->drawState.polygon[2],
						.p4 = this->drawState.polygon[3],
					},
					MAIN_COLOR,
				}
			});
			break;
		}
	}
	this->drawState.polygon.clear();
	updatePreviewPolygon(&this->drawState);
}

mercurial