Skip to content

Commit

Permalink
Add shortcut to restart PrEditor if running in standalone mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MHendricks committed Dec 19, 2023
1 parent 9114a63 commit 486576b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
9 changes: 7 additions & 2 deletions preditor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion preditor/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 32 additions & 3 deletions preditor/gui/loggerwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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
Expand Down Expand Up @@ -109,6 +109,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)
Expand Down Expand Up @@ -219,6 +220,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
Expand Down Expand Up @@ -268,6 +270,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,
Expand Down Expand Up @@ -675,6 +681,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()

Expand Down Expand Up @@ -1065,7 +1087,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:
Expand All @@ -1074,6 +1098,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
Expand All @@ -1086,7 +1113,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.
Expand Down
12 changes: 12 additions & 0 deletions preditor/gui/ui/loggerwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<addaction name="uiCloseWorkboxACT"/>
<addaction name="separator"/>
<addaction name="uiSaveConsoleSettingsACT"/>
<addaction name="uiRestartACT"/>
<addaction name="uiCloseLoggerACT"/>
</widget>
<widget class="QMenu" name="uiHelpMENU">
Expand Down Expand Up @@ -930,6 +931,17 @@ at the indicated line in the specified text editor.
<string>Backup</string>
</property>
</action>
<action name="uiRestartACT">
<property name="text">
<string>Restart PrEditor</string>
</property>
<property name="toolTip">
<string>Closes PrEditor and launches a new process with the same cli arguments.</string>
</property>
<property name="shortcut">
<string>Ctrl+Alt+Shift+R</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down
1 change: 1 addition & 0 deletions preditor/resource/img/restart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 486576b

Please sign in to comment.