Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix incorrect enter/exit called after scripted module reload #7351

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions Base/Python/slicer/util.py
Expand Up @@ -1414,10 +1414,10 @@ def reloadScriptedModule(moduleName):
for item in items:
parent.layout().removeItem(item)

# create new widget inside existing parent
widget = eval("reloaded_module.%s(parent)" % widgetName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we remove the use of the reloaded_module ivar, we should also revisit

with open(filePath, encoding="utf8") as fp:
reloaded_module = imp.load_module(
moduleName, fp, filePath, (".py", "r", imp.PY_SOURCE))
# find and hide the existing widget
parent = eval("slicer.modules.%s.widgetRepresentation()" % moduleName.lower())
for child in parent.children():
try:
child.hide()
except AttributeError:
pass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @jcfr - this code never seemed like a clean implementation but as I recall it was the best I could figure out at the time. If there's a cleaner and more maintainable implementation it would be great.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the imp.load_module call still seems to be required. Without it, the module is not actually reloaded.

widget.setup()
setattr(slicer.modules, widgetName, widget)
# Creates new widget at slicer.modules.{widgetName}.
# Also ensures that qSlicerScriptedLoadableModuleWidget has references to updated enter/exit/setup methods.
# See https://github.com/Slicer/Slicer/issues/7424
widget.parent.reload()

return reloaded_module

Expand Down
12 changes: 12 additions & 0 deletions Base/QTGUI/qSlicerScriptedLoadableModuleWidget.cxx
Expand Up @@ -176,6 +176,18 @@ bool qSlicerScriptedLoadableModuleWidget::setPythonSource(const QString& filePat
return true;
}

//-----------------------------------------------------------------------------
void qSlicerScriptedLoadableModuleWidget::reload()
{
Q_D(qSlicerScriptedLoadableModuleWidget);
if (!QFileInfo::exists(d->PythonSourceFilePath))
{
return;
}
this->setPythonSource(this->pythonSource());
this->setup();
}

//-----------------------------------------------------------------------------
PyObject* qSlicerScriptedLoadableModuleWidget::self() const
{
Expand Down
2 changes: 2 additions & 0 deletions Base/QTGUI/qSlicerScriptedLoadableModuleWidget.h
Expand Up @@ -47,6 +47,8 @@ class Q_SLICER_BASE_QTGUI_EXPORT qSlicerScriptedLoadableModuleWidget
QString pythonSource()const;
bool setPythonSource(const QString& filePath, const QString& className = QLatin1String(""));

Q_INVOKABLE void reload();

/// Convenience method allowing to retrieve the associated scripted instance
Q_INVOKABLE PyObject* self() const;

Expand Down