src/document.cpp

Sun, 12 Jun 2022 20:47:04 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 12 Jun 2022 20:47:04 +0300
changeset 214
8e1fe64ce4e3
parent 205
1a4342d80de7
child 217
6d95c1a41e6e
permissions
-rw-r--r--

begin refactor of gl side

/*
 *  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"

EditTools::EditTools(
	Model* model,
	const ColorTable& colorTable,
	QObject* parent) :
	QObject{parent},
	colorTable{colorTable},
	model{model},
	vertexMap{model}
{
#if 0
	connect(this->canvas, &Canvas::mouseClick, this, &EditTools::canvasMouseClick);
	connect(this->canvas, &Canvas::mouseMove, this, &EditTools::canvasMouseMove);
	connect(this->canvas, &Canvas::newStatusText, this, &EditTools::newStatusText);
	connect(this->model, &Model::dataChanged, this->canvas, qOverload<>(&Canvas::update));
	connect(&this->vertexMap, &VertexMap::verticesChanged, [&]()
	{
		this->canvas->rebuildVertices(&this->vertexMap);
	});
	this->canvas->drawState = &this->drawState;
#endif
}

EditTools::~EditTools()
{
}

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

void EditTools::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 EditTools::canvasMouseClick(QMouseEvent*)
{
#if 0
	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) {
			if (isCloseToExistingPoints(this->drawState.polygon, worldPosition)) {
				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;
	}
#endif
}
void EditTools::canvasMouseMove(QMouseEvent*)
{
#if 0
	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;
	}
#endif
}
#if 0
/*

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> EditTools::selectedObjects() const
{
	return this->canvas->selectedObjects();
}
#endif
EditingMode EditTools::currentEditingMode() const
{
	return this->drawState.mode;
}
#if 0
void EditTools::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);
}

#endif

mercurial