use of qupath.lib.objects.hierarchy.events.PathObjectSelectionModel in project qupath by qupath.
the class PathObjectHierarchyView method synchronizeSelectionModelToTree.
/**
* Ensure that the hierarchy selection model matches the selection within the TreeView.
* @param change
*/
private void synchronizeSelectionModelToTree(final ListChangeListener.Change<? extends TreeItem<PathObject>> change) {
if (synchronizingTreeToModel)
return;
PathObjectSelectionModel model = getHierarchySelectionModel();
if (model == null) {
return;
}
boolean wasSynchronizingToTree = synchronizingModelToTree;
try {
synchronizingModelToTree = true;
// Check - was anything removed?
boolean removed = false;
if (change != null) {
while (change.next()) removed = removed | change.wasRemoved();
}
MultipleSelectionModel<TreeItem<PathObject>> treeModel = treeView.getSelectionModel();
List<TreeItem<PathObject>> selectedItems = treeModel.getSelectedItems();
// If we just have no selected items, and something was removed, then clear the selection
if (selectedItems.isEmpty() && removed) {
model.clearSelection();
return;
}
// if (selectedItems.size() == 1 && removed) {
if (selectedItems.size() == 1) {
model.setSelectedObject(selectedItems.get(0).getValue(), false);
return;
}
// If we have multiple selected items, we need to ensure that everything in the tree matches with everything in the selection model
Set<PathObject> toSelect = treeView.getSelectionModel().getSelectedItems().stream().map(t -> t.getValue()).collect(Collectors.toSet());
TreeItem<PathObject> mainSelection = treeView.getSelectionModel().getSelectedItem();
PathObject primary = mainSelection == null ? null : mainSelection.getValue();
model.setSelectedObjects(toSelect, primary);
} finally {
synchronizingModelToTree = wasSynchronizingToTree;
}
}
use of qupath.lib.objects.hierarchy.events.PathObjectSelectionModel in project qupath by qupath.
the class SummaryMeasurementTableCommand method synchronizeSelectionModelToTable.
private void synchronizeSelectionModelToTable(final PathObjectHierarchy hierarchy, final ListChangeListener.Change<? extends PathObject> change, final TableView<PathObject> table) {
if (synchronizingTableToModel || hierarchy == null)
return;
PathObjectSelectionModel model = hierarchy.getSelectionModel();
if (model == null) {
return;
}
boolean wasSynchronizingToTree = synchronizingModelToTable;
try {
synchronizingModelToTable = true;
// Check - was anything removed?
boolean removed = false;
if (change != null) {
while (change.next()) removed = removed | change.wasRemoved();
}
MultipleSelectionModel<PathObject> treeModel = table.getSelectionModel();
List<PathObject> selectedItems = treeModel.getSelectedItems();
// If we just have no selected items, and something was removed, then clear the selection
if (selectedItems.isEmpty() && removed) {
model.clearSelection();
return;
}
// if (selectedItems.size() == 1 && removed) {
if (selectedItems.size() == 1) {
model.setSelectedObject(selectedItems.get(0), false);
return;
}
// If we have multiple selected items, we need to ensure that everything in the tree matches with everything in the selection model
Set<PathObject> toSelect = new HashSet<>(treeModel.getSelectedItems());
PathObject primary = treeModel.getSelectedItem();
model.setSelectedObjects(toSelect, primary);
} finally {
synchronizingModelToTable = wasSynchronizingToTree;
}
}
use of qupath.lib.objects.hierarchy.events.PathObjectSelectionModel in project qupath by qupath.
the class CountingPane method setHierarchy.
public void setHierarchy(PathObjectHierarchy hierarchy) {
if (this.hierarchy == hierarchy)
return;
if (this.hierarchy != null) {
this.hierarchy.getSelectionModel().removePathObjectSelectionListener(this);
this.hierarchy.removePathObjectListener(this);
}
this.hierarchy = hierarchy;
PathObject objectSelected = null;
if (this.hierarchy != null) {
PathObjectSelectionModel model = this.hierarchy.getSelectionModel();
model.addPathObjectSelectionListener(this);
objectSelected = model.getSelectedObject();
this.hierarchy.addPathObjectListener(this);
}
// Update selected object in list, if suitable
if (objectSelected != null && PathObjectTools.hasPointROI(objectSelected))
listCounts.getSelectionModel().select(objectSelected);
else
listCounts.getSelectionModel().clearSelection();
// Force update
hierarchyChanged(null);
}
use of qupath.lib.objects.hierarchy.events.PathObjectSelectionModel in project qupath by qupath.
the class SummaryMeasurementTableCommand method synchronizeTableToSelectionModel.
private void synchronizeTableToSelectionModel(final PathObjectHierarchy hierarchy, final TableView<PathObject> table) {
if (synchronizingModelToTable || hierarchy == null)
return;
boolean ownsChanges = !synchronizingTableToModel;
try {
synchronizingTableToModel = true;
PathObjectSelectionModel model = hierarchy.getSelectionModel();
TableViewSelectionModel<PathObject> tableModel = table.getSelectionModel();
if (model == null || model.noSelection()) {
tableModel.clearSelection();
return;
}
if (model.singleSelection() || tableModel.getSelectionMode() == SelectionMode.SINGLE) {
int ind = table.getItems().indexOf(model.getSelectedObject());
if (ind >= 0) {
tableModel.clearAndSelect(ind);
table.scrollTo(ind);
} else
tableModel.clearSelection();
return;
}
// Loop through all possible selections, and select them if they should be selected (and not if they shouldn't)
// For performance reasons, we need to do this using arrays - otherwise way too many events may be fired
int n = table.getItems().size();
PathObject mainSelectedObject = model.getSelectedObject();
int mainObjectInd = -1;
int[] indsToSelect = new int[table.getItems().size()];
int count = 0;
for (int i = 0; i < n; i++) {
PathObject temp = table.getItems().get(i);
if (temp == mainSelectedObject)
mainObjectInd = i;
if (model.isSelected(temp)) {
indsToSelect[count] = i;
count++;
}
}
tableModel.clearSelection();
if (count > 0) {
int maxCount = 1000;
if (count > maxCount) {
logger.warn("Only the first {} items will be selected in the table (out of {} total) - otherwise QuPath can grind to a halt, sorry", maxCount, count);
count = maxCount;
}
tableModel.selectIndices(indsToSelect[0], Arrays.copyOfRange(indsToSelect, 1, count));
}
// Ensure that the main object is focussed & its node expanded
if (mainObjectInd >= 0 && model.singleSelection()) {
tableModel.select(mainObjectInd);
table.scrollTo(mainObjectInd);
}
} finally {
if (ownsChanges)
synchronizingTableToModel = false;
}
}
Aggregations