summaryrefslogtreecommitdiff
path: root/lib/extensions/apply_satin_pattern.py
blob: 9da81075243cf85341b13647b5d284800de5fd64 (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
# Authors: see git history
#
# Copyright (c) 2021 Authors
# Licensed under the GNU GPL version 3.0 or later.  See the file LICENSE for details.

import inkex

from ..i18n import _
from ..svg.tags import INKSTITCH_ATTRIBS
from .base import InkstitchExtension
from ..elements import SatinColumn


class ApplySatinPattern(InkstitchExtension):
    # Add inkstitch:pattern attribute to selected patterns. The patterns will be projected on a satin column, which must be in the selection too

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

        if not self.svg.selected or not any(isinstance(item, SatinColumn) for item in self.elements) or len(self.svg.selected) < 2:
            inkex.errormsg(_("Please select at least one satin column and a pattern."))
            return

        if sum(isinstance(item, SatinColumn) for item in self.elements) > 1:
            inkex.errormsg(_("Please select only one satin column."))
            return

        satin_id = self.get_satin_column().node.get('id', None)
        patterns = self.get_patterns()

        for pattern in patterns:
            pattern.node.set(INKSTITCH_ATTRIBS['pattern'], satin_id)

    def get_satin_column(self):
        return list(filter(lambda satin: isinstance(satin, SatinColumn), self.elements))[0]

    def get_patterns(self):
        return list(filter(lambda satin: not isinstance(satin, SatinColumn), self.elements))