# HG changeset patch # User Teemu Piippo # Date 1686835083 -10800 # Node ID 530d23cd4e97f7f6273ef697e12d14a3ae603b6a # Parent 94d5587bb0c4573702c4855cc686c37d807098bd Refactor, make selecting elements from the model select the corresponding line from the editor as well diff -r 94d5587bb0c4 -r 530d23cd4e97 CMakeLists.txt --- 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 diff -r 94d5587bb0c4 -r 530d23cd4e97 src/layers/edittools.cpp --- 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(highlighted)}); } break; case editing_mode_e::draw: diff -r 94d5587bb0c4 -r 530d23cd4e97 src/layers/edittools.h --- 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& ids, bool retain); + void select(std::int32_t linenumber); void suggestCursor(const QCursor& cursor); protected: void mvpMatrixChanged(const glm::mat4& matrix) override; diff -r 94d5587bb0c4 -r 530d23cd4e97 src/main.cpp --- 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 +#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 canvas; - std::unique_ptr tools; - std::unique_ptr axesLayer; - std::unique_ptr gridLayer; - std::unique_ptr textcursor; - QTextDocument* model; -}; +#include -#include +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(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(); - data->canvas = std::make_unique(model, &state->documents, state->colorTable); - data->axesLayer = std::make_unique(); - data->gridLayer = std::make_unique(); - 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()); - data->canvas->addRenderLayer(data->gridLayer.get()); - data->canvas->addRenderLayer(data->tools.get()); - new LDrawSyntaxHighlighter{model}; - data->textcursor = std::make_unique(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& indices, bool retain) { - ModelData* data = findModelData(&documents, modelId); - if (data != nullptr) { - if (not retain) { - data->textcursor->clearSelection(); - } - for (const ElementId id : indices) { - opt 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); } } }); diff -r 94d5587bb0c4 -r 530d23cd4e97 src/model.h --- 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); diff -r 94d5587bb0c4 -r 530d23cd4e97 src/openedmodel.cpp --- /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(); + this->canvas = std::make_unique(model, documents, *colorTable); + this->axesLayer = std::make_unique(); + this->gridLayer = std::make_unique(); + 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()); + this->canvas->addRenderLayer(this->gridLayer.get()); + this->canvas->addRenderLayer(this->tools.get()); + new LDrawSyntaxHighlighter{model}; + this->textcursor = std::make_unique(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); +} diff -r 94d5587bb0c4 -r 530d23cd4e97 src/openedmodel.h --- /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 +#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 canvas; + std::unique_ptr tools; + std::unique_ptr axesLayer; + std::unique_ptr gridLayer; + std::unique_ptr 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