Skip to content

Commit 07952f9

Browse files
author
Christoph Daniel Schulze
committed
Core: Add option to keep FixedLayoutProvider from enlarging graphs. #475
Signed-off-by: Christoph Daniel Schulze <cds@informatik.uni-kiel.de>
1 parent b091f72 commit 07952f9

File tree

3 files changed

+117
-13
lines changed

3 files changed

+117
-13
lines changed

plugins/org.eclipse.elk.core/src/org/eclipse/elk/core/Core.melk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ algorithm fixed (FixedLayoutProvider) {
8989
supports bendPoints
9090
supports nodeSize.constraints
9191
supports nodeSize.minimum
92+
supports nodeSize.fixedGraphSize
9293
}
9394
algorithm box (BoxLayoutProvider) {
9495
label "Box Layout"
@@ -454,6 +455,13 @@ group nodeSize {
454455
default = new KVector(0, 0)
455456
targets nodes
456457
}
458+
option fixedGraphSize: boolean {
459+
label "Fixed Graph Size"
460+
description "By default, the fixed layout provider will enlarge a graph until it is large enough to contain
461+
its children. If this option is set, it won't do so."
462+
default = false
463+
targets parents
464+
}
457465
}
458466
//------- PROGRAMMATIC OPTIONS
459467
output option junctionPoints: KVectorChain {

plugins/org.eclipse.elk.core/src/org/eclipse/elk/core/util/FixedLayoutProvider.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2010, 2015 Kiel University and others.
2+
* Copyright (c) 2010, 2019 Kiel University and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -42,19 +42,17 @@
4242
* <p>
4343
* MIGRATE The fixed layout provider does not support hyperedges yet.
4444
* </p>
45-
*
46-
* @author msp
4745
*/
4846
public class FixedLayoutProvider extends AbstractLayoutProvider {
4947

50-
/**
51-
* {@inheritDoc}
52-
*/
5348
@Override
5449
public void layout(final ElkNode layoutNode, final IElkProgressMonitor progressMonitor) {
5550
progressMonitor.begin("Fixed Layout", 1);
51+
5652
EdgeRouting edgeRouting = layoutNode.getProperty(CoreOptions.EDGE_ROUTING);
57-
double maxx = 0, maxy = 0;
53+
54+
double maxx = 0;
55+
double maxy = 0;
5856

5957
for (ElkNode node : layoutNode.getChildren()) {
6058
// set the fixed position of the node, or leave it as it is
@@ -63,7 +61,6 @@ public void layout(final ElkNode layoutNode, final IElkProgressMonitor progressM
6361
node.setLocation(pos.x, pos.y);
6462

6563
// set the fixed size of the node
66-
// TODO Think about whether this makes sense with the new size constraint options.
6764
if (node.getProperty(FixedLayouterOptions.NODE_SIZE_CONSTRAINTS).contains(
6865
SizeConstraint.MINIMUM_SIZE)) {
6966

@@ -136,11 +133,13 @@ public void layout(final ElkNode layoutNode, final IElkProgressMonitor progressM
136133
}
137134
}
138135

139-
// set size of the parent node
140-
ElkPadding padding = layoutNode.getProperty(FixedLayouterOptions.PADDING);
141-
double newWidth = maxx + padding.getLeft() + padding.getRight();
142-
double newHeight = maxy + padding.getTop() + padding.getBottom();
143-
ElkUtil.resizeNode(layoutNode, newWidth, newHeight, true, true);
136+
// set size of the parent node unless its size should be fixed as well
137+
if (!layoutNode.getProperty(FixedLayouterOptions.NODE_SIZE_FIXED_GRAPH_SIZE)) {
138+
ElkPadding padding = layoutNode.getProperty(FixedLayouterOptions.PADDING);
139+
double newWidth = maxx + padding.getLeft() + padding.getRight();
140+
double newHeight = maxy + padding.getTop() + padding.getBottom();
141+
ElkUtil.resizeNode(layoutNode, newWidth, newHeight, true, true);
142+
}
144143
progressMonitor.done();
145144
}
146145

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 Kiel University and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*******************************************************************************/
10+
package org.eclipse.elk.alg.common.issues;
11+
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertTrue;
14+
15+
import java.util.List;
16+
17+
import org.eclipse.elk.alg.test.framework.LayoutTestRunner;
18+
import org.eclipse.elk.alg.test.framework.annotations.Algorithm;
19+
import org.eclipse.elk.alg.test.framework.annotations.Configurator;
20+
import org.eclipse.elk.alg.test.framework.annotations.DefaultConfiguration;
21+
import org.eclipse.elk.alg.test.framework.annotations.GraphResourceProvider;
22+
import org.eclipse.elk.alg.test.framework.io.AbstractResourcePath;
23+
import org.eclipse.elk.alg.test.framework.io.ModelResourcePath;
24+
import org.eclipse.elk.core.options.FixedLayouterOptions;
25+
import org.eclipse.elk.graph.ElkNode;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
29+
import com.google.common.collect.Lists;
30+
31+
/**
32+
* Test for issue 475.
33+
*/
34+
@RunWith(LayoutTestRunner.class)
35+
@Algorithm(FixedLayouterOptions.ALGORITHM_ID)
36+
@DefaultConfiguration
37+
public class Issue475Test {
38+
39+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
40+
// Sources
41+
42+
@GraphResourceProvider
43+
public List<AbstractResourcePath> testGraphs() {
44+
return Lists.newArrayList(new ModelResourcePath("tickets/core/475_fixedLayoutWithLongLabels.elkt"));
45+
}
46+
47+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
48+
// Configuration
49+
50+
/** Default compound node size. */
51+
private static final double SIZE = 20;
52+
53+
@Configurator
54+
public void configureFixedGraphSize(final ElkNode graph) {
55+
for (ElkNode child : graph.getChildren()) {
56+
// Make sure we know the size the node shall have
57+
child.setProperty(FixedLayouterOptions.NODE_SIZE_FIXED_GRAPH_SIZE, true);
58+
child.setHeight(SIZE);
59+
child.setWidth(SIZE);
60+
}
61+
}
62+
63+
@Configurator
64+
public void configureNoFixedGraphSize(final ElkNode graph) {
65+
graph.getChildren().stream()
66+
.forEach(node -> node.setProperty(FixedLayouterOptions.NODE_SIZE_FIXED_GRAPH_SIZE, false));
67+
}
68+
69+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
70+
// Tests
71+
72+
@Test
73+
public void testCompoundNodeSize(final ElkNode graph) {
74+
graph.getChildren().stream().forEach(node -> doTestCompoundNodeSize(node));
75+
}
76+
77+
public void doTestCompoundNodeSize(final ElkNode node) {
78+
if (node.getProperty(FixedLayouterOptions.NODE_SIZE_FIXED_GRAPH_SIZE)) {
79+
// The node's size should not have changed
80+
assertEquals(SIZE, node.getWidth(), 0);
81+
assertEquals(SIZE, node.getHeight(), 0);
82+
83+
} else {
84+
double maxX = 0;
85+
double maxY = 0;
86+
87+
for (ElkNode child : node.getChildren()) {
88+
maxX = Math.max(maxX, child.getX() + child.getWidth());
89+
maxY = Math.max(maxY, child.getY() + child.getHeight());
90+
}
91+
92+
assertTrue(node.getWidth() >= maxX);
93+
assertTrue(node.getHeight() >= maxY);
94+
}
95+
}
96+
97+
}

0 commit comments

Comments
 (0)