Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Utilities/GuiUtils.py

    r2eeda93 raed159f  
    1111import webbrowser 
    1212import urllib.parse 
    13 import json 
    14 from io import BytesIO 
    1513 
    1614import numpy as np 
     
    2826from sas.qtgui.Plotting.PlotterData import Data1D 
    2927from sas.qtgui.Plotting.PlotterData import Data2D 
    30 from sas.qtgui.Plotting.Plottables import Plottable 
    31 from sas.sascalc.dataloader.data_info import Sample, Source, Vector 
    32 from sas.qtgui.Plotting.Plottables import View 
    33 from sas.qtgui.Plotting.Plottables import PlottableTheory1D 
    34 from sas.qtgui.Plotting.Plottables import PlottableFit1D 
    35 from sas.qtgui.Plotting.Plottables import Text 
    36 from sas.qtgui.Plotting.Plottables import Chisq 
    37 from sas.qtgui.MainWindow.DataState import DataState 
    38  
    3928from sas.sascalc.dataloader.loader import Loader 
    4029from sas.qtgui.Utilities import CustomDir 
     
    10695    except ImportError: 
    10796        pass 
    108         #logging.error("Error loading %s/%s: %s" % (path, confg_file, sys.exc_value)) 
    10997    except ValueError: 
    11098        print("Value error") 
     
    113101        if fObj is not None: 
    114102            fObj.close() 
    115     #logging.info("GuiManager loaded %s/%s" % (path, confg_file)) 
    116103    return config_module 
    117104 
     
    270257    sendDataToGridSignal = QtCore.pyqtSignal(list) 
    271258 
     259    # Action Save Analysis triggered 
     260    saveAnalysisSignal = QtCore.pyqtSignal() 
     261 
    272262    # Mask Editor requested 
    273263    maskEditorSignal = QtCore.pyqtSignal(Data2D) 
     
    294284    changeDataExplorerTabSignal = QtCore.pyqtSignal(int) 
    295285 
    296 def updateModelItemWithPlot(item, update_data, name="", checkbox_state=None): 
     286    # Plot fitting results (FittingWidget->GuiManager) 
     287    resultPlotUpdateSignal = QtCore.pyqtSignal(list) 
     288 
     289def updateModelItemWithPlot(item, update_data, name=""): 
    297290    """ 
    298291    Adds a checkboxed row named "name" to QStandardItem 
     
    319312            # Force redisplay 
    320313            return 
     314 
    321315    # Create the new item 
    322316    checkbox_item = createModelItemWithPlot(update_data, name) 
    323317 
    324     if checkbox_state is not None: 
    325         checkbox_item.setCheckState(checkbox_state) 
    326318    # Append the new row to the main item 
    327319    item.appendRow(checkbox_item) 
     
    574566    if isinstance(data.process, list) and data.process: 
    575567        for process in data.process: 
    576             if process is None: 
    577                 continue 
    578568            process_date = process.date 
    579569            process_date_item = QtGui.QStandardItem("Date: " + process_date) 
     
    11501140    return result 
    11511141 
    1152 def saveData(fp, data): 
    1153     """ 
    1154     save content of data to fp (a .write()-supporting file-like object) 
    1155     """ 
    1156  
    1157     def add_type(dict, type): 
    1158         dict['__type__'] = type.__name__ 
    1159         return dict 
    1160  
    1161     def jdefault(o): 
    1162         """ 
    1163         objects that can't otherwise be serialized need to be converted 
    1164         """ 
    1165         # tuples and sets (TODO: default JSONEncoder converts tuples to lists, create custom Encoder that preserves tuples) 
    1166         if isinstance(o, (tuple, set)): 
    1167             content = { 'data': list(o) } 
    1168             return add_type(content, type(o)) 
    1169  
    1170         # "simple" types 
    1171         if isinstance(o, (Sample, Source, Vector)): 
    1172             return add_type(o.__dict__, type(o)) 
    1173         if isinstance(o, (Plottable, View)): 
    1174             return add_type(o.__dict__, type(o)) 
    1175  
    1176         # DataState 
    1177         if isinstance(o, (Data1D, Data2D)): 
    1178             # don't store parent 
    1179             content = o.__dict__.copy() 
    1180             #content.pop('parent') 
    1181             return add_type(content, type(o)) 
    1182  
    1183         # ndarray 
    1184         if isinstance(o, np.ndarray): 
    1185             buffer = BytesIO() 
    1186             np.save(buffer, o) 
    1187             buffer.seek(0) 
    1188             content = { 'data': buffer.read().decode('latin-1') } 
    1189             return add_type(content, type(o)) 
    1190  
    1191         # not supported 
    1192         logging.info("data cannot be serialized to json: %s" % type(o)) 
    1193         return None 
    1194  
    1195     json.dump(data, fp, indent=2, sort_keys=True, default=jdefault) 
    1196  
    1197 def readDataFromFile(fp): 
    1198     ''' 
    1199     ''' 
    1200     supported = [ 
    1201         tuple, set, 
    1202         Sample, Source, Vector, 
    1203         Plottable, Data1D, Data2D, PlottableTheory1D, PlottableFit1D, Text, Chisq, View, 
    1204         DataState, np.ndarray] 
    1205  
    1206     lookup = dict((cls.__name__, cls) for cls in supported) 
    1207  
    1208     class TooComplexException(Exception): 
    1209         pass 
    1210  
    1211     def simple_type(cls, data, level): 
    1212         class Empty(object): 
    1213             def __init__(self): 
    1214                 for key, value in data.items(): 
    1215                     setattr(self, key, generate(value, level)) 
    1216  
    1217         # create target object 
    1218         o = Empty() 
    1219         o.__class__ = cls 
    1220  
    1221         return o 
    1222  
    1223     def construct(type, data, level): 
    1224         try: 
    1225             cls = lookup[type] 
    1226         except KeyError: 
    1227             logging.info('unknown type: %s' % type) 
    1228             return None 
    1229  
    1230         # tuples and sets 
    1231         if cls in (tuple, set): 
    1232             # convert list to tuple/set 
    1233             return cls(generate(data['data'], level)) 
    1234  
    1235         # "simple" types 
    1236         if cls in (Sample, Source, Vector): 
    1237             return simple_type(cls, data, level) 
    1238         if issubclass(cls, Plottable) or (cls == View): 
    1239             return simple_type(cls, data, level) 
    1240  
    1241         # DataState 
    1242         if cls == DataState: 
    1243             o = simple_type(cls, data, level) 
    1244             o.parent = None # TODO: set to ??? 
    1245             return o 
    1246  
    1247         # ndarray 
    1248         if cls == np.ndarray: 
    1249             buffer = BytesIO() 
    1250             buffer.write(data['data'].encode('latin-1')) 
    1251             buffer.seek(0) 
    1252             return np.load(buffer) 
    1253  
    1254         logging.info('not implemented: %s, %s' % (type, cls)) 
    1255         return None 
    1256  
    1257     def generate(data, level): 
    1258         if level > 16: # recursion limit (arbitrary number) 
    1259             raise TooComplexException() 
    1260         else: 
    1261             level += 1 
    1262  
    1263         if isinstance(data, dict): 
    1264             try: 
    1265                 type = data['__type__'] 
    1266             except KeyError: 
    1267                 # if dictionary doesn't have __type__ then it is assumed to be just an ordinary dictionary 
    1268                 o = {} 
    1269                 for key, value in data.items(): 
    1270                     o[key] = generate(value, level) 
    1271                 return o 
    1272  
    1273             return construct(type, data, level) 
    1274  
    1275         if isinstance(data, list): 
    1276             return [generate(item, level) for item in data] 
    1277  
    1278         return data 
    1279  
    1280     new_stored_data = {} 
    1281     for id, data in json.load(fp).items(): 
    1282         try: 
    1283             new_stored_data[id] = generate(data, 0) 
    1284         except TooComplexException: 
    1285             logging.info('unable to load %s' % id) 
    1286  
    1287     return new_stored_data 
    1288  
    12891142 
    12901143def enum(*sequential, **named): 
Note: See TracChangeset for help on using the changeset viewer.