Skip to content

Commit

Permalink
up up
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnnsrs committed Nov 8, 2023
1 parent 289c514 commit 804a808
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 28 deletions.
6 changes: 6 additions & 0 deletions mikroj/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class ImageJBridge(BaseModel):
def set_ij_instance(self, ij: ImageJ):
self._ij = ij

def stop_ij_instance(self):
if self._ij:
self._ij.ui().dispose()
del self._ij
self._ij = None

def get_imageplus_roi_id(self, id):
imp = self.ij.WindowManager.getImage(id)
return imp
Expand Down
94 changes: 66 additions & 28 deletions mikroj/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@
)


class InitWorker(QtCore.QObject):
finished = QtCore.Signal()
initialized = QtCore.Signal(object)
progress = QtCore.Signal(str)
errored = QtCore.Signal(Exception)

def __init__(self, image_j_path):
super().__init__()
self.image_j_path = image_j_path

def run(self):
"""Long-running task."""
self.progress.emit("Initializing...")

try:
path = (
os.getcwd()
) # This is a hack until https://github.com/imagej/pyimagej/issues/150
self._ij = imagej.init(self.image_j_path, mode="interactive")
self.progress.emit("Initialized")
os.chdir(path) ##
self._ij.ui().showUI()

self.progress.emit("ImageJ Initialized")
self.initialized.emit(self._ij)
self.finished.emit()

except Exception as e:
self.image_j_path = None
self.progress.emit("ImageJ Failed to Initialize")
self.errored.emit(e)


class MikroJ(QtWidgets.QWidget):
bridge: Optional[ImageJBridge] = None

Expand Down Expand Up @@ -151,15 +184,12 @@ def __init__(self, *args, auto_init=None, **kwargs) -> None:

self.setLayout(self.vlayout)
self.magic_bar.magicb.setDisabled(True)

if self.image_j_path and self.auto_initialize:
self.initialize()

def request_imagej_dir(self):
if self._ij:
self.magic_bar.magicb.setDisabled(True)
self._ij.ui().dispose()
del self._ij
self._ij = None
self.bridge.stop_ij_instance()
dir = QtWidgets.QFileDialog.getExistingDirectory(
parent=self, caption="Select ImageJ directory"
)
Expand Down Expand Up @@ -443,7 +473,23 @@ def get_active_rois(

return arguments

def on_progress(self, progress: str):
self.imagej_button.setText(progress)

def on_finished(self, ij):
self.bridge.set_ij_instance(ij)

self.magic_bar.magicb.setDisabled(False)
self.imagej_button.setText("ImageJ Initialized")

def on_error(self, e):
self.show_exception(e)

def initialize(self):
print("Initializing...")
self.imagej_button.setDisabled(True)
self.imagej_button.setText("Initializing...")

if not self.image_j_path:
self.magic_bar.magicb.setDisabled(True)
self.request_imagej_dir()
Expand All @@ -452,29 +498,20 @@ def initialize(self):
# scyjava.config.add_option(f"-Dplugins.dir={self.plugins_dir}")
pass

try:
self.imagej_button.setDisabled(True)
self.imagej_button.setText("Initializing...")
path = (
os.getcwd()
) # This is a hack until https://github.com/imagej/pyimagej/issues/150
self._ij = imagej.init(self.image_j_path, mode="interactive")
os.chdir(path) ##
self.magic_bar.magicb.setDisabled(False)

self.vlayout.update()

self.bridge.set_ij_instance(self._ij)
self._ij.ui().showUI()
self.magic_bar.magicb.setDisabled(False)
self.imagej_button.setText("ImageJ Initialized")

except Exception as e:
self.image_j_path = None
self.imagej_button.setText("ImageJ Failed to Initialize")
self.magic_bar.magicb.setDisabled(True)
self.imagej_button.setDisabled(False)
self.show_exception(e)
self.thread = QtCore.QThread()
self.worker = InitWorker(self.image_j_path)
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.run)
self.worker.initialized.connect(self.on_finished)
self.worker.finished.connect(self.thread.quit)
self.worker.errored.connect(self.thread.quit)
self.worker.errored.connect(self.on_error)
self.worker.finished.connect(self.worker.deleteLater)
self.worker.errored.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.worker.progress.connect(self.on_progress)
self.thread.start()

def show_exception(self, exception: Exception):
msg = QMessageBox()
Expand All @@ -496,6 +533,7 @@ def show_exception(self, exception: Exception):
raise exception
except Exception:
traceback.print_exc()
pass


def main(run_packaged=False, **kwargs):
Expand Down

0 comments on commit 804a808

Please sign in to comment.