Commit 2d43ea16 authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

Now the obect oriented version is compatible with WXAGG, GTKAGG and TKAGG

parent d12041f4
......@@ -46,10 +46,45 @@ from matplotlib.axes import Axes
from matplotlib.figure import Figure
from matplotlib import is_interactive
from matplotlib import rcParams
# For interactive usage (require GTKAgg !) NOT YET COMPATIBLE WITH OTHER BACKENDS
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
from matplotlib.backends.backend_gtkagg import FigureManagerGTKAgg as FigureManager
from matplotlib import use
#use('GTKAgg')
rcParams['backend'] = u'WXAgg'
if rcParams['backend'] == u'GTKAgg':
print(" > info-: You ask to use GTKAgg")
try:
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
from matplotlib.backends.backend_gtkagg import FigureManagerGTKAgg as FigureManager
# FigureFrameWxAgg = None
# _create_wx_app = None
except:
print(" > sevr-: Apparently the backend GTKAgg is not available")
exit(1)
elif rcParams['backend'] == u'WXAgg':
print(" > info-: You ask to use WXAgg")
try:
from matplotlib.backends.backend_wxagg import FigureFrameWxAgg
from matplotlib.backends.backend_wx import _create_wx_app
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import FigureManagerWx as FigureManager
except:
print(" > sevr-: Apparently the backend WXAgg is not available")
exit(1)
elif rcParams['backend'] == u'TkAgg':
print(" > info-: You ask to use TkAgg")
try:
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg as FigureCanvas
from matplotlib.backends.backend_tkagg import FigureManagerTkAgg as FigureManager
except:
print(" > sevr-: Apparently the backend TkAgg is not available")
exit(1)
else:
print(" > sevr-: You asked a backend that is not yet supported.")
print(" > -----: " + str(rcParams['backend']))
# myplotlib
from myData import MyData
......@@ -63,7 +98,7 @@ SPCE = " > -----: "
# CONFIGURATION --------------------------------------------------------
# myFig
D_FIGNUM = -1 # default figure number
D_FIGNUM = 0 # default figure number
D_FIGSIZE = (8., 6.) # default figure size
D_REFORMAT = True # default reformat value
D_FORMATTED = False # default formatted value
......@@ -111,7 +146,9 @@ from myFig import MyFig
from myWin import MyWin
# myTool.*: interface functions to use myplotlib interactively
from myTool import print2file
from myTool import print2file, print2screen, printListCurrentWindows
from myTool import getWindow, getFigOnWindow, drawFigOnWindow
from myTool import closeWindow, closeAllWindows
# MyFig4Test: Overlay of Figure for testing
from myFig4Test import MyFig4Test
......
......@@ -65,14 +65,10 @@
#
#
# IMPORT ---------------------------------------------------------------
# Default values
from myplotlib import D_FIGSIZE, D_INPUTARG, D_DEBUG, D_REFORMAT, D_FORMATTED
# Forms
from myplotlib import DBUG, SEVR, INFO, SPCE, WARN
# Global variables
from myplotlib import G_RAWDATAS
# Classes
from myplotlib import Figure, FigureCanvas, MyAxes
from myplotlib import Figure, MyAxes
# Class MyFig Overwriting Matplotlib.figure.Figure
......
......@@ -15,19 +15,12 @@
#
# @functions
#
# - myfig: this is a wrapper around the fonction figure of
# matplotlib, to allow an easy usage of the ihnereted
# user-defined classes of type Figure.
#
# - setCurrentData: This tool is meant for interactive work,
# it set the given data identifier as being
# the current one such that it can be called
# with its identifier and the default
# one: G_RAWDATA['current'].
#
# - screen: This is the core function of myplotlib. It plots a
# given Figure-like class on the screen.
#
# - print2file: This function is meant to be used in script
# mode of myplotlib, more than in interactive mode,
# eventhough it can be used in both. It will save
......@@ -42,11 +35,13 @@
#
# IMPORT ---------------------------------------------------------------
from myplotlib import D_OPATH, D_OFORMAT
from myplotlib import G_RAWDATAS
from myplotlib import G_RAWDATAS, _G_WINDOWS
from myplotlib import INFO, SEVR, WARN, SPCE
from myplotlib import ion, ioff, is_interactive
from myplotlib import MyWin, MyFig
from myplotlib import np
......@@ -63,7 +58,80 @@ def setCurrentData(name):
return(True)
#
# PRINT TO SCREEN ------------------------------------------------------
def print2screen(ClassName, inputArg, *args, **kwargs):
try:
fig = ClassName(inputarg=inputArg, *args, **kwargs)
except:
print(SEVR + 'The type of figure you gave does not exists.')
return(False)
try:
MyWin(fig, *args, **kwargs)
except:
print(SEVR + "I couldn't draw the figure on a window... did you test the lib?")
return(False)
return(True)
def printListCurrentWindows():
for window in _G_WINDOWS:
if window == _G_WINDOWS[0]:
print(INFO + "Figure " + window.num)
else:
print(SPCE + "Figure " + window.num)
def getWindow(ID):
for window in _G_WINDOWS:
if window.num == ID:
return(window)
print(WARN + "The window does not exist.")
return(False)
def getFigOnWindow(ID):
for window in _G_WINDOWS:
if window.num == ID:
return(window.canvas.figure)
print(WARN + "The window does not exist.")
return(False)
def drawFigOnWindow(fig, ID):
return(MyWin(fig, fignum=ID))
def closeWindow(ID):
for window in _G_WINDOWS:
if window.num == ID:
return(window.close())
def closeAllWindows():
# a for-loop won't work because close() acts on _G_WINDOWS
# for window in _G_WINDOWS:
while(len(_G_WINDOWS) > 0):
window = _G_WINDOWS[0]
print(INFO + "window :" + str(window.num))
window.close()
if(len(_G_WINDOWS) == 0):
return(True)
else:
return(False)
# PRINT TO FILE --------------------------------------------------------
def print2file(ClassName, inputArg, filename,
opath=D_OPATH, oformat=D_OFORMAT,
......
......@@ -17,15 +17,33 @@
#
# @Constructor
#
# fig: the MyFig object that need to be drawn
# show: a flag to show the window just after
# creation. (default: True)
# fignum: number of both the Window and the Figure.
# debug: flag for debugging, more verbatile.
# (kw)args: user defined arguments and keywords.
# fig: the MyFig object that need to be drawn
# show: a flag to show the window just after
# creation. (default: True)
# fignum: number of both the Window and the Figure.
# debug: flag for debugging, more verbatile.
# (kw)args: user defined arguments and keywords.
#
# @section Functions
#
# - _boundFigure_: bound a figure to the window (priv.)
#
# - _unboundFigure_: unbound a figure to the window (priv.)
#
# - set_figure: a clean way of setting a new figure
# to the window.
#
# - refresh: redraw the window
# - clean the figure
# - plot the figure
# - draw the canvas
#
# - drawFigure: draw a given figure on the window
# - set the figure of needed
# - refresh
#
# - close: close the window
#
# @section History
#
# v 1.0.1 - MyWin class for the myplotlib module.
......@@ -33,14 +51,27 @@
# ======================================================================
#
# IMPORT ---------------------------------------------------------------
# Forms
from myplotlib import INFO, SEVR, WARN, DBUG, SPCE
# Classes
from myplotlib import FigureManager, FigureCanvas
# Global variables
from myplotlib import rcParams
from myplotlib import _G_WINDOWS
# Module
from myplotlib.np import where
from myplotlib import np
# For the various backends
from myplotlib import FigureManager, FigureCanvas
if rcParams['backend'] == u'WXAgg':
try:
from matplotlib.backends.backend_wx import _create_wx_app
from matplotlib.backends.backend_wxagg import FigureFrameWxAgg
except ImportError:
print(SEVR + "Apparently backend_wx[agg] are missing.")
exit(1)
elif rcParams['backend'] == u'TkAgg':
try:
from matplotlib.externals import six
from matplotlib.externals.six.moves import tkinter as Tk
except ImportError:
print(SEVR + "Apparently backend_tkagg is missing.")
exit(1)
# Class MyWin Overwriting the disgusting Matplotlib.FigureManager class
......@@ -53,23 +84,72 @@ class MyWin(FigureManager):
if(fig.dpi != 75.):
fig.dpi = 75.
# Get the user specified ID of the window of setit to -1
num = kwargs.get('fignum', -1)
# Get the debug value
debug = kwargs.get('debug', False)
# Get the user specified ID of the window or set it to 0
if 'fignum' in kwargs.keys():
num = kwargs['fignum']
fignumSpecifiedByUser = True
else:
num = 0
fignumSpecifiedByUser = False
# if ID already used
if(num in [(window.num) for window in _G_WINDOWS]):
# if user specified fignum then close former window and create new one
if(fignumSpecifiedByUser):
if(debug):
print(DBUG + "The fignum you specified already exists, I'll close the former window")
# Close the window with the specified number
_G_WINDOWS[np.where([(window.num == num) for window in _G_WINDOWS])[0][0]].close()
else:
# If user didn't specified a fignum
# increase ID until found a free one
while(num in [(window.num) for window in _G_WINDOWS]):
num = num + 1
# If debug print the ID of the window
if(debug):
print(DBUG + "You choosed num = " + str(num))
# before creating anything verify that the fig is not already
# bounded to another window
if fig.boundedToWin:
# if it is the case close the bounded window.
win = _G_WINDOWS[where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win.close()
# Call the ugly FigureManager class
# Call the ugly FigureManagerGTK class
# (already wondering if this is not a mistake...)
FigureManager.__init__(self, FigureCanvas(fig), num)
if rcParams['backend'] == u'GTKAgg':
try:
print(INFO + "GTKAgg")
FigureManager.__init__(self, FigureCanvas(fig), num)
# this will work if gtk is loaded not if wxPython is the current backend
except:
print(SEVR + "Problem with GTKAgg")
elif rcParams['backend'] == u'WXAgg':
try:
print(INFO + "WXAgg")
# Yes it is ugly! Not my fault.
_create_wx_app()
frame = FigureFrameWxAgg(num, fig)
FigureManager.__init__(self, frame.canvas, num, frame)
self.frame.figmgr = self
except:
print(SEVR + "Problem with WXAgg")
elif rcParams['backend'] == u'TkAgg':
try:
print(INFO + "TkAgg")
window = Tk.Tk()
window.withdraw()
canvas = FigureCanvas(fig, master=window)
FigureManager.__init__(self, canvas, num, window)
except:
print(SEVR + "Problem with TkAgg")
# bound the figure to the window
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = num
......@@ -81,8 +161,16 @@ class MyWin(FigureManager):
# if show is on, then show the window
if show:
self.refresh()
self.show()
if rcParams['backend'] in (u'GTKAgg', u'TkAgg'):
# For GTKAgg and TKAgg ---------------------------------
self.refresh()
self.show()
elif rcParams['backend'] == u'WXAgg':
# For WXAgg --------------------------------------------
self.canvas.figure.clf()
self.canvas.figure.plot()
self.show()
self.canvas.draw()
# BOUND ------------------------------------------------------------
......@@ -111,7 +199,7 @@ class MyWin(FigureManager):
# if the new figure is already bounded close
# its former container.
if fig.boundedToWin:
win = _G_WINDOWS[where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win.close()
# bound the new figure
......@@ -120,10 +208,19 @@ class MyWin(FigureManager):
# REFRESH ----------------------------------------------------------
def refresh(self):
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
if rcParams['backend'] in (u'GTKAgg', u'TkAgg'):
# For GTKAgg and TKAgg ---------------------------------
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
elif rcParams['backend'] == u'WXAgg':
# For WXAgg --------------------------------------------
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
self.canvas.figure.plot()
self.canvas.draw()
# DRAW -------------------------------------------------------------
......@@ -143,7 +240,7 @@ class MyWin(FigureManager):
self._unboundFigure_()
self.destroy()
del _G_WINDOWS[where([(window == self) for window in _G_WINDOWS])[0][0]]
del _G_WINDOWS[np.where([(window == self) for window in _G_WINDOWS])[0][0]]
# remark if you create the window like:
# win = MyWin(fig)
# then win is still refering the self
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment