casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
asaplotgui_qt4.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 PyQt4 as qt
00007 from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg, \
00008         FigureManagerQTAgg
00009 from matplotlib import _pylab_helpers
00010 # Force use of the newfangled toolbar.
00011 import matplotlib
00012 #matplotlib.use("Qt4Agg")
00013 matplotlib.rcParams['toolbar'] = 'toolbar2'
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.canvas = FigureCanvasQTAgg(self.figure)
00035         # Simply instantiating this is enough to get a working toolbar.
00036         self.figmgr = FigureManagerQTAgg(self.canvas, 0)
00037         self.window = self.figmgr.window
00038         self._set_window_title('ASAP Plotter - Qt4')
00039         # Register this plot to matplotlib without activating it
00040         #_pylab_helpers.Gcf.set_active(self.figmgr)
00041         _pylab_helpers.Gcf.figs[self.figmgr.num] = self.figmgr
00042 
00043         #############
00044         ### DO WE HAVE TO DO SOMETHING FOR WINDOW CLOSE CALL?
00045         #############
00046         def dest_callback():
00047             try:
00048                 self.is_dead = True
00049             except NameError:
00050                 pass
00051 
00052         qt.QtCore.QObject.connect(self.window, qt.QtCore.SIGNAL('destroyed()'),dest_callback)
00053 
00054         self.unmap()
00055         #self.canvas.show()
00056 
00057     def map(self):
00058         """
00059         Reveal the ASAPlot graphics window and bring it to the top of the
00060         window stack.
00061         """
00062         if self.is_dead:
00063             raise RuntimeError( "No plotter to show. Not yet plotted or plotter is closed." )
00064         self.window.activateWindow()
00065         #To raise this window to the top of the stacking order
00066         self.window.raise_()
00067         self.window.show()
00068 
00069     def quit(self):
00070         """
00071         Destroy the ASAPlot graphics window.
00072         """
00073         self.is_dead = True
00074         if not self.figmgr:
00075             return
00076         try:
00077             #self.window.close()
00078             # TODO destroy casabar
00079             _pylab_helpers.Gcf.destroy(self.figmgr.num)
00080             del self.window, self.canvas, self.figmgr
00081             self.window = None
00082             self.canvas = None
00083             self.figmgr = None
00084         except RuntimeError: pass # the window may already be closed by user
00085 
00086     def show(self, hardrefresh=True):
00087         """
00088         Show graphics dependent on the current buffering state.
00089         """
00090         if self.is_dead:
00091             raise RuntimeError( "No plotter to show (not yet plotted or closed)." )
00092         if not self.buffering:
00093             if hardrefresh:
00094                 asaplotbase.show(self)
00095             self.window.activateWindow()
00096             self.canvas.draw()
00097             #self.canvas.show()
00098             self.window.show()
00099 
00100     def terminate(self):
00101         """
00102         Clear the figure.
00103         """
00104         if not self.window:
00105             asaplog.push( "No plotter window to terminate." )
00106             asaplog.post( "WARN" )
00107             return
00108         self.window.close()
00109 
00110     def unmap(self):
00111         """
00112         Hide the ASAPlot graphics window.
00113         """
00114         if not self.window:
00115             asaplog.push( "No plotter window to unmap." )
00116             asaplog.post( "WARN" )
00117             return
00118         self.window.hide()
00119 
00120     def _set_window_title(self,title):
00121         # Set title to main window title bar
00122         self.window.setWindowTitle(title)