summaryrefslogtreecommitdiff
path: root/lib/extensions/layer_commands.py
blob: 576af044bbe3a2d8e021f02123e301d894f76c3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import sys
import inkex

from .commands import CommandsExtension
from ..commands import LAYER_COMMANDS, get_command_description
from ..i18n import _
from ..svg.tags import *
from ..svg import get_correction_transform


class LayerCommands(CommandsExtension):
    COMMANDS = LAYER_COMMANDS

    def ensure_current_layer(self):
        # if no layer is selected, inkex defaults to the root, which isn't
        # particularly useful
        if self.current_layer is self.document.getroot():
            try:
                self.current_layer = self.document.xpath(".//svg:g[@inkscape:groupmode='layer']", namespaces=inkex.NSS)[0]
            except IndexError:
                # No layers at all??  Fine, we'll stick with the default.
                pass

    def effect(self):
        commands = [command for command in self.COMMANDS if getattr(self.options, command)]

        if not commands:
            inkex.errormsg(_("Please choose one or more commands to add."))
            return

        self.ensure_current_layer()
        correction_transform = get_correction_transform(self.current_layer, child=True)

        for i, command in enumerate(commands):
            self.ensure_symbol(command)

            node = inkex.etree.SubElement(self.current_layer, SVG_USE_TAG,
                {
                    "id": self.uniqueId("use"),
                    INKSCAPE_LABEL: _("Ink/Stitch Command") + ": %s" % get_command_description(command),
                    XLINK_HREF: "#inkstitch_%s" % command,
                    "height": "100%",
                    "width": "100%",
                    "x": str(i * 20),
                    "y": "-10",
                    "transform": correction_transform
                })

        namedview = self.document.xpath("//sodipodi:namedview", namespaces=inkex.NSS)