diff --git a/src/main/java/qupath/ext/wsinfer/models/WSInferUtils.java b/src/main/java/qupath/ext/wsinfer/models/WSInferUtils.java index 59d86d3..f675849 100644 --- a/src/main/java/qupath/ext/wsinfer/models/WSInferUtils.java +++ b/src/main/java/qupath/ext/wsinfer/models/WSInferUtils.java @@ -32,6 +32,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; import java.util.Objects; import java.util.ResourceBundle; @@ -66,15 +67,20 @@ public static WSInferModelCollection getModelCollection() { cachedModelCollection = downloadModelCollection(); } } - String localModelDirectory = WSInferPrefs.localDirectoryProperty().get(); - if (localModelDirectory != null) { - addLocalModels(cachedModelCollection, localModelDirectory); + String pathModels = WSInferPrefs.modelDirectoryProperty().get(); + if (pathModels != null) { + File dirModels = new File(pathModels); + if (dirModels.isDirectory()) { + // Search for local models in 'local' or 'user' subdirectories + for (var name : Arrays.asList("local", "user")) { + addLocalModels(cachedModelCollection, new File(dirModels, name)); + } + } } return cachedModelCollection; } - private static void addLocalModels(WSInferModelCollection cachedModelCollection, String localModelDirectory) { - File modelDir = new File(localModelDirectory); + private static void addLocalModels(WSInferModelCollection cachedModelCollection, File modelDir) { if (!modelDir.exists() || !modelDir.isDirectory()) { return; } diff --git a/src/main/java/qupath/ext/wsinfer/ui/WSInferCommand.java b/src/main/java/qupath/ext/wsinfer/ui/WSInferCommand.java index 72cab15..99c6f7e 100644 --- a/src/main/java/qupath/ext/wsinfer/ui/WSInferCommand.java +++ b/src/main/java/qupath/ext/wsinfer/ui/WSInferCommand.java @@ -44,6 +44,7 @@ public class WSInferCommand implements Runnable { private final ResourceBundle resources = ResourceBundle.getBundle("qupath.ext.wsinfer.ui.strings"); private Stage stage; + private WSInferController controller; public WSInferCommand(QuPathGUI qupath) { this.qupath = qupath; @@ -62,8 +63,10 @@ public void run() { logger.error(e.getMessage(), e); return; } - } else + } else { + controller.refreshAvailableModels(); stage.show(); + } } private Stage createStage() throws IOException { @@ -77,6 +80,7 @@ private Stage createStage() throws IOException { var loader = new FXMLLoader(url, resources); loader.setClassLoader(this.getClass().getClassLoader()); VBox root = loader.load(); + controller = loader.getController(); // There's probably a better approach... but wrapping in a border pane // helped me get the resizing to behave diff --git a/src/main/java/qupath/ext/wsinfer/ui/WSInferController.java b/src/main/java/qupath/ext/wsinfer/ui/WSInferController.java index ba2b38b..cbb355f 100644 --- a/src/main/java/qupath/ext/wsinfer/ui/WSInferController.java +++ b/src/main/java/qupath/ext/wsinfer/ui/WSInferController.java @@ -39,6 +39,7 @@ import javafx.scene.control.Spinner; import javafx.scene.control.TextField; import javafx.scene.control.ToggleButton; +import javafx.scene.input.MouseEvent; import javafx.scene.web.WebView; import javafx.stage.Stage; import javafx.util.StringConverter; @@ -52,7 +53,6 @@ import org.controlsfx.control.action.ActionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import qupath.ext.wsinfer.ProgressListener; import qupath.ext.wsinfer.WSInfer; import qupath.ext.wsinfer.models.WSInferModel; import qupath.ext.wsinfer.models.WSInferModelCollection; @@ -64,6 +64,7 @@ import qupath.lib.common.ThreadTools; import qupath.lib.gui.QuPathGUI; import qupath.lib.gui.commands.Commands; +import qupath.lib.gui.tools.GuiTools; import qupath.lib.gui.tools.WebViews; import qupath.lib.images.ImageData; import qupath.lib.objects.PathAnnotationObject; @@ -129,8 +130,6 @@ public class WSInferController { private Spinner spinnerBatchSize; @FXML private TextField tfModelDirectory; - @FXML - private TextField localModelDirectory; private WebView infoWebView = WebViews.create(true); private PopOver infoPopover = new PopOver(infoWebView); @@ -150,7 +149,7 @@ private void initialize() { this.qupath = QuPathGUI.getInstance(); this.imageDataProperty.bind(qupath.imageDataProperty()); - configureModelChoices(); + refreshAvailableModels(); configureSelectionButtons(); configureDisplayToggleButtons(); @@ -186,7 +185,7 @@ private void configureMessageLabel() { /** * Populate the available models & configure the UI elements to select and download models. */ - private void configureModelChoices() { + void refreshAvailableModels() { WSInferModelCollection models = WSInferUtils.getModelCollection(); modelChoiceBox.getItems().setAll(models.getModels().values()); modelChoiceBox.setConverter(new ModelStringConverter(models)); @@ -273,8 +272,6 @@ private void configureActionToggleButton(Action action, ToggleButton button) { private void configureModelDirectory() { tfModelDirectory.textProperty().bindBidirectional(WSInferPrefs.modelDirectoryProperty()); - localModelDirectory.textProperty().bindBidirectional(WSInferPrefs.localDirectoryProperty()); - localModelDirectory.textProperty().addListener((v, o, n) -> configureModelChoices()); } private void configureNumWorkers() { @@ -375,12 +372,25 @@ public void downloadModel() { }); } - public void promptForModelDirectory() { - promptToUpdateDirectory(WSInferPrefs.modelDirectoryProperty()); + /** + * Open the model directory in the system file browser when double-clicked. + * @param event + */ + public void handleModelDirectoryLabelClick(MouseEvent event) { + if (event.getClickCount() != 2) + return; + var path = WSInferPrefs.modelDirectoryProperty().get(); + if (path == null || path.isEmpty()) + return; + var file = new File(path); + if (file.exists()) + GuiTools.browseDirectory(file); + else + logger.debug("Can't browse directory for {}", file); } - public void promptForLocalModelDirectory() { - promptToUpdateDirectory(WSInferPrefs.localDirectoryProperty()); + public void promptForModelDirectory() { + promptToUpdateDirectory(WSInferPrefs.modelDirectoryProperty()); } private void promptToUpdateDirectory(StringProperty dirPath) { diff --git a/src/main/java/qupath/ext/wsinfer/ui/WSInferPrefs.java b/src/main/java/qupath/ext/wsinfer/ui/WSInferPrefs.java index 063a4d2..e70a6eb 100644 --- a/src/main/java/qupath/ext/wsinfer/ui/WSInferPrefs.java +++ b/src/main/java/qupath/ext/wsinfer/ui/WSInferPrefs.java @@ -49,10 +49,6 @@ public class WSInferPrefs { 4 ).asObject(); - private static StringProperty localDirectoryProperty = PathPrefs.createPersistentPreference( - "wsinfer.localDirectory", - null); - /** * String storing the preferred directory to cache models. */ @@ -81,13 +77,6 @@ public static Property batchSizeProperty() { return batchSizeProperty; } - /** - * String storing the preferred directory for user-supplied model folders. - */ - public static StringProperty localDirectoryProperty() { - return localDirectoryProperty; - } - private static Path getUserDir() { Path userPath = UserDirectoryManager.getInstance().getUserPath(); Path cachePath = Paths.get(System.getProperty("user.dir"), ".cache", "QuPath"); diff --git a/src/main/resources/qupath/ext/wsinfer/ui/wsinfer_control.fxml b/src/main/resources/qupath/ext/wsinfer/ui/wsinfer_control.fxml index 572c64c..20bbfca 100644 --- a/src/main/resources/qupath/ext/wsinfer/ui/wsinfer_control.fxml +++ b/src/main/resources/qupath/ext/wsinfer/ui/wsinfer_control.fxml @@ -80,10 +80,10 @@ - - - - + + + + @@ -100,7 +100,7 @@