From: Reino Ruusu Date: Fri, 19 Jul 2019 11:24:28 +0000 (+0300) Subject: Show dynamic symbols when vertices and/or edges are hidden. X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=899b77b837b4473c0176dc9eb1b67e632b98db25;p=simantics%2Fdistrict.git Show dynamic symbols when vertices and/or edges are hidden. gitlab #27 Change-Id: Id9cda3403d1f318b7fd29dd31eeea632749328a5 --- diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java index 4d54246d..a089ecaf 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java @@ -16,6 +16,7 @@ import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElementFact import org.simantics.maps.MapScalingTransform; import org.simantics.scenegraph.INode; import org.simantics.scenegraph.ISelectionPainterNode; +import org.simantics.scenegraph.INode.PropertySetter; import org.simantics.scenegraph.g2d.G2DNode; import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.scenegraph.g2d.nodes.SVGNode; @@ -50,6 +51,8 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection private transient Rectangle2D symbolRect; private transient AffineTransform symbolTransform; + private boolean hidden = false; + private Double arrowLength; private static double startX; @@ -70,89 +73,95 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection g2d.transform(getTransform()); } - Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - - Color oldColor = g2d.getColor(); - BasicStroke oldStroke = (BasicStroke) g2d.getStroke(); - - BasicStroke bs = null; double scale = 1.0; if (scaleStroke) { AffineTransform tr = g2d.getTransform(); scale = DistrictNetworkNodeUtils.getScale(tr); - bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale)); - } else { - bs = STROKE; } - int zoomLevel = MapScalingTransform.zoomLevel(ot); - path = calculatePath(edge, path, zoomLevel > 15); - - if (isSelected()) { - g2d.setColor(SELECTION_COLOR); - g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale))); - g2d.draw(path); - } - - g2d.setColor(dynamicColor != null ? dynamicColor : color); - g2d.setStroke(bs); - g2d.draw(path); - - // Draw arrow - if (arrowLength != null) { - g2d.setColor(Color.BLACK); - float lw = STROKE.getLineWidth() / (float)scale; - g2d.setStroke(new BasicStroke(lw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); - - double l = arrowLength; - double w = 2 * (double) lw * Math.signum(l); - if (Math.abs(w) > Math.abs(l)) w = l; - double offset = 2 * (double) lw; - - double centerX = (startX + endX) / 2, centerY = (startY + endY) / 2; - double deltaX = endX - startX, deltaY = endY - startY; - double length = Math.sqrt(deltaX * deltaX + deltaY * deltaY); - deltaX /= length; - deltaY /= length; - - double x0 = centerX - l/2 * deltaX + offset * deltaY; - double y0 = centerY - l/2 * deltaY - offset * deltaX; - double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY; - double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX; + + if (!hidden) { + Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + + Color oldColor = g2d.getColor(); + BasicStroke oldStroke = (BasicStroke) g2d.getStroke(); + + BasicStroke bs = null; + if (scaleStroke) { + bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale)); + } else { + bs = STROKE; + } - g2d.draw(new Line2D.Double(x0, y0, x1, y1)); + int zoomLevel = MapScalingTransform.zoomLevel(ot); + path = calculatePath(edge, path, zoomLevel > 15); + + if (isSelected()) { + g2d.setColor(SELECTION_COLOR); + g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale))); + g2d.draw(path); + } + + g2d.setColor(dynamicColor != null ? dynamicColor : color); + g2d.setStroke(bs); + g2d.draw(path); + + // Draw arrow + if (arrowLength != null) { + g2d.setColor(Color.BLACK); + float lw = STROKE.getLineWidth() / (float)scale; + g2d.setStroke(new BasicStroke(lw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); + + double l = arrowLength; + double w = 2 * (double) lw * Math.signum(l); + if (Math.abs(w) > Math.abs(l)) w = l; + double offset = 2 * (double) lw; + + double centerX = (startX + endX) / 2, centerY = (startY + endY) / 2; + double deltaX = endX - startX, deltaY = endY - startY; + double length = Math.sqrt(deltaX * deltaX + deltaY * deltaY); + deltaX /= length; + deltaY /= length; + + double x0 = centerX - l/2 * deltaX + offset * deltaY; + double y0 = centerY - l/2 * deltaY - offset * deltaX; + double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY; + double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX; + + g2d.draw(new Line2D.Double(x0, y0, x1, y1)); + + Path2D path = new Path2D.Double(); + path.moveTo(x1 + w * deltaX, y1 + w * deltaY); + path.lineTo(x1 + w * deltaY, y1 - w * deltaX); + path.lineTo(x1 - w * deltaY, y1 + w * deltaX); + path.closePath(); + g2d.fill(path); + } - Path2D path = new Path2D.Double(); - path.moveTo(x1 + w * deltaX, y1 + w * deltaY); - path.lineTo(x1 + w * deltaY, y1 - w * deltaX); - path.lineTo(x1 - w * deltaY, y1 + w * deltaX); - path.closePath(); - g2d.fill(path); - } - - // Reset - g2d.setStroke(oldStroke); - g2d.setColor(oldColor); - - // Render SVG symbol - double viewScaleRecip = 10; - if (scaleStroke) { - viewScaleRecip /= scale; + // Reset + g2d.setStroke(oldStroke); + g2d.setColor(oldColor); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint); } - - Point2D p = getCenterPoint(); - symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip); - symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform); - + for (INode nn : getNodes()) { G2DNode g2dNode = (G2DNode)nn; - if (g2dNode instanceof SVGNode) + if (g2dNode instanceof SVGNode) { + // Render SVG symbol + double viewScaleRecip = 10; + if (scaleStroke) { + viewScaleRecip /= scale; + } + + Point2D p = getCenterPoint(); + symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip); + symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform); + g2dNode.setTransform(symbolTransform); + } g2dNode.render(g2d); } - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint); - if (ot != null) g2d.setTransform(ot); } @@ -284,4 +293,9 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection ((SVGNode)nn).setData(value); } + + @PropertySetter(value = "hidden") + public void setHidden(Boolean value) { + this.hidden = value; + } } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java index 061a883e..2e2b4293 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java @@ -51,6 +51,8 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti private double nodeSize = 1.0; + private boolean hidden = false; + @Override public void init() { setZIndex(2); @@ -58,9 +60,6 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti @Override public void render(Graphics2D g2d) { - if (nodeSize <= 0.0) - return; - AffineTransform ot = null; AffineTransform t = getTransform(); if (t != null && !t.isIdentity()) { @@ -68,50 +67,55 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti g2d.transform(t); } - Object oaaHint = null; - Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); - if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) { - oaaHint = aaHint; - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); - } - - Color oldColor = g2d.getColor(); - Color newColor = dynamicColor != null ? dynamicColor : color; - boolean changeColor = !oldColor.equals(newColor); - + // Translate lat and lon to X and Y + Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point); + double viewScaleRecip = 1; if (scaleStroke) { viewScaleRecip *= DistrictNetworkNodeUtils.calculateScaleRecip(g2d.getTransform()); } - double scaleRecip = viewScaleRecip * nodeSize; - - // Translate lat and lon to X and Y - Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point); - Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip); - - if (NodeUtil.isSelected(this, 1)) { - changeColor = true; - g2d.setColor(SELECTION_COLOR); - BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip); - g2d.setStroke(ss); - g2d.draw(toDraw); - } - - // render - if (changeColor) - g2d.setColor(newColor); - g2d.fill(toDraw); - // Reset settings - if (changeColor) - g2d.setColor(oldColor); - if (oaaHint != null) - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint); + if (!hidden && nodeSize > 0.0) { + Object oaaHint = null; + Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); + if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) { + oaaHint = aaHint; + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); + } + + Color oldColor = g2d.getColor(); + Color newColor = dynamicColor != null ? dynamicColor : color; + boolean changeColor = !oldColor.equals(newColor); + + double scaleRecip = viewScaleRecip * nodeSize; + + Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip); + + if (NodeUtil.isSelected(this, 1)) { + changeColor = true; + g2d.setColor(SELECTION_COLOR); + BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip); + g2d.setStroke(ss); + g2d.draw(toDraw); + } + + // render + if (changeColor) + g2d.setColor(newColor); + g2d.fill(toDraw); + + // Reset settings + if (changeColor) + g2d.setColor(oldColor); + if (oaaHint != null) + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint); + } // Render SVG symbol for (INode nn : getNodes()) { G2DNode g2dNode = (G2DNode)nn; if (nn instanceof SVGNode) { + Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, viewScaleRecip); symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform); g2dNode.setTransform(symbolTransform); } @@ -231,4 +235,8 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti this.dynamicColor = color; } + @PropertySetter(value = "hidden") + public void setHidden(Boolean value) { + this.hidden = value; + } } diff --git a/org.simantics.district.network/src/org/simantics/district/network/profile/HideStyle.java b/org.simantics.district.network/src/org/simantics/district/network/profile/HideStyle.java index e1d8bd04..d5f57c82 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/profile/HideStyle.java +++ b/org.simantics.district.network/src/org/simantics/district/network/profile/HideStyle.java @@ -3,16 +3,11 @@ package org.simantics.district.network.profile; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; -import org.simantics.diagram.profile.ProfileKeys; import org.simantics.diagram.profile.StyleBase; -import org.simantics.g2d.diagram.IDiagram; -import org.simantics.g2d.diagram.handler.DataElementMap; -import org.simantics.g2d.element.ElementUtils; -import org.simantics.g2d.element.IElement; import org.simantics.scenegraph.INode; import org.simantics.scenegraph.g2d.nodes.SingleElementNode; -import org.simantics.scenegraph.profile.DataNodeMap; import org.simantics.scenegraph.profile.EvaluationContext; +import org.simantics.scenegraph.profile.common.ProfileVariables; /** * @author Tuukka Lehtonen @@ -25,51 +20,17 @@ public class HideStyle extends StyleBase { } @Override - public void applyStyleForItem(EvaluationContext evaluationContext, DataNodeMap map, Object item, Boolean value) { - INode node = map.getNode(item); - if (node == null) { - evaluationContext.update(); - return; - } - - IDiagram diagram = evaluationContext.getConstant(ProfileKeys.DIAGRAM); - DataElementMap emap = diagram.getDiagramClass().getSingleItem(DataElementMap.class); - IElement element = emap.getElement(diagram, item); - if (element == null) - return; - - hideElements(evaluationContext, diagram, element, node); + public void applyStyleForNode(EvaluationContext evaluationContext, INode node, Boolean result) { + SingleElementNode n = (SingleElementNode) node; + for (INode nn : n.getNodes()) + ProfileVariables.claimNodeProperty(nn, "hidden", true, evaluationContext); } - + @Override - protected void cleanupStyleForItem(EvaluationContext evaluationContext, DataNodeMap map, Object item) { - IDiagram diagram = evaluationContext.getConstant(ProfileKeys.DIAGRAM); - DataElementMap emap = diagram.getDiagramClass().getSingleItem(DataElementMap.class); - - INode node = map.getNode(item); - if (node == null) - return; - IElement element = emap.getElement(diagram, item); - if (element == null) - return; - - showElements(diagram, element, node); - } - - protected void hideElements(EvaluationContext evaluationContext, IDiagram diagram, IElement element, INode node) { - setVisibility(element, node, false); - } - - protected void showElements(IDiagram diagram, IElement element, INode node) { - setVisibility(element, node, true); - } - - protected void setVisibility(IElement element, INode node, boolean visible) { - if (element != null) - ElementUtils.setHidden(element, !visible); - if (node instanceof SingleElementNode) { - ((SingleElementNode) node).setHidden(!visible); - } + protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) { + SingleElementNode n = (SingleElementNode) node; + for (INode nn : n.getNodes()) + ProfileVariables.claimNodeProperty(nn, "hidden", false, evaluationContext); } @Override