--- 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); } } });