Use explicit captures in main

Sat, 08 Apr 2023 16:12:12 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 08 Apr 2023 16:12:12 +0300
changeset 353
c229d38f04c6
parent 352
59a31dd8a33a
child 354
91053052bb28

Use explicit captures in main

src/main.cpp file | annotate | diff | comparison | revisions
--- a/src/main.cpp	Sat Apr 08 15:52:25 2023 +0300
+++ b/src/main.cpp	Sat Apr 08 16:12:12 2023 +0300
@@ -374,6 +374,32 @@
 	return subWindow;
 }
 
+static void executeAction(QTextDocument* model, const ModelAction& action)
+{
+	std::visit(overloaded{
+		[model](const AppendToModel& action){
+			QTextCursor cursor{model};
+			cursor.movePosition(QTextCursor::End);
+			const QString newText = modelElementToString(action.newElement);
+			// Make sure we have an empty line
+			if (not model->lastBlock().text().isEmpty()) {
+				cursor.insertBlock();
+			}
+			cursor.insertText(newText);
+		},
+		[](const DeleteFromModel&){},
+		[model](const ModifyModel& action){
+			QTextBlock block = model->findBlockByLineNumber((int) action.position);
+			if (block.isValid()) {
+				QTextCursor cursor{block};
+				cursor.select(QTextCursor::LineUnderCursor);
+				cursor.insertText(modelElementToString(action.newElement));
+			}
+			//model->assignAt(action.position, action.newElement);
+		},
+	}, action);
+}
+
 int main(int argc, char *argv[])
 {
 	doQtRegistrations();
@@ -399,7 +425,13 @@
 	};
 	const uiutilities::KeySequenceMap defaultKeyboardShortcuts =
 		uiutilities::makeKeySequenceMap(uiutilities::collectActions(&mainWindow));
-	const auto saveSettings = [&]{
+	const auto saveSettings = [
+		&libraries,
+		&mainWindow,
+		&recentlyOpenedFiles,
+		&renderPreferences,
+		&settingsChanged]
+	{
 		setSetting<Setting::MainWindowGeometry>(mainWindow.saveGeometry());
 		setSetting<Setting::MainWindowState>(mainWindow.saveState());
 		setSetting<Setting::RecentFiles>(recentlyOpenedFiles);
@@ -409,34 +441,16 @@
 		libraries.storeToSettings();
 		settingsChanged.emit();
 	};
-	const auto executeAction = [&](
-		QTextDocument* model, const ModelAction& action
-	) {
-		std::visit(overloaded{
-			[model](const AppendToModel& action){
-				QTextCursor cursor{model};
-				cursor.movePosition(QTextCursor::End);
-				const QString newText = modelElementToString(action.newElement);
-				// Make sure we have an empty line
-				if (not model->lastBlock().text().isEmpty()) {
-					cursor.insertBlock();
-				}
-				cursor.insertText(newText);
-			},
-			[](const DeleteFromModel&){},
-			[model](const ModifyModel& action){
-				QTextBlock block = model->findBlockByLineNumber((int) action.position);
-				if (block.isValid()) {
-					QTextCursor cursor{block};
-					cursor.select(QTextCursor::LineUnderCursor);
-					cursor.insertText(modelElementToString(action.newElement));
-				}
-				//model->assignAt(action.position, actio邉󠄌n.newElement);
-			},
-		}, action);
-		
-	};
-	const auto openModelForEditing = [&](const ModelId modelId){
+	const auto openModelForEditing = [
+		&colorTable,
+		&documents,
+		&mainWindow,
+		&messageLog,
+		&renderPreferences,
+		&settingsChanged,
+		&ui]
+		(const ModelId modelId)
+	{
 		QTextDocument* model = documents.getModelById(modelId);
 		if (model != nullptr) {
 			ModelData* data = new ModelData(&documents);
@@ -463,7 +477,7 @@
 			QObject::connect(
 				data->tools.get(),
 				&EditTools::newStatusText,
-				[&](const QString& newStatusText) {
+				[&mainWindow](const QString& newStatusText) {
 					mainWindow.statusBar()->showMessage(newStatusText);
 				});
 #if 0
@@ -508,7 +522,14 @@
 			subWindow->show();
 		}
 	};
-	const auto updateRecentlyOpenedDocumentsMenu = [&]{
+	const auto updateRecentlyOpenedDocumentsMenu = [
+		&documents,
+		&libraries,
+		&mainWindow,
+		&openModelForEditing,
+		&recentlyOpenedFiles,
+		&ui]
+	{
 		rebuildRecentFilesMenu(ui.menuRecentFiles, recentlyOpenedFiles, &mainWindow);
 		for (QAction* action : ui.menuRecentFiles->actions()) {
 			QString path = action->data().toString();
@@ -525,7 +546,17 @@
 			);
 		}
 	};
-	const auto restoreSettings = [&]{
+	const auto restoreSettings = [
+		&colorTable,
+		&documents,
+		&libraries,
+		&mainWindow,
+		&recentlyOpenedFiles,
+		&renderPreferences,
+		&settingsChanged,
+		&ui,
+		&updateRecentlyOpenedDocumentsMenu]
+	{
 		recentlyOpenedFiles = setting<Setting::RecentFiles>();
 		renderPreferences = loadRenderPreferences();
 		libraries.restoreFromSettings();
@@ -537,7 +568,12 @@
 		mainWindow.setToolButtonStyle(setting<Setting::ToolButtonStyle>());
 		settingsChanged.emit();
 	};
-	const auto addRecentlyOpenedFile = [&](const QString& path){
+	const auto addRecentlyOpenedFile = [
+		&recentlyOpenedFiles,
+		&saveSettings,
+		&updateRecentlyOpenedDocumentsMenu]
+		(const QString& path)
+	{
 		constexpr int maxRecentlyOpenedFiles = 10;
 		recentlyOpenedFiles.removeAll(path);
 		recentlyOpenedFiles.insert(0, path);
@@ -548,21 +584,26 @@
 		saveSettings();
 		updateRecentlyOpenedDocumentsMenu();
 	};
-	QObject::connect(ui.actionNew, &QAction::triggered, [&]{
-		openModelForEditing(documents.newModel());
-	});
-	QObject::connect(ui.actionOpen, &QAction::triggered, [&]{
-		const QString path = getOpenModelPath(&mainWindow);
-		if (not path.isEmpty())
+	QObject::connect(ui.actionNew, &QAction::triggered,
+		[&documents, &openModelForEditing]{
+			openModelForEditing(documents.newModel());
+		}
+	);
+	QObject::connect(ui.actionOpen, &QAction::triggered,
+		[&addRecentlyOpenedFile, &documents, &libraries, &mainWindow, &openModelForEditing]
 		{
-			const std::optional<ModelId> id = openModelFromPath(path, &libraries, &documents, &mainWindow);
-			if (id.has_value()) {
-				openModelForEditing(id.value());
-				addRecentlyOpenedFile(path);
+			const QString path = getOpenModelPath(&mainWindow);
+			if (not path.isEmpty())
+			{
+				const std::optional<ModelId> id = openModelFromPath(path, &libraries, &documents, &mainWindow);
+				if (id.has_value()) {
+					openModelForEditing(id.value());
+					addRecentlyOpenedFile(path);
+				}
 			}
 		}
-	});
-	QObject::connect(ui.actionSettingsEditor, &QAction::triggered, [&]{
+	);
+	QObject::connect(ui.actionSettingsEditor, &QAction::triggered, [&defaultKeyboardShortcuts, &restoreSettings, &settingsChanged, &ui]{
 		if (ui.mdiArea->findChildren<SettingsEditor*>().isEmpty()) {
 			auto* const settingsEditor = createSubWindow<SettingsEditor>(ui.mdiArea, defaultKeyboardShortcuts);
 			QObject::connect(&settingsChanged, &Signal::triggered, settingsEditor, &SettingsEditor::loadSettings);
@@ -584,7 +625,7 @@
 			// TODO
 		}
 	});
-	const auto save = [&](ModelId modelId){
+	const auto save = [&addRecentlyOpenedFile, &documents, &mainWindow](ModelId modelId){
 		QString error;
 		QTextStream errorStream{&error};
 		const bool succeeded = documents.saveModel(modelId, errorStream);
@@ -600,7 +641,7 @@
 			}
 		}
 	};;
-	const auto actionSaveAs = [&]{
+	const auto actionSaveAs = [&documents, &libraries, &mainWindow, &save, &ui]{
 		const std::optional<ModelId> modelId = findCurrentModelId(&ui);
 		if (modelId.has_value())
 		{
@@ -625,7 +666,12 @@
 		}
 	};
 	QObject::connect(ui.actionSaveAs, &QAction::triggered, actionSaveAs);
-	QObject::connect(ui.actionSave, &QAction::triggered, [&]{
+	QObject::connect(ui.actionSave, &QAction::triggered, [
+		&actionSaveAs,
+		&documents,
+		&save,
+		&ui]
+	{
 		const std::optional<ModelId> modelId = findCurrentModelId(&ui);
 		if (modelId.has_value()) {
 			const QString* path = documents.modelPath(*modelId);
@@ -637,12 +683,24 @@
 			}
 		}
 	});
-	QObject::connect(ui.actionDrawAxes, &QAction::triggered, [&](bool drawAxes){
+	QObject::connect(ui.actionDrawAxes, &QAction::triggered, [
+		&documents,
+		&renderPreferences,
+		&saveSettings,
+		&ui]
+		(bool drawAxes)
+	{
 		renderPreferences.drawAxes = drawAxes;
 		saveSettings();
 		updateRenderPreferences(&ui, &renderPreferences, &documents);
 	});
-	QObject::connect(ui.actionWireframe, &QAction::triggered, [&](bool enabled){
+	QObject::connect(ui.actionWireframe, &QAction::triggered, [
+		&documents,
+		&renderPreferences,
+		&saveSettings,
+		&ui]
+		(bool enabled)
+	{
 		renderPreferences.wireframe = enabled;
 		saveSettings();
 		updateRenderPreferences(&ui, &renderPreferences, &documents);
@@ -664,7 +722,12 @@
 	};
 	initializeTools(&ui, &toolWidgets, &mainWindow);
 	for (QAction* action : ui.editingModesToolBar->actions()) {
-		QObject::connect(action, &QAction::triggered, [&, action]{
+		QObject::connect(action, &QAction::triggered, [
+			action,
+			&checkEditingModeAction,
+			&documents,
+			&ui]
+		{
 			if (ModelData* data = currentModelData(&ui, &documents)) {
 				const EditingMode mode = action->data().value<EditingMode>();
 				data->tools->setEditMode(mode);
@@ -672,8 +735,13 @@
 			}
 		});
 	}
-	QObject::connect(ui.mdiArea, &QMdiArea::subWindowActivated,
-	[&](QMdiSubWindow* subWindow) {
+	QObject::connect(ui.mdiArea, &QMdiArea::subWindowActivated, [
+		&checkEditingModeAction,
+		&documents,
+		&ui,
+		&updateTitle]
+		(QMdiSubWindow* subWindow)
+	{
 		ModelSubWindow* modelSubWindow = qobject_cast<ModelSubWindow*>(subWindow);
 		if (modelSubWindow != nullptr) {
 			if (ModelData* data = documents.findPayload<ModelData>(modelSubWindow->modelId)) {
@@ -691,11 +759,11 @@
 	ui.messageLog->setModel(&messageLog);
 	QObject::connect(ui.actionAboutQt, &QAction::triggered, &app, &QApplication::aboutQt);
 	QObject::connect(&documents, &DocumentManager::message, &messageLog, &MessageLog::addMessage);
-	QObject::connect(&messageLog, &MessageLog::rowsAboutToBeInserted, [&]{
+	QObject::connect(&messageLog, &MessageLog::rowsAboutToBeInserted, [&ui]{
 		const auto bar = ui.messageLog->verticalScrollBar();
 		ui.messageLog->setProperty("shouldAutoScroll", bar->value() == bar->maximum());
 	});
-	QObject::connect(&messageLog, &MessageLog::rowsInserted, [&]{
+	QObject::connect(&messageLog, &MessageLog::rowsInserted, [&ui]{
 		ui.messageLog->resizeRowsToContents();
 		if (ui.messageLog->property("shouldAutoScroll").toBool()) {
 			ui.messageLog->scrollToBottom();
@@ -712,7 +780,7 @@
 	QObject::connect(
 		ui.actionMakeUnofficial,
 		&QAction::triggered,
-		[&]{
+		[&documents, &ui]{
 			if (ModelData* data = currentModelData(&ui, &documents)) {
 				QTextDocument* const model = data->model;
 				for (const ModelAction& action : ldraw::makeUnofficial(model)) {
@@ -735,7 +803,7 @@
 	QObject::connect(
 		ui.modelEdit,
 		&QPlainTextEdit::textChanged,
-		[&]{
+		[&documents, &libraries, &ui]{
 			if (ModelData* data = currentModelData(&ui, &documents)) {
 				documents.loadDependenciesForAllModels(libraries);
 				data->canvas->update();

mercurial