src/main.cpp

changeset 368
9444de9762c3
parent 367
df61cd3d5ab4
child 370
b2f9ded235a6
equal deleted inserted replaced
367:df61cd3d5ab4 368:9444de9762c3
58 qRegisterMetaTypeStreamOperators<QMdiArea::ViewMode>(); 58 qRegisterMetaTypeStreamOperators<QMdiArea::ViewMode>();
59 qRegisterMetaTypeStreamOperators<Qt::ToolButtonStyle>(); 59 qRegisterMetaTypeStreamOperators<Qt::ToolButtonStyle>();
60 #endif 60 #endif
61 } 61 }
62 62
63 static std::optional<ModelId> openModelFromPath( 63 struct ToolWidgets
64 const QString& path, 64 {
65 const LibrariesModel* libraries, 65 CircleToolOptionsWidget* circleToolOptions;
66 DocumentManager* documents, 66 };
67 QWidget* parent) 67
68 struct MainState
69 {
70 MainWindow mainWindow;
71 DocumentManager documents;
72 QString currentLanguage = "en";
73 QTranslator translator{&mainWindow};
74 LibrariesModel libraries{&mainWindow};
75 QStringList recentlyOpenedFiles;
76 ColorTable colorTable;
77 gl::RenderPreferences renderPreferences;
78 MessageLog messageLog;
79 ToolWidgets toolWidgets{
80 .circleToolOptions = new CircleToolOptionsWidget{&mainWindow},
81 };
82 };
83
84 static std::optional<ModelId> openModelFromPath(MainState* state, const QString& path)
68 { 85 {
69 QString errorString; 86 QString errorString;
70 QTextStream errorStream{&errorString}; 87 QTextStream errorStream{&errorString};
71 const std::optional<ModelId> modelIdOpt = documents->openModel( 88 const std::optional<ModelId> modelIdOpt = state->documents.openModel(
72 path, 89 path,
73 errorStream, 90 errorStream,
74 OpenType::ManuallyOpened); 91 OpenType::ManuallyOpened);
75 if (modelIdOpt.has_value()) { 92 if (modelIdOpt.has_value()) {
76 const DocumentManager::MissingDependencies missing = documents->loadDependenciesForAllModels(*libraries); 93 const DocumentManager::MissingDependencies missing = state->documents.loadDependenciesForAllModels(state->libraries);
77 if (not missing.empty()) { 94 if (not missing.empty()) {
78 QMessageBox::warning( 95 QMessageBox::warning(
79 parent, 96 &state->mainWindow,
80 QObject::tr("Problem loading references"), 97 QObject::tr("Problem loading references"),
81 errorStringFromMissingDependencies(missing)); 98 errorStringFromMissingDependencies(missing));
82 } 99 }
83 } 100 }
84 else { 101 else {
85 QMessageBox::critical( 102 QMessageBox::critical(
86 parent, 103 &state->mainWindow,
87 QObject::tr("Problem opening file"), 104 QObject::tr("Problem opening file"),
88 QObject::tr("Could not open %1: %2").arg(quoted(path), errorString) 105 QObject::tr("Could not open %1: %2").arg(quoted(path), errorString)
89 ); 106 );
90 } 107 }
91 return modelIdOpt; 108 return modelIdOpt;
186 .lineAntiAliasing = setting<Setting::LineAntiAliasing>(), 203 .lineAntiAliasing = setting<Setting::LineAntiAliasing>(),
187 .drawAxes = setting<Setting::DrawAxes>(), 204 .drawAxes = setting<Setting::DrawAxes>(),
188 .wireframe = setting<Setting::Wireframe>(), 205 .wireframe = setting<Setting::Wireframe>(),
189 }; 206 };
190 } 207 }
191
192 struct ToolWidgets
193 {
194 CircleToolOptionsWidget* circleToolOptions;
195 };
196 208
197 static void initializeTools(MainWindow* ui, ToolWidgets* toolWidgets, QWidget* parent) 209 static void initializeTools(MainWindow* ui, ToolWidgets* toolWidgets, QWidget* parent)
198 { 210 {
199 const struct 211 const struct
200 { 212 {
292 font.setFamily(setting<Setting::CodeEditorFontFamily>()); 304 font.setFamily(setting<Setting::CodeEditorFontFamily>());
293 font.setPointSize(setting<Setting::CodeEditorFontSize>()); 305 font.setPointSize(setting<Setting::CodeEditorFontSize>());
294 } 306 }
295 return font; 307 return font;
296 } 308 }
297
298 struct MainState
299 {
300 MainWindow mainWindow;
301 DocumentManager documents;
302 QString currentLanguage = "en";
303 QTranslator translator{&mainWindow};
304 LibrariesModel libraries{&mainWindow};
305 QStringList recentlyOpenedFiles;
306 ColorTable colorTable;
307 gl::RenderPreferences renderPreferences;
308 MessageLog messageLog;
309 ToolWidgets toolWidgets{
310 .circleToolOptions = new CircleToolOptionsWidget{&mainWindow},
311 };
312 };
313 309
314 static void openModelForEditing(MainState* state, const ModelId modelId) 310 static void openModelForEditing(MainState* state, const ModelId modelId)
315 { 311 {
316 QTextDocument* model = state->documents.getModelById(modelId); 312 QTextDocument* model = state->documents.getModelById(modelId);
317 if (model != nullptr) { 313 if (model != nullptr) {
490 MainState state; 486 MainState state;
491 QObject::connect( 487 QObject::connect(
492 &state.mainWindow, 488 &state.mainWindow,
493 &MainWindow::recentFileSelected, 489 &MainWindow::recentFileSelected,
494 [&state](const QString& path) { 490 [&state](const QString& path) {
495 const auto id = openModelFromPath(path, &state.libraries, &state.documents, &state.mainWindow); 491 const auto id = openModelFromPath(&state, path);
496 if (id.has_value()) 492 if (id.has_value())
497 { 493 {
498 openModelForEditing(&state, id.value()); 494 openModelForEditing(&state, id.value());
499 addRecentlyOpenedFile(&state, path); 495 addRecentlyOpenedFile(&state, path);
500 } 496 }
509 [&state] 505 [&state]
510 { 506 {
511 const QString path = getOpenModelPath(&state.mainWindow); 507 const QString path = getOpenModelPath(&state.mainWindow);
512 if (not path.isEmpty()) 508 if (not path.isEmpty())
513 { 509 {
514 const std::optional<ModelId> id = openModelFromPath(path, &state.libraries, &state.documents, &state.mainWindow); 510 const std::optional<ModelId> id = openModelFromPath(&state, path);
515 if (id.has_value()) { 511 if (id.has_value()) {
516 openModelForEditing(&state, id.value()); 512 openModelForEditing(&state, id.value());
517 addRecentlyOpenedFile(&state, path); 513 addRecentlyOpenedFile(&state, path);
518 } 514 }
519 } 515 }

mercurial