Merge pull request #1778 from alicevision/dev/lv/useLastPathForOpenSave

Use most recent project as base folder for file dialogs
This commit is contained in:
Fabien Castan 2022-09-28 23:06:54 +02:00 committed by GitHub
commit 243c278bcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 39 deletions

View file

@ -241,7 +241,7 @@ class Graph(BaseObject):
return Graph.IO.getFeaturesForVersion(self.header.get(Graph.IO.Keys.FileVersion, "0.0"))
@Slot(str)
def load(self, filepath, setupProjectFile=True, importScene=False):
def load(self, filepath, setupProjectFile=True, importProject=False):
"""
Load a meshroom graph ".mg" file.
@ -250,7 +250,7 @@ class Graph(BaseObject):
setupProjectFile: Store the reference to the project file and setup the cache directory.
If false, it only loads the graph of the project file as a template.
"""
if not importScene:
if not importProject:
self.clear()
with open(filepath) as jsonFile:
fileData = json.load(jsonFile)
@ -258,9 +258,9 @@ class Graph(BaseObject):
# older versions of Meshroom files only contained the serialized nodes
graphData = fileData.get(Graph.IO.Keys.Graph, fileData)
if importScene:
if importProject:
self._importedNodes.clear()
graphData = self.updateImportedScene(graphData)
graphData = self.updateImportedProject(graphData)
if not isinstance(graphData, dict):
raise RuntimeError('loadGraph error: Graph is not a dict. File: {}'.format(filepath))
@ -289,7 +289,7 @@ class Graph(BaseObject):
# Add node to the graph with raw attributes values
self._addNode(n, nodeName)
if importScene:
if importProject:
self._importedNodes.add(n)
# Create graph edges by resolving attributes expressions
@ -302,12 +302,12 @@ class Graph(BaseObject):
return True
def updateImportedScene(self, data):
def updateImportedProject(self, data):
"""
Update the names and links of the scene to import so that it can fit
Update the names and links of the project to import so that it can fit
correctly in the existing graph.
Parse all the nodes from the scene that is going to be imported.
Parse all the nodes from the project that is going to be imported.
If their name already exists in the graph, replace them with new names,
then parse all the nodes' inputs/outputs to replace the old names with
the new ones in the links.
@ -336,7 +336,7 @@ class Graph(BaseObject):
# First pass to get all the names that already exist in the graph, update them, and keep track of the changes
for nodeName, nodeData in sorted(data.items(), key=lambda x: self.getNodeIndexFromName(x[0])):
if not isinstance(nodeData, dict):
raise RuntimeError('updateImportedScene error: Node is not a dict.')
raise RuntimeError('updateImportedProject error: Node is not a dict.')
if nodeName in self._nodes.keys() or nodeName in updatedData.keys():
newName = createUniqueNodeName(self._nodes.keys(), nodeData["nodeType"])
@ -1404,7 +1404,7 @@ class Graph(BaseObject):
@property
def importedNodes(self):
"""" Return the list of nodes that were added to the graph with the latest 'Import Scene' action. """
"""" Return the list of nodes that were added to the graph with the latest 'Import Project' action. """
return self._importedNodes
@property

View file

@ -206,7 +206,7 @@ class PasteNodesCommand(GraphCommand):
self.nodeNames = []
def redoImpl(self):
data = self.graph.updateImportedScene(self.data)
data = self.graph.updateImportedProject(self.data)
nodes = self.graph.pasteNodes(data, self.position)
self.nodeNames = [node.name for node in nodes]
self.setText("Paste Node{} ({})".format("s" if len(self.nodeNames) > 1 else "", ", ".join(self.nodeNames)))
@ -217,20 +217,20 @@ class PasteNodesCommand(GraphCommand):
self.graph.removeNode(name)
class ImportSceneCommand(GraphCommand):
class ImportProjectCommand(GraphCommand):
"""
Handle the import of a scene into a Graph.
Handle the import of a project into a Graph.
"""
def __init__(self, graph, filepath=None, yOffset=0, parent=None):
super(ImportSceneCommand, self).__init__(graph, parent)
super(ImportProjectCommand, self).__init__(graph, parent)
self.filepath = filepath
self.importedNames = []
self.yOffset = yOffset
def redoImpl(self):
status = self.graph.load(self.filepath, setupProjectFile=False, importScene=True)
status = self.graph.load(self.filepath, setupProjectFile=False, importProject=True)
importedNodes = self.graph.importedNodes
self.setText("Import Scene ({} nodes)".format(importedNodes.count))
self.setText("Import Project ({} nodes)".format(importedNodes.count))
lowestY = 0
for node in self.graph.nodes:

View file

@ -354,7 +354,7 @@ class UIGraph(QObject):
return status
@Slot(QUrl, result=bool)
def importScene(self, filepath):
def importProject(self, filepath):
if isinstance(filepath, (QUrl)):
# depending how the QUrl has been initialized,
# toLocalFile() may return the local path or an empty string
@ -364,7 +364,7 @@ class UIGraph(QObject):
else:
localFile = filepath
yOffset = self.layout.gridSpacing + self.layout.nodeHeight
return self.push(commands.ImportSceneCommand(self._graph, localFile, yOffset=yOffset))
return self.push(commands.ImportProjectCommand(self._graph, localFile, yOffset=yOffset))
@Slot(QUrl)
def saveAs(self, url):

View file

@ -322,12 +322,12 @@ ApplicationWindow {
}
FileDialog {
id: importSceneDialog
title: "Import Scene"
id: importProjectDialog
title: "Import Project"
selectMultiple: false
nameFilters: ["Meshroom Graphs (*.mg)"]
onAccepted: {
graphEditor.uigraph.importScene(importSceneDialog.fileUrl)
graphEditor.uigraph.importProject(importProjectDialog.fileUrl)
}
}
@ -450,7 +450,7 @@ ApplicationWindow {
Action {
id: pasteAction
property string tooltip: "Paste the clipboard content to the scene if it contains valid nodes"
property string tooltip: "Paste the clipboard content to the project if it contains valid nodes"
text: "Paste Node(s)"
onTriggered: graphEditor.pasteNodes()
}
@ -460,6 +460,20 @@ ApplicationWindow {
onTriggered: _PaletteManager.togglePalette()
}
// Utility functions for elements in the menubar
function initFileDialogFolder(dialog) {
if(_reconstruction.graph && _reconstruction.graph.filepath) {
dialog.folder = Filepath.stringToUrl(Filepath.dirname(_reconstruction.graph.filepath));
} else {
var projects = MeshroomApp.recentProjectFiles;
if (projects.length > 0 && Filepath.exists(projects[0])) {
dialog.folder = Filepath.stringToUrl(Filepath.dirname(projects[0]));
}
}
}
header: MenuBar {
palette.window: Qt.darker(activePalette.window, 1.15)
Menu {
@ -509,10 +523,8 @@ ApplicationWindow {
text: "Open"
shortcut: "Ctrl+O"
onTriggered: ensureSaved(function() {
if(_reconstruction.graph && _reconstruction.graph.filepath) {
openFileDialog.folder = Filepath.stringToUrl(Filepath.dirname(_reconstruction.graph.filepath))
}
openFileDialog.open()
initFileDialogFolder(openFileDialog);
openFileDialog.open();
})
}
Menu {
@ -556,16 +568,22 @@ ApplicationWindow {
}
}
Action {
id: importSceneAction
text: "Import Scene"
id: importProjectAction
text: "Import Project"
shortcut: "Ctrl+Shift+I"
onTriggered: importSceneDialog.open()
onTriggered: {
initFileDialogFolder(importProjectDialog);
importProjectDialog.open();
}
}
Action {
id: importActionItem
text: "Import Images"
shortcut: "Ctrl+I"
onTriggered: importFilesDialog.open()
onTriggered: {
initFileDialogFolder(importFilesDialog);
importFilesDialog.open();
}
}
Action {
@ -602,7 +620,8 @@ ApplicationWindow {
}
else
{
saveFileDialog.open()
initFileDialogFolder(saveFileDialog);
saveFileDialog.open();
}
}
}
@ -611,10 +630,8 @@ ApplicationWindow {
text: "Save As..."
shortcut: "Ctrl+Shift+S"
onTriggered: {
if(_reconstruction.graph && _reconstruction.graph.filepath) {
saveFileDialog.folder = Filepath.stringToUrl(Filepath.dirname(_reconstruction.graph.filepath))
}
saveFileDialog.open()
initFileDialogFolder(saveFileDialog);
saveFileDialog.open();
}
}
Action {
@ -622,10 +639,8 @@ ApplicationWindow {
text: "Save As Template..."
shortcut: "Ctrl+Shift+T"
onTriggered: {
if(_reconstruction.graph && _reconstruction.graph.filepath) {
saveTemplateDialog.folder = Filepath.stringToUrl(Filepath.dirname(_reconstruction.graph.filepath))
}
saveTemplateDialog.open()
initFileDialogFolder(saveTemplateDialog);
saveTemplateDialog.open();
}
}
MenuSeparator { }