myWin.py 4.78 KB
Newer Older
1
2
3
4
5
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
#   myplotlib v1.0.1,
#
#   @file      myWin.py
#   @author    Yori 'AGy' Fournier
#   @licence   CC-BY-SA
#
#   MyWin 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
#
#   @section History
#
#   v 1.0.1 - MyWin class for the myplotlib module.
#
# ======================================================================
#
# IMPORT ---------------------------------------------------------------
# Forms
37
from myplotlib import INFO, SEVR, WARN, DBUG, SPCE
38
# Classes
39
from myplotlib import FigureManager, FigureCanvas
40
# Global variables
41
from myplotlib import _G_WINDOWS
42
43
# Module
from myplotlib.np import where
44
45


46
# Class MyWin Overwriting the disgusting Matplotlib.FigureManager class
47
class MyWin(FigureManager):
48
49

    # CONSTRUCTOR ------------------------------------------------------
50
51
    def __init__(self, fig, show=True, *args, **kwargs):
        
52
        # set the dpi to 75 (correct value for screen)
53
54
55
        if(fig.dpi != 75.):
            fig.dpi = 75.
        
56
57
58
59
60
61
62
63
        # 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)
        
        # before creating anything verify that the fig is not already
        # bounded to another window
64
        if fig.boundedToWin:
65
66
            # 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]]
67
68
            win.close()
        
69
70
        # Call the ugly FigureManager class
        # (already wondering if this is not a mistake...)
71
72
        FigureManager.__init__(self, FigureCanvas(fig), num)
        
73
        # bound the figure to the window
74
75
76
        self.canvas.figure.boundedToWin = True
        self.canvas.figure.keywords['fignum'] = num
        
77
78
79
        # add the window in the global variable _G_WINDOWS
        # this guarenty that you never loose a window
        # (see myTool.py getWindow(num))
80
        _G_WINDOWS.append(self)
81
82
        
        # if show is on, then show the window
83
84
85
86
        if show:
            self.refresh()
            self.show()
    
87
88
    
    # BOUND ------------------------------------------------------------
89
90
91
92
93
94
95
    def _boundFigure_(self, fig):
        
        fig.canvas = self.canvas
        self.canvas.figure = fig
        self.canvas.figure.boundedToWin = True
        self.canvas.figure.keywords['fignum'] = self.num
    
96
97
    
    # UNBOUND ----------------------------------------------------------
98
99
100
101
102
    def _unboundFigure_(self):
        
        self.canvas.figure.boundedToWin = False
        self.canvas.figure.keywords['fignum'] = -1
    
103
104
105
    
    # SETTERS ----------------------------------------------------------
    # SET FIGURE -------------------------------------------------------
106
107
    def set_figure(self, fig):
        
108
        # first unbound the former figure
109
110
        self._unboundFigure_()
        
111
112
        # if the new figure is already bounded close
        # its former container.
113
        if fig.boundedToWin:
114
            win = _G_WINDOWS[where([(window.num == fig.keywords['fignum']) for window in _G_WINDOWS])[0][0]]
115
            win.close()
116
117
        
        # bound the new figure
118
119
        self._boundFigure_(fig)
    
120
121
    
    # REFRESH ----------------------------------------------------------
122
123
124
125
126
127
    def refresh(self):
        
        self.canvas.figure.clf()
        self.canvas.figure.plot()
        self.canvas.draw()
    
128
129
    
    # DRAW -------------------------------------------------------------
130
    def drawFigure(self, fig):
131
132
        
        # if the figure is not the current one
133
        if(fig.keywords['fignum'] != self.num):
134
            # cleanly set the new figure
135
136
            self.set_figure(fig)
        
137
        # and refresh
138
139
        self.refresh()
    
140
141
    
    # CLOSE ------------------------------------------------------------
142
143
144
145
    def close(self):
        
        self._unboundFigure_()
        self.destroy()
146
147
148
149
150
151
        del _G_WINDOWS[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.