use of qupath.lib.gui.QuPathGUI in project qupath by qupath.
the class ImportObjectsCommand method runObjectImport.
/**
* Import objects or ROIs and adds them to the current hierarchy.
*
* @param qupath
* @return success
* @throws IOException
*/
public static boolean runObjectImport(QuPathGUI qupath) throws IOException {
var imageData = qupath.getImageData();
if (imageData == null)
return false;
var file = Dialogs.promptForFile("Choose file to import", null, "QuPath objects", PathIO.getObjectFileExtensions(true).toArray(String[]::new));
// User cancel
if (file == null)
return false;
List<PathObject> objs;
try {
objs = PathIO.readObjects(file);
} catch (IOException | IllegalArgumentException ex) {
Dialogs.showErrorNotification("Error importing objects", ex.getLocalizedMessage());
return false;
}
if (objs.isEmpty()) {
Dialogs.showWarningNotification("Import objects", "No objects found in " + file.getAbsolutePath());
return false;
}
int nObjects = objs.stream().mapToInt(p -> 1 + PathObjectTools.countDescendants(p)).sum();
String message = nObjects == 1 ? "Add 1 object to the hierarchy?" : String.format("Add %d objects to the hierarchy?", nObjects);
var confirm = Dialogs.showConfirmDialog("Add to hierarchy", message);
if (!confirm)
return false;
var hierarchy = imageData.getHierarchy();
hierarchy.addPathObjects(objs);
Map<String, String> map = new HashMap<>();
map.put("path", file.getPath());
String method = "Import objects";
String methodString = String.format("%s(%s%s%s)", "importObjectsFromFile", "\"", GeneralTools.escapeFilePath(file.getPath()), "\"");
imageData.getHistoryWorkflow().addStep(new DefaultScriptableWorkflowStep(method, map, methodString));
return true;
}
use of qupath.lib.gui.QuPathGUI in project qupath by qupath.
the class TMASummaryViewer method setTMAEntriesFromOpenImage.
private void setTMAEntriesFromOpenImage() {
QuPathGUI qupath = QuPathGUI.getInstance();
if (qupath == null || qupath.getImageData() == null || qupath.getImageData().getHierarchy().getTMAGrid() == null) {
Dialogs.showErrorMessage("Show TMA summary", "No TMA data available!");
return;
}
ImageData<BufferedImage> imageData = qupath.getImageData();
setTMAEntriesFromImageData(imageData);
}
use of qupath.lib.gui.QuPathGUI in project qupath by qupath.
the class GuiTools method createAnnotationsMenuImpl.
private static void createAnnotationsMenuImpl(QuPathGUI qupath, Object menu) {
// Add annotation options
CheckMenuItem miLockAnnotations = new CheckMenuItem("Lock");
CheckMenuItem miUnlockAnnotations = new CheckMenuItem("Unlock");
miLockAnnotations.setOnAction(e -> setSelectedAnnotationLock(qupath.getImageData(), true));
miUnlockAnnotations.setOnAction(e -> setSelectedAnnotationLock(qupath.getImageData(), false));
MenuItem miSetProperties = new MenuItem("Set properties");
miSetProperties.setOnAction(e -> {
var hierarchy = qupath.getViewer().getHierarchy();
if (hierarchy != null)
GuiTools.promptToSetActiveAnnotationProperties(hierarchy);
});
var actionInsertInHierarchy = qupath.createImageDataAction(imageData -> Commands.insertSelectedObjectsInHierarchy(imageData));
actionInsertInHierarchy.setText("Insert in hierarchy");
var miInsertHierarchy = ActionTools.createMenuItem(actionInsertInHierarchy);
var actionMerge = qupath.createImageDataAction(imageData -> Commands.mergeSelectedAnnotations(imageData));
actionMerge.setText("Merge selected");
var actionSubtract = qupath.createImageDataAction(imageData -> Commands.combineSelectedAnnotations(imageData, CombineOp.SUBTRACT));
actionSubtract.setText("Subtract selected");
var actionIntersect = qupath.createImageDataAction(imageData -> Commands.combineSelectedAnnotations(imageData, CombineOp.INTERSECT));
actionIntersect.setText("Intersect selected");
var actionInverse = qupath.createImageDataAction(imageData -> Commands.makeInverseAnnotation(imageData));
actionInverse.setText("Make inverse");
Menu menuCombine = MenuTools.createMenu("Edit multiple", actionMerge, // TODO: Make this less ambiguous!
actionSubtract, actionIntersect);
Menu menuEdit = MenuTools.createMenu("Edit single", actionInverse, qupath.createPluginAction("Split", SplitAnnotationsPlugin.class, null));
// Menu menuPoints = MenuTools.createMenu(
// "Points",
// QuPathGUI.createCommandAction(new MergePointsCommand(qupath, true), "Merge all points for class"),
// QuPathGUI.createCommandAction(new MergePointsCommand(qupath, true), "Merge selected points for class")
// );
MenuItem separator = new SeparatorMenuItem();
Runnable validator = () -> {
var imageData = qupath.getImageData();
PathObject selected = null;
Collection<PathObject> allSelected = Collections.emptyList();
boolean allSelectedAnnotations = false;
boolean hasSelectedAnnotation = false;
if (imageData != null) {
selected = imageData.getHierarchy().getSelectionModel().getSelectedObject();
allSelected = new ArrayList<>(imageData.getHierarchy().getSelectionModel().getSelectedObjects());
hasSelectedAnnotation = selected != null && selected.isAnnotation();
allSelectedAnnotations = allSelected.stream().allMatch(p -> p.isAnnotation());
}
miLockAnnotations.setDisable(!hasSelectedAnnotation);
miUnlockAnnotations.setDisable(!hasSelectedAnnotation);
if (hasSelectedAnnotation) {
boolean isLocked = selected.isLocked();
miLockAnnotations.setSelected(isLocked);
miUnlockAnnotations.setSelected(!isLocked);
}
miSetProperties.setDisable(!hasSelectedAnnotation);
miInsertHierarchy.setVisible(selected != null);
menuEdit.setVisible(hasSelectedAnnotation);
menuCombine.setVisible(allSelectedAnnotations && allSelected.size() > 1);
separator.setVisible(menuEdit.isVisible() || menuCombine.isVisible());
};
List<MenuItem> items;
if (menu instanceof Menu) {
Menu m = (Menu) menu;
items = m.getItems();
m.setOnMenuValidation(e -> validator.run());
} else if (menu instanceof ContextMenu) {
ContextMenu m = (ContextMenu) menu;
items = m.getItems();
m.setOnShowing(e -> validator.run());
} else
throw new IllegalArgumentException("Menu must be either a standard Menu or a ContextMenu!");
MenuTools.addMenuItems(items, miLockAnnotations, miUnlockAnnotations, miSetProperties, miInsertHierarchy, separator, menuEdit, menuCombine);
}
Aggregations