From: <jd...@us...> - 2008-07-03 13:22:14
|
Revision: 5707 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5707&view=rev Author: jdh2358 Date: 2008-07-03 06:21:54 -0700 (Thu, 03 Jul 2008) Log Message: ----------- axes was not calling artist set_axes method in set artist props Modified Paths: -------------- trunk/matplotlib/examples/widgets/menu.py trunk/matplotlib/lib/matplotlib/axes.py trunk/matplotlib/lib/matplotlib/pyplot.py trunk/matplotlib/src/_backend_agg.cpp trunk/matplotlib/src/_backend_agg.h trunk/matplotlib/unit/memleak_hawaii3.py Modified: trunk/matplotlib/examples/widgets/menu.py =================================================================== --- trunk/matplotlib/examples/widgets/menu.py 2008-07-02 15:36:38 UTC (rev 5706) +++ trunk/matplotlib/examples/widgets/menu.py 2008-07-03 13:21:54 UTC (rev 5707) @@ -1,45 +1,76 @@ import numpy as np import matplotlib +import matplotlib.colors as colors import matplotlib.patches as patches import matplotlib.mathtext as mathtext import matplotlib.pyplot as plt import matplotlib.artist as artist import matplotlib.image as image -matplotlib.rc('image', origin='upper') +class ItemProperties: + def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow', alpha=1.0): + self.fontsize = fontsize + self.labelcolor = labelcolor + self.bgcolor = bgcolor + self.alpha = alpha + + self.labelcolor_rgb = colors.colorConverter.to_rgb(labelcolor) + self.bgcolor_rgb = colors.colorConverter.to_rgb(bgcolor) + class MenuItem(artist.Artist): parser = mathtext.MathTextParser("Bitmap") padx = 5 pady =5 - def __init__(self, fig, labelstr): + def __init__(self, fig, labelstr, props=None, hoverprops=None, on_select=None): artist.Artist.__init__(self) + self.set_figure(fig) + self.labelstr = labelstr + if props is None: + props = ItemProperties() - x, self.depth = self.parser.to_rgba( - labelstr, color='black', fontsize=14, dpi=100) - xHover, depth = self.parser.to_rgba( - labelstr, color='white', fontsize=14, dpi=100) + if hoverprops is None: + hoverprops = ItemProperties() + self.props = props + self.hoverprops = hoverprops + + self.on_select = on_select + + x, self.depth = self.parser.to_mask( + labelstr, fontsize=props.fontsize, dpi=fig.dpi) + + if props.fontsize!=hoverprops.fontsize: + raise NotImplementedError('support for different font sizes not implemented') + + self.labelwidth = x.shape[1] self.labelheight = x.shape[0] - print 'h', self.labelheight - self.label = image.FigureImage(fig) - self.label.set_array(x.astype(float)/255.) - self.labelHover = image.FigureImage(fig) - self.labelHover.set_array(xHover.astype(float)/255.) + self.labelArray = np.zeros((x.shape[0], x.shape[1], 4)) + self.labelArray[:,:,-1] = x/255. + self.label = image.FigureImage(fig, origin='upper') + self.label.set_array(self.labelArray) - # we'll update these later - self.rect = patches.Rectangle((0,0), 1,1, facecolor='yellow', alpha=0.2) - self.rectHover = patches.Rectangle((0,0), 1,1, facecolor='blue', alpha=0.2) + self.rect = patches.Rectangle((0,0), 1,1) + self.set_hover_props(False) + fig.canvas.mpl_connect('button_release_event', self.check_select) + def check_select(self, event): + over, junk = self.rect.contains(event) + if not over: + return + + if self.on_select is not None: + self.on_select(self) + def set_extent(self, x, y, w, h): print x, y, w, h self.rect.set_x(x) @@ -47,65 +78,64 @@ self.rect.set_width(w) self.rect.set_height(h) - self.rectHover.set_x(x) - self.rectHover.set_y(y) - self.rectHover.set_width(w) - self.rectHover.set_height(h) - self.label.ox = x+self.padx self.label.oy = y-self.depth+self.pady/2. self.rect._update_patch_transform() - self.rectHover._update_patch_transform() - self.labelHover.ox = x+self.padx - self.labelHover.oy = y-self.depth+self.pady/2. self.hover = False - self.activeRect = self.rect - self.activeLabel = self.label - def draw(self, renderer): - self.activeRect.draw(renderer) - self.activeLabel.draw(renderer) + self.rect.draw(renderer) + self.label.draw(renderer) + def set_hover_props(self, b): + if b: + props = self.hoverprops + else: + props = self.props + + r, g, b = props.labelcolor_rgb + self.labelArray[:,:,0] = r + self.labelArray[:,:,1] = g + self.labelArray[:,:,2] = b + self.label.set_array(self.labelArray) + self.rect.set(facecolor=props.bgcolor, alpha=props.alpha) + def set_hover(self, event): 'check the hover status of event and return true if status is changed' b,junk = self.rect.contains(event) - if b: - self.activeRect = self.rectHover - self.activeLabel = self.labelHover - else: - self.activeRect = self.rect - self.activeLabel = self.label - h = self.hover + changed = (b != self.hover) + + if changed: + self.set_hover_props(b) + + self.hover = b - return b!=h + return changed class Menu: - def __init__(self, fig, labels): + def __init__(self, fig, menuitems): self.figure = fig fig.suppressComposite = True - menuitems = [] - self.numitems = len(labels) - for label in labels: - menuitems.append(MenuItem(fig, label)) self.menuitems = menuitems + self.numitems = len(menuitems) - maxw = max([item.labelwidth for item in menuitems]) maxh = max([item.labelheight for item in menuitems]) totalh = self.numitems*maxh + (self.numitems+1)*2*MenuItem.pady + x0 = 100 y0 = 400 width = maxw + 2*MenuItem.padx height = maxh+MenuItem.pady + for item in menuitems: left = x0 bottom = y0-maxh-MenuItem.pady @@ -122,17 +152,27 @@ def on_move(self, event): draw = False for item in self.menuitems: - b = item.set_hover(event) - draw = b + draw = item.set_hover(event) + if draw: + self.figure.canvas.draw() + break - if draw: - print 'draw' - self.figure.canvas.draw() - fig = plt.figure() -menu = Menu(fig, ('open', 'close', 'save', 'save as', 'quit')) +fig.subplots_adjust(left=0.3) +props = ItemProperties(labelcolor='black', bgcolor='yellow', + fontsize=15, alpha=0.2) +hoverprops = ItemProperties(labelcolor='white', bgcolor='blue', + fontsize=15, alpha=0.2) +menuitems = [] +for label in ('open', 'close', 'save', 'save as', 'quit'): + def on_select(item): + print 'you selected', item.labelstr + item = MenuItem(fig, label, props=props, hoverprops=hoverprops, on_select=on_select) + menuitems.append(item) + +menu = Menu(fig, menuitems) plt.show() Modified: trunk/matplotlib/lib/matplotlib/axes.py =================================================================== --- trunk/matplotlib/lib/matplotlib/axes.py 2008-07-02 15:36:38 UTC (rev 5706) +++ trunk/matplotlib/lib/matplotlib/axes.py 2008-07-03 13:21:54 UTC (rev 5707) @@ -813,8 +813,9 @@ a.set_figure(self.figure) if not a.is_transform_set(): a.set_transform(self.transData) - a.axes = self + a.set_axes(self) + def _gen_axes_patch(self): """ Returns the patch used to draw the background of the axes. It Modified: trunk/matplotlib/lib/matplotlib/pyplot.py =================================================================== --- trunk/matplotlib/lib/matplotlib/pyplot.py 2008-07-02 15:36:38 UTC (rev 5706) +++ trunk/matplotlib/lib/matplotlib/pyplot.py 2008-07-03 13:21:54 UTC (rev 5707) @@ -202,6 +202,14 @@ frameon=frameon, FigureClass=FigureClass, **kwargs) + + # make this figure current on button press event + def make_active(event): + _pylab_helpers.Gcf.set_active(figManager) + + cid = figManager.canvas.mpl_connect('button_press_event', make_active) + figManager._cidgcf = cid + _pylab_helpers.Gcf.set_active(figManager) figManager.canvas.figure.number = num @@ -252,17 +260,21 @@ if len(args)==0: figManager = _pylab_helpers.Gcf.get_active() if figManager is None: return - else: _pylab_helpers.Gcf.destroy(figManager.num) + else: + figManager.canvas.mpl_disconnect(figManager._cidgcf) + _pylab_helpers.Gcf.destroy(figManager.num) elif len(args)==1: arg = args[0] if arg=='all': for manager in _pylab_helpers.Gcf.get_all_fig_managers(): + manager.canvas.mpl_disconnect(manager._cidgcf) _pylab_helpers.Gcf.destroy(manager.num) elif isinstance(arg, int): _pylab_helpers.Gcf.destroy(arg) elif isinstance(arg, Figure): for manager in _pylab_helpers.Gcf.get_all_fig_managers(): if manager.canvas.figure==arg: + manager.canvas.mpl_disconnect(manager._cidgcf) _pylab_helpers.Gcf.destroy(manager.num) else: raise TypeError('Unrecognized argument type %s to close'%type(arg)) Modified: trunk/matplotlib/src/_backend_agg.cpp =================================================================== --- trunk/matplotlib/src/_backend_agg.cpp 2008-07-02 15:36:38 UTC (rev 5706) +++ trunk/matplotlib/src/_backend_agg.cpp 2008-07-03 13:21:54 UTC (rev 5707) @@ -90,6 +90,20 @@ return Py::String(PyString_FromStringAndSize((const char*)data, height*stride), true); } +Py::Object BufferRegion::set_x(const Py::Tuple &args) { + args.verify_length(1); + size_t x = Py::Int( args[0] ); + rect.x1 = x; + return Py::Object(); +} + +Py::Object BufferRegion::set_y(const Py::Tuple &args) { + args.verify_length(1); + size_t y = Py::Int( args[0] ); + rect.y1 = y; + return Py::Object(); +} + Py::Object BufferRegion::to_string_argb(const Py::Tuple &args) { // owned=true to prevent memory leak Py_ssize_t length; @@ -1608,6 +1622,13 @@ behaviors().name("BufferRegion"); behaviors().doc("A wrapper to pass agg buffer objects to and from the python level"); + + add_varargs_method("set_x", &BufferRegion::set_x, + "set_x(x)"); + + add_varargs_method("set_y", &BufferRegion::set_y, + "set_y(y)"); + add_varargs_method("to_string", &BufferRegion::to_string, "to_string()"); add_varargs_method("to_string_argb", &BufferRegion::to_string_argb, Modified: trunk/matplotlib/src/_backend_agg.h =================================================================== --- trunk/matplotlib/src/_backend_agg.h 2008-07-02 15:36:38 UTC (rev 5706) +++ trunk/matplotlib/src/_backend_agg.h 2008-07-03 13:21:54 UTC (rev 5707) @@ -102,6 +102,10 @@ bool freemem; + // set the x and y corners of the rectangle + Py::Object set_x(const Py::Tuple &args); + Py::Object set_y(const Py::Tuple &args); + Py::Object to_string(const Py::Tuple &args); Py::Object to_string_argb(const Py::Tuple &args); static void init_type(void); Modified: trunk/matplotlib/unit/memleak_hawaii3.py =================================================================== --- trunk/matplotlib/unit/memleak_hawaii3.py 2008-07-02 15:36:38 UTC (rev 5706) +++ trunk/matplotlib/unit/memleak_hawaii3.py 2008-07-03 13:21:54 UTC (rev 5707) @@ -2,15 +2,15 @@ import os, sys, time, gc import matplotlib -matplotlib.use('Agg') +matplotlib.use('PDF') from matplotlib.cbook import report_memory -import matplotlib.numerix as nx +import numpy as np from pylab import figure, show, close # take a memory snapshot on indStart and compare it with indEnd -rand = nx.mlab.rand +rand = np.mlab.rand indStart, indEnd = 200, 401 for i in range(indEnd): @@ -19,8 +19,8 @@ fig.clf() - t1 = nx.arange(0.0, 2.0, 0.01) - y1 = nx.sin(2*nx.pi*t1) + t1 = np.arange(0.0, 2.0, 0.01) + y1 = np.sin(2*np.pi*t1) y2 = rand(len(t1)) X = rand(50,50) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |