Skip to content

Commit

Permalink
Use proxy objects when accessing MantidPlot objects from Python.
Browse files Browse the repository at this point in the history
Protects against crashes if the windows are closed. Re #1037.
Some methods, objects are not done yet. These are ones that are
probably rarely, if ever, used and will be done tomorrow.
  • Loading branch information
RussellTaylor committed Jan 3, 2012
1 parent 87a32ac commit 6e32d1d
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 16 deletions.
224 changes: 215 additions & 9 deletions Code/Mantid/MantidPlot/mantidplotpy/mantidplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,128 @@

# Overload for consistency with qtiplot table(..) & matrix(..) commands
def workspace(name):
"""Get a handle on a workspace.
Args:
name: The name of the workspace in the Analysis Data Service.
"""
return mtd[name]

def table(name):
"""Get a handle on a table.
Args:
name: The name of the table.
Returns:
A handle to the table.
"""
return proxies.QtProxyObject(qti.app.table(name))

def newTable(name=None,rows=30,columns=2):
"""Create a table.
Args:
name: The name to give to the table (if None, a unique name will be generated).
row: The number of rows in the table (default: 30).
columns: The number of columns in the table (default: 2).
Returns:
A handle to the created table.
"""
if name is None:
return proxies.QtProxyObject(qti.app.newTable())
else:
return proxies.QtProxyObject(qti.app.newTable(name,rows,columns))

def matrix(name):
"""Get a handle on a matrix.
Args:
name: The name of the matrix.
Returns:
A handle to the matrix.
"""
return proxies.QtProxyObject(qti.app.matrix(name))

def newMatrix(name=None,rows=32,columns=32):
"""Create a matrix (N.B. This is not the same as a 'MantidMatrix').
Args:
name: The name to give to the matrix (if None, a unique name will be generated).
row: The number of rows in the matrix (default: 32).
columns: The number of columns in the matrix (default: 32).
Returns:
A handle to the created matrix.
"""
if name is None:
return proxies.QtProxyObject(qti.app.newMatrix())
else:
return proxies.QtProxyObject(qti.app.newMatrix(name,rows,columns))

def graph(name):
"""Get a handle on a graph widget.
Args:
name: The name of the graph window.
Returns:
A handle to the graph.
"""
return proxies.Graph(qti.app.graph(name))

def newGraph(name=None,layers=1,rows=1,columns=1):
"""Create a graph window.
Args:
name: The name to give to the graph (if None, a unique name will be generated).
layers: The number of plots (a.k.a. layers) to put in the graph window (default: 1).
rows: The number of rows of to put in the graph window (default: 1).
columns: The number of columns of to put in the graph window (default: 1).
Returns:
A handle to the created graph widget.
"""
if name is None:
return proxies.Graph(qti.app.newGraph())
else:
return proxies.Graph(qti.app.newGraph(name,layers,rows,columns))

def note(name):
"""Get a handle on a note.
Args:
name: The name of the note.
Returns:
A handle to the note.
"""
return proxies.QtProxyObject(qti.app.note(name))

def newNote(name=None):
"""Create a note.
Args:
name: The name to give to the note (if None, a unique name will be generated).
Returns:
A handle to the created note.
"""
if name is None:
return proxies.QtProxyObject(qti.app.newNote())
else:
return proxies.QtProxyObject(qti.app.newNote(name))

#-----------------------------------------------------------------------------
# Intercept qtiplot "plot" command and forward to plotSpectrum for a workspace
def plot(source, *args, **kwargs):
"""Plot a workspace."""
"""Create a new plot given a workspace, table or matrix."""
if isinstance(source,WorkspaceProxy):
return plotSpectrum(source, *args, **kwargs)
else:
return qti.app.plot(source, *args, **kwargs)
return proxies.Graph(qti.app.plot(source._getHeldObject(), *args, **kwargs))


#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -95,12 +206,113 @@ def stemPlot(source, index, power=None, startPoint=None, endPoint=None):
source = qti.app.mantidUI.workspaceToTable(wsName,wsName,[index],False,True)
# The C++ stemPlot method takes the name of the column, so get that
index = source.colName(2)
elif isinstance(source,proxies.QtProxyObject):
source = source._getHeldObject()
# Get column name if necessary
if isinstance(index, int):
index = source.colName(index)
# Call the C++ method
return qti.app.stemPlot(source,index,power,startPoint,endPoint)

#-----------------------------------------------------------------------------
def waterfallPlot(table, columns):
"""Create a waterfall plot from data in a table.
Args:
table: A reference to the table containing the data to plot
columns: A tuple of the column numbers whose data to plot
Returns:
A handle to the created plot (Layer).
"""
return proxies.Graph(qti.app.waterfallPlot(table._getHeldObject(),columns))

#-----------------------------------------------------------------------------
def importImage(filename):
"""Load an image file into a matrix.
Args:
filename: The name of the file to load.
Returns:
A handle to the matrix containing the image data.
"""
return proxies.QtProxyObject(qti.app.importImage(filename))

#-----------------------------------------------------------------------------
def newPlot3D():
return proxies.Graph3D(qti.app.newPlot3D())

def plot3D(*args):
if isinstance(args[0],str):
return proxies.Graph3D(qti.app.plot3D(*args))
else:
return proxies.Graph3D(qti.app.plot3D(args[0]._getHeldObject(),*args[1:]))

#-----------------------------------------------------------------------------
#-------------------------- Wrapped MantidUI functions -----------------------
#-----------------------------------------------------------------------------

def mergePlots(graph1,graph2):
"""Combine two graphs into a single plot"""
return proxies.Graph(qti.app.mantidUI.mergePlots(graph1._getHeldObject(),graph2._getHeldObject()))

def convertToWaterfall(graph):
"""Convert a graph (containing a number of plotted spectra) to a waterfall plot"""
qti.app.mantidUI.convertToWaterfall(graph._getHeldObject())

def getMantidMatrix(name):
"""Get a handle to the named Mantid matrix"""
return proxies.MantidMatrix(qti.app.mantidUI.getMantidMatrix(name))

def getInstrumentView(name, tab=-1):
"""Create an instrument view window based on the given workspace.
Args:
name: The name of the workspace.
tab: The index of the tab to display initially.
Returns:
A handle to the created instrument view widget.
"""
return proxies.QtProxyObject(qti.app.mantidUI.getInstrumentView(name,tab))

def importMatrixWorkspace(name, firstIndex=None, lastIndex=None, showDialog=False, visible=False):
"""Create a MantidMatrix object from the named workspace.
Args:
name: The name of the workspace.
firstIndex: The workspace index to start at (default: the first one).
lastIndex: The workspace index to stop at (default: the last one).
showDialog: Whether to bring up a dialog to allow options to be entered manually (default: no).
visible: Whether to initially show the created matrix (default: no).
Returns:
A handle to the created matrix.
"""
# Turn the optional arguments into the magic numbers that the C++ expects
if firstIndex is None:
firstIndex = -1
if lastIndex is None:
lastIndex = -1
return proxies.MantidMatrix(qti.app.mantidUI.importMatrixWorkspace(name,firstIndex,lastIndex,showDialog,visible))

def importTableWorkspace(name, visible=False):
"""Create a MantidPlot table from a table workspace.
Args:
name: The name of the workspace.
visible: Whether to initially show the created matrix (default: no).
Returns:
A handle to the newly created table.
"""
return proxies.QtProxyObject(qti.app.mantidUI.importTableWorkspace(name,False,visible))

#-----------------------------------------------------------------------------
#-------------------------- SliceViewer functions ----------------------------
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
def plotSlice(source, label="", xydim=None, slicepoint=None,
colormin=None, colormax=None, colorscalelog=False,
Expand Down Expand Up @@ -190,13 +402,7 @@ def closeAllSliceViewers():

# Ensure these functions are available as without the qti.app.mantidUI prefix
MantidUIImports = [
'importMatrixWorkspace',
'importTableWorkspace',
'getMantidMatrix',
'getInstrumentView',
'getSelectedWorkspaceName',
'mergePlots',
'convertToWaterfall'
'getSelectedWorkspaceName'
]
# Update globals
for name in MantidUIImports:
Expand Down

0 comments on commit 6e32d1d

Please sign in to comment.