]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Fix to zoom level handling in Map UI. 83/2783/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Mon, 11 Mar 2019 18:17:17 +0000 (20:17 +0200)
committerReino Ruusu <reino.ruusu@semantum.fi>
Mon, 18 Mar 2019 15:20:29 +0000 (15:20 +0000)
gitlab #32

Change-Id: If91a547d85d044d2e4cc78c10ee632f4dc5845b1
(cherry picked from commit a94392a0c857026b936436fbf61853b44a2c8d42)

org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictPanZoomRotateHandler.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictTransformUtil.java

index fecc17548e98c5f995eef4d912112c354007a5f0..05866bdabb3fea2b6bf14f4562267d5309bf11f9 100644 (file)
@@ -2,13 +2,15 @@ package org.simantics.district.network.ui;
 
 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;
@@ -89,7 +91,7 @@ public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler {
         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())
@@ -103,6 +105,14 @@ public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler {
         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());
         
@@ -124,7 +134,7 @@ public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler {
             // 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);
index ce28aba5428a76efa822a607e28ae6692314d801..e71acf0a8ff0e22c2a753ac653b97c3d3e6ca959 100644 (file)
@@ -14,7 +14,7 @@ public class DistrictTransformUtil extends TransformUtil {
     @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);
@@ -38,12 +38,10 @@ public class DistrictTransformUtil extends TransformUtil {
         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();
@@ -52,30 +50,24 @@ public class DistrictTransformUtil extends TransformUtil {
         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());