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;
}
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;
}
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.
@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;
}
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;
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?
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;
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;
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;
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());
}
}
+ 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;
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;
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 {
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;
+ }
}
}