[41d466f] | 1 | """ |
---|
| 2 | This software was developed by the University of Tennessee as part of the |
---|
| 3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 4 | project funded by the US National Science Foundation. |
---|
| 5 | |
---|
| 6 | See the license text in license.txt |
---|
| 7 | |
---|
| 8 | copyright 2008, University of Tennessee |
---|
[7681bac] | 9 | |
---|
| 10 | How-to build an application using guiframe: |
---|
| 11 | |
---|
| 12 | 1- Write a main application script along the lines of dummyapp.py |
---|
| 13 | 2- Write a config script along the lines of config.py, and name it local_config.py |
---|
| 14 | 3- Write your plug-ins and place them in a directory called "perspectives". |
---|
| 15 | - Look at local_perspectives/plotting for an example of a plug-in. |
---|
| 16 | - A plug-in should define a class called Plugin. See abstract class below. |
---|
| 17 | |
---|
[41d466f] | 18 | """ |
---|
| 19 | import wx |
---|
| 20 | import wx.aui |
---|
| 21 | import os, sys |
---|
[b0eee0f0] | 22 | import xml |
---|
| 23 | from xml import xpath |
---|
[41d466f] | 24 | try: |
---|
| 25 | # Try to find a local config |
---|
| 26 | import imp |
---|
| 27 | path = os.getcwd() |
---|
[cbb2e40] | 28 | if(os.path.isfile("%s/%s.py" % (path, 'local_config'))) or \ |
---|
| 29 | (os.path.isfile("%s/%s.pyc" % (path, 'local_config'))): |
---|
| 30 | fObj, path, descr = imp.find_module('local_config', [path]) |
---|
| 31 | config = imp.load_module('local_config', fObj, path, descr) |
---|
[278cc25] | 32 | else: |
---|
[cbb2e40] | 33 | # Try simply importing local_config |
---|
| 34 | import local_config as config |
---|
[41d466f] | 35 | except: |
---|
| 36 | # Didn't find local config, load the default |
---|
| 37 | import config |
---|
[cbb2e40] | 38 | |
---|
[6d920cd] | 39 | from sans.guicomm.events import EVT_STATUS |
---|
[0bd2cd8] | 40 | from sans.guicomm.events import EVT_NEW_PLOT,EVT_SLICER_PARS_UPDATE |
---|
[4ba846b] | 41 | |
---|
[adfcab3] | 42 | import warnings |
---|
| 43 | warnings.simplefilter("ignore") |
---|
| 44 | |
---|
[fc2b91a] | 45 | import logging |
---|
[adfcab3] | 46 | |
---|
[7681bac] | 47 | class Plugin: |
---|
| 48 | """ |
---|
| 49 | This class defines the interface for a Plugin class |
---|
| 50 | that can be used by the gui_manager. |
---|
| 51 | |
---|
| 52 | Plug-ins should be placed in a sub-directory called "perspectives". |
---|
| 53 | For example, a plug-in called Foo should be place in "perspectives/Foo". |
---|
| 54 | That directory contains at least two files: |
---|
| 55 | perspectives/Foo/__init.py contains two lines: |
---|
| 56 | |
---|
| 57 | PLUGIN_ID = "Foo plug-in 1.0" |
---|
| 58 | from Foo import * |
---|
| 59 | |
---|
| 60 | perspectives/Foo/Foo.py contains the definition of the Plugin |
---|
| 61 | class for the Foo plug-in. The interface of that Plugin class |
---|
| 62 | should follow the interface of the class you are looking at. |
---|
| 63 | """ |
---|
| 64 | |
---|
| 65 | def __init__(self): |
---|
| 66 | """ |
---|
| 67 | Abstract class for gui_manager Plugins. |
---|
| 68 | """ |
---|
| 69 | ## Plug-in name. It will appear on the application menu. |
---|
| 70 | self.sub_menu = "Plugin" |
---|
| 71 | |
---|
| 72 | ## Reference to the parent window. Filled by get_panels() below. |
---|
| 73 | self.parent = None |
---|
| 74 | |
---|
| 75 | ## List of panels that you would like to open in AUI windows |
---|
| 76 | # for your plug-in. This defines your plug-in "perspective" |
---|
| 77 | self.perspective = [] |
---|
| 78 | |
---|
| 79 | raise RuntimeError, "gui_manager.Plugin is an abstract class" |
---|
| 80 | |
---|
| 81 | def populate_menu(self, id, parent): |
---|
| 82 | """ |
---|
| 83 | Create and return the list of application menu |
---|
| 84 | items for the plug-in. |
---|
| 85 | |
---|
| 86 | @param id: deprecated. Un-used. |
---|
| 87 | @param parent: parent window |
---|
| 88 | @return: plug-in menu |
---|
| 89 | """ |
---|
| 90 | import wx |
---|
| 91 | # Create a menu |
---|
| 92 | plug_menu = wx.Menu() |
---|
| 93 | |
---|
| 94 | # Always get event IDs from wx |
---|
| 95 | id = wx.NewId() |
---|
| 96 | |
---|
| 97 | # Fill your menu |
---|
| 98 | plug_menu.Append(id, '&Do something') |
---|
| 99 | wx.EVT_MENU(owner, id, self._on_do_something) |
---|
| 100 | |
---|
| 101 | # Returns the menu and a name for it. |
---|
| 102 | return [(id, plug_menu, "name of the application menu")] |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | def get_panels(self, parent): |
---|
| 106 | """ |
---|
| 107 | Create and return the list of wx.Panels for your plug-in. |
---|
| 108 | Define the plug-in perspective. |
---|
| 109 | |
---|
| 110 | Panels should inherit from DefaultPanel defined below, |
---|
| 111 | or should present the same interface. They must define |
---|
| 112 | "window_caption" and "window_name". |
---|
| 113 | |
---|
| 114 | @param parent: parent window |
---|
| 115 | @return: list of panels |
---|
| 116 | """ |
---|
| 117 | ## Save a reference to the parent |
---|
| 118 | self.parent = parent |
---|
| 119 | |
---|
| 120 | # Define a panel |
---|
| 121 | mypanel = DefaultPanel(self.parent, -1) |
---|
| 122 | |
---|
| 123 | # If needed, add its name to the perspective list |
---|
| 124 | self.perspective.append(self.control_panel.window_name) |
---|
| 125 | |
---|
| 126 | # Return the list of panels |
---|
| 127 | return [mypanel] |
---|
| 128 | |
---|
| 129 | def get_context_menu(self, graph=None): |
---|
| 130 | """ |
---|
| 131 | This method is optional. |
---|
| 132 | |
---|
| 133 | When the context menu of a plot is rendered, the |
---|
| 134 | get_context_menu method will be called to give you a |
---|
| 135 | chance to add a menu item to the context menu. |
---|
| 136 | |
---|
| 137 | A ref to a Graph object is passed so that you can |
---|
| 138 | investigate the plot content and decide whether you |
---|
| 139 | need to add items to the context menu. |
---|
| 140 | |
---|
| 141 | This method returns a list of menu items. |
---|
| 142 | Each item is itself a list defining the text to |
---|
| 143 | appear in the menu, a tool-tip help text, and a |
---|
| 144 | call-back method. |
---|
| 145 | |
---|
| 146 | @param graph: the Graph object to which we attach the context menu |
---|
| 147 | @return: a list of menu items with call-back function |
---|
| 148 | """ |
---|
| 149 | return [["Menu text", |
---|
| 150 | "Tool-tip help text", |
---|
| 151 | self._on_context_do_something]] |
---|
| 152 | |
---|
| 153 | def get_perspective(self): |
---|
| 154 | """ |
---|
| 155 | Get the list of panel names for this perspective |
---|
| 156 | """ |
---|
| 157 | return self.perspective |
---|
| 158 | |
---|
| 159 | def on_perspective(self, event): |
---|
| 160 | """ |
---|
| 161 | Call back function for the perspective menu item. |
---|
| 162 | We notify the parent window that the perspective |
---|
| 163 | has changed. |
---|
| 164 | @param event: menu event |
---|
| 165 | """ |
---|
| 166 | self.parent.set_perspective(self.perspective) |
---|
| 167 | |
---|
| 168 | def post_init(self): |
---|
| 169 | """ |
---|
| 170 | Post initialization call back to close the loose ends |
---|
| 171 | """ |
---|
| 172 | pass |
---|
| 173 | |
---|
| 174 | |
---|
[41d466f] | 175 | class ViewerFrame(wx.Frame): |
---|
| 176 | """ |
---|
| 177 | Main application frame |
---|
| 178 | """ |
---|
[445f949] | 179 | def __init__(self, parent, id, title, window_height=700, window_width=1000): |
---|
[16bf519] | 180 | #def __init__(self, parent, id, title, window_height=800, window_width=800): |
---|
[41d466f] | 181 | """ |
---|
| 182 | Initialize the Frame object |
---|
| 183 | """ |
---|
| 184 | from local_perspectives.plotting import plotting |
---|
[16bf519] | 185 | #wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, size=(800, 700)) |
---|
[445f949] | 186 | wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, size=(1000, 600)) |
---|
[d0802c3] | 187 | |
---|
| 188 | # Preferred window size |
---|
| 189 | self._window_height = window_height |
---|
| 190 | self._window_width = window_width |
---|
[41d466f] | 191 | |
---|
[fc2b91a] | 192 | # Logging info |
---|
| 193 | logging.basicConfig(level=logging.DEBUG, |
---|
| 194 | format='%(asctime)s %(levelname)s %(message)s', |
---|
| 195 | filename='sans_app.log', |
---|
| 196 | filemode='w') |
---|
[c44e7cc] | 197 | path = os.path.dirname(__file__) |
---|
| 198 | temp_path= os.path.join(path,'images') |
---|
| 199 | ico_file = os.path.join(temp_path,'ball.ico') |
---|
[278cc25] | 200 | if os.path.isfile(ico_file): |
---|
| 201 | self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO)) |
---|
[cbb2e40] | 202 | else: |
---|
[c44e7cc] | 203 | temp_path= os.path.join(os.getcwd(),'images') |
---|
| 204 | ico_file = os.path.join(temp_path,'ball.ico') |
---|
[cbb2e40] | 205 | if os.path.isfile(ico_file): |
---|
| 206 | self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO)) |
---|
[41d466f] | 207 | |
---|
| 208 | ## Application manager |
---|
| 209 | self.app_manager = None |
---|
| 210 | |
---|
| 211 | ## Find plug-ins |
---|
| 212 | # Modify this so that we can specify the directory to look into |
---|
[a88ac04] | 213 | self.plugins =[] |
---|
[41d466f] | 214 | self.plugins.append(plotting.Plugin()) |
---|
[a88ac04] | 215 | self.plugins += self._find_plugins() |
---|
| 216 | |
---|
[41d466f] | 217 | ## List of panels |
---|
| 218 | self.panels = {} |
---|
| 219 | |
---|
| 220 | ## Next available ID for wx gui events |
---|
[2310d69] | 221 | #TODO: No longer used - remove all calls to this |
---|
[41d466f] | 222 | self.next_id = 20000 |
---|
| 223 | |
---|
[2310d69] | 224 | # Default locations |
---|
| 225 | self._default_save_location = os.getcwd() |
---|
| 226 | |
---|
[41d466f] | 227 | ## Default welcome panel |
---|
| 228 | self.defaultPanel = DefaultPanel(self, -1, style=wx.RAISED_BORDER) |
---|
[ca88b2e] | 229 | |
---|
[a0d56d5] | 230 | # Check for update |
---|
| 231 | self._check_update(None) |
---|
[b0eee0f0] | 232 | ## maximum number of opened files' paths to store |
---|
| 233 | self.n_maxfileopen = 2 |
---|
| 234 | ## number of file open |
---|
| 235 | self.n_fileOpen=0 |
---|
| 236 | ## list of path of open files |
---|
| 237 | self.filePathList=[] |
---|
| 238 | ## list of open file with name form menu |
---|
| 239 | #self._saveOpenData() |
---|
[25ccf33] | 240 | ## Dictionary of open file where keys are filename and values are number of copy of data plotted |
---|
| 241 | ## using the same loaded file |
---|
| 242 | self.indice_load_data={} |
---|
[278cc25] | 243 | # Register the close event so it calls our own method |
---|
| 244 | wx.EVT_CLOSE(self, self._onClose) |
---|
| 245 | # Register to status events |
---|
| 246 | self.Bind(EVT_STATUS, self._on_status_event) |
---|
[63c7d85d] | 247 | |
---|
[0bd2cd8] | 248 | |
---|
[278cc25] | 249 | def build_gui(self): |
---|
[41d466f] | 250 | # Set up the layout |
---|
| 251 | self._setup_layout() |
---|
| 252 | |
---|
| 253 | # Set up the menu |
---|
| 254 | self._setup_menus() |
---|
| 255 | |
---|
| 256 | self.Fit() |
---|
[75b40ce] | 257 | |
---|
| 258 | #self._check_update(None) |
---|
[41d466f] | 259 | |
---|
| 260 | def _setup_layout(self): |
---|
| 261 | """ |
---|
| 262 | Set up the layout |
---|
| 263 | """ |
---|
| 264 | # Status bar |
---|
[dd66fbd] | 265 | from statusbar import MyStatusBar |
---|
| 266 | self.sb = MyStatusBar(self,wx.ID_ANY) |
---|
| 267 | self.SetStatusBar(self.sb) |
---|
[d468daa] | 268 | |
---|
[41d466f] | 269 | # Add panel |
---|
| 270 | self._mgr = wx.aui.AuiManager(self) |
---|
| 271 | |
---|
| 272 | # Load panels |
---|
| 273 | self._load_panels() |
---|
| 274 | |
---|
| 275 | self._mgr.Update() |
---|
[278cc25] | 276 | |
---|
| 277 | def add_perspective(self, plugin): |
---|
| 278 | """ |
---|
| 279 | Add a perspective if it doesn't already |
---|
| 280 | exist. |
---|
| 281 | """ |
---|
| 282 | is_loaded = False |
---|
| 283 | for item in self.plugins: |
---|
[fd68aa9] | 284 | if plugin.__class__==item.__class__: |
---|
[278cc25] | 285 | print "Plugin %s already loaded" % plugin.__class__.__name__ |
---|
| 286 | is_loaded = True |
---|
| 287 | |
---|
| 288 | if not is_loaded: |
---|
| 289 | self.plugins.append(plugin) |
---|
[41d466f] | 290 | |
---|
| 291 | def _find_plugins(self, dir="perspectives"): |
---|
| 292 | """ |
---|
| 293 | Find available perspective plug-ins |
---|
| 294 | @param dir: directory in which to look for plug-ins |
---|
| 295 | @return: list of plug-ins |
---|
| 296 | """ |
---|
| 297 | import imp |
---|
[6d920cd] | 298 | #print "Looking for plug-ins in %s" % dir |
---|
[41d466f] | 299 | # List of plug-in objects |
---|
[278cc25] | 300 | |
---|
| 301 | #path_exe = os.getcwd() |
---|
| 302 | #path_plugs = os.path.join(path_exe, dir) |
---|
| 303 | f = open("load.log",'w') |
---|
| 304 | f.write(os.getcwd()+'\n\n') |
---|
| 305 | #f.write(str(os.listdir(dir))+'\n') |
---|
| 306 | |
---|
| 307 | |
---|
[41d466f] | 308 | plugins = [] |
---|
| 309 | # Go through files in panels directory |
---|
| 310 | try: |
---|
| 311 | list = os.listdir(dir) |
---|
[a88ac04] | 312 | ## the default panel is the panel is the last plugin added |
---|
| 313 | for item in list: |
---|
[41d466f] | 314 | toks = os.path.splitext(os.path.basename(item)) |
---|
| 315 | name = None |
---|
| 316 | if not toks[0] == '__init__': |
---|
| 317 | |
---|
| 318 | if toks[1]=='.py' or toks[1]=='': |
---|
| 319 | name = toks[0] |
---|
| 320 | |
---|
| 321 | path = [os.path.abspath(dir)] |
---|
| 322 | file = None |
---|
| 323 | try: |
---|
| 324 | if toks[1]=='': |
---|
[278cc25] | 325 | f.write("trying to import \n") |
---|
[41d466f] | 326 | mod_path = '.'.join([dir, name]) |
---|
[278cc25] | 327 | f.write("mod_path= %s\n" % mod_path) |
---|
[41d466f] | 328 | module = __import__(mod_path, globals(), locals(), [name]) |
---|
[278cc25] | 329 | f.write(str(module)+'\n') |
---|
[41d466f] | 330 | else: |
---|
| 331 | (file, path, info) = imp.find_module(name, path) |
---|
| 332 | module = imp.load_module( name, file, item, info ) |
---|
| 333 | if hasattr(module, "PLUGIN_ID"): |
---|
| 334 | try: |
---|
| 335 | plugins.append(module.Plugin()) |
---|
| 336 | print "Found plug-in: %s" % module.PLUGIN_ID |
---|
| 337 | except: |
---|
| 338 | config.printEVT("Error accessing PluginPanel in %s\n %s" % (name, sys.exc_value)) |
---|
| 339 | |
---|
| 340 | except: |
---|
| 341 | print sys.exc_value |
---|
[278cc25] | 342 | f.write(str(sys.exc_value)+'\n') |
---|
[41d466f] | 343 | finally: |
---|
| 344 | if not file==None: |
---|
| 345 | file.close() |
---|
| 346 | except: |
---|
| 347 | # Should raise and catch at a higher level and display error on status bar |
---|
| 348 | pass |
---|
[278cc25] | 349 | f.write(str(plugins)+'\n') |
---|
| 350 | f.close() |
---|
[41d466f] | 351 | return plugins |
---|
| 352 | |
---|
| 353 | |
---|
| 354 | |
---|
| 355 | def _load_panels(self): |
---|
| 356 | """ |
---|
| 357 | Load all panels in the panels directory |
---|
| 358 | """ |
---|
| 359 | |
---|
| 360 | # Look for plug-in panels |
---|
| 361 | panels = [] |
---|
| 362 | for item in self.plugins: |
---|
| 363 | if hasattr(item, "get_panels"): |
---|
| 364 | ps = item.get_panels(self) |
---|
| 365 | panels.extend(ps) |
---|
| 366 | |
---|
| 367 | # Show a default panel with some help information |
---|
| 368 | # It also sets the size of the application windows |
---|
| 369 | self.panels["default"] = self.defaultPanel |
---|
[ca88b2e] | 370 | |
---|
[41d466f] | 371 | self._mgr.AddPane(self.defaultPanel, wx.aui.AuiPaneInfo(). |
---|
| 372 | Name("default"). |
---|
| 373 | CenterPane(). |
---|
[d0802c3] | 374 | # This is where we set the size of the application window |
---|
| 375 | BestSize(wx.Size(self._window_width, self._window_height)). |
---|
| 376 | MinSize(wx.Size(self._window_width, self._window_height)). |
---|
[41d466f] | 377 | Show()) |
---|
[6d920cd] | 378 | |
---|
[41d466f] | 379 | |
---|
| 380 | # Add the panels to the AUI manager |
---|
| 381 | for panel_class in panels: |
---|
| 382 | p = panel_class |
---|
| 383 | id = wx.NewId() |
---|
| 384 | |
---|
| 385 | # Check whether we need to put this panel |
---|
| 386 | # in the center pane |
---|
| 387 | if hasattr(p, "CENTER_PANE"): |
---|
| 388 | if p.CENTER_PANE: |
---|
| 389 | self.panels[str(id)] = p |
---|
| 390 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
| 391 | Name(p.window_name).Caption(p.window_caption). |
---|
| 392 | CenterPane(). |
---|
[1b70098] | 393 | BestSize(wx.Size(550,600)). |
---|
[445f949] | 394 | MinSize(wx.Size(500,500)). |
---|
[41d466f] | 395 | Hide()) |
---|
| 396 | else: |
---|
| 397 | self.panels[str(id)] = p |
---|
| 398 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
| 399 | Name(p.window_name).Caption(p.window_caption). |
---|
| 400 | Right(). |
---|
| 401 | Dock(). |
---|
| 402 | TopDockable(). |
---|
| 403 | BottomDockable(). |
---|
| 404 | LeftDockable(). |
---|
| 405 | RightDockable(). |
---|
| 406 | MinimizeButton(). |
---|
| 407 | Hide(). |
---|
[1b70098] | 408 | BestSize(wx.Size(550,600)). |
---|
[b8d7491] | 409 | MinSize(wx.Size(500,500))) |
---|
[41d466f] | 410 | |
---|
| 411 | |
---|
[2310d69] | 412 | def get_context_menu(self, graph=None): |
---|
[41d466f] | 413 | """ |
---|
| 414 | Get the context menu items made available |
---|
| 415 | by the different plug-ins. |
---|
| 416 | This function is used by the plotting module |
---|
| 417 | """ |
---|
| 418 | menu_list = [] |
---|
| 419 | for item in self.plugins: |
---|
| 420 | if hasattr(item, "get_context_menu"): |
---|
[2310d69] | 421 | menu_list.extend(item.get_context_menu(graph)) |
---|
[41d466f] | 422 | |
---|
| 423 | return menu_list |
---|
| 424 | |
---|
| 425 | def popup_panel(self, p): |
---|
| 426 | """ |
---|
| 427 | Add a panel object to the AUI manager |
---|
| 428 | @param p: panel object to add to the AUI manager |
---|
| 429 | @return: ID of the event associated with the new panel [int] |
---|
| 430 | """ |
---|
| 431 | |
---|
| 432 | ID = wx.NewId() |
---|
| 433 | self.panels[str(ID)] = p |
---|
| 434 | |
---|
| 435 | count = 0 |
---|
| 436 | for item in self.panels: |
---|
[383189f9] | 437 | if self.panels[item].window_name.startswith(p.window_name): |
---|
[41d466f] | 438 | count += 1 |
---|
[16bf519] | 439 | |
---|
[41d466f] | 440 | windowname = p.window_name |
---|
| 441 | caption = p.window_caption |
---|
[16bf519] | 442 | |
---|
[41d466f] | 443 | if count>0: |
---|
| 444 | windowname += str(count+1) |
---|
| 445 | caption += (' '+str(count)) |
---|
[16bf519] | 446 | |
---|
[41d466f] | 447 | p.window_name = windowname |
---|
| 448 | p.window_caption = caption |
---|
| 449 | |
---|
| 450 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
| 451 | Name(windowname).Caption(caption). |
---|
| 452 | Floatable(). |
---|
| 453 | #Float(). |
---|
| 454 | Right(). |
---|
| 455 | Dock(). |
---|
| 456 | TopDockable(). |
---|
| 457 | BottomDockable(). |
---|
| 458 | LeftDockable(). |
---|
| 459 | RightDockable(). |
---|
| 460 | MinimizeButton(). |
---|
| 461 | #Hide(). |
---|
| 462 | #Show(). |
---|
[1b70098] | 463 | BestSize(wx.Size(550,600)). |
---|
[16bf519] | 464 | MinSize(wx.Size(500,500))) |
---|
| 465 | #BestSize(wx.Size(400,400)). |
---|
| 466 | #MinSize(wx.Size(350,350))) |
---|
[d0802c3] | 467 | pane = self._mgr.GetPane(windowname) |
---|
| 468 | self._mgr.MaximizePane(pane) |
---|
| 469 | self._mgr.RestoreMaximizedPane() |
---|
| 470 | |
---|
[41d466f] | 471 | |
---|
| 472 | # Register for showing/hiding the panel |
---|
[0d9dae8] | 473 | |
---|
[41d466f] | 474 | wx.EVT_MENU(self, ID, self._on_view) |
---|
| 475 | |
---|
| 476 | self._mgr.Update() |
---|
| 477 | return ID |
---|
| 478 | |
---|
| 479 | def _setup_menus(self): |
---|
| 480 | """ |
---|
| 481 | Set up the application menus |
---|
| 482 | """ |
---|
| 483 | # Menu |
---|
| 484 | menubar = wx.MenuBar() |
---|
| 485 | |
---|
| 486 | # File menu |
---|
[b0eee0f0] | 487 | self.filemenu = wx.Menu() |
---|
[fc2b91a] | 488 | |
---|
| 489 | id = wx.NewId() |
---|
[b0eee0f0] | 490 | self.filemenu.Append(id, '&Open', 'Open a file') |
---|
[fc2b91a] | 491 | wx.EVT_MENU(self, id, self._on_open) |
---|
[b0eee0f0] | 492 | #self.filemenu.AppendSeparator() |
---|
| 493 | |
---|
[ca88b2e] | 494 | id = wx.NewId() |
---|
[b0eee0f0] | 495 | self.filemenu.Append(id,'&Quit', 'Exit') |
---|
[fc2b91a] | 496 | wx.EVT_MENU(self, id, self.Close) |
---|
[41d466f] | 497 | |
---|
[adfcab3] | 498 | # Add sub menus |
---|
[b0eee0f0] | 499 | menubar.Append(self.filemenu, '&File') |
---|
[adfcab3] | 500 | |
---|
[41d466f] | 501 | # Plot menu |
---|
| 502 | # Attach a menu item for each panel in our |
---|
| 503 | # panel list that also appears in a plug-in. |
---|
| 504 | # TODO: clean this up. We should just identify |
---|
| 505 | # plug-in panels and add them all. |
---|
| 506 | |
---|
[b8d7491] | 507 | # Only add the panel menu if there is more than two panels |
---|
[adfcab3] | 508 | n_panels = 0 |
---|
[41d466f] | 509 | for plug in self.plugins: |
---|
| 510 | pers = plug.get_perspective() |
---|
| 511 | if len(pers)>0: |
---|
[adfcab3] | 512 | n_panels += 1 |
---|
[0d9dae8] | 513 | |
---|
[b8d7491] | 514 | if n_panels>2: |
---|
[adfcab3] | 515 | viewmenu = wx.Menu() |
---|
| 516 | for plug in self.plugins: |
---|
| 517 | plugmenu = wx.Menu() |
---|
| 518 | pers = plug.get_perspective() |
---|
| 519 | if len(pers)>0: |
---|
| 520 | for item in self.panels: |
---|
| 521 | if item == 'default': |
---|
| 522 | continue |
---|
| 523 | panel = self.panels[item] |
---|
| 524 | if panel.window_name in pers: |
---|
| 525 | plugmenu.Append(int(item), panel.window_caption, "Show %s window" % panel.window_caption) |
---|
[0d9dae8] | 526 | |
---|
| 527 | |
---|
| 528 | |
---|
[adfcab3] | 529 | wx.EVT_MENU(self, int(item), self._on_view) |
---|
| 530 | |
---|
| 531 | viewmenu.AppendMenu(wx.NewId(), plug.sub_menu, plugmenu, plug.sub_menu) |
---|
| 532 | menubar.Append(viewmenu, '&Panel') |
---|
| 533 | |
---|
[41d466f] | 534 | # Perspective |
---|
| 535 | # Attach a menu item for each defined perspective. |
---|
[adfcab3] | 536 | # Only add the perspective menu if there are more than one perspectves |
---|
| 537 | n_perspectives = 0 |
---|
[41d466f] | 538 | for plug in self.plugins: |
---|
| 539 | if len(plug.get_perspective()) > 0: |
---|
[adfcab3] | 540 | n_perspectives += 1 |
---|
| 541 | |
---|
| 542 | if n_perspectives>1: |
---|
| 543 | p_menu = wx.Menu() |
---|
| 544 | for plug in self.plugins: |
---|
| 545 | if len(plug.get_perspective()) > 0: |
---|
| 546 | id = wx.NewId() |
---|
| 547 | p_menu.Append(id, plug.sub_menu, "Switch to %s perspective" % plug.sub_menu) |
---|
| 548 | wx.EVT_MENU(self, id, plug.on_perspective) |
---|
| 549 | menubar.Append(p_menu, '&Perspective') |
---|
[41d466f] | 550 | |
---|
| 551 | # Help menu |
---|
| 552 | helpmenu = wx.Menu() |
---|
[fa452e4] | 553 | |
---|
| 554 | # Look for help item in plug-ins |
---|
| 555 | for item in self.plugins: |
---|
| 556 | if hasattr(item, "help"): |
---|
| 557 | id = wx.NewId() |
---|
| 558 | helpmenu.Append(id,'&%s help' % item.sub_menu, '') |
---|
| 559 | wx.EVT_MENU(self, id, item.help) |
---|
| 560 | |
---|
[41d466f] | 561 | if config._do_aboutbox: |
---|
| 562 | id = wx.NewId() |
---|
| 563 | helpmenu.Append(id,'&About', 'Software information') |
---|
| 564 | wx.EVT_MENU(self, id, self._onAbout) |
---|
| 565 | id = wx.NewId() |
---|
| 566 | helpmenu.Append(id,'&Check for update', 'Check for the latest version of %s' % config.__appname__) |
---|
| 567 | wx.EVT_MENU(self, id, self._check_update) |
---|
| 568 | |
---|
| 569 | |
---|
[adfcab3] | 570 | |
---|
[41d466f] | 571 | |
---|
| 572 | # Look for plug-in menus |
---|
| 573 | # Add available plug-in sub-menus. |
---|
| 574 | for item in self.plugins: |
---|
| 575 | if hasattr(item, "populate_menu"): |
---|
| 576 | for (self.next_id, menu, name) in item.populate_menu(self.next_id, self): |
---|
| 577 | menubar.Append(menu, name) |
---|
[0d9dae8] | 578 | |
---|
[41d466f] | 579 | |
---|
| 580 | menubar.Append(helpmenu, '&Help') |
---|
| 581 | |
---|
| 582 | self.SetMenuBar(menubar) |
---|
| 583 | |
---|
[fc2b91a] | 584 | |
---|
[41d466f] | 585 | |
---|
| 586 | def _on_status_event(self, evt): |
---|
| 587 | """ |
---|
| 588 | Display status message |
---|
| 589 | """ |
---|
[f706f393] | 590 | #self.sb.clear_gauge( msg="") |
---|
[dd66fbd] | 591 | mythread=None |
---|
| 592 | mytype= None |
---|
| 593 | if hasattr(evt, "curr_thread"): |
---|
| 594 | mythread= evt.curr_thread |
---|
| 595 | if hasattr(evt, "type"): |
---|
| 596 | mytype= evt.type |
---|
| 597 | self.sb.set_status( type=mytype,msg=str(evt.status),thread=mythread) |
---|
| 598 | |
---|
[41d466f] | 599 | |
---|
| 600 | |
---|
| 601 | def _on_view(self, evt): |
---|
| 602 | """ |
---|
| 603 | A panel was selected to be shown. If it's not already |
---|
| 604 | shown, display it. |
---|
| 605 | @param evt: menu event |
---|
| 606 | """ |
---|
| 607 | self.show_panel(evt.GetId()) |
---|
| 608 | |
---|
| 609 | def show_panel(self, uid): |
---|
| 610 | """ |
---|
| 611 | Shows the panel with the given id |
---|
| 612 | @param uid: unique ID number of the panel to show |
---|
| 613 | """ |
---|
| 614 | ID = str(uid) |
---|
| 615 | config.printEVT("show_panel: %s" % ID) |
---|
| 616 | if ID in self.panels.keys(): |
---|
| 617 | if not self._mgr.GetPane(self.panels[ID].window_name).IsShown(): |
---|
| 618 | self._mgr.GetPane(self.panels[ID].window_name).Show() |
---|
[383189f9] | 619 | # Hide default panel |
---|
| 620 | self._mgr.GetPane(self.panels["default"].window_name).Hide() |
---|
[353041d] | 621 | |
---|
[383189f9] | 622 | |
---|
[41d466f] | 623 | self._mgr.Update() |
---|
[6d920cd] | 624 | |
---|
[fc2b91a] | 625 | def _on_open(self, event): |
---|
[4102709] | 626 | |
---|
[fc2b91a] | 627 | from data_loader import plot_data |
---|
| 628 | path = self.choose_file() |
---|
[b8d7491] | 629 | |
---|
[700f9b4] | 630 | if path ==None: |
---|
| 631 | return |
---|
[fc2b91a] | 632 | if path and os.path.isfile(path): |
---|
| 633 | plot_data(self, path) |
---|
[4102709] | 634 | |
---|
[fc2b91a] | 635 | |
---|
| 636 | |
---|
[41d466f] | 637 | def _onClose(self, event): |
---|
[b0eee0f0] | 638 | """ |
---|
| 639 | Store info to retrieve in xml before closing the application |
---|
| 640 | """ |
---|
| 641 | try: |
---|
| 642 | doc = xml.dom.minidom.Document() |
---|
| 643 | main_node = doc.createElement("file Path") |
---|
| 644 | |
---|
| 645 | doc.appendChild(main_node) |
---|
| 646 | |
---|
| 647 | for item in self.filePathList: |
---|
| 648 | id, menuitem_name , path, title = item |
---|
| 649 | pt1 = doc.createElement("File") |
---|
| 650 | pt1.setAttribute("name", menuitem_name) |
---|
| 651 | pt2 = doc.createElement("path") |
---|
| 652 | pt2.appendChild(doc.createTextNode(str(path))) |
---|
| 653 | pt1.appendChild(pt2) |
---|
| 654 | pt3 = doc.createElement("title") |
---|
| 655 | pt3.appendChild(doc.createTextNode(str(title))) |
---|
| 656 | pt1.appendChild(pt3) |
---|
| 657 | |
---|
| 658 | main_node.appendChild(pt1) |
---|
| 659 | |
---|
| 660 | |
---|
| 661 | fd = open("fileOpened.xml",'w') |
---|
| 662 | fd.write(doc.toprettyxml()) |
---|
| 663 | fd.close() |
---|
| 664 | except: |
---|
| 665 | pass |
---|
| 666 | |
---|
[41d466f] | 667 | import sys |
---|
| 668 | wx.Exit() |
---|
| 669 | sys.exit() |
---|
| 670 | |
---|
[b0eee0f0] | 671 | |
---|
[41d466f] | 672 | def Close(self, event=None): |
---|
| 673 | """ |
---|
| 674 | Quit the application |
---|
| 675 | """ |
---|
| 676 | import sys |
---|
| 677 | wx.Frame.Close(self) |
---|
| 678 | wx.Exit() |
---|
| 679 | sys.exit() |
---|
| 680 | |
---|
| 681 | |
---|
| 682 | def _check_update(self, event=None): |
---|
| 683 | """ |
---|
| 684 | Check with the deployment server whether a new version |
---|
| 685 | of the application is available |
---|
| 686 | """ |
---|
| 687 | import urllib |
---|
| 688 | try: |
---|
| 689 | h = urllib.urlopen(config.__update_URL__) |
---|
| 690 | lines = h.readlines() |
---|
| 691 | line = '' |
---|
| 692 | if len(lines)>0: |
---|
| 693 | line = lines[0] |
---|
| 694 | |
---|
| 695 | toks = line.lstrip().rstrip().split('.') |
---|
| 696 | toks_current = config.__version__.split('.') |
---|
| 697 | update_available = False |
---|
| 698 | for i in range(len(toks)): |
---|
[75b40ce] | 699 | if len(toks[i].strip())>0: |
---|
| 700 | if int(toks[i].strip())>int(toks_current[i]): |
---|
| 701 | update_available = True |
---|
[41d466f] | 702 | if update_available: |
---|
| 703 | #print "Version %s is available" % line.rstrip().lstrip() |
---|
| 704 | self.SetStatusText("Version %s is available! See the Help menu to download it." % line.rstrip().lstrip()) |
---|
| 705 | if event != None: |
---|
| 706 | import webbrowser |
---|
| 707 | webbrowser.open(config.__download_page__) |
---|
| 708 | else: |
---|
[75b40ce] | 709 | if event != None: |
---|
| 710 | self.SetStatusText("You have the latest version of %s" % config.__appname__) |
---|
[41d466f] | 711 | except: |
---|
[75b40ce] | 712 | if event != None: |
---|
| 713 | self.SetStatusText("You have the latest version of %s" % config.__appname__) |
---|
[41d466f] | 714 | |
---|
| 715 | |
---|
| 716 | def _onAbout(self, evt): |
---|
| 717 | """ |
---|
| 718 | Pop up the about dialog |
---|
| 719 | @param evt: menu event |
---|
| 720 | """ |
---|
| 721 | if config._do_aboutbox: |
---|
| 722 | import aboutbox |
---|
| 723 | dialog = aboutbox.DialogAbout(None, -1, "") |
---|
| 724 | dialog.ShowModal() |
---|
| 725 | |
---|
[b0eee0f0] | 726 | |
---|
| 727 | def _saveOpenData(self): |
---|
| 728 | """ |
---|
| 729 | Savename and path of n opened data into as xml file |
---|
| 730 | """ |
---|
| 731 | try: |
---|
| 732 | fd = open("fileOpened.xml",'r') |
---|
| 733 | from xml.dom.minidom import parse |
---|
| 734 | dom = parse(fd) |
---|
| 735 | ## Check the format version number |
---|
| 736 | nodes = xpath.Evaluate('file Path\File', dom) |
---|
| 737 | print "node",nodes |
---|
| 738 | if nodes[0].hasAttributes(): |
---|
| 739 | print "--->" |
---|
| 740 | fd.close() |
---|
| 741 | except: |
---|
| 742 | raise |
---|
| 743 | |
---|
| 744 | |
---|
| 745 | |
---|
| 746 | def _onreloaFile(self, event): |
---|
| 747 | """ |
---|
| 748 | load a data previously opened |
---|
| 749 | """ |
---|
| 750 | from data_loader import plot_data |
---|
| 751 | for item in self.filePathList: |
---|
| 752 | id, menuitem_name , path, title = item |
---|
| 753 | if id == event.GetId(): |
---|
| 754 | if path and os.path.isfile(path): |
---|
| 755 | plot_data(self, path) |
---|
| 756 | break |
---|
| 757 | |
---|
| 758 | |
---|
[41d466f] | 759 | def set_manager(self, manager): |
---|
| 760 | """ |
---|
| 761 | Sets the application manager for this frame |
---|
| 762 | @param manager: frame manager |
---|
| 763 | """ |
---|
| 764 | self.app_manager = manager |
---|
| 765 | |
---|
| 766 | def post_init(self): |
---|
| 767 | """ |
---|
| 768 | This initialization method is called after the GUI |
---|
| 769 | has been created and all plug-ins loaded. It calls |
---|
| 770 | the post_init() method of each plug-in (if it exists) |
---|
| 771 | so that final initialization can be done. |
---|
| 772 | """ |
---|
| 773 | for item in self.plugins: |
---|
| 774 | if hasattr(item, "post_init"): |
---|
| 775 | item.post_init() |
---|
| 776 | |
---|
| 777 | def set_perspective(self, panels): |
---|
| 778 | """ |
---|
| 779 | Sets the perspective of the GUI. |
---|
| 780 | Opens all the panels in the list, and closes |
---|
| 781 | all the others. |
---|
| 782 | |
---|
| 783 | @param panels: list of panels |
---|
| 784 | """ |
---|
| 785 | for item in self.panels: |
---|
| 786 | # Check whether this is a sticky panel |
---|
| 787 | if hasattr(self.panels[item], "ALWAYS_ON"): |
---|
| 788 | if self.panels[item].ALWAYS_ON: |
---|
| 789 | continue |
---|
| 790 | |
---|
| 791 | if self.panels[item].window_name in panels: |
---|
| 792 | if not self._mgr.GetPane(self.panels[item].window_name).IsShown(): |
---|
| 793 | self._mgr.GetPane(self.panels[item].window_name).Show() |
---|
| 794 | else: |
---|
| 795 | if self._mgr.GetPane(self.panels[item].window_name).IsShown(): |
---|
| 796 | self._mgr.GetPane(self.panels[item].window_name).Hide() |
---|
| 797 | |
---|
| 798 | self._mgr.Update() |
---|
| 799 | |
---|
[8068b52] | 800 | def choose_file(self, path=None): |
---|
[41d466f] | 801 | """ |
---|
| 802 | Functionality that belongs elsewhere |
---|
| 803 | Should add a hook to specify the preferred file type/extension. |
---|
| 804 | """ |
---|
| 805 | #TODO: clean this up |
---|
| 806 | from data_loader import choose_data_file |
---|
[8068b52] | 807 | |
---|
| 808 | # Choose a file path |
---|
| 809 | if path==None: |
---|
| 810 | path = choose_data_file(self, self._default_save_location) |
---|
| 811 | |
---|
[2310d69] | 812 | if not path==None: |
---|
| 813 | try: |
---|
| 814 | self._default_save_location = os.path.dirname(path) |
---|
[b0eee0f0] | 815 | |
---|
[25ccf33] | 816 | #self.n_fileOpen += 1 |
---|
[b0eee0f0] | 817 | if self.n_fileOpen==1: |
---|
| 818 | pos= self.filemenu.GetMenuItemCount()-1 |
---|
| 819 | #self.filemenu.InsertSeparator(pos ) |
---|
| 820 | |
---|
| 821 | id = wx.NewId() |
---|
| 822 | filename= os.path.basename(path) |
---|
| 823 | dir= os.path.split(self._default_save_location)[1] |
---|
| 824 | title= str(os.path.join(dir,filename )) |
---|
| 825 | menuitem_name = str(self.n_fileOpen)+". "+ title |
---|
| 826 | position= self.filemenu.GetMenuItemCount()-2 |
---|
| 827 | #self.filemenu.Insert(id=id, pos= position,text=menuitem_name,help=str(path) ) |
---|
| 828 | #self.filePathList.append(( id, menuitem_name, path, title)) |
---|
| 829 | #wx.EVT_MENU(self, id, self._onreloaFile) |
---|
| 830 | |
---|
| 831 | ## construct menu item for open file |
---|
| 832 | if self.n_fileOpen == self.n_maxfileopen +1: |
---|
| 833 | ## reach the maximun number of path to store |
---|
| 834 | self.n_fileOpen = 0 |
---|
| 835 | id, menuitem_name , path, title = self.filePathList[0] |
---|
| 836 | self.filemenu.Delete(id) |
---|
| 837 | self.filePathList.pop(0) |
---|
| 838 | for item in self.filePathList: |
---|
| 839 | id, menuitem_name , path, title = item |
---|
| 840 | self.n_fileOpen += 1 |
---|
| 841 | label = str(self.n_fileOpen)+". "+ title |
---|
| 842 | #self.filemenu.FindItemById(id).SetItemLabel(label) |
---|
| 843 | |
---|
| 844 | |
---|
[2310d69] | 845 | except: |
---|
[b0eee0f0] | 846 | raise |
---|
| 847 | #pass |
---|
[2310d69] | 848 | return path |
---|
| 849 | |
---|
| 850 | def load_ascii_1D(self, path): |
---|
| 851 | from data_loader import load_ascii_1D |
---|
| 852 | return load_ascii_1D(path) |
---|
[41d466f] | 853 | |
---|
| 854 | class DefaultPanel(wx.Panel): |
---|
| 855 | """ |
---|
| 856 | Defines the API for a panels to work with |
---|
| 857 | the GUI manager |
---|
| 858 | """ |
---|
| 859 | ## Internal nickname for the window, used by the AUI manager |
---|
| 860 | window_name = "default" |
---|
| 861 | ## Name to appear on the window title bar |
---|
| 862 | window_caption = "Welcome panel" |
---|
| 863 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
| 864 | CENTER_PANE = True |
---|
| 865 | |
---|
| 866 | |
---|
| 867 | # Toy application to test this Frame |
---|
| 868 | class ViewApp(wx.App): |
---|
| 869 | def OnInit(self): |
---|
| 870 | #from gui_manager import ViewerFrame |
---|
| 871 | self.frame = ViewerFrame(None, -1, config.__appname__) |
---|
| 872 | self.frame.Show(True) |
---|
| 873 | |
---|
| 874 | if hasattr(self.frame, 'special'): |
---|
| 875 | print "Special?", self.frame.special.__class__.__name__ |
---|
| 876 | self.frame.special.SetCurrent() |
---|
| 877 | self.SetTopWindow(self.frame) |
---|
| 878 | return True |
---|
| 879 | |
---|
| 880 | def set_manager(self, manager): |
---|
| 881 | """ |
---|
| 882 | Sets a reference to the application manager |
---|
| 883 | of the GUI manager (Frame) |
---|
| 884 | """ |
---|
| 885 | self.frame.set_manager(manager) |
---|
| 886 | |
---|
[278cc25] | 887 | def build_gui(self): |
---|
| 888 | """ |
---|
| 889 | Build the GUI |
---|
| 890 | """ |
---|
| 891 | self.frame.build_gui() |
---|
| 892 | self.frame.post_init() |
---|
| 893 | |
---|
| 894 | def add_perspective(self, perspective): |
---|
| 895 | """ |
---|
| 896 | Manually add a perspective to the application GUI |
---|
| 897 | """ |
---|
| 898 | self.frame.add_perspective(perspective) |
---|
| 899 | |
---|
[41d466f] | 900 | |
---|
| 901 | if __name__ == "__main__": |
---|
| 902 | app = ViewApp(0) |
---|
| 903 | app.MainLoop() |
---|