src/document.cpp

changeset 200
ca23936b455b
parent 198
eb9d900dc79a
child 201
5d201ee4a9c3
--- a/src/document.cpp	Wed May 25 20:36:34 2022 +0300
+++ b/src/document.cpp	Mon Jun 06 22:01:22 2022 +0300
@@ -21,13 +21,9 @@
 #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(
+EditorTabWidget::EditorTabWidget(
 	Model* model,
 	DocumentManager* documents,
 	const ldraw::ColorTable& colorTable,
@@ -39,8 +35,7 @@
 	documents{documents},
 	vertexMap{model},
 	ui{*new Ui_Document},
-	toolsBar{new QToolBar{this}},
-	objectEditor{new ObjectEditor{this}}
+	toolsBar{new QToolBar{this}}
 {
 	this->ui.setupUi(this);
 	const int listWidth = static_cast<int>(this->width() / 3);
@@ -52,17 +47,17 @@
 	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->ui.viewportListSplitter, &QSplitter::splitterMoved, this, &EditorTabWidget::splitterChanged);
+	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->ui.listView->selectionModel(), &QItemSelectionModel::selectionChanged,
 		[&](const QItemSelection& selected, const QItemSelection& deselected)
 	{
-		auto resolveIndex = [this](const QModelIndex& index){ return (*this->model)[index.row()]->id; };
+		auto resolveIndex = [this](const QModelIndex& index){ return this->model->idAt(index.row()); };
 		auto resolve = [resolveIndex](const QItemSelection& selection)
 		{
-			return fn::map<QSet<ldraw::id_t>>(selection.indexes(), resolveIndex);
+			return fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex);
 		};
 		this->canvas->handleSelectionChange(resolve(selected), resolve(deselected));
 	});
@@ -75,38 +70,29 @@
 	this->initializeTools();
 }
 
-Document::~Document()
+EditorTabWidget::~EditorTabWidget()
 {
 	delete &this->ui;
 }
 
-QByteArray Document::saveSplitterState() const
+QByteArray EditorTabWidget::saveSplitterState() const
 {
 	return this->ui.viewportListSplitter->saveState();
 }
 
-void Document::restoreSplitterState(const QByteArray& state)
+void EditorTabWidget::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
+void EditorTabWidget::applyToVertices(VertexMap::ApplyFunction fn) const
 {
 	this->vertexMap.apply(fn);
 }
 
 const char INDEX_PROPERTY[] = "_editing_mode_index";
 
-void Document::initializeTools()
+void EditorTabWidget::initializeTools()
 {
 	const struct
 	{
@@ -118,7 +104,7 @@
 			.name = tr("Select"),
 			.tooltip = tr("Select elements from the model."),
 			.icon = {":/icons/navigate-outline.png"},
-			.widget = this->objectEditor,
+			//.widget = this->objectEditor,
 		},
 		{
 			.name = tr("Draw"),
@@ -142,12 +128,12 @@
 		}
 		this->ui.toolWidgetStack->addWidget(widget);
 		this->toolActions.push_back(action);
-		connect(action, &QAction::triggered, this, &Document::editingModeTriggered);
+		connect(action, &QAction::triggered, this, &EditorTabWidget::editingModeTriggered);
 	}
 	this->ui.listView->selectAll();
 }
 
-void Document::editingModeTriggered()
+void EditorTabWidget::editingModeTriggered()
 {
 	QAction* triggeredAction = qobject_cast<QAction*>(this->sender());
 	if (triggeredAction != nullptr)
@@ -186,16 +172,16 @@
 	return any(points, std::bind(geom::isclose, std::placeholders::_1, pos));
 }
 
-void Document::canvasMouseClick(QMouseEvent *event)
+void EditorTabWidget::canvasMouseClick(QMouseEvent *event)
 {
 	switch(this->drawState.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) {
+			const ModelId highlighted = this->canvas->getHighlightedObject();
+			QSet<ModelId> selected;
+			if (highlighted != ModelId{0}) {
 				selected.insert(highlighted);
 			}
 			this->select(selected);
@@ -229,7 +215,7 @@
 	}
 }
 
-void Document::canvasMouseMove(QMouseEvent *event)
+void EditorTabWidget::canvasMouseMove(QMouseEvent *event)
 {
 	switch(this->drawState.mode)
 	{
@@ -247,52 +233,59 @@
 	}
 }
 
-void Document::select(const QSet<ldraw::id_t> &selected)
+void EditorTabWidget::select(const QSet<ModelId> &selected)
 {
 	QItemSelectionModel* selectionModel = this->ui.listView->selectionModel();
 	QItemSelection itemSelection;
-	for (const ldraw::id_t id : selected)
+	for (const ModelId id : selected)
 	{
-		QModelIndex index = this->model->find(id);
-		if (index != QModelIndex{})
+		const std::optional<int> row = this->model->find(id);
+		if (row.has_value())
 		{
-			itemSelection.select(index, index);
+			const QModelIndex qindex = this->model->index(*row);
+			itemSelection.select(qindex, qindex);
 		}
 	}
 	selectionModel->select(itemSelection, QItemSelectionModel::ClearAndSelect);
 }
 
-const Model &Document::getModel() const
-{
-	return *this->model;
-}
-
-const QSet<ldraw::id_t> Document::selectedObjects() const
+const QSet<ModelId> EditorTabWidget::selectedObjects() const
 {
 	return this->canvas->selectedObjects();
 }
 
-void Document::closeShape()
+void EditorTabWidget::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);
+			this->model->append(Colored<LineSegment>{
+				LineSegment{
+					.p1 = this->drawState.polygon[0],
+					.p2 = this->drawState.polygon[1],
+				},
+				ldraw::EDGE_COLOR});
 			break;
 		case 3:
-			modelEditor->append<ldraw::Triangle>(
-				vectorToArray<3>(this->drawState.polygon),
-				ldraw::MAIN_COLOR);
+			this->model->append(Colored<Triangle>{
+				Triangle{
+					.p1 = this->drawState.polygon[0],
+					.p2 = this->drawState.polygon[1],
+					.p3 = this->drawState.polygon[2],
+				},
+				ldraw::MAIN_COLOR});
 			break;
 		case 4:
-			modelEditor->append<ldraw::Quadrilateral>(
-				vectorToArray<4>(this->drawState.polygon),
-				ldraw::MAIN_COLOR);
+			this->model->append(Colored<Quadrilateral>{
+				Quadrilateral{
+					.p1 = this->drawState.polygon[0],
+					.p2 = this->drawState.polygon[1],
+					.p3 = this->drawState.polygon[2],
+					.p4 = this->drawState.polygon[3],
+				},
+				ldraw::MAIN_COLOR});
 			break;
 		}
 	}

mercurial