author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Thu, 08 Aug 2019 15:56:17 +0200 | |
branch | svghmi |
changeset 2751 | eab6161e603d |
parent 2750 | 2694170cd88e |
child 2753 | 9a7e12e96399 |
permissions | -rw-r--r-- |
2745 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
# This file is part of Beremiz |
|
5 |
# Copyright (C) 2019: Edouard TISSERANT |
|
6 |
# |
|
7 |
# See COPYING file for copyrights details. |
|
8 |
||
9 |
from __future__ import absolute_import |
|
10 |
import os |
|
11 |
import shutil |
|
12 |
||
13 |
import wx |
|
14 |
||
15 |
import util.paths as paths |
|
16 |
from POULibrary import POULibrary |
|
17 |
from docutil import open_svg |
|
18 |
||
2749
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
19 |
HMI_TYPES_DESC = { |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
20 |
"HMI_CLASS":{}, |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
21 |
"HMI_LABEL":{}, |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
22 |
"HMI_STRING":{}, |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
23 |
"HMI_INT":{}, |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
24 |
"HMI_REAL":{} |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
25 |
} |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
26 |
|
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
27 |
HMI_TYPES = HMI_TYPES_DESC.keys() |
2745 | 28 |
|
29 |
class SVGHMILibrary(POULibrary): |
|
30 |
def GetLibraryPath(self): |
|
2750 | 31 |
return paths.AbsNeighbourFile(__file__, "pous.xml") |
2745 | 32 |
|
2749
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
33 |
def Generate_C(self, buildpath, varlist, IECCFLAGS): |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
34 |
|
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
35 |
# Filter known HMI types |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
36 |
hmi_types_instances = [v for v in varlist if v["derived"] in HMI_TYPES] |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
37 |
|
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
38 |
# TODO deduce HMI tree |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
39 |
|
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
40 |
# TODO generate C code to observe/access HMI tree variables |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
41 |
svghmi_c_filepath = paths.AbsNeighbourFile(__file__, "svghmi.c") |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
42 |
svghmi_c_file = open(svghmi_c_filepath, 'r') |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
43 |
svghmi_c_code = svghmi_c_file.read() |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
44 |
svghmi_c_file.close() |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
45 |
svghmi_c_code = svghmi_c_code % { "the code": "/* TODO */"} |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
46 |
|
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
47 |
gen_svghmi_c_path = os.path.join(buildpath, "svghmi.c") |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
48 |
gen_svghmi_c = open(gen_svghmi_c_path, 'w') |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
49 |
gen_svghmi_c.write(svghmi_c_code) |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
50 |
gen_svghmi_c.close() |
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
51 |
|
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
52 |
return (["svghmi"], [(gen_svghmi_c_path, IECCFLAGS)], True), "" |
2745 | 53 |
|
54 |
class SVGHMI(object): |
|
55 |
XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?> |
|
56 |
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
|
57 |
<xsd:element name="SVGHMI"> |
|
58 |
<xsd:complexType> |
|
59 |
<xsd:attribute name="enableHTTP" type="xsd:boolean" use="optional" default="false"/> |
|
60 |
<xsd:attribute name="bindAddress" type="xsd:string" use="optional" default="localhost"/> |
|
61 |
<xsd:attribute name="port" type="xsd:string" use="optional" default="8080"/> |
|
62 |
</xsd:complexType> |
|
63 |
</xsd:element> |
|
64 |
</xsd:schema> |
|
65 |
""" |
|
66 |
||
67 |
ConfNodeMethods = [ |
|
68 |
{ |
|
69 |
"bitmap": "ImportSVG", |
|
70 |
"name": _("Import SVG"), |
|
71 |
"tooltip": _("Import SVG"), |
|
72 |
"method": "_ImportSVG" |
|
73 |
}, |
|
74 |
{ |
|
75 |
"bitmap": "ImportSVG", # should be something different |
|
76 |
"name": _("Inkscape"), |
|
77 |
"tooltip": _("Edit HMI"), |
|
78 |
"method": "_StartInkscape" |
|
79 |
}, |
|
80 |
] |
|
81 |
||
82 |
def _getSVGpath(self, project_path=None): |
|
83 |
if project_path is None: |
|
84 |
project_path = self.CTNPath() |
|
85 |
# define name for SVG file containing gui layout |
|
86 |
return os.path.join(project_path, "gui.svg") |
|
87 |
||
88 |
||
89 |
def OnCTNSave(self, from_project_path=None): |
|
90 |
if from_project_path is not None: |
|
91 |
shutil.copyfile(self._getSVGpath(from_project_path), |
|
92 |
self._getSVGpath()) |
|
2750 | 93 |
return True |
2745 | 94 |
|
95 |
def CTNGenerate_C(self, buildpath, locations): |
|
96 |
""" |
|
97 |
Return C code generated by iec2c compiler |
|
98 |
when _generate_softPLC have been called |
|
99 |
@param locations: ignored |
|
100 |
@return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND |
|
101 |
""" |
|
102 |
||
2749
2769b3aed34d
Use a POU Library's Generate_C to collect all variables in SVGHMI.
Edouard Tisserant
parents:
2747
diff
changeset
|
103 |
# TODO fetch HMI tree from library |
2745 | 104 |
|
105 |
svgfile = self._getSVGpath() |
|
106 |
if os.path.exists(svgfile): |
|
107 |
# TODO : call xslt transform on Inkscape's SVG to generate |
|
108 |
# - Errors on HMI semantics |
|
109 |
# - Target XHTML as a DOM |
|
110 |
# - ... maybe something to have a global view of what is declared in SVG. |
|
111 |
pass |
|
112 |
else: |
|
113 |
# TODO : use default svg that expose the HMI tree as-is |
|
114 |
pass |
|
115 |
||
116 |
||
117 |
res = ([], "", False) |
|
118 |
||
119 |
targetpath = os.path.join(self._getBuildPath(), "target.xhtml") |
|
120 |
targetfile = open(targetpath, 'w') |
|
121 |
||
122 |
# TODO : DOM to string |
|
123 |
targetfile.write("TODO") |
|
124 |
targetfile.close() |
|
125 |
res += (("target.js", open(targetpath, "rb")),) |
|
126 |
||
127 |
# TODO add C code to expose HMI tree variables to shared memory |
|
128 |
# TODO generate a description of shared memory (xml or CSV) |
|
129 |
# that can be loaded by svghmi QTWeb* app or svghmi server |
|
130 |
||
131 |
||
132 |
return res |
|
133 |
||
134 |
def _ImportSVG(self): |
|
135 |
dialog = wx.FileDialog(self.GetCTRoot().AppFrame, _("Choose a SVG file"), os.getcwd(), "", _("SVG files (*.svg)|*.svg|All files|*.*"), wx.OPEN) |
|
136 |
if dialog.ShowModal() == wx.ID_OK: |
|
137 |
svgpath = dialog.GetPath() |
|
138 |
if os.path.isfile(svgpath): |
|
139 |
shutil.copy(svgpath, self._getSVGpath()) |
|
140 |
else: |
|
141 |
self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n") % svgpath) |
|
142 |
dialog.Destroy() |
|
143 |
||
144 |
def _StartInkscape(self): |
|
145 |
svgfile = self._getSVGpath() |
|
146 |
open_inkscape = True |
|
147 |
if not self.GetCTRoot().CheckProjectPathPerm(): |
|
148 |
dialog = wx.MessageDialog(self.GetCTRoot().AppFrame, |
|
149 |
_("You don't have write permissions.\nOpen Inkscape anyway ?"), |
|
150 |
_("Open Inkscape"), |
|
151 |
wx.YES_NO | wx.ICON_QUESTION) |
|
152 |
open_inkscape = dialog.ShowModal() == wx.ID_YES |
|
153 |
dialog.Destroy() |
|
154 |
if open_inkscape: |
|
155 |
if not os.path.isfile(svgfile): |
|
156 |
svgfile = None |
|
157 |
open_svg(svgfile) |