src/document.cpp

Wed, 25 May 2022 18:29:49 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 18:29:49 +0300
changeset 197
0e729e681a2c
parent 191
d355d4c52d51
child 198
eb9d900dc79a
permissions
-rw-r--r--

move drawState to Document

/*
 *  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 "document.h"
#include "ui_document.h"
#include "model.h"
#include "modeleditor.h"
#include "ui/objecteditor.h"
#include "linetypes/edge.h"
#include "linetypes/triangle.h"
#include "linetypes/quadrilateral.h"

Document::Document(
	Model* model,
	DocumentManager* documents,
	const ldraw::ColorTable& colorTable,
	QWidget* parent) :
	QWidget{parent},
	colorTable{colorTable},
	canvas{new Canvas{model, this, documents, colorTable, this}},
	model{model},
	documents{documents},
	vertexMap{model},
	ui{*new Ui_Document},
	toolsBar{new QToolBar{this}},
	objectEditor{new ObjectEditor{this}}
{
	this->ui.setupUi(this);
	const int listWidth = static_cast<int>(this->width() / 3);
	this->ui.viewportListSplitter->setSizes({listWidth * 2, listWidth});
	this->ui.toolsBarContainer->setLayout(new QVBoxLayout{this->ui.toolsBarContainer});
	this->ui.toolsBarContainer->layout()->addWidget(this->toolsBar);
	this->ui.listView->setModel(model);
	this->ui.viewportFrame->setLayout(new QVBoxLayout{this->ui.listView});
	this->ui.viewportFrame->layout()->addWidget(this->canvas);
	this->toolsBar->setOrientation(Qt::Vertical);
	this->setMouseTracking(true);
	connect(this->ui.viewportListSplitter, &QSplitter::splitterMoved, this, &Document::splitterChanged);
	connect(this->canvas, &Canvas::mouseClick, this, &Document::canvasMouseClick);
	connect(this->canvas, &Canvas::mouseMove, this, &Document::canvasMouseMove);
	connect(this->canvas, &Canvas::newStatusText, this, &Document::newStatusText);
	connect(this->canvas, &Canvas::selectionChanged, [&](const QSet<ldraw::id_t>& newSelection)
	{
		QItemSelectionModel* selectionModel = this->ui.listView->selectionModel();
		QItemSelection selection;
		for (const ldraw::id_t id : newSelection)
		{
			QModelIndex index = this->model->find(id);
			if (index != QModelIndex{})
			{
				selection.select(index, index);
			}
		}
		QSignalBlocker blocker{this};
		selectionModel->select(selection, QItemSelectionModel::ClearAndSelect);
	});
	connect(this->ui.listView->selectionModel(), &QItemSelectionModel::selectionChanged,
		[&](const QItemSelection& selected, const QItemSelection& deselected)
	{
		auto resolveIndex = [this](const QModelIndex& index){ return (*this->model)[index.row()]->id; };
		auto resolve = [resolveIndex](const QItemSelection& selection)
		{
			return fn::map<QSet<ldraw::id_t>>(selection.indexes(), resolveIndex);
		};
		this->canvas->handleSelectionChange(resolve(selected), resolve(deselected));
	});
	connect(this->model, &Model::dataChanged, this->canvas, qOverload<>(&Canvas::update));
	connect(&this->vertexMap, &VertexMap::verticesChanged, [&]()
	{
		this->canvas->rebuildVertices(this);
	});
	this->initializeTools();
}

Document::~Document()
{
	delete &this->ui;
}

QByteArray Document::saveSplitterState() const
{
	return this->ui.viewportListSplitter->saveState();
}

void Document::restoreSplitterState(const QByteArray& state)
{
	this->ui.viewportListSplitter->restoreState(state);
}

std::unique_ptr<ModelEditor> Document::editModel()
{
	std::unique_ptr<ModelEditor> editorPointer = std::make_unique<ModelEditor>(*this->model);
	connect(editorPointer.get(), &ModelEditor::objectModified, [&](int position){
		this->model->emitDataChangedSignal(position);
	});
	return editorPointer;
}

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

const char INDEX_PROPERTY[] = "_editing_mode_index";

void Document::initializeTools()
{
	const struct
	{
		QString name, tooltip;
		QPixmap icon;
		QWidget* widget;
	} editingModesInfo[] = {
		{
			.name = tr("Select"),
			.tooltip = tr("Select elements from the model."),
			.icon = {":/icons/navigate-outline.png"},
			.widget = this->objectEditor,
		},
		{
			.name = tr("Draw"),
			.tooltip = tr("Draw new elements into the model."),
			.icon = {":/icons/pencil-outline.png"},
			.widget = nullptr,
		},
	};
	for (int i = 0; i < countof(editingModesInfo); ++i) {
		const auto& editingModeInfo = editingModesInfo[i];
		QAction* action = new QAction{editingModeInfo.name, this};
		action->setCheckable(true);
		action->setChecked(i == 0);
		action->setToolTip(editingModeInfo.tooltip);
		action->setIcon(QPixmap{editingModeInfo.icon});
		action->setProperty(INDEX_PROPERTY, i);
		this->toolsBar->addAction(action);
		QWidget* widget = editingModeInfo.widget;
		if (widget == nullptr) {
			widget = new QWidget{this};
		}
		this->ui.toolWidgetStack->addWidget(widget);
		this->toolActions.push_back(action);
		connect(action, &QAction::triggered, this, &Document::editingModeTriggered);
	}
	this->ui.listView->selectAll();
}

void Document::editingModeTriggered()
{
	QAction* triggeredAction = qobject_cast<QAction*>(this->sender());
	if (triggeredAction != nullptr)
	{
		const int index = triggeredAction->property(INDEX_PROPERTY).toInt();
		this->mode = static_cast<EditingMode>(index);
		this->ui.toolWidgetStack->setCurrentIndex(index);
		for (QAction* action : this->toolActions) {
			action->setChecked(action == triggeredAction);
		}
	}
}

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 geom::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(geom::isclose, std::placeholders::_1, pos));
}

void Document::canvasMouseClick(QMouseEvent *event)
{
	switch(this->mode)
	{
	case SelectMode:
		if (event->button() == Qt::LeftButton)
		{
			const ldraw::id_t highlighted = this->canvas->getHighlightedObject();
			QSet<ldraw::id_t> selected;
			if (highlighted != ldraw::NULL_ID) {
				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 Document::canvasMouseMove(QMouseEvent *event)
{
	switch(this->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 Document::select(const QSet<ldraw::id_t> &selected)
{
	QItemSelectionModel* selectionModel = this->ui.listView->selectionModel();
	QItemSelection itemSelection;
	for (const ldraw::id_t id : selected)
	{
		QModelIndex index = this->model->find(id);
		if (index != QModelIndex{})
		{
			itemSelection.select(index, index);
		}
	}
	selectionModel->select(itemSelection, QItemSelectionModel::ClearAndSelect);
}

const Model &Document::getModel() const
{
	return *this->model;
}

const QSet<ldraw::id_t> Document::selectedObjects() const
{
	return this->canvas->selectedObjects();
}

void Document::closeShape()
{
	if (this->drawState.polygon.size() >= 2 and this->drawState.polygon.size() <= 4)
	{
		std::unique_ptr<ModelEditor> modelEditor = this->editModel();
		switch (this->drawState.polygon.size())
		{
		case 2:
			modelEditor->append<ldraw::Edge>(
				vectorToArray<2>(this->drawState.polygon),
				ldraw::EDGE_COLOR);
			break;
		case 3:
			modelEditor->append<ldraw::Triangle>(
				vectorToArray<3>(this->drawState.polygon),
				ldraw::MAIN_COLOR);
			break;
		case 4:
			modelEditor->append<ldraw::Quadrilateral>(
				vectorToArray<4>(this->drawState.polygon),
				ldraw::MAIN_COLOR);
			break;
		}
	}
	this->drawState.polygon.clear();
	updatePreviewPolygon(&this->drawState);
}

mercurial