src/main.cpp

changeset 217
6d95c1a41e6e
parent 216
c7241f504117
child 218
63125c36de73
--- a/src/main.cpp	Mon Jun 13 02:18:25 2022 +0300
+++ b/src/main.cpp	Tue Jun 14 17:55:50 2022 +0300
@@ -149,7 +149,8 @@
 
 static ModelSubWindow* currentModelSubWindow(Ui_MainWindow* ui)
 {
-	return qobject_cast<ModelSubWindow*>(ui->mdiArea->activeSubWindow());
+	auto* w = ui->mdiArea->activeSubWindow();
+	return qobject_cast<ModelSubWindow*>(w);
 }
 
 static ModelData* currentModelData(Ui_MainWindow* ui, DocumentManager* documents)
@@ -395,39 +396,39 @@
 		Model* model = documents.getModelById(modelId);
 		if (model != nullptr) {
 			ModelData* data = new ModelData(&documents);
-			data->tools = std::make_unique<EditTools>(model, colorTable);
+			data->tools = std::make_unique<EditTools>();
 			data->canvas = std::make_unique<PartRenderer>(model, &documents, colorTable);
-			data->itemSelectionModel = std::make_unique<QItemSelectionModel>(model);
+			data->itemSelectionModel = std::make_unique<QItemSelectionModel>();
+			data->itemSelectionModel->setModel(model);
 			data->axesLayer = std::make_unique<AxesLayer>();
+			constexpr glm::mat4 XZ = {{1, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 1}};
 			data->gridLayer = std::make_unique<GridLayer>();
+			data->gridLayer->setGridMatrix(XZ);
+			data->tools->setGridMatrix(XZ);
 			data->model = model;
 			data->canvas->addRenderLayer(data->axesLayer.get());
 			data->canvas->setLayerEnabled(data->axesLayer.get(), settings.drawAxes());
 			data->canvas->addRenderLayer(data->gridLayer.get());
+			data->canvas->addRenderLayer(data->tools.get());
 			documents.setModelPayload(modelId, data);
 			QObject::connect(
 				data->tools.get(),
 				&EditTools::modelAction,
 				std::bind(executeAction, model, std::placeholders::_1));
-#if 0
-			QObject::connect(data->itemSelectionModel.get(), &QItemSelectionModel::selectionChanged,
-				[modelId, &documents](
-				const QItemSelection& selected,
-				const QItemSelection& deselected)
-			{
+			QObject::connect(
+				data->itemSelectionModel.get(),
+				&QItemSelectionModel::selectionChanged,
+				[modelId, &documents]{
 				ModelData* data = findModelData(&documents, modelId);
 				if (data != nullptr) {
 					auto resolveIndex = [&data](const QModelIndex& index){
 						return data->model->idAt(index.row());
 					};
-					auto resolve = [&resolveIndex](const QItemSelection& selection)
-					{
-						return fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex);
-					};
-					data->canvas->handleSelectionChange(resolve(selected), resolve(deselected));
+					const auto selection = data->itemSelectionModel->selection();
+					const auto indices = fn::map<QSet<ModelId>>(selection.indexes(), resolveIndex);
+					data->canvas->setSelection(indices);
 				}
 			});
-#endif
 			data->canvas->setRenderPreferences(renderPreferences);
 			QObject::connect(
 				data->tools.get(),
@@ -435,8 +436,27 @@
 				[&](const QString& newStatusText) {
 					mainWindow.statusBar()->showMessage(newStatusText);
 				});
+			QObject::connect(
+				data->tools.get(),
+				&EditTools::select,
+				[modelId, &documents](const QSet<ModelId>& indices, bool retain) {
+					ModelData* data = findModelData(&documents, modelId);
+					if (data != nullptr) {
+						if (not retain) {
+							data->itemSelectionModel->clear();
+						}
+						for (const ModelId 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);
+							}
+						}
+					}
+				});
 			const QFileInfo fileInfo{*documents.modelPath(modelId)};
-			QMdiSubWindow* subWindow = ui.mdiArea->addSubWindow(data->canvas.get());
+			ModelSubWindow* subWindow = new ModelSubWindow{modelId, ui.mdiArea};
+			subWindow->setWidget(data->canvas.get());
 			subWindow->setWindowTitle(tabName(fileInfo));
 			subWindow->show();
 		}

mercurial