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

from ..svg.tags import INKSCAPE_GROUPMODE, INKSTITCH_ATTRIBS, SVG_GROUP_TAG
from .base import InkstitchExtension


class StitchPlanPreviewUndo(InkstitchExtension):
    def effect(self):
        reset_stitch_plan(self.document.getroot())


def reset_stitch_plan(svg, delete_stitch_plan=True):
    # delete old stitch plan
    layer = svg.find(".//*[@id='__inkstitch_stitch_plan__']")
    # get previously invisible layers (they still should be hidden afterwards)
    if layer is not None:
        display_method = layer.get(INKSTITCH_ATTRIBS['layer_visibility'], 'unchanged')
        invisible_layers = layer.get(INKSTITCH_ATTRIBS['invisible_layers'], '').split(",")
        if delete_stitch_plan:
            layer.getparent().remove(layer)

        if display_method == "unchanged":
            return

        # if there are layers with reduced opacity, remove opacity attribute or unhide hidden layers
        for g in svg.findall(SVG_GROUP_TAG):
            style = g.specified_style()
            if (g.get(INKSCAPE_GROUPMODE) == "layer" and float(style.get('opacity', 1)) == 0.4 or style.get('display', 'inline') == 'none'):
                if g.get_id() in invisible_layers:
                    continue

                g.style['opacity'] = 1
                g.style['display'] = 'inline'