src/main.cpp

changeset 368
9444de9762c3
parent 367
df61cd3d5ab4
child 370
b2f9ded235a6
--- a/src/main.cpp	Sun Apr 09 16:30:33 2023 +0300
+++ b/src/main.cpp	Sun Apr 09 17:05:40 2023 +0300
@@ -60,30 +60,47 @@
 #endif
 }
 
-static std::optional<ModelId> openModelFromPath(
-	const QString& path,
-	const LibrariesModel* libraries,
-	DocumentManager* documents,
-	QWidget* parent)
+struct ToolWidgets
+{
+	CircleToolOptionsWidget* circleToolOptions;
+};
+
+struct MainState
+{
+	MainWindow mainWindow;
+	DocumentManager documents;
+	QString currentLanguage = "en";
+	QTranslator translator{&mainWindow};
+	LibrariesModel libraries{&mainWindow};
+	QStringList recentlyOpenedFiles;
+	ColorTable colorTable;
+	gl::RenderPreferences renderPreferences;
+	MessageLog messageLog;
+	ToolWidgets toolWidgets{
+		.circleToolOptions = new CircleToolOptionsWidget{&mainWindow},
+	};
+};
+
+static std::optional<ModelId> openModelFromPath(MainState* state, const QString& path)
 {
 	QString errorString;
 	QTextStream errorStream{&errorString};
-	const std::optional<ModelId> modelIdOpt = documents->openModel(
+	const std::optional<ModelId> modelIdOpt = state->documents.openModel(
 		path,
 		errorStream,
 		OpenType::ManuallyOpened);
 	if (modelIdOpt.has_value()) {
-		const DocumentManager::MissingDependencies missing = documents->loadDependenciesForAllModels(*libraries);
+		const DocumentManager::MissingDependencies missing = state->documents.loadDependenciesForAllModels(state->libraries);
 		if (not missing.empty()) {
 			QMessageBox::warning(
-				parent,
+				&state->mainWindow,
 				QObject::tr("Problem loading references"),
 				errorStringFromMissingDependencies(missing));
 		}
 	}
 	else {
 		QMessageBox::critical(
-			parent,
+			&state->mainWindow,
 			QObject::tr("Problem opening file"),
 			QObject::tr("Could not open %1: %2").arg(quoted(path), errorString)
 		);
@@ -189,11 +206,6 @@
 	};
 }
 
-struct ToolWidgets
-{
-	CircleToolOptionsWidget* circleToolOptions;
-};
-
 static void initializeTools(MainWindow* ui, ToolWidgets* toolWidgets, QWidget* parent)
 {
 	const struct
@@ -295,22 +307,6 @@
 	return font;
 }
 
-struct MainState
-{
-	MainWindow mainWindow;
-	DocumentManager documents;
-	QString currentLanguage = "en";
-	QTranslator translator{&mainWindow};
-	LibrariesModel libraries{&mainWindow};
-	QStringList recentlyOpenedFiles;
-	ColorTable colorTable;
-	gl::RenderPreferences renderPreferences;
-	MessageLog messageLog;
-	ToolWidgets toolWidgets{
-		.circleToolOptions = new CircleToolOptionsWidget{&mainWindow},
-	};
-};
-
 static void openModelForEditing(MainState* state, const ModelId modelId)
 {
 	QTextDocument* model = state->documents.getModelById(modelId);
@@ -492,7 +488,7 @@
 		&state.mainWindow,
 		&MainWindow::recentFileSelected,
 		[&state](const QString& path) {
-			const auto id = openModelFromPath(path, &state.libraries, &state.documents, &state.mainWindow);
+			const auto id = openModelFromPath(&state, path);
 			if (id.has_value())
 			{
 				openModelForEditing(&state, id.value());
@@ -511,7 +507,7 @@
 			const QString path = getOpenModelPath(&state.mainWindow);
 			if (not path.isEmpty())
 			{
-				const std::optional<ModelId> id = openModelFromPath(path, &state.libraries, &state.documents, &state.mainWindow);
+				const std::optional<ModelId> id = openModelFromPath(&state, path);
 				if (id.has_value()) {
 					openModelForEditing(&state, id.value());
 					addRecentlyOpenedFile(&state, path);

mercurial