Refactor, make selecting elements from the model select the corresponding line from the editor as well default tip

Thu, 15 Jun 2023 16:18:03 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Thu, 15 Jun 2023 16:18:03 +0300
changeset 383
530d23cd4e97
parent 382
94d5587bb0c4

Refactor, make selecting elements from the model select the corresponding line from the editor as well

CMakeLists.txt file | annotate | diff | comparison | revisions
src/layers/edittools.cpp file | annotate | diff | comparison | revisions
src/layers/edittools.h file | annotate | diff | comparison | revisions
src/main.cpp file | annotate | diff | comparison | revisions
src/model.h file | annotate | diff | comparison | revisions
src/openedmodel.cpp file | annotate | diff | comparison | revisions
src/openedmodel.h file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Sat Jun 10 17:26:32 2023 +0300
+++ b/CMakeLists.txt	Thu Jun 15 16:18:03 2023 +0300
@@ -76,6 +76,7 @@
 	src/mainwindow.cpp
 	src/messagelog.cpp
 	src/model.cpp
+	src/openedmodel.cpp
 	src/parser.cpp
 	src/polygoncache.cpp
 	src/triangulate.cpp
@@ -112,6 +113,7 @@
 	src/messagelog.h
 	src/model.h
 	src/modelsubwindow.h
+	src/openedmodel.h
 	src/parser.h
 	src/polygoncache.h
 	src/settings.h
--- a/src/layers/edittools.cpp	Sat Jun 10 17:26:32 2023 +0300
+++ b/src/layers/edittools.cpp	Thu Jun 15 16:18:03 2023 +0300
@@ -293,7 +293,7 @@
 	case editing_mode_e::select:
 		if (event->button() == Qt::LeftButton) {
 			const std::int32_t highlighted = this->renderer->pick(event->pos());
-			Q_EMIT this->select({highlighted}, false);
+			Q_EMIT this->modelAction(SelectInModel{static_cast<size_t>(highlighted)});
 		}
 		break;
 	case editing_mode_e::draw:
--- a/src/layers/edittools.h	Sat Jun 10 17:26:32 2023 +0300
+++ b/src/layers/edittools.h	Thu Jun 15 16:18:03 2023 +0300
@@ -62,7 +62,7 @@
 Q_SIGNALS:
 	void newStatusText(const QString& newStatusText);
 	void modelAction(const ModelAction& action);
-	void select(const QSet<std::int32_t>& ids, bool retain);
+	void select(std::int32_t linenumber);
 	void suggestCursor(const QCursor& cursor);
 protected:
 	void mvpMatrixChanged(const glm::mat4& matrix) override;
--- a/src/main.cpp	Sat Jun 10 17:26:32 2023 +0300
+++ b/src/main.cpp	Thu Jun 15 16:18:03 2023 +0300
@@ -23,23 +23,13 @@
 #include "src/parser.h"
 #include "src/ldrawsyntaxhighlighter.h"
 #include <GL/glew.h>
+#include "src/openedmodel.h"
 
 static const QDir LOCALE_DIR {":/locale"};
 
-class ModelData : public QObject
-{
-	Q_OBJECT
-public:
-	ModelData(QObject* parent) : QObject {parent} {}
-	std::unique_ptr<PartRenderer> canvas;
-	std::unique_ptr<EditTools> tools;
-	std::unique_ptr<AxesLayer> axesLayer;
-	std::unique_ptr<GridLayer> gridLayer;
-	std::unique_ptr<QTextCursor> textcursor;
-	QTextDocument* model;
-};
+#include <main.moc>
 
-#include <main.moc>
+using ModelData = EditableModel;
 
 static void doQtRegistrations()
 {
@@ -280,7 +270,7 @@
 	return subWindow;
 }
 
-static void executeAction(QTextDocument* model, const ModelAction& action)
+static void executeAction(QTextDocument* model, QPlainTextEdit* editor, const ModelAction& action)
 {
 	std::visit(overloaded{
 		[model](const AppendToModel& action){
@@ -303,6 +293,14 @@
 			}
 			//model->assignAt(action.position, action.newElement);
 		},
+		[editor](const SelectInModel& action){
+			QTextCursor cursor = editor->textCursor();
+			cursor.movePosition(QTextCursor::Start);
+			cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, static_cast<signed>(action.position));
+			cursor.select(QTextCursor::LineUnderCursor);
+			editor->setTextCursor(cursor);
+			editor->update();
+		},
 	}, action);
 }
 
@@ -325,54 +323,21 @@
 {
 	QTextDocument* model = state->documents.getModelById(modelId);
 	if (model != nullptr) {
-		ModelData* data = new ModelData(&state->documents);
-		data->tools = std::make_unique<EditTools>();
-		data->canvas = std::make_unique<PartRenderer>(model, &state->documents, state->colorTable);
-		data->axesLayer = std::make_unique<AxesLayer>();
-		data->gridLayer = std::make_unique<GridLayer>();
-		data->gridLayer->setGridMatrix(DEFAULT_GRID_MATRIX);
-		data->tools->setGridMatrix(DEFAULT_GRID_MATRIX);
-		data->model = model;
-		data->canvas->addRenderLayer(data->axesLayer.get());
-		data->canvas->setLayerEnabled(data->axesLayer.get(), setting<Setting::DrawAxes>());
-		data->canvas->addRenderLayer(data->gridLayer.get());
-		data->canvas->addRenderLayer(data->tools.get());
-		new LDrawSyntaxHighlighter{model};
-		data->textcursor = std::make_unique<QTextCursor>(model);
+		ModelData* data = new ModelData(model, &state->documents, &state->colorTable);
 		state->documents.setModelPayload(modelId, data);
 		QObject::connect(
-			data->tools.get(),
-			&EditTools::modelAction,
-			std::bind(executeAction, model, std::placeholders::_1));
+			data,
+			&EditableModel::modelAction,
+			std::bind(executeAction, model, state->mainWindow.modelEdit, std::placeholders::_1));
 		data->canvas->render_preferences = &state->renderPreferences;
 		data->canvas->build_preferences = &state->user_gl_build_preferences;
 		QObject::connect(
-			data->tools.get(),
-			&EditTools::newStatusText,
+			data,
+			&EditableModel::newStatusText,
 			[&state](const QString& newStatusText) {
 				state->mainWindow.statusBar()->showMessage(newStatusText);
 			});
 #if 0
-		QObject::connect(
-			data->tools.get(),
-			&EditTools::select,
-			[modelId, &documents](const QSet<ElementId>& indices, bool retain) {
-				ModelData* data = findModelData(&documents, modelId);
-				if (data != nullptr) {
-					if (not retain) {
-						data->textcursor->clearSelection();
-					}
-					for (const ElementId id : indices) {
-						opt<int> index = data->model->find(id);
-						if (index.has_value()) {
-							const QModelIndex qindex = data->model->index(*index);
-							data->itemSelectionModel->select(qindex, QItemSelectionModel::Select);
-						}
-					}
-				}
-			});
-#endif
-#if 0
 		QObject::connect(this, &Main::settingsChanged, [modelId, this]{
 			ModelData* data = findModelData(&state.documents, modelId);
 			if (data != nullptr) {
@@ -691,7 +656,7 @@
 			if (ModelData* data = currentModelData(&state.mainWindow, &state.documents)) {
 				QTextDocument* const model = data->model;
 				for (const ModelAction& action : ldraw::makeUnofficial(model)) {
-					executeAction(model, action);
+					executeAction(model, state.mainWindow.modelEdit, action);
 				}
 			}
 		});
--- a/src/model.h	Sat Jun 10 17:26:32 2023 +0300
+++ b/src/model.h	Thu Jun 15 16:18:03 2023 +0300
@@ -203,9 +203,15 @@
 	ModelElement newElement;
 };
 
+struct SelectInModel
+{
+	std::size_t position;
+};
+
 using ModelAction = std::variant<
 	AppendToModel,
 	DeleteFromModel,
-	ModifyModel>;
+	ModifyModel,
+	SelectInModel>;
 
 QString modelElementToString(const ModelElement &element);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/openedmodel.cpp	Thu Jun 15 16:18:03 2023 +0300
@@ -0,0 +1,35 @@
+#include "openedmodel.h"
+#include "settings.h"
+#include "ldrawsyntaxhighlighter.h"
+
+EditableModel::EditableModel(QTextDocument* model, DocumentManager* documents, ColorTable* colorTable)
+	: QObject{documents}, model{model}
+{
+	this->tools = std::make_unique<EditTools>();
+	this->canvas = std::make_unique<PartRenderer>(model, documents, *colorTable);
+	this->axesLayer = std::make_unique<AxesLayer>();
+	this->gridLayer = std::make_unique<GridLayer>();
+	this->gridLayer->setGridMatrix(DEFAULT_GRID_MATRIX);
+	this->tools->setGridMatrix(DEFAULT_GRID_MATRIX);
+	this->canvas->addRenderLayer(this->axesLayer.get());
+	this->canvas->setLayerEnabled(this->axesLayer.get(), setting<Setting::DrawAxes>());
+	this->canvas->addRenderLayer(this->gridLayer.get());
+	this->canvas->addRenderLayer(this->tools.get());
+	new LDrawSyntaxHighlighter{model};
+	this->textcursor = std::make_unique<QTextCursor>(model);
+	QObject::connect(
+		this->tools.get(),
+		&EditTools::modelAction,
+		this,
+		&EditableModel::modelAction);
+	QObject::connect(
+		this->tools.get(),
+		&EditTools::newStatusText,
+		this,
+		&EditableModel::newStatusText);
+	QObject::connect(
+		this->tools.get(),
+		&EditTools::select,
+		this,
+		&EditableModel::select);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/openedmodel.h	Thu Jun 15 16:18:03 2023 +0300
@@ -0,0 +1,29 @@
+#ifndef OPENEDMODEL_H
+#define OPENEDMODEL_H
+
+#include <QObject>
+#include "basics.h"
+#include "gl/partrenderer.h"
+#include "layers/edittools.h"
+#include "layers/axeslayer.h"
+#include "layers/gridlayer.h"
+
+class EditableModel : public QObject
+{
+	Q_OBJECT
+public:
+	explicit EditableModel(QTextDocument* model, DocumentManager* documents, ColorTable* colorTable);
+	std::unique_ptr<PartRenderer> canvas;
+	std::unique_ptr<EditTools> tools;
+	std::unique_ptr<AxesLayer> axesLayer;
+	std::unique_ptr<GridLayer> gridLayer;
+	std::unique_ptr<QTextCursor> textcursor;
+	QTextDocument* const model;
+	
+Q_SIGNALS:
+	void modelAction(const ModelAction& action);
+	void select(std::int32_t linenumber);
+	void newStatusText(const QString& newStatusText);
+};
+
+#endif // OPENEDMODEL_H

mercurial