# HG changeset patch # User Edouard Tisserant # Date 1611053833 -3600 # Node ID bd20f911201464088f0da34afbdb1377b1afcfca # Parent 5d9ae04ee50f84310c2489e2979471844384ca9e SVGHMI: still WIP, now POT file is properly generated with utf-8 encoding and POEdit is launched when pressing button. diff -r 5d9ae04ee50f -r bd20f9112014 svghmi/gen_index_xhtml.xslt --- a/svghmi/gen_index_xhtml.xslt Mon Jan 18 10:32:13 2021 +0100 +++ b/svghmi/gen_index_xhtml.xslt Tue Jan 19 11:57:13 2021 +0100 @@ -1,6 +1,6 @@ - - + + @@ -573,22 +573,14 @@ jumps: [ - - - - - - - - hmi_widgets[" - - "] - - , - - - + hmi_widgets[" + + "] + + , + + ], @@ -706,6 +698,11 @@ All units must be set to "px" in Inkscape's document properties + + + + + @@ -929,6 +926,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + /* + + */ + + + + + var translations = { + + + " + + ":{ + + } + + , + + + + + }; + + + + + + @@ -1295,16 +1346,12 @@ overshot(new_val, max) { - // TODO: use a Toast - } undershot(new_val, min) { - // TODO: use a Toast - } @@ -2023,9 +2070,19 @@ dispatch(value) { + this.display_val = value; + + this.request_animate(); + + } + + + + animate(){ + if(this.value_elt) - this.value_elt.textContent = String(value); + this.value_elt.textContent = String(this.display_val); let [min,max,start,end] = this.range; @@ -2033,21 +2090,29 @@ let [rx,ry] = this.proportions; - let tip = start + (end-start)*Number(value)/(max-min); + let tip = start + (end-start)*Number(this.display_val)/(max-min); let size = 0; - if (tip-start > Math.PI) { + + + if (tip-start > Math.PI) size = 1; - } else { + else size = 0; - } - - this.path_elt.setAttribute('d', "M "+(cx+rx*Math.cos(start))+","+(cy+ry*Math.sin(start))+" A "+rx+","+ry+" 0 "+size+" 1 "+(cx+rx*Math.cos(tip))+","+(cy+ry*Math.sin(tip))); + + + this.path_elt.setAttribute('d', "M "+(cx+rx*Math.cos(start))+","+(cy+ry*Math.sin(start))+ + + " A "+rx+","+ry+ + + " 0 "+size+ + + " 1 "+(cx+rx*Math.cos(tip))+","+(cy+ry*Math.sin(tip))); } @@ -2055,41 +2120,31 @@ init() { - let start = Number(this.path_elt.getAttribute('sodipodi:start')); - - let end = Number(this.path_elt.getAttribute('sodipodi:end')); - - let cx = Number(this.path_elt.getAttribute('sodipodi:cx')); - - let cy = Number(this.path_elt.getAttribute('sodipodi:cy')); - - let rx = Number(this.path_elt.getAttribute('sodipodi:rx')); - - let ry = Number(this.path_elt.getAttribute('sodipodi:ry')); - - if (ry == 0) { + let [start, end, cx, cy, rx, ry] = ["start", "end", "cx", "cy", "rx", "ry"]. + + map(tag=>Number(this.path_elt.getAttribute('sodipodi:'+tag))) + + + + if (ry == 0) ry = rx; - } - - if (start > end) { + + + if (start > end) end = end + 2*Math.PI; - } - - let min = this.min_elt ? - - Number(this.min_elt.textContent) : - - this.args.length >= 1 ? this.args[0] : 0; - - let max = this.max_elt ? - - Number(this.max_elt.textContent) : - - this.args.length >= 2 ? this.args[1] : 100; + + + let [min,max] = [[this.min_elt,0],[this.max_elt,100]].map(([elt,def],i)=>elt? + + Number(elt.textContent) : + + this.args.length >= i+1 ? this.args[i] : def); + + this.range = [min, max, start, end]; @@ -2117,8 +2172,6 @@ - - class CircularSliderWidget extends Widget{ @@ -3835,10 +3888,10 @@ text box button highlight - // It is assumed that list content conforms to Array interface. - content: [ + /* TODO : Support HMI:List */ + " @@ -4638,142 +4691,146 @@ } - - - - - - active inactive - - - - - - - - - - disabled - - - + + class JumpWidget extends Widget{ + + + + activable = false; + + active = false; + + disabled = false; + + frequency = 2; + + + + update_activity() { + + if(this.active) { + + /* show active */ + + this.active_elt.setAttribute("style", this.active_elt_style); + + /* hide inactive */ + + this.inactive_elt.setAttribute("style", "display:none"); + + } else { + + /* show inactive */ + + this.inactive_elt.setAttribute("style", this.inactive_elt_style); + + /* hide active */ + + this.active_elt.setAttribute("style", "display:none"); + + } + + } + + + + make_on_click() { + + let that = this; + + const name = this.args[0]; + + return function(evt){ + + /* TODO: suport path pointing to local variable whom value + + would be an HMI_TREE index to jump to a relative page */ + + const index = that.indexes.length > 0 ? that.indexes[0] + that.offset : undefined; + + switch_page(name, index); + + } + + } + + + + notify_page_change(page_name, index) { + + if(this.activable) { + + const ref_index = this.indexes.length > 0 ? this.indexes[0] + this.offset : undefined; + + const ref_name = this.args[0]; + + this.active = ((ref_name == undefined || ref_name == page_name) && index == ref_index); + + this.update_activity(); + + } + + } + + + + dispatch(value) { + + this.disabled = !Number(value); + + if(this.disabled) { + + /* show disabled */ + + this.disabled_elt.setAttribute("style", this.disabled_elt_style); + + /* hide inactive */ + + this.inactive_elt.setAttribute("style", "display:none"); + + /* hide active */ + + this.active_elt.setAttribute("style", "display:none"); + + } else { + + /* hide disabled */ + + this.disabled_elt.setAttribute("style", "display:none"); + + this.update_activity(); + + } + + } + + } + - + + + active inactive + + - + + + disabled + + - - active: false, - - - disabled: false, - - frequency: 2, - - dispatch: function(value) { - - this.disabled = !Number(value); - - this.update(); - - }, - - - update: function(){ - - - if(this.disabled) { - - /* show disabled */ - - this.disabled_elt.setAttribute("style", this.active_elt_style); - - /* hide inactive */ - - this.inactive_elt.setAttribute("style", "display:none"); - - /* hide active */ - - this.active_elt.setAttribute("style", "display:none"); - - } else { - - /* hide disabled */ - - this.disabled_elt.setAttribute("style", "display:none"); - - - if(this.active) { - - /* show active */ - - this.active_elt.setAttribute("style", this.active_elt_style); - - /* hide inactive */ - - this.inactive_elt.setAttribute("style", "display:none"); - - } else { - - /* show inactive */ - - this.inactive_elt.setAttribute("style", this.inactive_elt_style); - - /* hide active */ - - this.active_elt.setAttribute("style", "display:none"); - - } - - - } - - - }, - - - - notify_page_change: function(page_name, index){ - - const ref_index = this.indexes.length > 0 ? this.indexes[0] + this.offset : undefined; - - const ref_name = this.args[0]; - - this.active =((ref_name == undefined || ref_name == page_name) && index == ref_index); - - this.update(); - - }, - - - make_on_click(){ - - let that = this; - - const name = this.args[0]; - - return function(evt){ - - const index = that.indexes.length > 0 ? that.indexes[0] + that.offset : undefined; - - switch_page(name, index); - - } - - }, - init: function() { this.element.onclick = this.make_on_click(); @@ -4783,6 +4840,8 @@ this.inactive_elt_style = this.inactive_elt.getAttribute("style"); + this.activable = true; + @@ -5360,13 +5419,23 @@ dispatch(value) { + this.display_val = value; + + this.request_animate(); + + } + + + + animate(){ + if(this.value_elt) - this.value_elt.textContent = String(value); + this.value_elt.textContent = String(this.display_val); let [min,max,totallength] = this.range; - let length = Math.max(0,Math.min(totallength,(Number(value)-min)*totallength/(max-min))); + let length = Math.max(0,Math.min(totallength,(Number(this.display_val)-min)*totallength/(max-min))); let tip = this.range_elt.getPointAtLength(length); @@ -5378,17 +5447,13 @@ init() { - let min = this.min_elt ? - - Number(this.min_elt.textContent) : - - this.args.length >= 1 ? this.args[0] : 0; - - let max = this.max_elt ? - - Number(this.max_elt.textContent) : - - this.args.length >= 2 ? this.args[1] : 100; + let [min,max] = [[this.min_elt,0],[this.max_elt,100]].map(([elt,def],i)=>elt? + + Number(elt.textContent) : + + this.args.length >= i+1 ? this.args[i] : def); + + this.range = [min, max, this.range_elt.getTotalLength()] @@ -5416,8 +5481,6 @@ - - class MultiStateWidget extends Widget{ @@ -6396,7 +6459,7 @@ - + diff -r 5d9ae04ee50f -r bd20f9112014 svghmi/i18n.py --- a/svghmi/i18n.py Mon Jan 18 10:32:13 2021 +0100 +++ b/svghmi/i18n.py Tue Jan 19 11:57:13 2021 +0100 @@ -1,4 +1,40 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# This file is part of Beremiz +# Copyright (C) 2021: Edouard TISSERANT +# +# See COPYING file for copyrights details. + +from __future__ import absolute_import +import sys +import subprocess import time +import wx + +def open_pofile(pofile): + """ Opens PO file with POEdit """ + + if sys.platform.startswith('win'): + from six.moves import winreg + poedit_cmd = None + try: + poedit_cmd = winreg.QueryValue(winreg.HKEY_LOCAL_MACHINE, + 'SOFTWARE\\Classes\\poedit\\shell\\open\\command') + poedit_path = poedit_cmd.replace('"%1"', '').strip().replace('"', '') + except OSError: + poedit_path = None + + else: + try: + poedit_path = subprocess.check_output("command -v poedit", shell=True).strip() + except subprocess.CalledProcessError: + poedit_path = None + + if poedit_path is None: + wx.MessageBox("POEdit is not found or installed !") + else: + subprocess.Popen([poedit_path,pofile]) locpfx = '#:svghmi.svg:' @@ -15,22 +51,71 @@ "Last-Translator: FULL NAME \\n" "Language-Team: LANGUAGE \\n" "MIME-Version: 1.0\\n" -"Content-Type: text/plain; charset=CHARSET\\n" -"Content-Transfer-Encoding: ENCODING\\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" "Generated-By: SVGHMI 1.0\\n" ''' +escapes = [] + +def make_escapes(pass_iso8859): + global escapes + escapes = [chr(i) for i in range(256)] + if pass_iso8859: + # Allow iso-8859 characters to pass through so that e.g. 'msgid + # "Höhe"' would result not result in 'msgid "H\366he"'. Otherwise we + # escape any character outside the 32..126 range. + mod = 128 + else: + mod = 256 + for i in range(mod): + if not(32 <= i <= 126): + escapes[i] = "\\%03o" % i + escapes[ord('\\')] = '\\\\' + escapes[ord('\t')] = '\\t' + escapes[ord('\r')] = '\\r' + escapes[ord('\n')] = '\\n' + escapes[ord('\"')] = '\\"' + +make_escapes(pass_iso8859 = True) + +EMPTYSTRING = '' + +def escape(s): + global escapes + s = list(s) + for i in range(len(s)): + s[i] = escapes[ord(s[i])] + return EMPTYSTRING.join(s) + +def normalize(s): + # This converts the various Python string types into a format that is + # appropriate for .po files, namely much closer to C style. + lines = s.split('\n') + if len(lines) == 1: + s = '"' + escape(s) + '"' + else: + if not lines[-1]: + del lines[-1] + lines[-1] = lines[-1] + '\n' + for i in range(len(lines)): + lines[i] = escape(lines[i]) + lineterm = '\\n"\n"' + s = '""\n"' + lineterm.join(lines) + '"' + return s + class POTWriter: def __init__(self): self.__messages = {} - def ImportMessages(self, msgs): + def ImportMessages(self, msgs): for msg in msgs: - self.addentry("\n".join([line.text for line in msg]), msg.get("label"), msg.get("id")) + self.addentry("\n".join([line.text.encode("utf-8") for line in msg]), msg.get("label"), msg.get("id")) def addentry(self, msg, label, svgid): entry = (label, svgid) + print(entry) self.__messages.setdefault(msg, set()).add(entry) def write(self, fp): @@ -47,12 +132,12 @@ rentries = reverse[rkey] rentries.sort() for k, v in rentries: - v = v.keys() + v = list(v) v.sort() locline = locpfx for label, svgid in v: d = {'label': label, 'svgid': svgid} - s = _(' %(label)s:%(svgid)d') % d + s = _(' %(label)s:%(svgid)s') % d if len(locline) + len(s) <= 78: locline = locline + s else: diff -r 5d9ae04ee50f -r bd20f9112014 svghmi/i18n.ysl2 --- a/svghmi/i18n.ysl2 Mon Jan 18 10:32:13 2021 +0100 +++ b/svghmi/i18n.ysl2 Tue Jan 19 11:57:13 2021 +0100 @@ -10,7 +10,7 @@ template "svg:text", mode="extract_i18n" { msg { attrib "id" value "@id"; - attrib "label" value "@inkscape:label"; + attrib "label" value "substring(@inkscape:label,2)"; apply "svg:*", mode="extract_i18n"; } } diff -r 5d9ae04ee50f -r bd20f9112014 svghmi/svghmi.py --- a/svghmi/svghmi.py Mon Jan 18 10:32:13 2021 +0100 +++ b/svghmi/svghmi.py Tue Jan 19 11:57:13 2021 +0100 @@ -30,7 +30,7 @@ import targets from editors.ConfTreeNodeEditor import ConfTreeNodeEditor from XSLTransform import XSLTransform -from svghmi.i18n import POTWriter, POReader +from svghmi.i18n import POTWriter, POReader, open_pofile HMI_TYPES_DESC = { "HMI_NODE":{}, @@ -455,19 +455,27 @@ "method": "_ImportSVG" }, { - "bitmap": "ImportSVG", # should be something different + "bitmap": "EditSVG", # should be something different "name": _("Inkscape"), "tooltip": _("Edit HMI"), "method": "_StartInkscape" }, - - # TODO : Launch POEdit button for new languqge (opens POT) - - # TODO : Launch POEdit button for existing languqge (opens one of existing PO) + { + "bitmap": "OpenPOT", # should be something different + "name": _("New lang"), + "tooltip": _("Open non translated message catalog (POT) to start new language"), + "method": "_OpenPOT" + }, + + { + "bitmap": "EditPO", # should be something different + "name": _("Edit lang"), + "tooltip": _("Edit existing message catalog (PO) for specific language"), + "method": "_EditPO" + }, # TODO : HMITree button # - can drag'n'drop variabes to Inkscape - ] def _getSVGpath(self, project_path=None): @@ -484,6 +492,9 @@ if from_project_path is not None: shutil.copyfile(self._getSVGpath(from_project_path), self._getSVGpath()) + shutil.copyfile(self._getPOTpath(from_project_path), + self._getPOTpath()) + # XXX TODO copy .PO files return True def GetSVGGeometry(self): @@ -522,8 +533,9 @@ w = POTWriter() w.ImportMessages(msgs) - # XXX get POT path - # XXX save POT file + + with open(self._getPOTpath(), 'w') as POT_file: + w.write(POT_file) # XXX scan existing PO files # XXX read PO files @@ -678,14 +690,24 @@ open_poedit = dialog.ShowModal() == wx.ID_YES dialog.Destroy() if open_poedit: - # XXX TODO - pass - - def _EditTranslation(self): + open_pofile(POFile) + + def _EditPO(self): """ Select a specific translation and edit it with POEdit """ - pass - - def _EditNewTranslation(self): + project_path = self.CTNPath() + dialog = wx.FileDialog(self.GetCTRoot().AppFrame, _("Choose a PO file"), project_path, "", _("PO files (*.po)|*.po"), wx.OPEN) + if dialog.ShowModal() == wx.ID_OK: + POFile = dialog.GetPath() + if os.path.isfile(POFile): + if os.path.dirname(POFile) == project_path: + self._StartPOEdit(POFile) + else: + self.GetCTRoot().logger.write_error(_("PO file misplaced: %s is not in %s\n") % (POFile,project_path)) + else: + self.GetCTRoot().logger.write_error(_("PO file do not exist: %s\n") % POFile) + dialog.Destroy() + + def _OpenPOT(self): """ Start POEdit with untouched empty catalog """ POFile = self._getPOTpath() self._StartPOEdit(POFile)