myWin_Qt4Agg.py 6.87 KB
Newer Older
Philipp Gast's avatar
Philipp Gast committed
1
2
#!/usr/bin/python
# -*- coding: utf-8 -*-
Philipp Gast's avatar
Philipp Gast committed
3
#
Philipp Gast's avatar
Philipp Gast committed
4
# ================= FILE HEADER ========================================
Philipp Gast's avatar
Philipp Gast committed
5
#
Philipp Gast's avatar
Philipp Gast committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#   myplotlib v3.0.1,
#
#   @file      myWin_Qt4Agg.py
#   @author    Yori 'AGy' Fournier
#   @licence   CC-BY-SA
#
#   MyWin_Qt4Agg 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
#
# ======================================================================
#
# IMPORT ---------------------------------------------------------------
from . import INFO, SEVR, WARN, DBUG, SPCE
from . import rcParams
from . import _G_WINDOWS
from . import np

Philipp Gast's avatar
Philipp Gast committed
55
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg, FigureManagerQT
Philipp Gast's avatar
Philipp Gast committed
56
57

# Class MyWin Overwriting the disgusting Matplotlib.FigureManager class
Philipp Gast's avatar
Philipp Gast committed
58
class MyWin_Qt4Agg( FigureManagerQT):
Philipp Gast's avatar
Philipp Gast committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    
    # 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.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.")
        
        # create the canvas
Philipp Gast's avatar
Philipp Gast committed
110
111
        canvas = FigureCanvasQTAgg(fig)
        FigureManagerQT.__init__(self, canvas, num)
112
        
Philipp Gast's avatar
Philipp Gast committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
        # bound the figure to the window
        self.canvas.figure.boundedToWin = True
        self.canvas.figure.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.fignum = self.num
    
    
    # UNBOUND ----------------------------------------------------------
    def _unboundFigure_(self):
        
        self.canvas.figure.boundedToWin = False
        self.canvas.figure.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.fignum) for window in _G_WINDOWS])[0][0]]
            win.close()
        
        # bound the new figure
        self._boundFigure_(fig)
    
    
    # REFRESH ----------------------------------------------------------
    def refresh(self):
        
        # in the case the figure got another canvas
        # rebound the figure. This situation should not happend!!
        if self.canvas != self.canvas.figure.canvas:
            print(WARN + "Something weird happend, the canvas of the figure have been changed")
            self.set_figure(self.canvas.figure)
        
        # refresh
        self.canvas.figure.plot()
        self.canvas.draw()
        
        return(True)
    
    
    # DRAW -------------------------------------------------------------
    def drawFigure(self, fig):
        
        # if the figure is not the current one
        if(fig.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.