Doc for menus

This commit is contained in:
El Mau 2022-03-06 22:19:26 -06:00
parent be2e994513
commit 8b20921c1c
7 changed files with 341 additions and 171 deletions

View File

@ -1,7 +0,0 @@
API
===
.. autosummary::
:toctree: generated
easymacro

View File

@ -273,6 +273,88 @@ For other applications: `writer`, `draw`, `impress`, `math`
Menus
-----
Add new
^^^^^^^
Insert new menu in Calc.
.. code-block:: python
menu_name = 'zaz.my.menu'
menu = {
'Label': 'My menu',
'CommandURL': menu_name,
'Submenu': [
{
'Label': 'Open Macros Dialog...',
'CommandURL': 'MacroDialog',
},
{
'Label': '-',
},
{
'Label': 'My macro',
'CommandURL': {'library': 'test', 'name': 'hello'},
},
{
'Label': 'Execute macro...',
'CommandURL': 'RunMacro',
'ShortCut': 'Shift+Ctrl+Alt+E',
},
]
}
menu_bar = app.menus['calc']
menu_bar.insert(menu)
Remove
^^^^^^
.. code-block:: python
menu_name = 'zaz.my.menu'
menu_bar = app.menus['calc']
menu_bar.remove(menu_name)
Insert in exists menu
^^^^^^^^^^^^^^^^^^^^^
Insert if not exists in menu Tools, after submenu Macros.
.. code-block:: python
menu = app.menus['calc']['tools']
menu_name = 'zaz.my.menu'
menu_new = {
'Label': 'My menu',
'CommandURL': menu_name,
'Submenu': [
{
'Label': 'Open Macros Dialog...',
'CommandURL': 'MacroDialog',
},
{
'Label': '-',
},
{
'Label': 'My macro',
'CommandURL': {'library': 'test', 'name': 'hello'},
},
{
'Label': 'Execute macro...',
'CommandURL': 'RunMacro',
'ShortCut': 'Shift+Ctrl+Alt+E',
},
]
}
if menu_name in menu:
menu.remove(menu_name)
else:
menu.insert(menu_new, '.uno:MacrosMenu')

View File

@ -1,7 +0,0 @@
API easymacro
=============
.. automodule:: easymacro
:members:
:undoc-members:
:show-inheritance:

View File

@ -1,80 +0,0 @@
easymacro
=========
.. automodule:: easymacro
.. rubric:: Functions
.. autosummary::
catch_exception
create_instance
data_to_dict
debug
dict_to_property
error
errorbox
get_app_config
info
mri
msgbox
question
render
run_in_thread
save_log
set_app_config
sleep
warning
.. rubric:: Classes
.. autosummary::
ClipBoard
Color
Config
Dates
Email
Hash
IOStream
Json
LODocBase
LODocCalc
LODocDraw
LODocDrawImpress
LODocIDE
LODocImpress
LODocMain
LODocMath
LODocWriter
LODocument
LODocuments
LOMain
LOServer
MBT
Macro
MessageBoxType
Paths
Shell
Timer
Url
classproperty

View File

@ -28,7 +28,6 @@ You can used **easymacro** with any extension or directly in your macros.
email
application
documents
easymacro
Indices and tables

View File

@ -1,7 +0,0 @@
source
======
.. toctree::
:maxdepth: 4
easymacro

View File

@ -2168,11 +2168,13 @@ class LOShortCuts(object):
return
class MenuDebug():
class LOMenuDebug():
"""Classe for debug info menu"""
@classmethod
def _get_info(cls, menu):
line = menu.get('CommandURL', '----------')
def _get_info(cls, menu, index):
"""Get every option menu"""
line = f"({index}) {menu.get('CommandURL', '----------')}"
submenu = menu.get('ItemDescriptorContainer', None)
if not submenu is None:
line += cls._get_submenus(submenu)
@ -2180,6 +2182,7 @@ class MenuDebug():
@classmethod
def _get_submenus(cls, menu, level=1):
"""Get submenus"""
line = ''
for i, v in enumerate(menu):
data = data_to_dict(v)
@ -2191,25 +2194,233 @@ class MenuDebug():
return line
def __call__(cls, menu):
for m in menu:
for i, m in enumerate(menu):
data = data_to_dict(m)
print(cls._get_info(data))
print(cls._get_info(data, i))
return
class LOMenuBase():
"""Classe base for menus"""
NODE = 'private:resource/menubar/menubar'
config = None
menus = None
app = ''
@classmethod
def _get_index(cls, parent: Any, name: Union[int, str]=''):
"""Get index menu from name
:param parent: Menu parent
:type parent: pyUno
:param name: Menu name for search if is str
:type name: int or str
:return: Index of menu
:rtype: int
"""
index = None
if isinstance(name, str) and name:
for i, m in enumerate(parent):
menu = data_to_dict(m)
if menu.get('CommandURL', '') == name:
index = i
break
elif isinstance(name, str):
index = len(parent) - 1
elif isinstance(name, int):
index = name
return index
@classmethod
def _get_command_url(cls, menu: dict):
"""Get url from command and set shortcut
:param menu: Menu data
:type menu: dict
:return: URL command
:rtype: str
"""
shortcut = menu.pop('ShortCut', '')
command = menu['CommandURL']
url = LOShortCuts.get_url_script(command)
if shortcut:
LOShortCuts(cls.app).set(shortcut, command)
return url
@classmethod
def _save(cls, parent: Any, menu: dict, index: int):
"""Insert menu
:param parent: Menu parent
:type parent: pyUno
:param menu: New menu data
:type menu: dict
:param index: Position to insert
:type index: int
"""
# ~ Some day
# ~ self._menus.insertByIndex(index, new_menu)
properties = dict_to_property(menu, True)
uno.invoke(parent, 'insertByIndex', (index, properties))
cls.config.replaceSettings(cls.NODE, cls.menus)
return
@classmethod
def _insert_submenu(cls, parent: Any, menus: list):
"""Insert submenus recursively
:param parent: Menu parent
:type parent: pyUno
:param menus: List of menus
:type menus: list
"""
for i, menu in enumerate(menus):
submenu = menu.pop('Submenu', False)
if submenu:
idc = cls.config.createSettings()
menu['ItemDescriptorContainer'] = idc
menu['Type'] = 0
if menu['Label'][0] == '-':
menu['Type'] = 1
else:
menu['CommandURL'] = cls._get_command_url(menu)
cls._save(parent, menu, i)
if submenu:
cls._insert_submenu(idc, submenu)
return
@classmethod
def _get_first_command(cls, command):
url = command
if isinstance(command, dict):
url = Macro.get_url_script(command)
return url
@classmethod
def insert(cls, parent: Any, menu: dict, after: Union[int, str]=''):
"""Insert new menu
:param parent: Menu parent
:type parent: pyUno
:param menu: New menu data
:type menu: dict
:param after: After menu insert
:type after: int or str
"""
index = cls._get_index(parent, after) + 1
submenu = menu.pop('Submenu', False)
menu['Type'] = 0
idc = cls.config.createSettings()
menu['ItemDescriptorContainer'] = idc
menu['CommandURL'] = cls._get_first_command(menu['CommandURL'])
cls._save(parent, menu, index)
if submenu:
cls._insert_submenu(idc, submenu)
return
@classmethod
def remove(cls, parent: Any, name: Union[str, dict]):
"""Remove name in parent
:param parent: Menu parent
:type parent: pyUno
:param menu: Menu name
:type menu: str
"""
if isinstance(name, dict):
name = Macro.get_url_script(name)
index = cls._get_index(parent, name)
if index is None:
debug(f'Not found: {name}')
return
uno.invoke(parent, 'removeByIndex', (index,))
cls.config.replaceSettings(cls.NODE, cls.menus)
cls.config.store()
return
class LOMenu(object):
"""Classe for individual menu"""
def __init__(self, app, menu, config):
self._app = app
self._menu = menu
def __init__(self, config: Any, menus: Any, app: str, menu: Any):
"""
:param config: Configuration Mananer
:type config: pyUno
:param menus: Menu bar main
:type menus: pyUno
:param app: Name LibreOffice module
:type app: str
:para menu: Particular menu
:type menu: pyUno
"""
self._config = config
self._menus = menus
self._app = app
self._parent = menu
def __contains__(self, name):
"""If exists name in menu"""
exists = False
for m in self._parent:
menu = data_to_dict(m)
cmd = menu.get('CommandURL', '')
if name == cmd:
exists = True
break
return exists
def __getitem__(self, index):
"""Index access"""
if isinstance(index, int):
menu = data_to_dict(self._parent[index])
else:
for m in self._parent:
menu = data_to_dict(m)
cmd = menu.get('CommandURL', '')
if cmd == index:
break
obj = LOMenu(self._config, self._menus, self._app,
menu['ItemDescriptorContainer'])
return obj
def debug(self):
MenuDebug()(self._menu)
"""Debug menu"""
LOMenuDebug()(self._parent)
return
def insert(self, menu: dict, after: Union[int, str]='', save: bool=True):
"""Insert new menu
:param menu: New menu data
:type menu: dict
:param after: Insert in after menu
:type after: int or str
:param save: For persistente save
:type save: bool
"""
LOMenuBase.config = self._config
LOMenuBase.menus = self._menus
LOMenuBase.app = self._app
LOMenuBase.insert(self._parent, menu, after)
if save:
self._config.store()
return
def remove(self, menu: str):
"""Remove menu
:param menu: Menu name
:type menu: str
"""
LOMenuBase.config = self._config
LOMenuBase.menus = self._menus
LOMenuBase.remove(self._parent, menu)
return
class LOMenuApp(object):
"""Classe for manager menu by LibreOffice module"""
NODE = 'private:resource/menubar/menubar'
MENUS = {
'file': '.uno:PickList',
@ -2233,12 +2444,17 @@ class LOMenuApp(object):
'slideshow': '.uno:SlideShowMenu',
}
def __init__(self, app):
def __init__(self, app: str):
"""
:param app: LibreOffice Module: calc, writer, draw, impress, math, main
:type app: str
"""
self._app = app
self._config = self._get_config()
self._menus = self._config.getSettings(self.NODE, True)
def _get_config(self):
"""Get config manager"""
service = 'com.sun.star.ui.ModuleUIConfigurationManagerSupplier'
type_app = LODocuments.TYPES[self._app]
manager = create_instance(service, True)
@ -2246,10 +2462,12 @@ class LOMenuApp(object):
return config
def debug(self):
MenuDebug()(self._menus)
"""Debug menu"""
LOMenuDebug()(self._menus)
return
def __contains__(self, name):
"""If exists name in menu"""
exists = False
for m in self._menus:
menu = data_to_dict(m)
@ -2260,83 +2478,55 @@ class LOMenuApp(object):
return exists
def __getitem__(self, index):
"""Index access"""
if isinstance(index, int):
menu = data_to_dict(self._menus[index])
else:
for m in self._menus:
menu = data_to_dict(m)
cmd = menu.get('CommandURL', '')
if self.MENUS[index.lower()] == cmd:
if cmd == index or cmd == self.MENUS[index.lower()]:
break
obj = LOMenu(self._app, menu['ItemDescriptorContainer'], self._config)
obj = LOMenu(self._config, self._menus, self._app,
menu['ItemDescriptorContainer'])
return obj
@property
def count(self):
return len(self._menus)
def _get_index(self, name):
index = -1
for i, m in enumerate(self._menus):
menu = data_to_dict(m)
if menu['CommandURL'] == self.MENUS[name]:
index = i
break
return index
def _get_command_url(self, menu: dict):
shortcut = menu.pop('ShortCut', '')
command = menu['CommandURL']
url = LOShortCuts.get_url_script(command)
if shortcut:
LOShortCuts(self._app).set(shortcut, command)
return url
def _insert_submenu(self, parent: Any, menus: list):
for i, menu in enumerate(menus):
submenu = menu.pop('Submenu', False)
if submenu:
idc = self._config.createSettings()
menu['ItemDescriptorContainer'] = idc
menu['Type'] = 0
if menu['Label'][0] == '-':
menu['Type'] = 1
else:
menu['CommandURL'] = self._get_command_url(menu)
self._save(parent, menu, i)
if submenu:
self._insert_submenu(idc, submenu)
return
def _save(self, parent: Any, menu: dict, index: int):
# ~ Some day
# ~ self._menus.insertByIndex(index, new_menu)
properties = dict_to_property(menu, True)
uno.invoke(parent, 'insertByIndex', (index, properties))
self._config.replaceSettings(self.NODE, self._menus)
return
def insert(self, menu, after: str='', save: bool=True):
if after:
index = self._get_index(after) + 1
else:
index = self.count
submenu = menu.pop('Submenu', False)
menu['Type'] = menu.get('Type', 0)
menu['ItemDescriptorContainer'] = self._config.createSettings()
self._save(self._menus, menu, index)
self._insert_submenu(menu['ItemDescriptorContainer'], submenu)
def insert(self, menu: dict, after: Union[int, str]='', save: bool=True):
"""Insert new menu
:param menu: New menu data
:type menu: dict
:param after: Insert in after menu
:type after: int or str
:param save: For persistente save
:type save: bool
"""
LOMenuBase.config = self._config
LOMenuBase.menus = self._menus
LOMenuBase.app = self._app
LOMenuBase.insert(self._menus, menu, after)
if save:
self._config.store()
return
def remove(self, menu: str):
"""Remove menu
:param menu: Menu name
:type menu: str
"""
LOMenuBase.config = self._config
LOMenuBase.menus = self._menus
LOMenuBase.remove(self._menus, menu)
return
class LOMenus(object):
"""Classe for manager menus"""
def __getitem__(self, index):
"""Index access"""
return LOMenuApp(index)