summaryrefslogtreecommitdiffstats
path: root/share/extensions/webslicer-create-group.py
blob: dcbf53fbe8ebd378681a3ac56e8938f63d9d4329 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
'''
Copyright (C) 2010 Aurelio A. Heckert, aurium (a) gmail dot com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''

import inkex
import gettext

_ = gettext.gettext

def is_empty(val):
    if val is None:
        return True
    else:
        return len(str(val)) == 0

class WebSlicer_CreateGroup(inkex.Effect):

    def __init__(self):
        inkex.Effect.__init__(self)
        self.OptionParser.add_option("--html-id",
                                     action="store", type="string",
                                     dest="html_id",
                                     help="")
        self.OptionParser.add_option("--html-class",
                                     action="store", type="string",
                                     dest="html_class",
                                     help="")
        self.OptionParser.add_option("--width-unity",
                                     action="store", type="string",
                                     dest="width_unity",
                                     help="")
        self.OptionParser.add_option("--height-unity",
                                     action="store", type="string",
                                     dest="height_unity",
                                     help="")


    def get_base_elements(self):
        self.layer = self.document.xpath(
                     '//*[@id="webslicer-layer" and @inkscape:groupmode="layer"]',
                     namespaces=inkex.NSS)[0]
        self.layer_descendants = self.get_descendants_in_array(self.layer)


    def get_descendants_in_array(self, el):
        descendants = el.getchildren()
        for e in descendants:
            descendants.extend( self.get_descendants_in_array(e) )
        return descendants


    def effect(self):
        self.get_base_elements()
        if len(self.selected) == 0:
            inkex.errormsg(_('You must to select some "Slicer rectangles" or other "Layout groups".'))
            exit(1)
        for id,node in self.selected.iteritems():
            if node not in self.layer_descendants:
                inkex.errormsg(_('Opss... The element "%s" is not in the Web Slicer layer') % id)
                exit(2)
        g_parent = self.getParentNode(node)
        inkex.errormsg( g_parent.get('id') )
        group = inkex.etree.SubElement(g_parent, 'g')
        desc = inkex.etree.SubElement(group, 'desc')
        conf_txt = ''
        if not is_empty(self.options.html_id):
            conf_txt += 'html-id:'    + self.options.html_id +'\n'
        if not is_empty(self.options.html_class):
            conf_txt += 'html-class:' + self.options.html_class +'\n'
        conf_txt += 'width-unity:' + self.options.width_unity +'\n'
        conf_txt += 'height-unity:' + self.options.height_unity
        desc.text = conf_txt
        for id,node in self.selected.iteritems():
            group.insert( 1, node )


if __name__ == '__main__':
    e = WebSlicer_CreateGroup()
    e.affect()