Search in sources :

Example 6 with ElkLabel

use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.

the class GraphRenderer method calculateBaseOffsetFromTopLevelGraph.

/**
 * Calculate the base offset so all graph elements can fit onto the canvas.
 *
 * @param parent the graph to be drawn
 */
public void calculateBaseOffsetFromTopLevelGraph(final ElkNode parent) {
    double minX = Double.POSITIVE_INFINITY;
    double minY = Double.POSITIVE_INFINITY;
    for (ElkNode childNode : parent.getChildren()) {
        minX = Math.min(minX, childNode.getX());
        minY = Math.min(minY, childNode.getY());
        for (ElkLabel childNodeLabel : childNode.getLabels()) {
            minX = Math.min(minX, childNode.getX() + childNodeLabel.getX());
            minY = Math.min(minY, childNode.getY() + childNodeLabel.getY());
        }
    }
    for (ElkEdge edge : parent.getContainedEdges()) {
        PaintRectangle edgeRect = new PaintRectangle(edge, new KVector(), scale);
        minX = Math.min(minX, edgeRect.x);
        minY = Math.min(minY, edgeRect.y);
        for (ElkLabel edgeLabel : edge.getLabels()) {
            minX = Math.min(minX, edgeLabel.getX());
            minY = Math.min(minY, edgeLabel.getY());
        }
    }
    baseOffset.x = -Math.min(0, minX);
    baseOffset.y = -Math.min(0, minY);
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ElkLabel(org.eclipse.elk.graph.ElkLabel) KVector(org.eclipse.elk.core.math.KVector) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 7 with ElkLabel

use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.

the class GraphRenderer method renderEdge.

/**
 * Paints an edge for the given dirty area.
 *
 * @param edge the edge to paint
 * @param graphics the graphics context used to paint
 * @param area dirty area that needs painting
 * @param offset offset to be added to relative coordinates
 * @param labelAlpha alpha value for labels
 */
private void renderEdge(final ElkEdge edge, final GC graphics, final Rectangle area, final KVector offset, final int labelAlpha) {
    if (configurator.getEdgeColor() == null) {
        return;
    }
    // Find our if the edge is actually eligible to be painted
    if (isEdgeFullyContainedInGraphToDraw(edge)) {
        // Get a PaintRectangle ready for the edge
        PaintRectangle rect = boundsMap.get(edge);
        if (rect == null) {
            rect = new PaintRectangle(edge, offset, scale);
            boundsMap.put(edge, rect);
        }
        if (!rect.painted && rect.intersects(area)) {
            // Gather some information
            final boolean splineEdge = edge.getProperty(CoreOptions.EDGE_ROUTING) == EdgeRouting.SPLINES;
            final boolean directedEdge = edge.getProperty(CoreOptions.EDGE_TYPE) != EdgeType.UNDIRECTED;
            graphics.setAlpha(255);
            // The background color is required to fill the arrow of directed edges
            graphics.setForeground(configurator.getEdgeColor());
            graphics.setBackground(configurator.getEdgeColor());
            for (ElkEdgeSection edgeSection : edge.getSections()) {
                KVectorChain bendPoints = ElkUtil.createVectorChain(edgeSection);
                bendPoints.scale(scale).offset(offset);
                // Draw the damn edge already...!
                Path path = new Path(graphics.getDevice());
                Iterator<KVector> pointIter = bendPoints.iterator();
                KVector startPoint = pointIter.next();
                path.moveTo((float) startPoint.x, (float) startPoint.y);
                KVector point1 = null;
                KVector point2 = null;
                while (pointIter.hasNext()) {
                    if (splineEdge) {
                        if (point1 == null) {
                            point1 = pointIter.next();
                        } else if (point2 == null) {
                            point2 = pointIter.next();
                        } else {
                            KVector endPoint = pointIter.next();
                            path.cubicTo((float) point1.x, (float) point1.y, (float) point2.x, (float) point2.y, (float) endPoint.x, (float) endPoint.y);
                            point1 = null;
                            point2 = null;
                        }
                    } else {
                        KVector nextPoint = pointIter.next();
                        path.lineTo((float) nextPoint.x, (float) nextPoint.y);
                    }
                }
                if (splineEdge && point2 != null) {
                    path.quadTo((float) point1.x, (float) point1.y, (float) point2.x, (float) point2.y);
                } else if (splineEdge && point1 != null) {
                    path.lineTo((float) point1.x, (float) point1.y);
                }
                graphics.drawPath(path);
                if (directedEdge) {
                    // Draw an arrow at the last segment of the connection
                    KVector referencePoint;
                    if (splineEdge && (bendPoints.size() - 1) % 3 != 1) {
                        int beginIndex;
                        if ((bendPoints.size() - 1) % 3 == 2) {
                            beginIndex = bendPoints.size() - 2;
                        } else {
                            beginIndex = bendPoints.size() - 3;
                        }
                        referencePoint = ElkMath.getPointOnBezierSegment(0.5, bendPoints.toArray(beginIndex));
                    } else {
                        referencePoint = bendPoints.get(bendPoints.size() - 2);
                    }
                    int[] arrowPoly = makeArrow(referencePoint, bendPoints.getLast());
                    if (arrowPoly != null) {
                        graphics.fillPolygon(arrowPoly);
                    }
                }
            }
            rect.painted = true;
        }
    }
    // paint junction points
    KVectorChain vc = edge.getProperty(CoreOptions.JUNCTION_POINTS);
    if (vc != null) {
        for (KVector v : vc) {
            KVector center = v.clone().scale(scale).add(offset).sub(2, 2);
            graphics.fillOval((int) center.x, (int) center.y, 6, 6);
        }
    }
    // paint the edge labels
    if (configurator.getEdgeLabelFont() != null) {
        graphics.setFont(configurator.getEdgeLabelFont());
        for (ElkLabel label : edge.getLabels()) {
            renderLabel(label, graphics, area, offset, labelAlpha);
        }
    }
}
Also used : Path(org.eclipse.swt.graphics.Path) ElkLabel(org.eclipse.elk.graph.ElkLabel) KVectorChain(org.eclipse.elk.core.math.KVectorChain) KVector(org.eclipse.elk.core.math.KVector) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection) ElkBendPoint(org.eclipse.elk.graph.ElkBendPoint)

Example 8 with ElkLabel

use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.

the class GraphRenderingCanvas method calculateRequiredCanvasSizeAndBaseOffset.

/**
 * Sets size of the canvas by looking up the biggest distances between graph elements in x- and y-direction
 * and return a KVector with the required offset of the origin coordinates to fit all elements on the canvas.
 *
 * @param graph to be painted
 */
private KVector calculateRequiredCanvasSizeAndBaseOffset(final ElkNode graph) {
    if (graph != null) {
        double minX = Double.MAX_VALUE;
        double maxX = Double.MIN_VALUE;
        double minY = Double.MAX_VALUE;
        double maxY = Double.MIN_VALUE;
        // check all nodes for their coordinates
        for (ElkNode node : graph.getChildren()) {
            minX = Math.min(minX, node.getX());
            maxX = Math.max(maxX, node.getX() + node.getWidth());
            minY = Math.min(minY, node.getY());
            maxY = Math.max(maxY, node.getY() + node.getHeight());
            // check node labels
            for (ElkLabel nodeLabel : node.getLabels()) {
                minX = Math.min(minX, node.getX() + nodeLabel.getX());
                maxX = Math.max(maxX, node.getX() + nodeLabel.getX() + nodeLabel.getWidth());
                minY = Math.min(minY, node.getY() + nodeLabel.getY());
                maxY = Math.max(maxY, node.getY() + nodeLabel.getY() + nodeLabel.getHeight());
            }
        }
        for (ElkEdge edge : graph.getContainedEdges()) {
            // check all sections of the edges for their coordinates
            for (ElkEdgeSection edgeSection : edge.getSections()) {
                minX = Math.min(minX, edgeSection.getStartX());
                minY = Math.min(minY, edgeSection.getStartY());
                maxX = Math.max(maxX, edgeSection.getStartX());
                maxY = Math.max(maxY, edgeSection.getStartY());
                minX = Math.min(minX, edgeSection.getEndX());
                minY = Math.min(minY, edgeSection.getEndY());
                maxX = Math.max(maxX, edgeSection.getEndX());
                maxY = Math.max(maxY, edgeSection.getEndY());
                for (ElkBendPoint bendPoint : edgeSection.getBendPoints()) {
                    minX = Math.min(minX, bendPoint.getX());
                    minY = Math.min(minY, bendPoint.getY());
                    maxX = Math.max(maxX, bendPoint.getX());
                    maxY = Math.max(maxY, bendPoint.getY());
                }
            }
            // check edge labels
            for (ElkLabel edgeLabel : edge.getLabels()) {
                minX = Math.min(minX, edgeLabel.getX());
                maxX = Math.max(maxX, edgeLabel.getX() + edgeLabel.getWidth());
                minY = Math.min(minY, edgeLabel.getY());
                maxY = Math.max(maxY, edgeLabel.getY() + edgeLabel.getHeight());
            }
        }
        int x = ((int) (Math.max(graph.getWidth(), maxX) - Math.min(0, minX))) + 1;
        int y = ((int) (Math.max(graph.getHeight(), maxY) - Math.min(0, minY))) + 1;
        setSize(new Point(x, y));
        return new KVector((-Math.min(0, minX)), (-Math.min(0, minY)));
    }
    return new KVector();
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ElkLabel(org.eclipse.elk.graph.ElkLabel) ElkBendPoint(org.eclipse.elk.graph.ElkBendPoint) Point(org.eclipse.swt.graphics.Point) ElkBendPoint(org.eclipse.elk.graph.ElkBendPoint) KVector(org.eclipse.elk.core.math.KVector) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection) Point(org.eclipse.swt.graphics.Point) ElkBendPoint(org.eclipse.elk.graph.ElkBendPoint) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 9 with ElkLabel

use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.

the class NodeMicroLayoutTest method verifyNodeLabelsHaveBeenPositioned.

private void verifyNodeLabelsHaveBeenPositioned(final ElkNode node) {
    final double beforeCenterThreshold = 40;
    final double afterCenterThreshold = 60;
    // INSIDE V_TOP H_CENTER
    ElkLabel a = getLabel(node, "A");
    assertThat(a.getY(), greaterThan(0.0));
    // INSIDE V_BOTTOM H_RIGHT
    ElkLabel b = getLabel(node, "B");
    assertThat(b.getX(), greaterThan(afterCenterThreshold));
    assertThat(b.getX(), lessThan(node.getWidth()));
    assertThat(b.getY(), greaterThan(afterCenterThreshold));
    assertThat(b.getY(), lessThan(node.getHeight()));
    // OUTSIDE V_BOTTOM H_CENTER
    ElkLabel c = getLabel(node, "C");
    assertThat(c.getX(), greaterThan(beforeCenterThreshold));
    assertThat(c.getX(), lessThan(afterCenterThreshold));
    assertThat(c.getY(), greaterThan(node.getHeight()));
    // OUTSIDE V_CENTER H_LEFT
    ElkLabel d = getLabel(node, "D");
    assertThat(d.getX(), lessThan(0.0));
    assertThat(d.getY(), greaterThan(beforeCenterThreshold));
    assertThat(d.getY(), lessThan(afterCenterThreshold));
}
Also used : ElkLabel(org.eclipse.elk.graph.ElkLabel)

Example 10 with ElkLabel

use of org.eclipse.elk.graph.ElkLabel in project elk by eclipse.

the class PortLabelPlacementVariantsTest method assertBelowOrRight.

/* - - - Checks and utility - - - */
private void assertBelowOrRight(final ElkPort port) {
    final ElkLabel label = getLabel(port);
    // Note that port label positions are relative to the port
    double portPosition = 0;
    if (PortSide.SIDES_EAST_WEST.contains(port.getProperty(CoreOptions.PORT_SIDE))) {
        portPosition = label.getY();
    } else if (PortSide.SIDES_NORTH_SOUTH.contains(port.getProperty(CoreOptions.PORT_SIDE))) {
        portPosition = label.getX();
    } else {
        assertTrue(false);
    }
    assertThat(portPosition, OrderingComparison.greaterThan(0.0));
}
Also used : ElkLabel(org.eclipse.elk.graph.ElkLabel)

Aggregations

ElkLabel (org.eclipse.elk.graph.ElkLabel)64 ElkNode (org.eclipse.elk.graph.ElkNode)33 KVector (org.eclipse.elk.core.math.KVector)25 ElkPort (org.eclipse.elk.graph.ElkPort)23 ElkEdge (org.eclipse.elk.graph.ElkEdge)20 ElkEdgeSection (org.eclipse.elk.graph.ElkEdgeSection)14 KVectorChain (org.eclipse.elk.core.math.KVectorChain)12 ElkPadding (org.eclipse.elk.core.math.ElkPadding)10 ElkGraphElement (org.eclipse.elk.graph.ElkGraphElement)9 SizeConstraint (org.eclipse.elk.core.options.SizeConstraint)7 PortSide (org.eclipse.elk.core.options.PortSide)6 List (java.util.List)5 LLabel (org.eclipse.elk.alg.layered.graph.LLabel)5 LPort (org.eclipse.elk.alg.layered.graph.LPort)5 ElkBendPoint (org.eclipse.elk.graph.ElkBendPoint)5 ElkConnectableShape (org.eclipse.elk.graph.ElkConnectableShape)5 Test (org.junit.Test)5 Lists (com.google.common.collect.Lists)4 Rectangle (org.eclipse.draw2d.geometry.Rectangle)4 DCElement (org.eclipse.elk.alg.disco.graph.DCElement)4