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.plottools import transform |
---|
31 | from sas.sasgui.plottools.convert_units import convert_unit |
---|
32 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
33 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
34 | from sas.sascalc.dataloader.loader import Loader |
---|
35 | |
---|
36 | |
---|
37 | def get_app_dir(): |
---|
38 | """ |
---|
39 | The application directory is the one where the default custom_config.py |
---|
40 | file resides. |
---|
41 | |
---|
42 | :returns: app_path - the path to the applicatin directory |
---|
43 | """ |
---|
44 | # First, try the directory of the executable we are running |
---|
45 | app_path = sys.path[0] |
---|
46 | if os.path.isfile(app_path): |
---|
47 | app_path = os.path.dirname(app_path) |
---|
48 | if os.path.isfile(os.path.join(app_path, "custom_config.py")): |
---|
49 | app_path = os.path.abspath(app_path) |
---|
50 | #logging.info("Using application path: %s", app_path) |
---|
51 | return app_path |
---|
52 | |
---|
53 | # Next, try the current working directory |
---|
54 | if os.path.isfile(os.path.join(os.getcwd(), "custom_config.py")): |
---|
55 | #logging.info("Using application path: %s", os.getcwd()) |
---|
56 | return os.path.abspath(os.getcwd()) |
---|
57 | |
---|
58 | # Finally, try the directory of the sasview module |
---|
59 | # TODO: gui_manager will have to know about sasview until we |
---|
60 | # clean all these module variables and put them into a config class |
---|
61 | # that can be passed by sasview.py. |
---|
62 | #logging.info(sys.executable) |
---|
63 | #logging.info(str(sys.argv)) |
---|
64 | from sas import sasview as sasview |
---|
65 | app_path = os.path.dirname(sasview.__file__) |
---|
66 | #logging.info("Using application path: %s", app_path) |
---|
67 | return app_path |
---|
68 | |
---|
69 | def get_user_directory(): |
---|
70 | """ |
---|
71 | Returns the user's home directory |
---|
72 | """ |
---|
73 | userdir = os.path.join(os.path.expanduser("~"), ".sasview") |
---|
74 | if not os.path.isdir(userdir): |
---|
75 | os.makedirs(userdir) |
---|
76 | return userdir |
---|
77 | |
---|
78 | def _find_local_config(confg_file, path): |
---|
79 | """ |
---|
80 | Find configuration file for the current application |
---|
81 | """ |
---|
82 | config_module = None |
---|
83 | fObj = None |
---|
84 | try: |
---|
85 | fObj, path_config, descr = imp.find_module(confg_file, [path]) |
---|
86 | config_module = imp.load_module(confg_file, fObj, path_config, descr) |
---|
87 | except ImportError: |
---|
88 | pass |
---|
89 | #logging.error("Error loading %s/%s: %s" % (path, confg_file, sys.exc_value)) |
---|
90 | finally: |
---|
91 | if fObj is not None: |
---|
92 | fObj.close() |
---|
93 | #logging.info("GuiManager loaded %s/%s" % (path, confg_file)) |
---|
94 | return config_module |
---|
95 | |
---|
96 | # Get APP folder |
---|
97 | PATH_APP = get_app_dir() |
---|
98 | DATAPATH = PATH_APP |
---|
99 | |
---|
100 | # GUI always starts from the App folder |
---|
101 | #os.chdir(PATH_APP) |
---|
102 | # Read in the local config, which can either be with the main |
---|
103 | # application or in the installation directory |
---|
104 | config = _find_local_config('local_config', PATH_APP) |
---|
105 | |
---|
106 | if config is None: |
---|
107 | config = _find_local_config('local_config', os.getcwd()) |
---|
108 | if config is None: |
---|
109 | # Didn't find local config, load the default |
---|
110 | import sas.sasgui.guiframe.config as config |
---|
111 | #logging.info("using default local_config") |
---|
112 | else: |
---|
113 | pass |
---|
114 | #logging.info("found local_config in %s", os.getcwd()) |
---|
115 | else: |
---|
116 | pass |
---|
117 | #logging.info("found local_config in %s", PATH_APP) |
---|
118 | |
---|
119 | |
---|
120 | from sas.sasgui.guiframe.customdir import SetupCustom |
---|
121 | c_conf_dir = SetupCustom().setup_dir(PATH_APP) |
---|
122 | custom_config = _find_local_config('custom_config', c_conf_dir) |
---|
123 | if custom_config is None: |
---|
124 | custom_config = _find_local_config('custom_config', os.getcwd()) |
---|
125 | if custom_config is None: |
---|
126 | msgConfig = "Custom_config file was not imported" |
---|
127 | #logging.info(msgConfig) |
---|
128 | else: |
---|
129 | pass |
---|
130 | #logging.info("using custom_config in %s", os.getcwd()) |
---|
131 | else: |
---|
132 | pass |
---|
133 | #logging.info("using custom_config from %s", c_conf_dir) |
---|
134 | |
---|
135 | #read some constants from config |
---|
136 | APPLICATION_STATE_EXTENSION = config.APPLICATION_STATE_EXTENSION |
---|
137 | APPLICATION_NAME = config.__appname__ |
---|
138 | SPLASH_SCREEN_PATH = config.SPLASH_SCREEN_PATH |
---|
139 | WELCOME_PANEL_ON = config.WELCOME_PANEL_ON |
---|
140 | SPLASH_SCREEN_WIDTH = config.SPLASH_SCREEN_WIDTH |
---|
141 | SPLASH_SCREEN_HEIGHT = config.SPLASH_SCREEN_HEIGHT |
---|
142 | SS_MAX_DISPLAY_TIME = config.SS_MAX_DISPLAY_TIME |
---|
143 | if not WELCOME_PANEL_ON: |
---|
144 | WELCOME_PANEL_SHOW = False |
---|
145 | else: |
---|
146 | WELCOME_PANEL_SHOW = True |
---|
147 | try: |
---|
148 | DATALOADER_SHOW = custom_config.DATALOADER_SHOW |
---|
149 | TOOLBAR_SHOW = custom_config.TOOLBAR_SHOW |
---|
150 | FIXED_PANEL = custom_config.FIXED_PANEL |
---|
151 | if WELCOME_PANEL_ON: |
---|
152 | WELCOME_PANEL_SHOW = custom_config.WELCOME_PANEL_SHOW |
---|
153 | PLOPANEL_WIDTH = custom_config.PLOPANEL_WIDTH |
---|
154 | DATAPANEL_WIDTH = custom_config.DATAPANEL_WIDTH |
---|
155 | GUIFRAME_WIDTH = custom_config.GUIFRAME_WIDTH |
---|
156 | GUIFRAME_HEIGHT = custom_config.GUIFRAME_HEIGHT |
---|
157 | CONTROL_WIDTH = custom_config.CONTROL_WIDTH |
---|
158 | CONTROL_HEIGHT = custom_config.CONTROL_HEIGHT |
---|
159 | DEFAULT_PERSPECTIVE = custom_config.DEFAULT_PERSPECTIVE |
---|
160 | CLEANUP_PLOT = custom_config.CLEANUP_PLOT |
---|
161 | # custom open_path |
---|
162 | open_folder = custom_config.DEFAULT_OPEN_FOLDER |
---|
163 | if open_folder != None and os.path.isdir(open_folder): |
---|
164 | DEFAULT_OPEN_FOLDER = os.path.abspath(open_folder) |
---|
165 | else: |
---|
166 | DEFAULT_OPEN_FOLDER = PATH_APP |
---|
167 | except AttributeError: |
---|
168 | DATALOADER_SHOW = True |
---|
169 | TOOLBAR_SHOW = True |
---|
170 | FIXED_PANEL = True |
---|
171 | WELCOME_PANEL_SHOW = False |
---|
172 | PLOPANEL_WIDTH = config.PLOPANEL_WIDTH |
---|
173 | DATAPANEL_WIDTH = config.DATAPANEL_WIDTH |
---|
174 | GUIFRAME_WIDTH = config.GUIFRAME_WIDTH |
---|
175 | GUIFRAME_HEIGHT = config.GUIFRAME_HEIGHT |
---|
176 | CONTROL_WIDTH = -1 |
---|
177 | CONTROL_HEIGHT = -1 |
---|
178 | DEFAULT_PERSPECTIVE = None |
---|
179 | CLEANUP_PLOT = False |
---|
180 | DEFAULT_OPEN_FOLDER = PATH_APP |
---|
181 | |
---|
182 | DEFAULT_STYLE = config.DEFAULT_STYLE |
---|
183 | |
---|
184 | PLUGIN_STATE_EXTENSIONS = config.PLUGIN_STATE_EXTENSIONS |
---|
185 | OPEN_SAVE_MENU = config.OPEN_SAVE_PROJECT_MENU |
---|
186 | VIEW_MENU = config.VIEW_MENU |
---|
187 | EDIT_MENU = config.EDIT_MENU |
---|
188 | extension_list = [] |
---|
189 | if APPLICATION_STATE_EXTENSION is not None: |
---|
190 | extension_list.append(APPLICATION_STATE_EXTENSION) |
---|
191 | EXTENSIONS = PLUGIN_STATE_EXTENSIONS + extension_list |
---|
192 | try: |
---|
193 | PLUGINS_WLIST = '|'.join(config.PLUGINS_WLIST) |
---|
194 | except AttributeError: |
---|
195 | PLUGINS_WLIST = '' |
---|
196 | APPLICATION_WLIST = config.APPLICATION_WLIST |
---|
197 | IS_WIN = True |
---|
198 | IS_LINUX = False |
---|
199 | CLOSE_SHOW = True |
---|
200 | TIME_FACTOR = 2 |
---|
201 | NOT_SO_GRAPH_LIST = ["BoxSum"] |
---|
202 | |
---|
203 | class Communicate(QtCore.QObject): |
---|
204 | """ |
---|
205 | Utility class for tracking of the Qt signals |
---|
206 | """ |
---|
207 | # File got successfully read |
---|
208 | fileReadSignal = QtCore.pyqtSignal(list) |
---|
209 | |
---|
210 | # Open File returns "list" of paths |
---|
211 | fileDataReceivedSignal = QtCore.pyqtSignal(dict) |
---|
212 | |
---|
213 | # Update Main window status bar with "str" |
---|
214 | # Old "StatusEvent" |
---|
215 | statusBarUpdateSignal = QtCore.pyqtSignal(str) |
---|
216 | |
---|
217 | # Send data to the current perspective |
---|
218 | updatePerspectiveWithDataSignal = QtCore.pyqtSignal(list) |
---|
219 | |
---|
220 | # New data in current perspective |
---|
221 | updateModelFromPerspectiveSignal = QtCore.pyqtSignal(QtGui.QStandardItem) |
---|
222 | |
---|
223 | # New plot requested from the GUI manager |
---|
224 | # Old "NewPlotEvent" |
---|
225 | plotRequestedSignal = QtCore.pyqtSignal(str) |
---|
226 | |
---|
227 | # Progress bar update value |
---|
228 | progressBarUpdateSignal = QtCore.pyqtSignal(int) |
---|
229 | |
---|
230 | # Workspace charts added/removed |
---|
231 | activeGraphsSignal = QtCore.pyqtSignal(list) |
---|
232 | |
---|
233 | # Current workspace chart's name changed |
---|
234 | activeGraphName = QtCore.pyqtSignal(tuple) |
---|
235 | |
---|
236 | |
---|
237 | def updateModelItemWithPlot(item, update_data, name=""): |
---|
238 | """ |
---|
239 | Adds a checkboxed row named "name" to QStandardItem |
---|
240 | Adds QVariant 'update_data' to that row. |
---|
241 | """ |
---|
242 | assert isinstance(item, QtGui.QStandardItem) |
---|
243 | assert isinstance(update_data, QtCore.QVariant) |
---|
244 | py_update_data = update_data.toPyObject() |
---|
245 | |
---|
246 | # Check if data with the same ID is already present |
---|
247 | for index in range(item.rowCount()): |
---|
248 | plot_item = item.child(index) |
---|
249 | if plot_item.isCheckable(): |
---|
250 | plot_data = plot_item.child(0).data().toPyObject() |
---|
251 | if plot_data.id == py_update_data.id: |
---|
252 | item.removeRow(index) |
---|
253 | break |
---|
254 | |
---|
255 | checkbox_item = QtGui.QStandardItem(True) |
---|
256 | checkbox_item.setCheckable(True) |
---|
257 | checkbox_item.setCheckState(QtCore.Qt.Checked) |
---|
258 | checkbox_item.setText(name) |
---|
259 | |
---|
260 | # Add "Info" item |
---|
261 | if isinstance(py_update_data, (Data1D or Data2D)): |
---|
262 | # If Data1/2D added - extract Info from it |
---|
263 | info_item = infoFromData(py_update_data) |
---|
264 | else: |
---|
265 | # otherwise just add a naked item |
---|
266 | info_item = QtGui.QStandardItem("Info") |
---|
267 | |
---|
268 | # Add the actual Data1D/Data2D object |
---|
269 | object_item = QtGui.QStandardItem() |
---|
270 | object_item.setData(update_data) |
---|
271 | |
---|
272 | # Set the data object as the first child |
---|
273 | checkbox_item.setChild(0, object_item) |
---|
274 | |
---|
275 | # Set info_item as the second child |
---|
276 | checkbox_item.setChild(1, info_item) |
---|
277 | |
---|
278 | # Append the new row to the main item |
---|
279 | item.appendRow(checkbox_item) |
---|
280 | |
---|
281 | def updateModelItem(item, update_data, name=""): |
---|
282 | """ |
---|
283 | Adds a simple named child to QStandardItem |
---|
284 | """ |
---|
285 | assert isinstance(item, QtGui.QStandardItem) |
---|
286 | assert isinstance(update_data, list) |
---|
287 | |
---|
288 | # Add the actual Data1D/Data2D object |
---|
289 | object_item = QtGui.QStandardItem() |
---|
290 | object_item.setText(name) |
---|
291 | object_item.setData(QtCore.QVariant(update_data)) |
---|
292 | |
---|
293 | # Append the new row to the main item |
---|
294 | item.appendRow(object_item) |
---|
295 | |
---|
296 | |
---|
297 | def plotsFromCheckedItems(model_item): |
---|
298 | """ |
---|
299 | Returns the list of plots for items in the model which are checked |
---|
300 | """ |
---|
301 | assert isinstance(model_item, QtGui.QStandardItemModel) |
---|
302 | |
---|
303 | plot_data = [] |
---|
304 | # Iterate over model looking for items with checkboxes |
---|
305 | for index in range(model_item.rowCount()): |
---|
306 | item = model_item.item(index) |
---|
307 | if item.isCheckable() and item.checkState() == QtCore.Qt.Checked: |
---|
308 | # TODO: assure item type is correct (either data1/2D or Plotter) |
---|
309 | plot_data.append((item, item.child(0).data().toPyObject())) |
---|
310 | # Going 1 level deeper only |
---|
311 | for index_2 in range(item.rowCount()): |
---|
312 | item_2 = item.child(index_2) |
---|
313 | if item_2 and item_2.isCheckable() and item_2.checkState() == QtCore.Qt.Checked: |
---|
314 | # TODO: assure item type is correct (either data1/2D or Plotter) |
---|
315 | plot_data.append((item_2, item_2.child(0).data().toPyObject())) |
---|
316 | |
---|
317 | return plot_data |
---|
318 | |
---|
319 | def infoFromData(data): |
---|
320 | """ |
---|
321 | Given Data1D/Data2D object, extract relevant Info elements |
---|
322 | and add them to a model item |
---|
323 | """ |
---|
324 | assert isinstance(data, (Data1D, Data2D)) |
---|
325 | |
---|
326 | info_item = QtGui.QStandardItem("Info") |
---|
327 | |
---|
328 | title_item = QtGui.QStandardItem("Title: " + data.title) |
---|
329 | info_item.appendRow(title_item) |
---|
330 | run_item = QtGui.QStandardItem("Run: " + str(data.run)) |
---|
331 | info_item.appendRow(run_item) |
---|
332 | type_item = QtGui.QStandardItem("Type: " + str(data.__class__.__name__)) |
---|
333 | info_item.appendRow(type_item) |
---|
334 | |
---|
335 | if data.path: |
---|
336 | path_item = QtGui.QStandardItem("Path: " + data.path) |
---|
337 | info_item.appendRow(path_item) |
---|
338 | |
---|
339 | if data.instrument: |
---|
340 | instr_item = QtGui.QStandardItem("Instrument: " + data.instrument) |
---|
341 | info_item.appendRow(instr_item) |
---|
342 | |
---|
343 | process_item = QtGui.QStandardItem("Process") |
---|
344 | if isinstance(data.process, list) and data.process: |
---|
345 | for process in data.process: |
---|
346 | process_date = process.date |
---|
347 | process_date_item = QtGui.QStandardItem("Date: " + process_date) |
---|
348 | process_item.appendRow(process_date_item) |
---|
349 | |
---|
350 | process_descr = process.description |
---|
351 | process_descr_item = QtGui.QStandardItem("Description: " + process_descr) |
---|
352 | process_item.appendRow(process_descr_item) |
---|
353 | |
---|
354 | process_name = process.name |
---|
355 | process_name_item = QtGui.QStandardItem("Name: " + process_name) |
---|
356 | process_item.appendRow(process_name_item) |
---|
357 | |
---|
358 | info_item.appendRow(process_item) |
---|
359 | |
---|
360 | return info_item |
---|
361 | |
---|
362 | def openLink(url): |
---|
363 | """ |
---|
364 | Open a URL in an external browser. |
---|
365 | Check the URL first, though. |
---|
366 | """ |
---|
367 | parsed_url = urlparse.urlparse(url) |
---|
368 | if parsed_url.scheme: |
---|
369 | webbrowser.open(url) |
---|
370 | else: |
---|
371 | msg = "Attempt at opening an invalid URL" |
---|
372 | raise AttributeError, msg |
---|
373 | |
---|
374 | def retrieveData1d(data): |
---|
375 | """ |
---|
376 | Retrieve 1D data from file and construct its text |
---|
377 | representation |
---|
378 | """ |
---|
379 | if not isinstance(data, Data1D): |
---|
380 | msg = "Incorrect type passed to retrieveData1d" |
---|
381 | raise AttributeError, msg |
---|
382 | try: |
---|
383 | xmin = min(data.x) |
---|
384 | ymin = min(data.y) |
---|
385 | except: |
---|
386 | msg = "Unable to find min/max of \n data named %s" % \ |
---|
387 | data.filename |
---|
388 | #logging.error(msg) |
---|
389 | raise ValueError, msg |
---|
390 | |
---|
391 | text = data.__str__() |
---|
392 | text += 'Data Min Max:\n' |
---|
393 | text += 'X_min = %s: X_max = %s\n' % (xmin, max(data.x)) |
---|
394 | text += 'Y_min = %s: Y_max = %s\n' % (ymin, max(data.y)) |
---|
395 | if data.dy != None: |
---|
396 | text += 'dY_min = %s: dY_max = %s\n' % (min(data.dy), max(data.dy)) |
---|
397 | text += '\nData Points:\n' |
---|
398 | x_st = "X" |
---|
399 | for index in range(len(data.x)): |
---|
400 | if data.dy != None and len(data.dy) > index: |
---|
401 | dy_val = data.dy[index] |
---|
402 | else: |
---|
403 | dy_val = 0.0 |
---|
404 | if data.dx != None and len(data.dx) > index: |
---|
405 | dx_val = data.dx[index] |
---|
406 | else: |
---|
407 | dx_val = 0.0 |
---|
408 | if data.dxl != None and len(data.dxl) > index: |
---|
409 | if index == 0: |
---|
410 | x_st = "Xl" |
---|
411 | dx_val = data.dxl[index] |
---|
412 | elif data.dxw != None and len(data.dxw) > index: |
---|
413 | if index == 0: |
---|
414 | x_st = "Xw" |
---|
415 | dx_val = data.dxw[index] |
---|
416 | |
---|
417 | if index == 0: |
---|
418 | text += "<index> \t<X> \t<Y> \t<dY> \t<d%s>\n" % x_st |
---|
419 | text += "%s \t%s \t%s \t%s \t%s\n" % (index, |
---|
420 | data.x[index], |
---|
421 | data.y[index], |
---|
422 | dy_val, |
---|
423 | dx_val) |
---|
424 | return text |
---|
425 | |
---|
426 | def retrieveData2d(data): |
---|
427 | """ |
---|
428 | Retrieve 2D data from file and construct its text |
---|
429 | representation |
---|
430 | """ |
---|
431 | if not isinstance(data, Data2D): |
---|
432 | msg = "Incorrect type passed to retrieveData2d" |
---|
433 | raise AttributeError, msg |
---|
434 | |
---|
435 | text = data.__str__() |
---|
436 | text += 'Data Min Max:\n' |
---|
437 | text += 'I_min = %s\n' % min(data.data) |
---|
438 | text += 'I_max = %s\n\n' % max(data.data) |
---|
439 | text += 'Data (First 2501) Points:\n' |
---|
440 | text += 'Data columns include err(I).\n' |
---|
441 | text += 'ASCII data starts here.\n' |
---|
442 | text += "<index> \t<Qx> \t<Qy> \t<I> \t<dI> \t<dQparal> \t<dQperp>\n" |
---|
443 | di_val = 0.0 |
---|
444 | dx_val = 0.0 |
---|
445 | dy_val = 0.0 |
---|
446 | len_data = len(data.qx_data) |
---|
447 | for index in xrange(0, len_data): |
---|
448 | x_val = data.qx_data[index] |
---|
449 | y_val = data.qy_data[index] |
---|
450 | i_val = data.data[index] |
---|
451 | if data.err_data != None: |
---|
452 | di_val = data.err_data[index] |
---|
453 | if data.dqx_data != None: |
---|
454 | dx_val = data.dqx_data[index] |
---|
455 | if data.dqy_data != None: |
---|
456 | dy_val = data.dqy_data[index] |
---|
457 | |
---|
458 | text += "%s \t%s \t%s \t%s \t%s \t%s \t%s\n" % (index, |
---|
459 | x_val, |
---|
460 | y_val, |
---|
461 | i_val, |
---|
462 | di_val, |
---|
463 | dx_val, |
---|
464 | dy_val) |
---|
465 | # Takes too long time for typical data2d: Break here |
---|
466 | if index >= 2500: |
---|
467 | text += ".............\n" |
---|
468 | break |
---|
469 | |
---|
470 | return text |
---|
471 | |
---|
472 | def onTXTSave(data, path): |
---|
473 | """ |
---|
474 | Save file as formatted txt |
---|
475 | """ |
---|
476 | with open(path,'w') as out: |
---|
477 | has_errors = True |
---|
478 | if data.dy == None or data.dy == []: |
---|
479 | has_errors = False |
---|
480 | # Sanity check |
---|
481 | if has_errors: |
---|
482 | try: |
---|
483 | if len(data.y) != len(data.dy): |
---|
484 | has_errors = False |
---|
485 | except: |
---|
486 | has_errors = False |
---|
487 | if has_errors: |
---|
488 | if data.dx != None and data.dx != []: |
---|
489 | out.write("<X> <Y> <dY> <dX>\n") |
---|
490 | else: |
---|
491 | out.write("<X> <Y> <dY>\n") |
---|
492 | else: |
---|
493 | out.write("<X> <Y>\n") |
---|
494 | |
---|
495 | for i in range(len(data.x)): |
---|
496 | if has_errors: |
---|
497 | if data.dx != None and data.dx != []: |
---|
498 | if data.dx[i] != None: |
---|
499 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
500 | data.y[i], |
---|
501 | data.dy[i], |
---|
502 | data.dx[i])) |
---|
503 | else: |
---|
504 | out.write("%g %g %g\n" % (data.x[i], |
---|
505 | data.y[i], |
---|
506 | data.dy[i])) |
---|
507 | else: |
---|
508 | out.write("%g %g %g\n" % (data.x[i], |
---|
509 | data.y[i], |
---|
510 | data.dy[i])) |
---|
511 | else: |
---|
512 | out.write("%g %g\n" % (data.x[i], |
---|
513 | data.y[i])) |
---|
514 | |
---|
515 | def saveData1D(data): |
---|
516 | """ |
---|
517 | Save 1D data points |
---|
518 | """ |
---|
519 | default_name = os.path.basename(data.filename) |
---|
520 | default_name, extension = os.path.splitext(default_name) |
---|
521 | default_name += "_out" + extension |
---|
522 | |
---|
523 | wildcard = "Text files (*.txt);;"\ |
---|
524 | "CanSAS 1D files(*.xml)" |
---|
525 | kwargs = { |
---|
526 | 'caption' : 'Save As', |
---|
527 | 'directory' : default_name, |
---|
528 | 'filter' : wildcard, |
---|
529 | 'parent' : None, |
---|
530 | } |
---|
531 | # Query user for filename. |
---|
532 | filename = QtGui.QFileDialog.getSaveFileName(**kwargs) |
---|
533 | |
---|
534 | # User cancelled. |
---|
535 | if not filename: |
---|
536 | return |
---|
537 | |
---|
538 | filename = str(filename) |
---|
539 | |
---|
540 | #Instantiate a loader |
---|
541 | loader = Loader() |
---|
542 | if os.path.splitext(filename)[1].lower() == ".txt": |
---|
543 | onTXTSave(data, filename) |
---|
544 | if os.path.splitext(filename)[1].lower() == ".xml": |
---|
545 | loader.save(filename, data, ".xml") |
---|
546 | |
---|
547 | def saveData2D(data): |
---|
548 | """ |
---|
549 | Save data2d dialog |
---|
550 | """ |
---|
551 | default_name = os.path.basename(data.filename) |
---|
552 | default_name, _ = os.path.splitext(default_name) |
---|
553 | ext_format = ".dat" |
---|
554 | default_name += "_out" + ext_format |
---|
555 | |
---|
556 | wildcard = "IGOR/DAT 2D file in Q_map (*.dat)" |
---|
557 | kwargs = { |
---|
558 | 'caption' : 'Save As', |
---|
559 | 'directory' : default_name, |
---|
560 | 'filter' : wildcard, |
---|
561 | 'parent' : None, |
---|
562 | } |
---|
563 | # Query user for filename. |
---|
564 | filename = QtGui.QFileDialog.getSaveFileName(**kwargs) |
---|
565 | |
---|
566 | # User cancelled. |
---|
567 | if not filename: |
---|
568 | return |
---|
569 | filename = str(filename) |
---|
570 | #Instantiate a loader |
---|
571 | loader = Loader() |
---|
572 | |
---|
573 | if os.path.splitext(filename)[1].lower() == ext_format: |
---|
574 | loader.save(filename, data, ext_format) |
---|
575 | |
---|
576 | class FormulaValidator(QtGui.QValidator): |
---|
577 | def __init__(self, parent=None): |
---|
578 | super(FormulaValidator, self).__init__(parent) |
---|
579 | |
---|
580 | def validate(self, input, pos): |
---|
581 | try: |
---|
582 | Formula(str(input)) |
---|
583 | self._setStyleSheet("") |
---|
584 | return QtGui.QValidator.Acceptable, pos |
---|
585 | |
---|
586 | except Exception as e: |
---|
587 | self._setStyleSheet("background-color:pink;") |
---|
588 | return QtGui.QValidator.Intermediate, pos |
---|
589 | |
---|
590 | def _setStyleSheet(self, value): |
---|
591 | try: |
---|
592 | if self.parent(): |
---|
593 | self.parent().setStyleSheet(value) |
---|
594 | except: |
---|
595 | pass |
---|
596 | |
---|
597 | def xyTransform(data, xLabel="", yLabel=""): |
---|
598 | """ |
---|
599 | Transforms x and y in View and set the scale |
---|
600 | """ |
---|
601 | # Changing the scale might be incompatible with |
---|
602 | # currently displayed data (for instance, going |
---|
603 | # from ln to log when all plotted values have |
---|
604 | # negative natural logs). |
---|
605 | # Go linear and only change the scale at the end. |
---|
606 | xscale = 'linear' |
---|
607 | yscale = 'linear' |
---|
608 | # Local data is either 1D or 2D |
---|
609 | if data.id == 'fit': |
---|
610 | return |
---|
611 | |
---|
612 | # control axis labels from the panel itself |
---|
613 | yname, yunits = data.get_yaxis() |
---|
614 | xname, xunits = data.get_xaxis() |
---|
615 | |
---|
616 | # Goes through all possible scales |
---|
617 | # self.x_label is already wrapped with Latex "$", so using the argument |
---|
618 | |
---|
619 | # X |
---|
620 | if xLabel == "x": |
---|
621 | data.transformX(transform.toX, transform.errToX) |
---|
622 | xLabel = "%s(%s)" % (xname, xunits) |
---|
623 | if xLabel == "x^(2)": |
---|
624 | data.transformX(transform.toX2, transform.errToX2) |
---|
625 | xunits = convert_unit(2, xunits) |
---|
626 | xLabel = "%s^{2}(%s)" % (xname, xunits) |
---|
627 | if xLabel == "x^(4)": |
---|
628 | data.transformX(transform.toX4, transform.errToX4) |
---|
629 | xunits = convert_unit(4, xunits) |
---|
630 | xLabel = "%s^{4}(%s)" % (xname, xunits) |
---|
631 | if xLabel == "ln(x)": |
---|
632 | data.transformX(transform.toLogX, transform.errToLogX) |
---|
633 | xLabel = "\ln{(%s)}(%s)" % (xname, xunits) |
---|
634 | if xLabel == "log10(x)": |
---|
635 | data.transformX(transform.toX_pos, transform.errToX_pos) |
---|
636 | xscale = 'log' |
---|
637 | xLabel = "%s(%s)" % (xname, xunits) |
---|
638 | if xLabel == "log10(x^(4))": |
---|
639 | data.transformX(transform.toX4, transform.errToX4) |
---|
640 | xunits = convert_unit(4, xunits) |
---|
641 | xLabel = "%s^{4}(%s)" % (xname, xunits) |
---|
642 | xscale = 'log' |
---|
643 | |
---|
644 | # Y |
---|
645 | if yLabel == "ln(y)": |
---|
646 | data.transformY(transform.toLogX, transform.errToLogX) |
---|
647 | yLabel = "\ln{(%s)}(%s)" % (yname, yunits) |
---|
648 | if yLabel == "y": |
---|
649 | data.transformY(transform.toX, transform.errToX) |
---|
650 | yLabel = "%s(%s)" % (yname, yunits) |
---|
651 | if yLabel == "log10(y)": |
---|
652 | data.transformY(transform.toX_pos, transform.errToX_pos) |
---|
653 | yscale = 'log' |
---|
654 | yLabel = "%s(%s)" % (yname, yunits) |
---|
655 | if yLabel == "y^(2)": |
---|
656 | data.transformY(transform.toX2, transform.errToX2) |
---|
657 | yunits = convert_unit(2, yunits) |
---|
658 | yLabel = "%s^{2}(%s)" % (yname, yunits) |
---|
659 | if yLabel == "1/y": |
---|
660 | data.transformY(transform.toOneOverX, transform.errOneOverX) |
---|
661 | yunits = convert_unit(-1, yunits) |
---|
662 | yLabel = "1/%s(%s)" % (yname, yunits) |
---|
663 | if yLabel == "y*x^(2)": |
---|
664 | data.transformY(transform.toYX2, transform.errToYX2) |
---|
665 | xunits = convert_unit(2, xunits) |
---|
666 | yLabel = "%s \ \ %s^{2}(%s%s)" % (yname, xname, yunits, xunits) |
---|
667 | if yLabel == "y*x^(4)": |
---|
668 | data.transformY(transform.toYX4, transform.errToYX4) |
---|
669 | xunits = convert_unit(4, xunits) |
---|
670 | yLabel = "%s \ \ %s^{4}(%s%s)" % (yname, xname, yunits, xunits) |
---|
671 | if yLabel == "1/sqrt(y)": |
---|
672 | data.transformY(transform.toOneOverSqrtX, |
---|
673 | transform.errOneOverSqrtX) |
---|
674 | yunits = convert_unit(-0.5, yunits) |
---|
675 | yLabel = "1/\sqrt{%s}(%s)" % (yname, yunits) |
---|
676 | if yLabel == "ln(y*x)": |
---|
677 | data.transformY(transform.toLogXY, transform.errToLogXY) |
---|
678 | yLabel = "\ln{(%s \ \ %s)}(%s%s)" % (yname, xname, yunits, xunits) |
---|
679 | if yLabel == "ln(y*x^(2))": |
---|
680 | data.transformY(transform.toLogYX2, transform.errToLogYX2) |
---|
681 | xunits = convert_unit(2, xunits) |
---|
682 | yLabel = "\ln (%s \ \ %s^{2})(%s%s)" % (yname, xname, yunits, xunits) |
---|
683 | if yLabel == "ln(y*x^(4))": |
---|
684 | data.transformY(transform.toLogYX4, transform.errToLogYX4) |
---|
685 | xunits = convert_unit(4, xunits) |
---|
686 | yLabel = "\ln (%s \ \ %s^{4})(%s%s)" % (yname, xname, yunits, xunits) |
---|
687 | if yLabel == "log10(y*x^(4))": |
---|
688 | data.transformY(transform.toYX4, transform.errToYX4) |
---|
689 | xunits = convert_unit(4, xunits) |
---|
690 | yscale = 'log' |
---|
691 | yLabel = "%s \ \ %s^{4}(%s%s)" % (yname, xname, yunits, xunits) |
---|
692 | |
---|
693 | # Perform the transformation of data in data1d->View |
---|
694 | data.transformView() |
---|
695 | |
---|
696 | return (xLabel, yLabel, xscale, yscale) |
---|
697 | |
---|
698 | def dataFromItem(item): |
---|
699 | """ |
---|
700 | Retrieve Data1D/2D component from QStandardItem. |
---|
701 | The assumption - data stored in SasView standard, in child 0 |
---|
702 | """ |
---|
703 | return item.child(0).data().toPyObject() |
---|
704 | |
---|
705 | def formatNumber(value, high=False): |
---|
706 | """ |
---|
707 | Return a float in a standardized, human-readable formatted string. |
---|
708 | This is used to output readable (e.g. x.xxxe-y) values to the panel. |
---|
709 | """ |
---|
710 | try: |
---|
711 | value = float(value) |
---|
712 | except: |
---|
713 | output = "NaN" |
---|
714 | return output.lstrip().rstrip() |
---|
715 | |
---|
716 | if high: |
---|
717 | output = "%-6.4g" % value |
---|
718 | |
---|
719 | else: |
---|
720 | output = "%-5.3g" % value |
---|
721 | return output.lstrip().rstrip() |
---|