Back to index

salome-gui  6.5.0
GLViewer_MimeData.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "GLViewer_MimeData.h"
00021 #include "GLViewer_BaseObjects.h"
00022 
00026 GLViewer_MimeData::~GLViewer_MimeData()
00027 {
00028 }
00029 
00034 bool GLViewer_MimeData::setObjects( QList<GLViewer_Object*> theObjects )
00035 {
00036     if( !theObjects.empty() )
00037     {
00038         QStringList aObjectsType;
00039         QList<QByteArray> aObjects;
00040         QList<GLViewer_Object*>::const_iterator anIt = theObjects.begin();
00041         QList<GLViewer_Object*>::const_iterator anEndIt = theObjects.end();
00042 
00043         int aObjByteSize = 0;
00044         for( ; anIt != anEndIt; anIt++ )
00045         {
00046             aObjects.append( (*anIt)->getByteCopy() );
00047             aObjByteSize += aObjects.last().size();
00048             aObjectsType.append( (*anIt)->getObjectType() );
00049         }
00050 
00051         int anISize = sizeof( int );
00052         QString aTypes = aObjectsType.join("");
00053         int aStrByteSize = aTypes.length();
00054         int aObjNum = aObjects.count();
00055 
00056         myByteArray.resize( anISize * (1 + 2*aObjNum) + aStrByteSize + aObjByteSize );
00057 
00058         int anIndex = 0, j = 0;
00059         char* aPointer = (char*)&aObjNum;
00060         for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
00061             myByteArray[anIndex] = *aPointer;
00062         
00063         QStringList::const_iterator aStrIt = aObjectsType.begin();
00064         QStringList::const_iterator aEndStrIt = aObjectsType.end();
00065         for( j = 1; aStrIt != aEndStrIt; aStrIt++, j++ )
00066         {
00067             int aStrLen = (*aStrIt).length();
00068             aPointer = (char*)&aStrLen;
00069             for( ; anIndex < anISize*( 1 + j ); anIndex++, aPointer++ )
00070                 myByteArray[anIndex] = *aPointer;
00071         }
00072 
00073         int aCurIndex = anIndex;
00074         const char* aStr = aTypes.toLatin1().constData();
00075 
00076         for( j = 0 ; anIndex < aCurIndex + aStrByteSize; aPointer++, anIndex++, j++ )
00077             myByteArray[anIndex] = aStr[j];
00078 
00079         aCurIndex = anIndex;
00080         QList<QByteArray>::iterator anObjIt = aObjects.begin();
00081         QList<QByteArray>::iterator anEndObjIt = aObjects.end();
00082         for( j = 1; anObjIt != anEndObjIt; anObjIt++, j++ )
00083         {
00084             int aObjLen = (int)((*anObjIt).size());
00085             aPointer = (char*)&aObjLen;
00086             for( ; anIndex < aCurIndex + anISize*j; anIndex++, aPointer++ )
00087                 myByteArray[anIndex] = *aPointer;
00088         }
00089 
00090         aCurIndex = anIndex;
00091         anObjIt = aObjects.begin();
00092 
00093         for( ; anObjIt != anEndObjIt; anObjIt++ )
00094         {
00095             int aObjLen = (int)((*anObjIt).size());
00096             for( j = 0 ; anIndex < aCurIndex + aObjLen; anIndex++, aPointer++, j++ )
00097                 myByteArray[anIndex] = (*anObjIt)[j];
00098             aCurIndex = anIndex;
00099         }
00100      
00101         return true;
00102     }
00103 
00104     return false;
00105 }
00106 
00112 GLViewer_Object* GLViewer_MimeData::getObject( QByteArray theArray, QString theType )
00113 {
00114     if( !theArray.isEmpty() )
00115     {
00116         if( theType == "GLViewer_MarkerSet" )
00117         {
00118             GLViewer_MarkerSet* aObject = new GLViewer_MarkerSet(  0, (float)0.0, 0  );
00119             if( aObject->initializeFromByteCopy( theArray ) )
00120                 return aObject;
00121         }
00122         else if ( theType == "GLViewer_Polyline" )
00123         {
00124             GLViewer_Polyline* aObject = new GLViewer_Polyline( 0, (float)0.0, 0 );
00125             if( aObject->initializeFromByteCopy( theArray ) )
00126                 return aObject;
00127         }
00128         else if( theType == "GLViewer_TextObject" )
00129         {
00130             GLViewer_TextObject* aObject = new GLViewer_TextObject( 0, 0, 0, QColor(255,255,255), 0 );
00131             if( aObject->initializeFromByteCopy( theArray ) )
00132                 return aObject;
00133         }
00134     }        
00135     
00136     return NULL;
00137 }
00138 
00144 QList<GLViewer_Object*> GLViewer_MimeData::getObjects( QByteArray theArray, QString theType )
00145 {
00146     if( !theArray.isEmpty() )
00147     {
00148         int anISize = sizeof( int );
00149         if( theType == "GLViewer_Objects" )
00150         {
00151             QStringList aTypeList;
00152             QList<QByteArray> aObjects;
00153             QList<GLViewer_Object*> aObjectList;
00154 
00155             QList<int> aTypeSizeList;
00156             QList<int> aObjSizeList;
00157             int aObjNum = 0;
00158             char* aPointer = (char*)&aObjNum;
00159 
00160             int anIndex = 0, j = 0;
00161             for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
00162                 *aPointer = theArray[anIndex];
00163             
00164             for( j = 0; j < aObjNum; j++ )
00165             {
00166                 int aTempVal = 0;
00167                 aPointer = (char*)&aTempVal;
00168                 for( ; anIndex < anISize*(j+2); anIndex++, aPointer++ )
00169                     *aPointer = theArray[anIndex];
00170                 aTypeSizeList.append( aTempVal );
00171             }
00172             
00173             int aCurIndex = anIndex;
00174             for( j = 0; j < aObjNum; j++ )
00175             {
00176                 QString aTempStr;
00177                 for( ; anIndex < aCurIndex + aTypeSizeList[j]; anIndex++ )
00178                 {    
00179                     char aLetter = theArray[anIndex];
00180                     aTempStr.append( aLetter );
00181                 }
00182                 aTypeList.append( aTempStr );
00183                 aCurIndex = anIndex;
00184             }
00185 
00186             for( j = 0; j < aObjNum; j++ )
00187             {
00188                 int aTempVal = 0;
00189                 aPointer = (char*)&aTempVal;
00190                 for( ; anIndex < aCurIndex + anISize*(j+1); anIndex++, aPointer++ )
00191                     *aPointer = theArray[anIndex];
00192                 aObjSizeList.append( aTempVal );
00193             }
00194 
00195             aCurIndex = anIndex;
00196             for( j = 0; j < aObjNum; j++ )
00197             {
00198                 QByteArray aTempArray;
00199                 aTempArray.resize(aObjSizeList[j]);
00200                 for( ; anIndex < aCurIndex + aObjSizeList[j]; anIndex++ )
00201                     aTempArray[anIndex-aCurIndex] = theArray[anIndex];
00202                 aObjects.append( aTempArray );
00203                 aCurIndex = anIndex;
00204             }
00205             
00206             for( j = 0; j < aObjNum; j++ )
00207                 aObjectList.append( getObject( aObjects[j], aTypeList[j] ) );
00208 
00209             return aObjectList;
00210         }
00211     }
00212     
00213     return QList<GLViewer_Object*>();    
00214 }
00215 
00220 const char* GLViewer_MimeData::format( int theIndex ) const
00221 {
00222     switch( theIndex )
00223     {
00224     case 0: return "GLViewer_Objects";
00225     //case 1: return "GLViewer_MarkerSet";
00226     //case 2: return "GLViewer_Polyline";
00227     //case 3: return "GLViewer_TextObject";
00228     default: return 0;
00229     }
00230 
00231 }
00232 
00236 QByteArray GLViewer_MimeData::encodedData( const char* theObjectType ) const
00237 {
00238     if( theObjectType == "GLViewer_Objects" )
00239         return myByteArray;
00240     
00241     return QByteArray();
00242 }