]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Allow starting/ending of manual network creation to vertices only 44/2844/3
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 8 Apr 2019 09:44:34 +0000 (12:44 +0300)
committerjsimomaa <jani.simomaa@gmail.com>
Mon, 8 Apr 2019 11:48:55 +0000 (14:48 +0300)
Added possibility to create default vertex mapping types by double click

Also disallow zoom levels higher than 20

gitlab #42
APROS-15325
APROS-15346

Change-Id: Iae9ce29b88b970e0e96bb245bd748d93b9f3db7d

org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.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/NetworkDrawingParticipant.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java
org.simantics.district.network/scl/Simantics/District.scl
org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java

index c0d756d3c86cfef87ef9eaf0ebca232e464d2725..8b0a377bfbaa797a7165df6e2999e2ff66eb533c 100644 (file)
@@ -263,7 +263,7 @@ public class CSVImportWizard extends Wizard implements IImportWizard {
                                                 flipAxes(startCoords);
                                                 flipAxes(endCoords);
                                                 
-                                                Optional<Resource> oedge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, startZCoord, endCoords, endZCoord, padding, true);
+                                                Optional<Resource> oedge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, startZCoord, endCoords, endZCoord, new double[0], padding, true);
                                                 if (oedge.isPresent()) {
                                                     Resource edge = oedge.get();
                                                     writeStringValue(graph, row, idColumn, edge, DN.HasId);
index e47e9a8743f8f66557cbbcf8a228fe846621a168..92a6187b59c7975fb70d4789c0e14eb88bcc686a 100644 (file)
@@ -45,7 +45,7 @@ public class DNEdgeBuilder {
         glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
     }
 
-    public static Optional<Resource> create(WriteGraph graph, Resource diagramResource, double[] start, double startElevation, double[] end, double endElevation, double padding) throws DatabaseException {
+    public static Optional<Resource> create(WriteGraph graph, Resource diagramResource, double[] start, double startElevation, double[] end, double endElevation, double[] detailedGeometryCoords, double padding) throws DatabaseException {
         Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
         double halfPadding = padding / 2;
 
@@ -59,11 +59,10 @@ public class DNEdgeBuilder {
             Envelope e = new Envelope(x1, x2, y1, y2);
             vv.insert(e, new ResourceVertex(vertex, coords, false));
         }
-        return create(graph, vv, diagramResource, null, start, startElevation, end, endElevation, padding, false);
+        return create(graph, vv, diagramResource, null, start, startElevation, end, endElevation, detailedGeometryCoords, padding, false);
     }
     
-    public static Optional<Resource> create(WriteGraph graph, Quadtree vertices, Resource diagramResource, Resource mapping, double[] start, double startElevation, double[] end, double endElevation, double padding, boolean writeElevationToEdgeFromPoints) throws DatabaseException {
-        
+    public static Optional<Resource> create(WriteGraph graph, Quadtree vertices, Resource diagramResource, Resource mapping, double[] start, double startElevation, double[] end, double endElevation, double[] detailedGeometryCoords, double padding, boolean writeElevationToEdgeFromPoints) throws DatabaseException {
         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
 
      // 2. Add vertices
@@ -75,7 +74,7 @@ public class DNEdgeBuilder {
         }
         
         // 1. Get diagram edge to construct
-        Resource edge = getOrCreateEdge(graph, diagramResource, mapping);
+        Resource edge = getOrCreateEdge(graph, diagramResource, mapping, detailedGeometryCoords);
         
         if (writeElevationToEdgeFromPoints) {
             graph.claimLiteral(edge, DN.Edge_HasElevation, calculateElevationFromVertices(graph, startVertex, endVertex), Bindings.DOUBLE);
@@ -107,9 +106,8 @@ public class DNEdgeBuilder {
         return 0;
     }
 
-    public void create(WriteGraph graph,  double[] start, double startElevation, double[] end, double endElevation, double padding) throws DatabaseException {
-        
-        Optional<Resource> edge = create(graph, diagramResource, start, startElevation, end, endElevation, padding);
+    public void create(WriteGraph graph,  double[] start, double startElevation, double[] end, double endElevation, double[] detailedGeometryCoords, double padding) throws DatabaseException {
+        Optional<Resource> edge = create(graph, diagramResource, start, startElevation, end, endElevation, detailedGeometryCoords, padding);
         // 7. Put the element on all the currently active layers if possible.
         if (glm != null) {
             putOnActiveLayer(graph, edge.get());
@@ -155,8 +153,8 @@ public class DNEdgeBuilder {
         return vertex;
     }
 
-    private static Resource getOrCreateEdge(WriteGraph graph, Resource diagramResource, Resource mapping) throws DatabaseException {
-        return DistrictNetworkUtil.createEdge(graph, diagramResource, mapping);
+    private static Resource getOrCreateEdge(WriteGraph graph, Resource diagramResource, Resource mapping, double[] detailedGeometryCoords) throws DatabaseException {
+        return DistrictNetworkUtil.createEdge(graph, diagramResource, mapping, detailedGeometryCoords);
     }
 
     public static class ResourceVertex {
index e71acf0a8ff0e22c2a753ac653b97c3d3e6ca959..4f4583d1a02dc62421d90507022b0f54d26ed652 100644 (file)
@@ -54,8 +54,11 @@ public class DistrictTransformUtil extends TransformUtil {
             int tarZoomLevel = MapScalingTransform.zoomLevel(tar);
             if (tarZoomLevel < 20 && tarZoomLevel > 0) {
                 toBeX = Math.pow(2.0, tarZoomLevel);
-            }
-            else {
+            } else if (tarZoomLevel > 20) {
+                toBeX = Math.pow(2.0, 20);
+            } else if (tarZoomLevel < 0) {
+                toBeX = 2;
+            } else {
                 toBeX = targetX;
             }
             
index 69d1f724193fcbb797ae1eeb0edc403463758c81..d420edc71cd74abe5be18447a2c9cffe2aa0988c 100644 (file)
@@ -87,6 +87,19 @@ public class NetworkDrawingParticipant extends AbstractDiagramParticipant {
         }
         return changed;
     }
+    
+    public boolean isHoveringOverNode(Point2D currentMousePos) {
+        PickRequest req = new PickRequest(currentMousePos).context(getContext());
+        List<IElement> pickables = new ArrayList<IElement>();
+        pick.pick(diagram, req, pickables);
+        for (IElement elem : pickables) {
+            Node node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
+            if (node instanceof DistrictNetworkVertexNode) {
+                return true;
+            }
+        }
+        return false;
+    }
 
     public AffineTransform getTransform() {
         return transform;
index b2121bf70128b358aada26990e75deae7a93b100..524bf7221e42133f76b0644130760af8fd43842a 100644 (file)
@@ -17,9 +17,12 @@ import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Write;
 import org.simantics.diagram.elements.DiagramNodeUtil;
 import org.simantics.diagram.ui.DiagramModelHints;
+import org.simantics.district.network.DistrictNetworkUtil;
 import org.simantics.district.network.ModelledCRS;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.district.network.ui.DNEdgeBuilder;
 import org.simantics.district.network.ui.NetworkDrawingParticipant;
 import org.simantics.g2d.canvas.Hints;
@@ -38,11 +41,17 @@ import org.simantics.scenegraph.utils.NodeUtil;
 
 public class NetworkDrawingNode extends G2DNode {
 
+    static class DrawingNode {
+
+        private List<Point2D> routeNodes = new ArrayList<>();
+    }
+
     private static final long serialVersionUID = -3475301184009620573L;
     
     private Point2D currentMousePos = null;
-    
-    private List<Point2D> nodes = new ArrayList<>();
+
+    private List<DrawingNode> nodes = new ArrayList<>();
+    private DrawingNode currentRouteNode = null;
 
     private Resource diagramResource;
 
@@ -82,35 +91,41 @@ public class NetworkDrawingNode extends G2DNode {
     public void render(Graphics2D g2d) {
         if (nodes.isEmpty())
             return;
-        
-        Path2D path = new Path2D.Double();
-        Iterator<Point2D> nodeIter = nodes.iterator();
-        if (nodeIter.hasNext()) {
-            Point2D node = nodeIter.next();
-            path.moveTo(node.getX(), node.getY());
-        }
-        while (nodeIter.hasNext()) {
-            Point2D node = nodeIter.next();
-            path.lineTo(node.getX(), node.getY());
-        }
-        if (currentMousePos != null)
-            path.lineTo(currentMousePos.getX(), currentMousePos.getY());
-        
+
         Color old = g2d.getColor();
         Stroke oldStroke = g2d.getStroke();
-        
-        if (DASHED_STROKE != null) {
-            if (scaleStroke && DASHED_STROKE instanceof BasicStroke) {
-                BasicStroke bs = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
-                g2d.setStroke(bs);
-            } else {
-                g2d.setStroke(DASHED_STROKE);
+
+        Iterator<DrawingNode> dnodeIterator = nodes.iterator();
+        while (dnodeIterator.hasNext()) {
+            Path2D path = new Path2D.Double();
+            DrawingNode dnode = dnodeIterator.next();
+            Iterator<Point2D> nodeIter = dnode.routeNodes.iterator();
+            if (nodeIter.hasNext()) {
+                Point2D node = nodeIter.next();
+                path.moveTo(node.getX(), node.getY());
             }
-        }
-        
-        g2d.setColor(BLUE_ALPHA);
+            while (nodeIter.hasNext()) {
+                Point2D node = nodeIter.next();
+                path.lineTo(node.getX(), node.getY());
+            }
+            if (!dnodeIterator.hasNext()) {
+                if (currentMousePos != null)
+                    path.lineTo(currentMousePos.getX(), currentMousePos.getY());
+            }
+            
+            if (DASHED_STROKE != null) {
+                if (scaleStroke && DASHED_STROKE instanceof BasicStroke) {
+                    BasicStroke bs = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
+                    g2d.setStroke(bs);
+                } else {
+                    g2d.setStroke(DASHED_STROKE);
+                }
+            }
+            
+            g2d.setColor(BLUE_ALPHA);
 
-        g2d.draw(path);
+            g2d.draw(path);
+        }
         
         g2d.setStroke(oldStroke);
         g2d.setColor(old);
@@ -131,35 +146,39 @@ public class NetworkDrawingNode extends G2DNode {
         // nodes to path2d
         IToolMode mode = getToolMode();
         if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
-            Point2D start = null;
-            Point2D end = null;
-            Iterator<Point2D> nodeIter = nodes.iterator();
-            while (nodeIter.hasNext()) {
-                if (end == null) {
-                    start = nodeIter.next();
-                    if (!nodeIter.hasNext()) {
-                        break;
+            // ok, new routenode starts from here
+            Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
+            Point2D.Double pos = new Point2D.Double(localPos.getX(), localPos.getY());
+            if (currentRouteNode != null) {
+                //currentRouteNode.routeNodes.add(pos);
+                currentRouteNode = new DrawingNode();
+                currentRouteNode.routeNodes.add(pos);
+                nodes.add(currentRouteNode);
+            } else {
+                // ok, this must be creation of dh_point
+                double scale = getTransform().getScaleY();
+                double x = ModelledCRS.xToLongitude(pos.getX() / scale);
+                double y = ModelledCRS.yToLatitude(-pos.getY() / scale);
+                Simantics.getSession().asyncRequest(new Write() {
+                    
+                    @Override
+                    public void perform(WriteGraph graph) throws DatabaseException {
+                        graph.markUndoPoint();
+                        Resource defaultMapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
+                        DistrictNetworkUtil.createVertex(graph, diagramResource, new double[] { x, y }, 0, defaultMapping); // TODO: elevation can be fetched from e.g. elevation API
                     }
-                } else {
-                    start = end;
-                }
-                
-                end = nodeIter.next();
-                
-                createEdge(start, end);
+                });
             }
-            
-            nodes.clear();
-            committed = true;
-            
             repaint();
-            
             return true;
         }
         return super.mouseDoubleClicked(e);
     }
 
-    private void createEdge(Point2D start, Point2D end) {
+    private void createEdge(DrawingNode node) {
+        
+        Point2D start = node.routeNodes.get(0);
+        Point2D end = node.routeNodes.get(node.routeNodes.size() - 1);
         
         double currentPadding = DistrictNetworkVertexNode.width;
         AffineTransform test = getTransform();
@@ -188,12 +207,21 @@ public class NetworkDrawingNode extends G2DNode {
         double[] startCoords = new double[] { startLon, startLat };
         double[] endCoords = new double[] { endLon, endLat };
         
+        double[] detailedGeometryCoords = new double[node.routeNodes.size() * 2];
+        int i = 0;
+        for (Point2D p : node.routeNodes) {
+            double lat = ModelledCRS.yToLatitude(-p.getY() / scaleY);
+            double lon = ModelledCRS.xToLongitude(p.getX() / scaleX);
+            detailedGeometryCoords[i++] = lon;
+            detailedGeometryCoords[i++] = lat;
+        }
+        
         DNEdgeBuilder builder = new DNEdgeBuilder(diagramResource, diagram);
         Simantics.getSession().asyncRequest(new WriteRequest() {
             
             @Override
             public void perform(WriteGraph graph) throws DatabaseException {
-                builder.create(graph, startCoords, 0, endCoords, 0, padding);
+                builder.create(graph, startCoords, 0, endCoords, 0, detailedGeometryCoords, padding);
             }
         });
         
@@ -212,14 +240,35 @@ public class NetworkDrawingNode extends G2DNode {
                 nodes.remove(nodes.size() - 1);
             } else if (e.button == MouseEvent.LEFT_BUTTON) {
                 Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());
-                nodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
+                if (currentRouteNode == null && canStartEdge(localPos)) {
+                    // ok, we can start from here
+                    currentRouteNode = new DrawingNode();
+                    currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
+                    nodes.add(currentRouteNode);
+                } else if (currentRouteNode != null && canStartEdge(localPos)) {
+                    // let's commit our new routenode
+                    currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
+                    Iterator<DrawingNode> nodeIter = nodes.iterator();
+                    while (nodeIter.hasNext()) {
+                        createEdge(nodeIter.next());
+                    }
+                    currentRouteNode = null;
+                    nodes.clear();
+                    committed = true;
+                } else if (currentRouteNode != null) {
+                    currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
+                }
             }
             repaint();
             return true;
         }
         return super.mouseClicked(e);
     }
-    
+
+    private boolean canStartEdge(Point2D currentPos) {
+        return participant.isHoveringOverNode(currentPos);
+    }
+
     private IToolMode getToolMode() {
         return participant.getHint(Hints.KEY_TOOL);
     }
@@ -249,6 +298,7 @@ public class NetworkDrawingNode extends G2DNode {
     @Override
     protected boolean keyPressed(KeyPressedEvent e) {
         if (e.keyCode == java.awt.event.KeyEvent.VK_ESCAPE) {
+            currentRouteNode = null;
             nodes.clear();
             repaint();
             return true;
index 2f46d361ded6b3eb212bec13d941e1dd6288b11a..f96ed607da33ee705fd591a08c946f401b3522ae 100644 (file)
@@ -82,8 +82,8 @@ translateElement elem = do
     ()
 
 importJava "org.simantics.district.network.DistrictNetworkUtil" where
-    createVertex :: Resource -> Vector Double -> Resource -> <WriteGraph, Proc> Resource
-    createEdge :: Resource -> Resource -> <WriteGraph, Proc> Resource
+    createVertex :: Resource -> Vector Double -> Double -> Resource -> <WriteGraph, Proc> Resource
+    createEdge :: Resource -> Resource -> Vector Double -> <WriteGraph, Proc> Resource
 
 """
 Tries to look for the Resource representing the configuration component
index fde334f770d89d7a68cdfe84361692b62bcbf5ba..bac4707d87cc8fe627e0d24f2604503a2fc6d22d 100644 (file)
@@ -29,11 +29,11 @@ import org.simantics.operation.Layer0X;
 
 public class DistrictNetworkUtil {
 
-    public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {
-        return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping));
+    public static Resource createEdge(WriteGraph graph, Resource composite, double[] detailedGeometryCoords) throws DatabaseException {
+        return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping), detailedGeometryCoords);
     }
 
-    public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping) throws DatabaseException {
+    public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping, double[] detailedGeometryCoords) throws DatabaseException {
         Layer0 L0 = Layer0.getInstance(graph);
         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
         if (mapping == null) {
@@ -60,6 +60,8 @@ public class DistrictNetworkUtil {
             });
         }
         
+        // add detailed geometry (if any)
+        graph.claimLiteral(edge, DN.Edge_HasGeometry, detailedGeometryCoords, Bindings.DOUBLE_ARRAY);
         return edge;
     }