From: Marko Luukkainen Date: Wed, 14 Aug 2019 10:42:15 +0000 (+0300) Subject: More precise position selection for add component action X-Git-Tag: v1.43.0~225^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=c17574943c09c562588136dbbc8585d6615b3a49;p=simantics%2F3d.git More precise position selection for add component action When we are adding variable length component or the component where we are inserting is variable length, we can shorten the variable length component based on user input. gitlab #26 Change-Id: I0ec9d3ff86497b109aa8a8320fb6476db897afd5 --- diff --git a/org.simantics.plant3d/icons/insert_end.png b/org.simantics.plant3d/icons/insert_end.png new file mode 100644 index 00000000..d4108b4f Binary files /dev/null and b/org.simantics.plant3d/icons/insert_end.png differ diff --git a/org.simantics.plant3d/icons/insert_end.svg b/org.simantics.plant3d/icons/insert_end.svg new file mode 100644 index 00000000..f3b0aa2a --- /dev/null +++ b/org.simantics.plant3d/icons/insert_end.svg @@ -0,0 +1,72 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + diff --git a/org.simantics.plant3d/icons/insert_middle.png b/org.simantics.plant3d/icons/insert_middle.png new file mode 100644 index 00000000..63ea77a4 Binary files /dev/null and b/org.simantics.plant3d/icons/insert_middle.png differ diff --git a/org.simantics.plant3d/icons/insert_middle.svg b/org.simantics.plant3d/icons/insert_middle.svg new file mode 100644 index 00000000..ff974bb5 --- /dev/null +++ b/org.simantics.plant3d/icons/insert_middle.svg @@ -0,0 +1,133 @@ + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + diff --git a/org.simantics.plant3d/icons/insert_start.png b/org.simantics.plant3d/icons/insert_start.png new file mode 100644 index 00000000..cb576131 Binary files /dev/null and b/org.simantics.plant3d/icons/insert_start.png differ diff --git a/org.simantics.plant3d/icons/insert_start.svg b/org.simantics.plant3d/icons/insert_start.svg new file mode 100644 index 00000000..cfb57ea2 --- /dev/null +++ b/org.simantics.plant3d/icons/insert_start.svg @@ -0,0 +1,253 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/actions/AddComponentAction.java b/org.simantics.plant3d/src/org/simantics/plant3d/actions/AddComponentAction.java index 60f95215..4f35291e 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/actions/AddComponentAction.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/actions/AddComponentAction.java @@ -45,6 +45,9 @@ public class AddComponentAction extends vtkSwtAction { private Set allowed = new HashSet(); private Item toAdd = null; + private PositionType insertPosition; + private boolean insertAdjustable; + private boolean lengthAdjustable; public AddComponentAction(InteractiveVtkComposite panel, P3DRootNode root) { super(panel); @@ -84,12 +87,16 @@ public class AddComponentAction extends vtkSwtAction { @Override public void run() { - ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed); + + ComponentSelectionDialog dialog = new ComponentSelectionDialog(Display.getCurrent().getActiveShell(), allowed, component); if (dialog.open() == ComponentSelectionDialog.CANCEL) return; toAdd = dialog.getSelected(); if (toAdd == null) return; + this.insertPosition = dialog.getInsertPosition(); + this.insertAdjustable = dialog.isInsertAdjustable(); + this.lengthAdjustable = dialog.isLenghtAdjustable(); this.length = dialog.getLength(); this.angle = dialog.getAngle(); this.diameter = dialog.getDiameter(); @@ -188,16 +195,31 @@ public class AddComponentAction extends vtkSwtAction { dir = new Vector3d(); toPcp.getInlineControlPointEnds(start, end, dir); dir.normalize(); - switch (position) { - case NEXT: - pos = new Vector3d(end); - break; - case PREVIOUS: - pos = new Vector3d(start); - break; - case SPLIT: - break; - } + if (!insertAdjustable || insertPosition == PositionType.NEXT) { + switch (position) { + case NEXT: + pos = new Vector3d(end); + break; + case PREVIOUS: + pos = new Vector3d(start); + break; + case SPLIT: + break; + } + } else if (insertPosition == PositionType.SPLIT) { + pos = new Vector3d(toPcp.getWorldPosition()); + } else { + switch (position) { + case NEXT: + pos = new Vector3d(start); + break; + case PREVIOUS: + pos = new Vector3d(end); + break; + case SPLIT: + break; + } + } } else if (toPcp.isDirected()) { dir = new Vector3d(toPcp.getDirection(Direction.NEXT)); @@ -205,9 +227,11 @@ public class AddComponentAction extends vtkSwtAction { } else if (toPcp.isTurn() && toPcp.isFixed()) { dir = new Vector3d(toPcp.getDirection(position == PositionType.NEXT ? Direction.NEXT : Direction.PREVIOUS)); pos = new Vector3d(toPcp.getWorldPosition()); - Vector3d v = new Vector3d(dir); - v.scale(toPcp.getInlineLength()); - pos.add(v); + if (!lengthAdjustable || insertPosition == PositionType.NEXT) { + Vector3d v = new Vector3d(dir); + v.scale(toPcp.getInlineLength()); + pos.add(v); + } } @@ -229,7 +253,16 @@ public class AddComponentAction extends vtkSwtAction { newComponent.updateParameters(); Vector3d v = new Vector3d(dir); - v.scale(newComponent.getControlPoint().getInlineLength()); + if (insertAdjustable) { + if (insertPosition == PositionType.NEXT) + v.scale(newComponent.getControlPoint().getInlineLength()); + else if (insertPosition == PositionType.SPLIT) + v.set(0, 0, 0); + else if (insertPosition == PositionType.PREVIOUS) + v.scale(-newComponent.getControlPoint().getInlineLength()); + } else { + v.scale(newComponent.getControlPoint().getInlineLength()); + } switch (position) { case NEXT: pos.add(v); diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java b/org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java index e5e220af..adfd98c9 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java @@ -7,15 +7,23 @@ import java.util.Set; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.resource.LocalResourceManager; +import org.eclipse.jface.resource.ResourceManager; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.ListViewer; import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.events.KeyAdapter; import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.ExpandBar; @@ -24,6 +32,12 @@ import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.simantics.db.exception.DatabaseException; +import org.simantics.plant3d.Activator; +import org.simantics.plant3d.scenegraph.EndComponent; +import org.simantics.plant3d.scenegraph.InlineComponent; +import org.simantics.plant3d.scenegraph.Nozzle; +import org.simantics.plant3d.scenegraph.PipelineComponent; +import org.simantics.plant3d.scenegraph.TurnComponent; import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType; import org.simantics.plant3d.utils.Item; import org.simantics.plant3d.utils.Item.Type; @@ -32,9 +46,17 @@ import org.simantics.utils.ui.ExceptionUtils; public class ComponentSelectionDialog extends Dialog implements ISelectionChangedListener{ + private ResourceManager resourceManager; + private Item selected; private Set allowed; private Set filterAllowed; + //private boolean positionAdjustment; + private PipelineComponent component; + private boolean insertAdjustable; + private boolean lenghtAdjustable; + private PositionType insertPosition = PositionType.NEXT; + private Double angle; private Double length; @@ -47,17 +69,30 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange private Text diameterText; private Text turnRadiusText; + private Button startButton; + private Button middleButton; + private Button endButton; + private boolean inlineSplit = false; + ListViewer inlineViewer; + ListViewer turnViewer; + ListViewer endViewer; + - public ComponentSelectionDialog(Shell parentShell, Set allowed) { + public ComponentSelectionDialog(Shell parentShell, Set allowed, PipelineComponent component){ super(parentShell); this.allowed = allowed; + this.component = component; filterAllowed = new HashSet(); + insertAdjustable = component instanceof InlineComponent ? ((InlineComponent)component).isVariableLength() : false; + lenghtAdjustable = false; } @Override protected Control createDialogArea(Composite parent) { + resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent); + Composite composite = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(2,false); layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); @@ -89,25 +124,28 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange ExceptionUtils.logError(e); return composite; } + ends = P3DUtil.filterUserComponents(ends); + turns = P3DUtil.filterUserComponents(turns); + inlines = P3DUtil.filterUserComponents(inlines); ExpandBar expandBar = new ExpandBar(composite, SWT.NONE); ExpandItem inlineItem = new ExpandItem(expandBar, SWT.NONE); inlineItem.setText("Inline"); - ListViewer inlineViewer = new ListViewer(expandBar); + inlineViewer = new ListViewer(expandBar); inlineViewer.setLabelProvider(new ComponentLabelProvider()); inlineViewer.setContentProvider(new ComponentContentProvider()); ExpandItem turnItem = new ExpandItem(expandBar, SWT.NONE); turnItem.setText("Turn"); - ListViewer turnViewer = new ListViewer(expandBar); + turnViewer = new ListViewer(expandBar); turnViewer.setLabelProvider(new ComponentLabelProvider()); turnViewer.setContentProvider(new ComponentContentProvider()); ExpandItem endItem = new ExpandItem(expandBar, SWT.NONE); endItem.setText("End"); - ListViewer endViewer = new ListViewer(expandBar); + endViewer = new ListViewer(expandBar); endViewer.setLabelProvider(new ComponentLabelProvider()); endViewer.setContentProvider(new ComponentContentProvider()); @@ -131,8 +169,44 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).span(2, 1).applyTo(expandBar); GridDataFactory.fillDefaults().minSize(500, 500).hint(500, 500).applyTo(composite); - Label label = new Label(composite, SWT.NONE); + label.setText("Position"); + Composite buttonComposite = new Composite(composite, SWT.NONE); + startButton = new Button(buttonComposite, SWT.TOGGLE); + middleButton = new Button(buttonComposite, SWT.TOGGLE); + endButton = new Button(buttonComposite, SWT.TOGGLE); + startButton.setImage(resourceManager.createImage(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/insert_start.png"))); + middleButton.setImage(resourceManager.createImage(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/insert_middle.png"))); + endButton.setImage(resourceManager.createImage(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/insert_end.png"))); + GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.TOP).applyTo(buttonComposite); + GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonComposite); + + startButton.setEnabled(false); + middleButton.setEnabled(false); + endButton.setEnabled(false); + + startButton.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + updateInsertPosition(PositionType.PREVIOUS); + } + }); + + middleButton.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + updateInsertPosition(PositionType.SPLIT); + } + }); + endButton.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + updateInsertPosition(PositionType.NEXT); + } + }); + endButton.setSelection(true); + + label = new Label(composite, SWT.NONE); label.setText("Length"); lengthText = new Text(composite, SWT.SINGLE|SWT.BORDER); label = new Label(composite, SWT.NONE); @@ -214,11 +288,33 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange } + private void updateInsertPosition(PositionType type) { + if (insertPosition == type) + return; + endButton.setSelection(type == PositionType.NEXT); + middleButton.setSelection(type == PositionType.SPLIT); + startButton.setSelection(type == PositionType.PREVIOUS); + insertPosition = type; + } + @Override public void selectionChanged(SelectionChangedEvent event) { IStructuredSelection sel = (IStructuredSelection)event.getSelection(); - selected = (Item)sel.getFirstElement(); - validate(); + Item i = (Item)sel.getFirstElement(); + if (i != null) { + selected = i; + if (event.getSource() == inlineViewer) { + turnViewer.setSelection(new StructuredSelection()); + endViewer.setSelection(new StructuredSelection()); + } else if (event.getSource() == turnViewer) { + inlineViewer.setSelection(new StructuredSelection()); + endViewer.setSelection(new StructuredSelection()); + } else if (event.getSource() == endViewer) { + inlineViewer.setSelection(new StructuredSelection()); + turnViewer.setSelection(new StructuredSelection()); + } + validate(); + } } private void validate() { @@ -230,7 +326,66 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange } else if (selected.isCode()) {// TODO : instead of disabling the button, we should filter the content. ok = false; } else { - if (selected.isVariable()) { + lenghtAdjustable = ((selected.getType() == Type.INLINE) && selected.isVariable()); + if (insertAdjustable) { + switch (selected.getType()) { + case END: + startButton.setEnabled(false); + middleButton.setEnabled(false); + endButton.setEnabled(true); + updateInsertPosition(PositionType.NEXT); + break; + case INLINE: + if (!selected.isVariable()) { + startButton.setEnabled(true); + middleButton.setEnabled(true); + endButton.setEnabled(true); + } else { + startButton.setEnabled(false); + middleButton.setEnabled(false); + endButton.setEnabled(true); + updateInsertPosition(PositionType.NEXT); + } + break; + case NOZZLE: + startButton.setEnabled(false); + middleButton.setEnabled(false); + endButton.setEnabled(true); + updateInsertPosition(PositionType.NEXT); + break; + case TURN: + startButton.setEnabled(false); + middleButton.setEnabled(true); + endButton.setEnabled(true); + if (insertPosition == PositionType.PREVIOUS) + updateInsertPosition(PositionType.NEXT); + break; + case EQUIPMENT: + throw new RuntimeException("Expected component, got equipment " + selected); + } + } else if (lenghtAdjustable) { + if (component instanceof InlineComponent) { + startButton.setEnabled(true); + middleButton.setEnabled(true); + endButton.setEnabled(true); + } else if (component instanceof TurnComponent) { + startButton.setEnabled(false); + middleButton.setEnabled(true); + endButton.setEnabled(true); + if (insertPosition == PositionType.PREVIOUS) + updateInsertPosition(PositionType.NEXT); + } else if (component instanceof EndComponent || component instanceof Nozzle) { + startButton.setEnabled(false); + middleButton.setEnabled(false); + endButton.setEnabled(true); + updateInsertPosition(PositionType.NEXT); + } + } else { + startButton.setEnabled(false); + middleButton.setEnabled(false); + endButton.setEnabled(true); + } + if (selected.isVariable()) { if (selected.getType() == Type.INLINE) { filterAllowed.add(PositionType.NEXT); filterAllowed.add(PositionType.PREVIOUS); @@ -332,5 +487,16 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange return filterAllowed; } + public PositionType getInsertPosition() { + return insertPosition; + } + + public boolean isInsertAdjustable() { + return insertAdjustable; + } + + public boolean isLenghtAdjustable() { + return lenghtAdjustable; + } } diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java b/org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java index b34f4f07..e6e3385f 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java @@ -1,6 +1,8 @@ package org.simantics.plant3d.utils; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import org.simantics.Simantics; @@ -92,6 +94,15 @@ public class P3DUtil { }); } + public static List filterUserComponents(List items) { + List result = new ArrayList(items.size()); + for (Item i : items) { + if (!i.isCode()) + result.add(i); + } + return result; + } + private static List getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{ Plant3D p3d = Plant3D.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); @@ -117,10 +128,16 @@ public class P3DUtil { } } } + Collections.sort(result, new Comparator() { + @Override + public int compare(Item o1, Item o2) { + return o1.getName().compareTo(o2.getName()); + } + }); return result; } - private static Item createItem(ReadGraph graph, Resource r) throws DatabaseException { + public static Item createItem(ReadGraph graph, Resource r) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); Plant3D p3d = Plant3D.getInstance(graph); String name = graph.getRelatedValue(r, l0.HasName);