src/main.cpp

changeset 204
52e10e8d88cc
parent 203
1909a0123c72
child 205
1a4342d80de7
--- a/src/main.cpp	Tue Jun 07 21:35:29 2022 +0300
+++ b/src/main.cpp	Wed Jun 08 19:33:00 2022 +0300
@@ -263,6 +263,22 @@
 	}
 }
 
+constexpr bool sortModelIndexesByRow(const QModelIndex& a, const QModelIndex& b)
+{
+	return a.row() < b.row();
+}
+
+std::vector<int> rows(const QModelIndexList& indexList)
+{
+	std::vector<int> result;
+	result.reserve(indexList.size());
+	for (const QModelIndex& index : indexList)
+	{
+		result.push_back(index.row());
+	}
+	return result;
+}
+
 int main(int argc, char *argv[])
 {
 	doQtRegistrations();
@@ -303,6 +319,18 @@
 			);
 		}
 	};
+	static constexpr auto executeAction = [&](
+		Model* model, const ModelAction& action
+	) {
+		std::visit(overloaded{
+			[model](const AppendToModel& action){
+				model->append(action.newElement);
+			},
+			[model](const DeleteFromModel& action){
+				model->remove(action.position);
+			},
+		}, action);
+	};
 	const auto restoreSettings = [&]{
 		recentlyOpenedFiles = settings.recentFiles();
 		documentSplitterState = settings.mainSplitterState();
@@ -329,6 +357,10 @@
 	const auto openModelForEditing = [&](const ModelId modelId){
 		Model* model = documents.getModelById(modelId);
 		EditorTabWidget* document = new EditorTabWidget{model, &documents, colorTable};
+		QObject::connect(
+			document,
+			&EditorTabWidget::modelAction,
+			std::bind(executeAction, model, std::placeholders::_1));
 		QItemSelectionModel* selectionModel = new QItemSelectionModel{model};
 		itemSelectionModels[model] = selectionModel;
 		QObject::connect(selectionModel, &QItemSelectionModel::selectionChanged,
@@ -447,6 +479,16 @@
 			}
 		}
 	});
+	QObject::connect(ui.actionDelete, &QAction::triggered, [&]{
+		EditorTabWidget* tab = currentTabWidget(&ui);
+		if (tab != nullptr) {
+			std::vector<int> selectedRows = rows(ui.modelListView->selectionModel()->selectedRows());
+			std::sort(selectedRows.begin(), selectedRows.end(), std::greater<int>{});
+			for (int row : selectedRows) {
+				executeAction(tab->model, DeleteFromModel{.position = row});
+			}
+		}
+	});
 	QObject::connect(ui.actionDrawAxes, &QAction::triggered, [&](bool drawAxes){
 		renderPreferences.drawAxes = drawAxes;
 		saveSettings();
@@ -477,14 +519,16 @@
 		});
 	}
 	QObject::connect(ui.mdiArea, &QMdiArea::subWindowActivated,
-	[&](QMdiSubWindow* subWindow){
-		EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(subWindow->widget());
-		if (tab != nullptr) {
-			checkEditingModeAction(tab->currentEditingMode());
-			QItemSelectionModel* selectionModel = itemSelectionModels.value(tab->model);
-			if (selectionModel != nullptr) {
-				ui.modelListView->setModel(tab->model);
-				ui.modelListView->setSelectionModel(selectionModel);
+	[&](QMdiSubWindow* subWindow) {
+		if (subWindow != nullptr) {
+			EditorTabWidget* tab = qobject_cast<EditorTabWidget*>(subWindow->widget());
+			if (tab != nullptr) {
+				checkEditingModeAction(tab->currentEditingMode());
+				QItemSelectionModel* selectionModel = itemSelectionModels.value(tab->model);
+				if (selectionModel != nullptr) {
+					ui.modelListView->setModel(tab->model);
+					ui.modelListView->setSelectionModel(selectionModel);
+				}
 			}
 		}
 	});

mercurial