use of qupath.lib.objects.PathROIObject in project qupath by qupath.
the class PointsTool method mousePressed.
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
if (!e.isPrimaryButtonDown() || e.isConsumed()) {
return;
}
// Get a server, if we can
var viewer = getViewer();
ImageServer<?> server = viewer.getServer();
if (server == null)
return;
var viewerPlane = viewer.getImagePlane();
// Find out the coordinates in the image domain
Point2D p = mouseLocationToImage(e, false, requestPixelSnapping());
double xx = p.getX();
double yy = p.getY();
// If we are outside the image, ignore click
if (xx < 0 || yy < 0 || xx >= server.getWidth() || yy >= server.getHeight())
return;
// See if we have a selected ROI
PathObject currentObjectTemp = viewer.getSelectedObject();
if (!(currentObjectTemp == null || currentObjectTemp instanceof PathROIObject))
return;
PathROIObject currentObject = (PathROIObject) currentObjectTemp;
ROI currentROI = currentObject == null ? null : currentObject.getROI();
RoiEditor editor = viewer.getROIEditor();
double radius = PathPrefs.pointRadiusProperty().get();
ROI points = null;
if (currentROI != null && currentROI.isPoint() && (currentROI.isEmpty() || currentROI.getImagePlane().equals(viewerPlane)))
points = currentROI;
// If Alt is pressed, try to delete a point
if (e.isAltDown()) {
handleAltClick(xx, yy, currentObject);
} else // Create a new ROI if we've got Alt & Shift pressed - or we just don't have a point ROI
if (points == null || (!PathPrefs.multipointToolProperty().get() && !editor.grabHandle(xx, yy, radius, e.isShiftDown())) || (e.isShiftDown() && e.getClickCount() > 1)) {
// PathPoints is effectively ready from the start - don't need to finalize
points = ROIs.createPointsROI(xx, yy, viewerPlane);
currentObject = (PathROIObject) PathObjects.createAnnotationObject(points, PathPrefs.autoSetAnnotationClassProperty().get());
viewer.getHierarchy().addPathObject(currentObject);
viewer.setSelectedObject(currentObject);
// viewer.createAnnotationObject(points);
editor.setROI(points);
editor.grabHandle(xx, yy, radius, e.isShiftDown());
} else if (points != null) {
// Add point to current ROI, or adjust the position of a nearby point
ImagePlane plane = points == null || points.isEmpty() ? viewerPlane : points.getImagePlane();
ROI points2 = addPoint(points, xx, yy, radius, plane);
if (points2 == points) {
// If we didn't add a point, try to grab a handle
if (!editor.grabHandle(xx, yy, radius, e.isShiftDown()))
return;
points2 = (PointsROI) editor.setActiveHandlePosition(xx, yy, 0.25, e.isShiftDown());
} else {
editor.setROI(points2);
editor.grabHandle(xx, yy, radius, e.isShiftDown());
}
if (points2 != points) {
currentObject.setROI(points2);
viewer.getHierarchy().updateObject(currentObject, true);
// viewer.getHierarchy().fireHierarchyChangedEvent(this, currentObject);
}
}
viewer.repaint();
}
use of qupath.lib.objects.PathROIObject in project qupath by qupath.
the class FillAnnotationHolesPlugin method getTasks.
@Override
protected Collection<Runnable> getTasks(final PluginRunner<T> runner) {
Collection<? extends PathObject> parentObjects = getParentObjects(runner);
if (parentObjects == null || parentObjects.isEmpty())
return Collections.emptyList();
// Add a single task, to avoid multithreading - which may complicate setting parents
List<Runnable> tasks = new ArrayList<>(1);
PathObjectHierarchy hierarchy = getHierarchy(runner);
// Want to reset selection
PathObject selected = hierarchy.getSelectionModel().getSelectedObject();
Collection<PathObject> previousSelection = new ArrayList<>(hierarchy.getSelectionModel().getSelectedObjects());
tasks.add(() -> {
Map<PathROIObject, ROI> toUpdate = new HashMap<>();
for (PathObject pathObject : parentObjects) {
ROI roiOrig = pathObject.getROI();
if (roiOrig == null || !roiOrig.isArea())
continue;
ROI roiUpdated = roiOrig;
roiUpdated = RoiTools.fillHoles(roiUpdated);
if (roiOrig != roiUpdated && pathObject instanceof PathROIObject) {
toUpdate.put((PathROIObject) pathObject, roiUpdated);
}
}
if (toUpdate.isEmpty())
return;
hierarchy.getSelectionModel().clearSelection();
if (!toUpdate.isEmpty()) {
hierarchy.removeObjects(toUpdate.keySet(), true);
toUpdate.forEach((p, r) -> p.setROI(r));
hierarchy.addPathObjects(toUpdate.keySet());
}
hierarchy.getSelectionModel().selectObjects(previousSelection);
hierarchy.getSelectionModel().setSelectedObject(selected, true);
});
return tasks;
}
use of qupath.lib.objects.PathROIObject in project qupath by qupath.
the class RigidObjectEditorCommand method commitChanges.
private void commitChanges(final boolean ignoreChanges) {
if (this.originalObject == null)
return;
// PathObject pathObject = null;
if (!ignoreChanges) {
DialogButton option = Dialogs.showYesNoCancelDialog("Affine object editing", "Confirm object changes?");
if (option == DialogButton.CANCEL)
return;
if (option == DialogButton.NO) {
for (Entry<PathObject, ROI> entry : originalObjectROIs.entrySet()) ((PathROIObject) entry.getKey()).setROI(entry.getValue());
} else {
var transform = transformer.transform;
var values = transform.getMatrixEntries();
logger.info("Applied ROI transform: {}", String.format("\n %f, %f, %f,\n%f, %f, %f", values[0], values[1], values[2], values[3], values[4], values[5]));
// Apply clipping now
for (Entry<PathObject, ROI> entry : originalObjectROIs.entrySet()) {
ROI roiTransformed = transformer.getTransformedROI(entry.getValue(), true);
((PathROIObject) entry.getKey()).setROI(roiTransformed);
}
viewer.getHierarchy().fireHierarchyChangedEvent(this, originalObject);
}
}
// Update the mode if the viewer is still active
qupath.setToolSwitchingEnabled(true);
if (viewer == qupath.getViewer())
viewer.setActiveTool(qupath.getSelectedTool());
viewer.getView().removeEventHandler(MouseEvent.ANY, mouseListener);
viewer.getCustomOverlayLayers().remove(overlay);
viewer.removeViewerListener(this);
// if (pathObject != null)
// viewer.getHierarchy().addPathObject(pathObject, true);
// // Ensure the object is selected
// viewer.setSelectedObject(pathObject);
viewer = null;
overlay = null;
originalObjectROIs.clear();
originalObject = null;
transformer = null;
}
Aggregations