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

Source Code for Module windSimSuite.interface.application

  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  """Describe the main wind-sim-suite application""" 
 23  import gtk 
 24  import gtk.glade as glade 
 25  import gobject 
 26   
 27  import os 
 28   
 29  from windSimSuite.interface.manager import Manager 
 30   
 31  PREFIX_FILE = os.path.dirname(__file__) 
 32  # Problem with py2exe on Win32 
 33  import sys 
 34  if getattr(sys, 'frozen', None): 
 35      PREFIX_FILE = sys.path[0] 
 36  del sys 
 37   
 38  GLADE_FILE = os.path.join(PREFIX_FILE, "interface.glade") 
 39   
 40   
41 -class CommonWindow:
42 """A base class for the window of that application. 43 Each window will be described inside a glade file, so 44 its part of the glade file and the wigdet 45 will be set as attributes. Moreover any item could 46 be got from its glade name. 47 """ 48
49 - def __init__(self, window_name):
50 self.xml = glade.XML(GLADE_FILE, window_name) 51 self.win = self.xml.get_widget(window_name) 52 self.win.hide()
53 #self.win.set_icon_from_file(ICON) 54
55 - def hide(self, *args):
56 """Hide the window""" 57 self.win.hide()
58
59 - def no_destroy(self, *args):
60 """Do not destroy the window""" 61 self.win.hide() 62 return True
63
64 - def __getitem__(self, key):
65 """Return a widget from its glade key""" 66 return self.xml.get_widget(key)
67 68
69 -class FileChooserDialog(CommonWindow):
70 """The file chooser dialog 71 """ 72
73 - def __init__(self):
74 CommonWindow.__init__(self, "filechooserdialog")
75
76 - def run(self):
77 """Show the dialog and return a filename or None""" 78 resp = self.win.run() 79 if resp == gtk.RESPONSE_OK: 80 #self.win.hide() 81 filename = self.win.get_filename() 82 self.win.destroy() 83 return filename 84 else: 85 self.win.destroy() 86 #self.win.hide() 87 return None
88 89
90 -class AboutWindow(gtk.AboutDialog):
91 """The window about the application 92 """ 93
94 - def __init__(self):
95 from windSimSuite import __version__ 96 gtk.AboutDialog.__init__(self) 97 self.set_name("Wind Sim Suite") 98 self.set_version(__version__) 99 100 self.set_authors(self._build_credits()) 101 self.set_website("http://mbdynsimsuite.sourceforge.net/") 102 103 self.set_license("GPL version 2 or later") 104 self.set_wrap_license(True) 105 106 self.set_comments("Based on MBDyn 1.2.7")
107
108 - def _build_credits(self):
109 from windSimSuite import __author_utf8__, __version__ 110 from windSimSuite import __author_email__, __research_group__ 111 from mbdyn import __mbdyn_developers__ 112 credits = [__author_utf8__ + " " + __author_email__] 113 credits.append("\n\nResearch group on open models for wind turbine:\n") 114 for name in __research_group__: 115 credits.append(name) 116 credits.append("\n\nMBDyn developers:\n") 117 for name in __mbdyn_developers__: 118 credits.append(name) 119 credits.append("\n\n") 120 return credits
121 122
123 -class ScreenInfo:
124 """The width an height of the screen 125 """ 126
127 - def __init__(self, screen):
128 self.width = screen.get_width() 129 self.height = screen.get_height()
130
131 - def get_width(self, value):
132 """Return the screen width as an integer""" 133 return int(value * self.width)
134
135 - def get_height(self, value):
136 """Return the screen height as an integer""" 137 return int(value * self.height)
138 139
140 -class Application(CommonWindow):
141 """The wind sim suite application 142 """ 143
144 - def __init__(self):
145 CommonWindow.__init__(self, "application") 146 147 handlers = { 148 "on_application_destroy" : gtk.main_quit, 149 150 "on_dialog_open_activate" : self.load_file, 151 "on_tjaereborg_activate" : self.load_tjaerborg, 152 "on_nrel_activate" : self.load_nrel, 153 "on_quit_activate" : gtk.main_quit, 154 155 "on_show_animation_toolbar_activate" : self.show_animation_toolbar, 156 "on_windturbine_tree_activate" : (self.select_treeview, 157 ["turbine"]), 158 "on_mbdyn_tree_activate" : (self.select_treeview, ["mbdyn"]), 159 "on_both_trees_activate" : (self.select_treeview, 160 ["turbine", "mbdyn"]), 161 "on_scale_table_activate" : self.show_scale_table, 162 "on_absolute_ref_activate" : self.show_absolute_ref, 163 "on_notebook_tab_activate" : self.show_notebook_tab, 164 165 "on_stop_task_clicked" : self.stop_current_task, 166 167 "on_about_activate" : self.show_about_window 168 } 169 self.xml.signal_autoconnect(handlers) 170 171 self.screen_info = ScreenInfo(self.win.get_screen()) 172 self["hpaned"].set_position(self.screen_info.get_width(0.2)) 173 self.win.resize(self.screen_info.get_width(0.8), 174 self.screen_info.get_height(0.8)) 175 self.win.move(0, 20) 176 177 # Probably better to create it each time. 178 self.dialog_open = FileChooserDialog() 179 180 self.manager = Manager(self) 181 self["gvtk_box"].pack_start(self.manager.vtk_area) 182 self["scrolledwindow_turbine"].add(self.manager.treeview["turbine"]) 183 self["scrolledwindow_mbdyn"].add(self.manager.treeview["mbdyn"]) 184 185 self.task_id = None 186 187 self.about_window = AboutWindow() 188 189 self.win.show() 190 self.manager.vtk_area.show()
191
192 - def load_file(self, *args):
193 """Load a file for post-processing""" 194 self.dialog_open = FileChooserDialog() 195 filename = self.dialog_open.run() 196 if filename != None: 197 load = self.manager.load_simulation_with_generators(filename) 198 self.task_id = gobject.idle_add(load.next)
199
200 - def load_tjaerborg(self, *args):
201 """Load an example of the Tjaereborg turbine""" 202 filename = os.path.join(PREFIX_FILE, "examples", "tjaerborg.pymb") 203 load = self.manager.load_simulation_with_generators(filename) 204 self.task_id = gobject.idle_add(load.next)
205
206 - def load_nrel(self, *args):
207 """Load an example of the NREL turbine""" 208 filename = os.path.join(PREFIX_FILE, "examples", "nrel.pymb") 209 load = self.manager.load_simulation_with_generators(filename) 210 self.task_id = gobject.idle_add(load.next)
211
212 - def stop_current_task(self, *args):
213 """Stop the loading task""" 214 gobject.source_remove(self.task_id) 215 self["task_box"].hide()
216
217 - def show_animation_toolbar(self, check_item):
218 """Show or hide the animation toolbar""" 219 if check_item.get_active(): 220 self["animation_toolbar_box"].show() 221 else: 222 self["animation_toolbar_box"].hide()
223
224 - def select_treeview(self, radio_menu_item, keys):
225 """Select which object treeview to display""" 226 if radio_menu_item.get_active(): 227 for key in keys: 228 self["scrolledwindow_%s" % key].show() 229 else: 230 for key in keys: 231 self["scrolledwindow_%s" % key].hide()
232
233 - def show_scale_table(self, check_item):
234 """Show or hide the table for entering the scale factor""" 235 if check_item.get_active(): 236 self.manager.scale_table.show() 237 else: 238 self.manager.scale_table.hide()
239
240 - def show_absolute_ref(self, check_item):
241 """Show or hide the absolute referential""" 242 if check_item.get_active(): 243 self.manager.show_absolute_ref() 244 else: 245 self.manager.hide_absolute_ref()
246
247 - def show_notebook_tab(self, check_item):
248 """Show or hide the notebok table""" 249 if check_item.get_active(): 250 self.manager.notebook.set_show_tabs(True) 251 else: 252 self.manager.notebook.set_show_tabs(False)
253
254 - def show_about_window(self, window):
255 """Show the about window""" 256 resp = self.about_window.run() 257 self.about_window.hide()
258
259 - def run(self):
260 """Run the main application. Always wait an 261 event from the user""" 262 gtk.main()
263