Add the "copy" option.
[mpl_properties.git] / mpl_properties.py
blobcfc1afafdae5ca46705c5b90b7d99bf9d7f1eb69
1 import pylab as P
2 import pygtk
3 pygtk.require('2.0')
4 import gtk
5 import gobject
7 def print_fig_prop(fig):
8 for ind_ax, ax in enumerate(P.figure(fig).axes):
9 print "*** Axe %d ***"%ind_ax
10 print "Xmin : %g"%ax.get_xlim()[0]
11 print "Xmax : %g"%ax.get_xlim()[1]
12 print "Ymin : %g"%ax.get_ylim()[0]
13 print "Ymax : %g"%ax.get_ylim()[1]
14 print "Fontsize (X) : %g"%ax.get_xticklabels()[0].get_fontsize()
15 print "Fontsize (Y) : %g"%ax.get_yticklabels()[0].get_fontsize()
16 print "Title : " + ax.title.get_text()
17 print "Title fontsize : %g"%ax.title.get_fontsize()
18 print "X label : " + ax.xaxis.get_label().get_text()
19 print "X label fontsize : %g"%ax.xaxis.get_label().get_fontsize()
20 print "Y label : " + ax.yaxis.get_label().get_text()
21 print "Y label fontsize : %g"%ax.yaxis.get_label().get_fontsize()
23 for ind_line, line in enumerate(ax.lines):
24 print " *** Line %d ***"%ind_line
25 print " Style : " + line.get_linestyle()
26 print " Marker : " + line.get_marker()
27 print " Line Width : %.1f"%line.get_linewidth()
28 print " Color : " + line.get_color()
29 for ind_im, im in enumerate(ax.images):
30 print " *** Image %d ***"%ind_im
31 print " Colormap : " + im.cmap.name
32 print " Interpolation : " + im.get_interpolation()
33 print " Extent : [%g, %g, %g, %g]"%(im.get_extent()[0],im.get_extent()[1],im.get_extent()[2],im.get_extent()[3], )
35 class properties:
37 def row_activated(self, treeview):
38 model, iter = treeview.get_selection().get_selected()
39 type = model.get_value(iter, 1)
40 self.type_selected = type
41 obj = model.get_value(iter,2)
42 self.selected = model.get_value(iter,2)
43 self.fig = model.get_value(iter, 3)
44 if type == 'line':
45 self.display_line(obj)
46 elif type == 'axe':
47 self.display_axe(obj)
48 elif type == 'image':
49 self.display_image(obj)
50 elif type == 'figure':
51 self.display_figure(obj)
52 elif type == 'text':
53 self.display_text(obj)
55 def get_value(self, obj, name):
56 if name == 'linestyle':
57 return obj.get_linestyle()
58 elif name == 'marker':
59 return obj.get_marker()
60 elif name == 'linewidth':
61 return obj.get_linewidth()
62 elif name == 'linecolor':
63 return obj.get_color()
64 elif name == 'xmin':
65 return obj.get_xlim()[0]
66 elif name == 'xmax':
67 return obj.get_xlim()[1]
68 elif name == 'ymin':
69 return obj.get_ylim()[0]
70 elif name == 'ymax':
71 return obj.get_ylim()[1]
72 elif name == 'title':
73 return obj.title.get_text()
74 elif name == 'titlefontsize':
75 return obj.title.get_fontsize()
76 elif name == 'xlabel':
77 return obj.xaxis.get_label().get_text()
78 elif name == 'ylabel':
79 return obj.yaxis.get_label().get_text()
80 elif name == 'xfontsize':
81 return obj.xaxis.get_ticklabels()[0].get_fontsize()
82 elif name == 'yfontsize':
83 return obj.yaxis.get_ticklabels()[0].get_fontsize()
84 elif name == 'xlabelfontsize':
85 return obj.xaxis.get_label().get_fontsize()
86 elif name == 'ylabelfontsize':
87 return obj.yaxis.get_label().get_fontsize()
88 elif name == 'figwidth':
89 return obj.window.get_size()[0]
90 elif name == 'figheight':
91 return obj.window.get_size()[1]
92 elif name == 'xpos':
93 return obj.get_position()[0]
94 elif name == 'ypos':
95 return obj.get_position()[1]
96 elif name == 'width':
97 return obj.get_position()[2]
98 elif name == 'height':
99 return obj.get_position()[3]
100 elif name == 'xbegin':
101 return obj.get_extent()[0]
102 elif name == 'xend':
103 return obj.get_extent()[1]
104 elif name == 'ybegin':
105 return obj.get_extent()[2]
106 elif name == 'yend':
107 return obj.get_extent()[3]
108 elif name == 'cmap':
109 return obj.cmap.name
110 elif name == 'interp':
111 return obj.get_interpolation()
112 elif name == 'label':
113 return obj.get_label()
114 elif name == 'textx':
115 return obj.get_position()[0]
116 elif name == 'texty':
117 return obj.get_position()[1]
118 elif name == 'text':
119 return obj.get_text()
120 elif name == 'textfontsize':
121 return obj.get_fontsize()
122 elif name == 'textrotation':
123 return obj.get_rotation()
127 def set_value(self, obj, name, value):
128 if name == 'linestyle':
129 obj.set_linestyle(value)
130 elif name == 'marker':
131 obj.set_marker(value)
132 elif name == 'linewidth':
133 obj.set_linewidth(float(value))
134 elif name == 'linecolor':
135 obj.set_color(value)
136 elif name == 'xmin' or name == 'xmax':
137 obj.set_xlim([float(self.param['xmin'].get_text()), float(self.param['xmax'].get_text())])
138 elif name == 'ymin' or name == 'ymax':
139 obj.set_ylim([float(self.param['ymin'].get_text()), float(self.param['ymax'].get_text())])
140 elif name == 'title':
141 obj.title.set_text(value)
142 elif name == 'titlefontsize':
143 obj.title.set_fontsize(float(value))
144 elif name == 'xlabel':
145 obj.xaxis.get_label().set_text(value)
146 elif name == 'ylabel':
147 obj.yaxis.get_label().set_text(value)
148 elif name == 'xfontsize':
149 for tick in obj.xaxis.get_ticklabels():
150 tick.set_fontsize(float(value))
151 elif name == 'yfontsize':
152 for tick in obj.yaxis.get_ticklabels():
153 tick.set_fontsize(float(value))
154 elif name == 'xlabelfontsize':
155 obj.xaxis.get_label().set_fontsize(float(value))
156 elif name == 'ylabelfontsize':
157 obj.yaxis.get_label().set_fontsize(float(value))
158 elif name == 'figwidth' or name == 'figheight':
159 obj.resize(int(self.param['figwidth'].get_text()),int(self.param['figheight'].get_text()) )
160 elif name == 'xpos' or name =='ypos' or name == 'width' or name == 'height':
161 a = float(self.param['xpos'].get_text())
162 b = float(self.param['ypos'].get_text())
163 c = float(self.param['width'].get_text())
164 d = float(self.param['height'].get_text())
165 obj.set_position([a,b,c,d])
166 elif name == 'cmap':
167 obj.set_cmap(getattr(P.cm, value))
168 elif name == 'interp':
169 obj.set_interpolation(value)
170 elif name == 'label':
171 obj.set_label(value)
172 elif name == 'textx':
173 obj.set_x(float(value))
174 elif name == 'texty':
175 obj.set_y(float(value))
176 elif name == 'text':
177 obj.set_text(value)
178 elif name == 'textfontsize':
179 obj.set_fontsize(float(value))
180 elif name == 'textrotation':
181 obj.set_rotation(float(value))
183 def remove(self, widget):
184 self.paramtable.remove(widget)
186 def add_param(self, widget, name, title, obj):
187 tmp = gtk.HBox()
188 tmp.show()
190 tmp2 = gtk.Label(title)
191 tmp2.show()
192 tmp.pack_start(tmp2)
194 default = self.get_value(obj, name)
195 if default == None:
196 default=''
197 self.param[name] = gtk.Entry(255)
198 self.param[name].set_text(str(default))
199 self.param[name].connect("activate", self.apply)
200 self.param[name].show()
201 tmp.pack_start(self.param[name])
203 widget.pack_start(tmp, False)
205 def display_line(self, line):
206 self.paramtable.foreach(self.remove)
207 self.param.clear()
208 for name in self.param_lines_order:
209 self.add_param(self.paramtable, name, self.param_names[name], line)
211 def display_axe(self, ax):
212 self.paramtable.foreach(self.remove)
213 self.param.clear()
214 for name in self.param_axe_order:
215 self.add_param(self.paramtable, name, self.param_names[name], ax)
217 def display_figure(self, figure):
218 self.paramtable.foreach(self.remove)
219 self.param.clear()
220 for name in self.param_figure_order:
221 self.add_param(self.paramtable, name, self.param_names[name], figure)
222 def display_text(self, text):
223 self.paramtable.foreach(self.remove)
224 self.param.clear()
225 for name in self.param_text_order:
226 self.add_param(self.paramtable, name, self.param_names[name], text)
228 def display_image(self, im):
229 self.paramtable.foreach(self.remove)
230 self.param.clear()
231 for name in self.param_image_order:
232 self.add_param(self.paramtable, name, self.param_name[name], im)
234 def apply(self, widget):
235 obj = self.selected
236 for name, button in self.param.items():
237 value = button.get_text()
238 self.set_value(obj, name, value)
239 P.figure(self.fig)
240 P.draw()
241 self.row_activated(self.treeview)
243 def delete(self, widget):
244 model, iter = self.treeview.get_selection().get_selected()
245 if self.type_selected=='axe':
246 P.delaxes(self.selected)
247 P.figure(self.fig)
248 P.draw()
249 elif self.type_selected=='figure':
250 P.close(self.fig)
251 elif self.type_selected=='line':
252 axe = model.get_value(model.iter_parent(iter), 2)
253 del axe.lines[model.get_value(iter, 4)]
254 P.figure(self.fig)
255 P.draw()
256 elif self.type_selected=='image':
257 axe = model.get_value(model.iter_parent(iter), 2)
258 del axe.images[model.get_value(iter, 4)]
259 P.figure(self.fig)
260 P.draw()
261 elif self.type_selected=='text':
262 axe = model.get_value(model.iter_parent(iter), 2)
263 del axe.texts[model.get_value(iter, 4)]
264 P.figure(self.fig)
265 P.draw()
266 self.treestore.remove(iter)
267 self.paramtable.foreach(self.remove)
268 self.param.clear()
270 def update(self, widget):
271 self.window.destroy()
272 properties()
274 def copy(self, widget):
275 model, iter = self.treeview.get_selection().get_selected()
276 self.wincopy = gtk.Window(gtk.WINDOW_TOPLEVEL)
277 self.wincopy.set_transient_for(self.window)
278 self.wincopy.set_title('Copy ' + model.get_value(iter, 0))
279 vbox = gtk.VBox()
280 self.wincopy.add(vbox)
281 scroll = gtk.ScrolledWindow()
282 vbox.pack_start(scroll)
283 treeview = gtk.TreeView(model)
284 renderer = gtk.CellRendererText()
285 column = gtk.TreeViewColumn('Object ID', renderer, text=0)
286 treeview.set_headers_visible(False)
287 treeview.append_column(column)
288 treeview.show()
289 scroll.add(treeview)
290 scroll.show()
291 hbox = gtk.HBox()
292 vbox.pack_start(hbox)
293 button = gtk.Button('Cancel')
294 button.connect("clicked", self.close_copy)
295 button.show()
296 hbox.pack_start(button)
297 button = gtk.Button('Copy')
298 button.connect("clicked", self.do_copy)
299 button.show()
300 hbox.pack_start(button)
301 hbox.show()
302 vbox.show()
303 self.wincopy.show()
304 if self.type_selected=='line':
305 print 'line'
307 def do_copy(self, widget):
308 print 'Do copy'
309 def close_copy(self, widget):
310 self.wincopy.destroy()
312 def __init__(self):
313 self.selected=None
314 self.param={}
315 self.param_names = {'label': 'Label',
316 'linestyle': 'Line style',
317 'marker': 'Marker',
318 'linewidth': 'Line Width',
319 'linecolor': 'Line Color',
320 'cmap': 'Colormap',
321 'interp': 'Interpolation',
322 'label': 'Label',
323 'xpos': 'X position',
324 'ypos': 'Y position',
325 'width': 'Width',
326 'height': 'Height',
327 'xmin': 'X min',
328 'xmax': 'X max',
329 'ymin': 'Y min',
330 'ymax': 'Y max',
331 'title': 'Title',
332 'titlefontsize': 'Title fontsize',
333 'xlabel': 'X label',
334 'ylabel': 'Y label',
335 'xlabelfontsize': 'Fontsize (X label)',
336 'ylabelfontsize': 'Fontsize (Y label)',
337 'xfontsize': 'Fontsize (X ticks)',
338 'yfontsize': 'Fontsize (Y ticks)',
339 'figwidth': 'Width',
340 'figheight': 'Height',
341 'textx': 'X position',
342 'texty': 'Y position',
343 'text': 'Text',
344 'textfontsize': 'Fontsize',
345 'textrotation': 'Rotation'}
347 self.param_lines_order = ['label', 'linestyle', 'linecolor', 'linewidth', 'marker']
348 self.param_image_order = ['label', 'cmap', 'interp']
349 self.param_axe_order = ['xpos', 'ypos', 'width', 'height',
350 'xmin', 'xmax', 'ymin', 'ymax',
351 'title', 'titlefontsize',
352 'xlabel', 'xlabelfontsize',
353 'ylabel', 'ylabelfontsize',
354 'xfontsize', 'yfontsize']
355 self.param_figure_order = ['figwidth', 'figheight']
356 self.param_text_order = ['textx', 'texty', 'text', 'textfontsize',
357 'textrotation']
360 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
361 self.window.set_title('Graphics properties')
362 self.window.set_default_size(300, 400)
363 vbox = gtk.VBox()
364 hbox = gtk.HBox()
365 self.paramtable = gtk.VBox()
366 self.paramtable.show()
367 self.treestore = gtk.TreeStore(gobject.TYPE_STRING, str, object, int, int)
368 self.treeview = gtk.TreeView(self.treestore)
369 self.treeview.set_headers_visible(False)
370 renderer = gtk.CellRendererText()
371 self.column = gtk.TreeViewColumn('Object ID', renderer, text=0)
372 self.treeview.append_column(self.column)
373 for figure in P.matplotlib._pylab_helpers.Gcf.get_all_fig_managers():
374 fig = figure.num
375 iter_fig = self.treestore.append(None, ['Figure %d'%fig, 'figure' ,figure, fig, fig])
376 for ind_ax, ax in enumerate(P.figure(fig).axes):
377 iter_ax = self.treestore.append(iter_fig, ['Axe %d'%ind_ax, 'axe' ,ax, fig, ind_ax])
378 for ind_line, line in enumerate(ax.lines):
379 iter = self.treestore.append(iter_ax, ['Line %d'%ind_line, 'line', line, fig, ind_line])
380 for ind_im, im in enumerate(ax.images):
381 iter = self.treestore.append(iter_ax, ['Image %d'%ind_im, 'image',im, fig, ind_im])
382 for ind_text, text in enumerate(ax.texts):
383 iter = self.treestore.append(iter_ax, ['Text %d'%ind_text, 'text', text, fig, ind_text])
385 try:
386 self.fig=fig
387 except:
388 return
389 self.treeview.connect("cursor-changed", self.row_activated)
390 scroll = gtk.ScrolledWindow()
391 scroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
392 scroll.add(self.treeview)
393 scroll.show()
394 hbox.pack_start(scroll)
396 viewport = gtk.Viewport()
397 viewport.add(self.paramtable)
398 viewport.show()
399 scroll = gtk.ScrolledWindow()
400 scroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
401 scroll.add(viewport)
402 scroll.show()
403 hbox.pack_start(scroll)
405 bottom=gtk.HBox()
406 button = gtk.Button('Update')
407 button.connect("clicked", self.update)
408 button.show()
409 bottom.pack_start(button)
410 button = gtk.Button('Copy')
411 button.connect("clicked", self.copy)
412 button.show()
413 bottom.pack_start(button)
414 button = gtk.Button('Delete')
415 button.connect("clicked", self.delete)
416 button.show()
417 bottom.pack_start(button)
418 bottom.show()
419 button = gtk.Button('Apply')
420 button.connect("clicked", self.apply)
421 button.show()
422 bottom.pack_start(button)
423 bottom.show()
425 vbox.pack_start(hbox)
426 vbox.pack_start(bottom, False)
427 self.window.add(vbox)
428 self.treeview.show()
429 hbox.show()
430 vbox.show()
431 self.window.show()
432 gtk.main()