svgui/svgui.py
author Edouard Tisserant 7BB06627 <edouard.tisserant@gmail.com>
Tue, 24 Jan 2017 01:09:54 +0100
branch1.2
changeset 1641 24168e52fc2b
parent 1511 91538d0c242c
child 1680 6db967480b7d
permissions -rw-r--r--
1.2rc1
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
     1
#!/usr/bin/env python
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
     2
# -*- coding: utf-8 -*-
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
     3
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
     6
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
     8
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
     9
# See COPYING file for copyrights details.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    10
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    11
# This program is free software; you can redistribute it and/or
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    12
# modify it under the terms of the GNU General Public License
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    13
# as published by the Free Software Foundation; either version 2
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    14
# of the License, or (at your option) any later version.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    15
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    16
# This program is distributed in the hope that it will be useful,
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    19
# GNU General Public License for more details.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    20
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    21
# You should have received a copy of the GNU General Public License
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    22
# along with this program; if not, write to the Free Software
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1061
diff changeset
    24
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    25
import wx
407
0bef13d73350 Bug while trying to import SVG file fixed
laurent
parents: 371
diff changeset
    26
import os, sys, shutil
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    27
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    28
from pyjs import translate
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    29
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    30
from POULibrary import POULibrary
737
7b421e080636 fixed typos
Edouard Tisserant
parents: 734
diff changeset
    31
from docutil import open_svg
1061
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    32
from py_ext import PythonFileCTNMixin
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    33
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    34
class SVGUILibrary(POULibrary):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    35
    def GetLibraryPath(self):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    36
        return os.path.join(os.path.split(__file__)[0], "pous.xml") 
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    37
1061
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    38
class SVGUI(PythonFileCTNMixin):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    39
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 427
diff changeset
    40
    ConfNodeMethods = [
734
5c42cafaee15 Moved LPC sources to a separate project
Edouard Tisserant
parents: 731
diff changeset
    41
        {"bitmap" : "ImportSVG",
415
339fa2542481 improved english spelling and grammar and internationalization updated
laurent
parents: 410
diff changeset
    42
         "name" : _("Import SVG"),
339fa2542481 improved english spelling and grammar and internationalization updated
laurent
parents: 410
diff changeset
    43
         "tooltip" : _("Import SVG"),
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    44
         "method" : "_ImportSVG"},
734
5c42cafaee15 Moved LPC sources to a separate project
Edouard Tisserant
parents: 731
diff changeset
    45
        {"bitmap" : "ImportSVG", # should be something different
415
339fa2542481 improved english spelling and grammar and internationalization updated
laurent
parents: 410
diff changeset
    46
         "name" : _("Inkscape"),
339fa2542481 improved english spelling and grammar and internationalization updated
laurent
parents: 410
diff changeset
    47
         "tooltip" : _("Create HMI"),
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    48
         "method" : "_StartInkscape"},
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    49
    ]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    50
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 427
diff changeset
    51
    def ConfNodePath(self):
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    52
        return os.path.join(os.path.dirname(__file__))
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    53
1061
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    54
    def _getSVGpath(self, project_path=None):
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    55
        if project_path is None:
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    56
            project_path = self.CTNPath()
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    57
        # define name for SVG file containing gui layout
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    58
        return os.path.join(project_path, "gui.svg")
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    59
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    60
    def _getSVGUIserverpath(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    61
        return os.path.join(os.path.dirname(__file__), "svgui_server.py")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    62
1061
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    63
    def OnCTNSave(self, from_project_path=None):
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    64
        if from_project_path is not None:
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    65
            shutil.copyfile(self._getSVGpath(from_project_path),
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    66
                            self._getSVGpath())
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    67
        return PythonFileCTNMixin.OnCTNSave(self, from_project_path)
02f371f3e063 Fixed Save As... function in Beremiz
Laurent Bessard
parents: 1014
diff changeset
    68
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
    69
    def CTNGenerate_C(self, buildpath, locations):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    70
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    71
        Return C code generated by iec2c compiler 
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    72
        when _generate_softPLC have been called
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    73
        @param locations: ignored
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    74
        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    75
        """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    76
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    77
        current_location = self.GetCurrentLocation()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    78
        # define a unique name for the generated C file
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    79
        location_str = "_".join(map(lambda x:str(x), current_location))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    80
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    81
        res = ([], "", False)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    82
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    83
        svgfile=self._getSVGpath()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    84
        if os.path.exists(svgfile):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    85
            res += (("gui.svg", file(svgfile,"rb")),)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    86
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    87
        svguiserverfile = open(self._getSVGUIserverpath(), 'r')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    88
        svguiservercode = svguiserverfile.read()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    89
        svguiserverfile.close()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    90
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    91
        svguilibpath = os.path.join(self._getBuildPath(), "svguilib.js")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    92
        svguilibfile = open(svguilibpath, 'w')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    93
        svguilibfile.write(translate(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "sys.py"), "sys"))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    94
        svguilibfile.write(open(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "_pyjs.js"), 'r').read())
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    95
        svguilibfile.write(translate(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "pyjslib.py"), "pyjslib"))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    96
        svguilibfile.write(translate(os.path.join(os.path.dirname(__file__), "svguilib.py"), "svguilib"))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    97
        svguilibfile.write("pyjslib();\nsvguilib();\n")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    98
        svguilibfile.write(open(os.path.join(os.path.dirname(__file__), "pyjs", "lib", "json.js"), 'r').read())
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    99
        svguilibfile.write(open(os.path.join(os.path.dirname(__file__), "livesvg.js"), 'r').read())
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   100
        svguilibfile.close()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   101
        jsmodules = {"LiveSVGPage": "svguilib.js"}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   102
        res += (("svguilib.js", file(svguilibpath,"rb")),)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   103
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   104
        runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   105
        runtimefile = open(runtimefile_path, 'w')
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   106
        runtimefile.write(svguiservercode % {"svgfile" : "gui.svg"})
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   107
        runtimefile.write("""
1014
e2f7d6c95db0 Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents: 737
diff changeset
   108
def _runtime_%(location)s_start():
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   109
    website.LoadHMI(%(svgui_class)s, %(jsmodules)s)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   110
    
1014
e2f7d6c95db0 Now python files provided by extentions have init, start, stop and cleanup hooks
Edouard Tisserant
parents: 737
diff changeset
   111
def _runtime_%(location)s_stop():
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   112
    website.UnLoadHMI()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   113
    
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   114
""" % {"location": location_str,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   115
       "svgui_class": "SVGUI_HMI",
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   116
       "jsmodules" : str(jsmodules),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   117
      })
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   118
        runtimefile.close()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   119
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   120
        res += (("runtime_%s.py"%location_str, file(runtimefile_path,"rb")),)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   121
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   122
        return res
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   123
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   124
    def _ImportSVG(self):
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   125
        dialog = wx.FileDialog(self.GetCTRoot().AppFrame, _("Choose a SVG file"), os.getcwd(), "",  _("SVG files (*.svg)|*.svg|All files|*.*"), wx.OPEN)
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   126
        if dialog.ShowModal() == wx.ID_OK:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   127
            svgpath = dialog.GetPath()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   128
            if os.path.isfile(svgpath):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   129
                shutil.copy(svgpath, self._getSVGpath())
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   130
            else:
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   131
                self.GetCTRoot().logger.write_error(_("No such SVG file: %s\n")%svgpath)
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   132
        dialog.Destroy()  
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   133
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   134
    def _StartInkscape(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   135
        svgfile = self._getSVGpath()
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 421
diff changeset
   136
        open_inkscape = True
718
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   137
        if not self.GetCTRoot().CheckProjectPathPerm():
5d4dc150b956 refactoring
Edouard Tisserant
parents: 717
diff changeset
   138
            dialog = wx.MessageDialog(self.GetCTRoot().AppFrame,
427
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 421
diff changeset
   139
                                      _("You don't have write permissions.\nOpen Inkscape anyway ?"),
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 421
diff changeset
   140
                                      _("Open Inkscape"),
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 421
diff changeset
   141
                                      wx.YES_NO|wx.ICON_QUESTION)
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 421
diff changeset
   142
            open_inkscape = dialog.ShowModal() == wx.ID_YES
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 421
diff changeset
   143
            dialog.Destroy()
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 421
diff changeset
   144
        if open_inkscape:
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 421
diff changeset
   145
            if not os.path.isfile(svgfile):
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 421
diff changeset
   146
                svgfile = None
7ac746c07ff2 Check ProjectPath write permission
greg
parents: 421
diff changeset
   147
            open_svg(svgfile)