SVGHMI: still quite naive path substitution whn prepearing widget for DnD, but now uses label generation. svghmi
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Tue, 13 Apr 2021 21:04:46 +0200
branchsvghmi
changeset 3229 c5be4fd425e7
parent 3228 80f96db773bd
child 3230 95f07764991f
SVGHMI: still quite naive path substitution whn prepearing widget for DnD, but now uses label generation.

Multiple widget DnD and mutiple variable still make no sense because all path are replaced with same path, but atleast min/max value are preserved...
svghmi/gen_dnd_widget_svg.xslt
svghmi/gen_dnd_widget_svg.ysl2
svghmi/gen_index_xhtml.xslt
svghmi/parse_labels.ysl2
svghmi/widgetlib/voltmeter.svg
--- a/svghmi/gen_dnd_widget_svg.xslt	Tue Apr 13 21:00:34 2021 +0200
+++ b/svghmi/gen_dnd_widget_svg.xslt	Tue Apr 13 21:04:46 2021 +0200
@@ -1,5 +1,5 @@
 <?xml version="1.0"?>
-<xsl:stylesheet xmlns:func="http://exslt.org/functions" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:svg="http://www.w3.org/2000/svg" xmlns:str="http://exslt.org/strings" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:exsl="http://exslt.org/common" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ns="beremiz" xmlns:cc="http://creativecommons.org/ns#" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/" extension-element-prefixes="ns func exsl regexp str dyn" version="1.0" exclude-result-prefixes="ns func exsl regexp str dyn">
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:str="http://exslt.org/strings" xmlns:func="http://exslt.org/functions" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:ns="beremiz" version="1.0" extension-element-prefixes="ns func exsl regexp str dyn" exclude-result-prefixes="ns func exsl regexp str dyn">
   <xsl:output method="xml"/>
   <xsl:param name="hmi_path"/>
   <xsl:variable name="svg" select="/svg:svg"/>
@@ -77,47 +77,69 @@
                   </xsl:message>
                 </xsl:when>
               </xsl:choose>
-              <xsl:choose>
-                <xsl:when test="regexp:test($path,'^\.[a-zA-Z0-9_]+$')">
-                  <xsl:attribute name="type">
-                    <xsl:text>PAGE_LOCAL</xsl:text>
-                  </xsl:attribute>
-                </xsl:when>
-                <xsl:when test="regexp:test($path,'^[a-zA-Z0-9_]+$')">
-                  <xsl:attribute name="type">
-                    <xsl:text>HMI_LOCAL</xsl:text>
-                  </xsl:attribute>
-                </xsl:when>
-                <xsl:otherwise>
-                  <xsl:variable name="item" select="$indexed_hmitree/*[@hmipath = $path]"/>
-                  <xsl:variable name="pathtype" select="local-name($item)"/>
-                  <xsl:if test="$pathminmaxcount = 3 and not($pathtype = 'HMI_INT' or $pathtype = 'HMI_REAL')">
-                    <xsl:message terminate="yes">
-                      <xsl:text>Widget id:</xsl:text>
-                      <xsl:value-of select="$id"/>
-                      <xsl:text> label:</xsl:text>
-                      <xsl:value-of select="$label"/>
-                      <xsl:text> path section </xsl:text>
-                      <xsl:value-of select="$pathminmax"/>
-                      <xsl:text> use min and max on non mumeric value</xsl:text>
-                    </xsl:message>
-                  </xsl:if>
-                  <xsl:if test="count($item) = 1">
-                    <xsl:attribute name="index">
-                      <xsl:value-of select="$item/@index"/>
+              <xsl:if test="$indexed_hmitree">
+                <xsl:choose>
+                  <xsl:when test="regexp:test($path,'^\.[a-zA-Z0-9_]+$')">
+                    <xsl:attribute name="type">
+                      <xsl:text>PAGE_LOCAL</xsl:text>
                     </xsl:attribute>
+                  </xsl:when>
+                  <xsl:when test="regexp:test($path,'^[a-zA-Z0-9_]+$')">
                     <xsl:attribute name="type">
-                      <xsl:value-of select="$pathtype"/>
+                      <xsl:text>HMI_LOCAL</xsl:text>
                     </xsl:attribute>
-                  </xsl:if>
-                </xsl:otherwise>
-              </xsl:choose>
+                  </xsl:when>
+                  <xsl:otherwise>
+                    <xsl:variable name="item" select="$indexed_hmitree/*[@hmipath = $path]"/>
+                    <xsl:variable name="pathtype" select="local-name($item)"/>
+                    <xsl:if test="$pathminmaxcount = 3 and not($pathtype = 'HMI_INT' or $pathtype = 'HMI_REAL')">
+                      <xsl:message terminate="yes">
+                        <xsl:text>Widget id:</xsl:text>
+                        <xsl:value-of select="$id"/>
+                        <xsl:text> label:</xsl:text>
+                        <xsl:value-of select="$label"/>
+                        <xsl:text> path section </xsl:text>
+                        <xsl:value-of select="$pathminmax"/>
+                        <xsl:text> use min and max on non mumeric value</xsl:text>
+                      </xsl:message>
+                    </xsl:if>
+                    <xsl:if test="count($item) = 1">
+                      <xsl:attribute name="index">
+                        <xsl:value-of select="$item/@index"/>
+                      </xsl:attribute>
+                      <xsl:attribute name="type">
+                        <xsl:value-of select="$pathtype"/>
+                      </xsl:attribute>
+                    </xsl:if>
+                  </xsl:otherwise>
+                </xsl:choose>
+              </xsl:if>
             </path>
           </xsl:if>
         </xsl:for-each>
       </widget>
     </xsl:if>
   </xsl:template>
+  <xsl:template mode="genlabel" match="arg">
+    <xsl:text>:</xsl:text>
+    <xsl:value-of select="@value"/>
+  </xsl:template>
+  <xsl:template mode="genlabel" match="path">
+    <xsl:text>@</xsl:text>
+    <xsl:value-of select="@value"/>
+    <xsl:if test="string-length(@min)&gt;0 or string-length(@max)&gt;0">
+      <xsl:text>:</xsl:text>
+      <xsl:value-of select="@min"/>
+      <xsl:text>:</xsl:text>
+      <xsl:value-of select="@max"/>
+    </xsl:if>
+  </xsl:template>
+  <xsl:template mode="genlabel" match="widget">
+    <xsl:text>HMI:</xsl:text>
+    <xsl:value-of select="@type"/>
+    <xsl:apply-templates mode="genlabel" select="arg"/>
+    <xsl:apply-templates mode="genlabel" select="path"/>
+  </xsl:template>
   <xsl:variable name="_parsed_widgets">
     <xsl:apply-templates mode="parselabel" select="$hmi_elements"/>
   </xsl:variable>
@@ -127,18 +149,34 @@
   <xsl:variable name="svg_widget_type" select="$svg_widget/@type"/>
   <xsl:variable name="svg_widget_path" select="$svg_widget/@path"/>
   <xsl:variable name="svg_widget_count" select="count($parsed_widgets/widget)"/>
+  <xsl:template mode="replace_path" match="@* | node()">
+    <xsl:copy>
+      <xsl:apply-templates mode="replace_path" select="@* | node()"/>
+    </xsl:copy>
+  </xsl:template>
+  <xsl:template mode="replace_path" match="path/@value">
+    <xsl:attribute name="value">
+      <xsl:value-of select="$hmi_path"/>
+    </xsl:attribute>
+  </xsl:template>
   <xsl:template xmlns="http://www.w3.org/2000/svg" mode="inline_svg" match="@*">
     <xsl:copy/>
   </xsl:template>
-  <xsl:template xmlns="http://www.w3.org/2000/svg" mode="inline_svg" match="@inkscape:label[starts-with(., 'HMI:')]">
-    <xsl:attribute name="inkscape:label">
-      <xsl:value-of select="substring-before(., '@')"/>
-      <xsl:text>@</xsl:text>
-      <xsl:value-of select="$hmi_path"/>
-    </xsl:attribute>
-  </xsl:template>
+  <xsl:template xmlns="http://www.w3.org/2000/svg" mode="inline_svg" match="@inkscape:label[starts-with(., 'HMI:')]"/>
   <xsl:template mode="inline_svg" match="node()">
     <xsl:copy>
+      <xsl:if test="@id = $svg_widget/@id">
+        <xsl:variable name="substituted_widget">
+          <xsl:apply-templates mode="replace_path" select="$svg_widget"/>
+        </xsl:variable>
+        <xsl:variable name="substituted_widget_ns" select="exsl:node-set($substituted_widget)"/>
+        <xsl:variable name="new_label">
+          <xsl:apply-templates mode="genlabel" select="$substituted_widget_ns"/>
+        </xsl:variable>
+        <xsl:attribute name="inkscape:label">
+          <xsl:value-of select="$new_label"/>
+        </xsl:attribute>
+      </xsl:if>
       <xsl:apply-templates mode="inline_svg" select="@* | node()"/>
     </xsl:copy>
   </xsl:template>
--- a/svghmi/gen_dnd_widget_svg.ysl2	Tue Apr 13 21:00:34 2021 +0200
+++ b/svghmi/gen_dnd_widget_svg.ysl2	Tue Apr 13 21:04:46 2021 +0200
@@ -30,22 +30,39 @@
     const "parsed_widgets","exsl:node-set($_parsed_widgets)";
 
     const "selected_node_type","local-name($subhmitree)";
-    const "svg_widget", "$parsed_widgets/widget[1]";
+    const "svg_widget", "$parsed_widgets/widget[1]"; // TODO take all widgets
     const "svg_widget_type", "$svg_widget/@type";
     const "svg_widget_path", "$svg_widget/@path";
     const "svg_widget_count", "count($parsed_widgets/widget)";
 
+    // Templates to change label paths(s)
+    template "@* | node()", mode="replace_path" {
+        xsl:copy apply "@* | node()", mode="replace_path";
+    }
+
+    template "path/@value", mode="replace_path" {
+        attrib "value" > «$hmi_path»
+    }
+
+    // all attribs are usually copied
     svgtmpl "@*", mode="inline_svg" xsl:copy;
 
-    svgtmpl "@inkscape:label[starts-with(., 'HMI:')]", mode="inline_svg" {
-        attrib "inkscape:label" > «substring-before(., '@')»@«$hmi_path»
+    // except labels, ignored
+    svgtmpl "@inkscape:label[starts-with(., 'HMI:')]", mode="inline_svg";
+
+    template "node()", mode="inline_svg" xsl:copy {
+
+      // in case this node widget's main element inject label
+      if "@id = $svg_widget/@id" {
+          const "substituted_widget" apply "$svg_widget", mode="replace_path";
+          const "substituted_widget_ns", "exsl:node-set($substituted_widget)";
+          const "new_label" apply "$substituted_widget_ns", mode="genlabel";
+          attrib "inkscape:label" > «$new_label»
+      }
+      // all nodes are copied as well
+      apply "@* | node()", mode="inline_svg";
     }
 
-    template "node()", mode="inline_svg" {
-      xsl:copy apply "@* | node()", mode="inline_svg";
-    }
-
-
     const "NODES_TYPES","str:split('HMI_ROOT HMI_NODE')";
     const "HMI_NODES_COMPAT","str:split('Page Jump Foreach')";
     template "/" {
--- a/svghmi/gen_index_xhtml.xslt	Tue Apr 13 21:00:34 2021 +0200
+++ b/svghmi/gen_index_xhtml.xslt	Tue Apr 13 21:04:46 2021 +0200
@@ -207,47 +207,69 @@
                   </xsl:message>
                 </xsl:when>
               </xsl:choose>
-              <xsl:choose>
-                <xsl:when test="regexp:test($path,'^\.[a-zA-Z0-9_]+$')">
-                  <xsl:attribute name="type">
-                    <xsl:text>PAGE_LOCAL</xsl:text>
-                  </xsl:attribute>
-                </xsl:when>
-                <xsl:when test="regexp:test($path,'^[a-zA-Z0-9_]+$')">
-                  <xsl:attribute name="type">
-                    <xsl:text>HMI_LOCAL</xsl:text>
-                  </xsl:attribute>
-                </xsl:when>
-                <xsl:otherwise>
-                  <xsl:variable name="item" select="$indexed_hmitree/*[@hmipath = $path]"/>
-                  <xsl:variable name="pathtype" select="local-name($item)"/>
-                  <xsl:if test="$pathminmaxcount = 3 and not($pathtype = 'HMI_INT' or $pathtype = 'HMI_REAL')">
-                    <xsl:message terminate="yes">
-                      <xsl:text>Widget id:</xsl:text>
-                      <xsl:value-of select="$id"/>
-                      <xsl:text> label:</xsl:text>
-                      <xsl:value-of select="$label"/>
-                      <xsl:text> path section </xsl:text>
-                      <xsl:value-of select="$pathminmax"/>
-                      <xsl:text> use min and max on non mumeric value</xsl:text>
-                    </xsl:message>
-                  </xsl:if>
-                  <xsl:if test="count($item) = 1">
-                    <xsl:attribute name="index">
-                      <xsl:value-of select="$item/@index"/>
+              <xsl:if test="$indexed_hmitree">
+                <xsl:choose>
+                  <xsl:when test="regexp:test($path,'^\.[a-zA-Z0-9_]+$')">
+                    <xsl:attribute name="type">
+                      <xsl:text>PAGE_LOCAL</xsl:text>
                     </xsl:attribute>
+                  </xsl:when>
+                  <xsl:when test="regexp:test($path,'^[a-zA-Z0-9_]+$')">
                     <xsl:attribute name="type">
-                      <xsl:value-of select="$pathtype"/>
+                      <xsl:text>HMI_LOCAL</xsl:text>
                     </xsl:attribute>
-                  </xsl:if>
-                </xsl:otherwise>
-              </xsl:choose>
+                  </xsl:when>
+                  <xsl:otherwise>
+                    <xsl:variable name="item" select="$indexed_hmitree/*[@hmipath = $path]"/>
+                    <xsl:variable name="pathtype" select="local-name($item)"/>
+                    <xsl:if test="$pathminmaxcount = 3 and not($pathtype = 'HMI_INT' or $pathtype = 'HMI_REAL')">
+                      <xsl:message terminate="yes">
+                        <xsl:text>Widget id:</xsl:text>
+                        <xsl:value-of select="$id"/>
+                        <xsl:text> label:</xsl:text>
+                        <xsl:value-of select="$label"/>
+                        <xsl:text> path section </xsl:text>
+                        <xsl:value-of select="$pathminmax"/>
+                        <xsl:text> use min and max on non mumeric value</xsl:text>
+                      </xsl:message>
+                    </xsl:if>
+                    <xsl:if test="count($item) = 1">
+                      <xsl:attribute name="index">
+                        <xsl:value-of select="$item/@index"/>
+                      </xsl:attribute>
+                      <xsl:attribute name="type">
+                        <xsl:value-of select="$pathtype"/>
+                      </xsl:attribute>
+                    </xsl:if>
+                  </xsl:otherwise>
+                </xsl:choose>
+              </xsl:if>
             </path>
           </xsl:if>
         </xsl:for-each>
       </widget>
     </xsl:if>
   </xsl:template>
+  <xsl:template mode="genlabel" match="arg">
+    <xsl:text>:</xsl:text>
+    <xsl:value-of select="@value"/>
+  </xsl:template>
+  <xsl:template mode="genlabel" match="path">
+    <xsl:text>@</xsl:text>
+    <xsl:value-of select="@value"/>
+    <xsl:if test="string-length(@min)&gt;0 or string-length(@max)&gt;0">
+      <xsl:text>:</xsl:text>
+      <xsl:value-of select="@min"/>
+      <xsl:text>:</xsl:text>
+      <xsl:value-of select="@max"/>
+    </xsl:if>
+  </xsl:template>
+  <xsl:template mode="genlabel" match="widget">
+    <xsl:text>HMI:</xsl:text>
+    <xsl:value-of select="@type"/>
+    <xsl:apply-templates mode="genlabel" select="arg"/>
+    <xsl:apply-templates mode="genlabel" select="path"/>
+  </xsl:template>
   <xsl:variable name="_parsed_widgets">
     <widget type="VarInitPersistent">
       <arg value="0"/>
--- a/svghmi/parse_labels.ysl2	Tue Apr 13 21:00:34 2021 +0200
+++ b/svghmi/parse_labels.ysl2	Tue Apr 13 21:04:46 2021 +0200
@@ -54,7 +54,7 @@
                         error > Widget id:«$id» label:«$label» has wrong syntax of path section «$pathminmax»
                     }
                 }
-                choose {
+                if "$indexed_hmitree" choose {
                     when "regexp:test($path,'^\.[a-zA-Z0-9_]+$')" {
                         attrib "type" > PAGE_LOCAL
                     }
@@ -78,3 +78,18 @@
     }
 }
 
+
+// Templates to generate label back from parsed tree
+template "arg", mode="genlabel" > :«@value»
+
+template "path", mode="genlabel" {
+    > @«@value»
+    if "string-length(@min)>0 or string-length(@max)>0"  > :«@min»:«@max»
+}
+
+template "widget", mode="genlabel" {
+    > HMI:«@type»
+    apply "arg", mode="genlabel";
+    apply "path", mode="genlabel";
+}
+
--- a/svghmi/widgetlib/voltmeter.svg	Tue Apr 13 21:00:34 2021 +0200
+++ b/svghmi/widgetlib/voltmeter.svg	Tue Apr 13 21:04:46 2021 +0200
@@ -93,16 +93,16 @@
      borderopacity="1.0"
      inkscape:pageopacity="0.0"
      inkscape:pageshadow="2"
-     inkscape:zoom="0.35"
-     inkscape:cx="-64.606209"
-     inkscape:cy="537.51037"
+     inkscape:zoom="3.312923"
+     inkscape:cx="554.2857"
+     inkscape:cy="554.2857"
      inkscape:document-units="mm"
      inkscape:current-layer="svg2354"
      showgrid="false"
-     inkscape:window-width="1452"
-     inkscape:window-height="940"
-     inkscape:window-x="1645"
-     inkscape:window-y="72"
+     inkscape:window-width="1605"
+     inkscape:window-height="1099"
+     inkscape:window-x="3543"
+     inkscape:window-y="375"
      inkscape:window-maximized="0" />
   <metadata
      id="metadata2351">
@@ -112,7 +112,7 @@
         <dc:format>image/svg+xml</dc:format>
         <dc:type
            rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
-        <dc:title></dc:title>
+        <dc:title />
       </cc:Work>
     </rdf:RDF>
   </metadata>
@@ -438,7 +438,7 @@
        id="path4304" />
   </g>
   <g
-     inkscape:label="HMI:Meter@/PUMP0/SLOTH"
+     inkscape:label="HMI:Meter@/PUMP0/SLOTH,0,666"
      transform="matrix(0.57180538,0,0,0.57180538,88.118425,163.79208)"
      id="g19348">
     <path