1 | """ |
---|
2 | ASAP plotting class based on matplotlib. |
---|
3 | """ |
---|
4 | |
---|
5 | from asap.asaplotbase import * |
---|
6 | import PyQt4 as qt |
---|
7 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg, \ |
---|
8 | FigureManagerQTAgg |
---|
9 | from matplotlib import _pylab_helpers |
---|
10 | # Force use of the newfangled toolbar. |
---|
11 | import matplotlib |
---|
12 | #matplotlib.use("Qt4Agg") |
---|
13 | matplotlib.rcParams['toolbar'] = 'toolbar2' |
---|
14 | |
---|
15 | class asaplotgui(asaplotbase): |
---|
16 | """ |
---|
17 | ASAP plotting class based on matplotlib. |
---|
18 | """ |
---|
19 | |
---|
20 | def __init__(self, rows=1, cols=0, title='', size=None, buffering=False): |
---|
21 | """ |
---|
22 | Create a new instance of the ASAPlot plotting class. |
---|
23 | |
---|
24 | If rows < 1 then a separate call to set_panels() is required to define |
---|
25 | the panel layout; refer to the doctext for set_panels(). |
---|
26 | """ |
---|
27 | v = vars() |
---|
28 | del v['self'] |
---|
29 | |
---|
30 | asaplotbase.__init__(self, **v) |
---|
31 | matplotlib.rcParams["interactive"] = True |
---|
32 | |
---|
33 | _pylab_helpers.Gcf.destroy(0) |
---|
34 | self.canvas = FigureCanvasQTAgg(self.figure) |
---|
35 | # Simply instantiating this is enough to get a working toolbar. |
---|
36 | self.figmgr = FigureManagerQTAgg(self.canvas, 0) |
---|
37 | self.window = self.figmgr.window |
---|
38 | self._set_window_title('ASAP Plotter - Qt4') |
---|
39 | # Register this plot to matplotlib without activating it |
---|
40 | #_pylab_helpers.Gcf.set_active(self.figmgr) |
---|
41 | _pylab_helpers.Gcf.figs[self.figmgr.num] = self.figmgr |
---|
42 | |
---|
43 | ############# |
---|
44 | ### DO WE HAVE TO DO SOMETHING FOR WINDOW CLOSE CALL? |
---|
45 | ############# |
---|
46 | def dest_callback(): |
---|
47 | try: |
---|
48 | self.is_dead = True |
---|
49 | except NameError: |
---|
50 | pass |
---|
51 | |
---|
52 | qt.QtCore.QObject.connect(self.window, qt.QtCore.SIGNAL('destroyed()'),dest_callback) |
---|
53 | |
---|
54 | self.unmap() |
---|
55 | #self.canvas.show() |
---|
56 | |
---|
57 | def map(self): |
---|
58 | """ |
---|
59 | Reveal the ASAPlot graphics window and bring it to the top of the |
---|
60 | window stack. |
---|
61 | """ |
---|
62 | if self.is_dead: |
---|
63 | raise RuntimeError( "No plotter to show. Not yet plotted or plotter is closed." ) |
---|
64 | self.window.activateWindow() |
---|
65 | #To raise this window to the top of the stacking order |
---|
66 | self.window.raise_() |
---|
67 | self.window.show() |
---|
68 | |
---|
69 | def quit(self): |
---|
70 | """ |
---|
71 | Destroy the ASAPlot graphics window. |
---|
72 | """ |
---|
73 | self.is_dead = True |
---|
74 | if not self.figmgr: |
---|
75 | return |
---|
76 | try: |
---|
77 | #self.window.close() |
---|
78 | # TODO destroy casabar |
---|
79 | _pylab_helpers.Gcf.destroy(self.figmgr.num) |
---|
80 | del self.window, self.canvas, self.figmgr |
---|
81 | self.window = None |
---|
82 | self.canvas = None |
---|
83 | self.figmgr = None |
---|
84 | except RuntimeError: pass # the window may already be closed by user |
---|
85 | |
---|
86 | def show(self, hardrefresh=True): |
---|
87 | """ |
---|
88 | Show graphics dependent on the current buffering state. |
---|
89 | """ |
---|
90 | if self.is_dead: |
---|
91 | raise RuntimeError( "No plotter to show (not yet plotted or closed)." ) |
---|
92 | if not self.buffering: |
---|
93 | if hardrefresh: |
---|
94 | asaplotbase.show(self) |
---|
95 | self.window.activateWindow() |
---|
96 | self.canvas.draw() |
---|
97 | #self.canvas.show() |
---|
98 | self.window.show() |
---|
99 | |
---|
100 | def terminate(self): |
---|
101 | """ |
---|
102 | Clear the figure. |
---|
103 | """ |
---|
104 | if not self.window: |
---|
105 | asaplog.push( "No plotter window to terminate." ) |
---|
106 | asaplog.post( "WARN" ) |
---|
107 | return |
---|
108 | self.window.close() |
---|
109 | |
---|
110 | def unmap(self): |
---|
111 | """ |
---|
112 | Hide the ASAPlot graphics window. |
---|
113 | """ |
---|
114 | if not self.window: |
---|
115 | asaplog.push( "No plotter window to unmap." ) |
---|
116 | asaplog.post( "WARN" ) |
---|
117 | return |
---|
118 | self.window.hide() |
---|
119 | |
---|
120 | def _set_window_title(self,title): |
---|
121 | # Set title to main window title bar |
---|
122 | self.window.setWindowTitle(title) |
---|