__xdata__name__ = "SIEY"

__xdata__items__ = [
    "SIEY_MolecularDynamics",
    "SIEY_MonteCarlo",
    "SIEY_ChemicalKinetics",
    "SIEY_Preferences",
    "XObjectForExtensionCreationWaitingXFunctions",
    ]

# ------------

__version__ = "0.5.0"

from xdata import *

class XObjectForExtensionCreationWaitingXFunctions(XObject):
    
    __object__xmethods__ = [
        XMethod('GetExtension',
                out_xtype=XSalomeReference(component="SIEY",
                                           idl="SIEY_Extension.idl",
                                           module="SIEY_CORBA",
                                           interface="Extension",
                                           )
                ),
        ]
    
    def GetExtension(self):
        from SIEY_Extension import Extension
        return Extension()._this()
    
    pass

# -------------

from qt import *

def customPopup(parent, popup, selection):
    print "SIEY_xdata:_customPopup:", parent, popup, selection
    #if len(selection) != 1: return
    #selection = selection[0]
    #if not hasattr(selection, "_narrow"): return
    #theObject = str(selection)
    #theObject = theObject.split("_objref_")[1]
    #theObject = theObject.split()[0]
    #theParent = "ObjectBrowser"
    #
    from SIEYGUI_Extension import definePopup
    theContext, theObject, theParent = definePopup( "", "", "" )
    print "theContext, theObject, theParent :", theContext, theObject, theParent
    if theObject == "TimeStamp":
        popup.insertItem("To Spheres",                   popupActivated, 0, 505, -1)
        popup.insertItem("To Points",                    popupActivated, 0, 506, -1)
        popup.insertItem("Show Title",                   popupActivated, 0, 507, -1)
        popup.insertItem("Hide Title",                   popupActivated, 0, 508, -1)
        popup.insertSeparator()
        popup.insertItem("Display",                      popupActivated, 0, 501, -1)
        popup.insertItem("Display Only",                 popupActivated, 0, 502, -1)
        popup.insertItem("Erase",                        popupActivated, 0, 503, -1)
        popup.insertSeparator()
        popup.insertItem("View Data",                    popupActivated, 0, 510, -1)
        popup.insertItem("Export to file...",            popupActivated, 0, 504, -1)
        popup.insertSeparator()
        popup.insertItem("Delete",                       popupActivated, 0, 801, -1)
        pass
    if theObject == "TabFile":
        popup.insertItem("View Data",                    popupActivated, 0, 510, -1)
        popup.insertItem("Export to file...",            popupActivated, 0, 504, -1)
        popup.insertItem("Import to VISU",               popupActivated, 0, 601, -1)
        popup.insertSeparator()
        popup.insertItem("Delete",                       popupActivated, 0, 801, -1)
        pass
    if theObject == "DataFile":
        popup.insertItem("View Data",                    popupActivated, 0, 510, -1)
        popup.insertItem("Export to file...",            popupActivated, 0, 504, -1)
        popup.insertSeparator()
        popup.insertItem("Delete",                       popupActivated, 0, 801, -1)
        pass
    if theObject == "BinaryFile":
        popup.insertItem("Export to file...",            popupActivated, 0, 504, -1)
        popup.insertSeparator()
        popup.insertItem("Delete",                       popupActivated, 0, 801, -1)
        pass
    if theObject == "SuperTimeStamp":
        popup.insertItem("Export Atomic Coordinates...", popupActivated, 0, 420, -1)
        popup.insertSeparator()
        popup.insertItem("To Spheres",                   popupActivated, 0, 505, -1)
        popup.insertItem("To Points",                    popupActivated, 0, 506, -1)
        popup.insertItem("Show Title",                   popupActivated, 0, 507, -1)
        popup.insertItem("Hide Title",                   popupActivated, 0, 508, -1)
        popup.insertSeparator()
        popup.insertItem("Erase All",                    popupActivated, 0, 406, -1)
        popup.insertItem("Animation",                    popupActivated, 0, 407, -1)
        popup.insertSeparator()
        popup.insertItem("View Data",                    popupActivated, 0, 510, -1)
        popup.insertItem("Export to file...",            popupActivated, 0, 504, -1)
        popup.insertSeparator()
        popup.insertItem("Delete",                       popupActivated, 0, 801, -1)
        pass
    if theObject == "OutputFiles":
        popup.insertItem("To Spheres",                   popupActivated, 0, 505, -1)
        popup.insertItem("To Points",                    popupActivated, 0, 506, -1)
        popup.insertItem("Show Title",                   popupActivated, 0, 507, -1)
        popup.insertItem("Hide Title",                   popupActivated, 0, 508, -1)
        popup.insertSeparator()
        popup.insertItem("Display",                      popupActivated, 0, 501, -1)
        popup.insertItem("Display Only",                 popupActivated, 0, 502, -1)
        popup.insertItem("Erase",                        popupActivated, 0, 503, -1)
        popup.insertSeparator()
        popup.insertItem("Export to files...",           popupActivated, 0, 420, -1)
        popup.insertSeparator()
        popup.insertItem("Delete",                       popupActivated, 0, 801, -1)
        pass
    if theObject == "TabFiles":
        popup.insertItem("Import to VISU",               popupActivated, 0, 601, -1)
        popup.insertSeparator()
        popup.insertItem("Export to files...",           popupActivated, 0, 420, -1)
        popup.insertSeparator()
        popup.insertItem("Delete",                       popupActivated, 0, 801, -1)
        pass
    #
    from SIEYGUI_Extension import customPopup
    customPopup(popup, "", theObject, theParent)
    return

def popupActivated(item_id):
    from SIEYGUI_Extension import OnGUIEvent
    OnGUIEvent(item_id)
    return

# ------------------

__SEPARATOR__ = " "
__entryToIORMap__ = {}

def Save(self, theComponent, theURL, isMultiFile):
    from XDATA_i import XDATA
    import SIEY_CORBA
    import StringIO
    # first call XDATA's Save() : 
    stream = XDATA.Save(self, theComponent, theURL, 0) # isMultiFile is always 0 - we save XDATA and SIEY data to the one file
    # then save non-XDATA objects
    mystream = StringIO.StringIO()
    itr = self.study.NewChildIterator(theComponent)
    itr.InitEx(1)
    while itr.More():
        so = itr.Value()
        itr.Next()
        obj = so.GetObject()
        if obj:
            if obj._narrow(SIEY_CORBA.BinaryFile):
                val = obj._narrow(SIEY_CORBA.BinaryFile).GetValue()
                mystream.write(str(len(val)) + __SEPARATOR__)
                mystream.write(so.GetID()    + __SEPARATOR__)
                mystream.write("BinaryFile"  + __SEPARATOR__)
                mystream.write(val)
            elif obj._narrow(SIEY_CORBA.TimeStamp):
                val = obj._narrow(SIEY_CORBA.DataFile).GetValue()
                mystream.write(str(len(val)) + __SEPARATOR__)
                mystream.write(so.GetID()    + __SEPARATOR__)
                mystream.write("TimeStamp"   + __SEPARATOR__)
                mystream.write(val)
            elif obj._narrow(SIEY_CORBA.TabFile):
                val = obj._narrow(SIEY_CORBA.DataFile).GetValue()
                mystream.write(str(len(val)) + __SEPARATOR__)
                mystream.write(so.GetID()    + __SEPARATOR__)
                mystream.write("TabFile"   + __SEPARATOR__)
                mystream.write(val)
            elif obj._narrow(SIEY_CORBA.DataFile):
                val = obj._narrow(SIEY_CORBA.DataFile).GetValue()
                mystream.write(str(len(val)) + __SEPARATOR__)
                mystream.write(so.GetID()    + __SEPARATOR__)
                mystream.write("DataFile"    + __SEPARATOR__)
                mystream.write(val)
                pass
            pass
        pass
    stream = str(mystream.len) + __SEPARATOR__ + mystream.getvalue() + stream
    if isMultiFile:
        filename = self.study._get_URL()
        idx = filename.rfind(".")
        filename = filename[:idx]
        filename +="_SIEY.sg"
        fd = open(filename, "w")
        fd.write(stream)
        fd.close()
        stream = filename
        pass
    return stream
def Load(self, theComponent, theStream, theURL, isMultiFile):
    from XDATA_i import XDATA
    studyId = theComponent.GetStudy()._get_StudyId()
    if __entryToIORMap__.has_key(studyId):
        __entryToIORMap__[studyId].clear()
    else:
        __entryToIORMap__[studyId] = {}
        pass
    if isMultiFile:
        self.study = theComponent.GetStudy()
        filename = self.study._get_URL()
        idx = filename.rfind(".")
        filename = filename[:idx]
        filename +="_SIEY.sg"
        fd = open(filename, "r")
        stream = fd.read()
        fd.close()
        pass
    else:
        stream = theStream
        pass
    ind = stream.find(__SEPARATOR__)
    datalen = int(stream[:ind])
    mystream = stream[ind+1:ind+1+datalen]
    xdatastream = stream[ind+1+datalen:]
    ind = 0
    while ind < len(mystream):
        prevind = ind
        ind = mystream.find(__SEPARATOR__, ind)
        if (ind < 0):  break
        datalen = int(mystream[prevind:ind])
        ind1 = mystream.find(__SEPARATOR__, ind+1)
        if (ind1 < 0): break
        entry = mystream[ind+1:ind1]
        ind2 = mystream.find(__SEPARATOR__, ind1+1)
        if (ind2 < 0): break
        type = mystream[ind1+1:ind2]
        data = mystream[ind2+1:ind2+1+datalen]
        ind = ind2+1+datalen
        import SIEY_Extension
        from xsalome import getORB
        if type == "BinaryFile":
            aServant = SIEY_Extension.BinaryFile(data)
            anObject = aServant._this()
            __entryToIORMap__[studyId][entry] = getORB().object_to_string(anObject)
        elif type == "TimeStamp":
            aServant = SIEY_Extension.TimeStamp(data)
            anObject = aServant._this()
            __entryToIORMap__[studyId][entry] = getORB().object_to_string(anObject)
        elif type == "TabFile":
            aServant = SIEY_Extension.TabFile(data)
            anObject = aServant._this()
            __entryToIORMap__[studyId][entry] = getORB().object_to_string(anObject)
        elif type == "DataFile":
            aServant = SIEY_Extension.DataFile(data)
            anObject = aServant._this()
            __entryToIORMap__[studyId][entry] = getORB().object_to_string(anObject)
            pass
        pass
    if len(xdatastream) > 0:
        return XDATA.Load(self, theComponent, xdatastream, theURL, 0) # isMultiFile is always 0 - we save XDATA and SIEY data to the one file
    else:
        return 1
    pass
def IORToLocalPersistentID(self, theSObject, IORString, isMultiFile, isASCII):
    from XDATA_i import XDATA
    return XDATA.IORToLocalPersistentID(self, theSObject, IORString, isMultiFile, isASCII) # XDATA uses SObject's entry as persistent id
def LocalPersistentIDToIOR(self, theSObject, PersistentID, isMultiFile, isASCII):
    from XDATA_i import XDATA
    studyId = theSObject.GetStudy()._get_StudyId()
    if __entryToIORMap__.has_key(studyId) and __entryToIORMap__[studyId].has_key(PersistentID):
        return __entryToIORMap__[studyId][PersistentID]
    return XDATA.LocalPersistentIDToIOR(self, theSObject, PersistentID, isMultiFile, isASCII)
def Close(self, theComponent):
    from XDATA_i import XDATA
    print "SIEY::Close()"
    __entryToIORMap__.clear()
    XDATA.Close(self, theComponent)
    pass

def setSettings():
    from SIEYGUI_Extension import setSettings
    setSettings()
    pass

