diff options
Diffstat (limited to 'lib/extensions')
| -rw-r--r-- | lib/extensions/__init__.py | 2 | ||||
| -rw-r--r-- | lib/extensions/tartan.py | 78 |
2 files changed, 80 insertions, 0 deletions
diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py index 72cafcc0..0ae88c76 100644 --- a/lib/extensions/__init__.py +++ b/lib/extensions/__init__.py @@ -54,6 +54,7 @@ from .simulator import Simulator from .stitch_plan_preview import StitchPlanPreview from .stitch_plan_preview_undo import StitchPlanPreviewUndo from .stroke_to_lpe_satin import StrokeToLpeSatin +from .tartan import Tartan from .test_swatches import TestSwatches from .troubleshoot import Troubleshoot from .update_svg import UpdateSvg @@ -111,6 +112,7 @@ __all__ = extensions = [ApplyPalette, StitchPlanPreview, StitchPlanPreviewUndo, StrokeToLpeSatin, + Tartan, TestSwatches, Troubleshoot, UpdateSvg, diff --git a/lib/extensions/tartan.py b/lib/extensions/tartan.py new file mode 100644 index 00000000..a9b34dfb --- /dev/null +++ b/lib/extensions/tartan.py @@ -0,0 +1,78 @@ +# Authors: see git history +# +# Copyright (c) 2023 Authors +# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. + +import sys + +import wx +import wx.adv +from inkex import errormsg + +from ..gui.simulator import SplitSimulatorWindow +from ..gui.tartan import TartanMainPanel +from ..i18n import _ +from ..svg.tags import EMBROIDERABLE_TAGS, INKSTITCH_TARTAN, SVG_GROUP_TAG +from .base import InkstitchExtension + + +class Tartan(InkstitchExtension): + def __init__(self, *args, **kwargs): + self.elements = set() + self.cancelled = False + InkstitchExtension.__init__(self, *args, **kwargs) + + def cancel(self): + self.cancelled = True + + def get_tartan_elements(self): + if self.svg.selection: + self._get_elements() + + def _get_elements(self): + for node in self.svg.selection: + node = self.get_outline(node) + if node.style('fill'): + self.elements.add(node) + + def get_outline(self, node): + # existing tartans are marked through their outline element + # we have either selected the element itself or some other element within a tartan group + if node.get(INKSTITCH_TARTAN, None): + return node + if node.get_id().startswith('inkstitch-tartan'): + for element in node.iterchildren(EMBROIDERABLE_TAGS): + if element.get(INKSTITCH_TARTAN, None): + return element + for group in node.iterancestors(SVG_GROUP_TAG): + if group.get_id().startswith('inkstitch-tartan'): + for element in group.iterchildren(EMBROIDERABLE_TAGS): + if element.get(INKSTITCH_TARTAN, None): + return element + # if we don't find an existing tartan, return node + return node + + def effect(self): + self.get_tartan_elements() + if not self.elements: + errormsg(_("To create a tartan pattern please select at least one element with a fill color.")) + return + metadata = self.get_inkstitch_metadata() + + app = wx.App() + frame = SplitSimulatorWindow( + title=_("Ink/Stitch Tartan"), + panel_class=TartanMainPanel, + elements=list(self.elements), + on_cancel=self.cancel, + metadata=metadata, + target_duration=1 + ) + + frame.Show() + app.MainLoop() + + if self.cancelled: + # This prevents the superclass from outputting the SVG, because we + # may have modified the DOM. + sys.exit(0) |
