~centralelyon2010/inkscape/imagelinks2

« back to all changes in this revision

Viewing changes to share/extensions/restack.py

  • Committer: JazzyNico
  • Date: 2011-08-29 20:25:30 UTC
  • Revision ID: nicoduf@yahoo.fr-20110829202530-6deuoz11q90usldv
Code refactoring and merging with trunk (revision 10599).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
"""
3
 
Copyright (C) 2007,2008 Rob Antonishen; rob.antonishen@gmail.com
 
3
Copyright (C) 2007-2011 Rob Antonishen; rob.antonishen@gmail.com
4
4
 
5
5
Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
of this software and associated documentation files (the "Software"), to deal
52
52
        if len( self.selected ) > 0:
53
53
            objlist = []
54
54
            svg = self.document.getroot()
55
 
 
 
55
            parentnode = self.current_layer
56
56
            file = self.args[ -1 ]
57
 
            #get all bounding boxes in file by calling inkscape again with the --querry-all command line option
 
57
                        
 
58
            #get all bounding boxes in file by calling inkscape again with the --query-all command line option
58
59
            #it returns a comma seperated list structured id,x,y,w,h
59
60
            if bsubprocess:
60
61
                p = Popen('inkscape --query-all "%s"' % (file), shell=True, stdout=PIPE, stderr=PIPE)
61
 
                rc = p.wait()
62
 
                f = p.stdout
63
62
                err = p.stderr
 
63
                f = p.communicate()[0]
 
64
                try:
 
65
                    reader=csv.CSVParser().parse_string(f)    #there was a module cvs.py in earlier inkscape that behaved differently
 
66
                except:
 
67
                    reader=csv.reader(f.split( os.linesep ))
 
68
                err.close() 
64
69
            else:
65
 
                _,f,err = os.popen3( "inkscape --query-all %s" % ( file ) )
66
 
            reader=csv.reader( f.readlines() )
67
 
            f.close()
68
 
            err.close()
69
 
 
 
70
                _,f,err = os.popen3('inkscape --query-all "%s"' % ( file ) )
 
71
                reader=csv.reader( f )
 
72
                err.close()
 
73
                                
70
74
            #build a dictionary with id as the key
71
75
            dimen = dict()
72
76
            for line in reader:
73
 
                dimen[line[0]] = map( float, line[1:])
 
77
                if len(line) > 0:
 
78
                    dimen[line[0]] = map( float, line[1:])
74
79
 
 
80
            if not bsubprocess: #close file if opened using os.popen3
 
81
                f.close
 
82
                                
75
83
            #find the center of all selected objects **Not the average!
76
84
            x,y,w,h = dimen[self.selected.keys()[0]]
77
85
            minx = x
113
121
                    cy = y + h
114
122
                else:  # middle
115
123
                    cy = y + h / 2
116
 
 
 
124
                                
117
125
                #direction chosen
118
 
                if self.options.direction == "tb" or self.options.angle == 270:
 
126
                if self.options.direction == "tb" or (self.options.direction == "aa" and self.options.angle == 270):
119
127
                    objlist.append([cy,id])
120
 
                elif self.options.direction == "bt" or self.options.angle == 90:
 
128
                elif self.options.direction == "bt" or (self.options.direction == "aa" and self.options.angle == 90):
121
129
                    objlist.append([-cy,id])
122
 
                elif self.options.direction == "lr" or self.options.angle == 0 or self.options.angle == 360:
 
130
                elif self.options.direction == "lr" or (self.options.direction == "aa" and (self.options.angle == 0 or self.options.angle == 360)):
123
131
                    objlist.append([cx,id])
124
 
                elif self.options.direction == "rl" or self.options.angle == 180:
 
132
                elif self.options.direction == "rl" or (self.options.direction == "aa" and self.options.angle == 180):
125
133
                    objlist.append([-cx,id])
126
134
                elif self.options.direction == "aa":
127
135
                    distance = math.hypot(cx,cy)*(math.cos(math.radians(-self.options.angle)-math.atan2(cy, cx)))
136
144
            objlist.sort()
137
145
            #move them to the top of the object stack in this order.
138
146
            for item in objlist:
139
 
                svg.append( self.selected[item[1]])
 
147
                parentnode.append( self.selected[item[1]])
140
148
 
141
149
if __name__ == '__main__':
142
150
    e = Restack()