add some fixes for migrate groups (still some bugs left)

This commit is contained in:
leyghisbb 2020-08-30 12:30:35 +02:00
parent a3c7174c68
commit dfc7d70fb9
2 changed files with 53 additions and 43 deletions

View File

@ -29,11 +29,11 @@
<separator/>
<vbox>
<label appearance="header">Gradients</label>
<param name="lineargradient" type="bool" gui-text="lineargradient">true</param>
<param name="radialgradient" type="bool" gui-text="radialgradient">true</param>
<param name="meshgradient" type="bool" gui-text="meshgradient">true</param>
<param name="meshrow" type="bool" gui-text="meshrow">true</param>
<param name="meshpatch" type="bool" gui-text="meshpatch">true</param>
<param name="linearGradient" type="bool" gui-text="linearGradient">true</param>
<param name="radialGradient" type="bool" gui-text="radialGradient">true</param>
<param name="meshGradient" type="bool" gui-text="meshGradient">true</param>
<param name="meshRow" type="bool" gui-text="meshRow">true</param>
<param name="meshPatch" type="bool" gui-text="meshPatch">true</param>
<param name="stop" type="bool" gui-text="stop">true</param>
</vbox>
<separator/>
@ -67,4 +67,4 @@
<script>
<command location="inx" interpreter="python">fablabchemnitz_migrategroups.py</command>
</script>
</inkscape-extension>
</inkscape-extension>

View File

@ -8,7 +8,7 @@ This extension parses the selection and will put all elements into one single gr
Author: Mario Voigt / FabLab Chemnitz
Mail: mario.voigt@stadtfabrikanten.org
Date: 13.08.2020
Last Patch: 23.08.2020
Last Patch: 29.08.2020
License: GNU GPL v3
"""
@ -17,9 +17,9 @@ from lxml import etree
class MigrateGroups(inkex.Effect):
allElements = []
allGroups = []
allNonMigrates = []
allElements = [] #list of all (sub)elements to process within selection
allGroups = [] #list of all groups (svg:g and svg:svg items) to delete
allNonMigrates = [] #list of all other elements except svg:g and svg:svg to drop while migrating
def __init__(self):
inkex.Effect.__init__(self)
@ -40,11 +40,11 @@ class MigrateGroups(inkex.Effect):
self.arg_parser.add_argument("--svg", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--text", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--tspan", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--lineargradient", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--radialgradient", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--meshgradient", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--meshrow", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--meshpatch", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--linearGradient", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--radialGradient", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--meshGradient", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--meshRow", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--meshPatch", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--metadata", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--script", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--stop", type=inkex.Boolean, default=True)
@ -69,11 +69,11 @@ class MigrateGroups(inkex.Effect):
#namespace.append("{http://www.w3.org/2000/svg}svg") if self.options.svg else ""
namespace.append("{http://www.w3.org/2000/svg}text") if self.options.text else ""
namespace.append("{http://www.w3.org/2000/svg}tspan") if self.options.tspan else ""
namespace.append("{http://www.w3.org/2000/svg}lineargradient") if self.options.lineargradient else ""
namespace.append("{http://www.w3.org/2000/svg}radialgradient") if self.options.radialgradient else ""
namespace.append("{http://www.w3.org/2000/svg}meshgradient") if self.options.meshgradient else ""
namespace.append("{http://www.w3.org/2000/svg}meshrow") if self.options.meshrow else ""
namespace.append("{http://www.w3.org/2000/svg}meshpatch") if self.options.meshpatch else ""
namespace.append("{http://www.w3.org/2000/svg}linearGradient") if self.options.linearGradient else ""
namespace.append("{http://www.w3.org/2000/svg}radialGradient") if self.options.radialGradient else ""
namespace.append("{http://www.w3.org/2000/svg}meshGradient") if self.options.meshGradient else ""
namespace.append("{http://www.w3.org/2000/svg}meshRow") if self.options.meshRow else ""
namespace.append("{http://www.w3.org/2000/svg}meshPatch") if self.options.meshPatch else ""
namespace.append("{http://www.w3.org/2000/svg}script") if self.options.script else ""
namespace.append("{http://www.w3.org/2000/svg}metadata") if self.options.metadata else ""
namespace.append("{http://www.w3.org/2000/svg}stop") if self.options.stop else ""
@ -82,6 +82,8 @@ class MigrateGroups(inkex.Effect):
namespace.append("{http://www.w3.org/2000/svg}flowRegion") if self.options.flowRegion else ""
namespace.append("{http://www.w3.org/2000/svg}flowPara") if self.options.flowPara else ""
#inkex.utils.debug(namespace)
#check if we have selected elements or if we should parse the whole document instead
selected = [] #list of elements to parse
if len(self.svg.selected) == 0:
@ -89,19 +91,21 @@ class MigrateGroups(inkex.Effect):
if element != self.document.getroot():
selected.append(element)
else:
selected = self.svg.selected
selected = self.svg.get_selected()
def parseNodes(self, element):
#check the element for it's type and put it into the the according list (either re-group or delete)
def parseElement(self, element):
if self.options.allitems:
if element not in self.allElements:
if element.tag != inkex.addNS('g','svg') and element.tag != inkex.addNS('svg','svg') and element.tag != inkex.addNS('namedview','sodipodi'):
self.allElements.append(element)
else:
#inkex.utils.debug(element.tag)
if element.tag in namespace:
if element not in self.allElements:
self.allElements.append(element)
else:
#inkex.utils.debug(element.tag)
if element.tag != inkex.addNS('g','svg') and element.tag != inkex.addNS('svg','svg') and element.tag != inkex.addNS('namedview','sodipodi'):
if element not in self.allNonMigrates:
self.allNonMigrates.append(element)
@ -112,18 +116,17 @@ class MigrateGroups(inkex.Effect):
groups = element.getchildren()
if groups is not None:
for group in groups:
parseNodes(self, group)
parseElement(self, group)
if group not in self.allGroups:
self.allGroups.append(group)
#get all elements from the selection. Remove all groups from the selection and form a new single group of it. We also handle svg:svg because it behaves like a group container too
for element in selected:
parseNodes(self, element)
parseElement(self, element)
#copy all element into the new group
if len(self.allElements) > 0:
#copy all element into the new group
newGroup = self.document.getroot().add(inkex.Group()) #make a new group at root level
for oldElement in self.allElements:
#oldElementId = oldElement.get('id')
newElement = oldElement.copy()
@ -133,23 +136,30 @@ class MigrateGroups(inkex.Effect):
if oldElement.getparent() is not None:
oldElement.getparent().remove(oldElement)
if self.options.droponly == False:
#now remove all the obsolete groups
if len(self.allGroups) > 0:
for group in self.allGroups:
#if group.getparent() is not None:
group.getparent().remove(group)
#remove the selected, now empty group (if it's the case) - this applies not if there is no user selection at all so some dangling group(s) might be left over
if len(self.svg.selected) > 0 and len(self.allElements) > 0:
if self.svg.selected[0].tag == inkex.addNS('g','svg') or self.svg.selected[0].tag == inkex.addNS('svg','svg'):
if self.svg.selected[0].getparent() is not None:
self.svg.selected[0].getparent().remove(self.svg.selected[0])
#show a list with items to delete
if self.options.showdroplist:
self.msg(str(len(self.allNonMigrates)) + " elements were removed during nodes while migration:")
for i in self.allNonMigrates:
if i.get('id') is not None:
self.msg(i.tag.replace("{http://www.w3.org/2000/svg}","svg:") + " id:" + i.get('id'))
MigrateGroups().run()
self.msg(i.tag.replace("{http://www.w3.org/2000/svg}","svg:") + " id:" + i.get('id'))
# #now remove the stuff with nonMigrates list. this has to be done before we drop the groups where they are located
# if len(self.allNonMigrates) > 0:
# for nonMigrate in self.allNonMigrates:
# if nonMigrate.getparent() is not None:
# nonMigrate.getparent().remove(nonMigrate)
#now remove all the obsolete groups
if self.options.droponly == False:
if len(self.allGroups) > 0:
for group in self.allGroups:
if group.getparent() is not None:
group.getparent().remove(group)
#remove the selected, now empty group (if it's the case) - this applies not if there is no user selection at all so some dangling group(s) might be left over
if len(self.svg.selected) > 0 and len(self.allElements) > 0:
if self.svg.get_first_selected().tag == inkex.addNS('g','svg') or self.svg.get_first_selected().tag == inkex.addNS('svg','svg'):
if self.svg.get_first_selected().getparent() is not None:
self.svg.get_first_selected().getparent().remove(self.svg.get_first_selected())
MigrateGroups().run()