[41d466f] | 1 | |
---|
[d955bf19] | 2 | ################################################################################ |
---|
| 3 | #This software was developed by the University of Tennessee as part of the |
---|
| 4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 5 | #project funded by the US National Science Foundation. |
---|
| 6 | # |
---|
| 7 | #See the license text in license.txt |
---|
| 8 | # |
---|
| 9 | #copyright 2008, University of Tennessee |
---|
| 10 | ################################################################################ |
---|
[7681bac] | 11 | |
---|
[d68c655] | 12 | |
---|
[41d466f] | 13 | import wx |
---|
| 14 | import wx.aui |
---|
[32c0841] | 15 | import os |
---|
| 16 | import sys |
---|
[b0eee0f0] | 17 | import xml |
---|
[db10f97] | 18 | |
---|
[41d466f] | 19 | try: |
---|
| 20 | # Try to find a local config |
---|
| 21 | import imp |
---|
| 22 | path = os.getcwd() |
---|
[cbb2e40] | 23 | if(os.path.isfile("%s/%s.py" % (path, 'local_config'))) or \ |
---|
[32c0841] | 24 | (os.path.isfile("%s/%s.pyc" % (path, 'local_config'))): |
---|
| 25 | fObj, path, descr = imp.find_module('local_config', [path]) |
---|
| 26 | config = imp.load_module('local_config', fObj, path, descr) |
---|
[278cc25] | 27 | else: |
---|
[cbb2e40] | 28 | # Try simply importing local_config |
---|
| 29 | import local_config as config |
---|
[41d466f] | 30 | except: |
---|
| 31 | # Didn't find local config, load the default |
---|
| 32 | import config |
---|
[4e9583c] | 33 | |
---|
| 34 | import warnings |
---|
| 35 | warnings.simplefilter("ignore") |
---|
[3c44c66] | 36 | |
---|
[4e9583c] | 37 | import logging |
---|
[32c0841] | 38 | |
---|
[f444b20] | 39 | from sans.guiframe.events import EVT_STATUS |
---|
| 40 | from sans.guiframe.events import EVT_ADD_MANY_DATA |
---|
| 41 | from sans.guiframe.events import StatusEvent |
---|
| 42 | from sans.guiframe.events import NewPlotEvent |
---|
[52b8b74] | 43 | from sans.guiframe.gui_style import * |
---|
| 44 | from sans.guiframe.events import NewLoadedDataEvent |
---|
[3feed3e] | 45 | from sans.guiframe.data_panel import DataPanel |
---|
[b5ca223] | 46 | |
---|
[3feed3e] | 47 | STATE_FILE_EXT = ['.inv', '.fitv', '.prv'] |
---|
[f444b20] | 48 | DATA_MANAGER = False |
---|
| 49 | AUTO_PLOT = False |
---|
| 50 | AUTO_SET_DATA = True |
---|
[3feed3e] | 51 | PLOPANEL_WIDTH = 400 |
---|
| 52 | PLOPANEL_HEIGTH = 400 |
---|
| 53 | |
---|
[7681bac] | 54 | |
---|
[213892bc] | 55 | |
---|
[41d466f] | 56 | class ViewerFrame(wx.Frame): |
---|
| 57 | """ |
---|
[d955bf19] | 58 | Main application frame |
---|
[41d466f] | 59 | """ |
---|
[f444b20] | 60 | |
---|
| 61 | def __init__(self, parent, id, title, |
---|
[52b8b74] | 62 | window_height=300, window_width=300, |
---|
[7a67e075] | 63 | gui_style=GUIFRAME.DEFAULT_STYLE): |
---|
[41d466f] | 64 | """ |
---|
[d955bf19] | 65 | Initialize the Frame object |
---|
[41d466f] | 66 | """ |
---|
[b7c7a1c] | 67 | |
---|
[32c0841] | 68 | wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, |
---|
| 69 | size=(window_width, window_height)) |
---|
[d0802c3] | 70 | # Preferred window size |
---|
| 71 | self._window_height = window_height |
---|
| 72 | self._window_width = window_width |
---|
[52b8b74] | 73 | self.__gui_style = gui_style |
---|
[41d466f] | 74 | |
---|
[fc2b91a] | 75 | # Logging info |
---|
| 76 | logging.basicConfig(level=logging.DEBUG, |
---|
| 77 | format='%(asctime)s %(levelname)s %(message)s', |
---|
| 78 | filename='sans_app.log', |
---|
| 79 | filemode='w') |
---|
[c44e7cc] | 80 | path = os.path.dirname(__file__) |
---|
[32c0841] | 81 | temp_path = os.path.join(path,'images') |
---|
[c44e7cc] | 82 | ico_file = os.path.join(temp_path,'ball.ico') |
---|
[278cc25] | 83 | if os.path.isfile(ico_file): |
---|
| 84 | self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO)) |
---|
[cbb2e40] | 85 | else: |
---|
[32c0841] | 86 | temp_path = os.path.join(os.getcwd(),'images') |
---|
[c44e7cc] | 87 | ico_file = os.path.join(temp_path,'ball.ico') |
---|
[cbb2e40] | 88 | if os.path.isfile(ico_file): |
---|
| 89 | self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO)) |
---|
[41d466f] | 90 | |
---|
| 91 | ## Application manager |
---|
| 92 | self.app_manager = None |
---|
[32c0841] | 93 | self._mgr = None |
---|
[f444b20] | 94 | #add current perpsective |
---|
| 95 | self._current_perspective = None |
---|
[52b8b74] | 96 | self._plotting_plugin = None |
---|
| 97 | self._data_plugin = None |
---|
| 98 | #Menu bar and item |
---|
| 99 | self._menubar = None |
---|
| 100 | self._file_menu = None |
---|
| 101 | self._data_menu = None |
---|
| 102 | self._window_menu = None |
---|
| 103 | self._window_menu = None |
---|
[c5e84fb] | 104 | self._help_menu = None |
---|
| 105 | self._tool_menu = None |
---|
[41d466f] | 106 | ## Find plug-ins |
---|
| 107 | # Modify this so that we can specify the directory to look into |
---|
[32c0841] | 108 | self.plugins = [] |
---|
[b7c7a1c] | 109 | #add local plugin |
---|
[52b8b74] | 110 | self.plugins += self._get_local_plugins() |
---|
[a88ac04] | 111 | self.plugins += self._find_plugins() |
---|
[41d466f] | 112 | ## List of panels |
---|
| 113 | self.panels = {} |
---|
| 114 | |
---|
| 115 | ## Next available ID for wx gui events |
---|
[2310d69] | 116 | #TODO: No longer used - remove all calls to this |
---|
[41d466f] | 117 | self.next_id = 20000 |
---|
| 118 | |
---|
[2310d69] | 119 | # Default locations |
---|
| 120 | self._default_save_location = os.getcwd() |
---|
[f444b20] | 121 | |
---|
[f9e803e] | 122 | # Welcome panel |
---|
| 123 | self.defaultPanel = None |
---|
[b91c736] | 124 | #panel on focus |
---|
| 125 | self.panel_on_focus = None |
---|
[213892bc] | 126 | #data manager |
---|
| 127 | from data_manager import DataManager |
---|
| 128 | self._data_manager = DataManager() |
---|
| 129 | self._data_panel = DataPanel(parent=self) |
---|
| 130 | if self.panel_on_focus is not None: |
---|
| 131 | self._data_panel.set_panel_on_focus(self.panel_on_focus.window_name) |
---|
[a0d56d5] | 132 | # Check for update |
---|
[af20f6b] | 133 | #self._check_update(None) |
---|
[278cc25] | 134 | # Register the close event so it calls our own method |
---|
| 135 | wx.EVT_CLOSE(self, self._onClose) |
---|
| 136 | # Register to status events |
---|
| 137 | self.Bind(EVT_STATUS, self._on_status_event) |
---|
[b91c736] | 138 | #Register add extra data on the same panel event on load |
---|
| 139 | self.Bind(EVT_ADD_MANY_DATA, self.set_panel_on_focus) |
---|
| 140 | |
---|
| 141 | def set_panel_on_focus(self, event): |
---|
| 142 | """ |
---|
[d955bf19] | 143 | Store reference to the last panel on focus |
---|
[b91c736] | 144 | """ |
---|
[213892bc] | 145 | print "set_panel_on_focus", event.panel |
---|
[b91c736] | 146 | self.panel_on_focus = event.panel |
---|
[213892bc] | 147 | if self.panel_on_focus is not None and self._data_panel is not None: |
---|
| 148 | self._data_panel.set_panel_on_focus(self.panel_on_focus.window_name) |
---|
| 149 | |
---|
[278cc25] | 150 | def build_gui(self): |
---|
[d955bf19] | 151 | """ |
---|
| 152 | """ |
---|
[41d466f] | 153 | # Set up the layout |
---|
| 154 | self._setup_layout() |
---|
| 155 | |
---|
| 156 | # Set up the menu |
---|
| 157 | self._setup_menus() |
---|
[c9454bb] | 158 | #self.Fit() |
---|
[af20f6b] | 159 | #self._check_update(None) |
---|
[41d466f] | 160 | |
---|
| 161 | def _setup_layout(self): |
---|
| 162 | """ |
---|
[d955bf19] | 163 | Set up the layout |
---|
[41d466f] | 164 | """ |
---|
| 165 | # Status bar |
---|
[db10f97] | 166 | from statusbar import StatusBar |
---|
| 167 | self.sb = StatusBar(self, wx.ID_ANY) |
---|
[dd66fbd] | 168 | self.SetStatusBar(self.sb) |
---|
[41d466f] | 169 | # Add panel |
---|
[52b8b74] | 170 | default_flag = wx.aui.AUI_MGR_DEFAULT| wx.aui.AUI_MGR_ALLOW_ACTIVE_PANE |
---|
| 171 | self._mgr = wx.aui.AuiManager(self, flags=default_flag) |
---|
[7a67e075] | 172 | |
---|
[41d466f] | 173 | # Load panels |
---|
| 174 | self._load_panels() |
---|
[c5e84fb] | 175 | self.set_default_perspective() |
---|
[41d466f] | 176 | self._mgr.Update() |
---|
[db10f97] | 177 | |
---|
| 178 | def SetStatusText(self, *args, **kwds): |
---|
[d955bf19] | 179 | """ |
---|
| 180 | """ |
---|
[db10f97] | 181 | number = self.sb.get_msg_position() |
---|
| 182 | wx.Frame.SetStatusText(number=number, *args, **kwds) |
---|
| 183 | |
---|
| 184 | def PopStatusText(self, *args, **kwds): |
---|
[d955bf19] | 185 | """ |
---|
| 186 | """ |
---|
[db10f97] | 187 | field = self.sb.get_msg_position() |
---|
| 188 | wx.Frame.PopStatusText(field=field) |
---|
| 189 | |
---|
| 190 | def PushStatusText(self, *args, **kwds): |
---|
[d955bf19] | 191 | """ |
---|
| 192 | """ |
---|
[db10f97] | 193 | field = self.sb.get_msg_position() |
---|
[32c0841] | 194 | wx.Frame.PushStatusText(self, field=field, string=string) |
---|
[278cc25] | 195 | |
---|
| 196 | def add_perspective(self, plugin): |
---|
| 197 | """ |
---|
[d955bf19] | 198 | Add a perspective if it doesn't already |
---|
| 199 | exist. |
---|
[278cc25] | 200 | """ |
---|
| 201 | is_loaded = False |
---|
| 202 | for item in self.plugins: |
---|
[32c0841] | 203 | if plugin.__class__ == item.__class__: |
---|
[f444b20] | 204 | msg = "Plugin %s already loaded" % plugin.__class__.__name__ |
---|
| 205 | logging.info(msg) |
---|
[32c0841] | 206 | is_loaded = True |
---|
[278cc25] | 207 | if not is_loaded: |
---|
| 208 | self.plugins.append(plugin) |
---|
[41d466f] | 209 | |
---|
[52b8b74] | 210 | def _get_local_plugins(self): |
---|
| 211 | """ |
---|
| 212 | get plugins local to guiframe and others |
---|
| 213 | """ |
---|
| 214 | plugins = [] |
---|
| 215 | #import guiframe local plugins |
---|
[7a67e075] | 216 | #check if the style contain guiframe.dataloader |
---|
| 217 | style1 = self.__gui_style & GUIFRAME.DATALOADER_ON |
---|
[3feed3e] | 218 | style2 = self.__gui_style & GUIFRAME.PLOTTING_ON |
---|
[7a67e075] | 219 | if style1 == GUIFRAME.DATALOADER_ON: |
---|
[52b8b74] | 220 | try: |
---|
| 221 | from sans.guiframe.local_perspectives.data_loader import data_loader |
---|
[3feed3e] | 222 | self._data_plugin = data_loader.Plugin() |
---|
[52b8b74] | 223 | plugins.append(self._data_plugin) |
---|
| 224 | except: |
---|
[3feed3e] | 225 | msg = "ViewerFrame._get_local_plugins:" |
---|
[52b8b74] | 226 | msg += "cannot import dataloader plugin.\n %s" % sys.exc_value |
---|
| 227 | logging.error(msg) |
---|
[3feed3e] | 228 | if style2 == GUIFRAME.PLOTTING_ON: |
---|
[52b8b74] | 229 | try: |
---|
| 230 | from sans.guiframe.local_perspectives.plotting import plotting |
---|
| 231 | self._plotting_plugin = plotting.Plugin() |
---|
| 232 | plugins.append(self._plotting_plugin) |
---|
| 233 | except: |
---|
[3feed3e] | 234 | msg = "ViewerFrame._get_local_plugins:" |
---|
[52b8b74] | 235 | msg += "cannot import plotting plugin.\n %s" % sys.exc_value |
---|
| 236 | logging.error(msg) |
---|
[3feed3e] | 237 | |
---|
[52b8b74] | 238 | return plugins |
---|
| 239 | |
---|
[41d466f] | 240 | def _find_plugins(self, dir="perspectives"): |
---|
| 241 | """ |
---|
[d955bf19] | 242 | Find available perspective plug-ins |
---|
| 243 | |
---|
| 244 | :param dir: directory in which to look for plug-ins |
---|
| 245 | |
---|
| 246 | :return: list of plug-ins |
---|
| 247 | |
---|
[41d466f] | 248 | """ |
---|
| 249 | import imp |
---|
| 250 | plugins = [] |
---|
| 251 | # Go through files in panels directory |
---|
| 252 | try: |
---|
| 253 | list = os.listdir(dir) |
---|
[a88ac04] | 254 | ## the default panel is the panel is the last plugin added |
---|
| 255 | for item in list: |
---|
[41d466f] | 256 | toks = os.path.splitext(os.path.basename(item)) |
---|
| 257 | name = None |
---|
| 258 | if not toks[0] == '__init__': |
---|
| 259 | |
---|
[32c0841] | 260 | if toks[1] == '.py' or toks[1] == '': |
---|
[41d466f] | 261 | name = toks[0] |
---|
| 262 | |
---|
| 263 | path = [os.path.abspath(dir)] |
---|
| 264 | file = None |
---|
| 265 | try: |
---|
[32c0841] | 266 | if toks[1] == '': |
---|
[41d466f] | 267 | mod_path = '.'.join([dir, name]) |
---|
[32c0841] | 268 | module = __import__(mod_path, globals(), |
---|
| 269 | locals(), [name]) |
---|
[41d466f] | 270 | else: |
---|
| 271 | (file, path, info) = imp.find_module(name, path) |
---|
[32c0841] | 272 | module = imp.load_module( name, file, item, info) |
---|
[41d466f] | 273 | if hasattr(module, "PLUGIN_ID"): |
---|
[f444b20] | 274 | try: |
---|
| 275 | plug = module.Plugin() |
---|
| 276 | if plug.set_default_perspective(): |
---|
| 277 | self._current_perspective = plug |
---|
| 278 | plugins.append(plug) |
---|
[32c0841] | 279 | msg = "Found plug-in: %s" % module.PLUGIN_ID |
---|
| 280 | logging.info(msg) |
---|
[41d466f] | 281 | except: |
---|
[32c0841] | 282 | msg = "Error accessing PluginPanel" |
---|
| 283 | msg += " in %s\n %s" % (name, sys.exc_value) |
---|
| 284 | config.printEVT(msg) |
---|
[41d466f] | 285 | except: |
---|
[3feed3e] | 286 | print sys.exc_value |
---|
[32c0841] | 287 | msg = "ViewerFrame._find_plugins: %s" % sys.exc_value |
---|
| 288 | logging.error(msg) |
---|
[41d466f] | 289 | finally: |
---|
[32c0841] | 290 | if not file == None: |
---|
[41d466f] | 291 | file.close() |
---|
| 292 | except: |
---|
[32c0841] | 293 | # Should raise and catch at a higher level and |
---|
| 294 | # display error on status bar |
---|
[41d466f] | 295 | pass |
---|
| 296 | return plugins |
---|
| 297 | |
---|
[f9e803e] | 298 | def set_welcome_panel(self, panel_class): |
---|
| 299 | """ |
---|
[d955bf19] | 300 | Sets the default panel as the given welcome panel |
---|
| 301 | |
---|
| 302 | :param panel_class: class of the welcome panel to be instantiated |
---|
| 303 | |
---|
[f9e803e] | 304 | """ |
---|
[d955bf19] | 305 | self.defaultPanel = panel_class(self, -1, style=wx.RAISED_BORDER) |
---|
[b28278e] | 306 | |
---|
[3feed3e] | 307 | def _get_panels_size(self, p): |
---|
| 308 | """ |
---|
| 309 | find the proper size of the current panel |
---|
| 310 | get the proper panel width and height |
---|
| 311 | """ |
---|
| 312 | panel_height_min = self._window_height |
---|
| 313 | panel_width_min = self._window_width |
---|
| 314 | style = self.__gui_style & (GUIFRAME.MANAGER_ON) |
---|
| 315 | if self._data_panel is not None and (p == self._data_panel): |
---|
| 316 | panel_width_min = self._window_width * 4/25 |
---|
| 317 | return panel_width_min, panel_height_min |
---|
| 318 | if hasattr(p, "CENTER_PANE") and p.CENTER_PANE: |
---|
| 319 | style = self.__gui_style & (GUIFRAME.PLOTTING_ON|GUIFRAME.MANAGER_ON) |
---|
| 320 | if style == (GUIFRAME.PLOTTING_ON|GUIFRAME.MANAGER_ON): |
---|
| 321 | panel_width_min = self._window_width * 13/25 |
---|
| 322 | return panel_width_min, panel_height_min |
---|
| 323 | return panel_width_min, panel_height_min |
---|
| 324 | |
---|
[41d466f] | 325 | def _load_panels(self): |
---|
| 326 | """ |
---|
[d955bf19] | 327 | Load all panels in the panels directory |
---|
[41d466f] | 328 | """ |
---|
| 329 | |
---|
| 330 | # Look for plug-in panels |
---|
[c1469ebe] | 331 | panels = [] |
---|
[41d466f] | 332 | for item in self.plugins: |
---|
| 333 | if hasattr(item, "get_panels"): |
---|
| 334 | ps = item.get_panels(self) |
---|
| 335 | panels.extend(ps) |
---|
[3feed3e] | 336 | |
---|
[41d466f] | 337 | # Show a default panel with some help information |
---|
| 338 | # It also sets the size of the application windows |
---|
[c9454bb] | 339 | #TODO: Use this for slpash screen |
---|
[f9e803e] | 340 | if self.defaultPanel is None: |
---|
[32c0841] | 341 | self.defaultPanel = DefaultPanel(self, -1, style=wx.RAISED_BORDER) |
---|
[3feed3e] | 342 | # add a blank default panel always present |
---|
[41d466f] | 343 | self.panels["default"] = self.defaultPanel |
---|
| 344 | self._mgr.AddPane(self.defaultPanel, wx.aui.AuiPaneInfo(). |
---|
| 345 | Name("default"). |
---|
[3feed3e] | 346 | Center(). |
---|
| 347 | CloseButton(True). |
---|
[32c0841] | 348 | # This is where we set the size of |
---|
| 349 | # the application window |
---|
| 350 | BestSize(wx.Size(self._window_width, |
---|
| 351 | self._window_height)). |
---|
[41d466f] | 352 | Show()) |
---|
[3feed3e] | 353 | #add data panel |
---|
| 354 | self.panels["data_panel"] = self._data_panel |
---|
| 355 | w, h = self._get_panels_size(self._data_panel) |
---|
| 356 | self._mgr.AddPane(self._data_panel, wx.aui.AuiPaneInfo(). |
---|
| 357 | Name(self._data_panel.window_name). |
---|
| 358 | Left(). |
---|
| 359 | MinimizeButton(). |
---|
| 360 | MinSize(wx.Size(w, h)). |
---|
| 361 | Show()) |
---|
| 362 | style = self.__gui_style & GUIFRAME.MANAGER_ON |
---|
| 363 | if style != GUIFRAME.MANAGER_ON: |
---|
| 364 | self._mgr.GetPane(self.panels["data_panel"].window_name).Hide() |
---|
| 365 | |
---|
[41d466f] | 366 | # Add the panels to the AUI manager |
---|
| 367 | for panel_class in panels: |
---|
| 368 | p = panel_class |
---|
| 369 | id = wx.NewId() |
---|
[3feed3e] | 370 | w, h = self._get_panels_size(p) |
---|
[41d466f] | 371 | # Check whether we need to put this panel |
---|
| 372 | # in the center pane |
---|
[6f59a98] | 373 | if hasattr(p, "CENTER_PANE") and p.CENTER_PANE: |
---|
[41d466f] | 374 | if p.CENTER_PANE: |
---|
| 375 | self.panels[str(id)] = p |
---|
| 376 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
| 377 | Name(p.window_name).Caption(p.window_caption). |
---|
[3feed3e] | 378 | Center(). |
---|
| 379 | CloseButton(True). |
---|
| 380 | MinSize(wx.Size(w, h)). |
---|
| 381 | Hide()) |
---|
[41d466f] | 382 | else: |
---|
| 383 | self.panels[str(id)] = p |
---|
| 384 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
| 385 | Name(p.window_name).Caption(p.window_caption). |
---|
| 386 | Right(). |
---|
| 387 | Dock(). |
---|
| 388 | TopDockable(). |
---|
| 389 | BottomDockable(). |
---|
| 390 | LeftDockable(). |
---|
| 391 | RightDockable(). |
---|
| 392 | MinimizeButton(). |
---|
[3feed3e] | 393 | Hide()) |
---|
[b28278e] | 394 | |
---|
[2310d69] | 395 | def get_context_menu(self, graph=None): |
---|
[41d466f] | 396 | """ |
---|
[d955bf19] | 397 | Get the context menu items made available |
---|
| 398 | by the different plug-ins. |
---|
| 399 | This function is used by the plotting module |
---|
[41d466f] | 400 | """ |
---|
| 401 | menu_list = [] |
---|
| 402 | for item in self.plugins: |
---|
| 403 | if hasattr(item, "get_context_menu"): |
---|
[2310d69] | 404 | menu_list.extend(item.get_context_menu(graph)) |
---|
[41d466f] | 405 | return menu_list |
---|
| 406 | |
---|
| 407 | def popup_panel(self, p): |
---|
| 408 | """ |
---|
[d955bf19] | 409 | Add a panel object to the AUI manager |
---|
| 410 | |
---|
| 411 | :param p: panel object to add to the AUI manager |
---|
[41d466f] | 412 | |
---|
[d955bf19] | 413 | :return: ID of the event associated with the new panel [int] |
---|
| 414 | |
---|
| 415 | """ |
---|
[41d466f] | 416 | ID = wx.NewId() |
---|
| 417 | self.panels[str(ID)] = p |
---|
| 418 | count = 0 |
---|
| 419 | for item in self.panels: |
---|
[383189f9] | 420 | if self.panels[item].window_name.startswith(p.window_name): |
---|
[41d466f] | 421 | count += 1 |
---|
| 422 | windowname = p.window_name |
---|
| 423 | caption = p.window_caption |
---|
[32c0841] | 424 | if count > 0: |
---|
[41d466f] | 425 | windowname += str(count+1) |
---|
| 426 | caption += (' '+str(count)) |
---|
| 427 | p.window_name = windowname |
---|
| 428 | p.window_caption = caption |
---|
| 429 | |
---|
[7a67e075] | 430 | style1 = self.__gui_style & GUIFRAME.FIXED_PANEL |
---|
| 431 | style2 = self.__gui_style & GUIFRAME.FLOATING_PANEL |
---|
| 432 | if style1 == GUIFRAME.FIXED_PANEL: |
---|
| 433 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
| 434 | Name(windowname).Caption(caption). |
---|
| 435 | MinimizeButton(). |
---|
| 436 | Resizable(True). |
---|
[c5e84fb] | 437 | # Use a large best size to make sure the AUI |
---|
| 438 | # manager takes all the available space |
---|
[3feed3e] | 439 | BestSize(wx.Size(PLOPANEL_WIDTH, PLOPANEL_HEIGTH))) |
---|
| 440 | self._popup_fixed_panel(p) |
---|
[213892bc] | 441 | |
---|
[7a67e075] | 442 | elif style2 in GUIFRAME.FLOATING_PANEL: |
---|
| 443 | self._mgr.AddPane(p, wx.aui.AuiPaneInfo(). |
---|
| 444 | Name(windowname).Caption(caption). |
---|
| 445 | MinimizeButton(). |
---|
| 446 | Resizable(True). |
---|
[c5e84fb] | 447 | # Use a large best size to make sure the AUI |
---|
| 448 | # manager takes all the available space |
---|
[3feed3e] | 449 | BestSize(wx.Size(PLOPANEL_WIDTH, PLOPANEL_HEIGTH))) |
---|
| 450 | self._popup_floating_panel(p) |
---|
[7a67e075] | 451 | |
---|
[d0802c3] | 452 | pane = self._mgr.GetPane(windowname) |
---|
| 453 | self._mgr.MaximizePane(pane) |
---|
| 454 | self._mgr.RestoreMaximizedPane() |
---|
[41d466f] | 455 | # Register for showing/hiding the panel |
---|
| 456 | wx.EVT_MENU(self, ID, self._on_view) |
---|
| 457 | |
---|
| 458 | self._mgr.Update() |
---|
| 459 | return ID |
---|
| 460 | |
---|
[b7c7a1c] | 461 | def _populate_file_menu(self): |
---|
| 462 | """ |
---|
| 463 | Insert menu item under file menu |
---|
| 464 | """ |
---|
| 465 | for plugin in self.plugins: |
---|
| 466 | if len(plugin.populate_file_menu()) > 0: |
---|
| 467 | for item in plugin.populate_file_menu(): |
---|
[df23f90] | 468 | id = wx.NewId() |
---|
[f444b20] | 469 | m_name, m_hint, m_handler = item |
---|
[52b8b74] | 470 | self._filemenu.Append(id, m_name, m_hint) |
---|
[b7c7a1c] | 471 | wx.EVT_MENU(self, id, m_handler) |
---|
[52b8b74] | 472 | self._filemenu.AppendSeparator() |
---|
[b7c7a1c] | 473 | |
---|
[41d466f] | 474 | def _setup_menus(self): |
---|
| 475 | """ |
---|
[d955bf19] | 476 | Set up the application menus |
---|
[41d466f] | 477 | """ |
---|
| 478 | # Menu |
---|
[b7c7a1c] | 479 | self._menubar = wx.MenuBar() |
---|
[52b8b74] | 480 | self._add_menu_file() |
---|
| 481 | self._add_menu_data() |
---|
| 482 | self._add_menu_application() |
---|
[c5e84fb] | 483 | self._add_current_plugin_menu() |
---|
| 484 | self._add_menu_tool() |
---|
[52b8b74] | 485 | self._add_menu_window() |
---|
[c5e84fb] | 486 | self._add_help_menu() |
---|
| 487 | self.SetMenuBar(self._menubar) |
---|
| 488 | |
---|
| 489 | def _add_menu_tool(self): |
---|
| 490 | """ |
---|
| 491 | Tools menu |
---|
| 492 | Go through plug-ins and find tools to populate the tools menu |
---|
| 493 | """ |
---|
| 494 | style = self.__gui_style & GUIFRAME.TOOL_ON |
---|
| 495 | if style == GUIFRAME.TOOL_ON: |
---|
| 496 | self._tool_menu = None |
---|
| 497 | for item in self.plugins: |
---|
| 498 | if hasattr(item, "get_tools"): |
---|
| 499 | for tool in item.get_tools(): |
---|
| 500 | # Only create a menu if we have at least one tool |
---|
| 501 | if self._tool_menu is None: |
---|
| 502 | self._tool_menu = wx.Menu() |
---|
| 503 | id = wx.NewId() |
---|
| 504 | self._tool_menu.Append(id, tool[0], tool[1]) |
---|
| 505 | wx.EVT_MENU(self, id, tool[2]) |
---|
| 506 | if self._tool_menu is not None: |
---|
| 507 | self._menubar.Append(self._tool_menu, '&Tools') |
---|
| 508 | |
---|
| 509 | def _add_current_plugin_menu(self): |
---|
| 510 | """ |
---|
| 511 | add current plugin menu |
---|
| 512 | """ |
---|
| 513 | # Look for plug-in menus |
---|
| 514 | # Add available plug-in sub-menus. |
---|
[d68c655] | 515 | for item in self.plugins: |
---|
[c5e84fb] | 516 | if item != self._plotting_plugin: |
---|
| 517 | for (self.next_id, menu, name) in \ |
---|
| 518 | item.populate_menu(self.next_id, self): |
---|
| 519 | self._menubar.Append(menu, name) |
---|
| 520 | |
---|
| 521 | def _add_help_menu(self): |
---|
| 522 | """ |
---|
| 523 | add help menu |
---|
| 524 | """ |
---|
[41d466f] | 525 | # Help menu |
---|
[c5e84fb] | 526 | self._help_menu = wx.Menu() |
---|
[c1469ebe] | 527 | # add the welcome panel menu item |
---|
[629e8b9] | 528 | if self.defaultPanel is not None: |
---|
| 529 | id = wx.NewId() |
---|
[c5e84fb] | 530 | self._help_menu.Append(id, '&Welcome', '') |
---|
| 531 | self._help_menu.AppendSeparator() |
---|
[629e8b9] | 532 | wx.EVT_MENU(self, id, self.show_welcome_panel) |
---|
[fa452e4] | 533 | # Look for help item in plug-ins |
---|
| 534 | for item in self.plugins: |
---|
| 535 | if hasattr(item, "help"): |
---|
| 536 | id = wx.NewId() |
---|
[c5e84fb] | 537 | self._help_menu.Append(id,'&%s help' % item.sub_menu, '') |
---|
[fa452e4] | 538 | wx.EVT_MENU(self, id, item.help) |
---|
[41d466f] | 539 | if config._do_aboutbox: |
---|
| 540 | id = wx.NewId() |
---|
[c5e84fb] | 541 | self._help_menu.Append(id,'&About', 'Software information') |
---|
[41d466f] | 542 | wx.EVT_MENU(self, id, self._onAbout) |
---|
[c5e84fb] | 543 | |
---|
[af20f6b] | 544 | # Checking for updates needs major refactoring to work with py2exe |
---|
| 545 | # We need to make sure it doesn't hang the application if the server |
---|
| 546 | # is not up. We also need to make sure there's a proper executable to |
---|
| 547 | # run if we spawn a new background process. |
---|
| 548 | #id = wx.NewId() |
---|
[c5e84fb] | 549 | #self._help_menu.Append(id,'&Check for update', |
---|
[32c0841] | 550 | #'Check for the latest version of %s' % config.__appname__) |
---|
[af20f6b] | 551 | #wx.EVT_MENU(self, id, self._check_update) |
---|
[c5e84fb] | 552 | self._menubar.Append(self._help_menu, '&Help') |
---|
| 553 | |
---|
[52b8b74] | 554 | def _add_menu_window(self): |
---|
| 555 | """ |
---|
| 556 | add a menu window to the menu bar |
---|
| 557 | Window menu |
---|
| 558 | Attach a menu item for each panel in our |
---|
| 559 | panel list that also appears in a plug-in. |
---|
| 560 | |
---|
| 561 | Only add the panel menu if there is only one perspective and |
---|
| 562 | it has more than two panels. |
---|
| 563 | Note: the first plug-in is always the plotting plug-in. |
---|
| 564 | The first application |
---|
| 565 | #plug-in is always the second one in the list. |
---|
| 566 | """ |
---|
| 567 | self._window_menu = wx.Menu() |
---|
| 568 | if self._plotting_plugin is not None: |
---|
| 569 | for (self.next_id, menu, name) in \ |
---|
| 570 | self._plotting_plugin.populate_menu(self.next_id, self): |
---|
| 571 | self._window_menu.AppendSubMenu(menu, name) |
---|
| 572 | self._window_menu.AppendSeparator() |
---|
| 573 | self._menubar.Append(self._window_menu, '&Window') |
---|
[c5e84fb] | 574 | |
---|
[7a67e075] | 575 | style = self.__gui_style & GUIFRAME.MANAGER_ON |
---|
| 576 | if style == GUIFRAME.MANAGER_ON: |
---|
[52b8b74] | 577 | id = wx.NewId() |
---|
| 578 | self._window_menu.Append(id,'&Data Manager', '') |
---|
| 579 | wx.EVT_MENU(self, id, self.show_data_panel) |
---|
| 580 | |
---|
[7a67e075] | 581 | id = wx.NewId() |
---|
| 582 | preferences_menu = wx.Menu() |
---|
| 583 | hint = "Plot panels will floating" |
---|
| 584 | preferences_menu.Append(id, '&Floating Plot Panel', hint) |
---|
| 585 | wx.EVT_MENU(self, id, self.set_plotpanel_floating) |
---|
| 586 | id = wx.NewId() |
---|
| 587 | hint = "Plot panels will displayed within the frame" |
---|
| 588 | preferences_menu.Append(id, '&Fixed Plot Panel', hint) |
---|
| 589 | wx.EVT_MENU(self, id, self.set_plotpanel_fixed) |
---|
| 590 | id = wx.NewId() |
---|
| 591 | self._window_menu.AppendSubMenu(preferences_menu,'&Preferences') |
---|
| 592 | #wx.EVT_MENU(self, id, self.show_preferences_panel) |
---|
[52b8b74] | 593 | """ |
---|
| 594 | if len(self.plugins) == 2: |
---|
| 595 | plug = self.plugins[1] |
---|
| 596 | pers = plug.get_perspective() |
---|
| 597 | |
---|
| 598 | if len(pers) > 1: |
---|
| 599 | self._window_menu = wx.Menu() |
---|
| 600 | for item in self.panels: |
---|
| 601 | if item == 'default': |
---|
| 602 | continue |
---|
| 603 | panel = self.panels[item] |
---|
| 604 | if panel.window_name in pers: |
---|
| 605 | self._window_menu.Append(int(item), |
---|
| 606 | panel.window_caption, |
---|
| 607 | "Show %s window" % panel.window_caption) |
---|
| 608 | wx.EVT_MENU(self, int(item), self._on_view) |
---|
| 609 | self._menubar.Append(self._window_menu, '&Window') |
---|
| 610 | """ |
---|
| 611 | |
---|
| 612 | def _add_menu_application(self): |
---|
| 613 | """ |
---|
| 614 | |
---|
| 615 | # Attach a menu item for each defined perspective or application. |
---|
| 616 | # Only add the perspective menu if there are more than one perspectives |
---|
| 617 | add menu application |
---|
| 618 | """ |
---|
[7a67e075] | 619 | style = self.__gui_style & GUIFRAME.MULTIPLE_APPLICATIONS |
---|
| 620 | if style == GUIFRAME.MULTIPLE_APPLICATIONS: |
---|
[52b8b74] | 621 | p_menu = wx.Menu() |
---|
| 622 | for plug in self.plugins: |
---|
| 623 | if len(plug.get_perspective()) > 0: |
---|
| 624 | id = wx.NewId() |
---|
| 625 | p_menu.Append(id, plug.sub_menu, |
---|
| 626 | "Switch to application: %s" % plug.sub_menu) |
---|
| 627 | wx.EVT_MENU(self, id, plug.on_perspective) |
---|
| 628 | self._menubar.Append(p_menu, '&Applications') |
---|
| 629 | |
---|
| 630 | def _add_menu_file(self): |
---|
| 631 | """ |
---|
| 632 | add menu file |
---|
| 633 | """ |
---|
| 634 | # File menu |
---|
| 635 | self._filemenu = wx.Menu() |
---|
| 636 | # some menu of plugin to be seen under file menu |
---|
| 637 | self._populate_file_menu() |
---|
| 638 | id = wx.NewId() |
---|
| 639 | self._filemenu.Append(id, '&Save state into File', |
---|
| 640 | 'Save project as a SansView (svs) file') |
---|
| 641 | wx.EVT_MENU(self, id, self._on_save) |
---|
| 642 | id = wx.NewId() |
---|
| 643 | self._filemenu.Append(id, '&Quit', 'Exit') |
---|
| 644 | wx.EVT_MENU(self, id, self.Close) |
---|
| 645 | # Add sub menus |
---|
| 646 | self._menubar.Append(self._filemenu, '&File') |
---|
| 647 | |
---|
| 648 | def _add_menu_data(self): |
---|
| 649 | """ |
---|
| 650 | Add menu item item data to menu bar |
---|
| 651 | """ |
---|
| 652 | # Add menu data |
---|
| 653 | self._data_menu = wx.Menu() |
---|
| 654 | #menu for data files |
---|
| 655 | data_file_id = wx.NewId() |
---|
| 656 | data_file_hint = "load one or more data in the application" |
---|
| 657 | self._data_menu.Append(data_file_id, |
---|
| 658 | '&Load Data File(s)', data_file_hint) |
---|
| 659 | wx.EVT_MENU(self, data_file_id, self._load_data) |
---|
[3feed3e] | 660 | style = self.__gui_style & GUIFRAME.MULTIPLE_APPLICATIONS |
---|
| 661 | style1 = self.__gui_style & GUIFRAME.SINGLE_APPLICATION |
---|
| 662 | if style == GUIFRAME.MULTIPLE_APPLICATIONS: |
---|
[52b8b74] | 663 | #menu for data from folder |
---|
| 664 | data_folder_id = wx.NewId() |
---|
| 665 | data_folder_hint = "load multiple data in the application" |
---|
| 666 | self._data_menu.Append(data_folder_id, |
---|
| 667 | '&Load Data Folder', data_folder_hint) |
---|
| 668 | wx.EVT_MENU(self, data_folder_id, self._load_folder) |
---|
| 669 | self._menubar.Append(self._data_menu, '&Data') |
---|
[3feed3e] | 670 | elif style1 == GUIFRAME.SINGLE_APPLICATION: |
---|
| 671 | self._menubar.Append(self._data_menu, '&Data') |
---|
[52b8b74] | 672 | |
---|
| 673 | def _load_data(self, event): |
---|
| 674 | """ |
---|
| 675 | connect menu item load data with the first plugin that can load data |
---|
| 676 | """ |
---|
| 677 | for plug in self.plugins: |
---|
| 678 | if plug.can_load_data(): |
---|
| 679 | plug.load_data(event) |
---|
[213892bc] | 680 | self.show_data_panel(event=None) |
---|
| 681 | print "_load_data" |
---|
[52b8b74] | 682 | |
---|
| 683 | def _load_folder(self, event): |
---|
| 684 | """ |
---|
| 685 | connect menu item load data with the first plugin that can load data and |
---|
| 686 | folder |
---|
| 687 | """ |
---|
| 688 | for plug in self.plugins: |
---|
| 689 | if plug.can_load_data(): |
---|
| 690 | plug.load_folder(event) |
---|
[213892bc] | 691 | self.show_data_panel(event=None) |
---|
[52b8b74] | 692 | |
---|
[41d466f] | 693 | def _on_status_event(self, evt): |
---|
| 694 | """ |
---|
[d955bf19] | 695 | Display status message |
---|
[41d466f] | 696 | """ |
---|
[db10f97] | 697 | self.sb.set_status(event=evt) |
---|
[dd66fbd] | 698 | |
---|
[41d466f] | 699 | def _on_view(self, evt): |
---|
| 700 | """ |
---|
[d955bf19] | 701 | A panel was selected to be shown. If it's not already |
---|
| 702 | shown, display it. |
---|
| 703 | |
---|
| 704 | :param evt: menu event |
---|
| 705 | |
---|
[41d466f] | 706 | """ |
---|
| 707 | self.show_panel(evt.GetId()) |
---|
[c1469ebe] | 708 | |
---|
[b28278e] | 709 | def on_close_welcome_panel(self): |
---|
[c1469ebe] | 710 | """ |
---|
[d955bf19] | 711 | Close the welcome panel |
---|
[c1469ebe] | 712 | """ |
---|
[629e8b9] | 713 | if self.defaultPanel is None: |
---|
| 714 | return |
---|
[c1469ebe] | 715 | self._mgr.GetPane(self.panels["default"].window_name).Hide() |
---|
| 716 | self._mgr.Update() |
---|
[b28278e] | 717 | # set a default perspective |
---|
| 718 | self.set_default_perspective() |
---|
[c1469ebe] | 719 | |
---|
| 720 | def show_welcome_panel(self, event): |
---|
| 721 | """ |
---|
[d955bf19] | 722 | Display the welcome panel |
---|
[c1469ebe] | 723 | """ |
---|
[629e8b9] | 724 | if self.defaultPanel is None: |
---|
| 725 | return |
---|
[c1469ebe] | 726 | for id in self.panels.keys(): |
---|
[c5e84fb] | 727 | if id == 'default': |
---|
| 728 | # Show default panel |
---|
| 729 | if not self._mgr.GetPane(self.panels["default"].window_name).IsShown(): |
---|
| 730 | self._mgr.GetPane(self.panels["default"].window_name).Show(True) |
---|
| 731 | elif id == "data_panel": |
---|
| 732 | flag = self._mgr.GetPane(self.panels["data_panel"].window_name).IsShown() |
---|
| 733 | self._mgr.GetPane(self.panels["data_panel"].window_name).Show(flag) |
---|
| 734 | else: |
---|
| 735 | self._mgr.GetPane(self.panels[id].window_name).IsShown() |
---|
[c1469ebe] | 736 | self._mgr.GetPane(self.panels[id].window_name).Hide() |
---|
| 737 | self._mgr.Update() |
---|
[c5e84fb] | 738 | |
---|
[41d466f] | 739 | def show_panel(self, uid): |
---|
| 740 | """ |
---|
[d955bf19] | 741 | Shows the panel with the given id |
---|
| 742 | |
---|
| 743 | :param uid: unique ID number of the panel to show |
---|
| 744 | |
---|
[41d466f] | 745 | """ |
---|
| 746 | ID = str(uid) |
---|
| 747 | config.printEVT("show_panel: %s" % ID) |
---|
| 748 | if ID in self.panels.keys(): |
---|
| 749 | if not self._mgr.GetPane(self.panels[ID].window_name).IsShown(): |
---|
| 750 | self._mgr.GetPane(self.panels[ID].window_name).Show() |
---|
[383189f9] | 751 | # Hide default panel |
---|
| 752 | self._mgr.GetPane(self.panels["default"].window_name).Hide() |
---|
[41d466f] | 753 | self._mgr.Update() |
---|
[4e9583c] | 754 | |
---|
| 755 | def _on_open(self, event): |
---|
[b3644f3] | 756 | """ |
---|
| 757 | """ |
---|
[fc2b91a] | 758 | path = self.choose_file() |
---|
[4e9583c] | 759 | if path is None: |
---|
[700f9b4] | 760 | return |
---|
[b3644f3] | 761 | |
---|
[4e9583c] | 762 | from data_loader import plot_data |
---|
[9b18735] | 763 | from sans.perspectives import invariant |
---|
[4e9583c] | 764 | if path and os.path.isfile(path): |
---|
[32c0841] | 765 | basename = os.path.basename(path) |
---|
| 766 | if basename.endswith('.svs'): |
---|
| 767 | #remove panels for new states |
---|
[7a07864] | 768 | for item in self.panels: |
---|
| 769 | try: |
---|
| 770 | self.panels[item].clear_panel() |
---|
[32c0841] | 771 | except: |
---|
| 772 | pass |
---|
[7a07864] | 773 | #reset states and plot data |
---|
| 774 | for item in STATE_FILE_EXT: |
---|
[32c0841] | 775 | exec "plot_data(self, path,'%s')" % str(item) |
---|
| 776 | else: |
---|
| 777 | plot_data(self, path) |
---|
[aebc4cc] | 778 | if self.defaultPanel is not None and \ |
---|
| 779 | self._mgr.GetPane(self.panels["default"].window_name).IsShown(): |
---|
| 780 | self.on_close_welcome_panel() |
---|
[b35d3d1] | 781 | |
---|
| 782 | def _on_save(self, event): |
---|
| 783 | """ |
---|
| 784 | Save state into a file |
---|
| 785 | """ |
---|
| 786 | # Ask the user the location of the file to write to. |
---|
| 787 | |
---|
| 788 | ## Default file location for save |
---|
| 789 | self._default_save_location = os.getcwd() |
---|
| 790 | path = None |
---|
[32c0841] | 791 | dlg = wx.FileDialog(self, "Choose a file", |
---|
| 792 | self._default_save_location, "", "*.svs", wx.SAVE) |
---|
[b35d3d1] | 793 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 794 | path = dlg.GetPath() |
---|
| 795 | self._default_save_location = os.path.dirname(path) |
---|
| 796 | else: |
---|
| 797 | return None |
---|
| 798 | dlg.Destroy() |
---|
| 799 | if path is None: |
---|
| 800 | return |
---|
| 801 | # default cansas xml doc |
---|
| 802 | doc = None |
---|
| 803 | for item in self.panels: |
---|
| 804 | try: |
---|
[7a07864] | 805 | if self.panels[item].window_name == 'Invariant': |
---|
| 806 | data = self.panels[item]._data |
---|
| 807 | if data != None: |
---|
| 808 | state = self.panels[item].state |
---|
[32c0841] | 809 | manager = self.panels[item]._manager |
---|
| 810 | new_doc = manager.state_reader.write_toXML(data, state) |
---|
[7a07864] | 811 | if hasattr(doc, "firstChild"): |
---|
[32c0841] | 812 | child = new_doc.firstChild.firstChild |
---|
| 813 | doc.firstChild.appendChild(child) |
---|
[7a07864] | 814 | else: |
---|
| 815 | doc = new_doc |
---|
| 816 | elif self.panels[item].window_name == 'pr_control': |
---|
| 817 | data = self.panels[item].manager.current_plottable |
---|
| 818 | if data != None: |
---|
| 819 | state = self.panels[item].get_state() |
---|
[32c0841] | 820 | manager = self.panels[item].manager |
---|
| 821 | new_doc = manager.state_reader.write_toXML(data, state) |
---|
[7a07864] | 822 | if hasattr(doc, "firstChild"): |
---|
[32c0841] | 823 | child = new_doc.firstChild.firstChild |
---|
| 824 | doc.firstChild.appendChild(child) |
---|
[7a07864] | 825 | else: |
---|
| 826 | doc = new_doc |
---|
| 827 | elif self.panels[item].window_name == 'Fit panel': |
---|
| 828 | for index in range(self.panels[item].GetPageCount()): |
---|
| 829 | selected_page = self.panels[item].GetPage(index) |
---|
| 830 | if hasattr(selected_page,"get_data"): |
---|
| 831 | data = selected_page.get_data() |
---|
| 832 | state = selected_page.state |
---|
[32c0841] | 833 | reader = selected_page.manager.state_reader |
---|
| 834 | new_doc = reader.write_toXML(data, state) |
---|
[7a07864] | 835 | if doc != None and hasattr(doc, "firstChild"): |
---|
[32c0841] | 836 | child = new_doc.firstChild.firstChild |
---|
| 837 | doc.firstChild.appendChild(child) |
---|
[028a0e8] | 838 | else: |
---|
[7a07864] | 839 | doc = new_doc |
---|
[b35d3d1] | 840 | except: |
---|
| 841 | pass |
---|
| 842 | # Write the XML document |
---|
| 843 | if doc != None: |
---|
| 844 | fd = open(path, 'w') |
---|
| 845 | fd.write(doc.toprettyxml()) |
---|
| 846 | fd.close() |
---|
| 847 | else: |
---|
[32c0841] | 848 | #print "Nothing to save..." |
---|
[b35d3d1] | 849 | raise RuntimeError, "%s is not a SansView (.svs) file..." % path |
---|
| 850 | |
---|
[41d466f] | 851 | def _onClose(self, event): |
---|
[b0eee0f0] | 852 | """ |
---|
[d955bf19] | 853 | Store info to retrieve in xml before closing the application |
---|
[b0eee0f0] | 854 | """ |
---|
[41d466f] | 855 | wx.Exit() |
---|
| 856 | sys.exit() |
---|
[b5ca223] | 857 | |
---|
| 858 | def quit_guiframe(self): |
---|
| 859 | """ |
---|
| 860 | Pop up message to make sure the user wants to quit the application |
---|
| 861 | """ |
---|
| 862 | message = "Do you really want to quit \n" |
---|
| 863 | message += "this application?" |
---|
| 864 | dial = wx.MessageDialog(self, message, 'Question', |
---|
| 865 | wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION) |
---|
| 866 | if dial.ShowModal() == wx.ID_YES: |
---|
| 867 | return True |
---|
| 868 | else: |
---|
| 869 | return False |
---|
| 870 | |
---|
[41d466f] | 871 | def Close(self, event=None): |
---|
| 872 | """ |
---|
[d955bf19] | 873 | Quit the application |
---|
[41d466f] | 874 | """ |
---|
[b5ca223] | 875 | flag = self.quit_guiframe() |
---|
[ba535a6] | 876 | if flag: |
---|
| 877 | wx.Frame.Close(self) |
---|
| 878 | wx.Exit() |
---|
| 879 | sys.exit() |
---|
[41d466f] | 880 | |
---|
| 881 | def _check_update(self, event=None): |
---|
| 882 | """ |
---|
[d955bf19] | 883 | Check with the deployment server whether a new version |
---|
| 884 | of the application is available. |
---|
| 885 | A thread is started for the connecting with the server. The thread calls |
---|
| 886 | a call-back method when the current version number has been obtained. |
---|
[52070a1] | 887 | """ |
---|
[d68c655] | 888 | if hasattr(config, "__update_URL__"): |
---|
| 889 | import version |
---|
[32c0841] | 890 | checker = version.VersionThread(config.__update_URL__, |
---|
| 891 | self._process_version, |
---|
| 892 | baggage=event==None) |
---|
[d68c655] | 893 | checker.start() |
---|
[52070a1] | 894 | |
---|
| 895 | def _process_version(self, version, standalone=True): |
---|
| 896 | """ |
---|
[d955bf19] | 897 | Call-back method for the process of checking for updates. |
---|
| 898 | This methods is called by a VersionThread object once the current |
---|
| 899 | version number has been obtained. If the check is being done in the |
---|
| 900 | background, the user will not be notified unless there's an update. |
---|
| 901 | |
---|
| 902 | :param version: version string |
---|
| 903 | :param standalone: True of the update is being checked in |
---|
| 904 | the background, False otherwise. |
---|
| 905 | |
---|
[52070a1] | 906 | """ |
---|
| 907 | try: |
---|
[32c0841] | 908 | if cmp(version, config.__version__) > 0: |
---|
| 909 | msg = "Version %s is available! See the Help " |
---|
| 910 | msg += "menu to download it." % version |
---|
| 911 | self.SetStatusText(msg) |
---|
[52070a1] | 912 | if not standalone: |
---|
| 913 | import webbrowser |
---|
| 914 | webbrowser.open(config.__download_page__) |
---|
| 915 | else: |
---|
| 916 | if not standalone: |
---|
[32c0841] | 917 | msg = "You have the latest version" |
---|
| 918 | msg += " of %s" % config.__appname__ |
---|
| 919 | self.SetStatusText(msg) |
---|
[41d466f] | 920 | except: |
---|
[32c0841] | 921 | msg = "guiframe: could not get latest application" |
---|
| 922 | msg += " version number\n %s" % sys.exc_value |
---|
| 923 | logging.error(msg) |
---|
[52070a1] | 924 | if not standalone: |
---|
[32c0841] | 925 | msg = "Could not connect to the application server." |
---|
| 926 | msg += " Please try again later." |
---|
| 927 | self.SetStatusText(msg) |
---|
[52070a1] | 928 | |
---|
[41d466f] | 929 | def _onAbout(self, evt): |
---|
| 930 | """ |
---|
[d955bf19] | 931 | Pop up the about dialog |
---|
| 932 | |
---|
| 933 | :param evt: menu event |
---|
| 934 | |
---|
[41d466f] | 935 | """ |
---|
| 936 | if config._do_aboutbox: |
---|
| 937 | import aboutbox |
---|
| 938 | dialog = aboutbox.DialogAbout(None, -1, "") |
---|
[6ab0ad1] | 939 | dialog.ShowModal() |
---|
[4e9583c] | 940 | |
---|
[41d466f] | 941 | def set_manager(self, manager): |
---|
| 942 | """ |
---|
[d955bf19] | 943 | Sets the application manager for this frame |
---|
| 944 | |
---|
| 945 | :param manager: frame manager |
---|
[41d466f] | 946 | """ |
---|
| 947 | self.app_manager = manager |
---|
| 948 | |
---|
| 949 | def post_init(self): |
---|
| 950 | """ |
---|
[d955bf19] | 951 | This initialization method is called after the GUI |
---|
| 952 | has been created and all plug-ins loaded. It calls |
---|
| 953 | the post_init() method of each plug-in (if it exists) |
---|
| 954 | so that final initialization can be done. |
---|
[41d466f] | 955 | """ |
---|
| 956 | for item in self.plugins: |
---|
| 957 | if hasattr(item, "post_init"): |
---|
| 958 | item.post_init() |
---|
| 959 | |
---|
[b28278e] | 960 | def set_default_perspective(self): |
---|
| 961 | """ |
---|
[d955bf19] | 962 | Choose among the plugin the first plug-in that has |
---|
| 963 | "set_default_perspective" method and its return value is True will be |
---|
| 964 | as a default perspective when the welcome page is closed |
---|
[b28278e] | 965 | """ |
---|
| 966 | for item in self.plugins: |
---|
| 967 | if hasattr(item, "set_default_perspective"): |
---|
| 968 | if item.set_default_perspective(): |
---|
[749eb8a] | 969 | item.on_perspective(event=None) |
---|
[b28278e] | 970 | return |
---|
[f444b20] | 971 | |
---|
[41d466f] | 972 | def set_perspective(self, panels): |
---|
| 973 | """ |
---|
[d955bf19] | 974 | Sets the perspective of the GUI. |
---|
| 975 | Opens all the panels in the list, and closes |
---|
| 976 | all the others. |
---|
| 977 | |
---|
| 978 | :param panels: list of panels |
---|
[41d466f] | 979 | """ |
---|
[f444b20] | 980 | |
---|
[41d466f] | 981 | for item in self.panels: |
---|
| 982 | # Check whether this is a sticky panel |
---|
| 983 | if hasattr(self.panels[item], "ALWAYS_ON"): |
---|
| 984 | if self.panels[item].ALWAYS_ON: |
---|
| 985 | continue |
---|
| 986 | if self.panels[item].window_name in panels: |
---|
| 987 | if not self._mgr.GetPane(self.panels[item].window_name).IsShown(): |
---|
| 988 | self._mgr.GetPane(self.panels[item].window_name).Show() |
---|
| 989 | else: |
---|
[3feed3e] | 990 | # always show the data panel if enable |
---|
| 991 | style = self.__gui_style & GUIFRAME.MANAGER_ON |
---|
| 992 | if (style == GUIFRAME.MANAGER_ON) and self.panels[item] == self._data_panel: |
---|
| 993 | if 'data_panel' in self.panels.keys(): |
---|
| 994 | flag = self._mgr.GetPane(self.panels['data_panel'].window_name).IsShown() |
---|
| 995 | self._mgr.GetPane(self.panels['data_panel'].window_name).Show(flag) |
---|
| 996 | else: |
---|
| 997 | if self._mgr.GetPane(self.panels[item].window_name).IsShown(): |
---|
| 998 | self._mgr.GetPane(self.panels[item].window_name).Hide() |
---|
[41d466f] | 999 | self._mgr.Update() |
---|
[4e9583c] | 1000 | |
---|
| 1001 | def choose_file(self, path=None): |
---|
[41d466f] | 1002 | """ |
---|
[d955bf19] | 1003 | Functionality that belongs elsewhere |
---|
| 1004 | Should add a hook to specify the preferred file type/extension. |
---|
[41d466f] | 1005 | """ |
---|
[4e9583c] | 1006 | #TODO: clean this up |
---|
[32c0841] | 1007 | from .data_loader import choose_data_file |
---|
[8068b52] | 1008 | # Choose a file path |
---|
[32c0841] | 1009 | if path == None: |
---|
[4e9583c] | 1010 | path = choose_data_file(self, self._default_save_location) |
---|
[32c0841] | 1011 | if not path == None: |
---|
[2310d69] | 1012 | try: |
---|
[4e9583c] | 1013 | self._default_save_location = os.path.dirname(path) |
---|
[2310d69] | 1014 | except: |
---|
[83ee3851] | 1015 | pass |
---|
[2310d69] | 1016 | return path |
---|
[f444b20] | 1017 | |
---|
[213892bc] | 1018 | def show_data_panel(self, event=None): |
---|
[52b8b74] | 1019 | """ |
---|
[3feed3e] | 1020 | show the data panel |
---|
[52b8b74] | 1021 | """ |
---|
[3feed3e] | 1022 | pane = self._mgr.GetPane(self.panels["data_panel"].window_name) |
---|
[213892bc] | 1023 | #if not pane.IsShown(): |
---|
| 1024 | pane.Show(True) |
---|
| 1025 | self._mgr.Update() |
---|
[c5e84fb] | 1026 | |
---|
[f444b20] | 1027 | def add_data(self, data_list): |
---|
| 1028 | """ |
---|
| 1029 | receive a list of data . store them its data manager if possible |
---|
| 1030 | determine if data was be plot of send to data perspectives |
---|
| 1031 | """ |
---|
[52b8b74] | 1032 | #send a list of available data to plotting plugin |
---|
| 1033 | avalaible_data = [] |
---|
| 1034 | if self._data_manager is not None: |
---|
| 1035 | self._data_manager.add_data(data_list) |
---|
| 1036 | avalaible_data = self._data_manager.get_all_data() |
---|
| 1037 | |
---|
[3feed3e] | 1038 | style = self.__gui_style & GUIFRAME.MANAGER_ON |
---|
| 1039 | if style == GUIFRAME.MANAGER_ON: |
---|
| 1040 | if self._data_panel is not None: |
---|
[c5e84fb] | 1041 | data_state = self._data_manager.get_selected_data() |
---|
| 1042 | self._data_panel.load_data_list(data_state) |
---|
[3feed3e] | 1043 | self._mgr.GetPane(self._data_panel.window_name).Show(True) |
---|
| 1044 | #wait for button press from the data panel to send data |
---|
| 1045 | else: |
---|
| 1046 | #automatically send that to the current perspective |
---|
| 1047 | style = self.__gui_style & GUIFRAME.SINGLE_APPLICATION |
---|
| 1048 | if style == GUIFRAME.SINGLE_APPLICATION: |
---|
[c5e84fb] | 1049 | self.set_data(data_list) |
---|
| 1050 | |
---|
[213892bc] | 1051 | def get_data_from_panel(self, data_id, plot=False,append=False): |
---|
[c5e84fb] | 1052 | """ |
---|
| 1053 | receive a list of data key retreive the data from data manager and set |
---|
| 1054 | then to the current perspective |
---|
| 1055 | """ |
---|
[584c4c4] | 1056 | data_dict = self._data_manager.get_by_id(data_id) |
---|
| 1057 | data_list = [] |
---|
| 1058 | for data_state in data_dict.values(): |
---|
| 1059 | data_list.append(data_state.data) |
---|
| 1060 | if plot: |
---|
[213892bc] | 1061 | self.plot_data(data_list, append=append) |
---|
[584c4c4] | 1062 | else: |
---|
| 1063 | #sent data to active application |
---|
| 1064 | self.set_data(data_list=data_list) |
---|
| 1065 | |
---|
[c5e84fb] | 1066 | |
---|
| 1067 | def set_data(self, data_list): |
---|
| 1068 | """ |
---|
| 1069 | set data to current perspective |
---|
| 1070 | """ |
---|
| 1071 | if self._current_perspective is not None: |
---|
| 1072 | try: |
---|
| 1073 | self._current_perspective.set_data(data_list) |
---|
| 1074 | except: |
---|
| 1075 | msg = str(sys.exc_value) |
---|
| 1076 | wx.PostEvent(self, StatusEvent(status=msg, info="error")) |
---|
| 1077 | else: |
---|
| 1078 | msg = "Guiframe does not have a current perspective" |
---|
| 1079 | logging.info(msg) |
---|
[3feed3e] | 1080 | |
---|
[213892bc] | 1081 | def plot_data(self, data_list, append=False): |
---|
[f444b20] | 1082 | """ |
---|
| 1083 | send a list of data to plot |
---|
| 1084 | """ |
---|
[213892bc] | 1085 | if not data_list: |
---|
| 1086 | message = "Please check data to plot or append" |
---|
| 1087 | wx.PostEvent(self, StatusEvent(status=message, info='warning')) |
---|
| 1088 | return |
---|
[f444b20] | 1089 | for new_plot in data_list: |
---|
[213892bc] | 1090 | if append: |
---|
| 1091 | if self.panel_on_focus is None: |
---|
| 1092 | message = "cannot append plot. No plot panel on focus!" |
---|
| 1093 | message += "please click on any available plot to set focus" |
---|
| 1094 | wx.PostEvent(self, StatusEvent(status=message, |
---|
| 1095 | info='warning')) |
---|
| 1096 | return |
---|
| 1097 | else: |
---|
| 1098 | if self.enable_add_data(new_plot): |
---|
| 1099 | new_plot.group_id = self.panel_on_focus.group_id |
---|
[f444b20] | 1100 | wx.PostEvent(self, NewPlotEvent(plot=new_plot, |
---|
| 1101 | title=str(new_plot.title))) |
---|
| 1102 | |
---|
[213892bc] | 1103 | def add_theory(self, data_id, theory): |
---|
| 1104 | """ |
---|
| 1105 | """ |
---|
| 1106 | self._data_manager.append_theory(data_id, theory) |
---|
| 1107 | style = self.__gui_style & GUIFRAME.MANAGER_ON |
---|
| 1108 | if style == GUIFRAME.MANAGER_ON: |
---|
| 1109 | if self._data_panel is not None: |
---|
| 1110 | data_state = self._data_manager.get_by_id([data_id]) |
---|
| 1111 | self._data_panel.load_data_list(data_state) |
---|
| 1112 | |
---|
| 1113 | def delete_data(self, data_id, theory_id=None, delete_all=True): |
---|
| 1114 | """ |
---|
| 1115 | Delete data state if data_id is provide |
---|
| 1116 | delete theory created with data of id data_id if theory_id is provide |
---|
| 1117 | if delete all true: delete the all state |
---|
| 1118 | else delete theory |
---|
| 1119 | """ |
---|
| 1120 | self._data_manager.delete_data(data_id=data_id, |
---|
| 1121 | theory_id=theory_id, |
---|
| 1122 | delete_all=delete_all) |
---|
| 1123 | |
---|
[f444b20] | 1124 | def set_current_perspective(self, perspective): |
---|
| 1125 | """ |
---|
| 1126 | set the current active perspective |
---|
| 1127 | """ |
---|
| 1128 | self._current_perspective = perspective |
---|
[c5e84fb] | 1129 | name = "No current Application selected" |
---|
| 1130 | if self._current_perspective is not None and \ |
---|
| 1131 | self._data_panel is not None: |
---|
| 1132 | name = self._current_perspective.sub_menu |
---|
| 1133 | self._data_panel.set_active_perspective(name) |
---|
| 1134 | |
---|
[213892bc] | 1135 | def set_plotpanel_floating(self, event=None): |
---|
[7a67e075] | 1136 | """ |
---|
| 1137 | make the plot panel floatable |
---|
| 1138 | """ |
---|
[3feed3e] | 1139 | self.__gui_style |= GUIFRAME.FLOATING_PANEL |
---|
| 1140 | for p in self.panels.values(): |
---|
| 1141 | plot_panel = self._plotting_plugin.plot_panels |
---|
| 1142 | for p in self.panels.values(): |
---|
| 1143 | if p in plot_panel: |
---|
| 1144 | self._popup_floating_panel(p) |
---|
| 1145 | |
---|
[213892bc] | 1146 | def set_plotpanel_fixed(self, event=None): |
---|
[7a67e075] | 1147 | """ |
---|
| 1148 | make the plot panel fixed |
---|
| 1149 | """ |
---|
[3feed3e] | 1150 | self.__gui_style |= GUIFRAME.FIXED_PANEL |
---|
| 1151 | plot_panel = [] |
---|
| 1152 | if self._plotting_plugin is not None: |
---|
| 1153 | plot_panel = self._plotting_plugin.plot_panels |
---|
| 1154 | for p in self.panels.values(): |
---|
| 1155 | if p in plot_panel: |
---|
| 1156 | self._popup_fixed_panel(p) |
---|
| 1157 | |
---|
| 1158 | def _popup_fixed_panel(self, p): |
---|
| 1159 | """ |
---|
| 1160 | """ |
---|
| 1161 | style = self.__gui_style & GUIFRAME.FIXED_PANEL |
---|
| 1162 | if style == GUIFRAME.FIXED_PANEL: |
---|
| 1163 | self._mgr.GetPane(p.window_name).Floatable() |
---|
| 1164 | self._mgr.GetPane(p.window_name).Right() |
---|
| 1165 | self._mgr.GetPane(p.window_name).TopDockable(False) |
---|
| 1166 | self._mgr.GetPane(p.window_name).BottomDockable(False) |
---|
| 1167 | self._mgr.GetPane(p.window_name).LeftDockable(False) |
---|
| 1168 | self._mgr.GetPane(p.window_name).RightDockable(True) |
---|
| 1169 | flag = self._mgr.GetPane(p.window_name).IsShown() |
---|
| 1170 | self._mgr.GetPane(p.window_name).Show(flag) |
---|
| 1171 | self._mgr.Update() |
---|
| 1172 | |
---|
| 1173 | def _popup_floating_panel(self, p): |
---|
| 1174 | """ |
---|
| 1175 | """ |
---|
| 1176 | style = self.__gui_style & GUIFRAME.FLOATING_PANEL |
---|
| 1177 | if style == GUIFRAME.FLOATING_PANEL: |
---|
| 1178 | self._mgr.GetPane(p.window_name).Floatable(True) |
---|
| 1179 | self._mgr.GetPane(p.window_name).Float() |
---|
| 1180 | self._mgr.GetPane(p.window_name).Dockable(False) |
---|
| 1181 | flag = self._mgr.GetPane(p.window_name).IsShown() |
---|
| 1182 | self._mgr.GetPane(p.window_name).Show(flag) |
---|
| 1183 | self._mgr.Update() |
---|
[213892bc] | 1184 | |
---|
| 1185 | def enable_add_data(self, new_plot): |
---|
| 1186 | """ |
---|
| 1187 | Enable append data on a plot panel |
---|
| 1188 | """ |
---|
| 1189 | is_theory = len(self.panel_on_focus.plots) <= 1 and \ |
---|
| 1190 | self.panel_on_focus.plots.values()[0].__class__.__name__ == "Theory1D" |
---|
| 1191 | |
---|
| 1192 | is_data2d = hasattr(new_plot, 'data') |
---|
| 1193 | is_data1d = self.panel_on_focus.__class__.__name__ == "ModelPanel1D"\ |
---|
| 1194 | and self.panel_on_focus.group_id is not None |
---|
| 1195 | has_meta_data = hasattr(new_plot, 'meta_data') |
---|
| 1196 | |
---|
| 1197 | #disable_add_data if the data is being recovered from a saved state file. |
---|
| 1198 | is_state_data = False |
---|
| 1199 | if has_meta_data: |
---|
| 1200 | if 'invstate' in new_plot.meta_data: is_state_data = True |
---|
| 1201 | if 'prstate' in new_plot.meta_data: is_state_data = True |
---|
| 1202 | if 'fitstate' in new_plot.meta_data: is_state_data = True |
---|
| 1203 | |
---|
| 1204 | return is_data1d and not is_data2d and not is_theory and not is_state_data |
---|
[3feed3e] | 1205 | |
---|
[41d466f] | 1206 | class DefaultPanel(wx.Panel): |
---|
| 1207 | """ |
---|
[d955bf19] | 1208 | Defines the API for a panels to work with |
---|
| 1209 | the GUI manager |
---|
[41d466f] | 1210 | """ |
---|
| 1211 | ## Internal nickname for the window, used by the AUI manager |
---|
| 1212 | window_name = "default" |
---|
| 1213 | ## Name to appear on the window title bar |
---|
| 1214 | window_caption = "Welcome panel" |
---|
| 1215 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
| 1216 | CENTER_PANE = True |
---|
| 1217 | |
---|
| 1218 | |
---|
| 1219 | # Toy application to test this Frame |
---|
| 1220 | class ViewApp(wx.App): |
---|
[d955bf19] | 1221 | """ |
---|
| 1222 | """ |
---|
[41d466f] | 1223 | def OnInit(self): |
---|
[d955bf19] | 1224 | """ |
---|
| 1225 | """ |
---|
[41d466f] | 1226 | self.frame = ViewerFrame(None, -1, config.__appname__) |
---|
| 1227 | self.frame.Show(True) |
---|
| 1228 | |
---|
| 1229 | if hasattr(self.frame, 'special'): |
---|
| 1230 | self.frame.special.SetCurrent() |
---|
| 1231 | self.SetTopWindow(self.frame) |
---|
| 1232 | return True |
---|
| 1233 | |
---|
| 1234 | def set_manager(self, manager): |
---|
| 1235 | """ |
---|
[d955bf19] | 1236 | Sets a reference to the application manager |
---|
| 1237 | of the GUI manager (Frame) |
---|
[41d466f] | 1238 | """ |
---|
| 1239 | self.frame.set_manager(manager) |
---|
| 1240 | |
---|
[278cc25] | 1241 | def build_gui(self): |
---|
| 1242 | """ |
---|
[d955bf19] | 1243 | Build the GUI |
---|
[278cc25] | 1244 | """ |
---|
| 1245 | self.frame.build_gui() |
---|
| 1246 | self.frame.post_init() |
---|
| 1247 | |
---|
[f9e803e] | 1248 | def set_welcome_panel(self, panel_class): |
---|
| 1249 | """ |
---|
[d955bf19] | 1250 | Set the welcome panel |
---|
| 1251 | |
---|
| 1252 | :param panel_class: class of the welcome panel to be instantiated |
---|
| 1253 | |
---|
[f9e803e] | 1254 | """ |
---|
| 1255 | self.frame.set_welcome_panel(panel_class) |
---|
| 1256 | |
---|
[278cc25] | 1257 | def add_perspective(self, perspective): |
---|
| 1258 | """ |
---|
[d955bf19] | 1259 | Manually add a perspective to the application GUI |
---|
[278cc25] | 1260 | """ |
---|
| 1261 | self.frame.add_perspective(perspective) |
---|
| 1262 | |
---|
[41d466f] | 1263 | |
---|
| 1264 | if __name__ == "__main__": |
---|
| 1265 | app = ViewApp(0) |
---|
[32c0841] | 1266 | app.MainLoop() |
---|
| 1267 | |
---|
| 1268 | |
---|