]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Add refresh to context menu in district network breakdown view 55/2755/1
authorjsimomaa <jani.simomaa@gmail.com>
Wed, 6 Mar 2019 20:09:23 +0000 (22:09 +0200)
committerjsimomaa <jani.simomaa@gmail.com>
Wed, 6 Mar 2019 20:09:23 +0000 (22:09 +0200)
Also fixes selection of single vertex

gitlab #11

Change-Id: I3c8af80ccfda86be17dfdcede1269bfde0b16a40

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
org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/breakdown/DistrictNetworkBreakdownPanel.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/breakdown/Input.java

index f9d9fccca449b8a265d4f0983d41952138cd39e8..fecc17548e98c5f995eef4d912112c354007a5f0 100644 (file)
@@ -7,6 +7,9 @@ import java.util.Set;
 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;
 import org.simantics.g2d.element.IElement;
@@ -74,14 +77,36 @@ public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler {
     }
 
     private boolean zoomToFit() {
-        return false;
+        CanvasBoundsParticipant boundsParticipant = getContext().getAtMostOneItemOfClass(CanvasBoundsParticipant.class);
+        if (boundsParticipant == null)
+            return false;
+
+        final Rectangle2D controlBounds = boundsParticipant.getControlBounds().getFrame();
+        if (controlBounds == null || controlBounds.isEmpty())
+            return false;
+
+        IDiagram d = getHint(DiagramHints.KEY_DIAGRAM);
+        if (d == null)
+            return false;
+
+        Rectangle2D diagramRect = DiagramUtils.getContentRect(d);
+        if (diagramRect == null)
+            return false;
+        if (diagramRect.isEmpty())
+            return false;
+
+        org.simantics.scenegraph.utils.GeometryUtils.expandRectangle(diagramRect, 1);
+        
+        // System.out.println("zoomToFit(" + controlArea + ", " + diagramRect + ")");
+        util.fitArea(controlBounds, diagramRect, null);
+
+        return true;
     }
 
     private boolean zoomToPage() {
         int currentZoomLevel = MapScalingTransform.zoomLevel(util.getTransform());
         
         util.setTransform(new AffineTransform(2,0,0,2,270,270));
-//        util.setTransform(new AffineTransform(MapScalingTransform.INSTANCE));
         return true;
     }
     
@@ -95,6 +120,10 @@ public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler {
             return false;
         
         Set<IElement> selections = selection.getAllSelections();
+        if (selections == null || selections.isEmpty()) {
+            // no can do, 
+            return zoomToPage();
+        }
         Rectangle2D diagramRect = ElementUtils.getSurroundingElementBoundsOnDiagram(selections);
         
         // Make sure that even empty bounds can be zoomed into.
@@ -136,26 +165,34 @@ public class DistrictPanZoomRotateHandler extends PanZoomRotateHandler {
         @Override
         public boolean mouseWheelMoved(MouseWheelMovedEvent me) {
             if (navigationEnabled && zoomEnabled) {
-                double z;
-                if (me.wheelRotation > 0) {
-                    z = DISTRICT_TRANSLATE_AMOUNT;
+                // check if min/max zoom already
+                AffineTransform transform = getTransform();
+                int zoomLevel = MapScalingTransform.zoomLevel(transform);
+                
+                if (0 < zoomLevel && zoomLevel < 20 || (zoomLevel == 0 && me.wheelRotation > 0) || (zoomLevel == 20 && me.wheelRotation < 0)) {
+                    double z;
+                    if (me.wheelRotation > 0) {
+                        z = DISTRICT_TRANSLATE_AMOUNT;
+                    } else {
+                        z = 1.0d / DISTRICT_TRANSLATE_AMOUNT;
+                    }
+                    //double scroll = Math.min(0.9, -me.wheelRotation / 20.0);
+                    //double z = 1 - scroll;
+                    double dx = (me.controlPosition.getX() - transform.getTranslateX()) / transform.getScaleX();
+                    double dy = (me.controlPosition.getY() - transform.getTranslateY()) / transform.getScaleY();
+                    dx = dx * (1 - z);
+                    dy = dy * (1 - z);
+//                    double limitedScale = limitScaleFactor(z);
+//                    if (limitedScale != 1.0) {
+                        translate(dx, dy);
+                        scale(z, z);
+                        transformChanged();
+                        dropQuality();
+                        repaint();
+//                    }
                 } else {
-                    z = 1.0d / DISTRICT_TRANSLATE_AMOUNT;
+                    // max zoom level reached
                 }
-                //double scroll = Math.min(0.9, -me.wheelRotation / 20.0);
-                //double z = 1 - scroll;
-                double dx = (me.controlPosition.getX() - transform.getTranslateX()) / transform.getScaleX();
-                double dy = (me.controlPosition.getY() - transform.getTranslateY()) / transform.getScaleY();
-                dx = dx * (1 - z);
-                dy = dy * (1 - z);
-//                double limitedScale = limitScaleFactor(z);
-//                if (limitedScale != 1.0) {
-                    translate(dx, dy);
-                    scale(z, z);
-                    transformChanged();
-                    dropQuality();
-                    repaint();
-//                }
             }
             return false;
         }
index 230c17cd55fedfce2cfd48d7ceaf4bae00e3dfb9..ce28aba5428a76efa822a607e28ae6692314d801 100644 (file)
@@ -4,6 +4,7 @@ import java.awt.geom.AffineTransform;
 import java.awt.geom.Rectangle2D;
 
 import org.simantics.g2d.participant.TransformUtil;
+import org.simantics.maps.MapScalingTransform;
 import org.simantics.scenegraph.utils.GeometryUtils;
 import org.simantics.utils.page.MarginUtils;
 import org.simantics.utils.page.MarginUtils.Margins;
@@ -49,23 +50,37 @@ public class DistrictTransformUtil extends TransformUtil {
         double targetX = scale;
         double toBeX = currentX;
         if (targetX > 0) {
-            if (toBeX < targetX) {
-                while ((toBeX) < targetX) {
-                    toBeX = toBeX * 2;
-                }
-            } else {
-                while ((toBeX * 0.5) > targetX) {
-                    toBeX = toBeX * 0.5;
+            // let's check that targetX zoomLevel is between 0-20
+            AffineTransform tar = new AffineTransform();
+            tar.scale(toBeX, toBeX);
+            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;
+                        }
+                    }
                 }
             }
-            //tx = tx * (1 - toBeX);
-            //ty = ty * (1 - toBeX);
             AffineTransform at = new AffineTransform();
             at.translate(tx, ty);
             at.translate(controlArea.getMinX(), controlArea.getMinY());
             at.scale(toBeX, toBeX);
             at.translate(-diagramArea.getMinX(), -diagramArea.getMinY());
-            
             setTransform(at);
         } else {
             // negative, how, no can do?
index d873835c878bc14916d977153dad4379d09b05aa..a989c56117644025e9b883620f1af29acf57ff5e 100644 (file)
@@ -22,7 +22,6 @@ import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
 import org.simantics.scenegraph.INode;
 import org.simantics.scenegraph.g2d.G2DParentNode;
 import org.simantics.scenegraph.g2d.nodes.SVGNode;
-import org.simantics.scenegraph.utils.GeometryUtils;
 import org.simantics.utils.datastructures.hints.IHintContext.Key;
 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
 
@@ -97,17 +96,16 @@ public class DistrictNetworkVertexElement {
             ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(node);
             AffineTransform canvasTransform = ctx.getHintStack().getHint(Hints.KEY_CANVAS_TRANSFORM);
             // for some reason PickContextImpl expands the rectangle by 0.001 (too much) - let's counter it
-            double counterExpansion = 0.001;
-            double x = boundsInLocal.getX() + counterExpansion;
-            double y = boundsInLocal.getY() + counterExpansion;
+            double x = boundsInLocal.getX();
+            double y = boundsInLocal.getY();
             double scaledWidth = boundsInLocal.getWidth();
             double scaledHeight = boundsInLocal.getHeight();
             if (canvasTransform != null) {
-                scaledWidth = boundsInLocal.getWidth() / canvasTransform.getScaleX() / 10000.0;
-                scaledHeight= boundsInLocal.getHeight() / canvasTransform.getScaleY() / 10000.0;
+                scaledWidth = boundsInLocal.getWidth() / canvasTransform.getScaleX();
+                scaledHeight= boundsInLocal.getHeight() / canvasTransform.getScaleY();
             }
-            double width = scaledWidth - 2*counterExpansion;
-            double height = scaledHeight - 2*counterExpansion;
+            double width = scaledWidth;
+            double height = scaledHeight;
             size.setFrame(x, y, width, height);
             
             return size;
index b3e6d6bbecad2d55ded30fa868829ec51fb959ae..c8db5165f497ba4964979917c6e7df599c6c4a1f 100644 (file)
@@ -1,7 +1,9 @@
 package org.simantics.district.network.ui.breakdown;
 
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
@@ -147,6 +149,15 @@ public class DistrictNetworkBreakdownPanel extends Composite {
                 if (sel.size() > 0 && fromSameDiagram(sel)) {
                     manager.add(new ShowSubnetworkAction(sel));
                 }
+                Set<NetworkDiagram> diagrams = new HashSet<>();
+                if (sel.size() > 0) {
+                    for (Subgraph graph : sel) {
+                        diagrams.add(graph.parent);
+                    }
+                }
+                if (diagrams.isEmpty())
+                    diagrams.addAll(subgraphs.keySet());
+                manager.add(new RefreshAction(diagrams));
                 //manager.add(new DeleteAction());
             }
 
@@ -445,6 +456,24 @@ public class DistrictNetworkBreakdownPanel extends Composite {
 
     }
 
+    private class RefreshAction extends Action {
+
+        private Set<NetworkDiagram> diagram;
+
+        public RefreshAction(Set<NetworkDiagram> diagrams) {
+            super("Refresh");
+            this.diagram = diagrams;
+        }
+
+        @Override
+        public void run() {
+            diagram.forEach(d -> {
+                subgraphs.remove(d);
+                tree.refresh();
+            });
+        }
+    }
+
     private static class ShowSubnetworkAction extends Action {
 
         private final List<Subgraph> subgraphs;
index dd80c2a657500a8c99fc141eec7738f73a42fcd5..6d9ae8b67cd39f0e4cdf67ed1173aa0a8a30d0a6 100644 (file)
@@ -14,9 +14,35 @@ public class Input {
 
     public static class NetworkDiagrams extends Bean {
         public List<NetworkDiagram> diagrams = new ArrayList<>();
+
+        @Override
+        public int hashCode() {
+            final int prime = 31;
+            int result = super.hashCode();
+            result = prime * result + ((diagrams == null) ? 0 : diagrams.hashCode());
+            return result;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj)
+                return true;
+            if (!super.equals(obj))
+                return false;
+            if (getClass() != obj.getClass())
+                return false;
+            NetworkDiagrams other = (NetworkDiagrams) obj;
+            if (diagrams == null) {
+                if (other.diagrams != null)
+                    return false;
+            } else if (!diagrams.equals(other.diagrams))
+                return false;
+            return true;
+        }
     }
 
     public static class NetworkDiagram extends Bean {
+
         public String name;
         public Resource diagram;
 
@@ -24,6 +50,37 @@ public class Input {
             this.name = name;
             this.diagram = diagram;
         }
+
+        @Override
+        public int hashCode() {
+            final int prime = 31;
+            int result = super.hashCode();
+            result = prime * result + ((diagram == null) ? 0 : diagram.hashCode());
+            result = prime * result + ((name == null) ? 0 : name.hashCode());
+            return result;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj)
+                return true;
+            if (!super.equals(obj))
+                return false;
+            if (getClass() != obj.getClass())
+                return false;
+            NetworkDiagram other = (NetworkDiagram) obj;
+            if (diagram == null) {
+                if (other.diagram != null)
+                    return false;
+            } else if (!diagram.equals(other.diagram))
+                return false;
+            if (name == null) {
+                if (other.name != null)
+                    return false;
+            } else if (!name.equals(other.name))
+                return false;
+            return true;
+        }
     }
 
     public static class Subgraph {
@@ -38,6 +95,46 @@ public class Input {
             this.vertices = vertices;
             this.edges = edges;
         }
+
+        @Override
+        public int hashCode() {
+            final int prime = 31;
+            int result = 1;
+            result = prime * result + ((edges == null) ? 0 : edges.hashCode());
+            result = prime * result + index;
+            result = prime * result + ((parent == null) ? 0 : parent.hashCode());
+            result = prime * result + ((vertices == null) ? 0 : vertices.hashCode());
+            return result;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj)
+                return true;
+            if (obj == null)
+                return false;
+            if (getClass() != obj.getClass())
+                return false;
+            Subgraph other = (Subgraph) obj;
+            if (edges == null) {
+                if (other.edges != null)
+                    return false;
+            } else if (!edges.equals(other.edges))
+                return false;
+            if (index != other.index)
+                return false;
+            if (parent == null) {
+                if (other.parent != null)
+                    return false;
+            } else if (!parent.equals(other.parent))
+                return false;
+            if (vertices == null) {
+                if (other.vertices != null)
+                    return false;
+            } else if (!vertices.equals(other.vertices))
+                return false;
+            return true;
+        }
     }
 
 }