Class MACreator

class ooodev.gui.menu.ma.MACreator(lo_inst=None, **kwargs)[source]

Bases: LoInstPropsPartial, EventsPartial, JsonEncoder

Class for creating context action menu.

This class can also be used to convert menu data to a format that can be used with JSON.

Example

# ...
menus = get_action_menu()
json_str = json.dumps(menus, cls=ContextCreator, indent=4)
with open("action_menu.json", "w") as f:
    f.write(json_str)
Parameters:

lo_inst (LoInst | None) –

__init__(lo_inst=None, **kwargs)[source]

Constructor

Parameters:
  • lo_inst (LoInst | None, optional) – LibreOffice instance. Defaults to None.

  • kwargs (Any, optional) – Additional keyword arguments. (Used by JsonEncoder)

Return type:

None

add_event_observers(*args)

Adds observers that gets their trigger method called when this class trigger method is called.

Parameters:

args (EventObserver) – One or more observers to add.

Return type:

None

Note

Observers are removed automatically when they are out of scope.

create(menus)[source]

Create popup menu.

Parameters:

menus (List[Dict[str, Any]]) – Menu Data.

Return type:

List[Dict[str, Any]]

default(o)

JsonEncoder default method.

Parameters:

o (Any) – Data to be encoded.

Returns:

Encoded data.

Return type:

Any

Note

This method or the on_json_encode() can be overridden by subclasses to encode objects to JSON. If this class is a subclass of EventsPartial, the json_encoding event is triggered before encoding. The event data is a dictionary with the key obj containing the object to be encoded. If the event data result key is set then the value is returned as the result of the encoding.

encode(o)

Return a JSON string representation of a Python data structure.

>>> from json.encoder import JSONEncoder
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
'{"foo": ["bar", "baz"]}'
get_json_dict(menus)[source]

Gets a dictionary that can be converted to JSON. This is an alternative to created json data. This is a more standard way that would not require this library to decode.

The dictionary created by this method can also be used with the create method to create an Action Item menu.

The menu dictionaries can have data such as {"command": ".uno:Cut", "module": ModuleNamesKind.SPREADSHEET_DOCUMENT}. For json this sort of data does not work, this this method converts all menu data to a standard format that can be used with json.

Parameters:

menus (List[Dict[str, Any]]) – Action Item Menu Data. This is can be the same data used to create an Action Item menu.

Return type:

List[Dict[str, Any]]

Note

Even though menu data such as {"command": ".uno:Cut", "module": ModuleNamesKind.SPREADSHEET_DOCUMENT} is not valid for json. It can still be encoded using this ContextCreator class.

Example:
# ...
menus = get_action_menu()
json_str = json.dumps(menus, cls=ContextCreator, indent=4)
with open("action_menu.json", "w") as f:
    f.write(json_str)
Return type:

List[Dict[str, Any]]

Parameters:

menus (List[Dict[str, Any]]) –

iterencode(o, _one_shot=False)

Encode the given object and yield each string representation as available.

For example:

for chunk in JSONEncoder().iterencode(bigobject):
    mysocket.write(chunk)
json_dump(file, menus, dynamic=False)[source]

Dump JSON data to file.

Parameters:
  • file (Any) – File path.

  • menus (List[Dict[str, Any]]) – Menu Data.

  • dynamic (bool, optional) – Dynamic data. Defaults to False.

Return type:

None

json_dumps(menus, dynamic=False)[source]

Get JSON data.

Parameters:
  • menus (List[Dict[str, Any]]) – Menu Data.

  • dynamic (bool) –

Returns:

JSON data.

Return type:

str

static json_load(json_file, **kwargs)[source]

Load JSON data from file.

Parameters:

json_file (str) – JSON file path.

Returns:

Menu Data.

Return type:

List[Dict[str, Any]]

static json_loads(json_str, **kwargs)[source]

Load JSON data.

Parameters:

json_str (str) – JSON data.

Returns:

Menu Data.

Return type:

List[Dict[str, Any]]

on_json_encode(obj)[source]

Protected method to encode object to JSON. Can be overridden by subclasses.

Parameters:

obj (Any) – Object to encode. This is the object that json is currently encoding.

Returns:

The result of the encoding. The default is NULL_OBJ which means that the encoding is not handled.

Return type:

Any

remove_event_observer(observer)

Removes an observer

Parameters:

observer (EventObserver) – One or more observers to add.

Returns:

True if observer has been removed; Otherwise, False.

Return type:

bool

subscribe_action_container_created(callback)[source]

Subscribe on Action Container created event.

The callback event_data is a dictionary with keys:

  • container: ActionTriggerContainer instance

Return type:

None

Parameters:

callback (Callable[[Any, EventArgs], None]) –

subscribe_after_process(callback)[source]

Subscribe on after process event.

The callback event_data is a dictionary with keys:

  • container: ActionTriggerContainer instance

  • action_item: Action Item instance

Return type:

None

Parameters:

callback (Callable[[Any, EventArgs], None]) –

subscribe_before_process(callback)[source]

Subscribe on before process event.

The callback event_data is a dictionary with keys:

  • container: ActionTriggerContainer instance

  • action_item: Action Item instance

Return type:

None

Parameters:

callback (Callable[[Any, CancelEventArgs], None]) –

subscribe_event(event_name, callback)

Add an event listener to current instance.

Parameters:
  • event_name (str) – Event Name.

  • callback (EventCallback) – Callback of the event listener.

Return type:

None

subscribe_module_no_text(callback)[source]

Subscribe on no text found for module menu entry.

This event occurs when a module menu entry is created using the module key and the text for the menu entry is not found. This event will not be raised if the module entry also provides a text key. A text key can be provided to the module entry to provide a valid menu text as a replacement if not found.

The callback event_data is a dictionary with keys:

  • module_kind: ModuleNamesKind

  • cmd: Command as a string.

  • index: Index as an integer.

  • menu: Menu Data as a dictionary.

The caller can set menu["text"] to provide a valid menu text. If the caller cancels the event then the menu item is not created.

Return type:

None

Parameters:

callback (Callable[[Any, CancelEventArgs], None]) –

trigger_event(event_name, event_args)

Trigger an event on current instance.

Parameters:
  • event_name (str) – Event Name.

  • event_args (EventArgsT) – Event Args.

Return type:

None

unsubscribe_action_container_created(callback)[source]

Unsubscribe on popup created event.

Return type:

None

Parameters:

callback (Callable[[Any, EventArgs], None]) –

unsubscribe_after_process(callback)[source]

Unsubscribe on after process event.

Return type:

None

Parameters:

callback (Callable[[Any, EventArgs], None]) –

unsubscribe_before_process(callback)[source]

Unsubscribe on before process event.

Return type:

None

Parameters:

callback (Callable[[Any, CancelEventArgs], None]) –

unsubscribe_event(event_name, callback)

Remove an event listener from current instance.

Parameters:
  • event_name (str) – Event Name.

  • callback (EventCallback) – Callback of the event listener.

Return type:

None

unsubscribe_module_no_text(callback)[source]

Unsubscribe on no text found for module menu entry.

Return type:

None

Parameters:

callback (Callable[[Any, CancelEventArgs], None]) –

property event_observer: EventObserver

Gets/Sets The Event Observer for this instance.

Return type:

EventObserver

item_separator = ', '
property key_lookups: Dict[str, str]

Get key lookups.

Returns:

Key lookups.

Return type:

Dict[str, Any]

key_separator = ': '
property lo_inst: LoInst

Lo Instance

Return type:

LoInst