def findOutsideRectangles(self, rect, innerRect): top, left, bottom, right = self.getEdges(rect) innertop, innerleft, innerbottom, innerright = self.getEdges(innerRect) insets = [ Insets(innerbottom - top, 0, 0, 0), Insets(0, innerright - left, 0, 0), Insets(0, 0, bottom - innertop, 0), Insets(0, 0, 0, right - innerleft) ] rects = [ rect.getCropped(i) for i in insets ] rects = filter(lambda r: getInt(r.height) and getInt(r.width), rects) return sorted(rects, key=lambda r: -getInt(r.height) * getInt(r.width))
def findOverlapRegionsFor(self, editParts, ignoreEditPart, bounds): overlaps = [] for otherPart in editParts: if otherPart is not ignoreEditPart and otherPart.part().isSelectable(): otherBounds = self.getBoundsInternal(otherPart) intersection = bounds.getIntersection(otherBounds) if getInt(intersection.height) and getInt(intersection.width) and intersection != bounds and intersection not in overlaps: overlaps.append(intersection) self.logger.debug("Overlap found at " + repr(intersection)) overlaps += self.findOverlapRegionsUnder(otherPart, ignoreEditPart, bounds) return overlaps
def getCenter(self, editPart): bounds = self.getBoundsInternal(editPart) self.logger.debug("Object bounds at " + repr(bounds)) viewportBounds = self.getFigureCanvas().getViewportBounds() self.logger.debug("Canvas viewport bounds at " + repr(viewportBounds)) visibleBounds = viewportBounds.getIntersection(bounds) if getInt(visibleBounds.height) == 0 or getInt(visibleBounds.width) == 0: self.logger.debug("No intersection with viewport, scrolling until there is") self.ensureInViewport(bounds, viewportBounds) visibleBounds = self.getBoundsInternal(editPart) self.logger.debug("Found bounds at " + repr(visibleBounds)) overlaps = self.findOverlapRegions(editPart, visibleBounds) return self.findNonOverlappingCentre(visibleBounds, overlaps)
def findOverlapRegionsFor(self, editParts, ignoreEditPart, bounds): overlaps = [] for otherPart in editParts: if otherPart is not ignoreEditPart and otherPart.part().isSelectable(): otherBounds = self.getBoundsInternal(otherPart) intersection = bounds.getIntersection(otherBounds) if ( getInt(intersection.height) and getInt(intersection.width) and intersection != bounds and intersection not in overlaps ): overlaps.append(intersection) self.logger.debug("Overlap found at " + repr(intersection)) overlaps += self.findOverlapRegionsUnder(otherPart, ignoreEditPart, bounds) # Handle the largest overlaps first, likely to give the best effect. Need to sort somehow to prevent indeterminism overlaps.sort(key=self.getArea, reverse=True) return overlaps
def findNonOverlappingCentre(self, bounds, overlaps): centre = bounds.getCenter() self.logger.debug("Found centre at " + repr(centre)) overlap = self.findOverlap(overlaps, centre) if overlap: self.logger.debug("Centre overlaps rectangle at " + repr(overlap)) for rect in self.findOutsideRectangles(bounds, overlap): self.logger.debug("Trying outside rectangle " + repr(rect)) newOverlaps = [] for otherOverlap in overlaps: if otherOverlap is not overlap: intersection = rect.getIntersection(otherOverlap) if getInt(intersection.height) and getInt(intersection.width) and intersection not in newOverlaps: self.logger.debug("Found new overlap " + repr(intersection)) newOverlaps.append(intersection) if rect not in newOverlaps: centre = self.findNonOverlappingCentre(rect, newOverlaps) if centre: return centre return getInt(centre.x), getInt(centre.y)
def getEdges(self, bounds): topEdge = getInt(bounds.y) bottomEdge = topEdge + getInt(bounds.height) leftEdge = getInt(bounds.x) rightEdge = leftEdge + getInt(bounds.width) return topEdge, leftEdge, bottomEdge, rightEdge
def doScroll(): currPos = getInt(self.widget.getViewport().getViewLocation().y) self.widget.scrollToY(currPos + offset)
def getArea(self, rect): return getInt(rect.height) * getInt(rect.width)