From: <jd...@us...> - 2008-04-29 21:10:45
|
Revision: 5099 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5099&view=rev Author: jdh2358 Date: 2008-04-29 14:10:44 -0700 (Tue, 29 Apr 2008) Log Message: ----------- added support for a editable recarray widget in gtk Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/mlab.py trunk/matplotlib/lib/mpl_toolkits/gtktools.py Modified: trunk/matplotlib/lib/matplotlib/mlab.py =================================================================== --- trunk/matplotlib/lib/matplotlib/mlab.py 2008-04-29 15:14:36 UTC (rev 5098) +++ trunk/matplotlib/lib/matplotlib/mlab.py 2008-04-29 21:10:44 UTC (rev 5099) @@ -2384,6 +2384,8 @@ def toval(self, x): return str(x) + def fromstr(self, s): + return s class FormatString(FormatObj): def tostr(self, x): @@ -2402,6 +2404,7 @@ if x is None: return 'None' return self.fmt%self.toval(x) + class FormatFloat(FormatFormatStr): def __init__(self, precision=4, scale=1.): FormatFormatStr.__init__(self, '%%1.%df'%precision) @@ -2413,10 +2416,17 @@ x = x * self.scale return x + def fromstr(self, s): + return float(s)/self.scale + + class FormatInt(FormatObj): def toval(self, x): return x + def fromstr(self, s): + return int(s) + class FormatPercent(FormatFloat): def __init__(self, precision=4): FormatFloat.__init__(self, precision, scale=100.) @@ -2425,6 +2435,7 @@ def __init__(self, precision=4): FormatFloat.__init__(self, precision, scale=1e-3) + class FormatMillions(FormatFloat): def __init__(self, precision=4): FormatFloat.__init__(self, precision, scale=1e-6) @@ -2438,11 +2449,21 @@ if x is None: return 'None' return x.strftime(self.fmt) + def fromstr(self, x): + import dateutil.parser + return dateutil.parser.parse(x).date() + class FormatDatetime(FormatDate): def __init__(self, fmt='%Y-%m-%d %H:%M:%S'): FormatDate.__init__(self, fmt) + def fromstr(self, x): + import dateutil.parser + return dateutil.parser.parse(x) + + + defaultformatd = { np.int16 : FormatInt(), np.int32 : FormatInt(), Modified: trunk/matplotlib/lib/mpl_toolkits/gtktools.py =================================================================== --- trunk/matplotlib/lib/mpl_toolkits/gtktools.py 2008-04-29 15:14:36 UTC (rev 5098) +++ trunk/matplotlib/lib/mpl_toolkits/gtktools.py 2008-04-29 21:10:44 UTC (rev 5099) @@ -8,7 +8,7 @@ import matplotlib.mlab as mlab import mpl_toolkits.gtktools as gtktools - + r = mlab.csv2rec('somefile.csv', checkrows=0) formatd = dict( @@ -46,7 +46,7 @@ cell = None """ - + if format is None: return None format = copy.copy(format) format.xalign = 0. format.cell = None @@ -148,6 +148,8 @@ column = gtk.TreeViewColumn(header, renderer, text=i) renderer.set_property('xalign', formatter.xalign) + renderer.set_property('editable', True) + renderer.connect("edited", self.position_edited, i) column.connect('clicked', Clicked(self, i)) column.set_property('clickable', True) @@ -164,6 +166,10 @@ self.treeview = treeview self.clear() + def position_edited(self, renderer, path, newtext, position): + #print path, position + self.model[path][position] = newtext + def clear(self): self.iterd = dict() self.iters = [] # an ordered list of iters @@ -291,9 +297,108 @@ if autowin: win = gtk.Window() win.set_default_size(800,600) + #win.set_geometry_hints(scroll) win.add(scroll) win.show_all() scroll.win = win return scroll + +class RecListStore(gtk.ListStore): + """ + A liststore as a model of an editable record array. + + attributes: + + * r - the record array with the edited values + + * callbacks - a matplotlib.cbook.CallbackRegistry. Connect to the cell_changed with + + def mycallback(liststore, rownum, colname, oldval, newval): + print 'verify: old=%s, new=%s, rec=%s'%(oldval, newval, liststore.r[rownum][colname]) + + cid = liststore.callbacks.connect('cell_changed', mycallback) + + """ + def __init__(self, r, formatd=None): + if formatd is None: + formatd = mlab.get_formatd(r) + + self.callbacks = cbook.CallbackRegistry(['cell_changed']) + + self.r = r + gtk.ListStore.__init__(self, *([gobject.TYPE_STRING]*len(r.dtype))) + self.headers = r.dtype.names + self.formats = [gtkformat_factory(formatd.get(name, mlab.FormatObj()),i) + for i,name in enumerate(self.headers)] + for row in r: + self.append([func.tostr(val) for func, val in zip(self.formats, row)]) + + + def position_edited(self, renderer, path, newtext, position): + self[path][position] = newtext + format = self.formats[int(position)] + + rownum = int(path) + colname = self.headers[int(position)] + oldval = self.r[rownum][colname] + newval = format.fromstr(newtext) + self.r[rownum][colname] = newval + self.callbacks.process('cell_changed', self, rownum, colname, oldval, newval) + +def editable_recarray(r, formatd=None): + """ + return a (gtk.TreeView, RecListStore) from record array t and + format dictionary formatd where the keys are record array dtype + names and the values are matplotlib.mlab.FormatObj instances + + Example: + + formatd = mlab.get_formatd(r) + formatd['date'] = mlab.FormatDate('%Y-%m-%d') + formatd['volume'] = mlab.FormatMillions(precision=1) + + treeview, liststore = gtktools.editable_recarray(r, formatd=formatd) + + def mycallback(liststore, rownum, colname, oldval, newval): + print 'verify: old=%s, new=%s, rec=%s'%(oldval, newval, liststore.r[rownum][colname]) + + liststore.callbacks.connect('cell_changed', mycallback) + + + win = gtk.Window() + win.show() + win.connect('destroy', lambda x: gtk.main_quit()) + win.add(treeview) + gtk.main() + + """ + liststore = RecListStore(r, formatd=formatd) + treeview = gtk.TreeView() + if formatd is None: + formatd = mlab.get_formatd(r) + for i, header in enumerate(liststore.headers): + renderer = gtk.CellRendererText() + renderer.connect("edited", liststore.position_edited, i) + renderer.set_property('editable', True) + + formatter = gtkformat_factory(formatd.get(header), i) + + if formatter is not None: + renderer.set_property('xalign', formatter.xalign) + + tvcol = gtk.TreeViewColumn(header) + treeview.append_column(tvcol) + tvcol.pack_start(renderer, True) + tvcol.add_attribute(renderer, 'text', i) + if formatter is not None and formatter.cell is not None: + tvcol.set_cell_data_func(renderer, formatter.cell) + + + treeview.set_model(liststore) + treeview.show() + treeview.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_BOTH) + + + return treeview, liststore This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |