summaryrefslogtreecommitdiff
path: root/lib/extensions/auto_satin.py
blob: 62fb15af7c5a5669dae01408d6998adb620efa0b (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Authors: see git history
#
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later.  See the file LICENSE for details.

import sys

import inkex

from ..elements import SatinColumn, Stroke
from ..i18n import _
from ..stitches.auto_satin import auto_satin
from .commands import CommandsExtension


class AutoSatin(CommandsExtension):
    COMMANDS = ["trim"]

    def __init__(self, *args, **kwargs):
        CommandsExtension.__init__(self, *args, **kwargs)

        self.arg_parser.add_argument("-p", "--preserve_order", dest="preserve_order", type=inkex.Boolean, default=False)

    def get_starting_point(self):
        return self.get_point("satin_start")

    def get_ending_point(self):
        return self.get_point("satin_end")

    def get_point(self, command_type):
        command = None
        for satin in self.elements:
            this_command = satin.get_command(command_type)
            if command is not None and this_command:
                inkex.errormsg(_("Please ensure that at most one start and end command is attached to the selected satin columns."))
                sys.exit(0)
            elif this_command:
                command = this_command

        if command is not None:
            return command.target_point

    def check_selection(self):
        if not self.get_elements():
            return

        if not self.svg.selected:
            # L10N auto-route satin columns extension
            inkex.errormsg(_("Please select one or more satin columns."))
            return False

        satincolumns = [element for element in self.elements if isinstance(element, SatinColumn)]
        if len(satincolumns) == 0:
            inkex.errormsg(_("Please select at least one satin column."))
            return False

        return True

    def effect(self):
        if not self.check_selection():
            return

        starting_point = self.get_starting_point()
        ending_point = self.get_ending_point()

        # Ignore fills
        elements = [element for element in self.elements if isinstance(element, SatinColumn) or isinstance(element, Stroke)]

        auto_satin(elements, self.options.preserve_order, starting_point, ending_point, self.options.trim)