Code

aadfded38e26b5a38099991a3a56e28f3b9b0b61
[inkscape.git] / share / extensions / webslicer_create_group.py
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="")
53     def get_base_elements(self):
54         layerArr = self.document.xpath(
55                    '//*[@id="webslicer-layer" and @inkscape:groupmode="layer"]',
56                    namespaces=inkex.NSS)
57         if len(layerArr) > 0:
58             self.layer = layerArr[0]
59         else:
60             inkex.errormsg(_('You must to create and select some "Slicer rectangles" before try to group.'))
61             exit(3)
62         self.layer_descendants = self.get_descendants_in_array(self.layer)
65     def get_descendants_in_array(self, el):
66         descendants = el.getchildren()
67         for e in descendants:
68             descendants.extend( self.get_descendants_in_array(e) )
69         return descendants
72     def effect(self):
73         self.get_base_elements()
74         if len(self.selected) == 0:
75             inkex.errormsg(_('You must to select some "Slicer rectangles" or other "Layout groups".'))
76             exit(1)
77         for id,node in self.selected.iteritems():
78             if node not in self.layer_descendants:
79                 inkex.errormsg(_('Opss... The element "%s" is not in the Web Slicer layer') % id)
80                 exit(2)
81         g_parent = self.getParentNode(node)
82         group = inkex.etree.SubElement(g_parent, 'g')
83         desc = inkex.etree.SubElement(group, 'desc')
84         conf_txt = ''
85         if not is_empty(self.options.html_id):
86             conf_txt += 'html-id:'    + self.options.html_id +'\n'
87         if not is_empty(self.options.html_class):
88             conf_txt += 'html-class:' + self.options.html_class +'\n'
89         conf_txt += 'width-unity:' + self.options.width_unity +'\n'
90         conf_txt += 'height-unity:' + self.options.height_unity
91         desc.text = conf_txt
92         for id,node in self.selected.iteritems():
93             group.insert( 1, node )
96 if __name__ == '__main__':
97     e = WebSlicer_CreateGroup()
98     e.affect()