From 40b3d2f72133b2804fdc4b1e5c7fb3a18a0b0ec3 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Mon, 8 Apr 2019 12:44:34 +0300 Subject: [PATCH] Allow starting/ending of manual network creation to vertices only 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 --- .../district/imports/ui/CSVImportWizard.java | 2 +- .../district/network/ui/DNEdgeBuilder.java | 18 +-- .../network/ui/DistrictTransformUtil.java | 7 +- .../network/ui/NetworkDrawingParticipant.java | 13 ++ .../network/ui/nodes/NetworkDrawingNode.java | 152 ++++++++++++------ .../scl/Simantics/District.scl | 4 +- .../district/network/DistrictNetworkUtil.java | 8 +- 7 files changed, 135 insertions(+), 69 deletions(-) diff --git a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java index c0d756d3..8b0a377b 100644 --- a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java +++ b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java @@ -263,7 +263,7 @@ public class CSVImportWizard extends Wizard implements IImportWizard { flipAxes(startCoords); flipAxes(endCoords); - Optional oedge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, startZCoord, endCoords, endZCoord, padding, true); + Optional 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); diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java index e47e9a87..92a6187b 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java @@ -45,7 +45,7 @@ public class DNEdgeBuilder { glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER); } - public static Optional create(WriteGraph graph, Resource diagramResource, double[] start, double startElevation, double[] end, double endElevation, double padding) throws DatabaseException { + public static Optional create(WriteGraph graph, Resource diagramResource, double[] start, double startElevation, double[] end, double endElevation, double[] detailedGeometryCoords, double padding) throws DatabaseException { Collection 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 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 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 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 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 { 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 e71acf0a..4f4583d1 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 @@ -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; } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java index 69d1f724..d420edc7 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java @@ -87,6 +87,19 @@ public class NetworkDrawingParticipant extends AbstractDiagramParticipant { } return changed; } + + public boolean isHoveringOverNode(Point2D currentMousePos) { + PickRequest req = new PickRequest(currentMousePos).context(getContext()); + List pickables = new ArrayList(); + 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; diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java index b2121bf7..524bf722 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java @@ -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 routeNodes = new ArrayList<>(); + } + private static final long serialVersionUID = -3475301184009620573L; private Point2D currentMousePos = null; - - private List nodes = new ArrayList<>(); + + private List 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 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 dnodeIterator = nodes.iterator(); + while (dnodeIterator.hasNext()) { + Path2D path = new Path2D.Double(); + DrawingNode dnode = dnodeIterator.next(); + Iterator 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 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 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; diff --git a/org.simantics.district.network/scl/Simantics/District.scl b/org.simantics.district.network/scl/Simantics/District.scl index 2f46d361..f96ed607 100644 --- a/org.simantics.district.network/scl/Simantics/District.scl +++ b/org.simantics.district.network/scl/Simantics/District.scl @@ -82,8 +82,8 @@ translateElement elem = do () importJava "org.simantics.district.network.DistrictNetworkUtil" where - createVertex :: Resource -> Vector Double -> Resource -> Resource - createEdge :: Resource -> Resource -> Resource + createVertex :: Resource -> Vector Double -> Double -> Resource -> Resource + createEdge :: Resource -> Resource -> Vector Double -> Resource """ Tries to look for the Resource representing the configuration component diff --git a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java index fde334f7..bac4707d 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java +++ b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java @@ -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; } -- 2.43.2