Package windSimSuite :: Package interface :: Module matplotlib_manager
[hide private]

Source Code for Module windSimSuite.interface.matplotlib_manager

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # This file is part of MBDyn sim suite. 
  5  # Copyright (C) 2007 André ESPAZE, as part of a Master thesis supervised by 
  6  # Martin O.L.Hansen (DTU) and Nicolas Chauvat (Logilab) 
  7   
  8  # MBDyn Sim Suite is free software; you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public License as published by 
 10  # the Free Software Foundation; either version 2 of the License, or 
 11  # (at your option) any later version. 
 12  # 
 13  # This program is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with this program; if not, write to the Free Software 
 20  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 21  # 
 22  """The figures manager. This part is supposed to allocate 
 23  new pages when a new figure is requested, however  
 24  the plot action will be achieved in the L{windSimSuite.interface.figures}. 
 25  """ 
 26  import gtk 
 27  import sys 
 28   
 29  import matplotlib 
 30  matplotlib.use('GTK') 
 31  from matplotlib.figure import Figure as FigureBase 
 32  from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg 
 33  from matplotlib.backends.backend_gtk import NavigationToolbar2GTK 
 34   
 35  from windSimSuite.interface.figures import PLOT_TABLE 
 36   
 37   
38 -class ManagerMenu(gtk.Menu):
39 """The menu for removing a tab 40 """ 41
42 - def __init__(self):
43 gtk.Menu.__init__(self) 44 self.item = gtk.ImageMenuItem(gtk.STOCK_REMOVE) 45 self.item.show() 46 self.append(self.item)
47
48 - def remove_callback(self, callback):
49 """Set the callback action when the remove button is activated""" 50 self.item.connect("activate", callback)
51
52 - def custom_popup(self, event):
53 """Popup the menu""" 54 self.popup(None, None, None, event.button, event.time)
55 56
57 -class Figure(FigureBase):
58 """A Matplotlib figure able to carry a tab name 59 for the notebook. 60 """ 61
62 - def __init__(self):
63 FigureBase.__init__(self) 64 self.tab_name = ""
65 66
67 -class FigurePage(gtk.VBox):
68 """The page for a new figure. This class inherits 69 from a VBox because the figure and the Matplotlib toolbar need 70 to be packed. 71 """ 72
73 - def __init__(self, manager_callback):
74 gtk.VBox.__init__(self) 75 self.status = "" 76 self.tab_label = gtk.Label() 77 78 self.event_box = gtk.EventBox() 79 self.event_box.add(self.tab_label) 80 self.event_box.connect("button-press-event", manager_callback)
81
82 - def plot(self, prop_key, obj, simu, window_ref):
83 """Plot the figure by calling an action contained 84 in the C{PLOT_TABLE} from the L{windSimSuite.interface.figures}.""" 85 fig = Figure() 86 PLOT_TABLE[prop_key](fig, obj, simu) 87 88 self.tab_label.set_text(fig.tab_name) 89 90 self._insert_figure(fig, window_ref) 91 self._show_all() 92 self.status = "Done for '%s'" % fig.tab_name
93
94 - def _insert_figure(self, fig, window_ref):
95 """Insert the new figure in gtk""" 96 canvas = FigureCanvasGTKAgg(fig) 97 canvas.show() 98 self.pack_start(canvas, True, True) 99 100 toolbar = NavigationToolbar2GTK(canvas, window_ref) 101 self.pack_start(toolbar, False, False)
102
103 - def _show_all(self):
104 """Show the figure in its page""" 105 self.tab_label.show() 106 self.show()
107 108
109 -class FigureManager:
110 """The figure manager. It will receive instructions 111 from the C{Manager} in the 112 L{windSimSuite.interface.manager} module and assure to open 113 a new page for a new figure in the notebook when 114 its L{plot} method is called. 115 """ 116
117 - def __init__(self, notebook, status_bar):
118 self.notebook = notebook 119 self.status_bar = status_bar 120 self.window_ref = None 121 122 self.menu = ManagerMenu() 123 self.menu.remove_callback(self.remove_page) 124 125 self.pages = [] 126 self.event_box = None 127 self.event_boxes = []
128
129 - def set_window_ref(self, window_ref):
130 """The Matplotlib toolbar need need a window reference""" 131 self.window_ref = window_ref
132
133 - def manager_callback(self, event_box, event):
134 """When a right click is done on the page label, this 135 is the callaback of the manager.""" 136 if event.button == 3: 137 self.event_box = event_box 138 self.menu.custom_popup(event) 139 return True 140 else: 141 return False
142
143 - def remove_page(self, gtk_menu_item):
144 """Remove a page from the notebook""" 145 page_id = self.event_boxes.index(self.event_box) 146 # The VTK area in the first tab must not be suppressed 147 self.notebook.remove_page(page_id + 1) 148 # Keep the focus on the following page 149 if self.notebook.get_n_pages() > 1: 150 self.notebook.set_current_page(page_id + 1) 151 # Update the list 152 self.event_boxes.remove(self.event_box) 153 self.pages.remove(self.pages[page_id])
154
155 - def _add(self, figure_page):
156 """Add a new page by receiving a L{FigurePage} instance.""" 157 self.pages.append(figure_page) 158 self.event_boxes.append(figure_page.event_box) 159 160 self.notebook.append_page(figure_page, 161 figure_page.event_box) 162 self.notebook.set_current_page(-1)
163
164 - def plot(self, prop_key, obj, simu):
165 """The method called from the C{Manager} when the user 166 has chosen a plotting action on an object.""" 167 fig_page = FigurePage(self.manager_callback) 168 fig_page.plot(prop_key, obj, simu, self.window_ref) 169 self._add(fig_page) 170 return fig_page.status
171