1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2010 Aurelio A. Heckert, aurium (a) gmail dot com
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 '''
20 import inkex
21 import gettext
23 _ = gettext.gettext
25 def is_empty(val):
26 if val is None:
27 return True
28 else:
29 return len(str(val)) == 0
31 class WebSlicer_CreateGroup(inkex.Effect):
33 def __init__(self):
34 inkex.Effect.__init__(self)
35 self.OptionParser.add_option("--html-id",
36 action="store", type="string",
37 dest="html_id",
38 help="")
39 self.OptionParser.add_option("--html-class",
40 action="store", type="string",
41 dest="html_class",
42 help="")
43 self.OptionParser.add_option("--width-unity",
44 action="store", type="string",
45 dest="width_unity",
46 help="")
47 self.OptionParser.add_option("--height-unity",
48 action="store", type="string",
49 dest="height_unity",
50 help="")
52 def effect(self):
53 layer = self.document.xpath(
54 '//*[@id="webslicer-layer" and @inkscape:groupmode="layer"]',
55 namespaces=inkex.NSS)[0]
56 if len(self.selected) == 0:
57 inkex.errormsg(_('You must to select some "Slicer rectangles" or other "Layout groups".'))
58 exit(1)
59 descendants = get_descendants_in_array(layer)
60 for id,node in self.selected.iteritems():
61 if node not in descendants:
62 inkex.errormsg(_('Opss... The element "%s" is not in the Web Slicer layer') % id)
63 exit(2)
64 g_parent = self.find_node_parent(node)
65 group = inkex.etree.SubElement(g_parent, 'g')
66 desc = inkex.etree.SubElement(group, 'desc')
67 conf_txt = ''
68 if not is_empty(self.options.html_id):
69 conf_txt += 'html-id:' + self.options.html_id +'\n'
70 if not is_empty(self.options.html_class):
71 conf_txt += 'html-class:' + self.options.html_class +'\n'
72 conf_txt += 'width-unity:' + self.options.width_unity +'\n'
73 conf_txt += 'height-unity:' + self.options.height_unity
74 desc.text = conf_txt
75 for id,node in self.selected.iteritems():
76 group.insert( 1, node )
79 def find_node_parent(self, node):
80 #TODO: make it real!
81 return self.document.xpath(
82 '//*[@id="webslicer-layer" and @inkscape:groupmode="layer"]',
83 namespaces=inkex.NSS)[0]
85 def get_descendants_in_array(el):
86 descendants = el.getchildren()
87 for e in descendants:
88 descendants.extend( get_descendants_in_array(e) )
89 return descendants
91 if __name__ == '__main__':
92 e = WebSlicer_CreateGroup()
93 e.affect()