Code

A little more work to Web Slicer (not usable yet)
[inkscape.git] / share / extensions / webslicer-create-rect.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_CreateRect(inkex.Effect):
33     def __init__(self):
34         inkex.Effect.__init__(self)
35         self.OptionParser.add_option("--name",
36                                      action="store", type="string",
37                                      dest="name",
38                                      help="")
39         self.OptionParser.add_option("--format",
40                                      action="store", type="string",
41                                      dest="format",
42                                      help="")
43         self.OptionParser.add_option("--dpi",
44                                      action="store", type="int",
45                                      dest="dpi",
46                                      help="")
47         self.OptionParser.add_option("--dimension",
48                                      action="store", type="string",
49                                      dest="dimension",
50                                      help="")
51         self.OptionParser.add_option("--bg-color",
52                                      action="store", type="string",
53                                      dest="bg_color",
54                                      help="")
55         self.OptionParser.add_option("--quality",
56                                      action="store", type="int",
57                                      dest="quality",
58                                      help="")
59         self.OptionParser.add_option("--gif-type",
60                                      action="store", type="string",
61                                      dest="gif_type",
62                                      help="")
63         self.OptionParser.add_option("--palette-size",
64                                      action="store", type="int",
65                                      dest="palette_size",
66                                      help="")
67         self.OptionParser.add_option("--html-id",
68                                      action="store", type="string",
69                                      dest="html_id",
70                                      help="")
71         self.OptionParser.add_option("--html-class",
72                                      action="store", type="string",
73                                      dest="html_class",
74                                      help="")
75         self.OptionParser.add_option("--layout-disposition",
76                                      action="store", type="string",
77                                      dest="layout_disposition",
78                                      help="")
79         self.OptionParser.add_option("--layout-position-anchor",
80                                      action="store", type="string",
81                                      dest="layout_position_anchor",
82                                      help="")
83         # inkscape param workarround
84         self.OptionParser.add_option("--tab")
87     def unique_slice_name(self):
88         name = self.options.name
89         el = self.document.xpath( '//*[@id="'+name+'"]', namespaces=inkex.NSS )
90         if len(el) > 0:
91             if name[-3:] == '-00': name = name[:-3]
92             num = 0
93             num_s = '00'
94             while len(el) > 0:
95                 num += 1
96                 num_s = str(num)
97                 if len(num_s)==1 : num_s = '0'+num_s
98                 el = self.document.xpath( '//*[@id="'+name+'-'+num_s+'"]',
99                                           namespaces=inkex.NSS )
100             self.options.name = name+'-'+num_s
103     def effect(self):
104         layer = self.get_slicer_layer()
105         #TODO: get selected elements to define location and size
106         rect = inkex.etree.SubElement(layer, 'rect')
107         if is_empty(self.options.name):
108             self.options.name = 'slice-00'
109         self.unique_slice_name()
110         rect.set('id', self.options.name)
111         rect.set('fill', 'red')
112         rect.set('opacity', '0.5')
113         rect.set('x', '-100')
114         rect.set('y', '-100')
115         rect.set('width', '200')
116         rect.set('height', '200')
117         desc = inkex.etree.SubElement(rect, 'desc')
118         conf_txt = "format:"+ self.options.format +"\n"
119         if not is_empty(self.options.dpi):
120             conf_txt += "dpi:"     + str(self.options.dpi) +"\n"
121         if not is_empty(self.options.html_id):
122             conf_txt += "html-id:" + self.options.html_id
123         desc.text = conf_txt
125     def get_slicer_layer(self):
126         # Test if webslicer-layer layer existis
127         layer = self.document.xpath(
128                      '//*[@id="webslicer-layer" and @inkscape:groupmode="layer"]',
129                      namespaces=inkex.NSS)
130         if len(layer) is 0:
131             # Create a new layer
132             layer = inkex.etree.SubElement(self.document.getroot(), 'g')
133             layer.set('id', 'webslicer-layer')
134             layer.set(inkex.addNS('label', 'inkscape'), 'Web Slicer')
135             layer.set(inkex.addNS('groupmode', 'inkscape'), 'layer')
136         else:
137             layer = layer[0]
138         return layer
140 if __name__ == '__main__':
141     e = WebSlicer_CreateRect()
142     e.affect()