SVGHMI: Add arbitrary variable assignment when entering Pages
authorEdouard Tisserant
Wed, 23 Nov 2022 11:27:55 +0100
changeset 3685 570a738239f4
parent 3684 2239f8e3de48
child 3686 a3c509e66695
SVGHMI: Add arbitrary variable assignment when entering Pages

Usage similar to Assign widget.
Required instanciating PageWidgets for each page, so that assigned variables can be subsribed.
exemples/svghmi_jumps/svghmi_0@svghmi/svghmi.svg
svghmi/detachable_pages.ysl2
svghmi/svghmi.js
svghmi/widget_jump.ysl2
svghmi/widget_page.ysl2
svghmi/widgets_common.ysl2
--- a/exemples/svghmi_jumps/svghmi_0@svghmi/svghmi.svg	Fri Nov 18 10:44:56 2022 +0100
+++ b/exemples/svghmi_jumps/svghmi_0@svghmi/svghmi.svg	Wed Nov 23 11:27:55 2022 +0100
@@ -25,7 +25,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>
@@ -40,17 +40,17 @@
      guidetolerance="10"
      inkscape:pageopacity="0"
      inkscape:pageshadow="2"
-     inkscape:window-width="1600"
-     inkscape:window-height="836"
+     inkscape:window-width="1850"
+     inkscape:window-height="1036"
      id="namedview4"
      showgrid="false"
-     inkscape:zoom="0.23177389"
-     inkscape:cx="1999.5317"
-     inkscape:cy="-682.74047"
+     inkscape:zoom="0.46354778"
+     inkscape:cx="-544.27948"
+     inkscape:cy="655.56978"
      inkscape:window-x="0"
      inkscape:window-y="27"
      inkscape:window-maximized="1"
-     inkscape:current-layer="hmi0"
+     inkscape:current-layer="g2496"
      showguides="true"
      inkscape:guide-bbox="true"
      borderlayer="true"
@@ -67,7 +67,7 @@
      transform="translate(1320,1520)"
      width="100%"
      height="100%"
-     inkscape:label="HMI:Page:RelativePage@/FB_ZERO" />
+     inkscape:label="HMI:Page:RelativePage:p=6@p=page_number@/FB_ZERO" />
   <use
      sodipodi:insensitive="true"
      x="0"
@@ -77,7 +77,7 @@
      transform="translate(2640,759.99998)"
      width="100%"
      height="100%"
-     inkscape:label="HMI:Page:Relative" />
+     inkscape:label="HMI:Page:Relative:p=5@p=page_number" />
   <use
      x="0"
      y="0"
@@ -86,10 +86,10 @@
      transform="translate(3940,-2.1367187e-5)"
      width="100%"
      height="100%"
-     inkscape:label="HMI:Page:Conditional"
+     inkscape:label="HMI:Page:Conditional:p=4@p=page_number"
      sodipodi:insensitive="true" />
   <use
-     inkscape:label="HMI:Page:Unconditional"
+     inkscape:label="HMI:Page:Unconditional:p=3@p=page_number"
      height="100%"
      width="100%"
      transform="translate(2640,-2.1367187e-5)"
@@ -99,7 +99,7 @@
      x="0"
      sodipodi:insensitive="true" />
   <use
-     inkscape:label="HMI:Page:AbsolutePage"
+     inkscape:label="HMI:Page:AbsolutePage:p=2@p=page_number"
      height="100%"
      width="100%"
      transform="translate(1320,759.99998)"
@@ -116,7 +116,7 @@
      transform="translate(1320,-2.1367187e-5)"
      width="100%"
      height="100%"
-     inkscape:label="HMI:Page:Home"
+     inkscape:label="HMI:Page:Home:p=1@p=page_number"
      sodipodi:insensitive="true" />
   <text
      id="text837"
@@ -621,6 +621,18 @@
          id="tspan1849"
          x="-1166.3386"
          y="598.98303">HMI:Jump:RelativePage</tspan></text>
+    <text
+       inkscape:label="HMI:Display@page_number"
+       xml:space="preserve"
+       style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:50.03883362px;line-height:125%;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
+       x="-75.162941"
+       y="702.24023"
+       id="text296"><tspan
+         sodipodi:role="line"
+         id="tspan294"
+         x="-75.162941"
+         y="702.24023"
+         style="stroke-width:1px">0</tspan></text>
   </g>
   <g
      id="g2585"
@@ -1200,7 +1212,7 @@
     <g
        id="g1711"
        inkscape:label="MYNODE:+1"
-       transform="matrix(1.3729714,0,0,1.3729714,-299.47126,-800.64485)"
+       transform="matrix(1.3729714,0,0,1.3729714,-359.47134,-800.64485)"
        style="stroke-width:0.7283473">
       <rect
          rx="15.554536"
@@ -1226,7 +1238,7 @@
     <g
        inkscape:label="MYNODE:3"
        id="g1837"
-       transform="translate(665.54481,-11.353461)">
+       transform="translate(620.54487,-11.353461)">
       <g
          transform="translate(1466.6549,2099.2529)"
          inkscape:label="HMI:Jump:RelativePage@/FB_TWO@enable=/FB_TWO/SOME_BOOL#enable"
@@ -1286,7 +1298,7 @@
       </g>
     </g>
     <g
-       transform="translate(342.39289,-11.353461)"
+       transform="translate(312.39295,-11.353461)"
        id="g1823"
        inkscape:label="MYNODE:2">
       <g
@@ -1350,7 +1362,7 @@
     <g
        inkscape:label="MYNODE:1"
        id="g1717"
-       transform="translate(19.240974,-11.353461)">
+       transform="translate(4.2410198,-11.353461)">
       <g
          transform="translate(1466.6549,2099.2529)"
          inkscape:label="HMI:Jump:RelativePage@/FB_ZERO@enable=/FB_ZERO/SOME_BOOL#enable"
@@ -1815,4 +1827,23 @@
        x="44.283585"
        y="-39.185921"
        id="tspan2966">- Press Ctrl+X to edit SVG elements directly with XML editor</tspan></text>
+  <g
+     transform="translate(-4020,2.1367187e-5)"
+     inkscape:label="HMI:VarInit:0@page_number"
+     id="g304">
+    <text
+       id="text302"
+       y="-108.39357"
+       x="3726.6924"
+       style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:18.66666603px;line-height:125%;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:center;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
+       xml:space="preserve"><tspan
+         y="-108.39357"
+         x="3726.6924"
+         id="tspan298"
+         sodipodi:role="line">declaration of user_level HMI local variable</tspan><tspan
+         id="tspan300"
+         y="-85.060234"
+         x="3726.6924"
+         sodipodi:role="line">(not a PLC variable)</tspan></text>
+  </g>
 </svg>
--- a/svghmi/detachable_pages.ysl2	Fri Nov 18 10:44:56 2022 +0100
+++ b/svghmi/detachable_pages.ysl2	Wed Nov 23 11:27:55 2022 +0100
@@ -137,6 +137,10 @@
 const "_detachable_elements", "func:detachable_elements($hmi_pages | $keypads)";
 const "detachable_elements", "$_detachable_elements[not(ancestor::*/@id = $_detachable_elements/@id)]";
 
+emit "declarations:page-class" {
+    | class PageWidget extends Widget{}
+}
+
 emit "declarations:detachable-elements" {
     |
     | var detachable_elements = {
@@ -165,8 +169,13 @@
 
     const "all_page_widgets","$hmi_widgets[@id = $page_all_elements/@id and @id != $page/@id]";
     const "page_managed_widgets","$all_page_widgets[not(@id=$in_forEach_widget_ids)]";
+
+    const "page_root_path", "$desc/path[not(@assign)]";
+    if "count($page_root_path)>1"
+        error > Page id="«$page/@id»" : only one root path can be declared
+
     const "page_relative_widgets",
-        "$page_managed_widgets[func:is_descendant_path(func:widget(@id)/path/@value, $desc/path/@value)]";
+        "$page_managed_widgets[func:is_descendant_path(func:widget(@id)/path/@value, $page_root_path/@value)]";
 
     // Take closest ancestor in detachable_elements
     // since nested detachable elements are filtered out
@@ -178,19 +187,19 @@
            ancestor-or-self::*[@id = $detachable_elements/@id]""";
 
     |   "«$pagename»": {
-    //|     widget: hmi_widgets["«@id»"],
     |     bbox: [«$p/@x», «$p/@y», «$p/@w», «$p/@h»],
-    if "$desc/path/@value" {
-        if "count($desc/path/@index)=0"
-            warning > Page id="«$page/@id»" : No match for path "«$desc/path/@value»" in HMI tree
-    |     page_index: «$desc/path/@index»,
-    |     page_class: "«$indexed_hmitree/*[@hmipath = $desc/path/@value]/@class»",
+    if "count($page_root_path)=1"{
+        if "count($page_root_path/@index)=0"
+            warning > Page id="«$page/@id»" : No match for path "«$page_root_path/@value»" in HMI tree
+    |     page_index: «$page_root_path/@index»,
+    |     page_class: "«$indexed_hmitree/*[@hmipath = $page_root_path/@value]/@class»",
     }
     |     widgets: [
+    |         [hmi_widgets["«$page/@id»"], []],
     foreach "$page_managed_widgets" {
         const "widget_paths_relativeness" 
             foreach "func:widget(@id)/path" {
-                value "func:is_descendant_path(@value, $desc/path/@value)";
+                value "func:is_descendant_path(@value, $page_root_path/@value)";
                 if "position()!=last()" > ,
             }
     |         [hmi_widgets["«@id»"], [«$widget_paths_relativeness»]]`if "position()!=last()" > ,`
--- a/svghmi/svghmi.js	Fri Nov 18 10:44:56 2022 +0100
+++ b/svghmi/svghmi.js	Wed Nov 23 11:27:55 2022 +0100
@@ -525,6 +525,9 @@
         ? page_name
         : page_name + "@" + hmitree_paths[page_index]);
 
+    // when entering a page, assignments are evaluated
+    new_desc.widgets[0][0].assign();
+
     return true;
 };
 
--- a/svghmi/widget_jump.ysl2	Fri Nov 18 10:44:56 2022 +0100
+++ b/svghmi/widget_jump.ysl2	Wed Nov 23 11:27:55 2022 +0100
@@ -143,8 +143,8 @@
             otherwise value "$page_desc/arg[1]/@value";
         }
         const "target_page_path" choose {
-            when "arg" value "$hmi_pages_descs[arg[1]/@value = $target_page_name]/path[1]/@value";
-            otherwise value "$page_desc/path[1]/@value";
+            when "arg" value "$hmi_pages_descs[arg[1]/@value = $target_page_name]/path[not(@assign)]/@value";
+            otherwise value "$page_desc/path[not(@assign)]/@value";
         }
 
         if "not(func:same_class_paths($target_page_path, path[1]/@value))"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/svghmi/widget_page.ysl2	Wed Nov 23 11:27:55 2022 +0100
@@ -0,0 +1,60 @@
+// widget_page.ysl2
+
+widget_desc("Page") {
+    longdesc
+    ||
+
+    Arguments are either:
+
+    - XXX reference path TODO
+
+    - name=value: setting variable with literal value.
+    - name=other_name: copy variable content into another
+
+    "active"+"inactive" labeled elements can be provided to show feedback when pressed
+
+    Exemples:
+
+    HMI:Page:notify=1@notify=/PLCVAR
+    HMI:Page:ack=2:notify=1@ack=.local_var@notify=/PLCVAR
+
+    ||
+
+    shortdesc > Page 
+
+}
+
+widget_defs("Page") {
+
+    |     assignments: {},
+    |     dispatch: function(value, oldval, varnum) {
+    const "widget", ".";
+    foreach "path" {
+        const "varid","generate-id()";
+        const "varnum","position()-1";
+        if "@assign" foreach "$widget/path[@assign]" if "$varid = generate-id()" {
+    |         if(varnum == «$varnum») this.assignments["«@assign»"] = value;
+        }
+    }
+    |     },
+    |     assign: function() {
+    const "paths","path";
+    foreach "arg[contains(@value,'=')]"{
+        const "name","substring-before(@value,'=')";
+        const "value","substring-after(@value,'=')";
+        const "index" foreach "$paths" if "@assign = $name" value "position()-1";
+        const "isVarName", "regexp:test($value,'^[a-zA-Z_][a-zA-Z0-9_]+$')";
+        choose {
+            when "$isVarName"{
+    |         const «$value» = this.assignments["«$value»"];
+    |         if(«$value» != undefined)
+    |             this.apply_hmi_value(«$index», «$value»);
+            }
+            otherwise {
+    |         this.apply_hmi_value(«$index», «$value»);
+            }
+        }
+    }
+    |     },
+}
+
--- a/svghmi/widgets_common.ysl2	Fri Nov 18 10:44:56 2022 +0100
+++ b/svghmi/widgets_common.ysl2	Wed Nov 23 11:27:55 2022 +0100
@@ -600,12 +600,14 @@
 }
 
 const "included_ids","$parsed_widgets/widget[not(@type = $excluded_types) and not(@id = $discardable_elements/@id)]/@id";
+const "page_ids","$parsed_widgets/widget[@type = 'Page']/@id";
 const "hmi_widgets","$hmi_elements[@id = $included_ids]";
+const "page_widgets","$hmi_elements[@id = $page_ids]";
 const "result_widgets","$result_svg_ns//*[@id = $hmi_widgets/@id]";
 
 emit "declarations:hmi-elements" {
     | var hmi_widgets = {
-    apply "$hmi_widgets", mode="hmi_widgets";
+    apply "$hmi_widgets | $page_widgets", mode="hmi_widgets";
     | }
     |
 }