Code

Extensions. Restack patch for bug #524481 (restack extension hangs for drawings with...
[inkscape.git] / share / extensions / restack.py
index 10510eb0bf3efdcf160e84336dafd4060fc1a999..615b415274fb8a5906bf1288c51486bb3cb6194a 100644 (file)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 """
-Copyright (C) 2007,2008 Rob Antonishen; rob.antonishen@gmail.com
+Copyright (C) 2007-2011 Rob Antonishen; rob.antonishen@gmail.com
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
@@ -23,6 +23,12 @@ THE SOFTWARE.
 """
 import inkex, os, csv, math
 
+try:
+    from subprocess import Popen, PIPE
+    bsubprocess = True
+except:
+    bsubprocess = False
+
 class Restack(inkex.Effect):
     def __init__(self):
         inkex.Effect.__init__(self)
@@ -46,20 +52,34 @@ class Restack(inkex.Effect):
         if len( self.selected ) > 0:
             objlist = []
             svg = self.document.getroot()
-
+            parentnode = self.current_layer
             file = self.args[ -1 ]
-            #get all bounding boxes in file by calling inkscape again with the --querry-all command line option
+                       
+            #get all bounding boxes in file by calling inkscape again with the --query-all command line option
             #it returns a comma seperated list structured id,x,y,w,h
-            _,f,err = os.popen3( "inkscape --query-all %s" % ( file ) )
-            reader=csv.reader( f.readlines() )
-            f.close()
-            err.close()
-
+            if bsubprocess:
+                p = Popen('inkscape --query-all "%s"' % (file), shell=True, stdout=PIPE, stderr=PIPE)
+                err = p.stderr
+                f = p.communicate()[0]
+                try:
+                    reader=csv.CSVParser().parse_string(f)    #there was a module cvs.py in earlier inkscape that behaved differently
+                except:
+                    reader=csv.reader(f.split( os.linesep ))
+                err.close() 
+            else:
+                _,f,err = os.popen3('inkscape --query-all "%s"' % ( file ) )
+                reader=csv.reader( f )
+                err.close()
+                               
             #build a dictionary with id as the key
             dimen = dict()
             for line in reader:
-                dimen[line[0]] = map( float, line[1:])
+                if len(line) > 0:
+                    dimen[line[0]] = map( float, line[1:])
 
+            if not bsubprocess: #close file if opened using os.popen3
+                f.close
+                               
             #find the center of all selected objects **Not the average!
             x,y,w,h = dimen[self.selected.keys()[0]]
             minx = x
@@ -82,7 +102,7 @@ class Restack(inkex.Effect):
             midx = (minx + maxx) / 2
             midy = (miny + maxy) / 2
 
-            #calculate distances fro each selected object
+            #calculate distances for each selected object
             for id, node in self.selected.iteritems():
                 # get the bounding box
                 x,y,w,h = dimen[id]
@@ -101,15 +121,15 @@ class Restack(inkex.Effect):
                     cy = y + h
                 else:  # middle
                     cy = y + h / 2
-
+                               
                 #direction chosen
-                if self.options.direction == "tb" or self.options.angle == 270:
+                if self.options.direction == "tb" or (self.options.direction == "aa" and self.options.angle == 270):
                     objlist.append([cy,id])
-                elif self.options.direction == "bt" or self.options.angle == 90:
+                elif self.options.direction == "bt" or (self.options.direction == "aa" and self.options.angle == 90):
                     objlist.append([-cy,id])
-                elif self.options.direction == "lr" or self.options.angle == 0 or self.options.angle == 360:
+                elif self.options.direction == "lr" or (self.options.direction == "aa" and (self.options.angle == 0 or self.options.angle == 360)):
                     objlist.append([cx,id])
-                elif self.options.direction == "rl" or self.options.angle == 180:
+                elif self.options.direction == "rl" or (self.options.direction == "aa" and self.options.angle == 180):
                     objlist.append([-cx,id])
                 elif self.options.direction == "aa":
                     distance = math.hypot(cx,cy)*(math.cos(math.radians(-self.options.angle)-math.atan2(cy, cx)))
@@ -124,7 +144,11 @@ class Restack(inkex.Effect):
             objlist.sort()
             #move them to the top of the object stack in this order.
             for item in objlist:
-                svg.append( self.selected[item[1]])
+                parentnode.append( self.selected[item[1]])
+
+if __name__ == '__main__':
+    e = Restack()
+    e.affect()
+
 
-e = Restack()
-e.affect()
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99