1 | """ |
---|
2 | Gui manager: manages the widgets making up an application |
---|
3 | """ |
---|
4 | ################################################################################ |
---|
5 | #This software was developed by the University of Tennessee as part of the |
---|
6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
7 | #project funded by the US National Science Foundation. |
---|
8 | # |
---|
9 | #See the license text in license.txtz |
---|
10 | # |
---|
11 | #copyright 2008, University of Tennessee |
---|
12 | ################################################################################ |
---|
13 | |
---|
14 | |
---|
15 | import wx |
---|
16 | import wx.aui |
---|
17 | import os |
---|
18 | import sys |
---|
19 | import time |
---|
20 | import imp |
---|
21 | import warnings |
---|
22 | import re |
---|
23 | warnings.simplefilter("ignore") |
---|
24 | import logging |
---|
25 | import httplib |
---|
26 | import webbrowser |
---|
27 | |
---|
28 | |
---|
29 | from sas.guiframe.events import EVT_CATEGORY |
---|
30 | from sas.guiframe.events import EVT_STATUS |
---|
31 | from sas.guiframe.events import EVT_APPEND_BOOKMARK |
---|
32 | from sas.guiframe.events import EVT_PANEL_ON_FOCUS |
---|
33 | from sas.guiframe.events import EVT_NEW_LOAD_DATA |
---|
34 | from sas.guiframe.events import EVT_NEW_COLOR |
---|
35 | from sas.guiframe.events import StatusEvent |
---|
36 | from sas.guiframe.events import NewPlotEvent |
---|
37 | from sas.guiframe.gui_style import GUIFRAME |
---|
38 | from sas.guiframe.gui_style import GUIFRAME_ID |
---|
39 | from sas.guiframe.data_panel import DataPanel |
---|
40 | from sas.guiframe.panel_base import PanelBase |
---|
41 | from sas.guiframe.gui_toolbar import GUIToolBar |
---|
42 | from sas.guiframe.data_processor import GridFrame |
---|
43 | from sas.guiframe.events import EVT_NEW_BATCH |
---|
44 | from sas.guiframe.CategoryManager import CategoryManager |
---|
45 | from sas.dataloader.loader import Loader |
---|
46 | from matplotlib import _pylab_helpers |
---|
47 | |
---|
48 | def get_app_dir(): |
---|
49 | """ |
---|
50 | The application directory is the one where the default custom_config.py |
---|
51 | file resides. |
---|
52 | """ |
---|
53 | # First, try the directory of the executable we are running |
---|
54 | app_path = sys.path[0] |
---|
55 | if os.path.isfile(app_path): |
---|
56 | app_path = os.path.dirname(app_path) |
---|
57 | if os.path.isfile(os.path.join(app_path, "custom_config.py")): |
---|
58 | app_path = os.path.abspath(app_path) |
---|
59 | logging.info("Using application path: %s", app_path) |
---|
60 | return app_path |
---|
61 | |
---|
62 | # Next, try the current working directory |
---|
63 | if os.path.isfile(os.path.join(os.getcwd(), "custom_config.py")): |
---|
64 | logging.info("Using application path: %s", os.getcwd()) |
---|
65 | return os.path.abspath(os.getcwd()) |
---|
66 | |
---|
67 | # Finally, try the directory of the sasview module |
---|
68 | #TODO: gui_manager will have to know about sasview until we |
---|
69 | # clean all these module variables and put them into a config class |
---|
70 | # that can be passed by sasview.py. |
---|
71 | logging.info(sys.executable) |
---|
72 | logging.info(str(sys.argv)) |
---|
73 | from sas import sasview as sasview |
---|
74 | app_path = os.path.dirname(sasview.__file__) |
---|
75 | logging.info("Using application path: %s", app_path) |
---|
76 | return app_path |
---|
77 | |
---|
78 | def get_user_directory(): |
---|
79 | """ |
---|
80 | Returns the user's home directory |
---|
81 | """ |
---|
82 | userdir = os.path.join(os.path.expanduser("~"),".sasview") |
---|
83 | if not os.path.isdir(userdir): |
---|
84 | os.makedirs(userdir) |
---|
85 | return userdir |
---|
86 | |
---|
87 | def _find_local_config(file, path): |
---|
88 | """ |
---|
89 | Find configuration file for the current application |
---|
90 | """ |
---|
91 | config_module = None |
---|
92 | fObj = None |
---|
93 | try: |
---|
94 | fObj, path_config, descr = imp.find_module(file, [path]) |
---|
95 | config_module = imp.load_module(file, fObj, path_config, descr) |
---|
96 | except: |
---|
97 | logging.error("Error loading %s/%s: %s" % (path, file, sys.exc_value)) |
---|
98 | finally: |
---|
99 | if fObj is not None: |
---|
100 | fObj.close() |
---|
101 | logging.info("GuiManager loaded %s/%s" % (path, file)) |
---|
102 | return config_module |
---|
103 | |
---|
104 | # Get APP folder |
---|
105 | PATH_APP = get_app_dir() |
---|
106 | DATAPATH = PATH_APP |
---|
107 | |
---|
108 | # GUI always starts from the App folder |
---|
109 | #os.chdir(PATH_APP) |
---|
110 | # Read in the local config, which can either be with the main |
---|
111 | # application or in the installation directory |
---|
112 | config = _find_local_config('local_config', PATH_APP) |
---|
113 | if config is None: |
---|
114 | config = _find_local_config('local_config', os.getcwd()) |
---|
115 | if config is None: |
---|
116 | # Didn't find local config, load the default |
---|
117 | import sas.guiframe.config as config |
---|
118 | logging.info("using default local_config") |
---|
119 | else: |
---|
120 | logging.info("found local_config in %s" % os.getcwd()) |
---|
121 | else: |
---|
122 | logging.info("found local_config in %s" % PATH_APP) |
---|
123 | |
---|
124 | from sas.guiframe.customdir import SetupCustom |
---|
125 | c_conf_dir = SetupCustom().setup_dir(PATH_APP) |
---|
126 | custom_config = _find_local_config('custom_config', c_conf_dir) |
---|
127 | if custom_config is None: |
---|
128 | custom_config = _find_local_config('custom_config', os.getcwd()) |
---|
129 | if custom_config is None: |
---|
130 | msgConfig = "Custom_config file was not imported" |
---|
131 | logging.info(msgConfig) |
---|
132 | else: |
---|
133 | logging.info("using custom_config in %s" % os.getcwd()) |
---|
134 | else: |
---|
135 | logging.info("using custom_config from %s" % c_conf_dir) |
---|
136 | |
---|
137 | #read some constants from config |
---|
138 | APPLICATION_STATE_EXTENSION = config.APPLICATION_STATE_EXTENSION |
---|
139 | APPLICATION_NAME = config.__appname__ |
---|
140 | SPLASH_SCREEN_PATH = config.SPLASH_SCREEN_PATH |
---|
141 | WELCOME_PANEL_ON = config.WELCOME_PANEL_ON |
---|
142 | SPLASH_SCREEN_WIDTH = config.SPLASH_SCREEN_WIDTH |
---|
143 | SPLASH_SCREEN_HEIGHT = config.SPLASH_SCREEN_HEIGHT |
---|
144 | SS_MAX_DISPLAY_TIME = config.SS_MAX_DISPLAY_TIME |
---|
145 | if not WELCOME_PANEL_ON: |
---|
146 | WELCOME_PANEL_SHOW = False |
---|
147 | else: |
---|
148 | WELCOME_PANEL_SHOW = True |
---|
149 | try: |
---|
150 | DATALOADER_SHOW = custom_config.DATALOADER_SHOW |
---|
151 | TOOLBAR_SHOW = custom_config.TOOLBAR_SHOW |
---|
152 | FIXED_PANEL = custom_config.FIXED_PANEL |
---|
153 | if WELCOME_PANEL_ON: |
---|
154 | WELCOME_PANEL_SHOW = custom_config.WELCOME_PANEL_SHOW |
---|
155 | PLOPANEL_WIDTH = custom_config.PLOPANEL_WIDTH |
---|
156 | DATAPANEL_WIDTH = custom_config.DATAPANEL_WIDTH |
---|
157 | GUIFRAME_WIDTH = custom_config.GUIFRAME_WIDTH |
---|
158 | GUIFRAME_HEIGHT = custom_config.GUIFRAME_HEIGHT |
---|
159 | CONTROL_WIDTH = custom_config.CONTROL_WIDTH |
---|
160 | CONTROL_HEIGHT = custom_config.CONTROL_HEIGHT |
---|
161 | DEFAULT_PERSPECTIVE = custom_config.DEFAULT_PERSPECTIVE |
---|
162 | CLEANUP_PLOT = custom_config.CLEANUP_PLOT |
---|
163 | # custom open_path |
---|
164 | open_folder = custom_config.DEFAULT_OPEN_FOLDER |
---|
165 | if open_folder != None and os.path.isdir(open_folder): |
---|
166 | DEFAULT_OPEN_FOLDER = os.path.abspath(open_folder) |
---|
167 | else: |
---|
168 | DEFAULT_OPEN_FOLDER = PATH_APP |
---|
169 | except: |
---|
170 | DATALOADER_SHOW = True |
---|
171 | TOOLBAR_SHOW = True |
---|
172 | FIXED_PANEL = True |
---|
173 | WELCOME_PANEL_SHOW = False |
---|
174 | PLOPANEL_WIDTH = config.PLOPANEL_WIDTH |
---|
175 | DATAPANEL_WIDTH = config.DATAPANEL_WIDTH |
---|
176 | GUIFRAME_WIDTH = config.GUIFRAME_WIDTH |
---|
177 | GUIFRAME_HEIGHT = config.GUIFRAME_HEIGHT |
---|
178 | CONTROL_WIDTH = -1 |
---|
179 | CONTROL_HEIGHT = -1 |
---|
180 | DEFAULT_PERSPECTIVE = None |
---|
181 | CLEANUP_PLOT = False |
---|
182 | DEFAULT_OPEN_FOLDER = PATH_APP |
---|
183 | |
---|
184 | DEFAULT_STYLE = config.DEFAULT_STYLE |
---|
185 | |
---|
186 | PLUGIN_STATE_EXTENSIONS = config.PLUGIN_STATE_EXTENSIONS |
---|
187 | OPEN_SAVE_MENU = config.OPEN_SAVE_PROJECT_MENU |
---|
188 | VIEW_MENU = config.VIEW_MENU |
---|
189 | EDIT_MENU = config.EDIT_MENU |
---|
190 | extension_list = [] |
---|
191 | if APPLICATION_STATE_EXTENSION is not None: |
---|
192 | extension_list.append(APPLICATION_STATE_EXTENSION) |
---|
193 | EXTENSIONS = PLUGIN_STATE_EXTENSIONS + extension_list |
---|
194 | try: |
---|
195 | PLUGINS_WLIST = '|'.join(config.PLUGINS_WLIST) |
---|
196 | except: |
---|
197 | PLUGINS_WLIST = '' |
---|
198 | APPLICATION_WLIST = config.APPLICATION_WLIST |
---|
199 | IS_WIN = True |
---|
200 | IS_LINUX = False |
---|
201 | CLOSE_SHOW = True |
---|
202 | TIME_FACTOR = 2 |
---|
203 | MDI_STYLE = wx.DEFAULT_FRAME_STYLE |
---|
204 | NOT_SO_GRAPH_LIST = ["BoxSum"] |
---|
205 | PARENT_FRAME = wx.MDIParentFrame |
---|
206 | CHILD_FRAME = wx.MDIChildFrame |
---|
207 | if sys.platform.count("win32") < 1: |
---|
208 | IS_WIN = False |
---|
209 | TIME_FACTOR = 2 |
---|
210 | if int(str(wx.__version__).split('.')[0]) == 2: |
---|
211 | if int(str(wx.__version__).split('.')[1]) < 9: |
---|
212 | CLOSE_SHOW = False |
---|
213 | if sys.platform.count("darwin") < 1: |
---|
214 | IS_LINUX = True |
---|
215 | PARENT_FRAME = wx.Frame |
---|
216 | CHILD_FRAME = wx.Frame |
---|
217 | |
---|
218 | class ViewerFrame(PARENT_FRAME): |
---|
219 | """ |
---|
220 | Main application frame |
---|
221 | """ |
---|
222 | |
---|
223 | def __init__(self, parent, title, |
---|
224 | size=(GUIFRAME_WIDTH, GUIFRAME_HEIGHT), |
---|
225 | gui_style=DEFAULT_STYLE, |
---|
226 | style=wx.DEFAULT_FRAME_STYLE, |
---|
227 | pos=wx.DefaultPosition): |
---|
228 | """ |
---|
229 | Initialize the Frame object |
---|
230 | """ |
---|
231 | |
---|
232 | PARENT_FRAME.__init__(self, parent=parent, title=title, pos=pos, size=size) |
---|
233 | # title |
---|
234 | self.title = title |
---|
235 | self.__gui_style = gui_style |
---|
236 | path = os.path.dirname(__file__) |
---|
237 | temp_path = os.path.join(path,'images') |
---|
238 | ico_file = os.path.join(temp_path,'ball.ico') |
---|
239 | if os.path.isfile(ico_file): |
---|
240 | self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO)) |
---|
241 | else: |
---|
242 | temp_path = os.path.join(os.getcwd(),'images') |
---|
243 | ico_file = os.path.join(temp_path,'ball.ico') |
---|
244 | if os.path.isfile(ico_file): |
---|
245 | self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO)) |
---|
246 | else: |
---|
247 | ico_file = os.path.join(os.path.dirname(os.path.sys.path[0]), |
---|
248 | 'images', 'ball.ico') |
---|
249 | if os.path.isfile(ico_file): |
---|
250 | self.SetIcon(wx.Icon(ico_file, wx.BITMAP_TYPE_ICO)) |
---|
251 | self.path = PATH_APP |
---|
252 | self.application_name = APPLICATION_NAME |
---|
253 | ## Application manager |
---|
254 | self._input_file = None |
---|
255 | self.app_manager = None |
---|
256 | self._mgr = None |
---|
257 | #add current perpsective |
---|
258 | self._current_perspective = None |
---|
259 | self._plotting_plugin = None |
---|
260 | self._data_plugin = None |
---|
261 | #Menu bar and item |
---|
262 | self._menubar = None |
---|
263 | self._file_menu = None |
---|
264 | self._data_menu = None |
---|
265 | self._view_menu = None |
---|
266 | self._data_panel_menu = None |
---|
267 | self._help_menu = None |
---|
268 | self._tool_menu = None |
---|
269 | self._applications_menu_pos = -1 |
---|
270 | self._applications_menu_name = None |
---|
271 | self._applications_menu = None |
---|
272 | self._edit_menu = None |
---|
273 | self._toolbar_menu = None |
---|
274 | self._save_appl_menu = None |
---|
275 | #tool bar |
---|
276 | self._toolbar = None |
---|
277 | # Status bar |
---|
278 | self.sb = None |
---|
279 | # number of plugins |
---|
280 | self._num_perspectives = 0 |
---|
281 | # plot duck cleanup option |
---|
282 | self.cleanup_plots = CLEANUP_PLOT |
---|
283 | ## Find plug-ins |
---|
284 | # Modify this so that we can specify the directory to look into |
---|
285 | self.plugins = [] |
---|
286 | #add local plugin |
---|
287 | self.plugins += self._get_local_plugins() |
---|
288 | self.plugins += self._find_plugins() |
---|
289 | ## List of panels |
---|
290 | self.panels = {} |
---|
291 | # List of plot panels |
---|
292 | self.plot_panels = {} |
---|
293 | # default Graph number fot the plotpanel caption |
---|
294 | self.graph_num = 0 |
---|
295 | |
---|
296 | # Default locations |
---|
297 | self._default_save_location = DEFAULT_OPEN_FOLDER |
---|
298 | # Welcome panel |
---|
299 | self.defaultPanel = None |
---|
300 | self.welcome_panel_class = None |
---|
301 | #panel on focus |
---|
302 | self.panel_on_focus = None |
---|
303 | #control_panel on focus |
---|
304 | self.cpanel_on_focus = None |
---|
305 | |
---|
306 | self.loader = Loader() |
---|
307 | #data manager |
---|
308 | self.batch_on = False |
---|
309 | from sas.guiframe.data_manager import DataManager |
---|
310 | self._data_manager = DataManager() |
---|
311 | self._data_panel = None#DataPanel(parent=self) |
---|
312 | if self.panel_on_focus is not None: |
---|
313 | self._data_panel.set_panel_on_focus( |
---|
314 | self.panel_on_focus.window_caption) |
---|
315 | # list of plot panels in schedule to full redraw |
---|
316 | self.schedule = False |
---|
317 | #self.callback = True |
---|
318 | self._idle_count = 0 |
---|
319 | self.schedule_full_draw_list = [] |
---|
320 | self.idletimer = wx.CallLater(TIME_FACTOR, self._onDrawIdle) |
---|
321 | |
---|
322 | self.batch_frame = GridFrame(parent=self) |
---|
323 | self.batch_frame.Hide() |
---|
324 | self.on_batch_selection(event=None) |
---|
325 | self.add_icon() |
---|
326 | |
---|
327 | # Register the close event so it calls our own method |
---|
328 | wx.EVT_CLOSE(self, self.WindowClose) |
---|
329 | # Register to status events |
---|
330 | self.Bind(EVT_STATUS, self._on_status_event) |
---|
331 | #Register add extra data on the same panel event on load |
---|
332 | self.Bind(EVT_PANEL_ON_FOCUS, self.set_panel_on_focus) |
---|
333 | self.Bind(EVT_APPEND_BOOKMARK, self.append_bookmark) |
---|
334 | self.Bind(EVT_NEW_LOAD_DATA, self.on_load_data) |
---|
335 | self.Bind(EVT_NEW_BATCH, self.on_batch_selection) |
---|
336 | self.Bind(EVT_NEW_COLOR, self.on_color_selection) |
---|
337 | self.Bind(EVT_CATEGORY, self.on_change_categories) |
---|
338 | self.setup_custom_conf() |
---|
339 | # Preferred window size |
---|
340 | self._window_width, self._window_height = size |
---|
341 | |
---|
342 | def add_icon(self): |
---|
343 | """ |
---|
344 | get list of child and attempt to add the default icon |
---|
345 | """ |
---|
346 | |
---|
347 | list_children = self.GetChildren() |
---|
348 | for frame in list_children: |
---|
349 | self.put_icon(frame) |
---|
350 | |
---|
351 | def put_icon(self, frame): |
---|
352 | """ |
---|
353 | Put icon on the tap of a panel |
---|
354 | """ |
---|
355 | if hasattr(frame, "IsIconized"): |
---|
356 | if not frame.IsIconized(): |
---|
357 | try: |
---|
358 | icon = self.GetIcon() |
---|
359 | frame.SetIcon(icon) |
---|
360 | except: |
---|
361 | pass |
---|
362 | |
---|
363 | def get_client_size(self): |
---|
364 | """ |
---|
365 | return client size tuple |
---|
366 | """ |
---|
367 | width, height = self.GetClientSizeTuple() |
---|
368 | height -= 45 |
---|
369 | # Adjust toolbar height |
---|
370 | toolbar = self.GetToolBar() |
---|
371 | if toolbar != None: |
---|
372 | _, tb_h = toolbar.GetSizeTuple() |
---|
373 | height -= tb_h |
---|
374 | return width, height |
---|
375 | |
---|
376 | def on_change_categories(self, evt): |
---|
377 | # ILL |
---|
378 | fitpanel = None |
---|
379 | for item in self.plugins: |
---|
380 | if hasattr(item, "get_panels"): |
---|
381 | if hasattr(item, "fit_panel"): |
---|
382 | fitpanel = item.fit_panel |
---|
383 | |
---|
384 | if fitpanel != None: |
---|
385 | for i in range(0,fitpanel.GetPageCount()): |
---|
386 | fitpanel.GetPage(i)._populate_listbox() |
---|
387 | |
---|
388 | |
---|
389 | |
---|
390 | def on_set_batch_result(self, data_outputs, data_inputs=None, |
---|
391 | plugin_name=""): |
---|
392 | """ |
---|
393 | Display data into a grid in batch mode and show the grid |
---|
394 | """ |
---|
395 | t = time.localtime(time.time()) |
---|
396 | time_str = time.strftime("%b %d %H;%M of %Y", t) |
---|
397 | details = "File Generated by %s : %s" % (APPLICATION_NAME, |
---|
398 | str(plugin_name)) |
---|
399 | details += "on %s.\n" % time_str |
---|
400 | ext = ".csv" |
---|
401 | file_name = "Batch_" + str(plugin_name)+ "_" + time_str + ext |
---|
402 | file_name = self._default_save_location + str(file_name) |
---|
403 | |
---|
404 | self.open_with_localapp(file_name=file_name, |
---|
405 | details=details, |
---|
406 | data_inputs=data_inputs, |
---|
407 | data_outputs=data_outputs) |
---|
408 | |
---|
409 | |
---|
410 | def open_with_localapp(self, data_inputs=None, details="", file_name=None, |
---|
411 | data_outputs=None): |
---|
412 | """ |
---|
413 | Display value of data into the application grid |
---|
414 | :param data: dictionary of string and list of items |
---|
415 | """ |
---|
416 | self.batch_frame.set_data(data_inputs=data_inputs, |
---|
417 | data_outputs=data_outputs, |
---|
418 | details=details, |
---|
419 | file_name=file_name) |
---|
420 | self.show_batch_frame(None) |
---|
421 | |
---|
422 | def on_read_batch_tofile(self, base): |
---|
423 | """ |
---|
424 | Open a file dialog , extract the file to read and display values |
---|
425 | into a grid |
---|
426 | """ |
---|
427 | path = None |
---|
428 | if self._default_save_location == None: |
---|
429 | self._default_save_location = os.getcwd() |
---|
430 | wildcard = "(*.csv; *.txt)|*.csv; *.txt" |
---|
431 | dlg = wx.FileDialog(base, |
---|
432 | "Choose a file", |
---|
433 | self._default_save_location, "", |
---|
434 | wildcard) |
---|
435 | if dlg.ShowModal() == wx.ID_OK: |
---|
436 | path = dlg.GetPath() |
---|
437 | if path is not None: |
---|
438 | self._default_save_location = os.path.dirname(path) |
---|
439 | dlg.Destroy() |
---|
440 | try: |
---|
441 | self.read_batch_tofile(file_name=path) |
---|
442 | except: |
---|
443 | msg = "Error occurred when reading the file; %s\n"% path |
---|
444 | msg += "%s\n"% sys.exc_value |
---|
445 | wx.PostEvent(self, StatusEvent(status=msg, |
---|
446 | info="error")) |
---|
447 | |
---|
448 | def read_batch_tofile(self, file_name): |
---|
449 | """ |
---|
450 | Extract value from file name and Display them into a grid |
---|
451 | """ |
---|
452 | if file_name is None or file_name.strip() == "": |
---|
453 | return |
---|
454 | data = {} |
---|
455 | fd = open(file_name, 'r') |
---|
456 | _, ext = os.path.splitext(file_name) |
---|
457 | separator = None |
---|
458 | if ext.lower() == ".csv": |
---|
459 | separator = "," |
---|
460 | buffer = fd.read() |
---|
461 | lines = buffer.split('\n') |
---|
462 | fd.close() |
---|
463 | column_names_line = "" |
---|
464 | index = None |
---|
465 | details = "" |
---|
466 | for index in range(len(lines)): |
---|
467 | line = lines[index] |
---|
468 | line.strip() |
---|
469 | count = 0 |
---|
470 | if separator == None: |
---|
471 | line.replace('\t', ' ') |
---|
472 | #found the first line containing the label |
---|
473 | col_name_toks = line.split() |
---|
474 | for item in col_name_toks: |
---|
475 | if item.strip() != "": |
---|
476 | count += 1 |
---|
477 | else: |
---|
478 | line = " " |
---|
479 | elif line.find(separator) != -1: |
---|
480 | if line.count(separator) >= 2: |
---|
481 | #found the first line containing the label |
---|
482 | col_name_toks = line.split(separator) |
---|
483 | for item in col_name_toks: |
---|
484 | if item.strip() != "": |
---|
485 | count += 1 |
---|
486 | else: |
---|
487 | details += line |
---|
488 | if count >= 2: |
---|
489 | column_names_line = line |
---|
490 | break |
---|
491 | |
---|
492 | if column_names_line.strip() == "" or index is None: |
---|
493 | return |
---|
494 | |
---|
495 | col_name_toks = column_names_line.split(separator) |
---|
496 | c_index = 0 |
---|
497 | for col_index in range(len(col_name_toks)): |
---|
498 | c_name = col_name_toks[col_index] |
---|
499 | if c_name.strip() != "": |
---|
500 | # distinguish between column name and value |
---|
501 | try: |
---|
502 | float(c_name) |
---|
503 | col_name = "Column %s"% str(col_index + 1) |
---|
504 | index_min = index |
---|
505 | except: |
---|
506 | col_name = c_name |
---|
507 | index_min = index + 1 |
---|
508 | data[col_name] = [ lines[row].split(separator)[c_index] |
---|
509 | for row in range(index_min, len(lines)-1)] |
---|
510 | c_index += 1 |
---|
511 | |
---|
512 | self.open_with_localapp(data_outputs=data, data_inputs=None, |
---|
513 | file_name=file_name, details=details) |
---|
514 | |
---|
515 | def write_batch_tofile(self, data, file_name, details=""): |
---|
516 | """ |
---|
517 | Helper to write result from batch into cvs file |
---|
518 | """ |
---|
519 | self._default_save_location = os.path.dirname(file_name) |
---|
520 | name = os.path.basename(file_name) |
---|
521 | if data is None or file_name is None or file_name.strip() == "": |
---|
522 | return |
---|
523 | _, ext = os.path.splitext(name) |
---|
524 | try: |
---|
525 | fd = open(file_name, 'w') |
---|
526 | except: |
---|
527 | # On Permission denied: IOError |
---|
528 | temp_dir = get_user_directory() |
---|
529 | temp_file_name = os.path.join(temp_dir, name) |
---|
530 | fd = open(temp_file_name, 'w') |
---|
531 | separator = "\t" |
---|
532 | if ext.lower() == ".csv": |
---|
533 | separator = "," |
---|
534 | fd.write(str(details)) |
---|
535 | for col_name in data.keys(): |
---|
536 | fd.write(str(col_name)) |
---|
537 | fd.write(separator) |
---|
538 | fd.write('\n') |
---|
539 | max_list = [len(value) for value in data.values()] |
---|
540 | if len(max_list) == 0: |
---|
541 | return |
---|
542 | max_index = max(max_list) |
---|
543 | index = 0 |
---|
544 | while(index < max_index): |
---|
545 | for value_list in data.values(): |
---|
546 | if index < len(value_list): |
---|
547 | fd.write(str(value_list[index])) |
---|
548 | fd.write(separator) |
---|
549 | else: |
---|
550 | fd.write('') |
---|
551 | fd.write(separator) |
---|
552 | fd.write('\n') |
---|
553 | index += 1 |
---|
554 | fd.close() |
---|
555 | |
---|
556 | def open_with_externalapp(self, data, file_name, details=""): |
---|
557 | """ |
---|
558 | Display data in the another application , by default Excel |
---|
559 | """ |
---|
560 | if not os.path.exists(file_name): |
---|
561 | self.write_batch_tofile(data=data, file_name=file_name, |
---|
562 | details=details) |
---|
563 | try: |
---|
564 | from win32com.client import Dispatch |
---|
565 | excel_app = Dispatch('Excel.Application') |
---|
566 | wb = excel_app.Workbooks.Open(file_name) |
---|
567 | excel_app.Visible = 1 |
---|
568 | except: |
---|
569 | msg = "Error occured when calling Excel.\n" |
---|
570 | msg += "Check that Excel installed in this machine or \n" |
---|
571 | msg += "check that %s really exists.\n" % str(file_name) |
---|
572 | wx.PostEvent(self, StatusEvent(status=msg, |
---|
573 | info="error")) |
---|
574 | |
---|
575 | |
---|
576 | def on_batch_selection(self, event=None): |
---|
577 | """ |
---|
578 | :param event: contains parameter enable . when enable is set to True |
---|
579 | the application is in Batch mode |
---|
580 | else the application is default mode(single mode) |
---|
581 | """ |
---|
582 | if event is not None: |
---|
583 | self.batch_on = event.enable |
---|
584 | for plug in self.plugins: |
---|
585 | plug.set_batch_selection(self.batch_on) |
---|
586 | |
---|
587 | def on_color_selection(self, event): |
---|
588 | """ |
---|
589 | :param event: contains parameters for id and color |
---|
590 | """ |
---|
591 | color, id = event.color, event.id |
---|
592 | for plug in self.plugins: |
---|
593 | plug.add_color(color, id) |
---|
594 | |
---|
595 | |
---|
596 | def setup_custom_conf(self): |
---|
597 | """ |
---|
598 | Set up custom configuration if exists |
---|
599 | """ |
---|
600 | if custom_config == None: |
---|
601 | return |
---|
602 | |
---|
603 | if not FIXED_PANEL: |
---|
604 | self.__gui_style &= (~GUIFRAME.FIXED_PANEL) |
---|
605 | self.__gui_style |= GUIFRAME.FLOATING_PANEL |
---|
606 | |
---|
607 | if not DATALOADER_SHOW: |
---|
608 | self.__gui_style &= (~GUIFRAME.MANAGER_ON) |
---|
609 | |
---|
610 | if not TOOLBAR_SHOW: |
---|
611 | self.__gui_style &= (~GUIFRAME.TOOLBAR_ON) |
---|
612 | |
---|
613 | if WELCOME_PANEL_SHOW: |
---|
614 | self.__gui_style |= GUIFRAME.WELCOME_PANEL_ON |
---|
615 | |
---|
616 | def set_custom_default_perspective(self): |
---|
617 | """ |
---|
618 | Set default starting perspective |
---|
619 | """ |
---|
620 | if custom_config == None: |
---|
621 | return |
---|
622 | for plugin in self.plugins: |
---|
623 | try: |
---|
624 | if plugin.sub_menu == DEFAULT_PERSPECTIVE: |
---|
625 | |
---|
626 | plugin.on_perspective(event=None) |
---|
627 | frame = plugin.get_frame() |
---|
628 | frame.Show(True) |
---|
629 | #break |
---|
630 | else: |
---|
631 | frame = plugin.get_frame() |
---|
632 | frame.Show(False) |
---|
633 | except: |
---|
634 | pass |
---|
635 | return |
---|
636 | |
---|
637 | def on_load_data(self, event): |
---|
638 | """ |
---|
639 | received an event to trigger load from data plugin |
---|
640 | """ |
---|
641 | if self._data_plugin is not None: |
---|
642 | self._data_plugin.load_data(event) |
---|
643 | |
---|
644 | def get_current_perspective(self): |
---|
645 | """ |
---|
646 | return the current perspective |
---|
647 | """ |
---|
648 | return self._current_perspective |
---|
649 | |
---|
650 | def get_save_location(self): |
---|
651 | """ |
---|
652 | return the _default_save_location |
---|
653 | """ |
---|
654 | return self._default_save_location |
---|
655 | |
---|
656 | def set_input_file(self, input_file): |
---|
657 | """ |
---|
658 | :param input_file: file to read |
---|
659 | """ |
---|
660 | self._input_file = input_file |
---|
661 | |
---|
662 | def get_data_manager(self): |
---|
663 | """ |
---|
664 | return the data manager. |
---|
665 | """ |
---|
666 | return self._data_manager |
---|
667 | |
---|
668 | def get_toolbar(self): |
---|
669 | """ |
---|
670 | return the toolbar. |
---|
671 | """ |
---|
672 | return self._toolbar |
---|
673 | |
---|
674 | def set_panel_on_focus(self, event): |
---|
675 | """ |
---|
676 | Store reference to the last panel on focus |
---|
677 | update the toolbar if available |
---|
678 | update edit menu if available |
---|
679 | """ |
---|
680 | if event != None: |
---|
681 | self.panel_on_focus = event.panel |
---|
682 | if self.panel_on_focus is not None: |
---|
683 | #Disable save application if the current panel is in batch mode |
---|
684 | flag = self.panel_on_focus.get_save_flag() |
---|
685 | if self._save_appl_menu != None: |
---|
686 | self._save_appl_menu.Enable(flag) |
---|
687 | |
---|
688 | if self.panel_on_focus not in self.plot_panels.values(): |
---|
689 | for ID in self.panels.keys(): |
---|
690 | if self.panel_on_focus != self.panels[ID]: |
---|
691 | self.panels[ID].on_kill_focus(None) |
---|
692 | |
---|
693 | if self._data_panel is not None and \ |
---|
694 | self.panel_on_focus is not None: |
---|
695 | self.set_panel_on_focus_helper() |
---|
696 | #update toolbar |
---|
697 | self._update_toolbar_helper() |
---|
698 | #update edit menu |
---|
699 | self.enable_edit_menu() |
---|
700 | |
---|
701 | def disable_app_menu(self, p_panel=None): |
---|
702 | """ |
---|
703 | Disables all menus in the menubar |
---|
704 | """ |
---|
705 | return |
---|
706 | |
---|
707 | def send_focus_to_datapanel(self, name): |
---|
708 | """ |
---|
709 | Send focusing on ID to data explorer |
---|
710 | """ |
---|
711 | if self._data_panel != None: |
---|
712 | self._data_panel.set_panel_on_focus(name) |
---|
713 | |
---|
714 | def set_panel_on_focus_helper(self): |
---|
715 | """ |
---|
716 | Helper for panel on focus with data_panel |
---|
717 | """ |
---|
718 | caption = self.panel_on_focus.window_caption |
---|
719 | self.send_focus_to_datapanel(caption) |
---|
720 | #update combo |
---|
721 | if self.panel_on_focus in self.plot_panels.values(): |
---|
722 | combo = self._data_panel.cb_plotpanel |
---|
723 | combo_title = str(self.panel_on_focus.window_caption) |
---|
724 | combo.SetStringSelection(combo_title) |
---|
725 | combo.SetToolTip( wx.ToolTip(combo_title )) |
---|
726 | elif self.panel_on_focus != self._data_panel: |
---|
727 | cpanel = self.panel_on_focus |
---|
728 | if self.cpanel_on_focus != cpanel: |
---|
729 | cpanel.on_tap_focus() |
---|
730 | self.cpanel_on_focus = self.panel_on_focus |
---|
731 | |
---|
732 | def reset_bookmark_menu(self, panel): |
---|
733 | """ |
---|
734 | Reset Bookmark menu list |
---|
735 | |
---|
736 | : param panel: a control panel or tap where the bookmark is |
---|
737 | """ |
---|
738 | cpanel = panel |
---|
739 | if self._toolbar != None and cpanel._bookmark_flag: |
---|
740 | for item in self._toolbar.get_bookmark_items(): |
---|
741 | self._toolbar.remove_bookmark_item(item) |
---|
742 | self._toolbar.add_bookmark_default() |
---|
743 | pos = 0 |
---|
744 | for bitem in cpanel.popUpMenu.GetMenuItems(): |
---|
745 | pos += 1 |
---|
746 | if pos < 3: |
---|
747 | continue |
---|
748 | id = bitem.GetId() |
---|
749 | label = bitem.GetLabel() |
---|
750 | self._toolbar.append_bookmark_item(id, label) |
---|
751 | wx.EVT_MENU(self, id, cpanel._back_to_bookmark) |
---|
752 | self._toolbar.Realize() |
---|
753 | |
---|
754 | |
---|
755 | def build_gui(self): |
---|
756 | """ |
---|
757 | Build the GUI by setting up the toolbar, menu and layout. |
---|
758 | """ |
---|
759 | # set tool bar |
---|
760 | self._setup_tool_bar() |
---|
761 | |
---|
762 | # Create the menu bar. To be filled later. |
---|
763 | # WX 3.0 needs us to create the menu bar first. |
---|
764 | self._menubar = wx.MenuBar() |
---|
765 | if wx.VERSION_STRING >= '3.0.0.0': |
---|
766 | self.SetMenuBar(self._menubar) |
---|
767 | self._add_menu_file() |
---|
768 | self._add_menu_edit() |
---|
769 | self._add_menu_view() |
---|
770 | self._add_menu_tool() |
---|
771 | # Set up the layout |
---|
772 | self._setup_layout() |
---|
773 | self._add_menu_application() |
---|
774 | |
---|
775 | # Set up the menu |
---|
776 | self._add_current_plugin_menu() |
---|
777 | self._add_help_menu() |
---|
778 | # Append item from plugin under menu file if necessary |
---|
779 | self._populate_file_menu() |
---|
780 | |
---|
781 | |
---|
782 | if not wx.VERSION_STRING >= '3.0.0.0': |
---|
783 | self.SetMenuBar(self._menubar) |
---|
784 | |
---|
785 | try: |
---|
786 | self.load_from_cmd(self._input_file) |
---|
787 | except: |
---|
788 | msg = "%s Cannot load file %s\n" %(str(APPLICATION_NAME), |
---|
789 | str(self._input_file)) |
---|
790 | msg += str(sys.exc_value) + '\n' |
---|
791 | logging.error(msg) |
---|
792 | if self._data_panel is not None and len(self.plugins) > 0: |
---|
793 | self._data_panel.fill_cbox_analysis(self.plugins) |
---|
794 | self.post_init() |
---|
795 | # Set Custom default |
---|
796 | self.set_custom_default_perspective() |
---|
797 | # Set up extra custom tool menu |
---|
798 | self._setup_extra_custom() |
---|
799 | self._check_update(None) |
---|
800 | |
---|
801 | def _setup_extra_custom(self): |
---|
802 | """ |
---|
803 | Set up toolbar and welcome view if needed |
---|
804 | """ |
---|
805 | style = self.__gui_style & GUIFRAME.TOOLBAR_ON |
---|
806 | if (style == GUIFRAME.TOOLBAR_ON) & (not self._toolbar.IsShown()): |
---|
807 | self._on_toggle_toolbar() |
---|
808 | |
---|
809 | # Set Custom deafult start page |
---|
810 | welcome_style = self.__gui_style & GUIFRAME.WELCOME_PANEL_ON |
---|
811 | if welcome_style == GUIFRAME.WELCOME_PANEL_ON: |
---|
812 | self.show_welcome_panel(None) |
---|
813 | |
---|
814 | def _setup_layout(self): |
---|
815 | """ |
---|
816 | Set up the layout |
---|
817 | """ |
---|
818 | # Status bar |
---|
819 | from sas.guiframe.gui_statusbar import StatusBar |
---|
820 | self.sb = StatusBar(self, wx.ID_ANY) |
---|
821 | self.SetStatusBar(self.sb) |
---|
822 | # Load panels |
---|
823 | self._load_panels() |
---|
824 | self.set_default_perspective() |
---|
825 | |
---|
826 | def SetStatusText(self, *args, **kwds): |
---|
827 | """ |
---|
828 | """ |
---|
829 | number = self.sb.get_msg_position() |
---|
830 | wx.Frame.SetStatusText(self, number=number, *args, **kwds) |
---|
831 | |
---|
832 | def PopStatusText(self, *args, **kwds): |
---|
833 | """ |
---|
834 | """ |
---|
835 | field = self.sb.get_msg_position() |
---|
836 | wx.Frame.PopStatusText(self, field=field) |
---|
837 | |
---|
838 | def PushStatusText(self, *args, **kwds): |
---|
839 | """ |
---|
840 | FIXME: No message is passed. What is this supposed to do? |
---|
841 | """ |
---|
842 | field = self.sb.get_msg_position() |
---|
843 | wx.Frame.PushStatusText(self, field=field, |
---|
844 | string="FIXME: PushStatusText called without text") |
---|
845 | |
---|
846 | def add_perspective(self, plugin): |
---|
847 | """ |
---|
848 | Add a perspective if it doesn't already |
---|
849 | exist. |
---|
850 | """ |
---|
851 | self._num_perspectives += 1 |
---|
852 | is_loaded = False |
---|
853 | for item in self.plugins: |
---|
854 | item.set_batch_selection(self.batch_on) |
---|
855 | if plugin.__class__ == item.__class__: |
---|
856 | msg = "Plugin %s already loaded" % plugin.sub_menu |
---|
857 | logging.info(msg) |
---|
858 | is_loaded = True |
---|
859 | if not is_loaded: |
---|
860 | self.plugins.append(plugin) |
---|
861 | msg = "Plugin %s appended" % plugin.sub_menu |
---|
862 | logging.info(msg) |
---|
863 | |
---|
864 | def _get_local_plugins(self): |
---|
865 | """ |
---|
866 | get plugins local to guiframe and others |
---|
867 | """ |
---|
868 | plugins = [] |
---|
869 | #import guiframe local plugins |
---|
870 | #check if the style contain guiframe.dataloader |
---|
871 | style1 = self.__gui_style & GUIFRAME.DATALOADER_ON |
---|
872 | style2 = self.__gui_style & GUIFRAME.PLOTTING_ON |
---|
873 | if style1 == GUIFRAME.DATALOADER_ON: |
---|
874 | try: |
---|
875 | from sas.guiframe.local_perspectives.data_loader import data_loader |
---|
876 | self._data_plugin = data_loader.Plugin() |
---|
877 | plugins.append(self._data_plugin) |
---|
878 | except: |
---|
879 | msg = "ViewerFrame._get_local_plugins:" |
---|
880 | msg += "cannot import dataloader plugin.\n %s" % sys.exc_value |
---|
881 | logging.error(msg) |
---|
882 | if style2 == GUIFRAME.PLOTTING_ON: |
---|
883 | try: |
---|
884 | from sas.guiframe.local_perspectives.plotting import plotting |
---|
885 | self._plotting_plugin = plotting.Plugin() |
---|
886 | plugins.append(self._plotting_plugin) |
---|
887 | except: |
---|
888 | msg = "ViewerFrame._get_local_plugins:" |
---|
889 | msg += "cannot import plotting plugin.\n %s" % sys.exc_value |
---|
890 | logging.error(msg) |
---|
891 | |
---|
892 | return plugins |
---|
893 | |
---|
894 | def _find_plugins(self, dir="perspectives"): |
---|
895 | """ |
---|
896 | Find available perspective plug-ins |
---|
897 | |
---|
898 | :param dir: directory in which to look for plug-ins |
---|
899 | |
---|
900 | :return: list of plug-ins |
---|
901 | |
---|
902 | """ |
---|
903 | plugins = [] |
---|
904 | # Go through files in panels directory |
---|
905 | try: |
---|
906 | list = os.listdir(dir) |
---|
907 | ## the default panel is the panel is the last plugin added |
---|
908 | for item in list: |
---|
909 | toks = os.path.splitext(os.path.basename(item)) |
---|
910 | name = '' |
---|
911 | if not toks[0] == '__init__': |
---|
912 | if toks[1] == '.py' or toks[1] == '': |
---|
913 | name = toks[0] |
---|
914 | #check the validity of the module name parsed |
---|
915 | #before trying to import it |
---|
916 | if name is None or name.strip() == '': |
---|
917 | continue |
---|
918 | path = [os.path.abspath(dir)] |
---|
919 | file = None |
---|
920 | try: |
---|
921 | if toks[1] == '': |
---|
922 | mod_path = '.'.join([dir, name]) |
---|
923 | module = __import__(mod_path, globals(), |
---|
924 | locals(), [name]) |
---|
925 | else: |
---|
926 | (file, path, info) = imp.find_module(name, path) |
---|
927 | module = imp.load_module( name, file, item, info) |
---|
928 | if hasattr(module, "PLUGIN_ID"): |
---|
929 | try: |
---|
930 | plug = module.Plugin() |
---|
931 | if plug.set_default_perspective(): |
---|
932 | self._current_perspective = plug |
---|
933 | plugins.append(plug) |
---|
934 | |
---|
935 | msg = "Found plug-in: %s" % module.PLUGIN_ID |
---|
936 | logging.info(msg) |
---|
937 | except: |
---|
938 | msg = "Error accessing PluginPanel" |
---|
939 | msg += " in %s\n %s" % (name, sys.exc_value) |
---|
940 | config.printEVT(msg) |
---|
941 | except: |
---|
942 | msg = "ViewerFrame._find_plugins: %s" % sys.exc_value |
---|
943 | logging.error(msg) |
---|
944 | finally: |
---|
945 | if not file == None: |
---|
946 | file.close() |
---|
947 | except: |
---|
948 | # Should raise and catch at a higher level and |
---|
949 | # display error on status bar |
---|
950 | pass |
---|
951 | |
---|
952 | return plugins |
---|
953 | |
---|
954 | def _get_panels_size(self, p): |
---|
955 | """ |
---|
956 | find the proper size of the current panel |
---|
957 | get the proper panel width and height |
---|
958 | """ |
---|
959 | self._window_width, self._window_height = self.get_client_size() |
---|
960 | ## Default size |
---|
961 | if DATAPANEL_WIDTH < 0: |
---|
962 | panel_width = int(self._window_width * 0.25) |
---|
963 | else: |
---|
964 | panel_width = DATAPANEL_WIDTH |
---|
965 | panel_height = int(self._window_height) |
---|
966 | style = self.__gui_style & (GUIFRAME.MANAGER_ON) |
---|
967 | if self._data_panel is not None and (p == self._data_panel): |
---|
968 | return panel_width, panel_height |
---|
969 | if hasattr(p, "CENTER_PANE") and p.CENTER_PANE: |
---|
970 | panel_width = self._window_width * 0.45 |
---|
971 | if CONTROL_WIDTH > 0: |
---|
972 | panel_width = CONTROL_WIDTH |
---|
973 | if CONTROL_HEIGHT > 0: |
---|
974 | panel_height = CONTROL_HEIGHT |
---|
975 | return panel_width, panel_height |
---|
976 | elif p == self.defaultPanel: |
---|
977 | return self._window_width, panel_height |
---|
978 | return panel_width, panel_height |
---|
979 | |
---|
980 | def _load_panels(self): |
---|
981 | """ |
---|
982 | Load all panels in the panels directory |
---|
983 | """ |
---|
984 | # Look for plug-in panels |
---|
985 | panels = [] |
---|
986 | if wx.VERSION_STRING >= '3.0.0.0': |
---|
987 | mac_pos_y = 85 |
---|
988 | else: |
---|
989 | mac_pos_y = 40 |
---|
990 | for item in self.plugins: |
---|
991 | if hasattr(item, "get_panels"): |
---|
992 | ps = item.get_panels(self) |
---|
993 | panels.extend(ps) |
---|
994 | |
---|
995 | # Set up welcome panel |
---|
996 | #TODO: this needs serious simplification |
---|
997 | if self.welcome_panel_class is not None: |
---|
998 | welcome_panel = MDIFrame(self, None, 'None', (100, 200)) |
---|
999 | self.defaultPanel = self.welcome_panel_class(welcome_panel, -1, style=wx.RAISED_BORDER) |
---|
1000 | welcome_panel.set_panel(self.defaultPanel) |
---|
1001 | self.defaultPanel.set_frame(welcome_panel) |
---|
1002 | welcome_panel.Show(False) |
---|
1003 | |
---|
1004 | self.panels["default"] = self.defaultPanel |
---|
1005 | size_t_bar = 70 |
---|
1006 | if IS_LINUX: |
---|
1007 | size_t_bar = 115 |
---|
1008 | if self.defaultPanel is not None: |
---|
1009 | w, h = self._get_panels_size(self.defaultPanel) |
---|
1010 | frame = self.defaultPanel.get_frame() |
---|
1011 | frame.SetSize((self._window_width, self._window_height)) |
---|
1012 | if not IS_WIN: |
---|
1013 | frame.SetPosition((0, mac_pos_y + size_t_bar)) |
---|
1014 | frame.Show(True) |
---|
1015 | #add data panel |
---|
1016 | win = MDIFrame(self, None, 'None', (100, 200)) |
---|
1017 | data_panel = DataPanel(parent=win, id=-1) |
---|
1018 | win.set_panel(data_panel) |
---|
1019 | self.panels["data_panel"] = data_panel |
---|
1020 | self._data_panel = data_panel |
---|
1021 | d_panel_width, h = self._get_panels_size(self._data_panel) |
---|
1022 | win.SetSize((d_panel_width, h)) |
---|
1023 | is_visible = self.__gui_style & GUIFRAME.MANAGER_ON == GUIFRAME.MANAGER_ON |
---|
1024 | if IS_WIN: |
---|
1025 | win.SetPosition((0, 0)) |
---|
1026 | else: |
---|
1027 | win.SetPosition((0, mac_pos_y + size_t_bar)) |
---|
1028 | win.Show(is_visible) |
---|
1029 | # Add the panels to the AUI manager |
---|
1030 | for panel_class in panels: |
---|
1031 | frame = panel_class.get_frame() |
---|
1032 | id = wx.NewId() |
---|
1033 | # Check whether we need to put this panel in the center pane |
---|
1034 | if hasattr(panel_class, "CENTER_PANE") and panel_class.CENTER_PANE: |
---|
1035 | w, h = self._get_panels_size(panel_class) |
---|
1036 | if panel_class.CENTER_PANE: |
---|
1037 | self.panels[str(id)] = panel_class |
---|
1038 | _, pos_y = frame.GetPositionTuple() |
---|
1039 | frame.SetPosition((d_panel_width + 1, pos_y)) |
---|
1040 | frame.SetSize((w, h)) |
---|
1041 | frame.Show(False) |
---|
1042 | elif panel_class == self._data_panel: |
---|
1043 | panel_class.frame.Show(is_visible) |
---|
1044 | continue |
---|
1045 | else: |
---|
1046 | self.panels[str(id)] = panel_class |
---|
1047 | frame.SetSize((w, h)) |
---|
1048 | frame.Show(False) |
---|
1049 | if IS_WIN: |
---|
1050 | frame.SetPosition((d_panel_width + 1, 0)) |
---|
1051 | else: |
---|
1052 | frame.SetPosition((d_panel_width + 1, mac_pos_y + size_t_bar)) |
---|
1053 | |
---|
1054 | if not IS_WIN: |
---|
1055 | win_height = mac_pos_y |
---|
1056 | if IS_LINUX: |
---|
1057 | if wx.VERSION_STRING >= '3.0.0.0': |
---|
1058 | win_height = mac_pos_y + 10 |
---|
1059 | else: |
---|
1060 | win_height = mac_pos_y + 55 |
---|
1061 | self.SetMaxSize((-1, win_height)) |
---|
1062 | else: |
---|
1063 | self.SetSize((self._window_width, win_height)) |
---|
1064 | |
---|
1065 | def update_data(self, prev_data, new_data): |
---|
1066 | """ |
---|
1067 | Update the data. |
---|
1068 | """ |
---|
1069 | prev_id, data_state = self._data_manager.update_data( |
---|
1070 | prev_data=prev_data, new_data=new_data) |
---|
1071 | |
---|
1072 | self._data_panel.remove_by_id(prev_id) |
---|
1073 | self._data_panel.load_data_list(data_state) |
---|
1074 | |
---|
1075 | def update_theory(self, data_id, theory, state=None): |
---|
1076 | """ |
---|
1077 | Update the theory |
---|
1078 | """ |
---|
1079 | data_state = self._data_manager.update_theory(data_id=data_id, |
---|
1080 | theory=theory, |
---|
1081 | state=state) |
---|
1082 | wx.CallAfter(self._data_panel.load_data_list, data_state) |
---|
1083 | |
---|
1084 | def onfreeze(self, theory_id): |
---|
1085 | """ |
---|
1086 | """ |
---|
1087 | data_state_list = self._data_manager.freeze(theory_id) |
---|
1088 | self._data_panel.load_data_list(list=data_state_list) |
---|
1089 | for data_state in data_state_list.values(): |
---|
1090 | new_plot = data_state.get_data() |
---|
1091 | |
---|
1092 | wx.PostEvent(self, NewPlotEvent(plot=new_plot, |
---|
1093 | title=new_plot.title)) |
---|
1094 | |
---|
1095 | def freeze(self, data_id, theory_id): |
---|
1096 | """ |
---|
1097 | """ |
---|
1098 | data_state_list = self._data_manager.freeze_theory(data_id=data_id, |
---|
1099 | theory_id=theory_id) |
---|
1100 | self._data_panel.load_data_list(list=data_state_list) |
---|
1101 | for data_state in data_state_list.values(): |
---|
1102 | new_plot = data_state.get_data() |
---|
1103 | wx.PostEvent(self, NewPlotEvent(plot=new_plot, |
---|
1104 | title=new_plot.title)) |
---|
1105 | |
---|
1106 | def delete_data(self, data): |
---|
1107 | """ |
---|
1108 | Delete the data. |
---|
1109 | """ |
---|
1110 | self._current_perspective.delete_data(data) |
---|
1111 | |
---|
1112 | |
---|
1113 | def get_context_menu(self, plotpanel=None): |
---|
1114 | """ |
---|
1115 | Get the context menu items made available |
---|
1116 | by the different plug-ins. |
---|
1117 | This function is used by the plotting module |
---|
1118 | """ |
---|
1119 | if plotpanel is None: |
---|
1120 | return |
---|
1121 | menu_list = [] |
---|
1122 | for item in self.plugins: |
---|
1123 | menu_list.extend(item.get_context_menu(plotpanel=plotpanel)) |
---|
1124 | return menu_list |
---|
1125 | |
---|
1126 | def get_current_context_menu(self, plotpanel=None): |
---|
1127 | """ |
---|
1128 | Get the context menu items made available |
---|
1129 | by the current plug-in. |
---|
1130 | This function is used by the plotting module |
---|
1131 | """ |
---|
1132 | if plotpanel is None: |
---|
1133 | return |
---|
1134 | menu_list = [] |
---|
1135 | item = self._current_perspective |
---|
1136 | if item != None: |
---|
1137 | menu_list.extend(item.get_context_menu(plotpanel=plotpanel)) |
---|
1138 | return menu_list |
---|
1139 | |
---|
1140 | def on_panel_close(self, event): |
---|
1141 | """ |
---|
1142 | Gets called when the close event for a panel runs. |
---|
1143 | This will check which panel has been closed and |
---|
1144 | delete it. |
---|
1145 | """ |
---|
1146 | frame = event.GetEventObject() |
---|
1147 | for ID in self.plot_panels.keys(): |
---|
1148 | if self.plot_panels[ID].window_name == frame.name: |
---|
1149 | self.disable_app_menu(self.plot_panels[ID]) |
---|
1150 | self.delete_panel(ID) |
---|
1151 | break |
---|
1152 | self.cpanel_on_focus.SetFocus() |
---|
1153 | |
---|
1154 | |
---|
1155 | def popup_panel(self, p): |
---|
1156 | """ |
---|
1157 | Add a panel object to the AUI manager |
---|
1158 | |
---|
1159 | :param p: panel object to add to the AUI manager |
---|
1160 | |
---|
1161 | :return: ID of the event associated with the new panel [int] |
---|
1162 | |
---|
1163 | """ |
---|
1164 | ID = wx.NewId() |
---|
1165 | self.panels[str(ID)] = p |
---|
1166 | ## Check and set the size |
---|
1167 | if PLOPANEL_WIDTH < 0: |
---|
1168 | p_panel_width = int(self._window_width * 0.45) |
---|
1169 | else: |
---|
1170 | p_panel_width = PLOPANEL_WIDTH |
---|
1171 | p_panel_height = int(p_panel_width * 0.76) |
---|
1172 | p.frame.SetSize((p_panel_width, p_panel_height)) |
---|
1173 | self.graph_num += 1 |
---|
1174 | if p.window_caption.split()[0] in NOT_SO_GRAPH_LIST: |
---|
1175 | windowcaption = p.window_caption |
---|
1176 | else: |
---|
1177 | windowcaption = 'Graph' |
---|
1178 | windowname = p.window_name |
---|
1179 | |
---|
1180 | # Append nummber |
---|
1181 | captions = self._get_plotpanel_captions() |
---|
1182 | while (1): |
---|
1183 | caption = windowcaption + '%s'% str(self.graph_num) |
---|
1184 | if caption not in captions: |
---|
1185 | break |
---|
1186 | self.graph_num += 1 |
---|
1187 | # protection from forever-loop: max num = 1000 |
---|
1188 | if self.graph_num > 1000: |
---|
1189 | break |
---|
1190 | if p.window_caption.split()[0] not in NOT_SO_GRAPH_LIST: |
---|
1191 | p.window_caption = caption |
---|
1192 | p.window_name = windowname + str(self.graph_num) |
---|
1193 | |
---|
1194 | p.frame.SetTitle(p.window_caption) |
---|
1195 | p.frame.name = p.window_name |
---|
1196 | if not IS_WIN: |
---|
1197 | p.frame.Center() |
---|
1198 | x_pos, _ = p.frame.GetPositionTuple() |
---|
1199 | p.frame.SetPosition((x_pos, 112)) |
---|
1200 | p.frame.Show(True) |
---|
1201 | |
---|
1202 | # Register for showing/hiding the panel |
---|
1203 | wx.EVT_MENU(self, ID, self.on_view) |
---|
1204 | if p not in self.plot_panels.values() and p.group_id != None: |
---|
1205 | self.plot_panels[ID] = p |
---|
1206 | if len(self.plot_panels) == 1: |
---|
1207 | self.panel_on_focus = p |
---|
1208 | self.set_panel_on_focus(None) |
---|
1209 | if self._data_panel is not None and \ |
---|
1210 | self._plotting_plugin is not None: |
---|
1211 | ind = self._data_panel.cb_plotpanel.FindString('None') |
---|
1212 | if ind != wx.NOT_FOUND: |
---|
1213 | self._data_panel.cb_plotpanel.Delete(ind) |
---|
1214 | if caption not in self._data_panel.cb_plotpanel.GetItems(): |
---|
1215 | self._data_panel.cb_plotpanel.Append(str(caption), p) |
---|
1216 | return ID |
---|
1217 | |
---|
1218 | def _get_plotpanel_captions(self): |
---|
1219 | """ |
---|
1220 | Get all the plotpanel cations |
---|
1221 | |
---|
1222 | : return: list of captions |
---|
1223 | """ |
---|
1224 | captions = [] |
---|
1225 | for Id in self.plot_panels.keys(): |
---|
1226 | captions.append(self.plot_panels[Id].window_caption) |
---|
1227 | |
---|
1228 | return captions |
---|
1229 | |
---|
1230 | def _setup_tool_bar(self): |
---|
1231 | """ |
---|
1232 | add toolbar to the frame |
---|
1233 | """ |
---|
1234 | self._toolbar = GUIToolBar(self) |
---|
1235 | # The legacy code doesn't work well for wx 3.0 |
---|
1236 | # but the old code produces better results with wx 2.8 |
---|
1237 | if not IS_WIN and wx.VERSION_STRING >= '3.0.0.0': |
---|
1238 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
1239 | sizer.Add(self._toolbar, 0, wx.EXPAND) |
---|
1240 | self.SetSizer(sizer) |
---|
1241 | else: |
---|
1242 | self.SetToolBar(self._toolbar) |
---|
1243 | self._update_toolbar_helper() |
---|
1244 | self._on_toggle_toolbar(event=None) |
---|
1245 | |
---|
1246 | def _update_toolbar_helper(self): |
---|
1247 | """ |
---|
1248 | Helping to update the toolbar |
---|
1249 | """ |
---|
1250 | application_name = 'No Selected Analysis' |
---|
1251 | panel_name = 'No Panel on Focus' |
---|
1252 | c_panel = self.cpanel_on_focus |
---|
1253 | if self._toolbar is None: |
---|
1254 | return |
---|
1255 | if c_panel is not None: |
---|
1256 | self.reset_bookmark_menu(self.cpanel_on_focus) |
---|
1257 | if self._current_perspective is not None: |
---|
1258 | application_name = self._current_perspective.sub_menu |
---|
1259 | c_panel_state = c_panel |
---|
1260 | if c_panel is not None: |
---|
1261 | panel_name = c_panel.window_caption |
---|
1262 | if not c_panel.IsShownOnScreen(): |
---|
1263 | c_panel_state = None |
---|
1264 | self._toolbar.update_toolbar(c_panel_state) |
---|
1265 | self._toolbar.update_button(application_name=application_name, |
---|
1266 | panel_name=panel_name) |
---|
1267 | self._toolbar.Realize() |
---|
1268 | |
---|
1269 | def _add_menu_tool(self): |
---|
1270 | """ |
---|
1271 | Tools menu |
---|
1272 | Go through plug-ins and find tools to populate the tools menu |
---|
1273 | """ |
---|
1274 | style = self.__gui_style & GUIFRAME.CALCULATOR_ON |
---|
1275 | if style == GUIFRAME.CALCULATOR_ON: |
---|
1276 | self._tool_menu = None |
---|
1277 | for item in self.plugins: |
---|
1278 | if hasattr(item, "get_tools"): |
---|
1279 | for tool in item.get_tools(): |
---|
1280 | # Only create a menu if we have at least one tool |
---|
1281 | if self._tool_menu is None: |
---|
1282 | self._tool_menu = wx.Menu() |
---|
1283 | if tool[0].lower().count('python') > 0: |
---|
1284 | self._tool_menu.AppendSeparator() |
---|
1285 | id = wx.NewId() |
---|
1286 | self._tool_menu.Append(id, tool[0], tool[1]) |
---|
1287 | wx.EVT_MENU(self, id, tool[2]) |
---|
1288 | if self._tool_menu is not None: |
---|
1289 | self._menubar.Append(self._tool_menu, '&Tool') |
---|
1290 | |
---|
1291 | def _add_current_plugin_menu(self): |
---|
1292 | """ |
---|
1293 | add current plugin menu |
---|
1294 | Look for plug-in menus |
---|
1295 | Add available plug-in sub-menus. |
---|
1296 | """ |
---|
1297 | if self._menubar is None or self._current_perspective is None \ |
---|
1298 | or self._menubar.GetMenuCount()==0: |
---|
1299 | return |
---|
1300 | #replace or add a new menu for the current plugin |
---|
1301 | |
---|
1302 | pos = self._menubar.FindMenu(str(self._applications_menu_name)) |
---|
1303 | if pos != -1: |
---|
1304 | menu_list = self._current_perspective.populate_menu(self) |
---|
1305 | if menu_list: |
---|
1306 | for (menu, name) in menu_list: |
---|
1307 | hidden_menu = self._menubar.Replace(pos, menu, name) |
---|
1308 | self._applications_menu_name = name |
---|
1309 | #self._applications_menu_pos = pos |
---|
1310 | else: |
---|
1311 | hidden_menu = self._menubar.Remove(pos) |
---|
1312 | self._applications_menu_name = None |
---|
1313 | #get the position of the menu when it first added |
---|
1314 | self._applications_menu_pos = pos |
---|
1315 | |
---|
1316 | else: |
---|
1317 | menu_list = self._current_perspective.populate_menu(self) |
---|
1318 | if menu_list: |
---|
1319 | for (menu, name) in menu_list: |
---|
1320 | if self._applications_menu_pos == -1: |
---|
1321 | self._menubar.Append(menu, name) |
---|
1322 | else: |
---|
1323 | self._menubar.Insert(self._applications_menu_pos, |
---|
1324 | menu, name) |
---|
1325 | self._applications_menu_name = name |
---|
1326 | |
---|
1327 | def _add_help_menu(self): |
---|
1328 | """ |
---|
1329 | add help menu to menu bar. Includes welcome page, about page, |
---|
1330 | tutorial PDF and documentation pages. |
---|
1331 | """ |
---|
1332 | # Help menu |
---|
1333 | self._help_menu = wx.Menu() |
---|
1334 | style = self.__gui_style & GUIFRAME.WELCOME_PANEL_ON |
---|
1335 | |
---|
1336 | if style == GUIFRAME.WELCOME_PANEL_ON or custom_config != None: |
---|
1337 | # add the welcome panel menu item |
---|
1338 | if config.WELCOME_PANEL_ON and self.defaultPanel is not None: |
---|
1339 | id = wx.NewId() |
---|
1340 | self._help_menu.Append(id, '&Welcome', '') |
---|
1341 | wx.EVT_MENU(self, id, self.show_welcome_panel) |
---|
1342 | |
---|
1343 | self._help_menu.AppendSeparator() |
---|
1344 | id = wx.NewId() |
---|
1345 | self._help_menu.Append(id, '&Documentation', '') |
---|
1346 | wx.EVT_MENU(self, id, self._onSphinxDocs) |
---|
1347 | |
---|
1348 | if config._do_tutorial and (IS_WIN or sys.platform =='darwin'): |
---|
1349 | self._help_menu.AppendSeparator() |
---|
1350 | id = wx.NewId() |
---|
1351 | self._help_menu.Append(id, '&Tutorial', 'Software tutorial') |
---|
1352 | wx.EVT_MENU(self, id, self._onTutorial) |
---|
1353 | |
---|
1354 | if config._do_aboutbox: |
---|
1355 | self._help_menu.AppendSeparator() |
---|
1356 | self._help_menu.Append(wx.ID_ABOUT, '&About', 'Software information') |
---|
1357 | wx.EVT_MENU(self, wx.ID_ABOUT, self._onAbout) |
---|
1358 | |
---|
1359 | # Checking for updates |
---|
1360 | id = wx.NewId() |
---|
1361 | self._help_menu.Append(id,'&Check for update', |
---|
1362 | 'Check for the latest version of %s' % config.__appname__) |
---|
1363 | wx.EVT_MENU(self, id, self._check_update) |
---|
1364 | self._menubar.Append(self._help_menu, '&Help') |
---|
1365 | |
---|
1366 | def _add_menu_view(self): |
---|
1367 | """ |
---|
1368 | add menu items under view menu |
---|
1369 | """ |
---|
1370 | if not VIEW_MENU: |
---|
1371 | return |
---|
1372 | self._view_menu = wx.Menu() |
---|
1373 | |
---|
1374 | id = wx.NewId() |
---|
1375 | hint = "Display the Grid Window for batch results etc." |
---|
1376 | self._view_menu.Append(id, '&Show Grid Window', hint) |
---|
1377 | wx.EVT_MENU(self, id, self.show_batch_frame) |
---|
1378 | |
---|
1379 | self._view_menu.AppendSeparator() |
---|
1380 | style = self.__gui_style & GUIFRAME.MANAGER_ON |
---|
1381 | id = wx.NewId() |
---|
1382 | self._data_panel_menu = self._view_menu.Append(id, |
---|
1383 | '&Show Data Explorer', '') |
---|
1384 | wx.EVT_MENU(self, id, self.show_data_panel) |
---|
1385 | if style == GUIFRAME.MANAGER_ON: |
---|
1386 | self._data_panel_menu.SetText('Hide Data Explorer') |
---|
1387 | else: |
---|
1388 | self._data_panel_menu.SetText('Show Data Explorer') |
---|
1389 | |
---|
1390 | self._view_menu.AppendSeparator() |
---|
1391 | id = wx.NewId() |
---|
1392 | style1 = self.__gui_style & GUIFRAME.TOOLBAR_ON |
---|
1393 | if style1 == GUIFRAME.TOOLBAR_ON: |
---|
1394 | self._toolbar_menu = self._view_menu.Append(id, '&Hide Toolbar', '') |
---|
1395 | else: |
---|
1396 | self._toolbar_menu = self._view_menu.Append(id, '&Show Toolbar', '') |
---|
1397 | wx.EVT_MENU(self, id, self._on_toggle_toolbar) |
---|
1398 | |
---|
1399 | if custom_config != None: |
---|
1400 | self._view_menu.AppendSeparator() |
---|
1401 | id = wx.NewId() |
---|
1402 | hint_ss = "Select the current/default configuration " |
---|
1403 | hint_ss += "as a startup setting" |
---|
1404 | preference_menu = self._view_menu.Append(id, 'Startup Setting', |
---|
1405 | hint_ss) |
---|
1406 | wx.EVT_MENU(self, id, self._on_preference_menu) |
---|
1407 | |
---|
1408 | id = wx.NewId() |
---|
1409 | self._view_menu.AppendSeparator() |
---|
1410 | self._view_menu.Append(id, 'Category Manager', 'Edit model categories') |
---|
1411 | wx.EVT_MENU(self, id, self._on_category_manager) |
---|
1412 | |
---|
1413 | self._menubar.Append(self._view_menu, '&View') |
---|
1414 | |
---|
1415 | def show_batch_frame(self, event=None): |
---|
1416 | """ |
---|
1417 | show the grid of result |
---|
1418 | """ |
---|
1419 | # Show(False) before Show(True) in order to bring it to the front |
---|
1420 | self.batch_frame.Show(False) |
---|
1421 | self.batch_frame.Show(True) |
---|
1422 | |
---|
1423 | def on_category_panel(self, event): |
---|
1424 | """ |
---|
1425 | On cat panel |
---|
1426 | """ |
---|
1427 | self._on_category_manager(event) |
---|
1428 | |
---|
1429 | def _on_category_manager(self, event): |
---|
1430 | """ |
---|
1431 | Category manager frame |
---|
1432 | """ |
---|
1433 | frame = CategoryManager(self, -1, 'Model Category Manager') |
---|
1434 | icon = self.GetIcon() |
---|
1435 | frame.SetIcon(icon) |
---|
1436 | |
---|
1437 | def _on_preference_menu(self, event): |
---|
1438 | """ |
---|
1439 | Build a panel to allow to edit Mask |
---|
1440 | """ |
---|
1441 | from sas.guiframe.startup_configuration \ |
---|
1442 | import StartupConfiguration as ConfDialog |
---|
1443 | |
---|
1444 | dialog = ConfDialog(parent=self, gui=self.__gui_style) |
---|
1445 | result = dialog.ShowModal() |
---|
1446 | if result == wx.ID_OK: |
---|
1447 | dialog.write_custom_config() |
---|
1448 | # post event for info |
---|
1449 | wx.PostEvent(self, StatusEvent(status="Wrote custom configuration", info='info')) |
---|
1450 | dialog.Destroy() |
---|
1451 | |
---|
1452 | def _add_menu_application(self): |
---|
1453 | """ |
---|
1454 | # Attach a menu item for each defined perspective or application. |
---|
1455 | # Only add the perspective menu if there are more than one perspectives |
---|
1456 | add menu application |
---|
1457 | """ |
---|
1458 | if self._num_perspectives > 1: |
---|
1459 | plug_data_count = False |
---|
1460 | plug_no_data_count = False |
---|
1461 | self._applications_menu = wx.Menu() |
---|
1462 | pos = 0 |
---|
1463 | separator = self._applications_menu.AppendSeparator() |
---|
1464 | for plug in self.plugins: |
---|
1465 | if len(plug.get_perspective()) > 0: |
---|
1466 | id = wx.NewId() |
---|
1467 | if plug.use_data(): |
---|
1468 | |
---|
1469 | self._applications_menu.InsertCheckItem(pos, id, plug.sub_menu, |
---|
1470 | "Switch to analysis: %s" % plug.sub_menu) |
---|
1471 | plug_data_count = True |
---|
1472 | pos += 1 |
---|
1473 | else: |
---|
1474 | plug_no_data_count = True |
---|
1475 | self._applications_menu.AppendCheckItem(id, plug.sub_menu, |
---|
1476 | "Switch to analysis: %s" % plug.sub_menu) |
---|
1477 | wx.EVT_MENU(self, id, plug.on_perspective) |
---|
1478 | |
---|
1479 | if (not plug_data_count or not plug_no_data_count): |
---|
1480 | self._applications_menu.RemoveItem(separator) |
---|
1481 | self._menubar.Append(self._applications_menu, '&Analysis') |
---|
1482 | self._check_applications_menu() |
---|
1483 | |
---|
1484 | def _populate_file_menu(self): |
---|
1485 | """ |
---|
1486 | Insert menu item under file menu |
---|
1487 | """ |
---|
1488 | for plugin in self.plugins: |
---|
1489 | if len(plugin.populate_file_menu()) > 0: |
---|
1490 | for item in plugin.populate_file_menu(): |
---|
1491 | m_name, m_hint, m_handler = item |
---|
1492 | id = wx.NewId() |
---|
1493 | self._file_menu.Append(id, m_name, m_hint) |
---|
1494 | wx.EVT_MENU(self, id, m_handler) |
---|
1495 | self._file_menu.AppendSeparator() |
---|
1496 | |
---|
1497 | style1 = self.__gui_style & GUIFRAME.MULTIPLE_APPLICATIONS |
---|
1498 | if OPEN_SAVE_MENU: |
---|
1499 | id = wx.NewId() |
---|
1500 | hint_load_file = "read all analysis states saved previously" |
---|
1501 | self._save_appl_menu = self._file_menu.Append(id, |
---|
1502 | '&Open Project', hint_load_file) |
---|
1503 | wx.EVT_MENU(self, id, self._on_open_state_project) |
---|
1504 | |
---|
1505 | if style1 == GUIFRAME.MULTIPLE_APPLICATIONS: |
---|
1506 | # some menu of plugin to be seen under file menu |
---|
1507 | hint_load_file = "Read a status files and load" |
---|
1508 | hint_load_file += " them into the analysis" |
---|
1509 | id = wx.NewId() |
---|
1510 | self._save_appl_menu = self._file_menu.Append(id, |
---|
1511 | '&Open Analysis', hint_load_file) |
---|
1512 | wx.EVT_MENU(self, id, self._on_open_state_application) |
---|
1513 | if OPEN_SAVE_MENU: |
---|
1514 | self._file_menu.AppendSeparator() |
---|
1515 | id = wx.NewId() |
---|
1516 | self._file_menu.Append(id, '&Save Project', |
---|
1517 | 'Save the state of the whole analysis') |
---|
1518 | wx.EVT_MENU(self, id, self._on_save_project) |
---|
1519 | if style1 == GUIFRAME.MULTIPLE_APPLICATIONS: |
---|
1520 | id = wx.NewId() |
---|
1521 | self._save_appl_menu = self._file_menu.Append(id, |
---|
1522 | '&Save Analysis', |
---|
1523 | 'Save state of the current active analysis panel') |
---|
1524 | wx.EVT_MENU(self, id, self._on_save_application) |
---|
1525 | if not sys.platform =='darwin': |
---|
1526 | self._file_menu.AppendSeparator() |
---|
1527 | id = wx.NewId() |
---|
1528 | self._file_menu.Append(id, '&Quit', 'Exit') |
---|
1529 | wx.EVT_MENU(self, id, self.Close) |
---|
1530 | |
---|
1531 | def _add_menu_file(self): |
---|
1532 | """ |
---|
1533 | add menu file |
---|
1534 | """ |
---|
1535 | # File menu |
---|
1536 | self._file_menu = wx.Menu() |
---|
1537 | # Add sub menus |
---|
1538 | self._menubar.Append(self._file_menu, '&File') |
---|
1539 | |
---|
1540 | def _add_menu_edit(self): |
---|
1541 | """ |
---|
1542 | add menu edit |
---|
1543 | """ |
---|
1544 | if not EDIT_MENU: |
---|
1545 | return |
---|
1546 | # Edit Menu |
---|
1547 | self._edit_menu = wx.Menu() |
---|
1548 | self._edit_menu.Append(GUIFRAME_ID.UNDO_ID, '&Undo', |
---|
1549 | 'Undo the previous action') |
---|
1550 | wx.EVT_MENU(self, GUIFRAME_ID.UNDO_ID, self.on_undo_panel) |
---|
1551 | self._edit_menu.Append(GUIFRAME_ID.REDO_ID, '&Redo', |
---|
1552 | 'Redo the previous action') |
---|
1553 | wx.EVT_MENU(self, GUIFRAME_ID.REDO_ID, self.on_redo_panel) |
---|
1554 | self._edit_menu.AppendSeparator() |
---|
1555 | self._edit_menu.Append(GUIFRAME_ID.COPY_ID, '&Copy Params', |
---|
1556 | 'Copy parameter values') |
---|
1557 | wx.EVT_MENU(self, GUIFRAME_ID.COPY_ID, self.on_copy_panel) |
---|
1558 | self._edit_menu.Append(GUIFRAME_ID.PASTE_ID, '&Paste Params', |
---|
1559 | 'Paste parameter values') |
---|
1560 | wx.EVT_MENU(self, GUIFRAME_ID.PASTE_ID, self.on_paste_panel) |
---|
1561 | |
---|
1562 | self._edit_menu.AppendSeparator() |
---|
1563 | |
---|
1564 | self._edit_menu_copyas = wx.Menu() |
---|
1565 | #Sub menu for Copy As... |
---|
1566 | self._edit_menu_copyas.Append(GUIFRAME_ID.COPYEX_ID, 'Copy current tab to Excel', |
---|
1567 | 'Copy parameter values in tabular format') |
---|
1568 | wx.EVT_MENU(self, GUIFRAME_ID.COPYEX_ID, self.on_copy_panel) |
---|
1569 | |
---|
1570 | self._edit_menu_copyas.Append(GUIFRAME_ID.COPYLAT_ID, 'Copy current tab to LaTeX', |
---|
1571 | 'Copy parameter values in tabular format') |
---|
1572 | wx.EVT_MENU(self, GUIFRAME_ID.COPYLAT_ID, self.on_copy_panel) |
---|
1573 | |
---|
1574 | |
---|
1575 | self._edit_menu.AppendMenu(GUIFRAME_ID.COPYAS_ID, 'Copy Params as...', self._edit_menu_copyas, |
---|
1576 | 'Copy parameter values in various formats') |
---|
1577 | |
---|
1578 | |
---|
1579 | self._edit_menu.AppendSeparator() |
---|
1580 | |
---|
1581 | self._edit_menu.Append(GUIFRAME_ID.PREVIEW_ID, '&Report Results', |
---|
1582 | 'Preview current panel') |
---|
1583 | wx.EVT_MENU(self, GUIFRAME_ID.PREVIEW_ID, self.on_preview_panel) |
---|
1584 | |
---|
1585 | self._edit_menu.Append(GUIFRAME_ID.RESET_ID, '&Reset Page', |
---|
1586 | 'Reset current panel') |
---|
1587 | wx.EVT_MENU(self, GUIFRAME_ID.RESET_ID, self.on_reset_panel) |
---|
1588 | |
---|
1589 | self._menubar.Append(self._edit_menu, '&Edit') |
---|
1590 | self.enable_edit_menu() |
---|
1591 | |
---|
1592 | def get_style(self): |
---|
1593 | """ |
---|
1594 | Return the gui style |
---|
1595 | """ |
---|
1596 | return self.__gui_style |
---|
1597 | |
---|
1598 | def _add_menu_data(self): |
---|
1599 | """ |
---|
1600 | Add menu item item data to menu bar |
---|
1601 | """ |
---|
1602 | if self._data_plugin is not None: |
---|
1603 | menu_list = self._data_plugin.populate_menu(self) |
---|
1604 | if menu_list: |
---|
1605 | for (menu, name) in menu_list: |
---|
1606 | self._menubar.Append(menu, name) |
---|
1607 | |
---|
1608 | |
---|
1609 | def _on_toggle_toolbar(self, event=None): |
---|
1610 | """ |
---|
1611 | hide or show toolbar |
---|
1612 | """ |
---|
1613 | if self._toolbar is None: |
---|
1614 | return |
---|
1615 | if self._toolbar.IsShown(): |
---|
1616 | if self._toolbar_menu is not None: |
---|
1617 | self._toolbar_menu.SetItemLabel('Show Toolbar') |
---|
1618 | self._toolbar.Hide() |
---|
1619 | else: |
---|
1620 | if self._toolbar_menu is not None: |
---|
1621 | self._toolbar_menu.SetItemLabel('Hide Toolbar') |
---|
1622 | self._toolbar.Show() |
---|
1623 | self._toolbar.Realize() |
---|
1624 | |
---|
1625 | def _on_status_event(self, evt): |
---|
1626 | """ |
---|
1627 | Display status message |
---|
1628 | """ |
---|
1629 | # This CallAfter fixes many crashes on MAC. |
---|
1630 | wx.CallAfter(self.sb.set_status, evt) |
---|
1631 | |
---|
1632 | def on_view(self, evt): |
---|
1633 | """ |
---|
1634 | A panel was selected to be shown. If it's not already |
---|
1635 | shown, display it. |
---|
1636 | |
---|
1637 | :param evt: menu event |
---|
1638 | |
---|
1639 | """ |
---|
1640 | panel_id = str(evt.GetId()) |
---|
1641 | self.on_set_plot_focus(self.panels[panel_id]) |
---|
1642 | wx.CallLater(5*TIME_FACTOR, self.set_schedule(True)) |
---|
1643 | self.set_plot_unfocus() |
---|
1644 | |
---|
1645 | def show_welcome_panel(self, event): |
---|
1646 | """ |
---|
1647 | Display the welcome panel |
---|
1648 | """ |
---|
1649 | if self.defaultPanel is None: |
---|
1650 | return |
---|
1651 | frame = self.panels['default'].get_frame() |
---|
1652 | if frame == None: |
---|
1653 | return |
---|
1654 | # Show default panel |
---|
1655 | if not frame.IsShown(): |
---|
1656 | frame.Show(True) |
---|
1657 | |
---|
1658 | def on_close_welcome_panel(self): |
---|
1659 | """ |
---|
1660 | Close the welcome panel |
---|
1661 | """ |
---|
1662 | if self.defaultPanel is None: |
---|
1663 | return |
---|
1664 | default_panel = self.panels["default"].frame |
---|
1665 | if default_panel.IsShown(): |
---|
1666 | default_panel.Show(False) |
---|
1667 | |
---|
1668 | def delete_panel(self, uid): |
---|
1669 | """ |
---|
1670 | delete panel given uid |
---|
1671 | """ |
---|
1672 | ID = str(uid) |
---|
1673 | config.printEVT("delete_panel: %s" % ID) |
---|
1674 | try: |
---|
1675 | caption = self.panels[ID].window_caption |
---|
1676 | except: |
---|
1677 | logging.error("delete_panel: No such plot id as %s" % ID) |
---|
1678 | return |
---|
1679 | if ID in self.panels.keys(): |
---|
1680 | self.panel_on_focus = None |
---|
1681 | panel = self.panels[ID] |
---|
1682 | |
---|
1683 | if hasattr(panel, "connect"): |
---|
1684 | panel.connect.disconnect() |
---|
1685 | self._plotting_plugin.delete_panel(panel.group_id) |
---|
1686 | |
---|
1687 | if panel in self.schedule_full_draw_list: |
---|
1688 | self.schedule_full_draw_list.remove(panel) |
---|
1689 | |
---|
1690 | #delete uid number not str(uid) |
---|
1691 | if ID in self.plot_panels.keys(): |
---|
1692 | del self.plot_panels[ID] |
---|
1693 | if ID in self.panels.keys(): |
---|
1694 | del self.panels[ID] |
---|
1695 | return |
---|
1696 | |
---|
1697 | def create_gui_data(self, data, path=None): |
---|
1698 | """ |
---|
1699 | """ |
---|
1700 | return self._data_manager.create_gui_data(data, path) |
---|
1701 | |
---|
1702 | def get_data(self, path): |
---|
1703 | """ |
---|
1704 | """ |
---|
1705 | message = "" |
---|
1706 | log_msg = '' |
---|
1707 | output = [] |
---|
1708 | error_message = "" |
---|
1709 | basename = os.path.basename(path) |
---|
1710 | root, extension = os.path.splitext(basename) |
---|
1711 | if extension.lower() not in EXTENSIONS: |
---|
1712 | log_msg = "File Loader cannot " |
---|
1713 | log_msg += "load: %s\n" % str(basename) |
---|
1714 | log_msg += "Try Data opening...." |
---|
1715 | logging.error(log_msg) |
---|
1716 | return |
---|
1717 | |
---|
1718 | #reading a state file |
---|
1719 | for plug in self.plugins: |
---|
1720 | reader, ext = plug.get_extensions() |
---|
1721 | if reader is not None: |
---|
1722 | #read the state of the single plugin |
---|
1723 | if extension == ext: |
---|
1724 | reader.read(path) |
---|
1725 | return |
---|
1726 | elif extension == APPLICATION_STATE_EXTENSION: |
---|
1727 | try: |
---|
1728 | reader.read(path) |
---|
1729 | except: |
---|
1730 | msg = "DataLoader Error: Encounted Non-ASCII character" |
---|
1731 | msg += "\n(%s)"% sys.exc_value |
---|
1732 | wx.PostEvent(self, StatusEvent(status=msg, |
---|
1733 | info="error", type="stop")) |
---|
1734 | return |
---|
1735 | |
---|
1736 | style = self.__gui_style & GUIFRAME.MANAGER_ON |
---|
1737 | if style == GUIFRAME.MANAGER_ON: |
---|
1738 | if self._data_panel is not None: |
---|
1739 | self._data_panel.frame.Show(True) |
---|
1740 | |
---|
1741 | def load_from_cmd(self, path): |
---|
1742 | """ |
---|
1743 | load data from cmd or application |
---|
1744 | """ |
---|
1745 | if path is None: |
---|
1746 | return |
---|
1747 | else: |
---|
1748 | path = os.path.abspath(path) |
---|
1749 | if not os.path.isfile(path) and not os.path.isdir(path): |
---|
1750 | return |
---|
1751 | |
---|
1752 | if os.path.isdir(path): |
---|
1753 | self.load_folder(path) |
---|
1754 | return |
---|
1755 | |
---|
1756 | basename = os.path.basename(path) |
---|
1757 | root, extension = os.path.splitext(basename) |
---|
1758 | if extension.lower() not in EXTENSIONS: |
---|
1759 | self.load_data(path) |
---|
1760 | else: |
---|
1761 | self.load_state(path) |
---|
1762 | |
---|
1763 | self._default_save_location = os.path.dirname(path) |
---|
1764 | |
---|
1765 | def load_state(self, path, is_project=False): |
---|
1766 | """ |
---|
1767 | load data from command line or application |
---|
1768 | """ |
---|
1769 | if path and (path is not None) and os.path.isfile(path): |
---|
1770 | basename = os.path.basename(path) |
---|
1771 | if APPLICATION_STATE_EXTENSION is not None \ |
---|
1772 | and basename.endswith(APPLICATION_STATE_EXTENSION): |
---|
1773 | if is_project: |
---|
1774 | for ID in self.plot_panels.keys(): |
---|
1775 | panel = self.plot_panels[ID] |
---|
1776 | panel.on_close(None) |
---|
1777 | self.get_data(path) |
---|
1778 | wx.PostEvent(self, StatusEvent(status="Completed loading.")) |
---|
1779 | else: |
---|
1780 | wx.PostEvent(self, StatusEvent(status=" ")) |
---|
1781 | |
---|
1782 | def load_data(self, path): |
---|
1783 | """ |
---|
1784 | load data from command line |
---|
1785 | """ |
---|
1786 | if not os.path.isfile(path): |
---|
1787 | return |
---|
1788 | basename = os.path.basename(path) |
---|
1789 | root, extension = os.path.splitext(basename) |
---|
1790 | if extension.lower() in EXTENSIONS: |
---|
1791 | log_msg = "Data Loader cannot " |
---|
1792 | log_msg += "load: %s\n" % str(path) |
---|
1793 | log_msg += "Try File opening ...." |
---|
1794 | logging.error(log_msg) |
---|
1795 | return |
---|
1796 | log_msg = '' |
---|
1797 | output = {} |
---|
1798 | error_message = "" |
---|
1799 | try: |
---|
1800 | logging.info("Loading Data...:\n" + str(path) + "\n") |
---|
1801 | temp = self.loader.load(path) |
---|
1802 | if temp.__class__.__name__ == "list": |
---|
1803 | for item in temp: |
---|
1804 | data = self.create_gui_data(item, path) |
---|
1805 | output[data.id] = data |
---|
1806 | else: |
---|
1807 | data = self.create_gui_data(temp, path) |
---|
1808 | output[data.id] = data |
---|
1809 | |
---|
1810 | self.add_data(data_list=output) |
---|
1811 | except: |
---|
1812 | error_message = "Error while loading" |
---|
1813 | error_message += " Data from cmd:\n %s\n" % str(path) |
---|
1814 | error_message += str(sys.exc_value) + "\n" |
---|
1815 | logging.error(error_message) |
---|
1816 | |
---|
1817 | def load_folder(self, path): |
---|
1818 | """ |
---|
1819 | Load entire folder |
---|
1820 | """ |
---|
1821 | if not os.path.isdir(path): |
---|
1822 | return |
---|
1823 | if self._data_plugin is None: |
---|
1824 | return |
---|
1825 | try: |
---|
1826 | if path is not None: |
---|
1827 | self._default_save_location = os.path.dirname(path) |
---|
1828 | file_list = self._data_plugin.get_file_path(path) |
---|
1829 | self._data_plugin.get_data(file_list) |
---|
1830 | else: |
---|
1831 | return |
---|
1832 | except: |
---|
1833 | error_message = "Error while loading" |
---|
1834 | error_message += " Data folder from cmd:\n %s\n" % str(path) |
---|
1835 | error_message += str(sys.exc_value) + "\n" |
---|
1836 | logging.error(error_message) |
---|
1837 | |
---|
1838 | def _on_open_state_application(self, event): |
---|
1839 | """ |
---|
1840 | """ |
---|
1841 | path = None |
---|
1842 | if self._default_save_location == None: |
---|
1843 | self._default_save_location = os.getcwd() |
---|
1844 | wx.PostEvent(self, StatusEvent(status="Loading Analysis file...")) |
---|
1845 | plug_wlist = self._on_open_state_app_helper() |
---|
1846 | dlg = wx.FileDialog(self, |
---|
1847 | "Choose a file", |
---|
1848 | self._default_save_location, "", |
---|
1849 | plug_wlist) |
---|
1850 | if dlg.ShowModal() == wx.ID_OK: |
---|
1851 | path = dlg.GetPath() |
---|
1852 | if path is not None: |
---|
1853 | self._default_save_location = os.path.dirname(path) |
---|
1854 | dlg.Destroy() |
---|
1855 | self.load_state(path=path) |
---|
1856 | |
---|
1857 | def _on_open_state_app_helper(self): |
---|
1858 | """ |
---|
1859 | Helps '_on_open_state_application()' to find the extension of |
---|
1860 | the current perspective/application |
---|
1861 | """ |
---|
1862 | # No current perspective or no extension attr |
---|
1863 | if self._current_perspective is None: |
---|
1864 | return PLUGINS_WLIST |
---|
1865 | try: |
---|
1866 | # Find the extension of the perspective |
---|
1867 | # and get that as 1st item in list |
---|
1868 | ind = None |
---|
1869 | app_ext = self._current_perspective._extensions |
---|
1870 | plug_wlist = config.PLUGINS_WLIST |
---|
1871 | for ext in set(plug_wlist): |
---|
1872 | if ext.count(app_ext) > 0: |
---|
1873 | ind = ext |
---|
1874 | break |
---|
1875 | # Found the extension |
---|
1876 | if ind != None: |
---|
1877 | plug_wlist.remove(ind) |
---|
1878 | plug_wlist.insert(0, ind) |
---|
1879 | try: |
---|
1880 | plug_wlist = '|'.join(plug_wlist) |
---|
1881 | except: |
---|
1882 | plug_wlist = '' |
---|
1883 | |
---|
1884 | except: |
---|
1885 | plug_wlist = PLUGINS_WLIST |
---|
1886 | |
---|
1887 | return plug_wlist |
---|
1888 | |
---|
1889 | def _on_open_state_project(self, event): |
---|
1890 | """ |
---|
1891 | """ |
---|
1892 | path = None |
---|
1893 | if self._default_save_location == None: |
---|
1894 | self._default_save_location = os.getcwd() |
---|
1895 | wx.PostEvent(self, StatusEvent(status="Loading Project file...")) |
---|
1896 | dlg = wx.FileDialog(self, |
---|
1897 | "Choose a file", |
---|
1898 | self._default_save_location, "", |
---|
1899 | APPLICATION_WLIST) |
---|
1900 | if dlg.ShowModal() == wx.ID_OK: |
---|
1901 | path = dlg.GetPath() |
---|
1902 | if path is not None: |
---|
1903 | self._default_save_location = os.path.dirname(path) |
---|
1904 | dlg.Destroy() |
---|
1905 | |
---|
1906 | self.load_state(path=path, is_project=True) |
---|
1907 | |
---|
1908 | def _on_save_application(self, event): |
---|
1909 | """ |
---|
1910 | save the state of the current active application |
---|
1911 | """ |
---|
1912 | if self.cpanel_on_focus is not None: |
---|
1913 | try: |
---|
1914 | wx.PostEvent(self, |
---|
1915 | StatusEvent(status="Saving Analysis file...")) |
---|
1916 | self.cpanel_on_focus.on_save(event) |
---|
1917 | wx.PostEvent(self, |
---|
1918 | StatusEvent(status="Completed saving.")) |
---|
1919 | except: |
---|
1920 | msg = "Error occurred while saving: " |
---|
1921 | msg += "To save, the application panel should have a data set.." |
---|
1922 | wx.PostEvent(self, StatusEvent(status=msg)) |
---|
1923 | |
---|
1924 | def _on_save_project(self, event): |
---|
1925 | """ |
---|
1926 | save the state of the SasView as *.svs |
---|
1927 | """ |
---|
1928 | if self._current_perspective is None: |
---|
1929 | return |
---|
1930 | wx.PostEvent(self, StatusEvent(status="Saving Project file...")) |
---|
1931 | reader, ext = self._current_perspective.get_extensions() |
---|
1932 | path = None |
---|
1933 | extension = '*' + APPLICATION_STATE_EXTENSION |
---|
1934 | dlg = wx.FileDialog(self, "Save Project file", |
---|
1935 | self._default_save_location, "sasview_proj", |
---|
1936 | extension, |
---|
1937 | wx.SAVE) |
---|
1938 | if dlg.ShowModal() == wx.ID_OK: |
---|
1939 | path = dlg.GetPath() |
---|
1940 | self._default_save_location = os.path.dirname(path) |
---|
1941 | else: |
---|
1942 | return None |
---|
1943 | dlg.Destroy() |
---|
1944 | try: |
---|
1945 | if path is None: |
---|
1946 | return |
---|
1947 | # default cansas xml doc |
---|
1948 | doc = None |
---|
1949 | for panel in self.panels.values(): |
---|
1950 | temp = panel.save_project(doc) |
---|
1951 | if temp is not None: |
---|
1952 | doc = temp |
---|
1953 | |
---|
1954 | # Write the XML document |
---|
1955 | extens = APPLICATION_STATE_EXTENSION |
---|
1956 | fName = os.path.splitext(path)[0] + extens |
---|
1957 | if doc != None: |
---|
1958 | fd = open(fName, 'w') |
---|
1959 | fd.write(doc.toprettyxml()) |
---|
1960 | fd.close() |
---|
1961 | wx.PostEvent(self, StatusEvent(status="Completed Saving.")) |
---|
1962 | else: |
---|
1963 | msg = "Error occurred while saving the project: " |
---|
1964 | msg += "To save, at least one application panel " |
---|
1965 | msg += "should have a data set " |
---|
1966 | msg += "and model selected. " |
---|
1967 | msg += "No project was saved to %s" % (str(path)) |
---|
1968 | logging.error(msg) |
---|
1969 | wx.PostEvent(self,StatusEvent(status=msg)) |
---|
1970 | except: |
---|
1971 | msg = "Error occurred while saving: " |
---|
1972 | msg += "To save, at least one application panel " |
---|
1973 | msg += "should have a data set.." |
---|
1974 | wx.PostEvent(self, StatusEvent(status=msg)) |
---|
1975 | |
---|
1976 | def on_save_helper(self, doc, reader, panel, path): |
---|
1977 | """ |
---|
1978 | Save state into a file |
---|
1979 | """ |
---|
1980 | try: |
---|
1981 | if reader is not None: |
---|
1982 | # case of a panel with multi-pages |
---|
1983 | if hasattr(panel, "opened_pages"): |
---|
1984 | for uid, page in panel.opened_pages.iteritems(): |
---|
1985 | data = page.get_data() |
---|
1986 | # state must be cloned |
---|
1987 | state = page.get_state().clone() |
---|
1988 | if data is not None: |
---|
1989 | new_doc = reader.write_toXML(data, state) |
---|
1990 | if doc != None and hasattr(doc, "firstChild"): |
---|
1991 | child = new_doc.firstChild.firstChild |
---|
1992 | doc.firstChild.appendChild(child) |
---|
1993 | else: |
---|
1994 | doc = new_doc |
---|
1995 | # case of only a panel |
---|
1996 | else: |
---|
1997 | data = panel.get_data() |
---|
1998 | state = panel.get_state() |
---|
1999 | if data is not None: |
---|
2000 | new_doc = reader.write_toXML(data, state) |
---|
2001 | if doc != None and hasattr(doc, "firstChild"): |
---|
2002 | child = new_doc.firstChild.firstChild |
---|
2003 | doc.firstChild.appendChild(child) |
---|
2004 | else: |
---|
2005 | doc = new_doc |
---|
2006 | except: |
---|
2007 | raise |
---|
2008 | #pass |
---|
2009 | |
---|
2010 | return doc |
---|
2011 | |
---|
2012 | def quit_guiframe(self): |
---|
2013 | """ |
---|
2014 | Pop up message to make sure the user wants to quit the application |
---|
2015 | """ |
---|
2016 | message = "\nDo you really want to exit this application? \n\n" |
---|
2017 | dial = wx.MessageDialog(self, message, 'Confirm Exit', |
---|
2018 | wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION) |
---|
2019 | if dial.ShowModal() == wx.ID_YES: |
---|
2020 | return True |
---|
2021 | else: |
---|
2022 | return False |
---|
2023 | |
---|
2024 | def WindowClose(self, event=None): |
---|
2025 | """ |
---|
2026 | Quit the application from x icon |
---|
2027 | """ |
---|
2028 | flag = self.quit_guiframe() |
---|
2029 | if flag: |
---|
2030 | _pylab_helpers.Gcf.figs = {} |
---|
2031 | self.Close() |
---|
2032 | |
---|
2033 | def Close(self, event=None): |
---|
2034 | """ |
---|
2035 | Quit the application |
---|
2036 | """ |
---|
2037 | wx.Exit() |
---|
2038 | sys.exit() |
---|
2039 | |
---|
2040 | def _check_update(self, event=None): |
---|
2041 | """ |
---|
2042 | Check with the deployment server whether a new version |
---|
2043 | of the application is available. |
---|
2044 | A thread is started for the connecting with the server. The thread calls |
---|
2045 | a call-back method when the current version number has been obtained. |
---|
2046 | """ |
---|
2047 | try: |
---|
2048 | conn = httplib.HTTPConnection(config.__update_URL__[0], |
---|
2049 | timeout=3.0) |
---|
2050 | conn.request("GET", config.__update_URL__[1]) |
---|
2051 | res = conn.getresponse() |
---|
2052 | content = res.read() |
---|
2053 | conn.close() |
---|
2054 | except: |
---|
2055 | content = "0.0.0" |
---|
2056 | |
---|
2057 | version = content.strip() |
---|
2058 | if len(re.findall('\d+\.\d+\.\d+$', version)) < 0: |
---|
2059 | content = "0.0.0" |
---|
2060 | self._process_version(content, standalone=event==None) |
---|
2061 | |
---|
2062 | def _process_version(self, version, standalone=True): |
---|
2063 | """ |
---|
2064 | Call-back method for the process of checking for updates. |
---|
2065 | This methods is called by a VersionThread object once the current |
---|
2066 | version number has been obtained. If the check is being done in the |
---|
2067 | background, the user will not be notified unless there's an update. |
---|
2068 | |
---|
2069 | :param version: version string |
---|
2070 | :param standalone: True of the update is being checked in |
---|
2071 | the background, False otherwise. |
---|
2072 | |
---|
2073 | """ |
---|
2074 | try: |
---|
2075 | if version == "0.0.0": |
---|
2076 | msg = "Could not connect to the application server." |
---|
2077 | msg += " Please try again later." |
---|
2078 | self.SetStatusText(msg) |
---|
2079 | elif cmp(version, config.__version__) > 0: |
---|
2080 | msg = "Version %s is available! " % str(version) |
---|
2081 | if not standalone: |
---|
2082 | import webbrowser |
---|
2083 | webbrowser.open(config.__download_page__) |
---|
2084 | else: |
---|
2085 | msg += "See the help menu to download it." |
---|
2086 | self.SetStatusText(msg) |
---|
2087 | else: |
---|
2088 | if not standalone: |
---|
2089 | msg = "You have the latest version" |
---|
2090 | msg += " of %s" % str(config.__appname__) |
---|
2091 | self.SetStatusText(msg) |
---|
2092 | except: |
---|
2093 | msg = "guiframe: could not get latest application" |
---|
2094 | msg += " version number\n %s" % sys.exc_value |
---|
2095 | logging.error(msg) |
---|
2096 | if not standalone: |
---|
2097 | msg = "Could not connect to the application server." |
---|
2098 | msg += " Please try again later." |
---|
2099 | self.SetStatusText(msg) |
---|
2100 | |
---|
2101 | def _onAbout(self, evt): |
---|
2102 | """ |
---|
2103 | Pop up the about dialog |
---|
2104 | |
---|
2105 | :param evt: menu event |
---|
2106 | |
---|
2107 | """ |
---|
2108 | if config._do_aboutbox: |
---|
2109 | import sas.guiframe.aboutbox as AboutBox |
---|
2110 | dialog = AboutBox.DialogAbout(None, -1, "") |
---|
2111 | dialog.ShowModal() |
---|
2112 | |
---|
2113 | def _onTutorial(self, evt): |
---|
2114 | """ |
---|
2115 | Pop up the tutorial dialog |
---|
2116 | |
---|
2117 | :param evt: menu event |
---|
2118 | |
---|
2119 | """ |
---|
2120 | if config._do_tutorial: |
---|
2121 | path = config.TUTORIAL_PATH |
---|
2122 | if IS_WIN: |
---|
2123 | try: |
---|
2124 | from sas.guiframe.pdfview import PDFFrame |
---|
2125 | dialog = PDFFrame(None, -1, "Tutorial", path) |
---|
2126 | # put icon |
---|
2127 | self.put_icon(dialog) |
---|
2128 | dialog.Show(True) |
---|
2129 | except: |
---|
2130 | logging.error("Error in _onTutorial: %s" % sys.exc_value) |
---|
2131 | try: |
---|
2132 | #in case when the pdf default set other than acrobat |
---|
2133 | import ho.pisa as pisa |
---|
2134 | pisa.startViewer(path) |
---|
2135 | except: |
---|
2136 | msg = "This feature requires 'PDF Viewer'\n" |
---|
2137 | msg += "Please install it first (Free)..." |
---|
2138 | wx.MessageBox(msg, 'Error') |
---|
2139 | else: |
---|
2140 | try: |
---|
2141 | command = "open '%s'" % path |
---|
2142 | os.system(command) |
---|
2143 | except: |
---|
2144 | try: |
---|
2145 | #in case when the pdf default set other than preview |
---|
2146 | import ho.pisa as pisa |
---|
2147 | pisa.startViewer(path) |
---|
2148 | except: |
---|
2149 | msg = "This feature requires 'Preview' Application\n" |
---|
2150 | msg += "Please install it first..." |
---|
2151 | wx.MessageBox(msg, 'Error') |
---|
2152 | |
---|
2153 | def _onSphinxDocs(self, evt): |
---|
2154 | """ |
---|
2155 | Bring up Sphinx Documentation. If Wx 2.9 or higher is installed |
---|
2156 | with proper HTML support then Pop up a Sphinx Documentation dialog |
---|
2157 | locally. If not pop up a new tab in the default system browser |
---|
2158 | calling the documentation website. |
---|
2159 | |
---|
2160 | :param evt: menu event |
---|
2161 | """ |
---|
2162 | # Running SasView "in-place" using run.py means the docs will be in a |
---|
2163 | # different place than they would otherwise. |
---|
2164 | |
---|
2165 | show_sphinx_docs = float(wx.__version__[:3]) >= 2.9 |
---|
2166 | if show_sphinx_docs: |
---|
2167 | SPHINX_DOC_ENV = "SASVIEW_DOC_PATH" |
---|
2168 | if SPHINX_DOC_ENV in os.environ: |
---|
2169 | docs_path = os.path.join(os.environ[SPHINX_DOC_ENV], "index.html") |
---|
2170 | else: |
---|
2171 | docs_path = os.path.join(PATH_APP, "..", "..", "doc", "index.html") |
---|
2172 | |
---|
2173 | if os.path.exists(docs_path): |
---|
2174 | from documentation_window import DocumentationWindow |
---|
2175 | |
---|
2176 | sphinx_doc_viewer = DocumentationWindow(None, -1, docs_path) |
---|
2177 | sphinx_doc_viewer.Show() |
---|
2178 | else: |
---|
2179 | logging.error("Could not find Sphinx documentation at '%' -- has it been built?" % docs_path) |
---|
2180 | else: |
---|
2181 | #For red hat and maybe others who do not have Wx 3.0 |
---|
2182 | #just send to webpage of documentation |
---|
2183 | webbrowser.open_new_tab('http://www.sasview.org/sasview') |
---|
2184 | |
---|
2185 | def set_manager(self, manager): |
---|
2186 | """ |
---|
2187 | Sets the application manager for this frame |
---|
2188 | |
---|
2189 | :param manager: frame manager |
---|
2190 | """ |
---|
2191 | self.app_manager = manager |
---|
2192 | |
---|
2193 | def post_init(self): |
---|
2194 | """ |
---|
2195 | This initialization method is called after the GUI |
---|
2196 | has been created and all plug-ins loaded. It calls |
---|
2197 | the post_init() method of each plug-in (if it exists) |
---|
2198 | so that final initialization can be done. |
---|
2199 | """ |
---|
2200 | for item in self.plugins: |
---|
2201 | if hasattr(item, "post_init"): |
---|
2202 | item.post_init() |
---|
2203 | |
---|
2204 | def set_default_perspective(self): |
---|
2205 | """ |
---|
2206 | Choose among the plugin the first plug-in that has |
---|
2207 | "set_default_perspective" method and its return value is True will be |
---|
2208 | as a default perspective when the welcome page is closed |
---|
2209 | """ |
---|
2210 | for item in self.plugins: |
---|
2211 | if hasattr(item, "set_default_perspective"): |
---|
2212 | if item.set_default_perspective(): |
---|
2213 | item.on_perspective(event=None) |
---|
2214 | return |
---|
2215 | |
---|
2216 | def set_perspective(self, panels): |
---|
2217 | """ |
---|
2218 | Sets the perspective of the GUI. |
---|
2219 | Opens all the panels in the list, and closes |
---|
2220 | all the others. |
---|
2221 | |
---|
2222 | :param panels: list of panels |
---|
2223 | """ |
---|
2224 | for item in self.panels.keys(): |
---|
2225 | # Check whether this is a sticky panel |
---|
2226 | if hasattr(self.panels[item], "ALWAYS_ON"): |
---|
2227 | if self.panels[item].ALWAYS_ON: |
---|
2228 | continue |
---|
2229 | if self.panels[item] == None: |
---|
2230 | continue |
---|
2231 | if self.panels[item].window_name in panels: |
---|
2232 | frame = self.panels[item].get_frame() |
---|
2233 | if not frame.IsShown(): |
---|
2234 | frame.Show(True) |
---|
2235 | else: |
---|
2236 | # always show the data panel if enable |
---|
2237 | style = self.__gui_style & GUIFRAME.MANAGER_ON |
---|
2238 | if (style == GUIFRAME.MANAGER_ON) and self.panels[item] == self._data_panel: |
---|
2239 | if 'data_panel' in self.panels.keys(): |
---|
2240 | frame = self.panels['data_panel'].get_frame() |
---|
2241 | if frame == None: |
---|
2242 | continue |
---|
2243 | flag = frame.IsShown() |
---|
2244 | frame.Show(flag) |
---|
2245 | else: |
---|
2246 | frame = self.panels[item].get_frame() |
---|
2247 | if frame == None: |
---|
2248 | continue |
---|
2249 | |
---|
2250 | if frame.IsShown(): |
---|
2251 | frame.Show(False) |
---|
2252 | |
---|
2253 | def show_data_panel(self, event=None, action=True): |
---|
2254 | """ |
---|
2255 | show the data panel |
---|
2256 | """ |
---|
2257 | if self._data_panel_menu == None: |
---|
2258 | return |
---|
2259 | label = self._data_panel_menu.GetText() |
---|
2260 | pane = self.panels["data_panel"] |
---|
2261 | frame = pane.get_frame() |
---|
2262 | if label == 'Show Data Explorer': |
---|
2263 | if action: |
---|
2264 | frame.Show(True) |
---|
2265 | self.__gui_style = self.__gui_style | GUIFRAME.MANAGER_ON |
---|
2266 | self._data_panel_menu.SetText('Hide Data Explorer') |
---|
2267 | else: |
---|
2268 | if action: |
---|
2269 | frame.Show(False) |
---|
2270 | self.__gui_style = self.__gui_style & (~GUIFRAME.MANAGER_ON) |
---|
2271 | self._data_panel_menu.SetText('Show Data Explorer') |
---|
2272 | |
---|
2273 | def add_data_helper(self, data_list): |
---|
2274 | """ |
---|
2275 | """ |
---|
2276 | if self._data_manager is not None: |
---|
2277 | self._data_manager.add_data(data_list) |
---|
2278 | |
---|
2279 | def add_data(self, data_list): |
---|
2280 | """ |
---|
2281 | receive a dictionary of data from loader |
---|
2282 | store them its data manager if possible |
---|
2283 | send to data the current active perspective if the data panel |
---|
2284 | is not active. |
---|
2285 | :param data_list: dictionary of data's ID and value Data |
---|
2286 | """ |
---|
2287 | #Store data into manager |
---|
2288 | self.add_data_helper(data_list) |
---|
2289 | # set data in the data panel |
---|
2290 | if self._data_panel is not None: |
---|
2291 | data_state = self._data_manager.get_data_state(data_list.keys()) |
---|
2292 | self._data_panel.load_data_list(data_state) |
---|
2293 | #if the data panel is shown wait for the user to press a button |
---|
2294 | #to send data to the current perspective. if the panel is not |
---|
2295 | #show automatically send the data to the current perspective |
---|
2296 | style = self.__gui_style & GUIFRAME.MANAGER_ON |
---|
2297 | if style == GUIFRAME.MANAGER_ON: |
---|
2298 | #wait for button press from the data panel to set_data |
---|
2299 | if self._data_panel is not None: |
---|
2300 | self._data_panel.frame.Show(True) |
---|
2301 | else: |
---|
2302 | #automatically send that to the current perspective |
---|
2303 | self.set_data(data_id=data_list.keys()) |
---|
2304 | |
---|
2305 | def set_data(self, data_id, theory_id=None): |
---|
2306 | """ |
---|
2307 | set data to current perspective |
---|
2308 | """ |
---|
2309 | list_data, _ = self._data_manager.get_by_id(data_id) |
---|
2310 | if self._current_perspective is not None: |
---|
2311 | self._current_perspective.set_data(list_data.values()) |
---|
2312 | |
---|
2313 | else: |
---|
2314 | msg = "Guiframe does not have a current perspective" |
---|
2315 | logging.info(msg) |
---|
2316 | |
---|
2317 | def set_theory(self, state_id, theory_id=None): |
---|
2318 | """ |
---|
2319 | """ |
---|
2320 | _, list_theory = self._data_manager.get_by_id(theory_id) |
---|
2321 | if self._current_perspective is not None: |
---|
2322 | try: |
---|
2323 | self._current_perspective.set_theory(list_theory.values()) |
---|
2324 | except: |
---|
2325 | msg = "Guiframe set_theory: \n" + str(sys.exc_value) |
---|
2326 | logging.info(msg) |
---|
2327 | wx.PostEvent(self, StatusEvent(status=msg, info="error")) |
---|
2328 | else: |
---|
2329 | msg = "Guiframe does not have a current perspective" |
---|
2330 | logging.info(msg) |
---|
2331 | |
---|
2332 | def plot_data(self, state_id, data_id=None, |
---|
2333 | theory_id=None, append=False): |
---|
2334 | """ |
---|
2335 | send a list of data to plot |
---|
2336 | """ |
---|
2337 | total_plot_list = [] |
---|
2338 | data_list, _ = self._data_manager.get_by_id(data_id) |
---|
2339 | _, temp_list_theory = self._data_manager.get_by_id(theory_id) |
---|
2340 | total_plot_list = data_list.values() |
---|
2341 | for item in temp_list_theory.values(): |
---|
2342 | theory_data, theory_state = item |
---|
2343 | total_plot_list.append(theory_data) |
---|
2344 | GROUP_ID = wx.NewId() |
---|
2345 | for new_plot in total_plot_list: |
---|
2346 | if append: |
---|
2347 | if self.panel_on_focus is None: |
---|
2348 | message = "cannot append plot. No plot panel on focus!" |
---|
2349 | message += "please click on any available plot to set focus" |
---|
2350 | wx.PostEvent(self, StatusEvent(status=message, |
---|
2351 | info='warning')) |
---|
2352 | return |
---|
2353 | else: |
---|
2354 | if self.enable_add_data(new_plot): |
---|
2355 | new_plot.group_id = self.panel_on_focus.group_id |
---|
2356 | else: |
---|
2357 | message = "Only 1D Data can be append to" |
---|
2358 | message += " plot panel containing 1D data.\n" |
---|
2359 | message += "%s not be appended.\n" %str(new_plot.name) |
---|
2360 | message += "try new plot option.\n" |
---|
2361 | wx.PostEvent(self, StatusEvent(status=message, |
---|
2362 | info='warning')) |
---|
2363 | else: |
---|
2364 | #if not append then new plot |
---|
2365 | from sas.guiframe.dataFitting import Data2D |
---|
2366 | if issubclass(Data2D, new_plot.__class__): |
---|
2367 | #for 2 D always plot in a separated new plot |
---|
2368 | new_plot.group_id = wx.NewId() |
---|
2369 | else: |
---|
2370 | # plot all 1D in a new plot |
---|
2371 | new_plot.group_id = GROUP_ID |
---|
2372 | title = "PLOT " + str(new_plot.title) |
---|
2373 | wx.PostEvent(self, NewPlotEvent(plot=new_plot, |
---|
2374 | title=title, |
---|
2375 | group_id = new_plot.group_id)) |
---|
2376 | |
---|
2377 | def remove_data(self, data_id, theory_id=None): |
---|
2378 | """ |
---|
2379 | Delete data state if data_id is provide |
---|
2380 | delete theory created with data of id data_id if theory_id is provide |
---|
2381 | if delete all true: delete the all state |
---|
2382 | else delete theory |
---|
2383 | """ |
---|
2384 | temp = data_id + theory_id |
---|
2385 | for plug in self.plugins: |
---|
2386 | plug.delete_data(temp) |
---|
2387 | total_plot_list = [] |
---|
2388 | data_list, _ = self._data_manager.get_by_id(data_id) |
---|
2389 | _, temp_list_theory = self._data_manager.get_by_id(theory_id) |
---|
2390 | total_plot_list = data_list.values() |
---|
2391 | for item in temp_list_theory.values(): |
---|
2392 | theory_data, theory_state = item |
---|
2393 | total_plot_list.append(theory_data) |
---|
2394 | for new_plot in total_plot_list: |
---|
2395 | id = new_plot.id |
---|
2396 | for group_id in new_plot.list_group_id: |
---|
2397 | wx.PostEvent(self, NewPlotEvent(id=id, |
---|
2398 | group_id=group_id, |
---|
2399 | action='remove')) |
---|
2400 | #remove res plot: Todo: improve |
---|
2401 | wx.CallAfter(self._remove_res_plot, id) |
---|
2402 | self._data_manager.delete_data(data_id=data_id, |
---|
2403 | theory_id=theory_id) |
---|
2404 | |
---|
2405 | def _remove_res_plot(self, id): |
---|
2406 | """ |
---|
2407 | Try to remove corresponding res plot |
---|
2408 | |
---|
2409 | : param id: id of the data |
---|
2410 | """ |
---|
2411 | try: |
---|
2412 | wx.PostEvent(self, NewPlotEvent(id=("res"+str(id)), |
---|
2413 | group_id=("res"+str(id)), |
---|
2414 | action='remove')) |
---|
2415 | except: |
---|
2416 | pass |
---|
2417 | |
---|
2418 | def save_data1d(self, data, fname): |
---|
2419 | """ |
---|
2420 | Save data dialog |
---|
2421 | """ |
---|
2422 | default_name = fname |
---|
2423 | wildcard = "Text files (*.txt)|*.txt|"\ |
---|
2424 | "CanSAS 1D files(*.xml)|*.xml" |
---|
2425 | path = None |
---|
2426 | dlg = wx.FileDialog(self, "Choose a file", |
---|
2427 | self._default_save_location, |
---|
2428 | default_name, wildcard , wx.SAVE) |
---|
2429 | |
---|
2430 | if dlg.ShowModal() == wx.ID_OK: |
---|
2431 | path = dlg.GetPath() |
---|
2432 | # ext_num = 0 for .txt, ext_num = 1 for .xml |
---|
2433 | # This is MAC Fix |
---|
2434 | ext_num = dlg.GetFilterIndex() |
---|
2435 | if ext_num == 0: |
---|
2436 | format = '.txt' |
---|
2437 | else: |
---|
2438 | format = '.xml' |
---|
2439 | path = os.path.splitext(path)[0] + format |
---|
2440 | mypath = os.path.basename(path) |
---|
2441 | |
---|
2442 | #TODO: This is bad design. The DataLoader is designed |
---|
2443 | #to recognize extensions. |
---|
2444 | # It should be a simple matter of calling the . |
---|
2445 | #save(file, data, '.xml') method |
---|
2446 | # of the sas.dataloader.loader.Loader class. |
---|
2447 | from sas.dataloader.loader import Loader |
---|
2448 | #Instantiate a loader |
---|
2449 | loader = Loader() |
---|
2450 | format = ".txt" |
---|
2451 | if os.path.splitext(mypath)[1].lower() == format: |
---|
2452 | # Make sure the ext included in the file name |
---|
2453 | # especially on MAC |
---|
2454 | fName = os.path.splitext(path)[0] + format |
---|
2455 | self._onsaveTXT(data, fName) |
---|
2456 | format = ".xml" |
---|
2457 | if os.path.splitext(mypath)[1].lower() == format: |
---|
2458 | # Make sure the ext included in the file name |
---|
2459 | # especially on MAC |
---|
2460 | fName = os.path.splitext(path)[0] + format |
---|
2461 | loader.save(fName, data, format) |
---|
2462 | try: |
---|
2463 | self._default_save_location = os.path.dirname(path) |
---|
2464 | except: |
---|
2465 | pass |
---|
2466 | dlg.Destroy() |
---|
2467 | |
---|
2468 | |
---|
2469 | def _onsaveTXT(self, data, path): |
---|
2470 | """ |
---|
2471 | Save file as txt |
---|
2472 | :TODO: Refactor and remove this method. See TODO in _onSave. |
---|
2473 | """ |
---|
2474 | if not path == None: |
---|
2475 | out = open(path, 'w') |
---|
2476 | has_errors = True |
---|
2477 | if data.dy == None or data.dy == []: |
---|
2478 | has_errors = False |
---|
2479 | # Sanity check |
---|
2480 | if has_errors: |
---|
2481 | try: |
---|
2482 | if len(data.y) != len(data.dy): |
---|
2483 | has_errors = False |
---|
2484 | except: |
---|
2485 | has_errors = False |
---|
2486 | if has_errors: |
---|
2487 | if data.dx != None and data.dx != []: |
---|
2488 | out.write("<X> <Y> <dY> <dX>\n") |
---|
2489 | else: |
---|
2490 | out.write("<X> <Y> <dY>\n") |
---|
2491 | else: |
---|
2492 | out.write("<X> <Y>\n") |
---|
2493 | |
---|
2494 | for i in range(len(data.x)): |
---|
2495 | if has_errors: |
---|
2496 | if data.dx != None and data.dx != []: |
---|
2497 | if data.dx[i] != None: |
---|
2498 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
2499 | data.y[i], |
---|
2500 | data.dy[i], |
---|
2501 | data.dx[i])) |
---|
2502 | else: |
---|
2503 | out.write("%g %g %g\n" % (data.x[i], |
---|
2504 | data.y[i], |
---|
2505 | data.dy[i])) |
---|
2506 | else: |
---|
2507 | out.write("%g %g %g\n" % (data.x[i], |
---|
2508 | data.y[i], |
---|
2509 | data.dy[i])) |
---|
2510 | else: |
---|
2511 | out.write("%g %g\n" % (data.x[i], |
---|
2512 | data.y[i])) |
---|
2513 | out.close() |
---|
2514 | |
---|
2515 | def show_data1d(self, data, name): |
---|
2516 | """ |
---|
2517 | Show data dialog |
---|
2518 | """ |
---|
2519 | try: |
---|
2520 | xmin = min(data.x) |
---|
2521 | ymin = min(data.y) |
---|
2522 | except: |
---|
2523 | msg = "Unable to find min/max of \n data named %s"% \ |
---|
2524 | data.filename |
---|
2525 | wx.PostEvent(self, StatusEvent(status=msg, |
---|
2526 | info="error")) |
---|
2527 | raise ValueError, msg |
---|
2528 | ## text = str(data) |
---|
2529 | text = data.__str__() |
---|
2530 | text += 'Data Min Max:\n' |
---|
2531 | text += 'X_min = %s: X_max = %s\n'% (xmin, max(data.x)) |
---|
2532 | text += 'Y_min = %s: Y_max = %s\n'% (ymin, max(data.y)) |
---|
2533 | if data.dy != None: |
---|
2534 | text += 'dY_min = %s: dY_max = %s\n'% (min(data.dy), max(data.dy)) |
---|
2535 | text += '\nData Points:\n' |
---|
2536 | x_st = "X" |
---|
2537 | for index in range(len(data.x)): |
---|
2538 | if data.dy != None and len(data.dy) > index: |
---|
2539 | dy_val = data.dy[index] |
---|
2540 | else: |
---|
2541 | dy_val = 0.0 |
---|
2542 | if data.dx != None and len(data.dx) > index: |
---|
2543 | dx_val = data.dx[index] |
---|
2544 | else: |
---|
2545 | dx_val = 0.0 |
---|
2546 | if data.dxl != None and len(data.dxl) > index: |
---|
2547 | if index == 0: |
---|
2548 | x_st = "Xl" |
---|
2549 | dx_val = data.dxl[index] |
---|
2550 | elif data.dxw != None and len(data.dxw) > index: |
---|
2551 | if index == 0: |
---|
2552 | x_st = "Xw" |
---|
2553 | dx_val = data.dxw[index] |
---|
2554 | |
---|
2555 | if index == 0: |
---|
2556 | text += "<index> \t<X> \t<Y> \t<dY> \t<d%s>\n"% x_st |
---|
2557 | text += "%s \t%s \t%s \t%s \t%s\n" % (index, |
---|
2558 | data.x[index], |
---|
2559 | data.y[index], |
---|
2560 | dy_val, |
---|
2561 | dx_val) |
---|
2562 | from pdfview import TextFrame |
---|
2563 | frame = TextFrame(None, -1, "Data Info: %s"% data.name, text) |
---|
2564 | # put icon |
---|
2565 | self.put_icon(frame) |
---|
2566 | frame.Show(True) |
---|
2567 | |
---|
2568 | def save_data2d(self, data, fname): |
---|
2569 | """ |
---|
2570 | Save data2d dialog |
---|
2571 | """ |
---|
2572 | default_name = fname |
---|
2573 | wildcard = "IGOR/DAT 2D file in Q_map (*.dat)|*.DAT" |
---|
2574 | dlg = wx.FileDialog(self, "Choose a file", |
---|
2575 | self._default_save_location, |
---|
2576 | default_name, wildcard , wx.SAVE) |
---|
2577 | |
---|
2578 | if dlg.ShowModal() == wx.ID_OK: |
---|
2579 | path = dlg.GetPath() |
---|
2580 | # ext_num = 0 for .txt, ext_num = 1 for .xml |
---|
2581 | # This is MAC Fix |
---|
2582 | ext_num = dlg.GetFilterIndex() |
---|
2583 | if ext_num == 0: |
---|
2584 | format = '.dat' |
---|
2585 | else: |
---|
2586 | format = '' |
---|
2587 | path = os.path.splitext(path)[0] + format |
---|
2588 | mypath = os.path.basename(path) |
---|
2589 | |
---|
2590 | #TODO: This is bad design. The DataLoader is designed |
---|
2591 | #to recognize extensions. |
---|
2592 | # It should be a simple matter of calling the . |
---|
2593 | #save(file, data, '.xml') method |
---|
2594 | # of the DataLoader.loader.Loader class. |
---|
2595 | from sas.dataloader.loader import Loader |
---|
2596 | #Instantiate a loader |
---|
2597 | loader = Loader() |
---|
2598 | |
---|
2599 | format = ".dat" |
---|
2600 | if os.path.splitext(mypath)[1].lower() == format: |
---|
2601 | # Make sure the ext included in the file name |
---|
2602 | # especially on MAC |
---|
2603 | fileName = os.path.splitext(path)[0] + format |
---|
2604 | loader.save(fileName, data, format) |
---|
2605 | try: |
---|
2606 | self._default_save_location = os.path.dirname(path) |
---|
2607 | except: |
---|
2608 | pass |
---|
2609 | dlg.Destroy() |
---|
2610 | |
---|
2611 | def show_data2d(self, data, name): |
---|
2612 | """ |
---|
2613 | Show data dialog |
---|
2614 | """ |
---|
2615 | |
---|
2616 | wx.PostEvent(self, StatusEvent(status = "Gathering Data2D Info.", |
---|
2617 | type = 'start' )) |
---|
2618 | text = data.__str__() |
---|
2619 | text += 'Data Min Max:\n' |
---|
2620 | text += 'I_min = %s\n'% min(data.data) |
---|
2621 | text += 'I_max = %s\n\n'% max(data.data) |
---|
2622 | text += 'Data (First 2501) Points:\n' |
---|
2623 | text += 'Data columns include err(I).\n' |
---|
2624 | text += 'ASCII data starts here.\n' |
---|
2625 | text += "<index> \t<Qx> \t<Qy> \t<I> \t<dI> \t<dQparal> \t<dQperp>\n" |
---|
2626 | di_val = 0.0 |
---|
2627 | dx_val = 0.0 |
---|
2628 | dy_val = 0.0 |
---|
2629 | #mask_val = True |
---|
2630 | len_data = len(data.qx_data) |
---|
2631 | for index in xrange(0, len_data): |
---|
2632 | x_val = data.qx_data[index] |
---|
2633 | y_val = data.qy_data[index] |
---|
2634 | i_val = data.data[index] |
---|
2635 | if data.err_data != None: |
---|
2636 | di_val = data.err_data[index] |
---|
2637 | if data.dqx_data != None: |
---|
2638 | dx_val = data.dqx_data[index] |
---|
2639 | if data.dqy_data != None: |
---|
2640 | dy_val = data.dqy_data[index] |
---|
2641 | |
---|
2642 | text += "%s \t%s \t%s \t%s \t%s \t%s \t%s\n" % (index, |
---|
2643 | x_val, |
---|
2644 | y_val, |
---|
2645 | i_val, |
---|
2646 | di_val, |
---|
2647 | dx_val, |
---|
2648 | dy_val) |
---|
2649 | # Takes too long time for typical data2d: Break here |
---|
2650 | if index >= 2500: |
---|
2651 | text += ".............\n" |
---|
2652 | break |
---|
2653 | |
---|
2654 | from pdfview import TextFrame |
---|
2655 | frame = TextFrame(None, -1, "Data Info: %s"% data.name, text) |
---|
2656 | # put icon |
---|
2657 | self.put_icon(frame) |
---|
2658 | frame.Show(True) |
---|
2659 | wx.PostEvent(self, StatusEvent(status = "Data2D Info Displayed", |
---|
2660 | type = 'stop' )) |
---|
2661 | |
---|
2662 | def set_current_perspective(self, perspective): |
---|
2663 | """ |
---|
2664 | set the current active perspective |
---|
2665 | """ |
---|
2666 | self._current_perspective = perspective |
---|
2667 | name = "No current analysis selected" |
---|
2668 | if self._current_perspective is not None: |
---|
2669 | self._add_current_plugin_menu() |
---|
2670 | for panel in self.panels.values(): |
---|
2671 | if hasattr(panel, 'CENTER_PANE') and panel.CENTER_PANE: |
---|
2672 | for name in self._current_perspective.get_perspective(): |
---|
2673 | frame = panel.get_frame() |
---|
2674 | if frame != None: |
---|
2675 | if name == panel.window_name: |
---|
2676 | panel.on_set_focus(event=None) |
---|
2677 | frame.Show(True) |
---|
2678 | else: |
---|
2679 | frame.Show(False) |
---|
2680 | #break |
---|
2681 | name = self._current_perspective.sub_menu |
---|
2682 | if self._data_panel is not None: |
---|
2683 | self._data_panel.set_active_perspective(name) |
---|
2684 | self._check_applications_menu() |
---|
2685 | #Set the SasView title |
---|
2686 | self._set_title_name(name) |
---|
2687 | |
---|
2688 | def _set_title_name(self, name): |
---|
2689 | """ |
---|
2690 | Set the SasView title w/ the current application name |
---|
2691 | |
---|
2692 | : param name: application name [string] |
---|
2693 | """ |
---|
2694 | # Set SanView Window title w/ application anme |
---|
2695 | title = self.title + " - " + name + " -" |
---|
2696 | self.SetTitle(title) |
---|
2697 | |
---|
2698 | def _check_applications_menu(self): |
---|
2699 | """ |
---|
2700 | check the menu of the current application |
---|
2701 | """ |
---|
2702 | if self._applications_menu is not None: |
---|
2703 | for menu in self._applications_menu.GetMenuItems(): |
---|
2704 | if self._current_perspective is not None: |
---|
2705 | name = self._current_perspective.sub_menu |
---|
2706 | if menu.IsCheckable(): |
---|
2707 | if menu.GetLabel() == name: |
---|
2708 | menu.Check(True) |
---|
2709 | else: |
---|
2710 | menu.Check(False) |
---|
2711 | |
---|
2712 | def enable_add_data(self, new_plot): |
---|
2713 | """ |
---|
2714 | Enable append data on a plot panel |
---|
2715 | """ |
---|
2716 | |
---|
2717 | if self.panel_on_focus not in self._plotting_plugin.plot_panels.values(): |
---|
2718 | return |
---|
2719 | is_theory = len(self.panel_on_focus.plots) <= 1 and \ |
---|
2720 | self.panel_on_focus.plots.values()[0].__class__.__name__ == "Theory1D" |
---|
2721 | |
---|
2722 | is_data2d = hasattr(new_plot, 'data') |
---|
2723 | |
---|
2724 | is_data1d = self.panel_on_focus.__class__.__name__ == "ModelPanel1D"\ |
---|
2725 | and self.panel_on_focus.group_id is not None |
---|
2726 | has_meta_data = hasattr(new_plot, 'meta_data') |
---|
2727 | |
---|
2728 | #disable_add_data if the data is being recovered from a saved state file. |
---|
2729 | is_state_data = False |
---|
2730 | if has_meta_data: |
---|
2731 | if 'invstate' in new_plot.meta_data: |
---|
2732 | is_state_data = True |
---|
2733 | if 'prstate' in new_plot.meta_data: |
---|
2734 | is_state_data = True |
---|
2735 | if 'fitstate' in new_plot.meta_data: |
---|
2736 | is_state_data = True |
---|
2737 | |
---|
2738 | return is_data1d and not is_data2d and not is_theory and not is_state_data |
---|
2739 | |
---|
2740 | def check_multimode(self, perspective=None): |
---|
2741 | """ |
---|
2742 | Check the perspective have batch mode capablitity |
---|
2743 | """ |
---|
2744 | if perspective == None or self._data_panel == None: |
---|
2745 | return |
---|
2746 | flag = perspective.get_batch_capable() |
---|
2747 | flag_on = perspective.batch_on |
---|
2748 | if flag: |
---|
2749 | self._data_panel.rb_single_mode.SetValue(not flag_on) |
---|
2750 | self._data_panel.rb_batch_mode.SetValue(flag_on) |
---|
2751 | else: |
---|
2752 | self._data_panel.rb_single_mode.SetValue(True) |
---|
2753 | self._data_panel.rb_batch_mode.SetValue(False) |
---|
2754 | self._data_panel.rb_single_mode.Enable(flag) |
---|
2755 | self._data_panel.rb_batch_mode.Enable(flag) |
---|
2756 | |
---|
2757 | |
---|
2758 | |
---|
2759 | def enable_edit_menu(self): |
---|
2760 | """ |
---|
2761 | enable menu item under edit menu depending on the panel on focus |
---|
2762 | """ |
---|
2763 | if self.cpanel_on_focus is not None and self._edit_menu is not None: |
---|
2764 | flag = self.cpanel_on_focus.get_undo_flag() |
---|
2765 | self._edit_menu.Enable(GUIFRAME_ID.UNDO_ID, flag) |
---|
2766 | flag = self.cpanel_on_focus.get_redo_flag() |
---|
2767 | self._edit_menu.Enable(GUIFRAME_ID.REDO_ID, flag) |
---|
2768 | flag = self.cpanel_on_focus.get_copy_flag() |
---|
2769 | self._edit_menu.Enable(GUIFRAME_ID.COPY_ID, flag) |
---|
2770 | flag = self.cpanel_on_focus.get_paste_flag() |
---|
2771 | self._edit_menu.Enable(GUIFRAME_ID.PASTE_ID, flag) |
---|
2772 | |
---|
2773 | #Copy menu |
---|
2774 | flag = self.cpanel_on_focus.get_copy_flag() |
---|
2775 | #self._edit_menu.ENABLE(GUIFRAME_ID.COPYAS_ID,flag) |
---|
2776 | self._edit_menu_copyas.Enable(GUIFRAME_ID.COPYEX_ID, flag) |
---|
2777 | self._edit_menu_copyas.Enable(GUIFRAME_ID.COPYLAT_ID, flag) |
---|
2778 | |
---|
2779 | flag = self.cpanel_on_focus.get_preview_flag() |
---|
2780 | self._edit_menu.Enable(GUIFRAME_ID.PREVIEW_ID, flag) |
---|
2781 | flag = self.cpanel_on_focus.get_reset_flag() |
---|
2782 | self._edit_menu.Enable(GUIFRAME_ID.RESET_ID, flag) |
---|
2783 | else: |
---|
2784 | flag = False |
---|
2785 | self._edit_menu.Enable(GUIFRAME_ID.UNDO_ID, flag) |
---|
2786 | self._edit_menu.Enable(GUIFRAME_ID.REDO_ID, flag) |
---|
2787 | self._edit_menu.Enable(GUIFRAME_ID.COPY_ID, flag) |
---|
2788 | self._edit_menu.Enable(GUIFRAME_ID.PASTE_ID, flag) |
---|
2789 | #self._edit_menu.Enable(GUIFRAME_ID.COPYEX_ID, flag) |
---|
2790 | #self._edit_menu.Enable(GUIFRAME_ID.COPYLAT_ID, flag) |
---|
2791 | #self._edit_menu.Enable(GUIFRAME_ID.COPYAS_ID, flag) |
---|
2792 | self._edit_menu.Enable(GUIFRAME_ID.PREVIEW_ID, flag) |
---|
2793 | self._edit_menu.Enable(GUIFRAME_ID.RESET_ID, flag) |
---|
2794 | |
---|
2795 | def on_undo_panel(self, event=None): |
---|
2796 | """ |
---|
2797 | undo previous action of the last panel on focus if possible |
---|
2798 | """ |
---|
2799 | if self.cpanel_on_focus is not None: |
---|
2800 | self.cpanel_on_focus.on_undo(event) |
---|
2801 | |
---|
2802 | def on_redo_panel(self, event=None): |
---|
2803 | """ |
---|
2804 | redo the last cancel action done on the last panel on focus |
---|
2805 | """ |
---|
2806 | if self.cpanel_on_focus is not None: |
---|
2807 | self.cpanel_on_focus.on_redo(event) |
---|
2808 | |
---|
2809 | def on_copy_panel(self, event=None): |
---|
2810 | """ |
---|
2811 | copy the last panel on focus if possible |
---|
2812 | """ |
---|
2813 | if self.cpanel_on_focus is not None: |
---|
2814 | self.cpanel_on_focus.on_copy(event) |
---|
2815 | |
---|
2816 | def on_paste_panel(self, event=None): |
---|
2817 | """ |
---|
2818 | paste clipboard to the last panel on focus |
---|
2819 | """ |
---|
2820 | if self.cpanel_on_focus is not None: |
---|
2821 | self.cpanel_on_focus.on_paste(event) |
---|
2822 | |
---|
2823 | def on_bookmark_panel(self, event=None): |
---|
2824 | """ |
---|
2825 | bookmark panel |
---|
2826 | """ |
---|
2827 | if self.cpanel_on_focus is not None: |
---|
2828 | self.cpanel_on_focus.on_bookmark(event) |
---|
2829 | |
---|
2830 | def append_bookmark(self, event=None): |
---|
2831 | """ |
---|
2832 | Bookmark available information of the panel on focus |
---|
2833 | """ |
---|
2834 | self._toolbar.append_bookmark(event) |
---|
2835 | |
---|
2836 | def on_save_panel(self, event=None): |
---|
2837 | """ |
---|
2838 | save possible information on the current panel |
---|
2839 | """ |
---|
2840 | if self.cpanel_on_focus is not None: |
---|
2841 | self.cpanel_on_focus.on_save(event) |
---|
2842 | |
---|
2843 | def on_preview_panel(self, event=None): |
---|
2844 | """ |
---|
2845 | preview information on the panel on focus |
---|
2846 | """ |
---|
2847 | if self.cpanel_on_focus is not None: |
---|
2848 | self.cpanel_on_focus.on_preview(event) |
---|
2849 | |
---|
2850 | def on_print_panel(self, event=None): |
---|
2851 | """ |
---|
2852 | print available information on the last panel on focus |
---|
2853 | """ |
---|
2854 | if self.cpanel_on_focus is not None: |
---|
2855 | self.cpanel_on_focus.on_print(event) |
---|
2856 | |
---|
2857 | def on_zoom_panel(self, event=None): |
---|
2858 | """ |
---|
2859 | zoom on the current panel if possible |
---|
2860 | """ |
---|
2861 | if self.cpanel_on_focus is not None: |
---|
2862 | self.cpanel_on_focus.on_zoom(event) |
---|
2863 | |
---|
2864 | def on_zoom_in_panel(self, event=None): |
---|
2865 | """ |
---|
2866 | zoom in of the panel on focus |
---|
2867 | """ |
---|
2868 | if self.cpanel_on_focus is not None: |
---|
2869 | self.cpanel_on_focus.on_zoom_in(event) |
---|
2870 | |
---|
2871 | def on_zoom_out_panel(self, event=None): |
---|
2872 | """ |
---|
2873 | zoom out on the panel on focus |
---|
2874 | """ |
---|
2875 | if self.cpanel_on_focus is not None: |
---|
2876 | self.cpanel_on_focus.on_zoom_out(event) |
---|
2877 | |
---|
2878 | def on_drag_panel(self, event=None): |
---|
2879 | """ |
---|
2880 | drag apply to the panel on focus |
---|
2881 | """ |
---|
2882 | if self.cpanel_on_focus is not None: |
---|
2883 | self.cpanel_on_focus.on_drag(event) |
---|
2884 | |
---|
2885 | def on_reset_panel(self, event=None): |
---|
2886 | """ |
---|
2887 | reset the current panel |
---|
2888 | """ |
---|
2889 | if self.cpanel_on_focus is not None: |
---|
2890 | self.cpanel_on_focus.on_reset(event) |
---|
2891 | |
---|
2892 | def on_change_caption(self, name, old_caption, new_caption): |
---|
2893 | """ |
---|
2894 | Change the panel caption |
---|
2895 | |
---|
2896 | :param name: window_name of the pane |
---|
2897 | :param old_caption: current caption [string] |
---|
2898 | :param new_caption: new caption [string] |
---|
2899 | """ |
---|
2900 | # wx.aui.AuiPaneInfo |
---|
2901 | pane_info = self.get_paneinfo(old_caption) |
---|
2902 | # update the data_panel.cb_plotpanel |
---|
2903 | if 'data_panel' in self.panels.keys(): |
---|
2904 | # remove from data_panel combobox |
---|
2905 | data_panel = self.panels["data_panel"] |
---|
2906 | if data_panel.cb_plotpanel is not None: |
---|
2907 | # Check if any panel has the same caption |
---|
2908 | has_newstring = data_panel.cb_plotpanel.FindString\ |
---|
2909 | (str(new_caption)) |
---|
2910 | caption = new_caption |
---|
2911 | if has_newstring != wx.NOT_FOUND: |
---|
2912 | captions = self._get_plotpanel_captions() |
---|
2913 | # Append nummber |
---|
2914 | inc = 1 |
---|
2915 | while (1): |
---|
2916 | caption = new_caption + '_%s'% str(inc) |
---|
2917 | if caption not in captions: |
---|
2918 | break |
---|
2919 | inc += 1 |
---|
2920 | # notify to users |
---|
2921 | msg = "Found Same Title: Added '_%s'"% str(inc) |
---|
2922 | wx.PostEvent(self, StatusEvent(status=msg)) |
---|
2923 | # update data_panel cb |
---|
2924 | pos = data_panel.cb_plotpanel.FindString(str(old_caption)) |
---|
2925 | if pos != wx.NOT_FOUND: |
---|
2926 | data_panel.cb_plotpanel.SetString(pos, caption) |
---|
2927 | data_panel.cb_plotpanel.SetStringSelection(caption) |
---|
2928 | # New Caption |
---|
2929 | pane_info.SetTitle(caption) |
---|
2930 | return caption |
---|
2931 | |
---|
2932 | def get_paneinfo(self, name): |
---|
2933 | """ |
---|
2934 | Get pane Caption from window_name |
---|
2935 | |
---|
2936 | :param name: window_name in AuiPaneInfo |
---|
2937 | : return: AuiPaneInfo of the name |
---|
2938 | """ |
---|
2939 | for panel in self.plot_panels.values(): |
---|
2940 | if panel.frame.GetTitle() == name: |
---|
2941 | return panel.frame |
---|
2942 | return None |
---|
2943 | |
---|
2944 | def enable_undo(self): |
---|
2945 | """ |
---|
2946 | enable undo related control |
---|
2947 | """ |
---|
2948 | if self.cpanel_on_focus is not None: |
---|
2949 | self._toolbar.enable_undo(self.cpanel_on_focus) |
---|
2950 | |
---|
2951 | def enable_redo(self): |
---|
2952 | """ |
---|
2953 | enable redo |
---|
2954 | """ |
---|
2955 | if self.cpanel_on_focus is not None: |
---|
2956 | self._toolbar.enable_redo(self.cpanel_on_focus) |
---|
2957 | |
---|
2958 | def enable_copy(self): |
---|
2959 | """ |
---|
2960 | enable copy related control |
---|
2961 | """ |
---|
2962 | if self.cpanel_on_focus is not None: |
---|
2963 | self._toolbar.enable_copy(self.cpanel_on_focus) |
---|
2964 | |
---|
2965 | def enable_paste(self): |
---|
2966 | """ |
---|
2967 | enable paste |
---|
2968 | """ |
---|
2969 | if self.cpanel_on_focus is not None: |
---|
2970 | self._toolbar.enable_paste(self.cpanel_on_focus) |
---|
2971 | |
---|
2972 | def enable_bookmark(self): |
---|
2973 | """ |
---|
2974 | Bookmark |
---|
2975 | """ |
---|
2976 | if self.cpanel_on_focus is not None: |
---|
2977 | self._toolbar.enable_bookmark(self.cpanel_on_focus) |
---|
2978 | |
---|
2979 | def enable_save(self): |
---|
2980 | """ |
---|
2981 | save |
---|
2982 | """ |
---|
2983 | if self.cpanel_on_focus is not None: |
---|
2984 | self._toolbar.enable_save(self.cpanel_on_focus) |
---|
2985 | |
---|
2986 | def enable_preview(self): |
---|
2987 | """ |
---|
2988 | preview |
---|
2989 | """ |
---|
2990 | if self.cpanel_on_focus is not None: |
---|
2991 | self._toolbar.enable_preview(self.cpanel_on_focus) |
---|
2992 | |
---|
2993 | def enable_print(self): |
---|
2994 | """ |
---|
2995 | print |
---|
2996 | """ |
---|
2997 | if self.cpanel_on_focus is not None: |
---|
2998 | self._toolbar.enable_print(self.cpanel_on_focus) |
---|
2999 | |
---|
3000 | def enable_zoom(self): |
---|
3001 | """ |
---|
3002 | zoom |
---|
3003 | """ |
---|
3004 | if self.cpanel_on_focus is not None: |
---|
3005 | self._toolbar.enable_zoom(self.panel_on_focus) |
---|
3006 | |
---|
3007 | def enable_zoom_in(self): |
---|
3008 | """ |
---|
3009 | zoom in |
---|
3010 | """ |
---|
3011 | if self.cpanel_on_focus is not None: |
---|
3012 | self._toolbar.enable_zoom_in(self.panel_on_focus) |
---|
3013 | |
---|
3014 | def enable_zoom_out(self): |
---|
3015 | """ |
---|
3016 | zoom out |
---|
3017 | """ |
---|
3018 | if self.cpanel_on_focus is not None: |
---|
3019 | self._toolbar.enable_zoom_out(self.panel_on_focus) |
---|
3020 | |
---|
3021 | def enable_drag(self, event=None): |
---|
3022 | """ |
---|
3023 | drag |
---|
3024 | """ |
---|
3025 | #Not implemeted |
---|
3026 | |
---|
3027 | def enable_reset(self): |
---|
3028 | """ |
---|
3029 | reset the current panel |
---|
3030 | """ |
---|
3031 | if self.cpanel_on_focus is not None: |
---|
3032 | self._toolbar.enable_reset(self.panel_on_focus) |
---|
3033 | |
---|
3034 | def get_toolbar_height(self): |
---|
3035 | """ |
---|
3036 | """ |
---|
3037 | size_y = 0 |
---|
3038 | if self.GetToolBar() != None and self.GetToolBar().IsShown(): |
---|
3039 | if not IS_LINUX: |
---|
3040 | _, size_y = self.GetToolBar().GetSizeTuple() |
---|
3041 | return size_y |
---|
3042 | |
---|
3043 | def set_schedule_full_draw(self, panel=None, func='del'): |
---|
3044 | """ |
---|
3045 | Add/subtract the schedule full draw list with the panel given |
---|
3046 | |
---|
3047 | :param panel: plot panel |
---|
3048 | :param func: append or del [string] |
---|
3049 | """ |
---|
3050 | |
---|
3051 | # append this panel in the schedule list if not in yet |
---|
3052 | if func == 'append': |
---|
3053 | if not panel in self.schedule_full_draw_list: |
---|
3054 | self.schedule_full_draw_list.append(panel) |
---|
3055 | # remove this panel from schedule list |
---|
3056 | elif func == 'del': |
---|
3057 | if len(self.schedule_full_draw_list) > 0: |
---|
3058 | if panel in self.schedule_full_draw_list: |
---|
3059 | self.schedule_full_draw_list.remove(panel) |
---|
3060 | |
---|
3061 | # reset the schdule |
---|
3062 | if len(self.schedule_full_draw_list) == 0: |
---|
3063 | self.schedule = False |
---|
3064 | else: |
---|
3065 | self.schedule = True |
---|
3066 | |
---|
3067 | def full_draw(self): |
---|
3068 | """ |
---|
3069 | Draw the panels with axes in the schedule to full dwar list |
---|
3070 | """ |
---|
3071 | |
---|
3072 | count = len(self.schedule_full_draw_list) |
---|
3073 | #if not self.schedule: |
---|
3074 | if count < 1: |
---|
3075 | self.set_schedule(False) |
---|
3076 | return |
---|
3077 | |
---|
3078 | else: |
---|
3079 | ind = 0 |
---|
3080 | # if any of the panel is shown do full_draw |
---|
3081 | for panel in self.schedule_full_draw_list: |
---|
3082 | ind += 1 |
---|
3083 | if panel.frame.IsShown(): |
---|
3084 | break |
---|
3085 | # otherwise, return |
---|
3086 | if ind == count: |
---|
3087 | return |
---|
3088 | #Simple redraw only for a panel shown |
---|
3089 | def f_draw(panel): |
---|
3090 | """ |
---|
3091 | Draw A panel in the full draw list |
---|
3092 | """ |
---|
3093 | try: |
---|
3094 | # This checking of GetCapture is to stop redrawing |
---|
3095 | # while any panel is capture. |
---|
3096 | frame = panel.frame |
---|
3097 | |
---|
3098 | if not frame.GetCapture(): |
---|
3099 | # draw if possible |
---|
3100 | panel.set_resizing(False) |
---|
3101 | #panel.Show(True) |
---|
3102 | panel.draw_plot() |
---|
3103 | # Check if the panel is not shown |
---|
3104 | flag = frame.IsShown() |
---|
3105 | frame.Show(flag) |
---|
3106 | except: |
---|
3107 | pass |
---|
3108 | |
---|
3109 | # Draw all panels |
---|
3110 | if count == 1: |
---|
3111 | f_draw(self.schedule_full_draw_list[0]) |
---|
3112 | else: |
---|
3113 | map(f_draw, self.schedule_full_draw_list) |
---|
3114 | # Reset the attr |
---|
3115 | if len(self.schedule_full_draw_list) == 0: |
---|
3116 | self.set_schedule(False) |
---|
3117 | else: |
---|
3118 | self.set_schedule(True) |
---|
3119 | |
---|
3120 | def set_schedule(self, schedule=False): |
---|
3121 | """ |
---|
3122 | Set schedule |
---|
3123 | """ |
---|
3124 | self.schedule = schedule |
---|
3125 | |
---|
3126 | def get_schedule(self): |
---|
3127 | """ |
---|
3128 | Get schedule |
---|
3129 | """ |
---|
3130 | return self.schedule |
---|
3131 | |
---|
3132 | def on_set_plot_focus(self, panel): |
---|
3133 | """ |
---|
3134 | Set focus on a plot panel |
---|
3135 | """ |
---|
3136 | if panel == None: |
---|
3137 | return |
---|
3138 | #self.set_plot_unfocus() |
---|
3139 | panel.on_set_focus(None) |
---|
3140 | # set focusing panel |
---|
3141 | self.panel_on_focus = panel |
---|
3142 | self.set_panel_on_focus(None) |
---|
3143 | |
---|
3144 | def set_plot_unfocus(self): |
---|
3145 | """ |
---|
3146 | Un focus all plot panels |
---|
3147 | """ |
---|
3148 | for plot in self.plot_panels.values(): |
---|
3149 | plot.on_kill_focus(None) |
---|
3150 | |
---|
3151 | def get_window_size(self): |
---|
3152 | """ |
---|
3153 | Get window size |
---|
3154 | |
---|
3155 | :return size: tuple |
---|
3156 | """ |
---|
3157 | width, height = self.GetSizeTuple() |
---|
3158 | if not IS_WIN: |
---|
3159 | # Subtract toolbar height to get real window side |
---|
3160 | if self._toolbar.IsShown(): |
---|
3161 | height -= 45 |
---|
3162 | return (width, height) |
---|
3163 | |
---|
3164 | def _onDrawIdle(self, *args, **kwargs): |
---|
3165 | """ |
---|
3166 | ReDraw with axes |
---|
3167 | """ |
---|
3168 | try: |
---|
3169 | # check if it is time to redraw |
---|
3170 | if self.GetCapture() == None: |
---|
3171 | # Draw plot, changes resizing too |
---|
3172 | self.full_draw() |
---|
3173 | except: |
---|
3174 | pass |
---|
3175 | |
---|
3176 | # restart idle |
---|
3177 | self._redraw_idle(*args, **kwargs) |
---|
3178 | |
---|
3179 | |
---|
3180 | def _redraw_idle(self, *args, **kwargs): |
---|
3181 | """ |
---|
3182 | Restart Idle |
---|
3183 | """ |
---|
3184 | # restart idle |
---|
3185 | self.idletimer.Restart(100*TIME_FACTOR, *args, **kwargs) |
---|
3186 | |
---|
3187 | |
---|
3188 | class DefaultPanel(wx.Panel, PanelBase): |
---|
3189 | """ |
---|
3190 | Defines the API for a panels to work with |
---|
3191 | the GUI manager |
---|
3192 | """ |
---|
3193 | ## Internal nickname for the window, used by the AUI manager |
---|
3194 | window_name = "default" |
---|
3195 | ## Name to appear on the window title bar |
---|
3196 | window_caption = "Welcome panel" |
---|
3197 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
3198 | CENTER_PANE = True |
---|
3199 | def __init__(self, parent, *args, **kwds): |
---|
3200 | wx.Panel.__init__(self, parent, *args, **kwds) |
---|
3201 | PanelBase.__init__(self, parent) |
---|
3202 | |
---|
3203 | |
---|
3204 | |
---|
3205 | class ViewApp(wx.App): |
---|
3206 | """ |
---|
3207 | Toy application to test this Frame |
---|
3208 | """ |
---|
3209 | def OnInit(self): |
---|
3210 | """ |
---|
3211 | When initialised |
---|
3212 | """ |
---|
3213 | pos, size, self.is_max = self.window_placement((GUIFRAME_WIDTH, |
---|
3214 | GUIFRAME_HEIGHT)) |
---|
3215 | self.frame = ViewerFrame(parent=None, |
---|
3216 | title=APPLICATION_NAME, |
---|
3217 | pos=pos, |
---|
3218 | gui_style = DEFAULT_STYLE, |
---|
3219 | size=size) |
---|
3220 | self.frame.Hide() |
---|
3221 | if not IS_WIN: |
---|
3222 | self.frame.EnableCloseButton(False) |
---|
3223 | self.s_screen = None |
---|
3224 | |
---|
3225 | try: |
---|
3226 | self.open_file() |
---|
3227 | except: |
---|
3228 | msg = "%s Could not load " % str(APPLICATION_NAME) |
---|
3229 | msg += "input file from command line.\n" |
---|
3230 | logging.error(msg) |
---|
3231 | # Display a splash screen on top of the frame. |
---|
3232 | try: |
---|
3233 | if os.path.isfile(SPLASH_SCREEN_PATH): |
---|
3234 | self.s_screen = self.display_splash_screen(parent=self.frame, |
---|
3235 | path=SPLASH_SCREEN_PATH) |
---|
3236 | else: |
---|
3237 | self.frame.Show() |
---|
3238 | except: |
---|
3239 | if self.s_screen is not None: |
---|
3240 | self.s_screen.Close() |
---|
3241 | msg = "Cannot display splash screen\n" |
---|
3242 | msg += str (sys.exc_value) |
---|
3243 | logging.error(msg) |
---|
3244 | self.frame.Show() |
---|
3245 | |
---|
3246 | self.SetTopWindow(self.frame) |
---|
3247 | |
---|
3248 | return True |
---|
3249 | |
---|
3250 | def maximize_win(self): |
---|
3251 | """ |
---|
3252 | Maximize the window after the frame shown |
---|
3253 | """ |
---|
3254 | if self.is_max: |
---|
3255 | if self.frame.IsShown(): |
---|
3256 | # Max window size |
---|
3257 | self.frame.Maximize(self.is_max) |
---|
3258 | |
---|
3259 | def open_file(self): |
---|
3260 | """ |
---|
3261 | open a state file at the start of the application |
---|
3262 | """ |
---|
3263 | input_file = None |
---|
3264 | if len(sys.argv) >= 2: |
---|
3265 | cmd = sys.argv[0].lower() |
---|
3266 | basename = os.path.basename(cmd) |
---|
3267 | app_base = str(APPLICATION_NAME).lower() |
---|
3268 | if os.path.isfile(cmd) or basename.lower() == app_base: |
---|
3269 | app_py = app_base + '.py' |
---|
3270 | app_exe = app_base + '.exe' |
---|
3271 | app_app = app_base + '.app' |
---|
3272 | if basename.lower() in [app_py, app_exe, app_app, app_base]: |
---|
3273 | data_base = sys.argv[1] |
---|
3274 | input_file = os.path.normpath(os.path.join(DATAPATH, |
---|
3275 | data_base)) |
---|
3276 | if input_file is None: |
---|
3277 | return |
---|
3278 | if self.frame is not None: |
---|
3279 | self.frame.set_input_file(input_file=input_file) |
---|
3280 | |
---|
3281 | def clean_plugin_models(self, path): |
---|
3282 | """ |
---|
3283 | Delete plugin models in app folder |
---|
3284 | |
---|
3285 | :param path: path of the plugin_models folder in app |
---|
3286 | """ |
---|
3287 | # do it only the first time app loaded |
---|
3288 | # delete unused model folder |
---|
3289 | model_folder = os.path.join(PATH_APP, path) |
---|
3290 | if os.path.exists(model_folder) and os.path.isdir(model_folder): |
---|
3291 | if len(os.listdir(model_folder)) > 0: |
---|
3292 | try: |
---|
3293 | for file in os.listdir(model_folder): |
---|
3294 | file_path = os.path.join(model_folder, file) |
---|
3295 | if os.path.isfile(file_path): |
---|
3296 | os.remove(file_path) |
---|
3297 | except: |
---|
3298 | logging.error("gui_manager.clean_plugin_models:\n %s" \ |
---|
3299 | % sys.exc_value) |
---|
3300 | |
---|
3301 | def set_manager(self, manager): |
---|
3302 | """ |
---|
3303 | Sets a reference to the application manager |
---|
3304 | of the GUI manager (Frame) |
---|
3305 | """ |
---|
3306 | self.frame.set_manager(manager) |
---|
3307 | |
---|
3308 | def build_gui(self): |
---|
3309 | """ |
---|
3310 | Build the GUI |
---|
3311 | """ |
---|
3312 | #try to load file at the start |
---|
3313 | try: |
---|
3314 | self.open_file() |
---|
3315 | except: |
---|
3316 | raise |
---|
3317 | self.frame.build_gui() |
---|
3318 | |
---|
3319 | def set_welcome_panel(self, panel_class): |
---|
3320 | """ |
---|
3321 | Set the welcome panel |
---|
3322 | |
---|
3323 | :param panel_class: class of the welcome panel to be instantiated |
---|
3324 | |
---|
3325 | """ |
---|
3326 | self.frame.welcome_panel_class = panel_class |
---|
3327 | |
---|
3328 | def add_perspective(self, perspective): |
---|
3329 | """ |
---|
3330 | Manually add a perspective to the application GUI |
---|
3331 | """ |
---|
3332 | self.frame.add_perspective(perspective) |
---|
3333 | |
---|
3334 | def window_placement(self, size): |
---|
3335 | """ |
---|
3336 | Determines the position and size of the application frame such that it |
---|
3337 | fits on the user's screen without obstructing (or being obstructed by) |
---|
3338 | the Windows task bar. The maximum initial size in pixels is bounded by |
---|
3339 | WIDTH x HEIGHT. For most monitors, the application |
---|
3340 | will be centered on the screen; for very large monitors it will be |
---|
3341 | placed on the left side of the screen. |
---|
3342 | """ |
---|
3343 | is_maximized = False |
---|
3344 | # Get size of screen without |
---|
3345 | for screenCount in range(wx.Display().GetCount()): |
---|
3346 | screen = wx.Display(screenCount) |
---|
3347 | if screen.IsPrimary(): |
---|
3348 | displayRect = screen.GetClientArea() |
---|
3349 | break |
---|
3350 | |
---|
3351 | posX, posY, displayWidth, displayHeight = displayRect |
---|
3352 | customWidth, customHeight = size |
---|
3353 | |
---|
3354 | # If the custom size is default, set 90% of the screen size |
---|
3355 | if customWidth <= 0 and customHeight <= 0: |
---|
3356 | if customWidth == 0 and customHeight == 0: |
---|
3357 | is_maximized = True |
---|
3358 | customWidth = displayWidth * 0.9 |
---|
3359 | customHeight = displayHeight * 0.9 |
---|
3360 | else: |
---|
3361 | # If the custom screen is bigger than the |
---|
3362 | # window screen than make maximum size |
---|
3363 | if customWidth > displayWidth: |
---|
3364 | customWidth = displayWidth |
---|
3365 | if customHeight > displayHeight: |
---|
3366 | customHeight = displayHeight |
---|
3367 | |
---|
3368 | # Note that when running Linux and using an Xming (X11) server on a PC |
---|
3369 | # with a dual monitor configuration, the reported display size may be |
---|
3370 | # that of both monitors combined with an incorrect display count of 1. |
---|
3371 | # To avoid displaying this app across both monitors, we check for |
---|
3372 | # screen 'too big'. If so, we assume a smaller width which means the |
---|
3373 | # application will be placed towards the left hand side of the screen. |
---|
3374 | |
---|
3375 | # If dual screen registered as 1 screen. Make width half. |
---|
3376 | # MAC just follows the default behavior of pos |
---|
3377 | if IS_WIN: |
---|
3378 | if displayWidth > (displayHeight*2): |
---|
3379 | if (customWidth == displayWidth): |
---|
3380 | customWidth = displayWidth/2 |
---|
3381 | # and set the position to be the corner of the screen. |
---|
3382 | posX = 0 |
---|
3383 | posY = 0 |
---|
3384 | |
---|
3385 | # Make the position the middle of the screen. (Not 0,0) |
---|
3386 | else: |
---|
3387 | posX = (displayWidth - customWidth)/2 |
---|
3388 | posY = (displayHeight - customHeight)/2 |
---|
3389 | # Return the suggested position and size for the application frame. |
---|
3390 | return (posX, posY), (customWidth, customHeight), is_maximized |
---|
3391 | |
---|
3392 | |
---|
3393 | def display_splash_screen(self, parent, |
---|
3394 | path=SPLASH_SCREEN_PATH): |
---|
3395 | """Displays the splash screen. It will exactly cover the main frame.""" |
---|
3396 | |
---|
3397 | # Prepare the picture. On a 2GHz intel cpu, this takes about a second. |
---|
3398 | image = wx.Image(path, wx.BITMAP_TYPE_PNG) |
---|
3399 | image.Rescale(SPLASH_SCREEN_WIDTH, |
---|
3400 | SPLASH_SCREEN_HEIGHT, wx.IMAGE_QUALITY_HIGH) |
---|
3401 | bm = image.ConvertToBitmap() |
---|
3402 | |
---|
3403 | # Create and show the splash screen. It will disappear only when the |
---|
3404 | # program has entered the event loop AND either the timeout has expired |
---|
3405 | # or the user has left clicked on the screen. Thus any processing |
---|
3406 | # performed in this routine (including sleeping) or processing in the |
---|
3407 | # calling routine (including doing imports) will prevent the splash |
---|
3408 | # screen from disappearing. |
---|
3409 | # |
---|
3410 | # Note that on Linux, the timeout appears to occur immediately in which |
---|
3411 | # case the splash screen disappears upon entering the event loop. |
---|
3412 | s_screen = wx.SplashScreen(bitmap=bm, |
---|
3413 | splashStyle=(wx.SPLASH_TIMEOUT| |
---|
3414 | wx.SPLASH_CENTRE_ON_SCREEN), |
---|
3415 | style=(wx.SIMPLE_BORDER| |
---|
3416 | wx.FRAME_NO_TASKBAR| |
---|
3417 | wx.FRAME_FLOAT_ON_PARENT), |
---|
3418 | milliseconds=SS_MAX_DISPLAY_TIME, |
---|
3419 | parent=parent, |
---|
3420 | id=wx.ID_ANY) |
---|
3421 | from sas.guiframe.gui_statusbar import SPageStatusbar |
---|
3422 | statusBar = SPageStatusbar(s_screen) |
---|
3423 | s_screen.SetStatusBar(statusBar) |
---|
3424 | s_screen.Bind(wx.EVT_CLOSE, self.on_close_splash_screen) |
---|
3425 | s_screen.Show() |
---|
3426 | return s_screen |
---|
3427 | |
---|
3428 | |
---|
3429 | def on_close_splash_screen(self, event): |
---|
3430 | """ |
---|
3431 | When the splash screen is closed. |
---|
3432 | """ |
---|
3433 | self.frame.Show(True) |
---|
3434 | event.Skip() |
---|
3435 | self.maximize_win() |
---|
3436 | |
---|
3437 | |
---|
3438 | class MDIFrame(CHILD_FRAME): |
---|
3439 | """ |
---|
3440 | Frame for panels |
---|
3441 | """ |
---|
3442 | def __init__(self, parent, panel, title="Untitled", size=(300,200)): |
---|
3443 | """ |
---|
3444 | comment |
---|
3445 | :param parent: parent panel/container |
---|
3446 | """ |
---|
3447 | # Initialize the Frame object |
---|
3448 | CHILD_FRAME.__init__(self, parent=parent, id=wx.ID_ANY, title=title, size=size) |
---|
3449 | self.parent = parent |
---|
3450 | self.name = "Untitled" |
---|
3451 | self.batch_on = self.parent.batch_on |
---|
3452 | self.panel = panel |
---|
3453 | if panel != None: |
---|
3454 | self.set_panel(panel) |
---|
3455 | self.Show(False) |
---|
3456 | |
---|
3457 | def show_data_panel(self, action): |
---|
3458 | """ |
---|
3459 | """ |
---|
3460 | self.parent.show_data_panel(action) |
---|
3461 | |
---|
3462 | def set_panel(self, panel): |
---|
3463 | """ |
---|
3464 | """ |
---|
3465 | self.panel = panel |
---|
3466 | self.name = panel.window_name |
---|
3467 | self.SetTitle(panel.window_caption) |
---|
3468 | self.SetHelpText(panel.help_string) |
---|
3469 | width, height = self.parent._get_panels_size(panel) |
---|
3470 | if hasattr(panel, "CENTER_PANE") and panel.CENTER_PANE: |
---|
3471 | width *= 0.6 |
---|
3472 | self.SetSize((width, height)) |
---|
3473 | self.parent.put_icon(self) |
---|
3474 | self.Bind(wx.EVT_SET_FOCUS, self.set_panel_focus) |
---|
3475 | self.Bind(wx.EVT_CLOSE, self.OnClose) |
---|
3476 | self.Show(False) |
---|
3477 | |
---|
3478 | def set_panel_focus(self, event): |
---|
3479 | """ |
---|
3480 | """ |
---|
3481 | if self.parent.panel_on_focus != self.panel: |
---|
3482 | self.panel.SetFocus() |
---|
3483 | self.parent.panel_on_focus = self.panel |
---|
3484 | |
---|
3485 | def OnClose(self, event): |
---|
3486 | """ |
---|
3487 | On Close event |
---|
3488 | """ |
---|
3489 | self.panel.on_close(event) |
---|
3490 | |
---|
3491 | if __name__ == "__main__": |
---|
3492 | app = ViewApp(0) |
---|
3493 | app.MainLoop() |
---|