diff --git a/easymacro.py b/easymacro.py index 74a3aa1..2cd3309 100644 --- a/easymacro.py +++ b/easymacro.py @@ -316,7 +316,10 @@ def get_app_config(node_name: str, key: str=''): LANGUAGE = get_app_config('org.openoffice.Setup/L10N/', 'ooLocale') LANG = LANGUAGE.split('-')[0] -COUNTRY = LANGUAGE.split('-')[1] +try: + COUNTRY = LANGUAGE.split('-')[1] +except: + COUNTRY = '' LOCALE = Locale(LANG, COUNTRY, '') NAME = TITLE = get_app_config('org.openoffice.Setup/Product', 'ooName') VERSION = get_app_config('org.openoffice.Setup/Product','ooSetupVersion') @@ -2879,6 +2882,55 @@ class LOCalcRange(object): return +class LOWriterStyles(object): + + def __init__(self, styles): + self._styles = styles + + @property + def names(self): + return self._styles.ElementNames + + def __str__(self): + return '\n'.join(self.names) + + +class LOWriterStylesFamilies(object): + + def __init__(self, styles): + self._styles = styles + + def __getitem__(self, index): + styles = { + 'Character': 'CharacterStyles', + 'Paragraph': 'ParagraphStyles', + 'Page': 'PageStyles', + 'Frame': 'FrameStyles', + 'Numbering': 'NumberingStyles', + 'Table': 'TableStyles', + 'Cell': 'CellStyles', + } + name = styles.get(index, index) + return LOWriterStyles(self._styles[name]) + + def __iter__(self): + self._index = 0 + return self + + def __next__(self): + obj = LOWriterStyles(self._styles[self._index]) + self._index += 1 + return obj + # ~ raise StopIteration + + @property + def names(self): + return self._styles.ElementNames + + def __str__(self): + return '\n'.join(self.names) + + class LOWriterPageStyle(LOBaseObject): def __init__(self, obj): @@ -2949,6 +3001,17 @@ class LOWriterTextRange(object): def value(self, value): self.string = value + @property + def style(self): + return self.obj.ParaStyleName + @style.setter + def style(self, value): + self.obj.ParaStyleName = value + + @property + def is_paragraph(self): + return self._is_paragraph + @property def is_table(self): return self._is_table @@ -3144,6 +3207,10 @@ class LOWriter(LODocument): ps = self.obj.StyleFamilies['PageStyles'] return LOWriterPageStyles(ps) + @property + def styles(self): + return LOWriterStylesFamilies(self.obj.StyleFamilies) + @property def search_descriptor(self): return self.obj.createSearchDescriptor() diff --git a/files/ZazDoc_v0.1.0.oxt b/files/ZazDoc_v0.1.0.oxt index 0a1a299..12a330d 100644 Binary files a/files/ZazDoc_v0.1.0.oxt and b/files/ZazDoc_v0.1.0.oxt differ diff --git a/source/ZazDoc.py b/source/ZazDoc.py index 39f0646..8813ede 100644 --- a/source/ZazDoc.py +++ b/source/ZazDoc.py @@ -1,6 +1,7 @@ import uno import unohelper from com.sun.star.task import XJobExecutor +import main ID_EXTENSION = 'net.elmau.zaz.doc' @@ -12,8 +13,9 @@ class ZazDoc(unohelper.Base, XJobExecutor): def __init__(self, ctx): self.ctx = ctx - def trigger(self, args='pyUNO'): - print('Hello World', args) + def trigger(self, args): + main.ID_EXTENSION = ID_EXTENSION + main.run(args, __file__) return diff --git a/source/images/close.svg b/source/images/close.svg new file mode 100644 index 0000000..8b7bc47 --- /dev/null +++ b/source/images/close.svg @@ -0,0 +1,52 @@ + + + + + + image/svg+xml + + 9.4 + + + + + 9.4 + Created with Sketch. + + + + + + + + diff --git a/source/images/replace.svg b/source/images/replace.svg new file mode 100644 index 0000000..f211040 --- /dev/null +++ b/source/images/replace.svg @@ -0,0 +1,50 @@ + +image/svg+xml diff --git a/source/locales/en/LC_MESSAGES/base.po b/source/locales/en/LC_MESSAGES/base.po new file mode 100644 index 0000000..8c659b9 --- /dev/null +++ b/source/locales/en/LC_MESSAGES/base.po @@ -0,0 +1,46 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR ORGANIZATION +# FIRST AUTHOR , YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: \n" +"POT-Creation-Date: 2021-04-12 17:25-0500\n" +"PO-Revision-Date: 2021-04-12 17:27-0500\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: pygettext.py 1.5\n" +"X-Generator: Poedit 2.4.1\n" +"Last-Translator: \n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"Language: en\n" + +#: source/pythonpath/main.py:28 +msgid "Select style source" +msgstr "" + +#: source/pythonpath/main.py:32 +msgid "Select style target" +msgstr "" + +#: source/pythonpath/main.py:36 +msgid "Select different styles" +msgstr "" + +#: source/pythonpath/main.py:40 +msgid "Replace selected styles?" +msgstr "" + +#: source/pythonpath/main.py:62 +msgid "Replace Styles" +msgstr "" + +#: source/pythonpath/main.py:73 +msgid "~Replace by paragraph" +msgstr "" + +#: source/pythonpath/main.py:112 +msgid "~Close" +msgstr "" diff --git a/source/locales/en/LC_MESSAGES/base.po~ b/source/locales/en/LC_MESSAGES/base.po~ new file mode 100644 index 0000000..f20ad8b --- /dev/null +++ b/source/locales/en/LC_MESSAGES/base.po~ @@ -0,0 +1,46 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR ORGANIZATION +# FIRST AUTHOR , YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: \n" +"POT-Creation-Date: 2021-04-12 17:11-0500\n" +"PO-Revision-Date: 2021-04-12 17:15-0500\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: pygettext.py 1.5\n" +"X-Generator: Poedit 2.4.1\n" +"Last-Translator: \n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"Language: en\n" + +#: source/pythonpath/main.py:28 +msgid "Select style source" +msgstr "" + +#: source/pythonpath/main.py:32 +msgid "Select style target" +msgstr "" + +#: source/pythonpath/main.py:36 +msgid "Select different styles" +msgstr "" + +#: source/pythonpath/main.py:40 +msgid "Replace style \"%s\" with style \"s%\" ?" +msgstr "" + +#: source/pythonpath/main.py:62 +msgid "Replace Styles" +msgstr "" + +#: source/pythonpath/main.py:73 +msgid "~Replace by paragraph" +msgstr "" + +#: source/pythonpath/main.py:112 +msgid "~Close" +msgstr "" diff --git a/source/locales/es/LC_MESSAGES/base.po b/source/locales/es/LC_MESSAGES/base.po new file mode 100644 index 0000000..5109999 --- /dev/null +++ b/source/locales/es/LC_MESSAGES/base.po @@ -0,0 +1,49 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR ORGANIZATION +# FIRST AUTHOR , YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: \n" +"POT-Creation-Date: 2021-04-12 17:25-0500\n" +"PO-Revision-Date: 2021-04-12 17:26-0500\n" +"Last-Translator: \n" +"Language-Team: \n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: pygettext.py 1.5\n" +"X-Generator: Poedit 2.4.1\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#: source/pythonpath/main.py:28 +msgid "Select style source" +msgstr "Seleccionar estilo origen" + +#: source/pythonpath/main.py:32 +msgid "Select style target" +msgstr "Seleccionar estilo destino" + +#: source/pythonpath/main.py:36 +msgid "Select different styles" +msgstr "Seleccionar estilos diferentes" + +#: source/pythonpath/main.py:40 +msgid "Replace selected styles?" +msgstr "¿Reemplazar estilos seleccionados?" + +#: source/pythonpath/main.py:62 +msgid "Replace Styles" +msgstr "Reemplazar Estilos" + +#: source/pythonpath/main.py:73 +msgid "~Replace by paragraph" +msgstr "~Reemplazar por párrafo" + +#: source/pythonpath/main.py:112 +msgid "~Close" +msgstr "~Cerrar" + +#~ msgid "Replace style \"%s\" with style \"s%\" ?" +#~ msgstr "¿Reemplazar el estilo \"%s\" con el estilo \"s%\" ?" diff --git a/source/locales/es/LC_MESSAGES/base.po~ b/source/locales/es/LC_MESSAGES/base.po~ new file mode 100644 index 0000000..80c5cc3 --- /dev/null +++ b/source/locales/es/LC_MESSAGES/base.po~ @@ -0,0 +1,46 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR ORGANIZATION +# FIRST AUTHOR , YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: \n" +"POT-Creation-Date: 2021-04-12 17:11-0500\n" +"PO-Revision-Date: 2021-04-12 17:14-0500\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: pygettext.py 1.5\n" +"X-Generator: Poedit 2.4.1\n" +"Last-Translator: \n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"Language: es\n" + +#: source/pythonpath/main.py:28 +msgid "Select style source" +msgstr "Seleccionar estilo origen" + +#: source/pythonpath/main.py:32 +msgid "Select style target" +msgstr "Seleccionar estilo destino" + +#: source/pythonpath/main.py:36 +msgid "Select different styles" +msgstr "Seleccionar estilos diferentes" + +#: source/pythonpath/main.py:40 +msgid "Replace style \"%s\" with style \"s%\" ?" +msgstr "¿Reemplazar el estilo \"%s\" con el estilo \"s%\" ?" + +#: source/pythonpath/main.py:62 +msgid "Replace Styles" +msgstr "Reemplazar Estilos" + +#: source/pythonpath/main.py:73 +msgid "~Replace by paragraph" +msgstr "~Reemplazar por párrafo" + +#: source/pythonpath/main.py:112 +msgid "~Close" +msgstr "~Cerrar" diff --git a/source/pythonpath/easymacro.py b/source/pythonpath/easymacro.py index 74a3aa1..2cd3309 100644 --- a/source/pythonpath/easymacro.py +++ b/source/pythonpath/easymacro.py @@ -316,7 +316,10 @@ def get_app_config(node_name: str, key: str=''): LANGUAGE = get_app_config('org.openoffice.Setup/L10N/', 'ooLocale') LANG = LANGUAGE.split('-')[0] -COUNTRY = LANGUAGE.split('-')[1] +try: + COUNTRY = LANGUAGE.split('-')[1] +except: + COUNTRY = '' LOCALE = Locale(LANG, COUNTRY, '') NAME = TITLE = get_app_config('org.openoffice.Setup/Product', 'ooName') VERSION = get_app_config('org.openoffice.Setup/Product','ooSetupVersion') @@ -2879,6 +2882,55 @@ class LOCalcRange(object): return +class LOWriterStyles(object): + + def __init__(self, styles): + self._styles = styles + + @property + def names(self): + return self._styles.ElementNames + + def __str__(self): + return '\n'.join(self.names) + + +class LOWriterStylesFamilies(object): + + def __init__(self, styles): + self._styles = styles + + def __getitem__(self, index): + styles = { + 'Character': 'CharacterStyles', + 'Paragraph': 'ParagraphStyles', + 'Page': 'PageStyles', + 'Frame': 'FrameStyles', + 'Numbering': 'NumberingStyles', + 'Table': 'TableStyles', + 'Cell': 'CellStyles', + } + name = styles.get(index, index) + return LOWriterStyles(self._styles[name]) + + def __iter__(self): + self._index = 0 + return self + + def __next__(self): + obj = LOWriterStyles(self._styles[self._index]) + self._index += 1 + return obj + # ~ raise StopIteration + + @property + def names(self): + return self._styles.ElementNames + + def __str__(self): + return '\n'.join(self.names) + + class LOWriterPageStyle(LOBaseObject): def __init__(self, obj): @@ -2949,6 +3001,17 @@ class LOWriterTextRange(object): def value(self, value): self.string = value + @property + def style(self): + return self.obj.ParaStyleName + @style.setter + def style(self, value): + self.obj.ParaStyleName = value + + @property + def is_paragraph(self): + return self._is_paragraph + @property def is_table(self): return self._is_table @@ -3144,6 +3207,10 @@ class LOWriter(LODocument): ps = self.obj.StyleFamilies['PageStyles'] return LOWriterPageStyles(ps) + @property + def styles(self): + return LOWriterStylesFamilies(self.obj.StyleFamilies) + @property def search_descriptor(self): return self.obj.createSearchDescriptor() diff --git a/source/pythonpath/main.py b/source/pythonpath/main.py new file mode 100644 index 0000000..8418a5e --- /dev/null +++ b/source/pythonpath/main.py @@ -0,0 +1,146 @@ +#!/usr/bin/env python3 + +import easymacro as app + + +ID_EXTENSION = '' +_ = None + + + +def _replace_styles(): + dlg = _create_dialog_replace_styles() + dlg.open() + return + + +class Controllers(object): + + def __init__(self, dlg): + self.d = dlg + + @app.catch_exception + def cmd_replace_by_paragraph_action(self, event): + source = self.d.lst_paragraph_source.value + target = self.d.lst_paragraph_target.value + + if not source: + msg = _('Select style source') + app.errorbox(msg) + return + if not target: + msg = _('Select style target') + app.errorbox(msg) + return + if source == target: + msg = _('Select different styles') + app.errorbox(msg) + return + + msg = _('Replace selected styles?') + if not app.question(msg): + return + + doc = app.active + i = 0 + for p in doc.paragraphs: + if not p.is_paragraph: + continue + if p.style == source: + p.style = target + i += 1 + + self.d.close() + app.debug(f'{i} replaces') + return + + def cmd_close_action(self, event): + self.d.close() + return + + +def _create_dialog_replace_styles(): + args = { + 'Name': 'dialog', + 'Title': _('Replace Styles'), + 'Width': 220, + 'Height': 100, + } + dlg = app.create_dialog(args) + dlg.id = ID_EXTENSION + dlg.events = Controllers + + args = { + 'Type': 'Label', + 'Name': 'lbl_paragraphs', + 'Label': _('~Replace by paragraph'), + 'Width': 70, + 'Height': 15, + 'X': 10, + 'Y': 10, + } + dlg.add_control(args) + + args = { + 'Type': 'ListBox', + 'Name': 'lst_paragraph_source', + 'Width': 85, + 'Height': 15, + 'Dropdown': True, + } + dlg.add_control(args) + + args = { + 'Type': 'ListBox', + 'Name': 'lst_paragraph_target', + 'Width': 85, + 'Height': 15, + 'Dropdown': True, + } + dlg.add_control(args) + + args = { + 'Type': 'Button', + 'Name': 'cmd_replace_by_paragraph', + 'Width': 15, + 'Height': 15, + 'ImageURL': 'replace.svg', + 'ImagePosition': 1, + } + dlg.add_control(args) + + args = { + 'Type': 'Button', + 'Name': 'cmd_close', + 'Label': _('~Close'), + 'Width': 70, + 'Height': 20, + 'ImageURL': 'close.svg', + 'ImagePosition': 1, + } + dlg.add_control(args) + + dlg.lst_paragraph_source.move(dlg.lbl_paragraphs) + dlg.lst_paragraph_target.move(dlg.lst_paragraph_source, 5, 0) + dlg.cmd_replace_by_paragraph.move(dlg.lst_paragraph_target, 5, 0) + dlg.cmd_close.move(dlg.lst_paragraph_source, 0, 20) + dlg.cmd_close.center() + + doc = app.active + styles = doc.styles['Paragraph'].names + dlg.lst_paragraph_source.data = styles + dlg.lst_paragraph_target.data = styles + + return dlg + + +@app.catch_exception +def run(args, path_locales): + global _ + + _ = app.install_locales(path_locales) + + if args == 'replacestyles': + _replace_styles() + + return