1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GLViewer_MimeData.h"
21 #include "GLViewer_BaseObjects.h"
26 GLViewer_MimeData::~GLViewer_MimeData()
31 Translate objects to byte array
32 \param theObjects - list of objects
34 bool GLViewer_MimeData::setObjects( QList<GLViewer_Object*> theObjects )
36 if( !theObjects.empty() )
38 QStringList aObjectsType;
39 QList<QByteArray> aObjects;
40 QList<GLViewer_Object*>::const_iterator anIt = theObjects.begin();
41 QList<GLViewer_Object*>::const_iterator anEndIt = theObjects.end();
44 for( ; anIt != anEndIt; anIt++ )
46 aObjects.append( (*anIt)->getByteCopy() );
47 aObjByteSize += aObjects.last().size();
48 aObjectsType.append( (*anIt)->getObjectType() );
51 int anISize = sizeof( int );
52 QString aTypes = aObjectsType.join("");
53 int aStrByteSize = aTypes.length();
54 int aObjNum = aObjects.count();
56 myByteArray.resize( anISize * (1 + 2*aObjNum) + aStrByteSize + aObjByteSize );
58 int anIndex = 0, j = 0;
59 char* aPointer = (char*)&aObjNum;
60 for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
61 myByteArray[anIndex] = *aPointer;
63 QStringList::const_iterator aStrIt = aObjectsType.begin();
64 QStringList::const_iterator aEndStrIt = aObjectsType.end();
65 for( j = 1; aStrIt != aEndStrIt; aStrIt++, j++ )
67 int aStrLen = (*aStrIt).length();
68 aPointer = (char*)&aStrLen;
69 for( ; anIndex < anISize*( 1 + j ); anIndex++, aPointer++ )
70 myByteArray[anIndex] = *aPointer;
73 int aCurIndex = anIndex;
74 const char* aStr = aTypes.toLatin1().constData();
76 for( j = 0 ; anIndex < aCurIndex + aStrByteSize; aPointer++, anIndex++, j++ )
77 myByteArray[anIndex] = aStr[j];
80 QList<QByteArray>::iterator anObjIt = aObjects.begin();
81 QList<QByteArray>::iterator anEndObjIt = aObjects.end();
82 for( j = 1; anObjIt != anEndObjIt; anObjIt++, j++ )
84 int aObjLen = (int)((*anObjIt).size());
85 aPointer = (char*)&aObjLen;
86 for( ; anIndex < aCurIndex + anISize*j; anIndex++, aPointer++ )
87 myByteArray[anIndex] = *aPointer;
91 anObjIt = aObjects.begin();
93 for( ; anObjIt != anEndObjIt; anObjIt++ )
95 int aObjLen = (int)((*anObjIt).size());
96 for( j = 0 ; anIndex < aCurIndex + aObjLen; anIndex++, aPointer++, j++ )
97 myByteArray[anIndex] = (*anObjIt)[j];
108 Creates object by it's representation (byte array)
109 \param theArray - byte array
110 \param theType - type of object
112 GLViewer_Object* GLViewer_MimeData::getObject( QByteArray theArray, QString theType )
114 if( !theArray.isEmpty() )
116 if( theType == "GLViewer_MarkerSet" )
118 GLViewer_MarkerSet* aObject = new GLViewer_MarkerSet( 0, (float)0.0, 0 );
119 if( aObject->initializeFromByteCopy( theArray ) )
122 else if ( theType == "GLViewer_Polyline" )
124 GLViewer_Polyline* aObject = new GLViewer_Polyline( 0, (float)0.0, 0 );
125 if( aObject->initializeFromByteCopy( theArray ) )
128 else if( theType == "GLViewer_TextObject" )
130 GLViewer_TextObject* aObject = new GLViewer_TextObject( 0, 0, 0, QColor(255,255,255), 0 );
131 if( aObject->initializeFromByteCopy( theArray ) )
140 Creates list of objects by its representation (byte array)
141 \param theArray - byte array
142 \param theType - type of object
144 QList<GLViewer_Object*> GLViewer_MimeData::getObjects( QByteArray theArray, QString theType )
146 if( !theArray.isEmpty() )
148 int anISize = sizeof( int );
149 if( theType == "GLViewer_Objects" )
151 QStringList aTypeList;
152 QList<QByteArray> aObjects;
153 QList<GLViewer_Object*> aObjectList;
155 QList<int> aTypeSizeList;
156 QList<int> aObjSizeList;
158 char* aPointer = (char*)&aObjNum;
160 int anIndex = 0, j = 0;
161 for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
162 *aPointer = theArray[anIndex];
164 for( j = 0; j < aObjNum; j++ )
167 aPointer = (char*)&aTempVal;
168 for( ; anIndex < anISize*(j+2); anIndex++, aPointer++ )
169 *aPointer = theArray[anIndex];
170 aTypeSizeList.append( aTempVal );
173 int aCurIndex = anIndex;
174 for( j = 0; j < aObjNum; j++ )
177 for( ; anIndex < aCurIndex + aTypeSizeList[j]; anIndex++ )
179 char aLetter = theArray[anIndex];
180 aTempStr.append( aLetter );
182 aTypeList.append( aTempStr );
186 for( j = 0; j < aObjNum; j++ )
189 aPointer = (char*)&aTempVal;
190 for( ; anIndex < aCurIndex + anISize*(j+1); anIndex++, aPointer++ )
191 *aPointer = theArray[anIndex];
192 aObjSizeList.append( aTempVal );
196 for( j = 0; j < aObjNum; j++ )
198 QByteArray aTempArray;
199 aTempArray.resize(aObjSizeList[j]);
200 for( ; anIndex < aCurIndex + aObjSizeList[j]; anIndex++ )
201 aTempArray[anIndex-aCurIndex] = theArray[anIndex];
202 aObjects.append( aTempArray );
206 for( j = 0; j < aObjNum; j++ )
207 aObjectList.append( getObject( aObjects[j], aTypeList[j] ) );
213 return QList<GLViewer_Object*>();
217 \return format by index
218 \param theIndex - index
220 const char* GLViewer_MimeData::format( int theIndex ) const
224 case 0: return "GLViewer_Objects";
225 //case 1: return "GLViewer_MarkerSet";
226 //case 2: return "GLViewer_Polyline";
227 //case 3: return "GLViewer_TextObject";
234 \return internal byte array
236 QByteArray GLViewer_MimeData::encodedData( const char* theObjectType ) const
238 if( theObjectType == "GLViewer_Objects" )