]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Supply SVG text editor with element measurement context 60/1060/7
authorAntti Villberg <antti.villberg@semantum.fi>
Sun, 1 Oct 2017 17:49:17 +0000 (20:49 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 2 Oct 2017 11:21:30 +0000 (14:21 +0300)
refs #7520

Change-Id: Ie72bc282f98b2545fed14ff6bd55fb10198e0151

bundles/org.simantics.diagram.ontology/graph/Diagram.pgraph
bundles/org.simantics.diagram/scl/Simantics/Diagram/Internal/DiagramOntologyEntryPoint.scl
bundles/org.simantics.diagram/scl/Simantics/Diagram/SVGNode.scl [new file with mode: 0644]
bundles/org.simantics.diagram/src/org/simantics/diagram/elements/EditorStateManager.java
bundles/org.simantics.diagram/src/org/simantics/diagram/elements/SVGMeasurementContext.java [new file with mode: 0644]
bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SVGNode.java

index 103140b6f49943aa923cf50e1161a4660c68dfd3..b26bfc698ee214ac9040b7228d2fe0a13c59b334 100644 (file)
@@ -127,7 +127,7 @@ DIA.DefinedElement
   >-- DIA.symbolCode <R L0.HasProperty : L0.FunctionalRelation
   >-- DIA.symbolDropHandler ==> "[WorkbenchSelectionElement] -> <WriteGraph,Proc> ()" <R L0.HasProperty : L0.FunctionalRelation
   // Parameters are id of edited element and new text.
-  >-- DIA.DefinedElement.textEditor ==> "String -> String -> <Proc> ()" <R L0.HasProperty : DIA.DefinedElement.Parameter
+  >-- DIA.DefinedElement.textEditor ==> "SVGMeasurementContext -> String -> String -> <Proc> ()" <R L0.HasProperty : DIA.DefinedElement.Parameter
   // This function gives initial text for the text editor. Parameter is the id of the edited element.
   >-- DIA.DefinedElement.textEditorFullText ==> "String -> <Proc> String" <R L0.HasProperty : DIA.DefinedElement.Parameter 
 
index e395a1fde8a1dd3191431fbee58656ee37b332ec..fb1709f7c9aa22a2d105d9fd7c6870034016c2e1 100644 (file)
@@ -1,4 +1,5 @@
 import "Simantics/Variables"
+include "Simantics/Diagram/SVGNode"
 
 import "http://www.simantics.org/Diagram-2.2" as DIA
 import "http://www.simantics.org/G2D-1.1" as G2D
diff --git a/bundles/org.simantics.diagram/scl/Simantics/Diagram/SVGNode.scl b/bundles/org.simantics.diagram/scl/Simantics/Diagram/SVGNode.scl
new file mode 100644 (file)
index 0000000..9c5d43c
--- /dev/null
@@ -0,0 +1,20 @@
+
+type BoundingBox = (Double,Double,Double,Double)
+
+importJava "org.simantics.diagram.elements.SVGMeasurementContext" where
+  data SVGMeasurementContext
+  getBoundingBox :: SVGMeasurementContext -> String -> <Proc> Maybe BoundingBox
+
+getBoundingBoxX :: BoundingBox -> Double
+getBoundingBoxX (x,_,_,_) = x
+
+getBoundingBoxY :: BoundingBox -> Double
+getBoundingBoxY (_,y,_,_) = y
+
+getBoundingBoxW :: BoundingBox -> Double
+getBoundingBoxW (_,_,w,_) = w
+
+getBoundingBoxH :: BoundingBox -> Double
+getBoundingBoxH (_,_,_,h) = h
+
+
index 5c59177cccf4f1c3a8088ff08690adf79c5c7ac7..1223d8eaffe74fe606d0461d7b122676622736c2 100644 (file)
@@ -1,3 +1,14 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
 package org.simantics.diagram.elements;
 
 import java.awt.geom.Point2D;
@@ -13,7 +24,8 @@ import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent;
 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
 import org.simantics.scl.runtime.function.Function1;
-import org.simantics.scl.runtime.function.Function2;
+import org.simantics.scl.runtime.function.Function3;
+import org.simantics.scl.runtime.tuple.Tuple4;
 
 import com.kitfox.svg.SVGDiagram;
 import com.kitfox.svg.SVGElement;
@@ -36,6 +48,23 @@ class EditorStateManager {
        private LinkedList<EditorState> editorState = null;
        private int editorStateIndex = 0;
 
+       static class SVGMeasurementContextImpl implements SVGMeasurementContext {
+               private SVGNode node;
+               public SVGMeasurementContextImpl(SVGNode node) {
+                       this.node = node;
+               }
+               @Override
+               public Tuple4 getBoundingBox(String id) {
+                       try {
+                               Rectangle2D rect = node.getElementBounds(id);
+                               if(rect == null) return null;
+                               return new Tuple4(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
+                       } catch (SVGException e) {
+                               return null;
+                       }
+               }
+       }
+
        EditorStateManager(SVGNode node) {
                this.node = node;
        }
@@ -119,9 +148,9 @@ class EditorStateManager {
                SingleElementNode sne = node.getSingleElementNode();
                if (sne != null) {
                        EditorState es = currentState();
-                       Function2<String,String,Object> editor = sne.getParameter("textEditor");
+                       Function3<SVGMeasurementContext,String,String,Object> editor = sne.getParameter("textEditor");
                        if(editor != null) {
-                               editor.apply(es.base.textElementId, es.currentText);
+                               editor.apply(new SVGMeasurementContextImpl(node), es.base.textElementId, es.currentText);
                        }
                }
        }
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/SVGMeasurementContext.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/SVGMeasurementContext.java
new file mode 100644 (file)
index 0000000..19175a8
--- /dev/null
@@ -0,0 +1,22 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.diagram.elements;
+
+import org.simantics.scl.runtime.tuple.Tuple4;
+
+/**
+ * @author Antti Villberg
+ * @since 1.31.0
+ */
+public interface SVGMeasurementContext {
+    Tuple4 getBoundingBox(String id);
+}
index db491a3fda07e3145d91a38bdbbf8e693595debf..d21ba1f16a73c91ee9517244492bfa6e2dccec2e 100644 (file)
@@ -45,6 +45,7 @@ import org.simantics.scl.runtime.function.Function1;
 import org.simantics.scl.runtime.function.Function2;
 import org.simantics.utils.threads.AWTThread;
 
+import com.kitfox.svg.RenderableElement;
 import com.kitfox.svg.SVGCache;
 import com.kitfox.svg.SVGDiagram;
 import com.kitfox.svg.SVGElement;
@@ -484,4 +485,13 @@ public class SVGNode extends G2DNode implements InitValueSupport, LoaderNode {
                return ret;
        }
 
+    public Rectangle2D getElementBounds(String id) throws SVGException {
+        SVGElement e = diagramCache.getElement(id);
+        if (e instanceof RenderableElement) {
+            return ((RenderableElement)e).getBoundingBox();
+        } else {
+           return null;
+        }
+    }
+
 }