393 }; |
394 }; |
394 const auto openModelForEditing = [&](const ModelId modelId){ |
395 const auto openModelForEditing = [&](const ModelId modelId){ |
395 Model* model = documents.getModelById(modelId); |
396 Model* model = documents.getModelById(modelId); |
396 if (model != nullptr) { |
397 if (model != nullptr) { |
397 ModelData* data = new ModelData(&documents); |
398 ModelData* data = new ModelData(&documents); |
398 data->tools = std::make_unique<EditTools>(model, colorTable); |
399 data->tools = std::make_unique<EditTools>(); |
399 data->canvas = std::make_unique<PartRenderer>(model, &documents, colorTable); |
400 data->canvas = std::make_unique<PartRenderer>(model, &documents, colorTable); |
400 data->itemSelectionModel = std::make_unique<QItemSelectionModel>(model); |
401 data->itemSelectionModel = std::make_unique<QItemSelectionModel>(); |
|
402 data->itemSelectionModel->setModel(model); |
401 data->axesLayer = std::make_unique<AxesLayer>(); |
403 data->axesLayer = std::make_unique<AxesLayer>(); |
|
404 constexpr glm::mat4 XZ = {{1, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 1}}; |
402 data->gridLayer = std::make_unique<GridLayer>(); |
405 data->gridLayer = std::make_unique<GridLayer>(); |
|
406 data->gridLayer->setGridMatrix(XZ); |
|
407 data->tools->setGridMatrix(XZ); |
403 data->model = model; |
408 data->model = model; |
404 data->canvas->addRenderLayer(data->axesLayer.get()); |
409 data->canvas->addRenderLayer(data->axesLayer.get()); |
405 data->canvas->setLayerEnabled(data->axesLayer.get(), settings.drawAxes()); |
410 data->canvas->setLayerEnabled(data->axesLayer.get(), settings.drawAxes()); |
406 data->canvas->addRenderLayer(data->gridLayer.get()); |
411 data->canvas->addRenderLayer(data->gridLayer.get()); |
|
412 data->canvas->addRenderLayer(data->tools.get()); |
407 documents.setModelPayload(modelId, data); |
413 documents.setModelPayload(modelId, data); |
408 QObject::connect( |
414 QObject::connect( |
409 data->tools.get(), |
415 data->tools.get(), |
410 &EditTools::modelAction, |
416 &EditTools::modelAction, |
411 std::bind(executeAction, model, std::placeholders::_1)); |
417 std::bind(executeAction, model, std::placeholders::_1)); |
412 #if 0 |
418 QObject::connect( |
413 QObject::connect(data->itemSelectionModel.get(), &QItemSelectionModel::selectionChanged, |
419 data->itemSelectionModel.get(), |
414 [modelId, &documents]( |
420 &QItemSelectionModel::selectionChanged, |
415 const QItemSelection& selected, |
421 [modelId, &documents]{ |
416 const QItemSelection& deselected) |
|
417 { |
|
418 ModelData* data = findModelData(&documents, modelId); |
422 ModelData* data = findModelData(&documents, modelId); |
419 if (data != nullptr) { |
423 if (data != nullptr) { |
420 auto resolveIndex = [&data](const QModelIndex& index){ |
424 auto resolveIndex = [&data](const QModelIndex& index){ |
421 return data->model->idAt(index.row()); |
425 return data->model->idAt(index.row()); |
422 }; |
426 }; |
423 auto resolve = [&resolveIndex](const QItemSelection& selection) |
427 const auto selection = data->itemSelectionModel->selection(); |
424 { |
428 const auto indices = fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex); |
425 return fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex); |
429 data->canvas->setSelection(indices); |
426 }; |
|
427 data->canvas->handleSelectionChange(resolve(selected), resolve(deselected)); |
|
428 } |
430 } |
429 }); |
431 }); |
430 #endif |
|
431 data->canvas->setRenderPreferences(renderPreferences); |
432 data->canvas->setRenderPreferences(renderPreferences); |
432 QObject::connect( |
433 QObject::connect( |
433 data->tools.get(), |
434 data->tools.get(), |
434 &EditTools::newStatusText, |
435 &EditTools::newStatusText, |
435 [&](const QString& newStatusText) { |
436 [&](const QString& newStatusText) { |
436 mainWindow.statusBar()->showMessage(newStatusText); |
437 mainWindow.statusBar()->showMessage(newStatusText); |
437 }); |
438 }); |
|
439 QObject::connect( |
|
440 data->tools.get(), |
|
441 &EditTools::select, |
|
442 [modelId, &documents](const QSet<ModelId>& indices, bool retain) { |
|
443 ModelData* data = findModelData(&documents, modelId); |
|
444 if (data != nullptr) { |
|
445 if (not retain) { |
|
446 data->itemSelectionModel->clear(); |
|
447 } |
|
448 for (const ModelId id : indices) { |
|
449 opt<int> index = data->model->find(id); |
|
450 if (index.has_value()) { |
|
451 const QModelIndex qindex = data->model->index(*index); |
|
452 data->itemSelectionModel->select(qindex, QItemSelectionModel::Select); |
|
453 } |
|
454 } |
|
455 } |
|
456 }); |
438 const QFileInfo fileInfo{*documents.modelPath(modelId)}; |
457 const QFileInfo fileInfo{*documents.modelPath(modelId)}; |
439 QMdiSubWindow* subWindow = ui.mdiArea->addSubWindow(data->canvas.get()); |
458 ModelSubWindow* subWindow = new ModelSubWindow{modelId, ui.mdiArea}; |
|
459 subWindow->setWidget(data->canvas.get()); |
440 subWindow->setWindowTitle(tabName(fileInfo)); |
460 subWindow->setWindowTitle(tabName(fileInfo)); |
441 subWindow->show(); |
461 subWindow->show(); |
442 } |
462 } |
443 }; |
463 }; |
444 QObject::connect(ui.actionNew, &QAction::triggered, [&]{ |
464 QObject::connect(ui.actionNew, &QAction::triggered, [&]{ |