Add blinked element
[mpl_properties.git] / mpl_properties.py
blobcff8ea34c4bba29ea01d215af121adaa2203b31e
1 import pylab as P
2 import pygtk
3 import gtk
4 import gobject
6 def print_fig_prop(fig):
7 for ind_ax, ax in enumerate(P.figure(fig).axes):
8 print "*** Axe %d ***"%ind_ax
9 print "Xmin : %g"%ax.get_xlim()[0]
10 print "Xmax : %g"%ax.get_xlim()[1]
11 print "Ymin : %g"%ax.get_ylim()[0]
12 print "Ymax : %g"%ax.get_ylim()[1]
13 print "Fontsize (X) : %g"%ax.get_xticklabels()[0].get_fontsize()
14 print "Fontsize (Y) : %g"%ax.get_yticklabels()[0].get_fontsize()
15 print "Title : " + ax.title.get_text()
16 print "Title fontsize : %g"%ax.title.get_fontsize()
17 print "X label : " + ax.xaxis.get_label().get_text()
18 print "X label fontsize : %g"%ax.xaxis.get_label().get_fontsize()
19 print "Y label : " + ax.yaxis.get_label().get_text()
20 print "Y label fontsize : %g"%ax.yaxis.get_label().get_fontsize()
22 for ind_line, line in enumerate(ax.lines):
23 print " *** Line %d ***"%ind_line
24 print " Style : " + line.get_linestyle()
25 print " Marker : " + line.get_marker()
26 print " Line Width : %.1f"%line.get_linewidth()
27 print " Color : " + line.get_color()
28 for ind_im, im in enumerate(ax.images):
29 print " *** Image %d ***"%ind_im
30 print " Colormap : " + im.cmap.name
31 print " Interpolation : " + im.get_interpolation()
32 print " Extent : [%g, %g, %g, %g]"%(im.get_extent()[0],im.get_extent()[1],im.get_extent()[2],im.get_extent()[3], )
34 class properties:
36 def row_activated(self, treeview):
37 model, iter = treeview.get_selection().get_selected()
38 type = model.get_value(iter, 1)
39 self.type_selected = type
40 obj = model.get_value(iter,2)
41 self.selected = model.get_value(iter,2)
42 self.fig = model.get_value(iter, 3)
43 if type == 'line':
44 self.display_line(obj)
45 elif type == 'axe':
46 self.display_axe(obj)
47 elif type == 'image':
48 self.display_image(obj)
49 elif type == 'figure':
50 self.display_figure(obj)
51 elif type == 'text':
52 self.display_text(obj)
53 ## if type != 'figure':
54 ## visi = obj.get_visible()
55 ## obj.set_visible(not(visi))
56 ## P.figure(self.fig)
57 ## P.draw()
58 ## obj.set_visible(visi)
59 ## P.figure(self.fig)
60 ## P.draw()
62 def get_value(self, obj, name):
63 if name == 'linestyle':
64 return obj.get_linestyle()
65 elif name == 'marker':
66 return obj.get_marker()
67 elif name == 'linewidth':
68 return obj.get_linewidth()
69 elif name == 'color':
70 return obj.get_color()
71 elif name == 'xmin':
72 return obj.get_xlim()[0]
73 elif name == 'xmax':
74 return obj.get_xlim()[1]
75 elif name == 'ymin':
76 return obj.get_ylim()[0]
77 elif name == 'ymax':
78 return obj.get_ylim()[1]
79 elif name == 'title':
80 return obj.title.get_text()
81 elif name == 'titlefontsize':
82 return obj.title.get_fontsize()
83 elif name == 'xlabel':
84 return obj.xaxis.get_label().get_text()
85 elif name == 'ylabel':
86 return obj.yaxis.get_label().get_text()
87 elif name == 'xfontsize':
88 return obj.xaxis.get_ticklabels()[0].get_fontsize()
89 elif name == 'yfontsize':
90 return obj.yaxis.get_ticklabels()[0].get_fontsize()
91 elif name == 'xlabelfontsize':
92 return obj.xaxis.get_label().get_fontsize()
93 elif name == 'ylabelfontsize':
94 return obj.yaxis.get_label().get_fontsize()
95 elif name == 'figwidth':
96 ## return obj.window.get_size()[0]
97 return obj.get_figwidth()
98 elif name == 'figheight':
99 ## return obj.window.get_size()[1]
100 return obj.get_figheight()
101 elif name == 'xpos':
102 return obj.get_position()[0]
103 elif name == 'ypos':
104 return obj.get_position()[1]
105 elif name == 'width':
106 return obj.get_position()[2]
107 elif name == 'height':
108 return obj.get_position()[3]
109 elif name == 'xbegin':
110 return obj.get_extent()[0]
111 elif name == 'xend':
112 return obj.get_extent()[1]
113 elif name == 'ybegin':
114 return obj.get_extent()[2]
115 elif name == 'yend':
116 return obj.get_extent()[3]
117 elif name == 'cmap':
118 return obj.cmap.name
119 elif name == 'interp':
120 return obj.get_interpolation()
121 elif name == 'label':
122 return obj.get_label()
123 elif name == 'textx':
124 return obj.get_position()[0]
125 elif name == 'texty':
126 return obj.get_position()[1]
127 elif name == 'text':
128 return obj.get_text()
129 elif name == 'textfontsize':
130 return obj.get_fontsize()
131 elif name == 'textrotation':
132 return obj.get_rotation()
133 elif name == 'markersize':
134 return obj.get_markersize()
135 elif name == 'markerfacecolor':
136 return obj.get_markerfacecolor()
137 elif name == 'markeredgecolor':
138 return obj.get_markeredgecolor()
139 elif name == 'markeredgewidth':
140 return obj.get_markeredgewidth()
143 def set_value(self, obj, name, value):
144 if name == 'linestyle':
145 obj.set_linestyle(value)
146 elif name == 'marker':
147 obj.set_marker(value)
148 elif name == 'linewidth':
149 obj.set_linewidth(float(value))
150 elif name == 'color':
151 obj.set_color(value)
152 elif name == 'xmin' or name == 'xmax':
153 obj.set_xlim([float(self.param['xmin'].get_text()), float(self.param['xmax'].get_text())])
154 elif name == 'ymin' or name == 'ymax':
155 obj.set_ylim([float(self.param['ymin'].get_text()), float(self.param['ymax'].get_text())])
156 elif name == 'title':
157 obj.title.set_text(value)
158 elif name == 'titlefontsize':
159 obj.title.set_fontsize(float(value))
160 elif name == 'xlabel':
161 obj.xaxis.get_label().set_text(value)
162 elif name == 'ylabel':
163 obj.yaxis.get_label().set_text(value)
164 elif name == 'xfontsize':
165 for tick in obj.xaxis.get_ticklabels():
166 tick.set_fontsize(float(value))
167 elif name == 'yfontsize':
168 for tick in obj.yaxis.get_ticklabels():
169 tick.set_fontsize(float(value))
170 elif name == 'xlabelfontsize':
171 obj.xaxis.get_label().set_fontsize(float(value))
172 elif name == 'ylabelfontsize':
173 obj.yaxis.get_label().set_fontsize(float(value))
174 ## elif name == 'figwidth' or name == 'figheight':
175 ## obj.resize(int(self.param['figwidth'].get_text()),int(self.param['figheight'].get_text()) )
176 elif name == 'figwidth':
177 obj.set_figwidth(float(value))
178 elif name == 'figheight':
179 obj.set_figheight(float(value))
180 elif name == 'xpos' or name =='ypos' or name == 'width' or name == 'height':
181 a = float(self.param['xpos'].get_text())
182 b = float(self.param['ypos'].get_text())
183 c = float(self.param['width'].get_text())
184 d = float(self.param['height'].get_text())
185 obj.set_position([a,b,c,d])
186 elif name == 'cmap':
187 obj.set_cmap(getattr(P.cm, value))
188 elif name == 'interp':
189 obj.set_interpolation(value)
190 elif name == 'label':
191 obj.set_label(value)
192 elif name == 'textx':
193 obj.set_x(float(value))
194 elif name == 'texty':
195 obj.set_y(float(value))
196 elif name == 'text':
197 obj.set_text(value)
198 elif name == 'textfontsize':
199 obj.set_fontsize(float(value))
200 elif name == 'textrotation':
201 obj.set_rotation(float(value))
202 elif name == 'markersize':
203 obj.set_markersize(float(value))
204 elif name == 'markerfacecolor':
205 obj.set_markerfacecolor(value)
206 elif name == 'markeredgecolor':
207 obj.set_markeredgecolor(value)
208 elif name == 'markeredgewidth':
209 obj.set_markeredgewidth(float(value))
211 def remove(self, widget):
212 self.paramtable.remove(widget)
214 def add_row(self, name, title, obj, line):
215 tmp = gtk.Label(title)
216 tmp.show()
217 self.paramtable.attach(tmp,0,1,line,line+1, yoptions=gtk.FILL)
219 default = self.get_value(obj, name)
220 ## if default == None or default == 'None':
221 ## default = ''
222 if not self.param_treestore.has_key(name):
223 self.param[name] = gtk.Entry(255)
224 self.param[name].set_text(str(default))
225 self.param[name].connect("activate", self.apply)
226 else:
227 self.param[name] = gtk.ComboBox(self.param_treestore[name])
228 cell = gtk.CellRendererText()
229 self.param[name].pack_start(cell, True)
230 self.param[name].add_attribute(cell, 'text', 0)
231 ii=0
232 while self.param_treestore[name][ii][0] != default:
233 ii = ii+1
234 self.param[name].set_active(ii)
235 self.param[name].connect("changed", self.apply)
236 self.param[name].show()
237 self.paramtable.attach(self.param[name], 1, 2, line, line+1, yoptions=gtk.FILL)
239 def display_line(self, line):
240 self.paramtable.foreach(self.remove)
241 self.param.clear()
242 self.paramtable.resize(2, len(self.param_lines_order))
243 for ii, name in enumerate(self.param_lines_order):
244 self.add_row(name, self.param_names[name], line, ii)
246 def display_axe(self, ax):
247 self.paramtable.foreach(self.remove)
248 self.param.clear()
249 self.paramtable.resize(2, len(self.param_axe_order))
250 for ii,name in enumerate(self.param_axe_order):
251 self.add_row(name, self.param_names[name], ax, ii)
253 def display_figure(self, figure):
254 self.paramtable.foreach(self.remove)
255 self.param.clear()
256 self.paramtable.resize(2, len(self.param_figure_order))
257 for ii, name in enumerate(self.param_figure_order):
258 self.add_row(name, self.param_names[name], figure, ii)
260 def display_text(self, text):
261 self.paramtable.foreach(self.remove)
262 self.param.clear()
263 self.paramtable.resize(2, len(self.param_text_order))
264 for ii, name in enumerate(self.param_text_order):
265 self.add_row(name, self.param_names[name], text, ii)
267 def display_image(self, im):
268 self.paramtable.foreach(self.remove)
269 self.param.clear()
270 self.paramtable.resize(2, len(self.param_image_order))
271 for ii, name in enumerate(self.param_image_order):
272 self.add_row(name, self.param_names[name], im, ii)
274 def apply(self, widget):
275 obj = self.selected
276 for name, button in self.param.items():
277 if not self.param_treestore.has_key(name):
278 value = button.get_text()
279 else:
280 iter = button.get_active_iter()
281 value = self.param_treestore[name].get_value(iter, 0)
282 self.set_value(obj, name, value)
283 P.figure(self.fig)
284 P.draw()
285 self.row_activated(self.treeview)
287 def save(self, widget):
288 model, iter = self.treeview.get_selection().get_selected()
289 if self.type_selected == 'line':
290 line = model.get_value(iter,2)
291 x = P.transpose(P.array([line.get_xdata(), line.get_ydata()]))
292 fileselection = gtk.FileSelection('Save ' + model.get_value(iter,0) + ' data in...')
293 fileselection.ok_button.connect("clicked", self.go_save, fileselection, x)
294 fileselection.cancel_button.connect("clicked", lambda w: fileselection.destroy())
295 fileselection.show()
296 def go_save(self, widget, win, data):
297 name = win.get_filename()
298 P.save(name, data)
299 win.destroy()
301 def delete(self, widget):
302 model, iter = self.treeview.get_selection().get_selected()
303 if self.type_selected=='axe':
304 P.delaxes(self.selected)
305 P.figure(self.fig)
306 P.draw()
307 elif self.type_selected=='figure':
308 P.close(self.fig)
309 elif self.type_selected=='line':
310 axe = model.get_value(model.iter_parent(iter), 2)
311 del axe.lines[model.get_value(iter, 4)]
312 P.figure(self.fig)
313 P.draw()
314 elif self.type_selected=='image':
315 axe = model.get_value(model.iter_parent(iter), 2)
316 del axe.images[model.get_value(iter, 4)]
317 P.figure(self.fig)
318 P.draw()
319 elif self.type_selected=='text':
320 axe = model.get_value(model.iter_parent(iter), 2)
321 del axe.texts[model.get_value(iter, 4)]
322 P.figure(self.fig)
323 P.draw()
324 self.treestore.remove(iter)
325 self.paramtable.foreach(self.remove)
326 self.param.clear()
327 self.update(None)
329 def update(self, widget):
330 self.treestore.clear()
331 for figure in P.matplotlib._pylab_helpers.Gcf.get_all_fig_managers():
332 fig = figure.num
333 iter_fig = self.treestore.append(None, ['Figure %d'%fig, 'figure' ,P.figure(fig), fig, fig])
334 for ind_ax, ax in enumerate(P.figure(fig).axes):
335 iter_ax = self.treestore.append(iter_fig, ['Axe %d'%ind_ax, 'axe' ,ax, fig, ind_ax])
336 for ind_line, line in enumerate(ax.lines):
337 iter = self.treestore.append(iter_ax, ['Line %d'%ind_line, 'line', line, fig, ind_line])
338 for ind_im, im in enumerate(ax.images):
339 iter = self.treestore.append(iter_ax, ['Image %d'%ind_im, 'image',im, fig, ind_im])
340 for ind_text, text in enumerate(ax.texts):
341 iter = self.treestore.append(iter_ax, ['Text %d'%ind_text, 'text', text, fig, ind_text])
342 try:
343 self.fig=fig
344 except:
345 self.window.destroy()
347 def copy(self, widget):
348 model, iter = self.treeview.get_selection().get_selected()
349 type_to_copy = model.get_value(iter, 1)
350 element_to_copy = model.get_value(iter, 2)
351 if type_to_copy == 'figure':
352 return
353 self.wincopy = gtk.Window(gtk.WINDOW_TOPLEVEL)
354 self.wincopy.set_transient_for(self.window)
355 self.wincopy.set_modal(True)
356 self.wincopy.set_title('Copy ' + model.get_value(iter, 0) + ' to...')
357 self.wincopy.set_default_size(200,100)
358 vbox = gtk.VBox()
359 self.wincopy.add(vbox)
360 scroll = gtk.ScrolledWindow()
361 scroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
362 vbox.pack_start(scroll)
363 model_filter = model.filter_new()
364 model_filter.set_visible_func(self.visible_model, type_to_copy)
365 self.treeview_copy = gtk.TreeView(model_filter)
366 renderer = gtk.CellRendererText()
367 column = gtk.TreeViewColumn('Object ID', renderer, text=0)
368 self.treeview_copy.set_headers_visible(False)
369 self.treeview_copy.append_column(column)
370 self.treeview_copy.show()
371 scroll.add(self.treeview_copy)
372 scroll.show()
373 hbox = gtk.HBox()
374 vbox.pack_start(hbox)
375 button = gtk.Button('Cancel')
376 button.connect("clicked", self.close_copy)
377 button.show()
378 hbox.pack_start(button)
379 button = gtk.Button('Copy')
380 button.connect("clicked", self.do_copy, type_to_copy, element_to_copy)
381 button.show()
382 hbox.pack_start(button)
383 hbox.show()
384 vbox.show()
385 self.wincopy.show()
387 def do_copy(self, widget, type, element):
388 model_to, iter_to = self.treeview_copy.get_selection().get_selected()
389 dest = model_to.get_value(iter_to, 2)
390 if type == 'line':
391 line = P.Line2D(element.get_xdata(), element.get_ydata(),
392 linewidth=element.get_linewidth(), linestyle=element.get_linestyle(),
393 color=element.get_color(), marker=element.get_marker())
394 dest.add_line(line)
395 elif type == 'image':
396 dest.images.append(element)
397 elif type == 'text':
398 dest.text(element.get_position()[0], element.get_position()[1],
399 element.get_text(), fontsize=element.get_fontsize())
400 self.close_copy(None)
401 self.update(None)
403 def close_copy(self, widget):
404 self.wincopy.destroy()
406 def visible_model(self, model, iter, data):
407 type = model.get_value(iter, 1)
408 if data == 'axe':
409 if type == 'figure':
410 return True
411 else:
412 return False
413 else:
414 if type == 'figure' or type == 'axe':
415 return True
416 else:
417 return False
419 def end_of_program(self, widget, data=None):
420 if P.backends.backend[:3]=='GTK':
421 self.window.destroy()
422 else:
423 gtk.main_quit()
425 def __init__(self):
426 self.selected=None
427 self.param={}
428 marker_types = ['.', ',', 'o', '^', 'v', '<', '>', 's', '+', 'x',
429 'D', 'd', '1', '2', '3', '4', 'h', 'H',
430 'p', '|', '_', 'None']
431 marker_treestore = gtk.TreeStore(str)
432 for marker in marker_types:
433 marker_treestore.append(None, [marker])
434 line_types = ['-', '--', '-.', ':', 'steps', 'None']
435 line_treestore = gtk.TreeStore(str)
436 [line_treestore.append(None, [ii]) for ii in line_types]
437 self.param_names = {'label': 'Label',
438 'linestyle': 'Line style',
439 'marker': 'Marker',
440 'markersize': 'Marker Size',
441 'markerfacecolor': 'Marker Face Color',
442 'markeredgecolor': 'Marker Edge Color',
443 'markeredgewidth': 'Marker Edge Width',
444 'linewidth': 'Line Width',
445 'color': 'Color',
446 'cmap': 'Colormap',
447 'interp': 'Interpolation',
448 'label': 'Label',
449 'xpos': 'X position',
450 'ypos': 'Y position',
451 'width': 'Width',
452 'height': 'Height',
453 'xmin': 'X min',
454 'xmax': 'X max',
455 'ymin': 'Y min',
456 'ymax': 'Y max',
457 'title': 'Title',
458 'titlefontsize': 'Title fontsize',
459 'xlabel': 'X label',
460 'ylabel': 'Y label',
461 'xlabelfontsize': 'Fontsize (X label)',
462 'ylabelfontsize': 'Fontsize (Y label)',
463 'xfontsize': 'Fontsize (X ticks)',
464 'yfontsize': 'Fontsize (Y ticks)',
465 'figwidth': 'Width',
466 'figheight': 'Height',
467 'textx': 'X position',
468 'texty': 'Y position',
469 'text': 'Text',
470 'textfontsize': 'Fontsize',
471 'textrotation': 'Rotation'}
473 self.param_lines_order = ['label', 'linestyle', 'color', 'linewidth',
474 'marker', 'markersize', 'markerfacecolor', 'markeredgecolor', 'markeredgewidth']
475 self.param_image_order = ['label', 'cmap', 'interp']
476 self.param_axe_order = ['xpos', 'ypos', 'width', 'height',
477 'xmin', 'xmax', 'ymin', 'ymax',
478 'title', 'titlefontsize',
479 'xlabel', 'xlabelfontsize',
480 'ylabel', 'ylabelfontsize',
481 'xfontsize', 'yfontsize']
482 self.param_figure_order = ['figwidth', 'figheight']
483 self.param_text_order = ['textx', 'texty', 'text', 'textfontsize',
484 'color', 'textrotation']
485 self.param_treestore = {'marker': marker_treestore,
486 'linestyle': line_treestore}
488 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
489 self.window.set_title('Graphics properties')
490 self.window.set_default_size(300, 400)
491 self.window.connect("delete_event", self.end_of_program)
492 self.window.connect("destroy", self.end_of_program)
493 vbox = gtk.VBox()
494 hbox = gtk.HBox()
495 self.paramtable = gtk.Table()
496 self.paramtable.show()
497 self.treestore = gtk.TreeStore(gobject.TYPE_STRING, str, object, int, int)
498 self.treeview = gtk.TreeView(self.treestore)
499 self.treeview.set_headers_visible(False)
500 renderer = gtk.CellRendererText()
501 self.column = gtk.TreeViewColumn('Object ID', renderer, text=0)
502 self.treeview.append_column(self.column)
504 self.treeview.connect("cursor-changed", self.row_activated)
505 scroll = gtk.ScrolledWindow()
506 scroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
507 scroll.add(self.treeview)
508 scroll.show()
509 self.update(None)
510 hbox.pack_start(scroll)
512 viewport = gtk.Viewport()
513 viewport.add(self.paramtable)
514 viewport.show()
515 scroll = gtk.ScrolledWindow()
516 scroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
517 scroll.add(viewport)
518 scroll.show()
519 hbox.pack_start(scroll)
521 bottom=gtk.HBox()
522 button = gtk.Button('Update')
523 button.connect("clicked", self.update)
524 button.show()
525 bottom.pack_start(button)
526 button = gtk.Button('Copy')
527 button.connect("clicked", self.copy)
528 button.show()
529 bottom.pack_start(button)
530 button = gtk.Button('Delete')
531 button.connect("clicked", self.delete)
532 button.show()
533 bottom.pack_start(button)
534 bottom.show()
535 button = gtk.Button('Save data')
536 button.connect("clicked", self.save)
537 button.show()
538 bottom.pack_start(button)
539 bottom.show()
540 button = gtk.Button('Apply')
541 button.connect("clicked", self.apply)
542 button.show()
543 bottom.pack_start(button)
544 bottom.show()
546 vbox.pack_start(hbox)
547 vbox.pack_start(bottom, False)
548 self.window.add(vbox)
549 self.treeview.show()
550 hbox.show()
551 vbox.show()
552 self.window.show()
553 gtk.main()