From: jsimomaa Date: Wed, 6 Mar 2019 20:09:23 +0000 (+0200) Subject: Add refresh to context menu in district network breakdown view X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F61%2F2761%2F1;p=simantics%2Fdistrict.git Add refresh to context menu in district network breakdown view Also fixes selection of single vertex gitlab #11 Change-Id: I3c8af80ccfda86be17dfdcede1269bfde0b16a40 --- diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictPanZoomRotateHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictPanZoomRotateHandler.java index f9d9fccc..fecc1754 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictPanZoomRotateHandler.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictPanZoomRotateHandler.java @@ -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 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; } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictTransformUtil.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictTransformUtil.java index 230c17cd..ce28aba5 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictTransformUtil.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictTransformUtil.java @@ -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? diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java index d873835c..a989c561 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java @@ -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; diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/breakdown/DistrictNetworkBreakdownPanel.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/breakdown/DistrictNetworkBreakdownPanel.java index b3e6d6bb..c8db5165 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/breakdown/DistrictNetworkBreakdownPanel.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/breakdown/DistrictNetworkBreakdownPanel.java @@ -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 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 diagram; + + public RefreshAction(Set 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 subgraphs; diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/breakdown/Input.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/breakdown/Input.java index dd80c2a6..6d9ae8b6 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/breakdown/Input.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/breakdown/Input.java @@ -14,9 +14,35 @@ public class Input { public static class NetworkDiagrams extends Bean { public List 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; + } } }