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;
// }
}
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;
}
Aggregations