def initializeDICOMDatabase(self): # Create alias for convenience slicer.dicomDatabase = slicer.app.dicomDatabase() # Set the dicom pre-cache tags once all plugin classes have been initialized. # Pre-caching tags is very important for fast DICOM loading because tags that are # not pre-cached during DICOM import in bulk, will be cached during Examine step one-by-one # (which takes magnitudes more time). tagsToPrecache = list(slicer.dicomDatabase.tagsToPrecache) for pluginClass in slicer.modules.dicomPlugins: plugin = slicer.modules.dicomPlugins[pluginClass]() tagsToPrecache += list(plugin.tags.values()) tagsToPrecache = sorted(set(tagsToPrecache)) # remove duplicates slicer.dicomDatabase.tagsToPrecache = tagsToPrecache # Try to initialize the database using the location stored in settings if slicer.app.commandOptions().testingEnabled: # For automatic tests (use a separate DICOM database for testing) slicer.dicomDatabaseDirectorySettingsKey = 'DatabaseDirectoryTest_' + ctk.ctkDICOMDatabase( ).schemaVersion() databaseDirectory = os.path.join( slicer.app.temporaryPath, 'temp' + slicer.app.applicationName + 'DICOMDatabase_' + ctk.ctkDICOMDatabase().schemaVersion()) else: # For production slicer.dicomDatabaseDirectorySettingsKey = 'DatabaseDirectory_' + ctk.ctkDICOMDatabase( ).schemaVersion() settings = qt.QSettings() databaseDirectory = settings.value( slicer.dicomDatabaseDirectorySettingsKey) if not databaseDirectory: documentsLocation = qt.QStandardPaths.DocumentsLocation documents = qt.QStandardPaths.writableLocation( documentsLocation) databaseDirectory = os.path.join( documents, slicer.app.applicationName + "DICOMDatabase") settings.setValue(slicer.dicomDatabaseDirectorySettingsKey, databaseDirectory) # Attempt to open the database. If it fails then user will have to configure it using DICOM module. databaseFileName = databaseDirectory + "/ctkDICOM.sql" slicer.dicomDatabase.openDatabase(databaseFileName) if slicer.dicomDatabase.isOpen: # There is an existing database at the current location if slicer.dicomDatabase.schemaVersionLoaded( ) != slicer.dicomDatabase.schemaVersion(): # Schema does not match, do not use it slicer.dicomDatabase.closeDatabase()
def performPostModuleDiscoveryTasks(self): """Since dicom plugins are discovered while the application is initialized, they may be found after the DICOM module itself if initialized. This method is tied to a singleShot that will be called once the event loop is read to start. """ if slicer.mrmlScene.GetTagByClassName( "vtkMRMLScriptedModuleNode") != 'ScriptedModule': slicer.mrmlScene.RegisterNodeClass(vtkMRMLScriptedModuleNode()) # initialize the dicom infrastructure slicer.dicomDatabase = None settings = qt.QSettings() # the dicom database is a global object for slicer if settings.contains('DatabaseDirectory'): databaseDirectory = settings.value('DatabaseDirectory') if databaseDirectory: slicer.dicomDatabase = ctk.ctkDICOMDatabase() slicer.dicomDatabase.openDatabase( databaseDirectory + "/ctkDICOM.sql", "SLICER") if not slicer.dicomDatabase.isOpen: # can't open the database, so prompt the user later if they enter module slicer.dicomDatabase = None else: self.startListener() if slicer.dicomDatabase: slicer.app.setDICOMDatabase(slicer.dicomDatabase) # set the dicom pre-cache tags once all plugin classes have been initialized DICOMLib.setDatabasePrecacheTags() if not slicer.app.commandOptions().noMainWindow: # add to the main app file menu self.addMenu() # add the settings options self.settingsPanel = DICOMSettingsPanel() slicer.app.settingsDialog().addPanel("DICOM", self.settingsPanel) dataProbe = slicer.util.mainWindow().findChild( "QWidget", "DataProbeCollapsibleWidget") self.wasDataProbeVisible = dataProbe.isVisible() layoutManager = slicer.app.layoutManager() layoutManager.layoutChanged.connect(self.onLayoutChanged) layout = ("<layout type=\"horizontal\">" " <item>" " <dicombrowser></dicombrowser>" " </item>" "</layout>") layoutNode = slicer.app.layoutManager().layoutLogic( ).GetLayoutNode() layoutNode.AddLayoutDescription( slicer.vtkMRMLLayoutNode.SlicerLayoutDicomBrowserView, layout) self.currentViewArrangement = layoutNode.GetViewArrangement() self.previousViewArrangement = layoutNode.GetViewArrangement() self.detailsPopup = None self.viewWidget = None self.browserSettingsWidget = None self.setDetailsPopup(DICOMWidget.getSavedDICOMDetailsWidgetType()())
def createTemporaryDatabase(directory=None): """ Open temporary DICOM database, return new database object """ # Specify temporary directory if not directory or directory == '': from time import gmtime, strftime directory = strftime("%Y%m%d_%H%M%S_", gmtime()) + 'TempDICOMDatabase' if os.path.isabs(directory): tempDatabaseDir = directory else: tempDatabaseDir = slicer.app.temporaryPath + '/' + directory logging.info('Switching to temporary DICOM database: ' + tempDatabaseDir) if not os.access(tempDatabaseDir, os.F_OK): qt.QDir().mkpath(tempDatabaseDir) databaseFileName = tempDatabaseDir + "/ctkDICOM.sql" dicomDatabase = ctk.ctkDICOMDatabase() dicomDatabase.openDatabase(databaseFileName) if dicomDatabase.isOpen: if dicomDatabase.schemaVersionLoaded() != dicomDatabase.schemaVersion(): dicomDatabase.closeDatabase() if dicomDatabase.isOpen: return dicomDatabase else: return None
def performPostModuleDiscoveryTasks(self): """Since dicom plugins are discovered while the application is initialized, they may be found after the DICOM module itself if initialized. This method is tied to a singleShot that will be called once the event loop is read to start. """ if slicer.mrmlScene.GetTagByClassName( "vtkMRMLScriptedModuleNode" ) != 'ScriptedModule': slicer.mrmlScene.RegisterNodeClass(vtkMRMLScriptedModuleNode()) # initialize the dicom infrastructure slicer.dicomDatabase = None settings = qt.QSettings() # the dicom database is a global object for slicer if settings.contains('DatabaseDirectory'): databaseDirectory = settings.value('DatabaseDirectory') if databaseDirectory: slicer.dicomDatabase = ctk.ctkDICOMDatabase() slicer.dicomDatabase.openDatabase(databaseDirectory + "/ctkDICOM.sql", "SLICER") if not slicer.dicomDatabase.isOpen: # can't open the database, so prompt the user later if they enter module slicer.dicomDatabase = None else: self.startListener() if slicer.dicomDatabase: slicer.app.setDICOMDatabase(slicer.dicomDatabase) # set the dicom pre-cache tags once all plugin classes have been initialized DICOMLib.setDatabasePrecacheTags() if not slicer.app.commandOptions().noMainWindow: # add to the main app file menu self.addMenu() # add the settings options self.settingsPanel = DICOMSettingsPanel() slicer.app.settingsDialog().addPanel("DICOM", self.settingsPanel)
def __init__(self, parent): ScriptedLoadableModule.__init__(self, parent) import string self.parent.title = "DICOM" self.parent.categories = ["", "Informatics"] # top level module self.parent.contributors = ["Steve Pieper (Isomics)"] self.parent.helpText = string.Template(""" The DICOM module integrates DICOM classes from CTK (based on DCMTK). See <a href=\"$a/Documentation/$b.$c/Modules/DICOM\">the documentaiton</a> for more information. """).substitute({ 'a': parent.slicerWikiUrl, 'b': slicer.app.majorVersion, 'c': slicer.app.minorVersion }) self.parent.acknowledgementText = """ This work is supported by NA-MIC, NAC, BIRN, NCIGT, and the Slicer Community. See <a href=http://www.slicer.org>http://www.slicer.org</a> for details. Module implemented by Steve Pieper. Based on work from CommonTK (http://www.commontk.org). """ self.parent.icon = qt.QIcon(':Icons/Medium/SlicerLoadDICOM.png') self.parent.dependencies = ["SubjectHierarchy"] if slicer.mrmlScene.GetTagByClassName( "vtkMRMLScriptedModuleNode") != 'ScriptedModule': slicer.mrmlScene.RegisterNodeClass(vtkMRMLScriptedModuleNode()) # initialize the dicom infrastructure slicer.dicomDatabase = None settings = qt.QSettings() # the dicom database is a global object for slicer if settings.contains('DatabaseDirectory'): databaseDirectory = settings.value('DatabaseDirectory') if databaseDirectory: slicer.dicomDatabase = ctk.ctkDICOMDatabase() slicer.dicomDatabase.openDatabase( databaseDirectory + "/ctkDICOM.sql", "SLICER") if not slicer.dicomDatabase.isOpen: # can't open the database, so prompt the user later if they enter module slicer.dicomDatabase = None else: # the dicom listener is also global, but only started on app start if # the user so chooses if settings.contains('DICOM/RunListenerAtStart'): if settings.value( 'DICOM/RunListenerAtStart') == 'true': if not hasattr(slicer, 'dicomListener'): try: slicer.dicomListener = DICOMLib.DICOMListener( slicer.dicomDatabase) slicer.dicomListener.start() except (UserWarning, OSError) as message: logging.error( 'Problem trying to start DICOMListener:\n %s' % message) if slicer.dicomDatabase: slicer.app.setDICOMDatabase(slicer.dicomDatabase) # Trigger the menu to be added when application has started up if not slicer.app.commandOptions().noMainWindow: qt.QTimer.singleShot(0, self.addMenu) # set the dicom pre-cache tags once all plugin classes have been initialized qt.QTimer.singleShot(0, DICOMLib.setDatabasePrecacheTags)
def __init__(self, parent): ScriptedLoadableModule.__init__(self, parent) import string self.parent.title = "DICOM" self.parent.categories = ["", "Informatics"] # top level module self.parent.contributors = ["Steve Pieper (Isomics)"] self.parent.helpText = """ The DICOM module integrates DICOM classes from CTK (based on DCMTK). """ self.parent.helpText += self.getDefaultModuleDocumentationLink() self.parent.acknowledgementText = """ This work is supported by NA-MIC, NAC, BIRN, NCIGT, and the Slicer Community. See <a href=http://www.slicer.org>http://www.slicer.org</a> for details. Module implemented by Steve Pieper. Based on work from CommonTK (http://www.commontk.org). """ self.parent.icon = qt.QIcon(':Icons/Medium/SlicerLoadDICOM.png') self.parent.dependencies = ["SubjectHierarchy"] if slicer.mrmlScene.GetTagByClassName( "vtkMRMLScriptedModuleNode") != 'ScriptedModule': slicer.mrmlScene.RegisterNodeClass(vtkMRMLScriptedModuleNode()) # initialize the dicom infrastructure slicer.dicomDatabase = None settings = qt.QSettings() # the dicom database is a global object for slicer if settings.contains('DatabaseDirectory'): databaseDirectory = settings.value('DatabaseDirectory') if databaseDirectory: slicer.dicomDatabase = ctk.ctkDICOMDatabase() slicer.dicomDatabase.openDatabase( databaseDirectory + "/ctkDICOM.sql", "SLICER") if not slicer.dicomDatabase.isOpen: # can't open the database, so prompt the user later if they enter module slicer.dicomDatabase = None else: # the dicom listener is also global, but only started on app start if # the user so chooses if settings.contains('DICOM/RunListenerAtStart'): if settings.value( 'DICOM/RunListenerAtStart') == 'true': if not hasattr(slicer, 'dicomListener'): try: slicer.dicomListener = DICOMLib.DICOMListener( slicer.dicomDatabase) slicer.dicomListener.start() except (UserWarning, OSError) as message: logging.error( 'Problem trying to start DICOMListener:\n %s' % message) if slicer.dicomDatabase: slicer.app.setDICOMDatabase(slicer.dicomDatabase) # Tasks to execute after the application has started up slicer.app.connect("startupCompleted()", self.performPostModuleDiscoveryTasks)
def __init__(self, parent): ScriptedLoadableModule.__init__(self, parent) import string self.parent.title = "DICOM" self.parent.categories = ["", "Informatics"] # top level module self.parent.contributors = ["Steve Pieper (Isomics)"] self.parent.helpText = """ The DICOM module integrates DICOM classes from CTK (based on DCMTK). """ self.parent.helpText += self.getDefaultModuleDocumentationLink() self.parent.acknowledgementText = """ This work is supported by NA-MIC, NAC, BIRN, NCIGT, and the Slicer Community. See <a href=http://www.slicer.org>http://www.slicer.org</a> for details. Module implemented by Steve Pieper. Based on work from CommonTK (http://www.commontk.org). """ self.parent.icon = qt.QIcon(':Icons/Medium/SlicerLoadDICOM.png') self.parent.dependencies = ["SubjectHierarchy"] if slicer.mrmlScene.GetTagByClassName( "vtkMRMLScriptedModuleNode" ) != 'ScriptedModule': slicer.mrmlScene.RegisterNodeClass(vtkMRMLScriptedModuleNode()) # initialize the dicom infrastructure slicer.dicomDatabase = None settings = qt.QSettings() # the dicom database is a global object for slicer if settings.contains('DatabaseDirectory'): databaseDirectory = settings.value('DatabaseDirectory') if databaseDirectory: slicer.dicomDatabase = ctk.ctkDICOMDatabase() slicer.dicomDatabase.openDatabase(databaseDirectory + "/ctkDICOM.sql", "SLICER") if not slicer.dicomDatabase.isOpen: # can't open the database, so prompt the user later if they enter module slicer.dicomDatabase = None else: # the dicom listener is also global, but only started on app start if # the user so chooses if settings.contains('DICOM/RunListenerAtStart'): if settings.value('DICOM/RunListenerAtStart') == 'true': if not hasattr(slicer, 'dicomListener'): try: slicer.dicomListener = DICOMLib.DICOMListener(slicer.dicomDatabase) slicer.dicomListener.start() except (UserWarning,OSError) as message: logging.error('Problem trying to start DICOMListener:\n %s' % message) if slicer.dicomDatabase: slicer.app.setDICOMDatabase(slicer.dicomDatabase) # Trigger the menu to be added when application has started up if not slicer.app.commandOptions().noMainWindow : qt.QTimer.singleShot(0, self.addMenu) # set the dicom pre-cache tags once all plugin classes have been initialized qt.QTimer.singleShot(0, DICOMLib.setDatabasePrecacheTags)
def onDatabaseDirectoryChanged(self,databaseDirectory): if not hasattr(slicer, 'dicomDatabase') or not slicer.dicomDatabase: slicer.dicomDatabase = ctk.ctkDICOMDatabase() setDatabasePrecacheTags(self.dicomBrowser) databaseFilepath = databaseDirectory + "/ctkDICOM.sql" messages = "" if not os.path.exists(databaseDirectory): try: os.mkdir(databaseDirectory) except OSError: messages += "Directory does not exist and cannot be created. " else: if not os.access(databaseDirectory, os.W_OK): messages += "Directory not writable. " if not os.access(databaseDirectory, os.R_OK): messages += "Directory not readable. " if os.listdir(databaseDirectory) and not os.path.isfile(databaseFilepath): # Prevent users from the error of trying to import a DICOM directory by selecting it as DICOM database path messages += "Directory is not empty and not an existing DICOM database." if messages != "": slicer.util.warningDisplay('The database file path "%s" cannot be used. %s\n' 'Please pick a different database directory using the ' 'LocalDatabase button in the DICOM Browser' % (databaseFilepath,messages), windowTitle="DICOM") else: slicer.dicomDatabase.openDatabase(databaseDirectory + "/ctkDICOM.sql", "SLICER") if not slicer.dicomDatabase.isOpen: slicer.util.warningDisplay('The database file path "%s" cannot be opened.\n' 'Please pick a different database directory using the ' 'LocalDatabase button in the DICOM Browser.' % databaseFilepath, windowTitle="DICOM") self.dicomDatabase = None else: if self.dicomBrowser: if self.dicomBrowser.databaseDirectory != databaseDirectory: self.dicomBrowser.databaseDirectory = databaseDirectory else: settings = qt.QSettings() settings.setValue('DatabaseDirectory', databaseDirectory) settings.sync() if slicer.dicomDatabase: slicer.app.setDICOMDatabase(slicer.dicomDatabase)
def performPostModuleDiscoveryTasks(self): """Since dicom plugins are discovered while the application is initialized, they may be found after the DICOM module itself if initialized. This method is tied to a singleShot that will be called once the event loop is read to start. """ if slicer.mrmlScene.GetTagByClassName( "vtkMRMLScriptedModuleNode") != 'ScriptedModule': slicer.mrmlScene.RegisterNodeClass(vtkMRMLScriptedModuleNode()) # initialize the dicom infrastructure slicer.dicomDatabase = None settings = qt.QSettings() # the dicom database is a global object for slicer if settings.contains('DatabaseDirectory'): databaseDirectory = settings.value('DatabaseDirectory') if databaseDirectory: slicer.dicomDatabase = ctk.ctkDICOMDatabase() slicer.dicomDatabase.openDatabase( databaseDirectory + "/ctkDICOM.sql", "SLICER") if not slicer.dicomDatabase.isOpen: # can't open the database, so prompt the user later if they enter module slicer.dicomDatabase = None else: self.startListener() if slicer.dicomDatabase: slicer.app.setDICOMDatabase(slicer.dicomDatabase) # set the dicom pre-cache tags once all plugin classes have been initialized DICOMLib.setDatabasePrecacheTags() if not slicer.app.commandOptions().noMainWindow: # add to the main app file menu self.addMenu() # add the settings options self.settingsPanel = DICOMSettingsPanel() slicer.app.settingsDialog().addPanel("DICOM", self.settingsPanel)