1 // Copyright (C) 2005 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.
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
19 // Author : OPEN CASCADE
22 //#include <GLViewerAfx.h>
23 #include "GLViewer_MimeSource.h"
24 #include "GLViewer_BaseObjects.h"
27 //using namespace std;
32 GLViewer_MimeSource::~GLViewer_MimeSource()
37 Translate objects to byte array
38 \param theObjects - list of objects
40 bool GLViewer_MimeSource::setObjects( QValueList<GLViewer_Object*> theObjects )
42 if( !theObjects.empty() )
44 QStringList aObjectsType;
45 QValueList<QByteArray> aObjects;
46 QValueList<GLViewer_Object*>::const_iterator anIt = theObjects.begin();
47 QValueList<GLViewer_Object*>::const_iterator anEndIt = theObjects.end();
50 for( ; anIt != anEndIt; anIt++ )
52 aObjects.append( (*anIt)->getByteCopy() );
53 aObjByteSize += aObjects.last().size();
54 aObjectsType.append( (*anIt)->getObjectType() );
57 int anISize = sizeof( int );
58 QString aTypes = aObjectsType.join("");
59 int aStrByteSize = aTypes.length();
60 int aObjNum = aObjects.count();
62 myByteArray = QByteArray( anISize * (1 + 2*aObjNum) + aStrByteSize + aObjByteSize );
64 int anIndex = 0, j = 0;
65 char* aPointer = (char*)&aObjNum;
66 for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
67 myByteArray[anIndex] = *aPointer;
69 QStringList::const_iterator aStrIt = aObjectsType.begin();
70 QStringList::const_iterator aEndStrIt = aObjectsType.end();
71 for( j = 1; aStrIt != aEndStrIt; aStrIt++, j++ )
73 int aStrLen = (*aStrIt).length();
74 aPointer = (char*)&aStrLen;
75 for( ; anIndex < anISize*( 1 + j ); anIndex++, aPointer++ )
76 myByteArray[anIndex] = *aPointer;
79 int aCurIndex = anIndex;
80 const char* aStr = aTypes.data();
82 for( j = 0 ; anIndex < aCurIndex + aStrByteSize; aPointer++, anIndex++, j++ )
83 myByteArray[anIndex] = aStr[j];
86 QValueList<QByteArray>::iterator anObjIt = aObjects.begin();
87 QValueList<QByteArray>::iterator anEndObjIt = aObjects.end();
88 for( j = 1; anObjIt != anEndObjIt; anObjIt++, j++ )
90 int aObjLen = (int)((*anObjIt).size());
91 aPointer = (char*)&aObjLen;
92 for( ; anIndex < aCurIndex + anISize*j; anIndex++, aPointer++ )
93 myByteArray[anIndex] = *aPointer;
97 anObjIt = aObjects.begin();
99 for( ; anObjIt != anEndObjIt; anObjIt++ )
101 int aObjLen = (int)((*anObjIt).size());
102 for( j = 0 ; anIndex < aCurIndex + aObjLen; anIndex++, aPointer++, j++ )
103 myByteArray[anIndex] = (*anObjIt)[j];
114 Creates object by it's representation (byte array)
115 \param theArray - byte array
116 \param theType - type of object
118 GLViewer_Object* GLViewer_MimeSource::getObject( QByteArray theArray, QString theType )
120 if( !theArray.isEmpty() )
122 if( theType == "GLViewer_MarkerSet" )
124 GLViewer_MarkerSet* aObject = new GLViewer_MarkerSet( 0, (float)0.0, 0 );
125 if( aObject->initializeFromByteCopy( theArray ) )
128 else if ( theType == "GLViewer_Polyline" )
130 GLViewer_Polyline* aObject = new GLViewer_Polyline( 0, (float)0.0, 0 );
131 if( aObject->initializeFromByteCopy( theArray ) )
134 else if( theType == "GLViewer_TextObject" )
136 GLViewer_TextObject* aObject = new GLViewer_TextObject( 0, 0, 0, QColor(255,255,255), 0 );
137 if( aObject->initializeFromByteCopy( theArray ) )
146 Creates list of objects by its representation (byte array)
147 \param theArray - byte array
148 \param theType - type of object
150 QValueList<GLViewer_Object*> GLViewer_MimeSource::getObjects( QByteArray theArray, QString theType )
152 if( !theArray.isEmpty() )
154 int anISize = sizeof( int );
155 if( theType == "GLViewer_Objects" )
157 QStringList aTypeList;
158 QValueList<QByteArray> aObjects;
159 QValueList<GLViewer_Object*> aObjectList;
161 QValueList<int> aTypeSizeList;
162 QValueList<int> aObjSizeList;
164 char* aPointer = (char*)&aObjNum;
166 int anIndex = 0, j = 0;
167 for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
168 *aPointer = theArray[anIndex];
170 for( j = 0; j < aObjNum; j++ )
173 aPointer = (char*)&aTempVal;
174 for( ; anIndex < anISize*(j+2); anIndex++, aPointer++ )
175 *aPointer = theArray[anIndex];
176 aTypeSizeList.append( aTempVal );
179 int aCurIndex = anIndex;
180 for( j = 0; j < aObjNum; j++ )
183 for( ; anIndex < aCurIndex + aTypeSizeList[j]; anIndex++ )
185 char aLetter = theArray[anIndex];
186 aTempStr.append( aLetter );
188 aTypeList.append( aTempStr );
192 for( j = 0; j < aObjNum; j++ )
195 aPointer = (char*)&aTempVal;
196 for( ; anIndex < aCurIndex + anISize*(j+1); anIndex++, aPointer++ )
197 *aPointer = theArray[anIndex];
198 aObjSizeList.append( aTempVal );
202 for( j = 0; j < aObjNum; j++ )
204 QByteArray aTempArray(aObjSizeList[j]);
205 for( ; anIndex < aCurIndex + aObjSizeList[j]; anIndex++ )
206 aTempArray[anIndex-aCurIndex] = theArray[anIndex];
207 aObjects.append( aTempArray );
211 for( j = 0; j < aObjNum; j++ )
212 aObjectList.append( getObject( aObjects[j], aTypeList[j] ) );
218 return QValueList<GLViewer_Object*>();
222 \return format by index
223 \param theIndex - index
225 const char* GLViewer_MimeSource::format( int theIndex ) const
229 case 0: return "GLViewer_Objects";
230 //case 1: return "GLViewer_MarkerSet";
231 //case 2: return "GLViewer_Polyline";
232 //case 3: return "GLViewer_TextObject";
239 \return internal byte array
241 QByteArray GLViewer_MimeSource::encodedData( const char* theObjectType ) const
243 if( theObjectType == "GLViewer_Objects" )