From: jsimomaa Date: Wed, 25 Sep 2019 11:13:07 +0000 (+0300) Subject: Add support for gradients in dynamic visualisations X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=0a1b7351ff6fcf3724ee2b8e4121779ee6d42929;p=simantics%2Fdistrict.git Add support for gradients in dynamic visualisations gitlab #59 Change-Id: Id0c2037168819ab78a0acf944df87fc01ca6611f (cherry picked from commit a2c485af95024784aa9de3378759ee94b8caea2e) --- diff --git a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph index 592053e5..33408b2f 100644 --- a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph +++ b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph @@ -212,13 +212,17 @@ DN.Diagram.splitToMultipleEnabled -- DN.Diagram.Visualisations.ShowColorBars @defProperty "Show Color Bars" L0.Boolean >-- DN.Diagram.Visualisations.ShowColorBarTicks @defProperty "Show Color Bar Ticks" L0.Boolean + >-- DN.Diagram.Visualisations.UseColorBarGradients + @defProperty "Use Color Bar Gradients" L0.Boolean >-- DN.Diagram.Visualisations.ColorBarLocation @defProperty "Color Bar Location" L0.String >-- DN.Diagram.Visualisations.ColorBarSize @@ -227,6 +231,8 @@ DN.Diagram.Visualisations -- DN.Diagram.Visualisations.ShowSizeBarTicks @defProperty "Show Size Bar Ticks" L0.Boolean + >-- DN.Diagram.Visualisations.UseSizeBarGradients + @defProperty "Use Size Bar Gradients" L0.Boolean >-- DN.Diagram.Visualisations.SizeBarLocation @defProperty "Size Bar Location" L0.String >-- DN.Diagram.Visualisations.SizeBarSize @@ -267,6 +273,8 @@ DN.Diagram.Visualisations.sizeContributionUsed -- DN.DistrictNodeGroup.hasComponentTypeName ==> "String" perform(ReadGraph graph) throws DatabaseException { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - Resource activeVisualisation = graph.getPossibleObject(parameter, DN.Diagram_hasActiveVisualisation); + Resource model = graph.syncRequest(new IndexRoot(parameter)); + Resource vf = DistrictNetworkUtil.getVisualisationFolder(graph, model); + Resource activeVisualisation = graph.getPossibleObject(vf, DN.Diagram_hasActiveVisualisation); if (activeVisualisation != null) { return DistrictNetworkUtil.colorContributions(graph, activeVisualisation); } @@ -396,7 +401,9 @@ public class DistrictDiagramViewer extends DiagramViewer { @Override public SizeBarOptions perform(ReadGraph graph) throws DatabaseException { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - Resource activeVisualisation = graph.getPossibleObject(parameter, DN.Diagram_hasActiveVisualisation); + Resource model = graph.syncRequest(new IndexRoot(parameter)); + Resource vf = DistrictNetworkUtil.getVisualisationFolder(graph, model); + Resource activeVisualisation = graph.getPossibleObject(vf, DN.Diagram_hasActiveVisualisation); if (activeVisualisation != null) { return DistrictNetworkUtil.sizeBarOptions(graph, activeVisualisation); } @@ -441,7 +448,9 @@ public class DistrictDiagramViewer extends DiagramViewer { @Override public Map perform(ReadGraph graph) throws DatabaseException { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - Resource activeVisualisation = graph.getPossibleObject(parameter, DN.Diagram_hasActiveVisualisation); + Resource model = graph.syncRequest(new IndexRoot(parameter)); + Resource vf = DistrictNetworkUtil.getVisualisationFolder(graph, model); + Resource activeVisualisation = graph.getPossibleObject(vf, DN.Diagram_hasActiveVisualisation); if (activeVisualisation != null) { return DistrictNetworkUtil.sizeContributions(graph, activeVisualisation); } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DynamicVisualisationContributionsNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DynamicVisualisationContributionsNode.java index b3149324..d8d2e1d1 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DynamicVisualisationContributionsNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DynamicVisualisationContributionsNode.java @@ -125,9 +125,41 @@ public class DynamicVisualisationContributionsNode extends G2DNode { RGBIntensity intensity = intensities.get(j); - g2d.setColor(new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue(), 1f)); - Rectangle2D colorVertical = new Rectangle2D.Double(colorVerticalLeft, colorVerticalTop, colorVerticalWidth, colorVerticalHeigth); - g2d.fill(colorVertical); + if (colorBarsOptions.isUseGradients()) { + + RGBIntensity upperLimitIntensity; + if (j + 1 != intensities.size()) + upperLimitIntensity = intensities.get(j + 1); + else + upperLimitIntensity = intensity; + + double minRed = intensity.getRed(); + double minGreen = intensity.getGreen(); + double minBlue = intensity.getBlue(); + + double maxRed = upperLimitIntensity.getRed(); + double maxGreen = upperLimitIntensity.getGreen(); + double maxBlue = upperLimitIntensity.getBlue(); + + double redDelta = (maxRed - minRed) / colorVerticalWidth; + double greenDelta = (maxGreen - minGreen) / colorVerticalWidth; + double blueDelta = (maxBlue - minBlue) / colorVerticalWidth; + + for (int k = 0; k < colorVerticalWidth; k++) { + + + g2d.setColor(new Color((float)(minRed + (k * redDelta)), (float)(minGreen + (k * greenDelta)), (float)(minBlue + (k * blueDelta)), 1f)); + Rectangle2D colorVertical = new Rectangle2D.Double(colorVerticalLeft + k, colorVerticalTop, 1, colorVerticalHeigth); + g2d.fill(colorVertical); + } + + } else { + g2d.setColor(new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue(), 1f)); + Rectangle2D colorVertical = new Rectangle2D.Double(colorVerticalLeft, colorVerticalTop, colorVerticalWidth, colorVerticalHeigth); + g2d.fill(colorVertical); + } + + double value = min + j * interval; String str = Double.toString(value); diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/visualisations/DynamicVisualisationsUI.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/visualisations/DynamicVisualisationsUI.java index 5cb80a15..14a8f00f 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/visualisations/DynamicVisualisationsUI.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/visualisations/DynamicVisualisationsUI.java @@ -39,7 +39,6 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.RemoverUtil; import org.simantics.db.procedure.Listener; import org.simantics.district.network.DistrictNetworkUtil; -import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.district.network.profile.ActiveDynamicVisualisationsRequest; import org.simantics.district.network.profile.DynamicVisualisationsRequest; import org.simantics.district.network.visualisations.DynamicVisualisationsContributions; @@ -65,7 +64,7 @@ public class DynamicVisualisationsUI extends Composite { private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationsUI.class); - private Resource diagramResource; + private Resource parentResource; private VisualisationListener listener; private DynamicVisualisation visualisation; @@ -131,7 +130,8 @@ public class DynamicVisualisationsUI extends Composite { @Override public void perform(WriteGraph graph) throws DatabaseException { - DistrictNetworkUtil.setActiveVisualisation(graph, diagramResource, template.getResource()); + Resource vf = DistrictNetworkUtil.getVisualisationFolder(graph, parentResource); + DistrictNetworkUtil.setActiveVisualisation(graph, vf, template.getResource()); } }); break; @@ -269,12 +269,11 @@ public class DynamicVisualisationsUI extends Composite { @Override public void perform(WriteGraph graph) throws DatabaseException { - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); Resource exist; if (existing.isPresent()) { exist = existing.get(); } else { - exist = DistrictNetworkUtil.createVisualisation(graph, diagramResource, templateName); + exist = DistrictNetworkUtil.createVisualisation(graph, parentResource, templateName); } DistrictNetworkUtil.setColorContributions(graph, exist, colorCollect); @@ -298,16 +297,17 @@ public class DynamicVisualisationsUI extends Composite { colorSuppliers = new ArrayList<>(); { try { - Collection result = Simantics.getSession().syncRequest(new UniqueRead>() { + Pair, Map> result = Simantics.getSession().syncRequest(new UniqueRead, Map>>() { @Override - public Collection perform(ReadGraph graph) throws DatabaseException { - return DynamicVisualisationsContributions.dynamicColoringObjects(graph); + public Pair, Map> perform(ReadGraph graph) throws DatabaseException { + Map dynamicColorMaps = DynamicVisualisationsContributions.dynamicColorMaps(graph); + return Pair.make(DynamicVisualisationsContributions.dynamicColoringObjects(graph), dynamicColorMaps); } }); - for (DynamicColoringObject object : result) { - colorSuppliers.add(createColoringObjectRow(group, object)); + for (DynamicColoringObject object : result.first) { + colorSuppliers.add(createColoringObjectRow(group, object, result.second)); } } catch (DatabaseException e) { @@ -367,11 +367,11 @@ public class DynamicVisualisationsUI extends Composite { private final Combo variableCombo; private final Text minText; private final Text maxText; - private final Label unit; + private final Text unit; private final Combo colorMapCombo; private final Button defaultButton; - public ColoringObjectRow(Label label, Button usedButton, Combo variableCombo, Text minText, Text maxText, Label unit, + public ColoringObjectRow(Label label, Button usedButton, Combo variableCombo, Text minText, Text maxText, Text unit, Combo colorMapCombo, Button defaultButton) { super(); this.label = label; @@ -396,17 +396,14 @@ public class DynamicVisualisationsUI extends Composite { maxText.setText(Double.toString(colorContribution.getDefaultMax())); unit.setText(colorContribution.getUnit()); - // color map only supports single for now - colorMapCombo.setItems(colorContribution.getDefaultColorMap().getLabel()); - colorMapCombo.select(0); -// String[] colorItems = colorMapCombo.getItems(); -// for (int i = 0; i < colorItems.length; i++) { -// -// if (colorContribution.getDefaultColorMap().getLabel().equals(colorItems[i])) { -// colorMapCombo.select(i); -// break; -// } -// } + String[] colorItems = colorMapCombo.getItems(); + for (int i = 0; i < colorItems.length; i++) { + + if (colorContribution.getDefaultColorMap().getLabel().equals(colorItems[i])) { + colorMapCombo.select(i); + break; + } + } usedButton.setSelection(colorContribution.isUsed()); defaultButton.setSelection(colorContribution.isUseDefault()); @@ -452,17 +449,13 @@ public class DynamicVisualisationsUI extends Composite { maxText.setText(Double.toString(sizeContribution.getDefaultMax())); unit.setText(sizeContribution.getUnit()); - // color map only supports single for now - sizeMapCombo.setItems(sizeContribution.getDefaultSizeMap().getLabel()); - sizeMapCombo.select(0); -// String[] colorItems = colorMapCombo.getItems(); -// for (int i = 0; i < colorItems.length; i++) { -// -// if (colorContribution.getDefaultColorMap().getLabel().equals(colorItems[i])) { -// colorMapCombo.select(i); -// break; -// } -// } + String[] sizeItems = sizeMapCombo.getItems(); + for (int i = 0; i < sizeItems.length; i++) { + if (sizeContribution.getDefaultSizeMap().getLabel().equals(sizeItems[i])) { + sizeMapCombo.select(i); + break; + } + } usedButton.setSelection(sizeContribution.isUsed()); defaultButton.setSelection(sizeContribution.isUseDefault()); @@ -472,7 +465,7 @@ public class DynamicVisualisationsUI extends Composite { } } - private Supplier> createColoringObjectRow(Composite parent, DynamicColoringObject object) { + private Supplier> createColoringObjectRow(Composite parent, DynamicColoringObject object, Map colorMaps) { Label label = new Label(parent, SWT.NONE); label.setText(object.getColoringObject().getName()); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(label); @@ -493,12 +486,12 @@ public class DynamicVisualisationsUI extends Composite { Text maxText = new Text(parent, SWT.BORDER); GridDataFactory.fillDefaults().grab(true, false).hint(150, SWT.DEFAULT).align(SWT.CENTER, SWT.CENTER).applyTo(maxText); - Label unit = new Label(parent, SWT.NONE); - unit.setText(""); - GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(unit); + Text unit = new Text(parent, SWT.READ_ONLY); + GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(unit); Combo colorMapCombo = new Combo(parent, SWT.READ_ONLY); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(colorMapCombo); + colorMapCombo.setItems(colorMaps.keySet().toArray(new String[colorMaps.keySet().size()])); Button defaultButton = new Button(parent, SWT.CHECK); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(defaultButton); @@ -517,8 +510,14 @@ public class DynamicVisualisationsUI extends Composite { maxText.setEnabled(!defaultButton.getSelection()); unit.setText(cont.getUnit()); - colorMapCombo.setItems(cont.getDefaultColorMap().getLabel()); - colorMapCombo.select(0); + String[] items = colorMapCombo.getItems(); + for (int i = 0; i < items.length; i++) { + String item = items[i]; + if (item.equals(cont.getDefaultColorMap().getLabel())) { + colorMapCombo.select(i); + break; + } + } colorMapCombo.setEnabled(!defaultButton.getSelection()); } } @@ -542,8 +541,14 @@ public class DynamicVisualisationsUI extends Composite { } unit.setText(cont.getUnit()); - colorMapCombo.setItems(cont.getDefaultColorMap().getLabel()); - colorMapCombo.select(0); + String[] items = colorMapCombo.getItems(); + for (int i = 0; i < items.length; i++) { + String item = items[i]; + if (item.equals(cont.getDefaultColorMap().getLabel())) { + colorMapCombo.select(i); + break; + } + } defaultButton.setSelection(true); } @@ -623,7 +628,7 @@ public class DynamicVisualisationsUI extends Composite { GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label); } - private Supplier> createSizingObjectRow(Composite parent, DynamicSizingObject object) { + private Supplier> createSizingObjectRow(Composite parent, DynamicSizingObject object, Map sizeMaps) { Label label = new Label(parent, SWT.NONE); label.setText(object.getSizingObject().getName()); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(label); @@ -645,14 +650,41 @@ public class DynamicVisualisationsUI extends Composite { GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(maxText); Label unit = new Label(parent, SWT.NONE); - unit.setText(""); - GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(unit); + GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(unit); Combo sizeMapCombo = new Combo(parent, SWT.READ_ONLY); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(sizeMapCombo); + sizeMapCombo.setItems(sizeMaps.keySet().toArray(new String[sizeMaps.keySet().size()])); Button defaultButton = new Button(parent, SWT.CHECK); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(defaultButton); + defaultButton.addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent e) { + int index = variableCombo.getSelectionIndex(); + if (index >= 0) { + String key = variableCombo.getItem(index); + DynamicSizeContribution cont = sizeContributions.get(key); + + minText.setText(Double.toString(cont.getDefaultMin())); + minText.setEnabled(!defaultButton.getSelection()); + maxText.setText(Double.toString(cont.getDefaultMax())); + maxText.setEnabled(!defaultButton.getSelection()); + unit.setText(cont.getUnit()); + + String[] items = sizeMapCombo.getItems(); + for (int i = 0; i < items.length; i++) { + String item = items[i]; + if (item.equals(cont.getDefaultSizeMap().getLabel())) { + sizeMapCombo.select(i); + break; + } + } + sizeMapCombo.setEnabled(!defaultButton.getSelection()); + } + } + }); variableCombo.addSelectionListener(new SelectionAdapter() { @@ -670,8 +702,14 @@ public class DynamicVisualisationsUI extends Composite { } unit.setText(cont.getUnit()); - sizeMapCombo.setItems(cont.getDefaultSizeMap().getLabel()); - sizeMapCombo.select(0); + String[] items = sizeMapCombo.getItems(); + for (int i = 0; i < items.length; i++) { + String item = items[i]; + if (item.equals(cont.getDefaultSizeMap().getLabel())) { + sizeMapCombo.select(i); + break; + } + } defaultButton.setSelection(true); } @@ -770,16 +808,17 @@ public class DynamicVisualisationsUI extends Composite { sizeSuppliers = new ArrayList<>(); try { - Collection resultSizing = Simantics.getSession().syncRequest(new UniqueRead>() { + Pair, Map> resultSizing = Simantics.getSession().syncRequest(new UniqueRead, Map>>() { @Override - public Collection perform(ReadGraph graph) throws DatabaseException { - return DynamicVisualisationsContributions.dynamicSizingObjects(graph); + public Pair, Map> perform(ReadGraph graph) throws DatabaseException { + Map dynamicSizeMaps = DynamicVisualisationsContributions.dynamicSizeMaps(graph); + return Pair.make(DynamicVisualisationsContributions.dynamicSizingObjects(graph), dynamicSizeMaps); } }); - for (DynamicSizingObject object : resultSizing) { - sizeSuppliers.add(createSizingObjectRow(parent, object)); + for (DynamicSizingObject object : resultSizing.first) { + sizeSuppliers.add(createSizingObjectRow(parent, object, resultSizing.second)); } } catch (DatabaseException e) { LOGGER.error("Could not create object sizes", e); @@ -824,9 +863,9 @@ public class DynamicVisualisationsUI extends Composite { } } - public void setDiagramResource(Resource diagramResource) { - if (this.diagramResource != diagramResource) { - this.diagramResource = diagramResource; + public void setParentResource(Resource parentResource) { + if (this.parentResource != parentResource) { + this.parentResource = parentResource; updateListening(); } } @@ -835,12 +874,12 @@ public class DynamicVisualisationsUI extends Composite { if (visualisationsListener != null) visualisationsListener.dispose(); visualisationsListener = new VisualisationsListener(this); - Simantics.getSession().asyncRequest(new DynamicVisualisationsRequest(diagramResource), visualisationsListener); + Simantics.getSession().asyncRequest(new DynamicVisualisationsRequest(parentResource), visualisationsListener); if (listener != null) listener.dispose(); listener = new VisualisationListener(this); - Simantics.getSession().asyncRequest(new ActiveDynamicVisualisationsRequest(diagramResource), listener); + Simantics.getSession().asyncRequest(new ActiveDynamicVisualisationsRequest(parentResource), listener); } private static class VisualisationsListener implements Listener> { @@ -938,6 +977,7 @@ public class DynamicVisualisationsUI extends Composite { ColorBarOptions colorOptions = visualisation.getColorBarOptions(); showColorButton.setSelection(colorOptions.isShowColorBars()); colorTicksButton.setSelection(colorOptions.isShowColorBarsTicks()); + colorGradientButton.setSelection(colorOptions.isUseGradients()); for (int i = 0; i < colorLocationCombo.getItems().length; i++) { String item = colorLocationCombo.getItem(i); if (item.equals(colorOptions.getLocation().toString())) { @@ -968,6 +1008,7 @@ public class DynamicVisualisationsUI extends Composite { SizeBarOptions sizeOptions = visualisation.getSizeBarOptions(); showSizeButton.setSelection(sizeOptions.isShowSizeBars()); sizeTicksButton.setSelection(sizeOptions.isShowSizeBarsTicks()); + sizeGradientButton.setSelection(sizeOptions.isUseGradients()); for (int i = 0; i < sizeLocationCombo.getItems().length; i++) { String item = sizeLocationCombo.getItem(i); if (item.equals(sizeOptions.getLocation().toString())) { diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/visualisations/DynamicVisualisationsView.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/visualisations/DynamicVisualisationsView.java index d98a3787..6bdf0037 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/visualisations/DynamicVisualisationsView.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/visualisations/DynamicVisualisationsView.java @@ -2,85 +2,44 @@ package org.simantics.district.network.ui.visualisations; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; -import javax.inject.Inject; -import org.eclipse.e4.ui.model.application.ui.basic.MPart; -import org.eclipse.e4.ui.workbench.modeling.EPartService; -import org.eclipse.e4.ui.workbench.modeling.ESelectionService; -import org.eclipse.e4.ui.workbench.modeling.IPartListener; import org.eclipse.swt.widgets.Composite; -import org.eclipse.ui.IEditorInput; -import org.eclipse.ui.IEditorPart; -import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor; -import org.simantics.district.network.ui.DistrictDiagramEditor; -import org.simantics.ui.workbench.IResourceEditorInput; -import org.simantics.utils.ui.workbench.WorkbenchUtils; +import org.simantics.Simantics; +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.procedure.adapter.AsyncListenerAdapter; +import org.simantics.db.layer0.request.PossibleActiveModel; public class DynamicVisualisationsView { - @Inject - ESelectionService selectionService; - - @Inject - EPartService partService; - private DynamicVisualisationsUI ui; @PostConstruct public void postConstruct(Composite parent) { ui = new DynamicVisualisationsUI(parent, 0); - IEditorPart editor = WorkbenchUtils.getActiveEditor(); - if (editor instanceof DistrictDiagramEditor) - setDiagramResource(editor.getEditorInput()); - - partService.addPartListener(partListener); + Simantics.getSession().asyncRequest(new PossibleActiveModel(Simantics.getProjectResource()), new AsyncListenerAdapter() { + + @Override + public void execute(AsyncReadGraph graph, Resource result) { + if (ui != null && !ui.isDisposed()) { + ui.getDisplay().asyncExec(() -> { + ui.setParentResource(result); + }); + } + } + + @Override + public boolean isDisposed() { + return ui != null ? ui.isDisposed() : true; + } + }); } @PreDestroy public void dispose() { - partService.removePartListener(partListener); ui.dispose(); ui = null; } - private void setDiagramResourceFromCompatibilityEditor(MPart part) { - if (part.getObject() instanceof CompatibilityEditor) { - CompatibilityEditor editor = (CompatibilityEditor) part.getObject(); - IEditorPart editorPart = editor.getEditor(); - setDiagramResource(editorPart.getEditorInput()); - } - } - - private void setDiagramResource(IEditorInput input) { - if (input instanceof IResourceEditorInput) { - ui.setDiagramResource(((IResourceEditorInput) input).getResource()); - } - } - - private IPartListener partListener = new IPartListener() { - - @Override - public void partVisible(MPart part) { - setDiagramResourceFromCompatibilityEditor(part); - } - - @Override - public void partHidden(MPart part) { - } - - @Override - public void partDeactivated(MPart part) { - } - - @Override - public void partBroughtToTop(MPart part) { - setDiagramResourceFromCompatibilityEditor(part); - } - - @Override - public void partActivated(MPart part) { - setDiagramResourceFromCompatibilityEditor(part); - } - }; } diff --git a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java index 596ac044..54d3a595 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java +++ b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java @@ -8,6 +8,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -26,6 +27,7 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; import org.simantics.db.exception.ServiceException; import org.simantics.db.indexing.IndexUtils; +import org.simantics.db.layer0.QueryIndexUtils; import org.simantics.db.layer0.request.PossibleVariable; import org.simantics.db.layer0.variable.Variable; import org.simantics.diagram.stubs.DiagramResource; @@ -409,10 +411,12 @@ public class DistrictNetworkUtil { String colorBarSize = graph.getPossibleRelatedValue(visualisation, DN.Diagram_Visualisations_ColorBarSize, Bindings.STRING); Boolean show = graph.getPossibleRelatedValue(visualisation, DN.Diagram_Visualisations_ShowColorBars, Bindings.BOOLEAN); Boolean showTicks = graph.getPossibleRelatedValue(visualisation, DN.Diagram_Visualisations_ShowColorBarTicks, Bindings.BOOLEAN); + Boolean useGradients = graph.getPossibleRelatedValue(visualisation, DN.Diagram_Visualisations_UseColorBarGradients, Bindings.BOOLEAN); if (colorBarLocation != null) { return new ColorBarOptions() .showColorBars(show != null ? show : false) .showColorBarsTicks(showTicks != null ? showTicks : false) + .useGradients(useGradients != null ? useGradients : false) .withLocation(ColorBarsLocation.valueOf(colorBarLocation)) .withSize(ColorBarsSize.valueOf(colorBarSize)); } @@ -426,18 +430,40 @@ public class DistrictNetworkUtil { graph.claimLiteral(visualisation, DN.Diagram_Visualisations_ColorBarSize, options.getSize().toString(), Bindings.STRING); graph.claimLiteral(visualisation, DN.Diagram_Visualisations_ShowColorBars, options.isShowColorBars(), Bindings.BOOLEAN); graph.claimLiteral(visualisation, DN.Diagram_Visualisations_ShowColorBarTicks, options.isShowColorBarsTicks(), Bindings.BOOLEAN); + graph.claimLiteral(visualisation, DN.Diagram_Visualisations_UseColorBarGradients, options.isUseGradients(), Bindings.BOOLEAN); } - public static Resource createVisualisation(WriteGraph graph, Resource diagram, String visualisationName) throws DatabaseException { + public static Resource createVisualisation(WriteGraph graph, Resource parent, String visualisationName) throws DatabaseException { + Resource vf = getOrCreateVisualisationsFolder(graph, parent); Layer0 L0 = Layer0.getInstance(graph); DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); Resource visualisation = graph.newResource(); graph.claim(visualisation, L0.InstanceOf, DN.Diagram_Visualisations); graph.claimLiteral(visualisation, L0.HasName, visualisationName); - graph.claim(diagram, DN.Diagram_hasVisualisation, visualisation); + graph.claim(vf, DN.Diagram_hasVisualisation, visualisation); return visualisation; } + public static Resource getOrCreateVisualisationsFolder(WriteGraph graph, Resource model) throws DatabaseException { + Resource rf = getVisualisationFolder(graph, model); + if (rf == null) { + Layer0 L0 = Layer0.getInstance(graph); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + rf = graph.newResource(); + graph.claim(rf, L0.InstanceOf, null, DN.VisualisationsFolder); + graph.claimLiteral(rf, L0.HasName, L0.NameOf, L0.String, UUID.randomUUID().toString(), Bindings.STRING); + graph.claim(model, L0.ConsistsOf, L0.PartOf, rf); + } + return rf; + } + + public static Resource getVisualisationFolder(ReadGraph graph, Resource model) throws DatabaseException { + List visualisationsFolders = QueryIndexUtils.searchByType(graph, model, DistrictNetworkResource.getInstance(graph).VisualisationsFolder); + if (visualisationsFolders.size() > 0) + return visualisationsFolders.get(0); + return null; + } + public static SizeBarOptions sizeBarOptions(ReadGraph graph, Resource visualisation) throws DatabaseException { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); if (visualisation != null) { @@ -445,10 +471,12 @@ public class DistrictNetworkUtil { String sizeBarSize = graph.getPossibleRelatedValue(visualisation, DN.Diagram_Visualisations_SizeBarSize, Bindings.STRING); Boolean show = graph.getPossibleRelatedValue(visualisation, DN.Diagram_Visualisations_ShowSizeBars, Bindings.BOOLEAN); Boolean showTicks = graph.getPossibleRelatedValue(visualisation, DN.Diagram_Visualisations_ShowSizeBarTicks, Bindings.BOOLEAN); + Boolean useGradients = graph.getPossibleRelatedValue(visualisation, DN.Diagram_Visualisations_UseSizeBarGradients, Bindings.BOOLEAN); if (sizeBarLocation != null) { return new SizeBarOptions() .showSizeBars(show != null ? show : false) .showSizeBarsTicks(showTicks != null ? showTicks : false) + .useGradients(useGradients != null ? useGradients : false) .withLocation(SizeBarsLocation.valueOf(sizeBarLocation)) .withSize(SizeBarsSize.valueOf(sizeBarSize)); } @@ -462,6 +490,7 @@ public class DistrictNetworkUtil { graph.claimLiteral(visualisation, DN.Diagram_Visualisations_SizeBarSize, options.getSize().toString(), Bindings.STRING); graph.claimLiteral(visualisation, DN.Diagram_Visualisations_ShowSizeBars, options.isShowSizeBars(), Bindings.BOOLEAN); graph.claimLiteral(visualisation, DN.Diagram_Visualisations_ShowSizeBarTicks, options.isShowSizeBarsTicks(), Bindings.BOOLEAN); + graph.claimLiteral(visualisation, DN.Diagram_Visualisations_UseSizeBarGradients, options.isUseGradients(), Bindings.BOOLEAN); } public static Map colorContributions(ReadGraph graph, Resource visualisation) throws DatabaseException { diff --git a/org.simantics.district.network/src/org/simantics/district/network/profile/ActiveDynamicVisualisationsRequest.java b/org.simantics.district.network/src/org/simantics/district/network/profile/ActiveDynamicVisualisationsRequest.java index 52cfd61c..58499db0 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/profile/ActiveDynamicVisualisationsRequest.java +++ b/org.simantics.district.network/src/org/simantics/district/network/profile/ActiveDynamicVisualisationsRequest.java @@ -20,22 +20,25 @@ import org.simantics.layer0.Layer0; */ public class ActiveDynamicVisualisationsRequest extends ResourceRead { - public ActiveDynamicVisualisationsRequest(Resource diagram) { - super(diagram); + public ActiveDynamicVisualisationsRequest(Resource parent) { + super(parent); } @Override public DynamicVisualisation perform(ReadGraph graph) throws DatabaseException { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - Resource visualisationResource = graph.getPossibleObject(resource, DN.Diagram_hasActiveVisualisation); - if (visualisationResource != null) { - String name = graph.getRelatedValue(visualisationResource, Layer0.getInstance(graph).HasName); - Map colorContributions = DistrictNetworkUtil.colorContributions(graph, visualisationResource); - ColorBarOptions colorBarOptions = DistrictNetworkUtil.colorBarOptions(graph, visualisationResource); - Map sizeContributions = DistrictNetworkUtil.sizeContributions(graph, visualisationResource); - SizeBarOptions sizeBarOptions = DistrictNetworkUtil.sizeBarOptions(graph, visualisationResource); - DynamicVisualisation visualisation = new DynamicVisualisation(name, visualisationResource, colorContributions, colorBarOptions, sizeContributions, sizeBarOptions); - return visualisation; + Resource visualisationFolder = DistrictNetworkUtil.getVisualisationFolder(graph, resource); + if (visualisationFolder != null) { + Resource visualisationResource = graph.getPossibleObject(visualisationFolder, DN.Diagram_hasActiveVisualisation); + if (visualisationResource != null) { + String name = graph.getRelatedValue(visualisationResource, Layer0.getInstance(graph).HasName); + Map colorContributions = DistrictNetworkUtil.colorContributions(graph, visualisationResource); + ColorBarOptions colorBarOptions = DistrictNetworkUtil.colorBarOptions(graph, visualisationResource); + Map sizeContributions = DistrictNetworkUtil.sizeContributions(graph, visualisationResource); + SizeBarOptions sizeBarOptions = DistrictNetworkUtil.sizeBarOptions(graph, visualisationResource); + DynamicVisualisation visualisation = new DynamicVisualisation(name, visualisationResource, colorContributions, colorBarOptions, sizeContributions, sizeBarOptions); + return visualisation; + } } return null; } diff --git a/org.simantics.district.network/src/org/simantics/district/network/profile/DNElementColorStyle.java b/org.simantics.district.network/src/org/simantics/district/network/profile/DNElementColorStyle.java index 7ebff91e..9bcae2e5 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/profile/DNElementColorStyle.java +++ b/org.simantics.district.network/src/org/simantics/district/network/profile/DNElementColorStyle.java @@ -70,7 +70,7 @@ public class DNElementColorStyle extends ThrottledStyleBase { // here we do the adjusting according to spec in #15038 double adjustedValue = possibleValue.doubleValue() * dcc.getVariableGain() + dcc.getVariableBias(); DynamicColorMap defaultColorMap = dcc.getDefaultColorMap(); - Color color = defaultColorMap.getColor(adjustedValue, dcc.getDefaultMin(), dcc.getDefaultMax()); + Color color = defaultColorMap.getColor(adjustedValue, dv.getColorBarOptions().isUseGradients(), dcc.getDefaultMin(), dcc.getDefaultMax()); return color; } else { LOGGER.warn("No value for {}", attribute.getURI(graph)); diff --git a/org.simantics.district.network/src/org/simantics/district/network/profile/DNElementSizeStyle.java b/org.simantics.district.network/src/org/simantics/district/network/profile/DNElementSizeStyle.java index 5963954f..0a454eef 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/profile/DNElementSizeStyle.java +++ b/org.simantics.district.network/src/org/simantics/district/network/profile/DNElementSizeStyle.java @@ -71,7 +71,7 @@ public class DNElementSizeStyle extends ThrottledStyleBase { // here we do the adjusting according to spec in #15038 double adjustedValue = possibleValue.doubleValue() * dsc.getVariableGain() + dsc.getVariableBias(); DynamicSizeMap defaultSizeMap = dsc.getDefaultSizeMap(); - double size = defaultSizeMap.getSize(adjustedValue, dsc.getDefaultMin(), dsc.getDefaultMax()); + double size = defaultSizeMap.getSize(adjustedValue, dv.getSizeBarOptions().isUseGradients(), dsc.getDefaultMin(), dsc.getDefaultMax()); return size; } else { LOGGER.warn("No value for {}", attribute.getURI(graph)); diff --git a/org.simantics.district.network/src/org/simantics/district/network/profile/DynamicVisualisationsRequest.java b/org.simantics.district.network/src/org/simantics/district/network/profile/DynamicVisualisationsRequest.java index 3c187a10..c2b473c5 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/profile/DynamicVisualisationsRequest.java +++ b/org.simantics.district.network/src/org/simantics/district/network/profile/DynamicVisualisationsRequest.java @@ -9,6 +9,7 @@ import org.simantics.db.Resource; import org.simantics.db.common.NamedResource; import org.simantics.db.common.request.ResourceRead; import org.simantics.db.exception.DatabaseException; +import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.layer0.Layer0; @@ -17,19 +18,22 @@ import org.simantics.layer0.Layer0; */ public class DynamicVisualisationsRequest extends ResourceRead> { - public DynamicVisualisationsRequest(Resource diagram) { - super(diagram); + public DynamicVisualisationsRequest(Resource parent) { + super(parent); } @Override public Collection perform(ReadGraph graph) throws DatabaseException { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - Collection visualisationResources = graph.getObjects(resource, DN.Diagram_hasVisualisation); List results = new ArrayList<>(); - if (visualisationResources != null && !visualisationResources.isEmpty()) { - for (Resource visualisationResource : visualisationResources) { - String name = graph.getRelatedValue(visualisationResource, Layer0.getInstance(graph).HasName); - results.add(new NamedResource(name, visualisationResource)); + Resource visualisationFolder = DistrictNetworkUtil.getVisualisationFolder(graph, resource); + if (visualisationFolder != null) { + Collection visualisationResources = graph.getObjects(visualisationFolder, DN.Diagram_hasVisualisation); + if (visualisationResources != null && !visualisationResources.isEmpty()) { + for (Resource visualisationResource : visualisationResources) { + String name = graph.getRelatedValue(visualisationResource, Layer0.getInstance(graph).HasName); + results.add(new NamedResource(name, visualisationResource)); + } } } return results; diff --git a/org.simantics.district.network/src/org/simantics/district/network/profile/RuntimeDynamicVisualisationsRequest.java b/org.simantics.district.network/src/org/simantics/district/network/profile/RuntimeDynamicVisualisationsRequest.java index 05ed85bd..97d23147 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/profile/RuntimeDynamicVisualisationsRequest.java +++ b/org.simantics.district.network/src/org/simantics/district/network/profile/RuntimeDynamicVisualisationsRequest.java @@ -3,6 +3,7 @@ package org.simantics.district.network.profile; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener; +import org.simantics.db.common.request.IndexRoot; import org.simantics.db.common.request.ResourceRead; import org.simantics.db.exception.DatabaseException; import org.simantics.diagram.stubs.DiagramResource; @@ -22,7 +23,8 @@ public class RuntimeDynamicVisualisationsRequest extends ResourceRead extends StyleBase result) { - if (!Optional.empty().equals(result)) { + if (!Optional.empty().equals(result) && result != null) { applyThrottledStyleForNode(evaluationContext, node, result.get()); } else { LOGGER.debug("Do not apply as results are unchanged for {} {} {}", evaluationContext, node, result); diff --git a/org.simantics.district.network/src/org/simantics/district/network/visualisations/model/DynamicColorMap.java b/org.simantics.district.network/src/org/simantics/district/network/visualisations/model/DynamicColorMap.java index 09cb5794..4c458097 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/visualisations/model/DynamicColorMap.java +++ b/org.simantics.district.network/src/org/simantics/district/network/visualisations/model/DynamicColorMap.java @@ -1,15 +1,10 @@ package org.simantics.district.network.visualisations.model; import java.awt.Color; -import java.util.Arrays; import java.util.List; public class DynamicColorMap { - static List blues = Arrays.asList(new RGBIntensity(0, 0, 0.1), new RGBIntensity(0, 0, 0.5), new RGBIntensity(0, 0, 0.9)); - - public static final DynamicColorMap DEFAULT = new DynamicColorMap("default", blues); - private String label; private List intensities; @@ -51,20 +46,49 @@ public class DynamicColorMap { } } - public Color getColor(double value, double defaultMin, double defaultMax) { + public Color getColor(double value, boolean useGradient, double defaultMin, double defaultMax) { + List intensities = getIntensities(); double gap = defaultMax - defaultMin; - double singleGap = gap / getIntensities().size(); + double singleGap = gap / intensities.size(); + double threshold = defaultMin; int i = 0; - while (i < getIntensities().size() - 1) { - if (value <= defaultMin + (i * singleGap)) { + while (i < intensities.size() - 1) { + threshold = threshold + singleGap; + if (value <= threshold) { break; } i++; } - RGBIntensity intensity = getIntensities().get(i); - return new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue()); + RGBIntensity intensity = intensities.get(i); + if (useGradient) { + + RGBIntensity upperLimitIntensity; + if (i + 1 != intensities.size()) + upperLimitIntensity = intensities.get(i + 1); + else + upperLimitIntensity = intensity; + + double minRed = intensity.getRed(); + double minGreen = intensity.getGreen(); + double minBlue = intensity.getBlue(); + + double maxRed = upperLimitIntensity.getRed(); + double maxGreen = upperLimitIntensity.getGreen(); + double maxBlue = upperLimitIntensity.getBlue(); + + double delta = Math.max(value - (defaultMin + singleGap * i), 0); + + double d = delta / singleGap; + + double redDelta = (maxRed - minRed) * d; + double greenDelta = (maxGreen - minGreen) * d; + double blueDelta = (maxBlue - minBlue) * d; + return new Color((float)(minRed + redDelta), (float)(minGreen + greenDelta), (float)(minBlue + blueDelta)); + } else { + return new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue()); + } } } diff --git a/org.simantics.district.network/src/org/simantics/district/network/visualisations/model/DynamicSizeMap.java b/org.simantics.district.network/src/org/simantics/district/network/visualisations/model/DynamicSizeMap.java index 7edb12b9..e2adb888 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/visualisations/model/DynamicSizeMap.java +++ b/org.simantics.district.network/src/org/simantics/district/network/visualisations/model/DynamicSizeMap.java @@ -20,19 +20,39 @@ public class DynamicSizeMap { return sizes; } - public double getSize(double value, double defaultMin, double defaultMax) { + public double getSize(double value, boolean useGradient, double defaultMin, double defaultMax) { + List sizes = getSizes(); double gap = defaultMax - defaultMin; - double singleGap = gap / getSizes().size(); + double singleGap = gap / sizes.size(); + double threshold = defaultMin; int i = 0; - while (i < getSizes().size() - 1) { - if (value <= defaultMin + (i * singleGap)) { + while (i < sizes.size() - 1) { + threshold = threshold + singleGap; + if (value <= threshold) { break; } i++; } - return getSizes().get(i); + Double size = sizes.get(i); + if (useGradient) { + + Double upperSize; + if (i + 1 != sizes.size()) { + upperSize = sizes.get(i + 1); + } else { + upperSize = size; + } + + double delta = Math.max(value - (defaultMin + singleGap * i), 0); + double d = delta / singleGap; + double sizeDelta = (upperSize - size) * d; + + return size + sizeDelta; + } + + return size; } }