Search in sources :

Example 1 with PathInteractivePlugin

use of qupath.lib.plugins.PathInteractivePlugin in project qupath by qupath.

the class QuPathGUI method runPlugin.

/**
 * Run a plugin, interactively (i.e. launching a dialog) if necessary.
 * <p>
 * Note that this does not in itself perform any exception handling.
 *
 * @param plugin the plugin to run
 * @param arg optional string argument (usually JSON)
 * @param doInteractive if true, show an interactive dialog if the plugin is an instance of {@link PathInteractivePlugin}
 * @return true if running the plugin was successful and was not cancelled.
 *              Note that if {@code doInteractive == true} and the dialog was launched
 *              but not run, this will also return true.
 * @throws Exception
 */
public boolean runPlugin(final PathPlugin<BufferedImage> plugin, final String arg, final boolean doInteractive) throws Exception {
    // TODO: Check safety...
    if (doInteractive && plugin instanceof PathInteractivePlugin) {
        PathInteractivePlugin<BufferedImage> pluginInteractive = (PathInteractivePlugin<BufferedImage>) plugin;
        ParameterList params = pluginInteractive.getDefaultParameterList(getImageData());
        // Update parameter list, if necessary
        if (arg != null) {
            Map<String, String> map = GeneralTools.parseArgStringValues(arg);
            // We use the US locale because we need to ensure decimal points (not commas)
            ParameterList.updateParameterList(params, map, Locale.US);
        }
        var runner = new PluginRunnerFX(this);
        ParameterDialogWrapper<BufferedImage> dialog = new ParameterDialogWrapper<>(pluginInteractive, params, runner);
        dialog.showDialog();
        return !runner.isCancelled();
    } else {
        try {
            pluginRunning.set(true);
            var runner = new PluginRunnerFX(this);
            @SuppressWarnings("unused") var completed = plugin.runPlugin(runner, arg);
            return !runner.isCancelled();
        } finally {
            pluginRunning.set(false);
        }
    }
// } catch (Exception e) {
// logger.error("Unable to run plugin " + plugin, e);
// return false;
// }
}
Also used : PathInteractivePlugin(qupath.lib.plugins.PathInteractivePlugin) ParameterList(qupath.lib.plugins.parameters.ParameterList) BufferedImage(java.awt.image.BufferedImage)

Example 2 with PathInteractivePlugin

use of qupath.lib.plugins.PathInteractivePlugin in project qupath by qupath.

the class ParameterDialogWrapper method createDialog.

private Stage createDialog(final PathInteractivePlugin<T> plugin, final ParameterList params, final PluginRunner<T> pluginRunner) {
    panel = new ParameterPanelFX(params);
    panel.getPane().setPadding(new Insets(5, 5, 5, 5));
    // panel.addParameterChangeListener(new ParameterChangeListener() {
    // 
    // @Override
    // public void parameterChanged(ParameterList parameterList, String key, boolean isAdjusting) {
    // 
    // if (!plugin.requestLiveUpdate())
    // return;
    // 
    // PathObjectHierarchy hierarchy = pluginRunner.getHierarchy();
    // if (hierarchy == null)
    // return;
    // 
    // Collection<Class<? extends PathObject>> supportedParents = plugin.getSupportedParentObjectClasses();
    // 
    // PathObject selectedObject = pluginRunner.getSelectedObject();
    // if (selectedObject == null) {
    // if (supportedParents.contains(PathRootObject.class))
    // Collections.singleton(hierarchy.getRootObject());
    // } else if (supportedParents.contains(selectedObject.getClass()))
    // Collections.singleton(selectedObject);
    // }
    // 
    // });
    // final Button btnRun = new Button("Run " + plugin.getName());
    final Button btnRun = new Button("Run");
    btnRun.textProperty().bind(Bindings.createStringBinding(() -> {
        if (btnRun.isDisabled())
            return "Please wait...";
        else
            return "Run";
    }, btnRun.disabledProperty()));
    final Stage dialog = new Stage();
    QuPathGUI qupath = QuPathGUI.getInstance();
    if (qupath != null)
        dialog.initOwner(qupath.getStage());
    dialog.setTitle(plugin.getName());
    final String emptyLabel = " \n";
    final Label label = new Label(emptyLabel);
    label.setStyle("-fx-font-weight: bold;");
    label.setPadding(new Insets(5, 5, 5, 5));
    label.setAlignment(Pos.CENTER);
    label.setTextAlignment(TextAlignment.CENTER);
    btnRun.setOnAction(e -> {
        // Check if we have the parent objects available to make this worthwhile
        if (plugin instanceof PathInteractivePlugin) {
            // // Strip off any of our extra parameters
            // params.removeParameter(KEY_REGIONS);
            boolean alwaysPrompt = plugin.alwaysPromptForObjects();
            ImageData<?> imageData = pluginRunner.getImageData();
            Collection<PathObject> selected = imageData == null ? Collections.emptyList() : imageData.getHierarchy().getSelectionModel().getSelectedObjects();
            Collection<? extends PathObject> parents = PathObjectTools.getSupportedObjects(selected, plugin.getSupportedParentObjectClasses());
            if (alwaysPrompt || parents == null || parents.isEmpty()) {
                if (!ParameterDialogWrapper.promptForParentObjects(pluginRunner, plugin, alwaysPrompt && !parents.isEmpty()))
                    return;
            }
        // promptForParentObjects
        }
        dialog.getScene().setCursor(Cursor.WAIT);
        btnRun.setDisable(true);
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    WorkflowStep lastStep = pluginRunner.getImageData().getHistoryWorkflow().getLastStep();
                    boolean success = plugin.runPlugin(pluginRunner, ParameterList.getParameterListJSON(params, "  "));
                    WorkflowStep lastStepNew = pluginRunner.getImageData().getHistoryWorkflow().getLastStep();
                    if (success && lastStep != lastStepNew)
                        lastWorkflowStep = lastStepNew;
                    else
                        lastWorkflowStep = null;
                } catch (Exception e) {
                    Dialogs.showErrorMessage("Plugin error", e);
                } catch (OutOfMemoryError e) {
                    // This doesn't actually work...
                    Dialogs.showErrorMessage("Out of memory error", "Out of memory - try to close other applications, or decrease the number of parallel processors in the QuPath preferences");
                } finally {
                    Platform.runLater(() -> {
                        QuPathGUI.getInstance().pluginRunningProperty().set(false);
                        dialog.getScene().setCursor(Cursor.DEFAULT);
                        label.setText(plugin.getLastResultsDescription());
                        btnRun.setDisable(false);
                    });
                }
            }
        };
        Thread t = new Thread(runnable, "Plugin thread");
        QuPathGUI.getInstance().pluginRunningProperty().set(true);
        t.start();
    });
    BorderPane pane = new BorderPane();
    ScrollPane scrollPane = new ScrollPane();
    label.setMaxWidth(Double.MAX_VALUE);
    scrollPane.setContent(panel.getPane());
    scrollPane.setFitToWidth(true);
    pane.setCenter(scrollPane);
    btnRun.setMaxWidth(Double.MAX_VALUE);
    btnRun.setPadding(new Insets(5, 5, 5, 5));
    pane.setBottom(btnRun);
    Scene scene = new Scene(pane);
    dialog.setScene(scene);
    // Request focus, to make it easier to run from the keyboard
    btnRun.requestFocus();
    dialog.sizeToScene();
    return dialog;
}
Also used : BorderPane(javafx.scene.layout.BorderPane) Insets(javafx.geometry.Insets) WorkflowStep(qupath.lib.plugins.workflow.WorkflowStep) Label(javafx.scene.control.Label) Scene(javafx.scene.Scene) ParameterPanelFX(qupath.lib.gui.dialogs.ParameterPanelFX) PathInteractivePlugin(qupath.lib.plugins.PathInteractivePlugin) PathObject(qupath.lib.objects.PathObject) Button(javafx.scene.control.Button) ScrollPane(javafx.scene.control.ScrollPane) Stage(javafx.stage.Stage)

Aggregations

PathInteractivePlugin (qupath.lib.plugins.PathInteractivePlugin)2 BufferedImage (java.awt.image.BufferedImage)1 Insets (javafx.geometry.Insets)1 Scene (javafx.scene.Scene)1 Button (javafx.scene.control.Button)1 Label (javafx.scene.control.Label)1 ScrollPane (javafx.scene.control.ScrollPane)1 BorderPane (javafx.scene.layout.BorderPane)1 Stage (javafx.stage.Stage)1 ParameterPanelFX (qupath.lib.gui.dialogs.ParameterPanelFX)1 PathObject (qupath.lib.objects.PathObject)1 ParameterList (qupath.lib.plugins.parameters.ParameterList)1 WorkflowStep (qupath.lib.plugins.workflow.WorkflowStep)1