1 | """ |
---|
2 | Global defaults and various utility functions usable by the general GUI |
---|
3 | """ |
---|
4 | |
---|
5 | import os |
---|
6 | import sys |
---|
7 | import imp |
---|
8 | import warnings |
---|
9 | import webbrowser |
---|
10 | import urlparse |
---|
11 | |
---|
12 | warnings.simplefilter("ignore") |
---|
13 | import logging |
---|
14 | |
---|
15 | from PyQt4 import QtCore |
---|
16 | from PyQt4 import QtGui |
---|
17 | |
---|
18 | # Translate event handlers |
---|
19 | #from sas.sasgui.guiframe.events import EVT_CATEGORY |
---|
20 | #from sas.sasgui.guiframe.events import EVT_STATUS |
---|
21 | #from sas.sasgui.guiframe.events import EVT_APPEND_BOOKMARK |
---|
22 | #from sas.sasgui.guiframe.events import EVT_PANEL_ON_FOCUS |
---|
23 | #from sas.sasgui.guiframe.events import EVT_NEW_LOAD_DATA |
---|
24 | #from sas.sasgui.guiframe.events import EVT_NEW_COLOR |
---|
25 | #from sas.sasgui.guiframe.events import StatusEvent |
---|
26 | #from sas.sasgui.guiframe.events import NewPlotEvent |
---|
27 | |
---|
28 | from periodictable import formula as Formula |
---|
29 | |
---|
30 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
31 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
32 | from sas.sascalc.dataloader.loader import Loader |
---|
33 | |
---|
34 | |
---|
35 | def get_app_dir(): |
---|
36 | """ |
---|
37 | The application directory is the one where the default custom_config.py |
---|
38 | file resides. |
---|
39 | |
---|
40 | :returns: app_path - the path to the applicatin directory |
---|
41 | """ |
---|
42 | # First, try the directory of the executable we are running |
---|
43 | app_path = sys.path[0] |
---|
44 | if os.path.isfile(app_path): |
---|
45 | app_path = os.path.dirname(app_path) |
---|
46 | if os.path.isfile(os.path.join(app_path, "custom_config.py")): |
---|
47 | app_path = os.path.abspath(app_path) |
---|
48 | #logging.info("Using application path: %s", app_path) |
---|
49 | return app_path |
---|
50 | |
---|
51 | # Next, try the current working directory |
---|
52 | if os.path.isfile(os.path.join(os.getcwd(), "custom_config.py")): |
---|
53 | #logging.info("Using application path: %s", os.getcwd()) |
---|
54 | return os.path.abspath(os.getcwd()) |
---|
55 | |
---|
56 | # Finally, try the directory of the sasview module |
---|
57 | # TODO: gui_manager will have to know about sasview until we |
---|
58 | # clean all these module variables and put them into a config class |
---|
59 | # that can be passed by sasview.py. |
---|
60 | #logging.info(sys.executable) |
---|
61 | #logging.info(str(sys.argv)) |
---|
62 | from sas import sasview as sasview |
---|
63 | app_path = os.path.dirname(sasview.__file__) |
---|
64 | #logging.info("Using application path: %s", app_path) |
---|
65 | return app_path |
---|
66 | |
---|
67 | def get_user_directory(): |
---|
68 | """ |
---|
69 | Returns the user's home directory |
---|
70 | """ |
---|
71 | userdir = os.path.join(os.path.expanduser("~"), ".sasview") |
---|
72 | if not os.path.isdir(userdir): |
---|
73 | os.makedirs(userdir) |
---|
74 | return userdir |
---|
75 | |
---|
76 | def _find_local_config(confg_file, path): |
---|
77 | """ |
---|
78 | Find configuration file for the current application |
---|
79 | """ |
---|
80 | config_module = None |
---|
81 | fObj = None |
---|
82 | try: |
---|
83 | fObj, path_config, descr = imp.find_module(confg_file, [path]) |
---|
84 | config_module = imp.load_module(confg_file, fObj, path_config, descr) |
---|
85 | except ImportError: |
---|
86 | pass |
---|
87 | #logging.error("Error loading %s/%s: %s" % (path, confg_file, sys.exc_value)) |
---|
88 | finally: |
---|
89 | if fObj is not None: |
---|
90 | fObj.close() |
---|
91 | #logging.info("GuiManager loaded %s/%s" % (path, confg_file)) |
---|
92 | return config_module |
---|
93 | |
---|
94 | # Get APP folder |
---|
95 | PATH_APP = get_app_dir() |
---|
96 | DATAPATH = PATH_APP |
---|
97 | |
---|
98 | # GUI always starts from the App folder |
---|
99 | #os.chdir(PATH_APP) |
---|
100 | # Read in the local config, which can either be with the main |
---|
101 | # application or in the installation directory |
---|
102 | config = _find_local_config('local_config', PATH_APP) |
---|
103 | |
---|
104 | if config is None: |
---|
105 | config = _find_local_config('local_config', os.getcwd()) |
---|
106 | if config is None: |
---|
107 | # Didn't find local config, load the default |
---|
108 | import sas.sasgui.guiframe.config as config |
---|
109 | #logging.info("using default local_config") |
---|
110 | else: |
---|
111 | pass |
---|
112 | #logging.info("found local_config in %s", os.getcwd()) |
---|
113 | else: |
---|
114 | pass |
---|
115 | #logging.info("found local_config in %s", PATH_APP) |
---|
116 | |
---|
117 | |
---|
118 | from sas.sasgui.guiframe.customdir import SetupCustom |
---|
119 | c_conf_dir = SetupCustom().setup_dir(PATH_APP) |
---|
120 | custom_config = _find_local_config('custom_config', c_conf_dir) |
---|
121 | if custom_config is None: |
---|
122 | custom_config = _find_local_config('custom_config', os.getcwd()) |
---|
123 | if custom_config is None: |
---|
124 | msgConfig = "Custom_config file was not imported" |
---|
125 | #logging.info(msgConfig) |
---|
126 | else: |
---|
127 | pass |
---|
128 | #logging.info("using custom_config in %s", os.getcwd()) |
---|
129 | else: |
---|
130 | pass |
---|
131 | #logging.info("using custom_config from %s", c_conf_dir) |
---|
132 | |
---|
133 | #read some constants from config |
---|
134 | APPLICATION_STATE_EXTENSION = config.APPLICATION_STATE_EXTENSION |
---|
135 | APPLICATION_NAME = config.__appname__ |
---|
136 | SPLASH_SCREEN_PATH = config.SPLASH_SCREEN_PATH |
---|
137 | WELCOME_PANEL_ON = config.WELCOME_PANEL_ON |
---|
138 | SPLASH_SCREEN_WIDTH = config.SPLASH_SCREEN_WIDTH |
---|
139 | SPLASH_SCREEN_HEIGHT = config.SPLASH_SCREEN_HEIGHT |
---|
140 | SS_MAX_DISPLAY_TIME = config.SS_MAX_DISPLAY_TIME |
---|
141 | if not WELCOME_PANEL_ON: |
---|
142 | WELCOME_PANEL_SHOW = False |
---|
143 | else: |
---|
144 | WELCOME_PANEL_SHOW = True |
---|
145 | try: |
---|
146 | DATALOADER_SHOW = custom_config.DATALOADER_SHOW |
---|
147 | TOOLBAR_SHOW = custom_config.TOOLBAR_SHOW |
---|
148 | FIXED_PANEL = custom_config.FIXED_PANEL |
---|
149 | if WELCOME_PANEL_ON: |
---|
150 | WELCOME_PANEL_SHOW = custom_config.WELCOME_PANEL_SHOW |
---|
151 | PLOPANEL_WIDTH = custom_config.PLOPANEL_WIDTH |
---|
152 | DATAPANEL_WIDTH = custom_config.DATAPANEL_WIDTH |
---|
153 | GUIFRAME_WIDTH = custom_config.GUIFRAME_WIDTH |
---|
154 | GUIFRAME_HEIGHT = custom_config.GUIFRAME_HEIGHT |
---|
155 | CONTROL_WIDTH = custom_config.CONTROL_WIDTH |
---|
156 | CONTROL_HEIGHT = custom_config.CONTROL_HEIGHT |
---|
157 | DEFAULT_PERSPECTIVE = custom_config.DEFAULT_PERSPECTIVE |
---|
158 | CLEANUP_PLOT = custom_config.CLEANUP_PLOT |
---|
159 | # custom open_path |
---|
160 | open_folder = custom_config.DEFAULT_OPEN_FOLDER |
---|
161 | if open_folder != None and os.path.isdir(open_folder): |
---|
162 | DEFAULT_OPEN_FOLDER = os.path.abspath(open_folder) |
---|
163 | else: |
---|
164 | DEFAULT_OPEN_FOLDER = PATH_APP |
---|
165 | except AttributeError: |
---|
166 | DATALOADER_SHOW = True |
---|
167 | TOOLBAR_SHOW = True |
---|
168 | FIXED_PANEL = True |
---|
169 | WELCOME_PANEL_SHOW = False |
---|
170 | PLOPANEL_WIDTH = config.PLOPANEL_WIDTH |
---|
171 | DATAPANEL_WIDTH = config.DATAPANEL_WIDTH |
---|
172 | GUIFRAME_WIDTH = config.GUIFRAME_WIDTH |
---|
173 | GUIFRAME_HEIGHT = config.GUIFRAME_HEIGHT |
---|
174 | CONTROL_WIDTH = -1 |
---|
175 | CONTROL_HEIGHT = -1 |
---|
176 | DEFAULT_PERSPECTIVE = None |
---|
177 | CLEANUP_PLOT = False |
---|
178 | DEFAULT_OPEN_FOLDER = PATH_APP |
---|
179 | |
---|
180 | DEFAULT_STYLE = config.DEFAULT_STYLE |
---|
181 | |
---|
182 | PLUGIN_STATE_EXTENSIONS = config.PLUGIN_STATE_EXTENSIONS |
---|
183 | OPEN_SAVE_MENU = config.OPEN_SAVE_PROJECT_MENU |
---|
184 | VIEW_MENU = config.VIEW_MENU |
---|
185 | EDIT_MENU = config.EDIT_MENU |
---|
186 | extension_list = [] |
---|
187 | if APPLICATION_STATE_EXTENSION is not None: |
---|
188 | extension_list.append(APPLICATION_STATE_EXTENSION) |
---|
189 | EXTENSIONS = PLUGIN_STATE_EXTENSIONS + extension_list |
---|
190 | try: |
---|
191 | PLUGINS_WLIST = '|'.join(config.PLUGINS_WLIST) |
---|
192 | except AttributeError: |
---|
193 | PLUGINS_WLIST = '' |
---|
194 | APPLICATION_WLIST = config.APPLICATION_WLIST |
---|
195 | IS_WIN = True |
---|
196 | IS_LINUX = False |
---|
197 | CLOSE_SHOW = True |
---|
198 | TIME_FACTOR = 2 |
---|
199 | NOT_SO_GRAPH_LIST = ["BoxSum"] |
---|
200 | |
---|
201 | class Communicate(QtCore.QObject): |
---|
202 | """ |
---|
203 | Utility class for tracking of the Qt signals |
---|
204 | """ |
---|
205 | # File got successfully read |
---|
206 | fileReadSignal = QtCore.pyqtSignal(list) |
---|
207 | |
---|
208 | # Open File returns "list" of paths |
---|
209 | fileDataReceivedSignal = QtCore.pyqtSignal(dict) |
---|
210 | |
---|
211 | # Update Main window status bar with "str" |
---|
212 | # Old "StatusEvent" |
---|
213 | statusBarUpdateSignal = QtCore.pyqtSignal(str) |
---|
214 | |
---|
215 | # Send data to the current perspective |
---|
216 | updatePerspectiveWithDataSignal = QtCore.pyqtSignal(list) |
---|
217 | |
---|
218 | # New data in current perspective |
---|
219 | updateModelFromPerspectiveSignal = QtCore.pyqtSignal(QtGui.QStandardItem) |
---|
220 | |
---|
221 | # New plot requested from the GUI manager |
---|
222 | # Old "NewPlotEvent" |
---|
223 | plotRequestedSignal = QtCore.pyqtSignal(str) |
---|
224 | |
---|
225 | # Progress bar update value |
---|
226 | progressBarUpdateSignal = QtCore.pyqtSignal(int) |
---|
227 | |
---|
228 | # Workspace charts added/removed |
---|
229 | activeGraphsSignal = QtCore.pyqtSignal(list) |
---|
230 | |
---|
231 | |
---|
232 | def updateModelItemWithPlot(item, update_data, name=""): |
---|
233 | """ |
---|
234 | Adds a checkboxed row named "name" to QStandardItem |
---|
235 | Adds QVariant 'update_data' to that row. |
---|
236 | """ |
---|
237 | assert isinstance(item, QtGui.QStandardItem) |
---|
238 | assert isinstance(update_data, QtCore.QVariant) |
---|
239 | |
---|
240 | checkbox_item = QtGui.QStandardItem(True) |
---|
241 | checkbox_item.setCheckable(True) |
---|
242 | checkbox_item.setCheckState(QtCore.Qt.Checked) |
---|
243 | checkbox_item.setText(name) |
---|
244 | |
---|
245 | # Add "Info" item |
---|
246 | py_update_data = update_data.toPyObject() |
---|
247 | if isinstance(py_update_data, (Data1D or Data2D)): |
---|
248 | # If Data1/2D added - extract Info from it |
---|
249 | info_item = infoFromData(py_update_data) |
---|
250 | else: |
---|
251 | # otherwise just add a naked item |
---|
252 | info_item = QtGui.QStandardItem("Info") |
---|
253 | |
---|
254 | # Add the actual Data1D/Data2D object |
---|
255 | object_item = QtGui.QStandardItem() |
---|
256 | object_item.setData(update_data) |
---|
257 | |
---|
258 | # Set the data object as the first child |
---|
259 | checkbox_item.setChild(0, object_item) |
---|
260 | |
---|
261 | # Set info_item as the second child |
---|
262 | checkbox_item.setChild(1, info_item) |
---|
263 | |
---|
264 | # Append the new row to the main item |
---|
265 | item.appendRow(checkbox_item) |
---|
266 | |
---|
267 | def updateModelItem(item, update_data, name=""): |
---|
268 | """ |
---|
269 | Adds a simple named child to QStandardItem |
---|
270 | """ |
---|
271 | assert isinstance(item, QtGui.QStandardItem) |
---|
272 | assert isinstance(update_data, list) |
---|
273 | |
---|
274 | # Add the actual Data1D/Data2D object |
---|
275 | object_item = QtGui.QStandardItem() |
---|
276 | object_item.setText(name) |
---|
277 | object_item.setData(QtCore.QVariant(update_data)) |
---|
278 | |
---|
279 | # Append the new row to the main item |
---|
280 | item.appendRow(object_item) |
---|
281 | |
---|
282 | |
---|
283 | def plotsFromCheckedItems(model_item): |
---|
284 | """ |
---|
285 | Returns the list of plots for items in the model which are checked |
---|
286 | """ |
---|
287 | assert isinstance(model_item, QtGui.QStandardItemModel) |
---|
288 | |
---|
289 | plot_data = [] |
---|
290 | # Iterate over model looking for items with checkboxes |
---|
291 | for index in range(model_item.rowCount()): |
---|
292 | item = model_item.item(index) |
---|
293 | if item.isCheckable() and item.checkState() == QtCore.Qt.Checked: |
---|
294 | # TODO: assure item type is correct (either data1/2D or Plotter) |
---|
295 | plot_data.append(item.child(0).data().toPyObject()) |
---|
296 | # Going 1 level deeper only |
---|
297 | for index_2 in range(item.rowCount()): |
---|
298 | item_2 = item.child(index_2) |
---|
299 | if item_2 and item_2.isCheckable() and item_2.checkState() == QtCore.Qt.Checked: |
---|
300 | # TODO: assure item type is correct (either data1/2D or Plotter) |
---|
301 | plot_data.append(item_2.child(0).data().toPyObject()) |
---|
302 | |
---|
303 | return plot_data |
---|
304 | |
---|
305 | def infoFromData(data): |
---|
306 | """ |
---|
307 | Given Data1D/Data2D object, extract relevant Info elements |
---|
308 | and add them to a model item |
---|
309 | """ |
---|
310 | assert isinstance(data, (Data1D, Data2D)) |
---|
311 | |
---|
312 | info_item = QtGui.QStandardItem("Info") |
---|
313 | |
---|
314 | title_item = QtGui.QStandardItem("Title: " + data.title) |
---|
315 | info_item.appendRow(title_item) |
---|
316 | run_item = QtGui.QStandardItem("Run: " + str(data.run)) |
---|
317 | info_item.appendRow(run_item) |
---|
318 | type_item = QtGui.QStandardItem("Type: " + str(data.__class__.__name__)) |
---|
319 | info_item.appendRow(type_item) |
---|
320 | |
---|
321 | if data.path: |
---|
322 | path_item = QtGui.QStandardItem("Path: " + data.path) |
---|
323 | info_item.appendRow(path_item) |
---|
324 | |
---|
325 | if data.instrument: |
---|
326 | instr_item = QtGui.QStandardItem("Instrument: " + data.instrument) |
---|
327 | info_item.appendRow(instr_item) |
---|
328 | |
---|
329 | process_item = QtGui.QStandardItem("Process") |
---|
330 | if isinstance(data.process, list) and data.process: |
---|
331 | for process in data.process: |
---|
332 | process_date = process.date |
---|
333 | process_date_item = QtGui.QStandardItem("Date: " + process_date) |
---|
334 | process_item.appendRow(process_date_item) |
---|
335 | |
---|
336 | process_descr = process.description |
---|
337 | process_descr_item = QtGui.QStandardItem("Description: " + process_descr) |
---|
338 | process_item.appendRow(process_descr_item) |
---|
339 | |
---|
340 | process_name = process.name |
---|
341 | process_name_item = QtGui.QStandardItem("Name: " + process_name) |
---|
342 | process_item.appendRow(process_name_item) |
---|
343 | |
---|
344 | info_item.appendRow(process_item) |
---|
345 | |
---|
346 | return info_item |
---|
347 | |
---|
348 | def openLink(url): |
---|
349 | """ |
---|
350 | Open a URL in an external browser. |
---|
351 | Check the URL first, though. |
---|
352 | """ |
---|
353 | parsed_url = urlparse.urlparse(url) |
---|
354 | if parsed_url.scheme: |
---|
355 | webbrowser.open(url) |
---|
356 | else: |
---|
357 | msg = "Attempt at opening an invalid URL" |
---|
358 | raise AttributeError, msg |
---|
359 | |
---|
360 | def retrieveData1d(data): |
---|
361 | """ |
---|
362 | Retrieve 1D data from file and construct its text |
---|
363 | representation |
---|
364 | """ |
---|
365 | if not isinstance(data, Data1D): |
---|
366 | msg = "Incorrect type passed to retrieveData1d" |
---|
367 | raise AttributeError, msg |
---|
368 | try: |
---|
369 | xmin = min(data.x) |
---|
370 | ymin = min(data.y) |
---|
371 | except: |
---|
372 | msg = "Unable to find min/max of \n data named %s" % \ |
---|
373 | data.filename |
---|
374 | #logging.error(msg) |
---|
375 | raise ValueError, msg |
---|
376 | |
---|
377 | text = data.__str__() |
---|
378 | text += 'Data Min Max:\n' |
---|
379 | text += 'X_min = %s: X_max = %s\n' % (xmin, max(data.x)) |
---|
380 | text += 'Y_min = %s: Y_max = %s\n' % (ymin, max(data.y)) |
---|
381 | if data.dy != None: |
---|
382 | text += 'dY_min = %s: dY_max = %s\n' % (min(data.dy), max(data.dy)) |
---|
383 | text += '\nData Points:\n' |
---|
384 | x_st = "X" |
---|
385 | for index in range(len(data.x)): |
---|
386 | if data.dy != None and len(data.dy) > index: |
---|
387 | dy_val = data.dy[index] |
---|
388 | else: |
---|
389 | dy_val = 0.0 |
---|
390 | if data.dx != None and len(data.dx) > index: |
---|
391 | dx_val = data.dx[index] |
---|
392 | else: |
---|
393 | dx_val = 0.0 |
---|
394 | if data.dxl != None and len(data.dxl) > index: |
---|
395 | if index == 0: |
---|
396 | x_st = "Xl" |
---|
397 | dx_val = data.dxl[index] |
---|
398 | elif data.dxw != None and len(data.dxw) > index: |
---|
399 | if index == 0: |
---|
400 | x_st = "Xw" |
---|
401 | dx_val = data.dxw[index] |
---|
402 | |
---|
403 | if index == 0: |
---|
404 | text += "<index> \t<X> \t<Y> \t<dY> \t<d%s>\n" % x_st |
---|
405 | text += "%s \t%s \t%s \t%s \t%s\n" % (index, |
---|
406 | data.x[index], |
---|
407 | data.y[index], |
---|
408 | dy_val, |
---|
409 | dx_val) |
---|
410 | return text |
---|
411 | |
---|
412 | def retrieveData2d(data): |
---|
413 | """ |
---|
414 | Retrieve 2D data from file and construct its text |
---|
415 | representation |
---|
416 | """ |
---|
417 | if not isinstance(data, Data2D): |
---|
418 | msg = "Incorrect type passed to retrieveData2d" |
---|
419 | raise AttributeError, msg |
---|
420 | |
---|
421 | text = data.__str__() |
---|
422 | text += 'Data Min Max:\n' |
---|
423 | text += 'I_min = %s\n' % min(data.data) |
---|
424 | text += 'I_max = %s\n\n' % max(data.data) |
---|
425 | text += 'Data (First 2501) Points:\n' |
---|
426 | text += 'Data columns include err(I).\n' |
---|
427 | text += 'ASCII data starts here.\n' |
---|
428 | text += "<index> \t<Qx> \t<Qy> \t<I> \t<dI> \t<dQparal> \t<dQperp>\n" |
---|
429 | di_val = 0.0 |
---|
430 | dx_val = 0.0 |
---|
431 | dy_val = 0.0 |
---|
432 | len_data = len(data.qx_data) |
---|
433 | for index in xrange(0, len_data): |
---|
434 | x_val = data.qx_data[index] |
---|
435 | y_val = data.qy_data[index] |
---|
436 | i_val = data.data[index] |
---|
437 | if data.err_data != None: |
---|
438 | di_val = data.err_data[index] |
---|
439 | if data.dqx_data != None: |
---|
440 | dx_val = data.dqx_data[index] |
---|
441 | if data.dqy_data != None: |
---|
442 | dy_val = data.dqy_data[index] |
---|
443 | |
---|
444 | text += "%s \t%s \t%s \t%s \t%s \t%s \t%s\n" % (index, |
---|
445 | x_val, |
---|
446 | y_val, |
---|
447 | i_val, |
---|
448 | di_val, |
---|
449 | dx_val, |
---|
450 | dy_val) |
---|
451 | # Takes too long time for typical data2d: Break here |
---|
452 | if index >= 2500: |
---|
453 | text += ".............\n" |
---|
454 | break |
---|
455 | |
---|
456 | return text |
---|
457 | |
---|
458 | def onTXTSave(data, path): |
---|
459 | """ |
---|
460 | Save file as formatted txt |
---|
461 | """ |
---|
462 | with open(path,'w') as out: |
---|
463 | has_errors = True |
---|
464 | if data.dy == None or data.dy == []: |
---|
465 | has_errors = False |
---|
466 | # Sanity check |
---|
467 | if has_errors: |
---|
468 | try: |
---|
469 | if len(data.y) != len(data.dy): |
---|
470 | has_errors = False |
---|
471 | except: |
---|
472 | has_errors = False |
---|
473 | if has_errors: |
---|
474 | if data.dx != None and data.dx != []: |
---|
475 | out.write("<X> <Y> <dY> <dX>\n") |
---|
476 | else: |
---|
477 | out.write("<X> <Y> <dY>\n") |
---|
478 | else: |
---|
479 | out.write("<X> <Y>\n") |
---|
480 | |
---|
481 | for i in range(len(data.x)): |
---|
482 | if has_errors: |
---|
483 | if data.dx != None and data.dx != []: |
---|
484 | if data.dx[i] != None: |
---|
485 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
486 | data.y[i], |
---|
487 | data.dy[i], |
---|
488 | data.dx[i])) |
---|
489 | else: |
---|
490 | out.write("%g %g %g\n" % (data.x[i], |
---|
491 | data.y[i], |
---|
492 | data.dy[i])) |
---|
493 | else: |
---|
494 | out.write("%g %g %g\n" % (data.x[i], |
---|
495 | data.y[i], |
---|
496 | data.dy[i])) |
---|
497 | else: |
---|
498 | out.write("%g %g\n" % (data.x[i], |
---|
499 | data.y[i])) |
---|
500 | |
---|
501 | def saveData1D(data): |
---|
502 | """ |
---|
503 | Save 1D data points |
---|
504 | """ |
---|
505 | default_name = os.path.basename(data.filename) |
---|
506 | default_name, extension = os.path.splitext(default_name) |
---|
507 | default_name += "_out" + extension |
---|
508 | |
---|
509 | wildcard = "Text files (*.txt);;"\ |
---|
510 | "CanSAS 1D files(*.xml)" |
---|
511 | kwargs = { |
---|
512 | 'caption' : 'Save As', |
---|
513 | 'directory' : default_name, |
---|
514 | 'filter' : wildcard, |
---|
515 | 'parent' : None, |
---|
516 | } |
---|
517 | # Query user for filename. |
---|
518 | filename = QtGui.QFileDialog.getSaveFileName(**kwargs) |
---|
519 | |
---|
520 | # User cancelled. |
---|
521 | if not filename: |
---|
522 | return |
---|
523 | |
---|
524 | filename = str(filename) |
---|
525 | |
---|
526 | #Instantiate a loader |
---|
527 | loader = Loader() |
---|
528 | if os.path.splitext(filename)[1].lower() == ".txt": |
---|
529 | onTXTSave(data, filename) |
---|
530 | if os.path.splitext(filename)[1].lower() == ".xml": |
---|
531 | loader.save(filename, data, ".xml") |
---|
532 | |
---|
533 | def saveData2D(data): |
---|
534 | """ |
---|
535 | Save data2d dialog |
---|
536 | """ |
---|
537 | default_name = os.path.basename(data.filename) |
---|
538 | default_name, _ = os.path.splitext(default_name) |
---|
539 | ext_format = ".dat" |
---|
540 | default_name += "_out" + ext_format |
---|
541 | |
---|
542 | wildcard = "IGOR/DAT 2D file in Q_map (*.dat)" |
---|
543 | kwargs = { |
---|
544 | 'caption' : 'Save As', |
---|
545 | 'directory' : default_name, |
---|
546 | 'filter' : wildcard, |
---|
547 | 'parent' : None, |
---|
548 | } |
---|
549 | # Query user for filename. |
---|
550 | filename = QtGui.QFileDialog.getSaveFileName(**kwargs) |
---|
551 | |
---|
552 | # User cancelled. |
---|
553 | if not filename: |
---|
554 | return |
---|
555 | filename = str(filename) |
---|
556 | #Instantiate a loader |
---|
557 | loader = Loader() |
---|
558 | |
---|
559 | if os.path.splitext(filename)[1].lower() == ext_format: |
---|
560 | loader.save(filename, data, ext_format) |
---|
561 | |
---|
562 | class FormulaValidator(QtGui.QValidator): |
---|
563 | def __init__(self, parent=None): |
---|
564 | super(FormulaValidator, self).__init__(parent) |
---|
565 | |
---|
566 | def validate(self, input, pos): |
---|
567 | try: |
---|
568 | Formula(str(input)) |
---|
569 | self._setStyleSheet("") |
---|
570 | return QtGui.QValidator.Acceptable, pos |
---|
571 | |
---|
572 | except Exception as e: |
---|
573 | self._setStyleSheet("background-color:pink;") |
---|
574 | return QtGui.QValidator.Intermediate, pos |
---|
575 | |
---|
576 | def _setStyleSheet(self, value): |
---|
577 | try: |
---|
578 | if self.parent(): |
---|
579 | self.parent().setStyleSheet(value) |
---|
580 | except: |
---|
581 | pass |
---|
582 | |
---|
583 | def dataFromItem(item): |
---|
584 | """ |
---|
585 | Retrieve Data1D/2D component from QStandardItem. |
---|
586 | The assumption - data stored in SasView standard, in child 0 |
---|
587 | """ |
---|
588 | return item.child(0).data().toPyObject() |
---|