nixModule.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import os
  2. import unittest
  3. from __main__ import vtk, qt, ctk, slicer
  4. from slicer.ScriptedLoadableModule import *
  5. #import slicerNetwork
  6. import json
  7. import zipfile
  8. import pathlib
  9. import sys
  10. import urllib3
  11. import string
  12. import chardet
  13. import base64
  14. try:
  15. import nixWrapper
  16. except ModuleNotFoundError:
  17. import subprocess
  18. py='{}PythonSlicer'.format(slicer.app.slicerHome[:-2])
  19. print(f'Executable: {py}')
  20. subprocess.check_call([py, '-m', 'pip', 'install', 'nixWrapper'])
  21. import nixWrapper
  22. #
  23. # labkeySlicerPythonExtension
  24. #
  25. class nixModule(ScriptedLoadableModule):
  26. """Uses ScriptedLoadableModule base class, available at:
  27. https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  28. """
  29. def __init__(self, parent):
  30. ScriptedLoadableModule.__init__(self, parent)
  31. self.parent.title = "nixModule" # TODO make this more human readable by adding spaces
  32. self.parent.categories = ["NIX"]
  33. self.parent.dependencies = []
  34. self.parent.contributors = ["Andrej Studen (UL/FMF)"] # replace with "Firstname Lastname (Organization)"
  35. self.parent.helpText = """
  36. Interface to NIX software
  37. """
  38. self.parent.acknowledgementText = """
  39. Developed within the medical physics research programme of the Slovenian research agency.
  40. """ # replace with organization, grant and thanks.
  41. #
  42. # labkeySlicerPythonExtensionWidget
  43. #
  44. class nixModuleWidget(ScriptedLoadableModuleWidget):
  45. """Uses ScriptedLoadableModuleWidget base class, available at:
  46. https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  47. """
  48. def setup(self):
  49. ScriptedLoadableModuleWidget.setup(self)
  50. # Instantiate and connect widgets ...
  51. self.logic=nixModuleLogic(self)
  52. fhome=os.path.expanduser('~')
  53. #fwrapper=getWrapper('nixWrapper.py')
  54. #p=pathlib.Path(fwrapper)
  55. #sys.path.append(str(p.parent))
  56. self.remoteResources=nixWrapper.getResources()
  57. #self.network=slicerNetwork.labkeyURIHandler()
  58. #
  59. # Parameters Area
  60. #
  61. loadCollapsibleButton = ctk.ctkCollapsibleButton()
  62. loadCollapsibleButton.text = "Add module"
  63. self.layout.addWidget(loadCollapsibleButton)
  64. loadFormLayout = qt.QFormLayout(loadCollapsibleButton)
  65. self.libraryName=qt.QComboBox()
  66. self.libraryName.insertItems(0,[m for m in self.remoteResources])
  67. self.libraryName.currentIndexChanged.connect(self.onLibraryNameChanged)
  68. loadFormLayout.addRow("Library: ", self.libraryName)
  69. self.moduleName=qt.QComboBox()
  70. try:
  71. print('Library: '.format(self.libraryName.currentText))
  72. self.moduleName.insertItems(0,[m for m in self.remoteResources[self.libraryName.currentText]['modules']])
  73. except KeyError:
  74. pass
  75. self.moduleName.currentIndexChanged.connect(self.onModuleNameChanged)
  76. loadFormLayout.addRow("Module: ", self.moduleName)
  77. self.loadLibraryButton=qt.QPushButton("Load")
  78. self.loadLibraryButton.toolTip="Load library"
  79. self.loadLibraryButton.clicked.connect(self.onLoadLibraryButtonClicked)
  80. loadFormLayout.addRow("Load library:",self.loadLibraryButton)
  81. self.loadModuleButton=qt.QPushButton("Load")
  82. self.loadModuleButton.toolTip="Load library"
  83. self.loadModuleButton.clicked.connect(self.onLoadModuleButtonClicked)
  84. loadFormLayout.addRow("Load module:",self.loadModuleButton)
  85. def onLoadModuleButtonClicked(self):
  86. self.logic.loadModule(self.libraryName.currentText,self.moduleName.currentText)
  87. print('onLoadModuleButtonClicked')
  88. def onLoadLibraryButtonClicked(self):
  89. self.logic.loadLibrary(self.libraryName.currentText)
  90. print('onLoadLibraryButtonClicked')
  91. def onModuleNameChanged(self):
  92. pass
  93. def onLibraryNameChanged(self):
  94. self.moduleName.clear()
  95. self.moduleName.insertItem(0,'Select')
  96. self.moduleName.insertItems(1,self.remoteResources[self.libraryName.currentText]['modules'])
  97. # labkeySlicerPythonExtensionLogic
  98. #
  99. class nixModuleLogic(ScriptedLoadableModuleLogic):
  100. """This class should implement all the actual
  101. computation done by your module. The interface
  102. should be such that other python code can import
  103. this class and make use of the functionality without
  104. requiring an instance of the Widget.
  105. Uses ScriptedLoadableModuleLogic base class, available at:
  106. https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  107. """
  108. def __init__(self,parent):
  109. ScriptedLoadableModuleLogic.__init__(self, parent)
  110. def loadModule(self,libName,modName):
  111. # import nixWrapper
  112. nixWrapper.loadModule(slicer,qt,libName,modName)
  113. def loadLibrary(self,libName):
  114. # import nixWrapper
  115. nixWrapper.loadLibrary(libName)
  116. class nixModuleTest(ScriptedLoadableModuleTest):
  117. """
  118. This is the test case for your scripted module.
  119. Uses ScriptedLoadableModuleTest base class, available at:
  120. https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  121. """
  122. def setUp(self):
  123. """ Do whatever is needed to reset the state - typically a scene clear will be enough.
  124. """
  125. slicer.mrmlScene.Clear(0)
  126. def runTest(self):
  127. pass