Search in sources :

Example 6 with EdgeLabelPlacement

use of org.eclipse.elk.core.options.EdgeLabelPlacement in project elk by eclipse.

the class LayoutDotExporter method setEdgeLabels.

@Override
protected void setEdgeLabels(final ElkEdge kedge, final List<Attribute> attributes, final boolean isVertical) {
    super.setEdgeLabels(kedge, attributes, isVertical);
    // set label distance and angle
    if (Iterables.any(kedge.getLabels(), (ElkLabel label) -> {
        EdgeLabelPlacement elp = label.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
        return elp == EdgeLabelPlacement.HEAD || elp == EdgeLabelPlacement.TAIL;
    })) {
        double distance = kedge.getProperty(GraphvizMetaDataProvider.LABEL_DISTANCE);
        if (distance >= 0.0) {
            attributes.add(createAttribute(Attributes.LABELDISTANCE, distance));
        }
        double angle = kedge.getProperty(GraphvizMetaDataProvider.LABEL_ANGLE);
        attributes.add(createAttribute(Attributes.LABELANGLE, angle));
    }
}
Also used : ElkLabel(org.eclipse.elk.graph.ElkLabel) EdgeLabelPlacement(org.eclipse.elk.core.options.EdgeLabelPlacement)

Example 7 with EdgeLabelPlacement

use of org.eclipse.elk.core.options.EdgeLabelPlacement in project elk by eclipse.

the class DotExporter method applyEdgeLabelPos.

/**
 * Applies the edge label positions for the given edge.
 *
 * @param elkedge edge for which labels are processed
 * @param posString string with label position
 * @param placement label placement to choose
 * @param offsetx x offset added to positions
 * @param offsety y offset added to positions
 */
private void applyEdgeLabelPos(final ElkEdge elkedge, final String posString, final EdgeLabelPlacement placement, final KVector offset) {
    double combinedWidth = 0.0;
    double combinedHeight = 0.0;
    for (ElkLabel label : elkedge.getLabels()) {
        EdgeLabelPlacement elp = label.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
        if (elp == placement) {
            combinedWidth = Math.max(combinedWidth, label.getWidth());
            combinedHeight += label.getHeight();
        }
    }
    try {
        KVector pos = new KVector();
        pos.parse(posString);
        double xpos = pos.x - combinedWidth / 2 + offset.x;
        double ypos = pos.y - combinedHeight / 2 + offset.y;
        for (ElkLabel label : elkedge.getLabels()) {
            EdgeLabelPlacement elp = label.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
            if (elp == placement) {
                double xoffset = (combinedWidth - label.getWidth()) / 2;
                label.setX(xpos + xoffset);
                label.setY(ypos);
                ypos += label.getHeight();
            }
        }
    } catch (IllegalArgumentException exception) {
    // ignore exception
    }
}
Also used : ElkLabel(org.eclipse.elk.graph.ElkLabel) EdgeLabelPlacement(org.eclipse.elk.core.options.EdgeLabelPlacement) KVector(org.eclipse.elk.core.math.KVector)

Example 8 with EdgeLabelPlacement

use of org.eclipse.elk.core.options.EdgeLabelPlacement in project elk by eclipse.

the class DotExporter method setEdgeLabels.

/**
 * Set edge labels for the given edge.
 *
 * @param elkedge edge whose labels shall be set
 * @param attributes edge attribute list to which the labels are added
 * @param isVertical indicates whether vertical layout direction is active
 */
protected void setEdgeLabels(final ElkEdge elkedge, final List<Attribute> attributes, final boolean isVertical) {
    if (elkedge.getLabels().isEmpty()) {
        return;
    }
    // as Graphviz only supports positioning of one label per label placement, all labels
    // are stacked to one big label as workaround
    StringBuilder midLabel = new StringBuilder(), headLabel = new StringBuilder(), tailLabel = new StringBuilder();
    String fontName = null;
    int fontSize = 0;
    boolean isCenterFontName = false, isCenterFontSize = false;
    for (ElkLabel label : elkedge.getLabels()) {
        StringBuilder buffer = midLabel;
        EdgeLabelPlacement placement = label.getProperty(CoreOptions.EDGE_LABELS_PLACEMENT);
        boolean takeFontName = false, takeFontSize = false;
        switch(placement) {
            case HEAD:
                takeFontName = fontName == null;
                takeFontSize = fontSize <= 0;
                buffer = headLabel;
                break;
            case TAIL:
                takeFontName = fontName == null;
                takeFontSize = fontSize <= 0;
                buffer = tailLabel;
                break;
            default:
                // CENTER
                takeFontName = fontName == null || !isCenterFontName;
                isCenterFontName = true;
                takeFontSize = fontSize <= 0 || !isCenterFontSize;
                isCenterFontSize = true;
                break;
        }
        if (buffer.length() > 0) {
            buffer.append("\n");
        }
        buffer.append(label.getText());
        if (takeFontName) {
            fontName = label.getProperty(CoreOptions.FONT_NAME);
        }
        if (takeFontSize) {
            fontSize = label.getProperty(CoreOptions.FONT_SIZE);
            // increase the font size to let Graphviz prepare more room for the label
            if (fontSize > 0) {
                fontSize *= FONT_SIZE_MULT;
            }
        }
    }
    // edge overlapping
    if (midLabel.length() > 0) {
        double labelSpacing = elkedge.getProperty(CoreOptions.SPACING_EDGE_LABEL);
        if (labelSpacing < 1) {
            labelSpacing = 0;
        }
        int charsToAdd = (int) labelSpacing - 1;
        for (int i = 0; i < charsToAdd; i++) {
            midLabel.append(isVertical ? "O" : "\nO");
        }
        attributes.add(createAttribute(Attributes.LABEL, createString(midLabel.toString())));
    }
    // set head label
    if (headLabel.length() > 0) {
        attributes.add(createAttribute(Attributes.HEADLABEL, createString(headLabel.toString())));
    }
    // set tail label
    if (tailLabel.length() > 0) {
        attributes.add(createAttribute(Attributes.TAILLABEL, createString(tailLabel.toString())));
    }
    // set font name
    if (fontName != null && fontName.length() > 0) {
        attributes.add(createAttribute(Attributes.FONTNAME, "\"" + fontName + "\""));
    }
    // set font size
    if (fontSize > 0) {
        attributes.add(createAttribute(Attributes.FONTSIZE, fontSize));
    }
}
Also used : ElkLabel(org.eclipse.elk.graph.ElkLabel) EdgeLabelPlacement(org.eclipse.elk.core.options.EdgeLabelPlacement) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint)

Aggregations

EdgeLabelPlacement (org.eclipse.elk.core.options.EdgeLabelPlacement)8 ElkLabel (org.eclipse.elk.graph.ElkLabel)3 LLabel (org.eclipse.elk.alg.layered.graph.LLabel)2 KVector (org.eclipse.elk.core.math.KVector)2 ElkEdge (org.eclipse.elk.graph.ElkEdge)2 ElkGraphElement (org.eclipse.elk.graph.ElkGraphElement)2 ElkNode (org.eclipse.elk.graph.ElkNode)2 ElkPort (org.eclipse.elk.graph.ElkPort)2 EObject (org.eclipse.emf.ecore.EObject)2 EReference (org.eclipse.emf.ecore.EReference)2 ConnectionEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart)2 LabelEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart)2 HashMap (java.util.HashMap)1 Point (org.eclipse.draw2d.geometry.Point)1 PointList (org.eclipse.draw2d.geometry.PointList)1 PrecisionPoint (org.eclipse.draw2d.geometry.PrecisionPoint)1 Rectangle (org.eclipse.draw2d.geometry.Rectangle)1 LEdge (org.eclipse.elk.alg.layered.graph.LEdge)1 LNode (org.eclipse.elk.alg.layered.graph.LNode)1 LPort (org.eclipse.elk.alg.layered.graph.LPort)1