Code

Upgrading to trunk
[inkscape.git] / share / extensions / jessyInk_view.py
1 #!/usr/bin/env python
2 # Copyright 2008, 2009 Hannes Hochreiner
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see http://www.gnu.org/licenses/.
16 # These lines are only needed if you don't put the script directly into
17 # the installation directory
18 import sys
19 # Unix
20 sys.path.append('/usr/share/inkscape/extensions')
21 # OS X
22 sys.path.append('/Applications/Inkscape.app/Contents/Resources/extensions')
23 # Windows
24 sys.path.append('C:\Program Files\Inkscape\share\extensions')
26 # We will use the inkex module with the predefined Effect base class.
27 import inkex
29 def propStrToList(str):
30         list = []
31         propList = str.split(";")
32         for prop in propList:
33                 if not (len(prop) == 0):
34                         list.append(prop.strip())
35         return list
37 def propListToDict(list):
38         dictio = {}
40         for prop in list:
41                 keyValue = prop.split(":")
43                 if len(keyValue) == 2:
44                         dictio[keyValue[0].strip()] = keyValue[1].strip()
46         return dictio
48 class JessyInk_Effects(inkex.Effect):
49         def __init__(self):
50                 # Call the base class constructor.
51                 inkex.Effect.__init__(self)
53                 self.OptionParser.add_option('--tab', action = 'store', type = 'string', dest = 'what')
54                 self.OptionParser.add_option('--viewOrder', action = 'store', type = 'string', dest = 'viewOrder', default = 1)
55                 self.OptionParser.add_option('--viewDuration', action = 'store', type = 'float', dest = 'viewDuration', default = 0.8)
56                 self.OptionParser.add_option('--removeView', action = 'store', type = 'inkbool', dest = 'removeView', default = False)
58                 inkex.NSS[u"jessyink"] = u"https://launchpad.net/jessyink"
60         def effect(self):
61                 # Check version.
62                 scriptNodes = self.document.xpath("//svg:script[@jessyink:version='1.5.1']", namespaces=inkex.NSS)
64                 if len(scriptNodes) != 1:
65                         sys.stderr.write("The JessyInk script is not installed in this SVG file or has a different version than the JessyInk extensions. Please select \"install/update...\" from the \"JessyInk\" sub-menu of the \"Effects\" menu to install or update the JessyInk script.\n\n")
67                 rect = None
69                 for id, node in self.selected.items():
70                         if rect == None:
71                                 rect = node
72                         else:
73                                 sys.stderr.write("More than one object selected. Please select only one object.\n")
74                                 exit()
76                 if rect == None:
77                         sys.stderr.write("No object selected. Please select the object you want to assign a view to and then press apply.\n")
78                         exit()
80                 if not self.options.removeView:
81                         # Remove the view that currently has the requested order number.
82                         for node in rect.xpath("ancestor::svg:g[@inkscape:groupmode='layer']/descendant::*[@jessyink:view]", namespaces=inkex.NSS):
83                                 propDict = propListToDict(propStrToList(node.attrib["{" + inkex.NSS["jessyink"] + "}view"]))
84         
85                                 if propDict["order"] == self.options.viewOrder:
86                                         del node.attrib["{" + inkex.NSS["jessyink"] + "}view"]
87                         
88                         # Set the new view.
89                         rect.set("{" + inkex.NSS["jessyink"] + "}view","name:view;order:" + self.options.viewOrder + ";length:" + str(int(self.options.viewDuration * 1000)))
91                         # Remove possible effect arguments.
92                         if rect.attrib.has_key("{" + inkex.NSS["jessyink"] + "}effectIn"):
93                                 del rect.attrib["{" + inkex.NSS["jessyink"] + "}effectIn"]
95                         if rect.attrib.has_key("{" + inkex.NSS["jessyink"] + "}effectOut"):
96                                 del rect.attrib["{" + inkex.NSS["jessyink"] + "}effectOut"]
97                 else:
98                         if node.attrib.has_key("{" + inkex.NSS["jessyink"] + "}view"):
99                                 del node.attrib["{" + inkex.NSS["jessyink"] + "}view"]
100                 
101 # Create effect instance
102 effect = JessyInk_Effects()
103 effect.affect()