21 #include "src/version.h" |
21 #include "src/version.h" |
22 #include "src/widgets/colorselectdialog.h" |
22 #include "src/widgets/colorselectdialog.h" |
23 #include "src/parser.h" |
23 #include "src/parser.h" |
24 #include "src/ldrawsyntaxhighlighter.h" |
24 #include "src/ldrawsyntaxhighlighter.h" |
25 #include <GL/glew.h> |
25 #include <GL/glew.h> |
|
26 #include "src/openedmodel.h" |
26 |
27 |
27 static const QDir LOCALE_DIR {":/locale"}; |
28 static const QDir LOCALE_DIR {":/locale"}; |
28 |
29 |
29 class ModelData : public QObject |
|
30 { |
|
31 Q_OBJECT |
|
32 public: |
|
33 ModelData(QObject* parent) : QObject {parent} {} |
|
34 std::unique_ptr<PartRenderer> canvas; |
|
35 std::unique_ptr<EditTools> tools; |
|
36 std::unique_ptr<AxesLayer> axesLayer; |
|
37 std::unique_ptr<GridLayer> gridLayer; |
|
38 std::unique_ptr<QTextCursor> textcursor; |
|
39 QTextDocument* model; |
|
40 }; |
|
41 |
|
42 #include <main.moc> |
30 #include <main.moc> |
|
31 |
|
32 using ModelData = EditableModel; |
43 |
33 |
44 static void doQtRegistrations() |
34 static void doQtRegistrations() |
45 { |
35 { |
46 QCoreApplication::setApplicationName(QStringLiteral(CMAKE_PROJECT_NAME)); |
36 QCoreApplication::setApplicationName(QStringLiteral(CMAKE_PROJECT_NAME)); |
47 QCoreApplication::setOrganizationName("hecknology.net"); |
37 QCoreApplication::setOrganizationName("hecknology.net"); |
278 SubWindow* subWindow = new SubWindow{args..., nullptr}; |
268 SubWindow* subWindow = new SubWindow{args..., nullptr}; |
279 mdiArea->addSubWindow(subWindow); |
269 mdiArea->addSubWindow(subWindow); |
280 return subWindow; |
270 return subWindow; |
281 } |
271 } |
282 |
272 |
283 static void executeAction(QTextDocument* model, const ModelAction& action) |
273 static void executeAction(QTextDocument* model, QPlainTextEdit* editor, const ModelAction& action) |
284 { |
274 { |
285 std::visit(overloaded{ |
275 std::visit(overloaded{ |
286 [model](const AppendToModel& action){ |
276 [model](const AppendToModel& action){ |
287 QTextCursor cursor{model}; |
277 QTextCursor cursor{model}; |
288 cursor.movePosition(QTextCursor::End); |
278 cursor.movePosition(QTextCursor::End); |
301 cursor.select(QTextCursor::LineUnderCursor); |
291 cursor.select(QTextCursor::LineUnderCursor); |
302 cursor.insertText(modelElementToString(action.newElement)); |
292 cursor.insertText(modelElementToString(action.newElement)); |
303 } |
293 } |
304 //model->assignAt(action.position, action.newElement); |
294 //model->assignAt(action.position, action.newElement); |
305 }, |
295 }, |
|
296 [editor](const SelectInModel& action){ |
|
297 QTextCursor cursor = editor->textCursor(); |
|
298 cursor.movePosition(QTextCursor::Start); |
|
299 cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, static_cast<signed>(action.position)); |
|
300 cursor.select(QTextCursor::LineUnderCursor); |
|
301 editor->setTextCursor(cursor); |
|
302 editor->update(); |
|
303 }, |
306 }, action); |
304 }, action); |
307 } |
305 } |
308 |
306 |
309 QFont codeEditorFontFromSettings() |
307 QFont codeEditorFontFromSettings() |
310 { |
308 { |
323 |
321 |
324 static void openModelForEditing(MainState* state, const ModelId modelId) |
322 static void openModelForEditing(MainState* state, const ModelId modelId) |
325 { |
323 { |
326 QTextDocument* model = state->documents.getModelById(modelId); |
324 QTextDocument* model = state->documents.getModelById(modelId); |
327 if (model != nullptr) { |
325 if (model != nullptr) { |
328 ModelData* data = new ModelData(&state->documents); |
326 ModelData* data = new ModelData(model, &state->documents, &state->colorTable); |
329 data->tools = std::make_unique<EditTools>(); |
|
330 data->canvas = std::make_unique<PartRenderer>(model, &state->documents, state->colorTable); |
|
331 data->axesLayer = std::make_unique<AxesLayer>(); |
|
332 data->gridLayer = std::make_unique<GridLayer>(); |
|
333 data->gridLayer->setGridMatrix(DEFAULT_GRID_MATRIX); |
|
334 data->tools->setGridMatrix(DEFAULT_GRID_MATRIX); |
|
335 data->model = model; |
|
336 data->canvas->addRenderLayer(data->axesLayer.get()); |
|
337 data->canvas->setLayerEnabled(data->axesLayer.get(), setting<Setting::DrawAxes>()); |
|
338 data->canvas->addRenderLayer(data->gridLayer.get()); |
|
339 data->canvas->addRenderLayer(data->tools.get()); |
|
340 new LDrawSyntaxHighlighter{model}; |
|
341 data->textcursor = std::make_unique<QTextCursor>(model); |
|
342 state->documents.setModelPayload(modelId, data); |
327 state->documents.setModelPayload(modelId, data); |
343 QObject::connect( |
328 QObject::connect( |
344 data->tools.get(), |
329 data, |
345 &EditTools::modelAction, |
330 &EditableModel::modelAction, |
346 std::bind(executeAction, model, std::placeholders::_1)); |
331 std::bind(executeAction, model, state->mainWindow.modelEdit, std::placeholders::_1)); |
347 data->canvas->render_preferences = &state->renderPreferences; |
332 data->canvas->render_preferences = &state->renderPreferences; |
348 data->canvas->build_preferences = &state->user_gl_build_preferences; |
333 data->canvas->build_preferences = &state->user_gl_build_preferences; |
349 QObject::connect( |
334 QObject::connect( |
350 data->tools.get(), |
335 data, |
351 &EditTools::newStatusText, |
336 &EditableModel::newStatusText, |
352 [&state](const QString& newStatusText) { |
337 [&state](const QString& newStatusText) { |
353 state->mainWindow.statusBar()->showMessage(newStatusText); |
338 state->mainWindow.statusBar()->showMessage(newStatusText); |
354 }); |
339 }); |
355 #if 0 |
|
356 QObject::connect( |
|
357 data->tools.get(), |
|
358 &EditTools::select, |
|
359 [modelId, &documents](const QSet<ElementId>& indices, bool retain) { |
|
360 ModelData* data = findModelData(&documents, modelId); |
|
361 if (data != nullptr) { |
|
362 if (not retain) { |
|
363 data->textcursor->clearSelection(); |
|
364 } |
|
365 for (const ElementId id : indices) { |
|
366 opt<int> index = data->model->find(id); |
|
367 if (index.has_value()) { |
|
368 const QModelIndex qindex = data->model->index(*index); |
|
369 data->itemSelectionModel->select(qindex, QItemSelectionModel::Select); |
|
370 } |
|
371 } |
|
372 } |
|
373 }); |
|
374 #endif |
|
375 #if 0 |
340 #if 0 |
376 QObject::connect(this, &Main::settingsChanged, [modelId, this]{ |
341 QObject::connect(this, &Main::settingsChanged, [modelId, this]{ |
377 ModelData* data = findModelData(&state.documents, modelId); |
342 ModelData* data = findModelData(&state.documents, modelId); |
378 if (data != nullptr) { |
343 if (data != nullptr) { |
379 data->gridLayer->settingsChanged(); |
344 data->gridLayer->settingsChanged(); |
689 &QAction::triggered, |
654 &QAction::triggered, |
690 [&state]{ |
655 [&state]{ |
691 if (ModelData* data = currentModelData(&state.mainWindow, &state.documents)) { |
656 if (ModelData* data = currentModelData(&state.mainWindow, &state.documents)) { |
692 QTextDocument* const model = data->model; |
657 QTextDocument* const model = data->model; |
693 for (const ModelAction& action : ldraw::makeUnofficial(model)) { |
658 for (const ModelAction& action : ldraw::makeUnofficial(model)) { |
694 executeAction(model, action); |
659 executeAction(model, state.mainWindow.modelEdit, action); |
695 } |
660 } |
696 } |
661 } |
697 }); |
662 }); |
698 QObject::connect(state.mainWindow.actionAboutQt, &QAction::triggered, &QApplication::aboutQt); |
663 QObject::connect(state.mainWindow.actionAboutQt, &QAction::triggered, &QApplication::aboutQt); |
699 QObject::connect( |
664 QObject::connect( |