1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // Author : OPEN CASCADE
23 //#include <GLViewerAfx.h>
25 #include "GLViewer_MimeData.h"
26 #include "GLViewer_BaseObjects.h"
29 //using namespace std;
34 GLViewer_MimeData::~GLViewer_MimeData()
39 Translate objects to byte array
40 \param theObjects - list of objects
42 bool GLViewer_MimeData::setObjects( QList<GLViewer_Object*> theObjects )
44 if( !theObjects.empty() )
46 QStringList aObjectsType;
47 QList<QByteArray> aObjects;
48 QList<GLViewer_Object*>::const_iterator anIt = theObjects.begin();
49 QList<GLViewer_Object*>::const_iterator anEndIt = theObjects.end();
52 for( ; anIt != anEndIt; anIt++ )
54 aObjects.append( (*anIt)->getByteCopy() );
55 aObjByteSize += aObjects.last().size();
56 aObjectsType.append( (*anIt)->getObjectType() );
59 int anISize = sizeof( int );
60 QString aTypes = aObjectsType.join("");
61 int aStrByteSize = aTypes.length();
62 int aObjNum = aObjects.count();
64 myByteArray.resize( anISize * (1 + 2*aObjNum) + aStrByteSize + aObjByteSize );
66 int anIndex = 0, j = 0;
67 char* aPointer = (char*)&aObjNum;
68 for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
69 myByteArray[anIndex] = *aPointer;
71 QStringList::const_iterator aStrIt = aObjectsType.begin();
72 QStringList::const_iterator aEndStrIt = aObjectsType.end();
73 for( j = 1; aStrIt != aEndStrIt; aStrIt++, j++ )
75 int aStrLen = (*aStrIt).length();
76 aPointer = (char*)&aStrLen;
77 for( ; anIndex < anISize*( 1 + j ); anIndex++, aPointer++ )
78 myByteArray[anIndex] = *aPointer;
81 int aCurIndex = anIndex;
82 const char* aStr = aTypes.toLatin1().constData();
84 for( j = 0 ; anIndex < aCurIndex + aStrByteSize; aPointer++, anIndex++, j++ )
85 myByteArray[anIndex] = aStr[j];
88 QList<QByteArray>::iterator anObjIt = aObjects.begin();
89 QList<QByteArray>::iterator anEndObjIt = aObjects.end();
90 for( j = 1; anObjIt != anEndObjIt; anObjIt++, j++ )
92 int aObjLen = (int)((*anObjIt).size());
93 aPointer = (char*)&aObjLen;
94 for( ; anIndex < aCurIndex + anISize*j; anIndex++, aPointer++ )
95 myByteArray[anIndex] = *aPointer;
99 anObjIt = aObjects.begin();
101 for( ; anObjIt != anEndObjIt; anObjIt++ )
103 int aObjLen = (int)((*anObjIt).size());
104 for( j = 0 ; anIndex < aCurIndex + aObjLen; anIndex++, aPointer++, j++ )
105 myByteArray[anIndex] = (*anObjIt)[j];
116 Creates object by it's representation (byte array)
117 \param theArray - byte array
118 \param theType - type of object
120 GLViewer_Object* GLViewer_MimeData::getObject( QByteArray theArray, QString theType )
122 if( !theArray.isEmpty() )
124 if( theType == "GLViewer_MarkerSet" )
126 GLViewer_MarkerSet* aObject = new GLViewer_MarkerSet( 0, (float)0.0, 0 );
127 if( aObject->initializeFromByteCopy( theArray ) )
130 else if ( theType == "GLViewer_Polyline" )
132 GLViewer_Polyline* aObject = new GLViewer_Polyline( 0, (float)0.0, 0 );
133 if( aObject->initializeFromByteCopy( theArray ) )
136 else if( theType == "GLViewer_TextObject" )
138 GLViewer_TextObject* aObject = new GLViewer_TextObject( 0, 0, 0, QColor(255,255,255), 0 );
139 if( aObject->initializeFromByteCopy( theArray ) )
148 Creates list of objects by its representation (byte array)
149 \param theArray - byte array
150 \param theType - type of object
152 QList<GLViewer_Object*> GLViewer_MimeData::getObjects( QByteArray theArray, QString theType )
154 if( !theArray.isEmpty() )
156 int anISize = sizeof( int );
157 if( theType == "GLViewer_Objects" )
159 QStringList aTypeList;
160 QList<QByteArray> aObjects;
161 QList<GLViewer_Object*> aObjectList;
163 QList<int> aTypeSizeList;
164 QList<int> aObjSizeList;
166 char* aPointer = (char*)&aObjNum;
168 int anIndex = 0, j = 0;
169 for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
170 *aPointer = theArray[anIndex];
172 for( j = 0; j < aObjNum; j++ )
175 aPointer = (char*)&aTempVal;
176 for( ; anIndex < anISize*(j+2); anIndex++, aPointer++ )
177 *aPointer = theArray[anIndex];
178 aTypeSizeList.append( aTempVal );
181 int aCurIndex = anIndex;
182 for( j = 0; j < aObjNum; j++ )
185 for( ; anIndex < aCurIndex + aTypeSizeList[j]; anIndex++ )
187 char aLetter = theArray[anIndex];
188 aTempStr.append( aLetter );
190 aTypeList.append( aTempStr );
194 for( j = 0; j < aObjNum; j++ )
197 aPointer = (char*)&aTempVal;
198 for( ; anIndex < aCurIndex + anISize*(j+1); anIndex++, aPointer++ )
199 *aPointer = theArray[anIndex];
200 aObjSizeList.append( aTempVal );
204 for( j = 0; j < aObjNum; j++ )
206 QByteArray aTempArray;
207 aTempArray.resize(aObjSizeList[j]);
208 for( ; anIndex < aCurIndex + aObjSizeList[j]; anIndex++ )
209 aTempArray[anIndex-aCurIndex] = theArray[anIndex];
210 aObjects.append( aTempArray );
214 for( j = 0; j < aObjNum; j++ )
215 aObjectList.append( getObject( aObjects[j], aTypeList[j] ) );
221 return QList<GLViewer_Object*>();
225 \return format by index
226 \param theIndex - index
228 const char* GLViewer_MimeData::format( int theIndex ) const
232 case 0: return "GLViewer_Objects";
233 //case 1: return "GLViewer_MarkerSet";
234 //case 2: return "GLViewer_Polyline";
235 //case 3: return "GLViewer_TextObject";
242 \return internal byte array
244 QByteArray GLViewer_MimeData::encodedData( const char* theObjectType ) const
246 if( theObjectType == "GLViewer_Objects" )