diff --git a/CadQuery/Gui/Command.py b/CadQuery/Gui/Command.py index 77c934d..db53205 100644 --- a/CadQuery/Gui/Command.py +++ b/CadQuery/Gui/Command.py @@ -302,7 +302,7 @@ class CadQuerySaveAsScript: def Activated(self): #So we can open the save-as dialog mw = FreeCADGui.getMainWindow() - cqCodePane = mw.findChild(QtGui.QPlainTextEdit, "cqCodePane") + cqCodePane = Shared.getActiveCodePane() #Try to keep track of the previous path used to open as a convenience to the user if self.previousPath is None: @@ -323,6 +323,9 @@ class CadQuerySaveAsScript: #Assume that there was no 3D view to close pass + # Change the name of our script window's tab + Shared.setActiveWindowTitle(os.path.basename(filename[0])) + #Save the file before closing the original and the re-rendering the new one ExportCQ.save(filename[0]) CadQueryExecuteScript().Activated() diff --git a/CadQuery/Shared.py b/CadQuery/Shared.py index a68c343..5f0fbca 100644 --- a/CadQuery/Shared.py +++ b/CadQuery/Shared.py @@ -23,10 +23,69 @@ def clearActiveDocument(): def getActiveCodePane(): + """Gets the currently active code pane, even if its 3D view is selected.""" + + # TODO: Make sure that the code pane is selected, even if the associated 3D view currently has focus + # Grab our code editor so we can interact with it mw = FreeCADGui.getMainWindow() mdi = mw.findChild(QtGui.QMdiArea) - winName = mdi.currentSubWindow().windowTitle().split('.')[0] + + # If our current subwindow doesn't contain a script, we need to find the one that does + mdiWin = mdi.currentSubWindow() + if mdiWin == 0 or ".py" not in mdiWin.windowTitle(): + subList = mdi.subWindowList() + + for sub in subList: + print(sub.windowTitle()) + if sub.windowTitle() == mdiWin.windowTitle().split(" ")[0] + ".py": + mdiWin = sub + + winName = mdiWin.windowTitle().split('.')[0] cqCodePane = mw.findChild(QtGui.QPlainTextEdit, "cqCodePane_" + winName) - return cqCodePane \ No newline at end of file + return cqCodePane + + +def getActiveCodeWindow(): + """Gets the currently active code window (that holds the pane), even if the associated 3D view window has focus""" + + mw = FreeCADGui.getMainWindow() + mdi = mw.findChild(QtGui.QMdiArea) + + # We cannot trust the current subwindow to be a script window, it may be the associated 3D view + mdiWin = mdi.currentSubWindow() + + # We have a 3D view selected so we need to find the corresponding script window + if mdiWin == 0 or ".py" not in mdiWin.windowTitle(): + subList = mdi.subWindowList() + + for sub in subList: + if sub.windowTitle() == mdiWin.windowTitle() + ".py": + mdiWin = sub + + return mdiWin + + +def setActiveWindowTitle(title): + """Sets the title of the currently active MDI window, as long as it is a scripting window""" + + mw = FreeCADGui.getMainWindow() + mdi = mw.findChild(QtGui.QMdiArea) + + # We cannot trust the current subwindow to be a script window, it may be the associated 3D view + mdiWin = mdi.currentSubWindow() + + if mdiWin == 0 or ".py" not in mdiWin.windowTitle(): + subList = mdi.subWindowList() + + for sub in subList: + if sub.windowTitle() == mdiWin.windowTitle() + ".py": + mdiWin = sub + + # Change the window title if there is something there to change + if (mdiWin != 0): + mdiWin.setWindowTitle(title) + + cqCodePane = mdiWin.findChild(QtGui.QPlainTextEdit) + cqCodePane.setObjectName("cqCodePane_" + title.split('.')[0]) \ No newline at end of file