2021-03-12 04:17:19 +00:00
|
|
|
# Authors: see git history
|
|
|
|
#
|
|
|
|
# Copyright (c) 2010 Authors
|
|
|
|
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
|
|
|
|
|
2024-07-03 02:29:29 +00:00
|
|
|
from inkex import errormsg
|
2018-08-17 02:50:34 +00:00
|
|
|
|
2019-02-23 03:07:15 +00:00
|
|
|
from ..commands import OBJECT_COMMANDS, add_commands
|
2024-07-03 02:29:29 +00:00
|
|
|
from ..elements import Clone
|
2018-08-17 02:50:34 +00:00
|
|
|
from ..i18n import _
|
2019-02-23 03:07:15 +00:00
|
|
|
from .commands import CommandsExtension
|
2018-08-17 02:50:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ObjectCommands(CommandsExtension):
|
2018-08-18 00:55:43 +00:00
|
|
|
COMMANDS = OBJECT_COMMANDS
|
2018-08-17 02:50:34 +00:00
|
|
|
|
|
|
|
def effect(self):
|
|
|
|
if not self.get_elements():
|
|
|
|
return
|
|
|
|
|
2022-04-24 06:27:42 +00:00
|
|
|
if not self.svg.selection:
|
2024-07-03 02:29:29 +00:00
|
|
|
errormsg(_("Please select one or more objects to which to attach commands."))
|
2018-08-17 02:50:34 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self.svg = self.document.getroot()
|
|
|
|
|
|
|
|
commands = [command for command in self.COMMANDS if getattr(self.options, command)]
|
|
|
|
|
|
|
|
if not commands:
|
2024-07-03 02:29:29 +00:00
|
|
|
errormsg(_("Please choose one or more commands to attach."))
|
2018-08-17 02:50:34 +00:00
|
|
|
return
|
|
|
|
|
2024-07-03 02:29:29 +00:00
|
|
|
# Clones currently can't really take any commands, so error if we try to add one to them.
|
|
|
|
clones = [e for e in self.elements if isinstance(e, Clone)]
|
|
|
|
if clones:
|
|
|
|
errormsg(_(
|
|
|
|
"Cannot attach commands to Clone element(s) {clones}. "
|
|
|
|
"They must be unlinked to add commands.\n"
|
|
|
|
"* Select the clone(s)\n"
|
|
|
|
"* Run: Extensions > Ink/Stitch > Edit > Unlink Clone"
|
|
|
|
).format(clones=", ".join(c.node.get_id() for c in clones)))
|
|
|
|
|
2018-08-17 02:50:34 +00:00
|
|
|
# Each object (node) in the SVG may correspond to multiple Elements of different
|
|
|
|
# types (e.g. stroke + fill). We only want to process each one once.
|
|
|
|
seen_nodes = set()
|
|
|
|
|
|
|
|
for element in self.elements:
|
2024-07-03 02:29:29 +00:00
|
|
|
if element.node not in seen_nodes and element.shape and not isinstance(element, Clone):
|
2019-02-23 03:07:15 +00:00
|
|
|
add_commands(element, commands)
|
2018-08-17 02:50:34 +00:00
|
|
|
seen_nodes.add(element.node)
|