From ecfb82d74992d36933b0e78bce550f177e17eb05 Mon Sep 17 00:00:00 2001 From: Mike Hendricks Date: Tue, 19 Dec 2023 15:28:41 -0600 Subject: [PATCH] Add shortcut to restart PrEditor if running in standalone mode --- preditor/__init__.py | 9 ++++++-- preditor/cli.py | 2 +- preditor/gui/loggerwindow.py | 35 ++++++++++++++++++++++++++++--- preditor/gui/ui/loggerwindow.ui | 12 +++++++++++ preditor/resource/img/restart.svg | 1 + 5 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 preditor/resource/img/restart.svg diff --git a/preditor/__init__.py b/preditor/__init__.py index ee168c03..bea1f93a 100644 --- a/preditor/__init__.py +++ b/preditor/__init__.py @@ -113,7 +113,7 @@ def configure(name, parent_callback=None, excepthook=True, logging=True, streams preditor.debug.BlurExcepthook.install() -def launch(run_workbox=False, app_id=None, name=None): +def launch(run_workbox=False, app_id=None, name=None, standalone=False): """Launches the preditor gui creating the QApplication instance if not already created. @@ -125,6 +125,9 @@ def launch(run_workbox=False, app_id=None, name=None): app_id (str, optional): Set the QApplication's applicationName to this value. This is normally only used when launching a standalone instance of the PrEditor gui. + standalone (bool, optional): Launch PrEditor in standalone mode. This + enables extra options that only make sense when it is running as + its own app, not inside of another app. Returns: preditor.gui.loggerwindow.LoggerWindow: The instance of the PrEditor @@ -147,7 +150,9 @@ def launch(run_workbox=False, app_id=None, name=None): # Check if we can actually run the PrEditor gui and setup Qt if required app = App(name=app_id) - widget = LoggerWindow.instance(run_workbox=run_workbox, name=name) + widget = LoggerWindow.instance( + run_workbox=run_workbox, name=name, standalone=standalone + ) # Show the PrEditor instance and make sure it regains focus and visibility widget.show() diff --git a/preditor/cli.py b/preditor/cli.py index ca2b4866..82e2e161 100644 --- a/preditor/cli.py +++ b/preditor/cli.py @@ -66,7 +66,7 @@ def launch(name, run_workbox): parameter_source = click.get_current_context().get_parameter_source('name') app_id = get_app_id(name, parameter_source == ParameterSource.DEFAULT) - preditor.launch(run_workbox=run_workbox, app_id=app_id, name=name) + preditor.launch(run_workbox=run_workbox, app_id=app_id, name=name, standalone=True) # shortcut diff --git a/preditor/gui/loggerwindow.py b/preditor/gui/loggerwindow.py index e1173782..987edc30 100644 --- a/preditor/gui/loggerwindow.py +++ b/preditor/gui/loggerwindow.py @@ -57,7 +57,7 @@ class LoggerWindow(Window): _instance = None styleSheetChanged = Signal(str) - def __init__(self, parent, name=None, run_workbox=False): + def __init__(self, parent, name=None, run_workbox=False, standalone=False): super(LoggerWindow, self).__init__(parent=parent) self.name = name if name else DEFAULT_CORE_NAME self.aboutToClearPathsEnabled = False @@ -111,6 +111,7 @@ def __init__(self, parent, name=None, run_workbox=False): self._stds = None self.uiLogToFileClearACT.setVisible(False) + self.uiRestartACT.triggered.connect(self.restartLogger) self.uiCloseLoggerACT.triggered.connect(self.closeLogger) self.uiRunAllACT.triggered.connect(self.execAll) @@ -223,6 +224,7 @@ def __init__(self, parent, name=None, run_workbox=False): QIcon(resourcePath('img/content-save.png')) ) self.uiAboutPreditorACT.setIcon(QIcon(resourcePath('img/information.png'))) + self.uiRestartACT.setIcon(QIcon(resourcePath('img/restart.svg'))) self.uiCloseLoggerACT.setIcon(QIcon(resourcePath('img/close-thick.png'))) # Make action shortcuts available anywhere in the Logger @@ -272,6 +274,10 @@ def __init__(self, parent, name=None, run_workbox=False): self.setup_run_workbox() + if not standalone: + # This action only is valid when running in standalone mode + self.uiRestartACT.setVisible(False) + # Run the current workbox after the LoggerWindow is shown. if run_workbox: # By using two singleShot timers, we can show and draw the LoggerWindow, @@ -696,6 +702,22 @@ def save_prefs(self, pref): with open(filename, 'w') as fp: json.dump(pref, fp, indent=4) + def restartLogger(self): + """Closes this PrEditor instance and starts a new process with the same + cli arguments. + + Note: This only works if PrEditor is running in standalone mode. It doesn't + quit the QApplication or other host process. It simply closes this instance + of PrEditor, saving its preferences, which should allow Qt to exit if no + other windows are open. + """ + self.close() + + # Get the current command and launch it as a new process. + cmd = sys.argv[0] + args = sys.argv[1:] + QtCore.QProcess.startDetached(cmd, args) + def restorePrefs(self): pref = self.load_prefs() @@ -1093,7 +1115,9 @@ def gotoTabByIndex(self, index): group_tab.setCurrentIndex(index) @staticmethod - def instance(parent=None, name=None, run_workbox=False, create=True): + def instance( + parent=None, name=None, run_workbox=False, create=True, standalone=False + ): """Returns the existing instance of the PrEditor gui creating it on first call. Args: @@ -1102,6 +1126,9 @@ def instance(parent=None, name=None, run_workbox=False, create=True): run_workbox (bool, optional): If the instance hasn't been created yet, this will execute the active workbox's code once fully initialized. create (bool, optional): Returns None if the instance has not been created. + standalone (bool, optional): Launch PrEditor in standalone mode. This + enables extra options that only make sense when it is running as + its own app, not inside of another app. Returns: Returns a fully initialized instance of the PrEditor gui. If called more @@ -1114,7 +1141,9 @@ def instance(parent=None, name=None, run_workbox=False, create=True): return None # create the logger instance - inst = LoggerWindow(parent, name=name, run_workbox=run_workbox) + inst = LoggerWindow( + parent, name=name, run_workbox=run_workbox, standalone=standalone + ) # RV has a Unique window structure. It makes more sense to not parent a # singleton window than to parent it to a specific top level window. diff --git a/preditor/gui/ui/loggerwindow.ui b/preditor/gui/ui/loggerwindow.ui index d4bdab47..4fc142c3 100644 --- a/preditor/gui/ui/loggerwindow.ui +++ b/preditor/gui/ui/loggerwindow.ui @@ -110,6 +110,7 @@ + @@ -939,6 +940,17 @@ at the indicated line in the specified text editor. Ctrl+P + + + Restart PrEditor + + + Closes PrEditor and launches a new process with the same cli arguments. + + + Ctrl+Alt+Shift+R + + diff --git a/preditor/resource/img/restart.svg b/preditor/resource/img/restart.svg new file mode 100644 index 00000000..81f410b3 --- /dev/null +++ b/preditor/resource/img/restart.svg @@ -0,0 +1 @@ +