Skip to content

Commit 0f95684

Browse files
authored
add apply attribute extension (#3983)
1 parent a91c18c commit 0f95684

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed

icons/inx/apply_attribute.svg

Lines changed: 119 additions & 0 deletions
Loading

lib/extensions/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
55

66
from .about import About
7+
from .apply_attribute import ApplyAttribute
78
from .apply_palette import ApplyPalette
89
from .apply_threadlist import ApplyThreadlist
910
from .auto_run import AutoRun
@@ -82,6 +83,7 @@
8283

8384
extensions = [
8485
About,
86+
ApplyAttribute,
8587
ApplyPalette,
8688
ApplyThreadlist,
8789
AutoRun,

lib/extensions/apply_attribute.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Authors: see git history
2+
#
3+
# Copyright (c) 2025 Authors
4+
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
5+
6+
from inkex import Boolean, errormsg
7+
8+
from ..i18n import _
9+
from .base import InkstitchExtension
10+
11+
12+
class ApplyAttribute(InkstitchExtension):
13+
'''
14+
Applies a given attribute to all selected elements
15+
'''
16+
def __init__(self, *args, **kwargs):
17+
InkstitchExtension.__init__(self, *args, **kwargs)
18+
self.arg_parser.add_argument("--notebook")
19+
self.arg_parser.add_argument("-n", "--namespace", dest="namespace", type=str, default='inkstitch')
20+
self.arg_parser.add_argument("-k", "--key", dest="key", type=str, default='')
21+
self.arg_parser.add_argument("-v", "--value", dest="value", type=str, default='')
22+
self.arg_parser.add_argument("-r", "--remove", dest="remove", type=Boolean, default=False)
23+
24+
def effect(self):
25+
self.get_elements()
26+
if not self.elements:
27+
errormsg(_("Please select at least one element."))
28+
return
29+
30+
if not self.options.key:
31+
errormsg(_("Please enter the attribute name."))
32+
return
33+
34+
key = ''
35+
if self.options.namespace:
36+
key = f'{self.options.namespace}:'
37+
key += self.options.key
38+
39+
if self.options.remove:
40+
for element in self.elements:
41+
element.node.pop(key)
42+
else:
43+
if not self.options.value:
44+
errormsg(_("Please enter a value."))
45+
return
46+
for element in self.elements:
47+
element.node.set(key, self.options.value)

templates/apply_attribute.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<inkscape-extension translationdomain="inkstitch" xmlns="http://www.inkscape.org/namespace/inkscape/extension">
3+
<name>Apply attribute</name>
4+
<id>org.{{ id_inkstitch }}.apply_attribute</id>
5+
<param name="extension" type="string" gui-hidden="true">apply_attribute</param>
6+
<effect>
7+
<object-type>all</object-type>
8+
<icon>{{ icon_path }}inx/apply_attribute.svg</icon>
9+
<menu-tip>Applies a custom attribute to multiple elements</menu-tip>
10+
<effects-menu>
11+
<submenu name="{{ menu_inkstitch }}" translatable="no">
12+
<submenu name="Edit" />
13+
</submenu>
14+
</effects-menu>
15+
</effect>
16+
<param name="notebook" type="notebook">
17+
<page name="options" gui-text="Options">
18+
<param name="namespace" type="string" gui-text="Namespace">inkstitch</param>
19+
<param name="key" type="string" gui-text="Attribute"></param>
20+
<param name="value" type="string" gui-text="Value"></param>
21+
<spacer />
22+
<param name="remove" type="boolean" gui-text="Remove attribute">false</param>
23+
</page>
24+
<page name="info" gui-text="Help">
25+
<label appearance="header">This extension sets a custom attribute to all selected elements.</label>
26+
<spacer/>
27+
<label>It is an extension for experienced users.</label>
28+
<spacer/>
29+
<label appearance="url">https://inkstitch.org/docs/edit/#apply-attribute</label>
30+
</page>
31+
</param>
32+
<script>
33+
{{ command_tag | safe }}
34+
</script>
35+
</inkscape-extension>

0 commit comments

Comments
 (0)