src/main.cpp

changeset 350
676d0b43b577
parent 338
719b909a7d2b
child 351
9e3c1f838406
--- a/src/main.cpp	Sat Apr 08 15:15:41 2023 +0300
+++ b/src/main.cpp	Sat Apr 08 15:34:32 2023 +0300
@@ -425,19 +425,6 @@
 		libraries.storeToSettings();
 		settingsChanged.emit();
 	};
-	const auto updateRecentlyOpenedDocumentsMenu = [&]{
-		rebuildRecentFilesMenu(ui.menuRecentFiles, recentlyOpenedFiles, &mainWindow);
-		for (QAction* action : ui.menuRecentFiles->findChildren<QAction*>()) {
-			QString path = action->data().toString();
-			QObject::connect(
-				action,
-				&QAction::triggered,
-				[path, &libraries, &documents, &mainWindow]() {
-					openModelFromPath(path, &libraries, &documents, &mainWindow);
-				}
-			);
-		}
-	};
 	const auto executeAction = [&](
 		QTextDocument* model, const ModelAction& action
 	) {
@@ -465,29 +452,6 @@
 		}, action);
 		
 	};
-	const auto restoreSettings = [&]{
-		recentlyOpenedFiles = setting<Setting::RecentFiles>();
-		renderPreferences = loadRenderPreferences();
-		libraries.restoreFromSettings();
-		updateRecentlyOpenedDocumentsMenu();
-		colorTable = loadColors(&libraries);
-		updateRenderPreferences(&ui, &renderPreferences, &documents);
-		ui.mdiArea->setViewMode(setting<Setting::ViewMode>());
-		ui.retranslateUi(&mainWindow);
-		mainWindow.setToolButtonStyle(setting<Setting::ToolButtonStyle>());
-		settingsChanged.emit();
-	};
-	const auto addRecentlyOpenedFile = [&](const QString& path){
-		constexpr int maxRecentlyOpenedFiles = 10;
-		recentlyOpenedFiles.removeAll(path);
-		recentlyOpenedFiles.insert(0, path);
-		while (recentlyOpenedFiles.size() > maxRecentlyOpenedFiles)
-		{
-			recentlyOpenedFiles.removeLast();
-		}
-		saveSettings();
-		updateRecentlyOpenedDocumentsMenu();
-	};
 	const auto openModelForEditing = [&](const ModelId modelId){
 		QTextDocument* model = documents.getModelById(modelId);
 		if (model != nullptr) {
@@ -560,6 +524,46 @@
 			subWindow->show();
 		}
 	};
+	const auto updateRecentlyOpenedDocumentsMenu = [&]{
+		rebuildRecentFilesMenu(ui.menuRecentFiles, recentlyOpenedFiles, &mainWindow);
+		for (QAction* action : ui.menuRecentFiles->actions()) {
+			QString path = action->data().toString();
+			QObject::connect(
+				action,
+				&QAction::triggered,
+				[path, &libraries, &documents, &mainWindow, &openModelForEditing]() {
+					const auto id = openModelFromPath(path, &libraries, &documents, &mainWindow);
+					if (id.has_value())
+					{
+						openModelForEditing(id.value());
+					}
+				}
+			);
+		}
+	};
+	const auto restoreSettings = [&]{
+		recentlyOpenedFiles = setting<Setting::RecentFiles>();
+		renderPreferences = loadRenderPreferences();
+		libraries.restoreFromSettings();
+		updateRecentlyOpenedDocumentsMenu();
+		colorTable = loadColors(&libraries);
+		updateRenderPreferences(&ui, &renderPreferences, &documents);
+		ui.mdiArea->setViewMode(setting<Setting::ViewMode>());
+		ui.retranslateUi(&mainWindow);
+		mainWindow.setToolButtonStyle(setting<Setting::ToolButtonStyle>());
+		settingsChanged.emit();
+	};
+	const auto addRecentlyOpenedFile = [&](const QString& path){
+		constexpr int maxRecentlyOpenedFiles = 10;
+		recentlyOpenedFiles.removeAll(path);
+		recentlyOpenedFiles.insert(0, path);
+		while (recentlyOpenedFiles.size() > maxRecentlyOpenedFiles)
+		{
+			recentlyOpenedFiles.removeLast();
+		}
+		saveSettings();
+		updateRecentlyOpenedDocumentsMenu();
+	};
 	QObject::connect(ui.actionNew, &QAction::triggered, [&]{
 		openModelForEditing(documents.newModel());
 	});

mercurial