def loadCustomExtensions(self,depth=1): """TODO: this does not actually load the modules correctly. May not be possible to load shared libraries and CLIs with this method""" factory = slicer.app.moduleManager().factoryManager() loadedModules = factory.instantiatedModuleNames() newModules = [] failedModules = [] for relPath in @CUSTOM_REL_PATHS@: absPath = os.path.join( slicer.app.slicerHome, "@CUSTOM_APP_NAME@"+"-Extensions", relPath) print("Looking for modules in: " + absPath) modules = ModuleInfo.findModules(absPath,depth) print("Found custom modules: " + str(modules)) candidates = [m for m in modules if m.key not in loadedModules] print("These are candidates: " + str(candidates)) for candidate in candidates: factory.registerModule(qt.QFileInfo(candidate.path)) if not factory.isRegistered(candidate.key): failedModules.append(candidate.key) else: newModules.append(candidate.key) if len(failedModules): title = "Customizer" text = "Warning: the following custom modules failed to load:\n\n" text += str(failedModules) qt.QMessageBox.warning(slicer.util.mainWindow(), title, text) else: print("Loaded all new modules correctly!") print("New modules are: " + str(newModules))
def isMimeDataAccepted(self): """Checks the dropped data and returns true if it is one or more directories""" self.directoriesToAdd = [] mimeData = self.qSlicerFileDialog.mimeData() if mimeData.hasFormat('text/uri-list'): urls = mimeData.urls() for url in urls: localPath = url.toLocalFile() # convert QUrl to local path pathInfo = qt.QFileInfo() pathInfo.setFile(localPath) # information about the path if pathInfo.isDir(): # if it is a directory we add the files to the dialog self.directoriesToAdd.append(localPath) self.qSlicerFileDialog.acceptMimeData(len(self.directoriesToAdd) != 0)
def tempDirectory(self,key='__SlicerTestTemp__',tempDir=None,includeDateTime=False): """Come up with a unique directory name in the temp dir and make it and return it # TODO: switch to QTemporaryDir in Qt5. Note: this directory is not automatically cleaned up """ if not tempDir: tempDir = qt.QDir(slicer.app.temporaryPath) tempDirName = key if includeDateTime: key += qt.QDateTime().currentDateTime().toString("yyyy-MM-dd_hh+mm+ss.zzz") fileInfo = qt.QFileInfo(qt.QDir(tempDir), tempDirName) dirPath = fileInfo.absoluteFilePath() qt.QDir().mkpath(dirPath) return dirPath
def addModule(self, fileName, permanent): """ Loads a module in the Slicer factory while Slicer is running """ logging.info('Module addition process started') # Determine which modules in above are not already loaded factory = slicer.app.moduleManager().factoryManager() myModule = type('moduleType', (), {}) myModule.dirPath = os.path.dirname(fileName) myModule.baseName = os.path.basename(fileName) myModule.key, myModule.fileExtension = os.path.splitext( myModule.baseName) if factory.isLoaded(myModule.key): raise Exception("Abort: Module already loaded") if permanent: # Add module(s) to permanent search paths, if requested settings = slicer.app.revisionUserSettings() rawSearchPaths = list( self._settingsList(settings, "Modules/AdditionalPaths")) searchPaths = [qt.QDir(path) for path in rawSearchPaths] modified = False rawPath = myModule.dirPath path = qt.QDir(rawPath) if path not in searchPaths: searchPaths.append(path) rawSearchPaths.append(rawPath) modified = True if modified: settings.setValue("Modules/AdditionalPaths", rawSearchPaths) # Register requested module(s) factory.registerModule(qt.QFileInfo(fileName)) if not factory.isRegistered(myModule.key): raise Exception("Abort: Failed to register module %s", myModule.key) # Instantiate and load requested module(s) if not factory.loadModules([myModule.key]): raise Exception( "Abort: The module factory manager reported an error. \ One or more of the requested module(s) and/or \ dependencies thereof may not have been loaded.") logging.info('Module addition process completed') return True
def onParamertClicked( self, index ): data = self.get('ParameterFileComboBox').itemData(index) if data == self.logic.getParameterFile(): return if not data: data = qt.QFileDialog.getOpenFileName( self.widget, 'Load a parameter file', qt.QDir.home().absolutePath(), '*.mtp') if not data: self.get('ParameterFileComboBox').setCurrentIndex(0) return wasBlocking = self.get('ParameterFileComboBox').blockSignals(True) self.get('ParameterFileComboBox').insertItem(0, qt.QFileInfo(data).baseName(), data) self.get('ParameterFileComboBox').blockSignals(wasBlocking) self.get('ParameterFileComboBox').setCurrentIndex(0) else: self.logic.setParameterFile(data)
def populateParameters( self ): parameterDir = qt.QDir(PARAM_FILES_DIR) for path in parameterDir.entryList(parameterDir.Files): fileInfo = qt.QFileInfo(path) name = fileInfo.baseName().replace("_", " ") self.get('ParameterFileComboBox').addItem(name, fileInfo.absolutePath() + '/' + path) # Add load from file self.get('ParameterFileComboBox').insertSeparator( self.get('ParameterFileComboBox').count) self.get('ParameterFileComboBox').addItem('Load from file...', None) comboBox = self.get('LayoutComboBox') comboBox.addItem('Quad View', slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpView) comboBox.addItem('Red Slice', slicer.vtkMRMLLayoutNode.SlicerLayoutOneUpRedSliceView) comboBox.addItem('Yellow Slice', slicer.vtkMRMLLayoutNode.SlicerLayoutOneUpYellowSliceView) comboBox.addItem('Green Slice', slicer.vtkMRMLLayoutNode.SlicerLayoutOneUpGreenSliceView) comboBox.addItem('3D View', slicer.vtkMRMLLayoutNode.SlicerLayoutOneUp3DView) comboBox.addItem('MIP', slicer.vtkMRMLLayoutNode.SlicerLayoutOneUpRedSliceView)
def loadModules(self, path, depth=1): # Get list of modules in specified path modules = ModuleInfo.findModules(path, depth) # Determine which modules in above are not already loaded factory = slicer.app.moduleManager().factoryManager() loadedModules = factory.instantiatedModuleNames() candidates = [m for m in modules if m.key not in loadedModules] # Prompt to load additional module(s) if len(candidates): dlg = LoadModulesDialog(self.parent.window()) dlg.setModules(candidates) if dlg.exec_() == qt.QDialog.Accepted: modulesToLoad = dlg.selectedModules # Add module(s) to permanent search paths, if requested if dlg.addToSearchPaths: settings = slicer.app.revisionUserSettings() rawSearchPaths = list( settings.value("Modules/AdditionalPaths")) searchPaths = [qt.QDir(path) for path in rawSearchPaths] modified = False for module in modulesToLoad: rawPath = os.path.dirname(module.path) path = qt.QDir(rawPath) if not path in searchPaths: searchPaths.append(path) rawSearchPaths.append(rawPath) modified = True if modified: settings.setValue("Modules/AdditionalPaths", rawSearchPaths) # Register requested module(s) failed = [] for module in modulesToLoad: factory.registerModule(qt.QFileInfo(module.path)) if not factory.isRegistered(module.key): failed.append(module) if len(failed): md = qt.QMessageBox(self.parent.window()) md.icon = qt.QMessageBox.Critical md.standardButtons = qt.QMessageBox.Close md.windowTitle = "Module loading failed" if len(failed) > 1: md.text = "The following modules could not be registered:" else: md.text = "The '%s' module could not be registered:" % failed[ 0].key failedFormat = "<ul><li>%(key)s<br/>(%(path)s)</li></ul>" md.informativeText = "".join( [failedFormat % m.__dict__ for m in failed]) md.exec_() return # Instantiate and load requested module(s) if not factory.loadModules( [module.key for module in modulesToLoad]): md = qt.QMessageBox(self.parent.window()) md.icon = qt.QMessageBox.Critical md.standardButtons = qt.QMessageBox.Close md.windowTitle = "Error loading module(s)" md.text = ( "The module factory manager reported an error. " "One or more of the requested module(s) and/or " "dependencies thereof may not have been loaded.") md.exec_()