Move some recent file handling to MainWindow

Sun, 09 Apr 2023 13:28:36 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 09 Apr 2023 13:28:36 +0300
changeset 361
c5e8b68e34f8
parent 360
41b38b9e05a2
child 362
e1d646a4cbd8

Move some recent file handling to MainWindow

src/main.cpp file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.h file | annotate | diff | comparison | revisions
--- a/src/main.cpp	Sun Apr 09 12:23:32 2023 +0300
+++ b/src/main.cpp	Sun Apr 09 13:28:36 2023 +0300
@@ -180,16 +180,6 @@
 	return result;
 }
 
-static void rebuildRecentFilesMenu(QMenu* menu, const QStringList& strings, QWidget* parent)
-{
-	menu->clear();
-	for (const QString& path : strings) {
-		QAction* action = new QAction{path, parent};
-		action->setData(path);
-		menu->addAction(action);
-	}
-}
-
 template<typename Fn>
 static void forEachModel(const DocumentManager* documents, Fn&& fn)
 {
@@ -459,27 +449,10 @@
 		}
 	};
 	const auto updateRecentlyOpenedDocumentsMenu = [
-		&documents,
-		&libraries,
 		&mainWindow,
-		&openModelForEditing,
 		&recentlyOpenedFiles]
 	{
-		rebuildRecentFilesMenu(mainWindow.menuRecentFiles, recentlyOpenedFiles, &mainWindow);
-		for (QAction* action : mainWindow.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());
-					}
-				}
-			);
-		}
+		mainWindow.rebuildRecentFilesMenu(recentlyOpenedFiles);
 	};
 	const auto restoreSettings = [
 		&colorTable,
@@ -518,6 +491,18 @@
 		saveSettings();
 		updateRecentlyOpenedDocumentsMenu();
 	};
+	QObject::connect(
+		&mainWindow,
+		&MainWindow::recentFileSelected,
+		[&libraries, &documents, &mainWindow, &openModelForEditing, &addRecentlyOpenedFile](const QString& path) {
+			const auto id = openModelFromPath(path, &libraries, &documents, &mainWindow);
+			if (id.has_value())
+			{
+				openModelForEditing(id.value());
+				addRecentlyOpenedFile(path);
+			}
+		}
+	);
 	QObject::connect(mainWindow.actionNew, &QAction::triggered,
 		[&documents, &openModelForEditing]{
 			openModelForEditing(documents.newModel());
--- a/src/mainwindow.cpp	Sun Apr 09 12:23:32 2023 +0300
+++ b/src/mainwindow.cpp	Sun Apr 09 13:28:36 2023 +0300
@@ -87,3 +87,16 @@
 	dialog.setWindowTitle(QObject::tr("About %1").arg(CMAKE_PROJECT_NAME));
 	dialog.exec();
 }
+
+void MainWindow::rebuildRecentFilesMenu(const QStringList& strings)
+{
+	this->menuRecentFiles->clear();
+	for (const QString& path : strings)
+	{
+		QAction* action = new QAction{path, this};
+		this->menuRecentFiles->addAction(action);
+		connect(action, &QAction::triggered, [&, path]{
+			Q_EMIT this->recentFileSelected(path);
+		});
+	}
+}
--- a/src/mainwindow.h	Sun Apr 09 12:23:32 2023 +0300
+++ b/src/mainwindow.h	Sun Apr 09 13:28:36 2023 +0300
@@ -12,8 +12,10 @@
 	explicit MainWindow(QWidget *parent = nullptr);
 Q_SIGNALS:
 	void renderStyleSelected(gl::RenderStyle newStyle);
+	void recentFileSelected(const QString& path);
 public Q_SLOTS:
 	void setRenderStyle(gl::RenderStyle style);
+	void rebuildRecentFilesMenu(const QStringList& strings);
 private Q_SLOTS:
 	void showAboutDialog();
 };

mercurial