~ubuntu-branches/debian/experimental/spyder/experimental

« back to all changes in this revision

Viewing changes to spyderlib/mpl_patch.py

  • Committer: Package Import Robot
  • Author(s): Picca Frédéric-Emmanuel
  • Date: 2013-01-20 12:19:54 UTC
  • mfrom: (1.1.16)
  • Revision ID: package-import@ubuntu.com-20130120121954-1jt1xa924bshhvh0
Tags: 2.2.0~beta1+dfsg-2
fix typo ipython-qtconsol -> ipython-qtconsole

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
#
3
 
# Copyright © 2009-2010 Pierre Raybaut
4
 
# Licensed under the terms of the MIT License
5
 
# (see spyderlib/__init__.py for details)
6
 
 
7
 
"""Patching matplotlib's FigureManager"""
8
 
 
9
 
import sys
10
 
 
11
 
 
12
 
def is_available():
13
 
    """Is Matplotlib installed version supported by this patch?"""
14
 
    import matplotlib
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
18
 
        return True
19
 
 
20
 
 
21
 
def apply():
22
 
    """Monkey patching matplotlib Qt4 backend figures"""
23
 
    if not is_available():
24
 
        return
25
 
 
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
31
 
    
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
38
 
    
39
 
    from matplotlib.backends import backend_qt4
40
 
    
41
 
    # Class added to matplotlib to fix a bug with PyQt4 v4.6+
42
 
    class FigureWindow(QMainWindow):
43
 
        def __init__(self):
44
 
            super(FigureWindow, self).__init__()
45
 
            
46
 
        def closeEvent(self, event):
47
 
            super(FigureWindow, self).closeEvent(event)
48
 
            if is_pyqt46:
49
 
                self.emit(SIGNAL('destroyed()'))
50
 
    # ****************************************************************
51
 
    # *  FigureManagerQT
52
 
    # ****************************************************************
53
 
    class FigureManagerQT(backend_qt4.FigureManagerQT):
54
 
        """
55
 
        Patching matplotlib...
56
 
        """
57
 
        def __init__(self, canvas, num):
58
 
            import matplotlib
59
 
            
60
 
            if backend_qt4.DEBUG:
61
 
                print 'FigureManagerQT.%s' % backend_qt4.fn_name()
62
 
            backend_qt4.FigureManagerBase.__init__(self, canvas, num)
63
 
            self.canvas = canvas
64
 
            
65
 
            self.window = FigureWindow()
66
 
            self.window.setWindowTitle("Figure %d" % num)
67
 
            self.window.setAttribute(Qt.WA_DeleteOnClose)
68
 
 
69
 
            import os.path as osp
70
 
            image = osp.join(matplotlib.rcParams['datapath'],
71
 
                             'images', 'matplotlib.png' )
72
 
            self.window.setWindowIcon(QIcon(image))
73
 
    
74
 
            # Give the keyboard focus to the figure instead of the manager
75
 
            self.canvas.setFocusPolicy(Qt.ClickFocus)
76
 
            self.canvas.setFocus()
77
 
    
78
 
            QObject.connect(self.window, SIGNAL('destroyed()'),
79
 
                            lambda: self._widgetclosed())
80
 
            self.window._destroying = False
81
 
    
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)
86
 
    
87
 
            self.window.setCentralWidget(self.canvas)
88
 
    
89
 
            if matplotlib.is_interactive():
90
 
                self.window.show()
91
 
    
92
 
            # attach a show method to the figure for pylab ease of use
93
 
            self.canvas.figure.show = lambda *args: self.window.show()
94
 
    
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
101
 
    
102
 
    # ****************************************************************
103
 
    # *  NavigationToolbar2QT
104
 
    # ****************************************************************
105
 
    try:
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
112
 
        
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:
125
 
                axes = allaxes[0]
126
 
            elif len(allaxes) > 1:
127
 
                titles = []
128
 
                for axes in allaxes:
129
 
                    title = axes.get_title()
130
 
                    ylabel = axes.get_ylabel()
131
 
                    if title:
132
 
                        text = title
133
 
                        if ylabel:
134
 
                            text += ": "+ylabel
135
 
                        text += " (%s)"
136
 
                    elif ylabel:
137
 
                        text = ylabel+" (%s)"
138
 
                    else:
139
 
                        text = "%s"
140
 
                    titles.append(text % repr(axes))
141
 
                item, ok = QInputDialog.getItem(self, 'Customize',
142
 
                                                'Select axes:', titles,
143
 
                                                0, False)
144
 
                if ok:
145
 
                    axes = allaxes[titles.index(unicode(item))]
146
 
                else:
147
 
                    return
148
 
            else:
149
 
                return
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 -*-
 
2
#
 
3
# Copyright © 2009-2010 Pierre Raybaut
 
4
# Licensed under the terms of the MIT License
 
5
# (see spyderlib/__init__.py for details)
 
6
 
 
7
"""Patching matplotlib's FigureManager"""
 
8
 
 
9
import sys
 
10
 
 
11
 
 
12
def is_available():
 
13
    """Is Matplotlib installed version supported by this patch?"""
 
14
    import matplotlib
 
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
 
18
        return True
 
19
 
 
20
 
 
21
def apply():
 
22
    """Monkey patching matplotlib Qt4 backend figures"""
 
23
    if not is_available():
 
24
        return
 
25
 
 
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
 
31
    
 
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
 
38
    
 
39
    from matplotlib.backends import backend_qt4
 
40
    
 
41
    # Class added to matplotlib to fix a bug with PyQt4 v4.6+
 
42
    class FigureWindow(QMainWindow):
 
43
        def __init__(self):
 
44
            super(FigureWindow, self).__init__()
 
45
            
 
46
        def closeEvent(self, event):
 
47
            super(FigureWindow, self).closeEvent(event)
 
48
            if is_pyqt46:
 
49
                self.emit(SIGNAL('destroyed()'))
 
50
    # ****************************************************************
 
51
    # *  FigureManagerQT
 
52
    # ****************************************************************
 
53
    class FigureManagerQT(backend_qt4.FigureManagerQT):
 
54
        """
 
55
        Patching matplotlib...
 
56
        """
 
57
        def __init__(self, canvas, num):
 
58
            import matplotlib
 
59
            
 
60
            if backend_qt4.DEBUG:
 
61
                print 'FigureManagerQT.%s' % backend_qt4.fn_name()
 
62
            backend_qt4.FigureManagerBase.__init__(self, canvas, num)
 
63
            self.canvas = canvas
 
64
            
 
65
            self.window = FigureWindow()
 
66
            self.window.setWindowTitle("Figure %d" % num)
 
67
            self.window.setAttribute(Qt.WA_DeleteOnClose)
 
68
 
 
69
            import os.path as osp
 
70
            image = osp.join(matplotlib.rcParams['datapath'],
 
71
                             'images', 'matplotlib.png' )
 
72
            self.window.setWindowIcon(QIcon(image))
 
73
    
 
74
            # Give the keyboard focus to the figure instead of the manager
 
75
            self.canvas.setFocusPolicy(Qt.ClickFocus)
 
76
            self.canvas.setFocus()
 
77
    
 
78
            QObject.connect(self.window, SIGNAL('destroyed()'),
 
79
                            lambda: self._widgetclosed())
 
80
            self.window._destroying = False
 
81
    
 
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)
 
86
    
 
87
            self.window.setCentralWidget(self.canvas)
 
88
    
 
89
            if matplotlib.is_interactive():
 
90
                self.window.show()
 
91
    
 
92
            # attach a show method to the figure for pylab ease of use
 
93
            self.canvas.figure.show = lambda *args: self.window.show()
 
94
    
 
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
 
101
    
 
102
    # ****************************************************************
 
103
    # *  NavigationToolbar2QT
 
104
    # ****************************************************************
 
105
    try:
 
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
 
112
        
 
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:
 
125
                axes = allaxes[0]
 
126
            elif len(allaxes) > 1:
 
127
                titles = []
 
128
                for axes in allaxes:
 
129
                    title = axes.get_title()
 
130
                    ylabel = axes.get_ylabel()
 
131
                    if title:
 
132
                        text = title
 
133
                        if ylabel:
 
134
                            text += ": "+ylabel
 
135
                        text += " (%s)"
 
136
                    elif ylabel:
 
137
                        text = ylabel+" (%s)"
 
138
                    else:
 
139
                        text = "%s"
 
140
                    titles.append(text % repr(axes))
 
141
                item, ok = QInputDialog.getItem(self, 'Customize',
 
142
                                                'Select axes:', titles,
 
143
                                                0, False)
 
144
                if ok:
 
145
                    axes = allaxes[titles.index(unicode(item))]
 
146
                else:
 
147
                    return
 
148
            else:
 
149
                return
 
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