176 QString result = fileInfo.baseName(); |
176 QString result = fileInfo.baseName(); |
177 if (result.isEmpty()) { |
177 if (result.isEmpty()) { |
178 result = QObject::tr("<unnamed>"); |
178 result = QObject::tr("<unnamed>"); |
179 } |
179 } |
180 return result; |
180 return result; |
181 } |
|
182 |
|
183 static void rebuildRecentFilesMenu(QMenu* menu, const QStringList& strings, QWidget* parent) |
|
184 { |
|
185 menu->clear(); |
|
186 for (const QString& path : strings) { |
|
187 QAction* action = new QAction{path, parent}; |
|
188 action->setData(path); |
|
189 menu->addAction(action); |
|
190 } |
|
191 } |
181 } |
192 |
182 |
193 template<typename Fn> |
183 template<typename Fn> |
194 static void forEachModel(const DocumentManager* documents, Fn&& fn) |
184 static void forEachModel(const DocumentManager* documents, Fn&& fn) |
195 { |
185 { |
457 subWindow->setWindowTitle(tabName(fileInfo)); |
447 subWindow->setWindowTitle(tabName(fileInfo)); |
458 subWindow->show(); |
448 subWindow->show(); |
459 } |
449 } |
460 }; |
450 }; |
461 const auto updateRecentlyOpenedDocumentsMenu = [ |
451 const auto updateRecentlyOpenedDocumentsMenu = [ |
462 &documents, |
|
463 &libraries, |
|
464 &mainWindow, |
452 &mainWindow, |
465 &openModelForEditing, |
|
466 &recentlyOpenedFiles] |
453 &recentlyOpenedFiles] |
467 { |
454 { |
468 rebuildRecentFilesMenu(mainWindow.menuRecentFiles, recentlyOpenedFiles, &mainWindow); |
455 mainWindow.rebuildRecentFilesMenu(recentlyOpenedFiles); |
469 for (QAction* action : mainWindow.menuRecentFiles->actions()) { |
|
470 QString path = action->data().toString(); |
|
471 QObject::connect( |
|
472 action, |
|
473 &QAction::triggered, |
|
474 [path, &libraries, &documents, &mainWindow, &openModelForEditing]() { |
|
475 const auto id = openModelFromPath(path, &libraries, &documents, &mainWindow); |
|
476 if (id.has_value()) |
|
477 { |
|
478 openModelForEditing(id.value()); |
|
479 } |
|
480 } |
|
481 ); |
|
482 } |
|
483 }; |
456 }; |
484 const auto restoreSettings = [ |
457 const auto restoreSettings = [ |
485 &colorTable, |
458 &colorTable, |
486 &documents, |
459 &documents, |
487 &libraries, |
460 &libraries, |
516 recentlyOpenedFiles.removeLast(); |
489 recentlyOpenedFiles.removeLast(); |
517 } |
490 } |
518 saveSettings(); |
491 saveSettings(); |
519 updateRecentlyOpenedDocumentsMenu(); |
492 updateRecentlyOpenedDocumentsMenu(); |
520 }; |
493 }; |
|
494 QObject::connect( |
|
495 &mainWindow, |
|
496 &MainWindow::recentFileSelected, |
|
497 [&libraries, &documents, &mainWindow, &openModelForEditing, &addRecentlyOpenedFile](const QString& path) { |
|
498 const auto id = openModelFromPath(path, &libraries, &documents, &mainWindow); |
|
499 if (id.has_value()) |
|
500 { |
|
501 openModelForEditing(id.value()); |
|
502 addRecentlyOpenedFile(path); |
|
503 } |
|
504 } |
|
505 ); |
521 QObject::connect(mainWindow.actionNew, &QAction::triggered, |
506 QObject::connect(mainWindow.actionNew, &QAction::triggered, |
522 [&documents, &openModelForEditing]{ |
507 [&documents, &openModelForEditing]{ |
523 openModelForEditing(documents.newModel()); |
508 openModelForEditing(documents.newModel()); |
524 } |
509 } |
525 ); |
510 ); |