casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
asaplotgui.py
Go to the documentation of this file.
00001 """
00002 ASAP plotting class based on matplotlib.
00003 """
00004 
00005 from asap.asaplotbase import *
00006 import Tkinter as Tk
00007 import matplotlib
00008 from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, \
00009         FigureManagerTkAgg
00010 # Force use of the newfangled toolbar.
00011 matplotlib.rcParams['toolbar'] = 'toolbar2'
00012 from matplotlib import _pylab_helpers
00013 from asap.logging import asaplog, asaplog_post_dec
00014 
00015 class asaplotgui(asaplotbase):
00016     """
00017     ASAP plotting class based on matplotlib.
00018     """
00019 
00020     def __init__(self, rows=1, cols=0, title='', size=None, buffering=False):
00021         """
00022         Create a new instance of the ASAPlot plotting class.
00023 
00024         If rows < 1 then a separate call to set_panels() is required to define
00025         the panel layout; refer to the doctext for set_panels().
00026         """
00027         v = vars()
00028         del v['self']
00029 
00030         asaplotbase.__init__(self, **v)
00031         #matplotlib.rcParams["interactive"] = True
00032 
00033         _pylab_helpers.Gcf.destroy(0)
00034         self.window = Tk.Tk()
00035         self.window.protocol("WM_DELETE_WINDOW", self.quit)
00036         self.canvas = FigureCanvasTkAgg(self.figure, master=self.window)
00037         self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
00038         # Simply instantiating this is enough to get a working toolbar.
00039         self.figmgr = FigureManagerTkAgg(self.canvas, 0, self.window)
00040         self.figmgr._cidgcf = None
00041         # Register this plot to matplotlib without activating it
00042         #_pylab_helpers.Gcf.set_active(self.figmgr)
00043         _pylab_helpers.Gcf.figs[self.figmgr.num] = self.figmgr
00044         self._set_window_title('ASAP Plotter - Tk')
00045         self.canvas.show()
00046 
00047 
00048     def map(self):
00049         """
00050         Reveal the ASAPlot graphics window and bring it to the top of the
00051         window stack.
00052         """
00053         if self.is_dead:
00054             raise RuntimeError( "No plotter to show. Not yet plotted or plotter is closed." )
00055         self.window.wm_deiconify()
00056         self.window.lift()
00057 
00058     def quit(self):
00059         """
00060         Destroy the ASAPlot graphics window.
00061         """
00062         self.is_dead = True
00063         if not self.figmgr:
00064             return
00065         #self.window.destroy()
00066         _pylab_helpers.Gcf.destroy(self.figmgr.num)
00067         del self.window, self.canvas, self.figmgr
00068         self.window = None
00069         self.canvas = None
00070         self.figmgr = None
00071 
00072     def show(self, hardrefresh=True):
00073         """
00074         Show graphics dependent on the current buffering state.
00075         """
00076         if self.is_dead:
00077             raise RuntimeError( "No plotter to show (not yet plotted or closed)." )
00078         if not self.buffering:
00079             if hardrefresh:
00080                 asaplotbase.show(self)
00081             self.window.wm_deiconify()
00082             self.canvas.show()
00083 
00084     def terminate(self):
00085         """
00086         Clear the figure.
00087         """
00088         if not self.window:
00089             asaplog.push( "No plotter window to terminate." )
00090             asaplog.post( "WARN" )
00091             return
00092         self.window.destroy()
00093 
00094     def unmap(self):
00095         """
00096         Hide the ASAPlot graphics window.
00097         """
00098         if not self.window:
00099             asaplog.push( "No plotter window to unmap." )
00100             asaplog.post( "WARN" )
00101             return
00102         self.window.wm_withdraw()
00103 
00104     def _set_window_title(self,title):
00105         # Set title to main window title bar
00106         self.window.wm_title(title)