SVGHMI: Ensure that when widgets are nested in HMI:Switch widget, their sub-elements are taken as switch's choice in case their label match the syntax. svghmi
authorEdouard Tisserant
Thu, 01 Apr 2021 16:00:58 +0200
branchsvghmi
changeset 3220 ec365ef396b1
parent 3219 cc0ecc5e918f
child 3221 3d307ad803ea
SVGHMI: Ensure that when widgets are nested in HMI:Switch widget, their sub-elements are taken as switch's choice in case their label match the syntax.
svghmi/gen_index_xhtml.xslt
svghmi/widget_switch.ysl2
tests/svghmi/svghmi_0@svghmi/svghmi.svg
--- a/svghmi/gen_index_xhtml.xslt	Thu Apr 01 15:57:06 2021 +0200
+++ b/svghmi/gen_index_xhtml.xslt	Thu Apr 01 16:00:58 2021 +0200
@@ -6715,7 +6715,10 @@
     <xsl:text>    choices: [
 </xsl:text>
     <xsl:variable name="regex" select="'^(&quot;[^&quot;].*&quot;|\-?[0-9]+|false|true)(#.*)?$'"/>
-    <xsl:for-each select="$result_svg_ns//*[@id = $hmi_element/@id]//*[regexp:test(@inkscape:label,$regex)]">
+    <xsl:variable name="subelts" select="$result_widgets[@id = $hmi_element/@id]//*"/>
+    <xsl:variable name="subwidgets" select="$subelts//*[@id = $hmi_widgets/@id]"/>
+    <xsl:variable name="accepted" select="$subelts[not(ancestor-or-self::*/@id = $subwidgets/@id)]"/>
+    <xsl:for-each select="$accepted[regexp:test(@inkscape:label,$regex)]">
       <xsl:variable name="literal" select="regexp:match(@inkscape:label,$regex)[2]"/>
       <xsl:text>        {
 </xsl:text>
@@ -6770,15 +6773,7 @@
 </xsl:text>
     <xsl:text>        //toggle state and apply
 </xsl:text>
-    <xsl:text>        if (this.state) {
-</xsl:text>
-    <xsl:text>            this.state = 0;
-</xsl:text>
-    <xsl:text>        } else {
-</xsl:text>
-    <xsl:text>            this.state = 1;
-</xsl:text>
-    <xsl:text>        }
+    <xsl:text>        this.state = this.state ? false : true;
 </xsl:text>
     <xsl:text>        this.apply_hmi_value(0, this.state);
 </xsl:text>
@@ -6792,23 +6787,27 @@
 </xsl:text>
     <xsl:text>
 </xsl:text>
+    <xsl:text>    activate(val) {
+</xsl:text>
+    <xsl:text>        let [active, inactive] = val ? ["none",""] : ["", "none"];
+</xsl:text>
+    <xsl:text>        if (this.active_elt)
+</xsl:text>
+    <xsl:text>            this.active_elt.style.display = active;
+</xsl:text>
+    <xsl:text>        if (this.inactive_elt)
+</xsl:text>
+    <xsl:text>            this.inactive_elt.style.display = inactive;
+</xsl:text>
+    <xsl:text>    }
+</xsl:text>
+    <xsl:text>
+</xsl:text>
     <xsl:text>    animate(){
 </xsl:text>
-    <xsl:text>       // redraw toggle button on screen refresh
-</xsl:text>
-    <xsl:text>       if (this.state) {
-</xsl:text>
-    <xsl:text>           this.active_elt.setAttribute("style", this.active_style);
-</xsl:text>
-    <xsl:text>           this.inactive_elt.setAttribute("style", "display:none");
-</xsl:text>
-    <xsl:text>       } else {
-</xsl:text>
-    <xsl:text>           this.inactive_elt.setAttribute("style", this.inactive_style);
-</xsl:text>
-    <xsl:text>           this.active_elt.setAttribute("style", "display:none");
-</xsl:text>
-    <xsl:text>       }
+    <xsl:text>        // redraw toggle button on screen refresh
+</xsl:text>
+    <xsl:text>        this.activate(this.state);
 </xsl:text>
     <xsl:text>    }
 </xsl:text>
@@ -6816,23 +6815,9 @@
 </xsl:text>
     <xsl:text>    init() {
 </xsl:text>
-    <xsl:text>        this.active_style = this.active_elt ? this.active_elt.style.cssText : undefined;
-</xsl:text>
-    <xsl:text>        this.inactive_style = this.inactive_elt ? this.inactive_elt.style.cssText : undefined;
-</xsl:text>
-    <xsl:text>
-</xsl:text>
-    <xsl:text>        if (this.active_style &amp;&amp; this.inactive_style) {
-</xsl:text>
-    <xsl:text>            this.active_elt.setAttribute("style", "display:none");
-</xsl:text>
-    <xsl:text>            this.inactive_elt.setAttribute("style", this.inactive_style);
-</xsl:text>
-    <xsl:text>        }
-</xsl:text>
-    <xsl:text>
-</xsl:text>
-    <xsl:text>        this.element.setAttribute("onclick", "hmi_widgets['"+this.element_id+"'].on_click(evt)");
+    <xsl:text>        this.activate(false);
+</xsl:text>
+    <xsl:text>        this.element.onclick = (evt) =&gt; this.on_click(evt);
 </xsl:text>
     <xsl:text>    }
 </xsl:text>
@@ -6848,8 +6833,6 @@
       </xsl:with-param>
       <xsl:with-param name="mandatory" select="'no'"/>
     </xsl:call-template>
-    <xsl:text>
-</xsl:text>
   </xsl:template>
   <xsl:template match="/">
     <xsl:comment>
--- a/svghmi/widget_switch.ysl2	Thu Apr 01 15:57:06 2021 +0200
+++ b/svghmi/widget_switch.ysl2	Thu Apr 01 16:00:58 2021 +0200
@@ -20,7 +20,12 @@
     param "hmi_element";
     |     choices: [
     const "regex",!"'^(\"[^\"].*\"|\-?[0-9]+|false|true)(#.*)?$'"!;
-    foreach "$result_svg_ns//*[@id = $hmi_element/@id]//*[regexp:test(@inkscape:label,$regex)]" {
+
+    const "subelts", "$result_widgets[@id = $hmi_element/@id]//*";
+    const "subwidgets", "$subelts//*[@id = $hmi_widgets/@id]";
+    const "accepted", "$subelts[not(ancestor-or-self::*/@id = $subwidgets/@id)]";
+
+    foreach "$accepted[regexp:test(@inkscape:label,$regex)]" {
         const "literal", "regexp:match(@inkscape:label,$regex)[2]";
     |         {
     |             elt:id("«@id»"),
--- a/tests/svghmi/svghmi_0@svghmi/svghmi.svg	Thu Apr 01 15:57:06 2021 +0200
+++ b/tests/svghmi/svghmi_0@svghmi/svghmi.svg	Thu Apr 01 16:00:58 2021 +0200
@@ -128,9 +128,9 @@
      inkscape:current-layer="hmi0"
      showgrid="false"
      units="px"
-     inkscape:zoom="0.42177815"
-     inkscape:cx="1614.7137"
-     inkscape:cy="168.68416"
+     inkscape:zoom="1.1929688"
+     inkscape:cx="772.0138"
+     inkscape:cy="-68.272506"
      inkscape:window-width="1600"
      inkscape:window-height="836"
      inkscape:window-x="0"
@@ -2575,6 +2575,152 @@
          style="fill:#8667bf;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-linejoin:round"
          id="path3447"
          inkscape:connector-curvature="0" />
+      <g
+         inkscape:label="HMI:Switch@/TARGETPRESSURE"
+         id="g991-3"
+         transform="matrix(0.5,0,0,0.5,699.09791,793.95217)"
+         style="stroke-width:2">
+        <g
+           id="g1091-3"
+           inkscape:label="4"
+           transform="translate(-136.52022,250.47452)"
+           style="stroke-width:2">
+          <path
+             inkscape:transform-center-x="14.956371"
+             inkscape:transform-center-y="-3.6154501e-05"
+             d="m 622.6459,-170.03172 -51.81035,0 25.90517,-44.86908 z"
+             inkscape:randomized="0"
+             inkscape:rounded="0"
+             inkscape:flatsided="true"
+             sodipodi:arg2="1.5707963"
+             sodipodi:arg1="0.52359878"
+             sodipodi:r2="14.956361"
+             sodipodi:r1="29.912722"
+             sodipodi:cy="-184.98808"
+             sodipodi:cx="596.74072"
+             sodipodi:sides="3"
+             id="path981-1"
+             style="color:#000000;clip-rule:nonzero;display:inline;overflow:visible;visibility:visible;opacity:1;isolation:auto;mix-blend-mode:normal;color-interpolation:sRGB;color-interpolation-filters:linearRGB;solid-color:#000000;solid-opacity:1;vector-effect:none;fill:#e6e6e6;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:10;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;marker:none;color-rendering:auto;image-rendering:auto;shape-rendering:auto;text-rendering:auto;enable-background:accumulate"
+             sodipodi:type="star"
+             transform="matrix(0,-2.0000001,1.9999999,0,1034.195,1298.6541)" />
+          <text
+             xml:space="preserve"
+             style="font-style:normal;font-weight:normal;font-size:20px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
+             x="661.09552"
+             y="111.05016"
+             id="text1059-7"><tspan
+               sodipodi:role="line"
+               id="tspan1057-5"
+               x="661.09552"
+               y="111.05016"
+               style="stroke-width:1px">4</tspan></text>
+        </g>
+        <g
+           id="g1096-9"
+           inkscape:label="3"
+           transform="translate(-136.52022,250.47452)"
+           style="stroke-width:2">
+          <path
+             inkscape:transform-center-x="14.956364"
+             transform="rotate(-90,746.45698,-44.543641)"
+             sodipodi:type="star"
+             style="color:#000000;clip-rule:nonzero;display:inline;overflow:visible;visibility:visible;opacity:1;isolation:auto;mix-blend-mode:normal;color-interpolation:sRGB;color-interpolation-filters:linearRGB;solid-color:#000000;solid-opacity:1;vector-effect:none;fill:#e6e6e6;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:10;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;marker:none;color-rendering:auto;image-rendering:auto;shape-rendering:auto;text-rendering:auto;enable-background:accumulate"
+             id="path979-6"
+             sodipodi:sides="3"
+             sodipodi:cx="596.74072"
+             sodipodi:cy="-216.2599"
+             sodipodi:r1="59.825443"
+             sodipodi:r2="29.912722"
+             sodipodi:arg1="0.52359878"
+             sodipodi:arg2="1.5707963"
+             inkscape:flatsided="true"
+             inkscape:rounded="0"
+             inkscape:randomized="0"
+             d="m 648.55108,-186.34718 -103.62071,0 51.81035,-89.73817 z"
+             inkscape:transform-center-y="-5.9989963e-06" />
+          <text
+             id="text1063-2"
+             y="111.05016"
+             x="565.25018"
+             style="font-style:normal;font-weight:normal;font-size:20px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
+             xml:space="preserve"><tspan
+               style="stroke-width:1px"
+               y="111.05016"
+               x="565.25018"
+               id="tspan1061-1"
+               sodipodi:role="line">3</tspan></text>
+        </g>
+        <g
+           id="g1076-7"
+           inkscape:label="2"
+           transform="translate(-416.52022,170.47452)"
+           style="stroke-width:2">
+          <path
+             inkscape:transform-center-x="-14.956365"
+             transform="matrix(0,-1,-1,0,1043.9134,701.91334)"
+             inkscape:transform-center-y="-5.5023185e-06"
+             d="m 648.55108,135.08534 -103.62071,0 51.81035,-89.738161 z"
+             inkscape:randomized="0"
+             inkscape:rounded="0"
+             inkscape:flatsided="true"
+             sodipodi:arg2="1.5707963"
+             sodipodi:arg1="0.52359878"
+             sodipodi:r2="29.912722"
+             sodipodi:r1="59.825443"
+             sodipodi:cy="105.17262"
+             sodipodi:cx="596.74072"
+             sodipodi:sides="3"
+             id="path985-8"
+             style="color:#000000;clip-rule:nonzero;display:inline;overflow:visible;visibility:visible;opacity:1;isolation:auto;mix-blend-mode:normal;color-interpolation:sRGB;color-interpolation-filters:linearRGB;solid-color:#000000;solid-opacity:1;vector-effect:none;fill:#e6e6e6;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:10;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;marker:none;color-rendering:auto;image-rendering:auto;shape-rendering:auto;text-rendering:auto;enable-background:accumulate"
+             sodipodi:type="star" />
+          <text
+             id="text1067-5"
+             y="111.05016"
+             x="925.82605"
+             style="font-style:normal;font-weight:normal;font-size:20px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
+             xml:space="preserve"><tspan
+               style="stroke-width:1px"
+               y="111.05016"
+               x="925.82605"
+               id="tspan1065-7"
+               sodipodi:role="line">2</tspan></text>
+        </g>
+        <g
+           id="g1081-4"
+           inkscape:label="1"
+           transform="translate(-416.52022,170.47452)"
+           style="stroke-width:2">
+          <path
+             inkscape:transform-center-x="-14.956349"
+             transform="matrix(0,-2.0000001,-1.9999999,0,1122.1514,1298.6541)"
+             sodipodi:type="star"
+             style="color:#000000;clip-rule:nonzero;display:inline;overflow:visible;visibility:visible;opacity:1;isolation:auto;mix-blend-mode:normal;color-interpolation:sRGB;color-interpolation-filters:linearRGB;solid-color:#000000;solid-opacity:1;vector-effect:none;fill:#e6e6e6;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:10;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;marker:none;color-rendering:auto;image-rendering:auto;shape-rendering:auto;text-rendering:auto;enable-background:accumulate"
+             id="path987-1"
+             sodipodi:sides="3"
+             sodipodi:cx="596.74072"
+             sodipodi:cy="136.44444"
+             sodipodi:r1="29.912722"
+             sodipodi:r2="14.956361"
+             sodipodi:arg1="0.52359878"
+             sodipodi:arg2="1.5707963"
+             inkscape:flatsided="true"
+             inkscape:rounded="0"
+             inkscape:randomized="0"
+             d="m 622.6459,151.4008 -51.81035,0 25.90517,-44.86908 z"
+             inkscape:transform-center-y="-3.3040441e-05" />
+          <text
+             xml:space="preserve"
+             style="font-style:normal;font-weight:normal;font-size:20px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
+             x="842.71497"
+             y="111.05016"
+             id="text1071-8"><tspan
+               sodipodi:role="line"
+               id="tspan1069-5"
+               x="842.71497"
+               y="111.05016"
+               style="stroke-width:1px">1</tspan></text>
+        </g>
+      </g>
     </g>
   </g>
   <g
@@ -7172,4 +7318,47 @@
        x="1569.0527"
        y="594.56055"
        style="fill:#ffffff;fill-opacity:1;stroke-width:0.25px">page node</tspan></text>
+  <g
+     transform="matrix(0.57180538,0,0,0.57180538,165.58244,517.80347)"
+     id="g443-9"
+     inkscape:label="HMI:ToggleButton@/PUMP0/BOOLIN"
+     style="stroke-width:1">
+    <rect
+       style="color:#000000;clip-rule:nonzero;display:inline;overflow:visible;visibility:visible;opacity:1;isolation:auto;mix-blend-mode:normal;color-interpolation:sRGB;color-interpolation-filters:linearRGB;solid-color:#000000;solid-opacity:1;vector-effect:none;fill:#e6e6e6;fill-opacity:1;fill-rule:nonzero;stroke:#ff6600;stroke-width:5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;marker:none;color-rendering:auto;image-rendering:auto;shape-rendering:auto;text-rendering:auto;enable-background:accumulate"
+       id="rect5492-7"
+       width="245.44583"
+       height="95.723877"
+       x="971.96545"
+       y="594.82263"
+       ry="23.930969"
+       inkscape:label="inactive"
+       rx="23.930969" />
+    <rect
+       rx="23.930969"
+       inkscape:label="active"
+       ry="23.930969"
+       y="594.82263"
+       x="971.96545"
+       height="95.723877"
+       width="245.44583"
+       id="rect433-5"
+       style="color:#000000;clip-rule:nonzero;display:inline;overflow:visible;visibility:visible;opacity:1;isolation:auto;mix-blend-mode:normal;color-interpolation:sRGB;color-interpolation-filters:linearRGB;solid-color:#000000;solid-opacity:1;vector-effect:none;fill:#fdfdfd;fill-opacity:1;fill-rule:nonzero;stroke:#ffd0b2;stroke-width:28.60938263;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;marker:none;color-rendering:auto;image-rendering:auto;shape-rendering:auto;text-rendering:auto;enable-background:accumulate" />
+    <g
+       style="stroke-width:1"
+       inkscape:label="text"
+       id="g952-3">
+      <text
+         xml:space="preserve"
+         style="font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;text-align:center;letter-spacing:0px;word-spacing:0px;text-anchor:middle;display:inline;fill:#ff6600;fill-opacity:1;stroke:none;stroke-width:0.99999994px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
+         x="1090.7626"
+         y="656.98151"
+         id="text950-8"
+         inkscape:label="setting_jmp"><tspan
+           sodipodi:role="line"
+           id="tspan948-8"
+           x="1090.7626"
+           y="656.98151"
+           style="text-align:center;text-anchor:middle;fill:#ff6600;stroke-width:0.99999994px">up</tspan></text>
+    </g>
+  </g>
 </svg>