Skip to content

Commit

Permalink
Merge pull request #165 from kieler/mka/autopadding
Browse files Browse the repository at this point in the history
Add property to automatically set minimal paddings for rounded rectangles
  • Loading branch information
NiklasRentzCAU authored Jul 18, 2023
2 parents ab02a2c + 6ef6809 commit 572bfab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.elk.core.math.ElkPadding;
import org.eclipse.elk.core.math.KVector;
import org.eclipse.elk.core.options.CoreOptions;
import org.eclipse.elk.core.service.IDiagramLayoutConnector;
Expand Down Expand Up @@ -377,9 +378,28 @@ private void createNode(final LayoutMapping mapping, final KNode node,

// KLighD is somewhat mean and doesn't care about existing insets
node.setInsets(insets);
node.getProperty(CoreOptions.PADDING);
// The Insets are used in {@link KlighdLayoutConfigurationStore} to retrieve the padding
// of the node

if (node.getProperty(KlighdProperties.ROUNDED_RECTANGLE_AUTOPADDING) != null) {
KVector radii = node.getProperty(KlighdProperties.ROUNDED_RECTANGLE_AUTOPADDING);
double padding = 0;
// threshold to check for almost zero-ness
double EPSILON = 0.00001;
// x and y cannot be zero, if one of the two is zero use the larger value
if (radii.x < EPSILON && radii.y >= EPSILON) {
radii.x = EPSILON;
} else if (radii.x >= EPSILON && radii.y < EPSILON) {
radii.y = EPSILON;
}
if (!(radii.x < EPSILON && radii.y < EPSILON)) {
// computes a padding x such that the corners of the inner rectangle fit exactly within the rounded rectangle
double numerator = radii.x * radii.y * (-Math.sqrt(2) * Math.sqrt(radii.x * radii.y) + radii.x + radii.y);
double denominator = radii.x * radii.x + radii.y * radii.y;
padding = numerator / denominator;
}
node.setProperty(CoreOptions.PADDING, new ElkPadding(padding));
}

layoutParent.getChildren().add(layoutNode);
mapping.getGraphMap().put(layoutNode, node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,13 @@ public static boolean isSelectable(final EObject viewElement) {
*/
public static final IProperty<Boolean> IS_NODE_TITLE =
new Property<Boolean>("klighd.isNodeTitle", false);

/**
* Automatically computes the padding required to fit the content of a node within the bounds of a
* rounded rectangle. The x and y corner radii are specified as x and y of a KVector. If this property
* is set, the ElkPadding set on the graph will be overridden internally. This property should only be
* used if no padding is manually set on the graph element.
*/
public static final IProperty<KVector> ROUNDED_RECTANGLE_AUTOPADDING =
new Property<KVector>("klighd.roundedRectangle.autopadding", null);
}

0 comments on commit 572bfab

Please sign in to comment.