Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add property to automatically set minimal paddings for rounded rectangles #165

Merged
merged 3 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,26 @@ 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;
// x and y cannot be zero, if one of the two is zero use the larger value
if (radii.x < 0.00001 && radii.y >= 0.00001) {
radii.x = 0.00001;
} else if (radii.x >= 0.00001 && radii.y < 0.00001) {
radii.y = 0.00001;
}
if (!(radii.x < 0.00001 && radii.y < 0.00001)) {
NiklasRentzCAU marked this conversation as resolved.
Show resolved Hide resolved
// 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,11 @@ 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.
*/
Eddykasp marked this conversation as resolved.
Show resolved Hide resolved
public static final IProperty<KVector> ROUNDED_RECTANGLE_AUTOPADDING =
new Property<KVector>("klighd.roundedRectangle.autopadding", null);
}