Commit a9619c64 authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

Rewrote myWin as three different classes depending on the backend changed the...

Rewrote myWin as three different classes depending on the backend changed the imports in __init__ to make it cleaner.
parent 2d43ea16
......@@ -48,43 +48,7 @@ from matplotlib import is_interactive
from matplotlib import rcParams
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']))
rcParams['backend'] = u'TkAgg'
# myplotlib
from myData import MyData
......@@ -143,7 +107,14 @@ from myIOs import readStupidData, readStupidData2
from myFig import MyFig
# MyFig: Overlay on matplotlib.FigureManager class
from myWin import MyWin
if rcParams['backend'] == u'TkAgg':
from myWin_TkAgg import MyWin_TkAgg as MyWin
elif rcParams['backend'] == u'GTKAgg':
from myWin_GTKAgg import MyWin_GTKAgg as MyWin
elif rcParams['backend'] == u'WXAgg':
from myWin_WXAgg import MyWin_WXAgg as MyWin
else:
print(SEVR + "The backend you choosed is not supported interactive mode not available")
# myTool.*: interface functions to use myplotlib interactively
from myTool import print2file, print2screen, printListCurrentWindows
......
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
# myplotlib v1.0.1,
#
# @file myWin_GTKAgg.py
# @author Yori 'AGy' Fournier
# @licence CC-BY-SA
#
# MyWin_GTKAgg class: Overlay of matplotlib FigureManager
# class. It allows a cleaner and more confortable
# usage of windows.
#
# @Class MyWin
#
# @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.
#
# @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_GTKAgg class for the myplotlib module.
#
# ======================================================================
#
# IMPORT ---------------------------------------------------------------
from myplotlib import INFO, SEVR, WARN, DBUG, SPCE
from myplotlib import rcParams
from myplotlib import _G_WINDOWS
from myplotlib import np
# For the GTKAgg backend
from matplotlib.backends.backend_gtkagg import FigureManagerGTKAgg, FigureCanvasGTKAgg
# Class MyWin Overwriting the disgusting Matplotlib.FigureManager class
class MyWin_GTKAgg(FigureManagerGTKAgg):
# CONSTRUCTOR ------------------------------------------------------
def __init__(self, fig, show=True, *args, **kwargs):
# set the dpi to 75 (correct value for screen)
if(fig.dpi != 75.):
fig.dpi = 75.
# 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[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win.close()
# Call FigureManagerGTK class
# (already wondering if this is not a mistake...)
if(debug):
print(INFO + "Used GTKAgg backend.")
FigureManagerGTKAgg.__init__(self, FigureCanvasGTKAgg(fig), num)
# bound the figure to the window
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = num
# add the window in the global variable _G_WINDOWS
# this guarenty that you never loose a window
# (see myTool.py getWindow(num))
_G_WINDOWS.append(self)
# if show is on, then show the window
if show:
# For GTKAgg and TKAgg ---------------------------------
self.refresh()
self.show()
# BOUND ------------------------------------------------------------
def _boundFigure_(self, fig):
fig.canvas = self.canvas
self.canvas.figure = fig
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = self.num
# UNBOUND ----------------------------------------------------------
def _unboundFigure_(self):
self.canvas.figure.boundedToWin = False
self.canvas.figure.keywords['fignum'] = -1
# SETTERS ----------------------------------------------------------
# SET FIGURE -------------------------------------------------------
def set_figure(self, fig):
# first unbound the former figure
self._unboundFigure_()
# if the new figure is already bounded close
# its former container.
if fig.boundedToWin:
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win.close()
# bound the new figure
self._boundFigure_(fig)
# REFRESH ----------------------------------------------------------
def refresh(self):
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
# DRAW -------------------------------------------------------------
def drawFigure(self, fig):
# if the figure is not the current one
if(fig.keywords['fignum'] != self.num):
# cleanly set the new figure
self.set_figure(fig)
# and refresh
self.refresh()
# CLOSE ------------------------------------------------------------
def close(self):
self._unboundFigure_()
self.destroy()
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
# and therefore not yet freed
# del win would free it entirely.
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
# myplotlib v1.0.1,
#
# @file myWin_TkAgg.py
# @author Yori 'AGy' Fournier
# @licence CC-BY-SA
#
# MyWin_TkAgg class: Overlay of matplotlib FigureManagerTkAgg
# class. It allows a cleaner and more confortable usage of windows
# with the TkAgg Backend.
#
# @Class MyWin_TkAgg
#
# @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.
#
# @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_TkAgg class for the myplotlib module.
#
# ======================================================================
#
# IMPORT ---------------------------------------------------------------
from myplotlib import INFO, SEVR, WARN, DBUG, SPCE
from myplotlib import rcParams
from myplotlib import _G_WINDOWS
from myplotlib import np
# For the backend
from matplotlib.backends.backend_tkagg import FigureManagerTkAgg, FigureCanvasTkAgg
from matplotlib.externals import six
from matplotlib.externals.six.moves import tkinter as Tk
# Class MyWin Overwriting the disgusting Matplotlib.FigureManager class
class MyWin_TkAgg(FigureManagerTkAgg):
# CONSTRUCTOR ------------------------------------------------------
def __init__(self, fig, show=True, *args, **kwargs):
# set the dpi to 75 (correct value for screen)
if(fig.dpi != 75.):
fig.dpi = 75.
# 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[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win.close()
# Call FigureManagerTkAgg class
# (already wondering if this is not a mistake...)
if(debug):
print(INFO + "Used TkAgg backend")
# Create the master app
window = Tk.Tk()
window.withdraw()
# create the canvas
canvas = FigureCanvasTkAgg(fig, master=window)
# create the Figure manager (what we call window)
FigureManagerTkAgg.__init__(self, canvas, num, window)
# bound the figure to the window
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = num
# add the window in the global variable _G_WINDOWS
# this guarenty that you never loose a window
# (see myTool.py getWindow(num))
_G_WINDOWS.append(self)
# if show is on, then show the window
if show:
self.refresh()
self.show()
# BOUND ------------------------------------------------------------
def _boundFigure_(self, fig):
fig.canvas = self.canvas
self.canvas.figure = fig
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = self.num
# UNBOUND ----------------------------------------------------------
def _unboundFigure_(self):
self.canvas.figure.boundedToWin = False
self.canvas.figure.keywords['fignum'] = -1
# SETTERS ----------------------------------------------------------
# SET FIGURE -------------------------------------------------------
def set_figure(self, fig):
# first unbound the former figure
self._unboundFigure_()
# if the new figure is already bounded close
# its former container.
if fig.boundedToWin:
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win.close()
# bound the new figure
self._boundFigure_(fig)
# REFRESH ----------------------------------------------------------
def refresh(self):
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
# DRAW -------------------------------------------------------------
def drawFigure(self, fig):
# if the figure is not the current one
if(fig.keywords['fignum'] != self.num):
# cleanly set the new figure
self.set_figure(fig)
# and refresh
self.refresh()
# CLOSE ------------------------------------------------------------
def close(self):
self._unboundFigure_()
self.destroy()
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
# and therefore not yet freed
# del win would free it entirely.
......@@ -5,7 +5,7 @@
#
# myplotlib v1.0.1,
#
# @file myWin.py
# @file myWin_WXAgg.py
# @author Yori 'AGy' Fournier
# @licence CC-BY-SA
#
......@@ -13,7 +13,7 @@
# class. It allows a cleaner and more confortable
# usage of windows.
#
# @Class MyWin
# @Class MyWin_WXAgg
#
# @Constructor
#
......@@ -56,27 +56,16 @@ from myplotlib import rcParams
from myplotlib import _G_WINDOWS
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)
# For the WXAgg backend
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.backends.backend_wx import FigureManagerWx
from matplotlib.backends.backend_wx import _create_wx_app
from matplotlib.backends.backend_wxagg import FigureFrameWxAgg
# Class MyWin Overwriting the disgusting Matplotlib.FigureManager class
class MyWin(FigureManager):
class MyWin_WXAgg(FigureManagerWx):
# CONSTRUCTOR ------------------------------------------------------
def __init__(self, fig, show=True, *args, **kwargs):
......@@ -109,7 +98,7 @@ class MyWin(FigureManager):
# 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))
......@@ -121,35 +110,23 @@ class MyWin(FigureManager):
win = _G_WINDOWS[np.where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
win.close()
# Call the ugly FigureManagerGTK class
# Call the ugly FigureManagerWxAgg class
# (already wondering if this is not a mistake...)
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")
if(debug):
print(INFO + "Used WXAgg backend")
# create master app
_create_wx_app()
# create the frame
frame = FigureFrameWxAgg(num, fig)
# create the figManager (our window)
FigureManagerWx.__init__(self, frame.canvas, num, frame)
# required for wxagg
self.frame.figmgr = self
# bound the figure to the window
self.canvas.figure.boundedToWin = True
self.canvas.figure.keywords['fignum'] = num
......@@ -161,16 +138,10 @@ class MyWin(FigureManager):
# if show is on, then show the window
if 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()
self.canvas.figure.clf()
self.canvas.figure.plot()
self.show()
self.canvas.draw()
# BOUND ------------------------------------------------------------
......@@ -208,19 +179,12 @@ class MyWin(FigureManager):
# REFRESH ----------------------------------------------------------
def refresh(self):
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()
self.canvas.figure.clf()
self.canvas.figure.plot()
self.canvas.draw()
self.canvas.figure.plot()
self.canvas.draw()
# DRAW -------------------------------------------------------------
......
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