import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
+import java.util.Collection;
+import java.util.List;
import java.util.Set;
+import java.util.stream.Collectors;
import org.simantics.g2d.canvas.ICanvasContext;
import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
import org.simantics.g2d.canvas.impl.DependencyReflection.Reference;
import org.simantics.g2d.diagram.DiagramHints;
-import org.simantics.g2d.diagram.DiagramUtils;
import org.simantics.g2d.diagram.IDiagram;
import org.simantics.g2d.diagram.participant.Selection;
import org.simantics.g2d.element.ElementUtils;
if (d == null)
return false;
- Rectangle2D diagramRect = DiagramUtils.getContentRect(d);
+ Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(getMapElements(d.getElements()));
if (diagramRect == null)
return false;
if (diagramRect.isEmpty())
return true;
}
+ protected static List<IElement> getMapElements(Collection<IElement> elements) {
+ List<IElement> justMapElements = elements.stream()
+ .filter(e -> "DistrictNetworkEdgeElement".equals(e.getElementClass().getId())
+ || "DistrictNetworkVertexElement".equals(e.getElementClass().getId()))
+ .collect(Collectors.toList());
+ return justMapElements;
+ }
+
private boolean zoomToPage() {
int currentZoomLevel = MapScalingTransform.zoomLevel(util.getTransform());
// no can do,
return zoomToPage();
}
- Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(selections);
+ Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(getMapElements(selections));
// Make sure that even empty bounds can be zoomed into.
org.simantics.scenegraph.utils.GeometryUtils.expandRectangle(diagramRect, 1);
@Override
public void fitArea(Rectangle2D controlArea, Rectangle2D diagramArea, Margins margins) {
if (margins == null) {
- MarginUtils.Margin margin = MarginUtils.marginOf(40, 0, 0);
+ MarginUtils.Margin margin = MarginUtils.marginOf(0, 0, 0);
margins = new MarginUtils.Margins(margin, margin, margin, margin);
}
org.simantics.scenegraph.utils.GeometryUtils.expandRectangle(diagramArea, -1);
if (controlAspectRatio>canvasAspectRatio)
{
scale = controlArea.getHeight() / diagramArea.getHeight();
- tx = ( controlArea.getWidth() - diagramArea.getWidth() * scale ) / 2;
} else
// Control is really tall => center canvas vertically, match horizontally
{
scale = controlArea.getWidth() / diagramArea.getWidth();
- ty = ( controlArea.getHeight() - diagramArea.getHeight() * scale ) / 2;
}
AffineTransform current = getTransform();
double currentX = current.getScaleX();
if (targetX > 0) {
// let's check that targetX zoomLevel is between 0-20
AffineTransform tar = new AffineTransform();
- tar.scale(toBeX, toBeX);
+ tar.scale(targetX, targetX);
int tarZoomLevel = MapScalingTransform.zoomLevel(tar);
if (tarZoomLevel < 20 && tarZoomLevel > 0) {
- if (toBeX < targetX) {
- while ((toBeX * 0.5) < targetX) {
- toBeX = toBeX * 2;
- AffineTransform tr = new AffineTransform();
- tr.scale(toBeX, toBeX);
- int zoomLevel = MapScalingTransform.zoomLevel(tr);
- if (zoomLevel >= 20) { // let's not zoom too close
- break;
- }
- }
- } else {
- while ((toBeX * 0.25) > targetX) {
- toBeX = toBeX * 0.5;
- AffineTransform tr = new AffineTransform();
- tr.scale(toBeX, toBeX);
- if (MapScalingTransform.zoomLevel(tr) <= 0) { // let's not zoom too far away
- break;
- }
- }
- }
+ toBeX = Math.pow(2.0, tarZoomLevel);
}
+ else {
+ toBeX = targetX;
+ }
+
+ if (controlAspectRatio>canvasAspectRatio) {
+ tx = ( controlArea.getWidth() - diagramArea.getWidth() * toBeX ) / 2;
+ ty = diagramArea.getHeight() * (targetX - toBeX) / 2;
+ }
+ else {
+ ty = ( controlArea.getHeight() - diagramArea.getHeight() * toBeX ) / 2;
+ tx = diagramArea.getWidth() * (targetX - toBeX) / 2;
+ }
+
AffineTransform at = new AffineTransform();
at.translate(tx, ty);
at.translate(controlArea.getMinX(), controlArea.getMinY());