From 1859e962fa0ecd76969c4de974b91b4ead9f8b83 Mon Sep 17 00:00:00 2001 From: Steve Pieper Date: Wed, 9 Oct 2019 20:18:11 -0400 Subject: [PATCH] ENH: expose module load/unload API Exposing these methods allows programmatic control over module loading and unloading, which can be useful for giving users access to experimental modules, like with the following script: ``` import os import shutil archiveFilePath = os.path.join(slicer.app.temporaryPath, "master.zip") outputDir = os.path.join(slicer.app.temporaryPath, "SlicerAnimator") try: os.remove(archiveFilePath) except FileNotFoundError: pass try: shutil.rmtree(outputDir) except FileNotFoundError: pass os.mkdir(outputDir) slicer.util.downloadAndExtractArchive( url = "https://github.com/pieper/SlicerAnimator/archive/master.zip", archiveFilePath = archiveFilePath, outputDir = outputDir) modulePath = os.path.join(outputDir, "SlicerAnimator-master", "Animator", "Animator.py") factoryManager = slicer.app.moduleManager().factoryManager() factoryManager.registerModule(qt.QFileInfo(modulePath)) factoryManager.instantiateModule("Animator") factoryManager.loadModule("Animator") slicer.util.selectModule("Animator") slicer.util.delayDisplay("Waiting") factoryManager.unloadModule("Animator") ``` --- Base/QTCore/qSlicerAbstractModuleFactoryManager.h | 12 ++++++------ Base/QTCore/qSlicerModuleFactoryManager.h | 9 +++++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Base/QTCore/qSlicerAbstractModuleFactoryManager.h b/Base/QTCore/qSlicerAbstractModuleFactoryManager.h index 71f2ecbe0c2..8d029f15646 100644 --- a/Base/QTCore/qSlicerAbstractModuleFactoryManager.h +++ b/Base/QTCore/qSlicerAbstractModuleFactoryManager.h @@ -183,6 +183,12 @@ class Q_SLICER_BASE_QTCORE_EXPORT qSlicerAbstractModuleFactoryManager : public Q /// Uninstantiate all instantiated modules void uninstantiateModules(); + /// Instantiate a module given its \a name + Q_INVOKABLE qSlicerAbstractCoreModule* instantiateModule(const QString& name); + + /// Uninstantiate a module given its \a moduleName + Q_INVOKABLE virtual void uninstantiateModule(const QString& moduleName); + /// Enable/Disable verbose output during module discovery process void setVerboseModuleDiscovery(bool value); @@ -227,12 +233,6 @@ public slots: void registerModules(const QString& directoryPath); - /// Instantiate a module given its \a name - qSlicerAbstractCoreModule* instantiateModule(const QString& name); - - /// Uninstantiate a module given its \a moduleName - virtual void uninstantiateModule(const QString& moduleName); - private: Q_DECLARE_PRIVATE(qSlicerAbstractModuleFactoryManager); Q_DISABLE_COPY(qSlicerAbstractModuleFactoryManager); diff --git a/Base/QTCore/qSlicerModuleFactoryManager.h b/Base/QTCore/qSlicerModuleFactoryManager.h index ca96099d074..6150d28298e 100644 --- a/Base/QTCore/qSlicerModuleFactoryManager.h +++ b/Base/QTCore/qSlicerModuleFactoryManager.h @@ -86,7 +86,11 @@ class Q_SLICER_BASE_QTCORE_EXPORT qSlicerModuleFactoryManager /// Load module identified by \a name /// \todo move it as protected - bool loadModule(const QString& name); + Q_INVOKABLE bool loadModule(const QString& name); + + /// Unload module identified by \a name + Q_INVOKABLE void unloadModule(const QString& name); + /// Return all module paths that are direct child of \a basePath. QStringList modulePaths(const QString& basePath); @@ -112,9 +116,6 @@ public slots: bool loadModule(const QString& name, const QString& dependee); - /// Unload module identified by \a name - void unloadModule(const QString& name); - /// Uninstantiate a module given its \a moduleName void uninstantiateModule(const QString& moduleName) override;