svghmi/ui.py
branchpython3
changeset 3750 f62625418bff
parent 3609 51a3d6f39944
child 3818 e528b11a60cc
equal deleted inserted replaced
3749:fda6c1a37662 3750:f62625418bff
     4 # This file is part of Beremiz
     4 # This file is part of Beremiz
     5 # Copyright (C) 2021: Edouard TISSERANT
     5 # Copyright (C) 2021: Edouard TISSERANT
     6 #
     6 #
     7 # See COPYING file for copyrights details.
     7 # See COPYING file for copyrights details.
     8 
     8 
     9 from __future__ import absolute_import
     9 
    10 import os
    10 import os
    11 import hashlib
    11 import hashlib
    12 import weakref
    12 import weakref
    13 import re
    13 import re
    14 import tempfile
    14 import tempfile
    15 from threading import Thread, Lock
    15 from threading import Thread, Lock
    16 from functools import reduce
    16 from functools import reduce
    17 from itertools import izip
    17 
    18 from operator import or_
    18 from operator import or_
    19 from tempfile import NamedTemporaryFile
    19 from tempfile import NamedTemporaryFile
    20 
    20 
    21 import wx
    21 import wx
    22 from wx.lib.scrolledpanel import ScrolledPanel
    22 from wx.lib.scrolledpanel import ScrolledPanel
    32 from util.ProcessLogger import ProcessLogger
    32 from util.ProcessLogger import ProcessLogger
    33 
    33 
    34 # When running as a confined Snap, /tmp isn't accessible from the outside
    34 # When running as a confined Snap, /tmp isn't accessible from the outside
    35 # and Widget DnD to Inkscape can't work, since it can't find generated svg 
    35 # and Widget DnD to Inkscape can't work, since it can't find generated svg 
    36 # This forces tmp directory in $SNAP_USER_DATA, accessible from other apps
    36 # This forces tmp directory in $SNAP_USER_DATA, accessible from other apps
    37 if os.environ.has_key("SNAP"):
    37 if "SNAP" in os.environ:
    38      NamedTemporaryFile_orig = NamedTemporaryFile
    38      NamedTemporaryFile_orig = NamedTemporaryFile
    39      tmpdir = os.path.join(os.environ["SNAP_USER_DATA"], ".tmp")
    39      tmpdir = os.path.join(os.environ["SNAP_USER_DATA"], ".tmp")
    40      if not os.path.exists(tmpdir):
    40      if not os.path.exists(tmpdir):
    41          os.mkdir(tmpdir)
    41          os.mkdir(tmpdir)
    42      def NamedTemporaryFile(*args,**kwargs):
    42      def NamedTemporaryFile(*args,**kwargs):
   247     def OnArgChanged(self, event):
   247     def OnArgChanged(self, event):
   248         txt = self.edit.GetValue()
   248         txt = self.edit.GetValue()
   249         accepts = self.argdesc.get("accepts").split(',')
   249         accepts = self.argdesc.get("accepts").split(',')
   250         self.setValidity(
   250         self.setValidity(
   251             reduce(or_,
   251             reduce(or_,
   252                    map(lambda typename: 
   252                    [models[typename].match(txt) is not None for typename in accepts], 
   253                            models[typename].match(txt) is not None,
       
   254                        accepts), 
       
   255                    False)
   253                    False)
   256             if accepts and txt else None)
   254             if accepts and txt else None)
   257         self.ParentObj.RegenSVGLater()
   255         self.ParentObj.RegenSVGLater()
   258         event.Skip()
   256         event.Skip()
   259 
   257 
   281         self.setValidity(None)
   279         self.setValidity(None)
   282         self.ParentObj.RegenSVGLater()
   280         self.ParentObj.RegenSVGLater()
   283         event.Skip()
   281         event.Skip()
   284     
   282     
   285 def KeepDoubleNewLines(txt):
   283 def KeepDoubleNewLines(txt):
   286     return "\n\n".join(map(
   284     return "\n\n".join([re.sub(r'\s+',' ',s) for s in txt.split("\n\n")])
   287         lambda s:re.sub(r'\s+',' ',s),
       
   288         txt.split("\n\n")))
       
   289 
   285 
   290 _conf_key = "SVGHMIWidgetLib"
   286 _conf_key = "SVGHMIWidgetLib"
   291 _preview_height = 200
   287 _preview_height = 200
   292 _preview_margin = 5
   288 _preview_margin = 5
   293 thumbnail_temp_path = None
   289 thumbnail_temp_path = None
   651         if args and len(prefillargs) > len(args):
   647         if args and len(prefillargs) > len(args):
   652             # TODO: check ordinality of last arg
   648             # TODO: check ordinality of last arg
   653             # TODO: check that only last arg has multiple ordinality
   649             # TODO: check that only last arg has multiple ordinality
   654             args += [args[-1]]*(len(prefillargs)-len(args))
   650             args += [args[-1]]*(len(prefillargs)-len(args))
   655         self.args_box.Show(len(args)!=0)
   651         self.args_box.Show(len(args)!=0)
   656         for arg, prefillarg in izip(args,prefillargs):
   652         for arg, prefillarg in zip(args,prefillargs):
   657             self.AddArgToSignature(arg, prefillarg)
   653             self.AddArgToSignature(arg, prefillarg)
   658 
   654 
   659         # TODO support predefined path count (as for XYGraph)
   655         # TODO support predefined path count (as for XYGraph)
   660         paths = defs.findall("path")
   656         paths = defs.findall("path")
   661         self.paths_box.Show(len(paths)!=0)
   657         self.paths_box.Show(len(paths)!=0)