1
# -*- coding: utf-8 -*-
3
# Copyright © 2009-2010 Pierre Raybaut
4
# Licensed under the terms of the MIT License
5
# (see spyderlib/__init__.py for details)
7
"""Patching matplotlib's FigureManager"""
13
"""Is Matplotlib installed version supported by this patch?"""
15
mpl_ver = matplotlib.__version__.split('.')
16
if int(mpl_ver[0]) < 1 or int(mpl_ver[0]) == 1 and int(mpl_ver[1]) == 0:
17
# Matplotlib <=v1.0 is installed
22
"""Monkey patching matplotlib Qt4 backend figures"""
23
if not is_available():
26
# Warning: do not move these import statements outside this function,
27
# otherwise, PyQt would be imported as soon as this module would be.
28
from spyderlib.qt import is_pyqt46
29
from spyderlib.qt.QtGui import QIcon, QCursor, QInputDialog, QMainWindow
30
from spyderlib.qt.QtCore import Qt, SIGNAL, QObject
32
# Avoid using matplotlib's formlayout version which is not compatible
33
# with PyQt4 API #2 and PySide (at least up to Matplotlib v1.0.1)
34
from spyderlib.widgets import formlayout
35
sys.modules['matplotlib.backends.qt4_editor.formlayout'] = formlayout
36
import matplotlib.backends.qt4_editor
37
matplotlib.backends.qt4_editor.formlayout = formlayout
39
from matplotlib.backends import backend_qt4
41
# Class added to matplotlib to fix a bug with PyQt4 v4.6+
42
class FigureWindow(QMainWindow):
44
super(FigureWindow, self).__init__()
46
def closeEvent(self, event):
47
super(FigureWindow, self).closeEvent(event)
49
self.emit(SIGNAL('destroyed()'))
50
# ****************************************************************
52
# ****************************************************************
53
class FigureManagerQT(backend_qt4.FigureManagerQT):
55
Patching matplotlib...
57
def __init__(self, canvas, num):
61
print 'FigureManagerQT.%s' % backend_qt4.fn_name()
62
backend_qt4.FigureManagerBase.__init__(self, canvas, num)
65
self.window = FigureWindow()
66
self.window.setWindowTitle("Figure %d" % num)
67
self.window.setAttribute(Qt.WA_DeleteOnClose)
70
image = osp.join(matplotlib.rcParams['datapath'],
71
'images', 'matplotlib.png' )
72
self.window.setWindowIcon(QIcon(image))
74
# Give the keyboard focus to the figure instead of the manager
75
self.canvas.setFocusPolicy(Qt.ClickFocus)
76
self.canvas.setFocus()
78
QObject.connect(self.window, SIGNAL('destroyed()'),
79
lambda: self._widgetclosed())
80
self.window._destroying = False
82
self.toolbar = self._get_toolbar(self.canvas, self.window)
83
self.window.addToolBar(self.toolbar)
84
QObject.connect(self.toolbar, SIGNAL("message"),
85
self.window.statusBar().showMessage)
87
self.window.setCentralWidget(self.canvas)
89
if matplotlib.is_interactive():
92
# attach a show method to the figure for pylab ease of use
93
self.canvas.figure.show = lambda *args: self.window.show()
95
def notify_axes_change(fig):
96
# This will be called whenever the current axes is changed
97
if self.toolbar != None: self.toolbar.update()
98
self.canvas.figure.add_axobserver(notify_axes_change)
99
# ****************************************************************
100
backend_qt4.FigureManagerQT = FigureManagerQT
102
# ****************************************************************
103
# * NavigationToolbar2QT
104
# ****************************************************************
106
# This will work with the next matplotlib release:
107
edit_parameters = backend_qt4.NavigationToolbar2QT.edit_parameters
108
# -> Figure options button has already been added by matplotlib
109
except AttributeError:
110
edit_parameters = None
111
# -> Figure options button does not exist yet
113
from spyderlib.widgets.figureoptions import figure_edit
114
class NavigationToolbar2QT(backend_qt4.NavigationToolbar2QT):
115
def _init_toolbar(self):
116
super(NavigationToolbar2QT, self)._init_toolbar()
117
if edit_parameters is None:
118
from spyderlib.config import get_icon
119
a = self.addAction(get_icon("options.svg"),
120
'Customize', self.edit_parameters)
121
a.setToolTip('Edit curves line and axes parameters')
122
def edit_parameters(self):
123
allaxes = self.canvas.figure.get_axes()
124
if len(allaxes) == 1:
126
elif len(allaxes) > 1:
129
title = axes.get_title()
130
ylabel = axes.get_ylabel()
137
text = ylabel+" (%s)"
140
titles.append(text % repr(axes))
141
item, ok = QInputDialog.getItem(self, 'Customize',
142
'Select axes:', titles,
145
axes = allaxes[titles.index(unicode(item))]
150
figure_edit(axes, self)
151
def save_figure(self):
152
super(NavigationToolbar2QT, self).save_figure()
153
def set_cursor(self, cursor):
154
if backend_qt4.DEBUG: print 'Set cursor' , cursor
155
self.parent().setCursor(QCursor(backend_qt4.cursord[cursor]))
156
# ****************************************************************
157
backend_qt4.NavigationToolbar2QT = NavigationToolbar2QT
1
# -*- coding: utf-8 -*-
3
# Copyright © 2009-2010 Pierre Raybaut
4
# Licensed under the terms of the MIT License
5
# (see spyderlib/__init__.py for details)
7
"""Patching matplotlib's FigureManager"""
13
"""Is Matplotlib installed version supported by this patch?"""
15
mpl_ver = matplotlib.__version__.split('.')
16
if int(mpl_ver[0]) < 1 or int(mpl_ver[0]) == 1 and int(mpl_ver[1]) == 0:
17
# Matplotlib <=v1.0 is installed
22
"""Monkey patching matplotlib Qt4 backend figures"""
23
if not is_available():
26
# Warning: do not move these import statements outside this function,
27
# otherwise, PyQt would be imported as soon as this module would be.
28
from spyderlib.qt import is_pyqt46
29
from spyderlib.qt.QtGui import QIcon, QCursor, QInputDialog, QMainWindow
30
from spyderlib.qt.QtCore import Qt, SIGNAL, QObject
32
# Avoid using matplotlib's formlayout version which is not compatible
33
# with PyQt4 API #2 and PySide (at least up to Matplotlib v1.0.1)
34
from spyderlib.widgets import formlayout
35
sys.modules['matplotlib.backends.qt4_editor.formlayout'] = formlayout
36
import matplotlib.backends.qt4_editor
37
matplotlib.backends.qt4_editor.formlayout = formlayout
39
from matplotlib.backends import backend_qt4
41
# Class added to matplotlib to fix a bug with PyQt4 v4.6+
42
class FigureWindow(QMainWindow):
44
super(FigureWindow, self).__init__()
46
def closeEvent(self, event):
47
super(FigureWindow, self).closeEvent(event)
49
self.emit(SIGNAL('destroyed()'))
50
# ****************************************************************
52
# ****************************************************************
53
class FigureManagerQT(backend_qt4.FigureManagerQT):
55
Patching matplotlib...
57
def __init__(self, canvas, num):
61
print 'FigureManagerQT.%s' % backend_qt4.fn_name()
62
backend_qt4.FigureManagerBase.__init__(self, canvas, num)
65
self.window = FigureWindow()
66
self.window.setWindowTitle("Figure %d" % num)
67
self.window.setAttribute(Qt.WA_DeleteOnClose)
70
image = osp.join(matplotlib.rcParams['datapath'],
71
'images', 'matplotlib.png' )
72
self.window.setWindowIcon(QIcon(image))
74
# Give the keyboard focus to the figure instead of the manager
75
self.canvas.setFocusPolicy(Qt.ClickFocus)
76
self.canvas.setFocus()
78
QObject.connect(self.window, SIGNAL('destroyed()'),
79
lambda: self._widgetclosed())
80
self.window._destroying = False
82
self.toolbar = self._get_toolbar(self.canvas, self.window)
83
self.window.addToolBar(self.toolbar)
84
QObject.connect(self.toolbar, SIGNAL("message"),
85
self.window.statusBar().showMessage)
87
self.window.setCentralWidget(self.canvas)
89
if matplotlib.is_interactive():
92
# attach a show method to the figure for pylab ease of use
93
self.canvas.figure.show = lambda *args: self.window.show()
95
def notify_axes_change(fig):
96
# This will be called whenever the current axes is changed
97
if self.toolbar != None: self.toolbar.update()
98
self.canvas.figure.add_axobserver(notify_axes_change)
99
# ****************************************************************
100
backend_qt4.FigureManagerQT = FigureManagerQT
102
# ****************************************************************
103
# * NavigationToolbar2QT
104
# ****************************************************************
106
# This will work with the next matplotlib release:
107
edit_parameters = backend_qt4.NavigationToolbar2QT.edit_parameters
108
# -> Figure options button has already been added by matplotlib
109
except AttributeError:
110
edit_parameters = None
111
# -> Figure options button does not exist yet
113
from spyderlib.widgets.figureoptions import figure_edit
114
class NavigationToolbar2QT(backend_qt4.NavigationToolbar2QT):
115
def _init_toolbar(self):
116
super(NavigationToolbar2QT, self)._init_toolbar()
117
if edit_parameters is None:
118
from spyderlib.config import get_icon
119
a = self.addAction(get_icon("options.svg"),
120
'Customize', self.edit_parameters)
121
a.setToolTip('Edit curves line and axes parameters')
122
def edit_parameters(self):
123
allaxes = self.canvas.figure.get_axes()
124
if len(allaxes) == 1:
126
elif len(allaxes) > 1:
129
title = axes.get_title()
130
ylabel = axes.get_ylabel()
137
text = ylabel+" (%s)"
140
titles.append(text % repr(axes))
141
item, ok = QInputDialog.getItem(self, 'Customize',
142
'Select axes:', titles,
145
axes = allaxes[titles.index(unicode(item))]
150
figure_edit(axes, self)
151
def save_figure(self):
152
super(NavigationToolbar2QT, self).save_figure()
153
def set_cursor(self, cursor):
154
if backend_qt4.DEBUG: print 'Set cursor' , cursor
155
self.parent().setCursor(QCursor(backend_qt4.cursord[cursor]))
156
# ****************************************************************
157
backend_qt4.NavigationToolbar2QT = NavigationToolbar2QT