Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / GLViewer / GLViewer_MimeSource.cxx
1 //  Copyright (C) 2005 OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //  Author : OPEN CASCADE
20 //
21
22 //#include <GLViewerAfx.h>
23 #include "GLViewer_MimeSource.h"
24 #include "GLViewer_BaseObjects.h"
25
26 //#include <cmath>
27 //using namespace std;
28
29 /*!
30   Destructor
31 */
32 GLViewer_MimeSource::~GLViewer_MimeSource()
33 {
34 }
35
36 /*!
37   Translate objects to byte array
38   \param theObjects - list of objects
39 */
40 bool GLViewer_MimeSource::setObjects( QValueList<GLViewer_Object*> theObjects )
41 {
42     if( !theObjects.empty() )
43     {
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();
48
49         int aObjByteSize = 0;
50         for( ; anIt != anEndIt; anIt++ )
51         {
52             aObjects.append( (*anIt)->getByteCopy() );
53             aObjByteSize += aObjects.last().size();
54             aObjectsType.append( (*anIt)->getObjectType() );
55         }
56
57         int anISize = sizeof( int );
58         QString aTypes = aObjectsType.join("");
59         int aStrByteSize = aTypes.length();
60         int aObjNum = aObjects.count();
61
62         myByteArray = QByteArray( anISize * (1 + 2*aObjNum) + aStrByteSize + aObjByteSize );
63
64         int anIndex = 0, j = 0;
65         char* aPointer = (char*)&aObjNum;
66         for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
67             myByteArray[anIndex] = *aPointer;
68         
69         QStringList::const_iterator aStrIt = aObjectsType.begin();
70         QStringList::const_iterator aEndStrIt = aObjectsType.end();
71         for( j = 1; aStrIt != aEndStrIt; aStrIt++, j++ )
72         {
73             int aStrLen = (*aStrIt).length();
74             aPointer = (char*)&aStrLen;
75             for( ; anIndex < anISize*( 1 + j ); anIndex++, aPointer++ )
76                 myByteArray[anIndex] = *aPointer;
77         }
78
79         int aCurIndex = anIndex;
80         const char* aStr = aTypes.data();
81
82         for( j = 0 ; anIndex < aCurIndex + aStrByteSize; aPointer++, anIndex++, j++ )
83             myByteArray[anIndex] = aStr[j];
84
85         aCurIndex = anIndex;
86         QValueList<QByteArray>::iterator anObjIt = aObjects.begin();
87         QValueList<QByteArray>::iterator anEndObjIt = aObjects.end();
88         for( j = 1; anObjIt != anEndObjIt; anObjIt++, j++ )
89         {
90             int aObjLen = (int)((*anObjIt).size());
91             aPointer = (char*)&aObjLen;
92             for( ; anIndex < aCurIndex + anISize*j; anIndex++, aPointer++ )
93                 myByteArray[anIndex] = *aPointer;
94         }
95
96         aCurIndex = anIndex;
97         anObjIt = aObjects.begin();
98
99         for( ; anObjIt != anEndObjIt; anObjIt++ )
100         {
101             int aObjLen = (int)((*anObjIt).size());
102             for( j = 0 ; anIndex < aCurIndex + aObjLen; anIndex++, aPointer++, j++ )
103                 myByteArray[anIndex] = (*anObjIt)[j];
104             aCurIndex = anIndex;
105         }
106      
107         return true;
108     }
109
110     return false;
111 }
112
113 /*!
114   Creates object by it's representation (byte array)
115   \param theArray - byte array
116   \param theType - type of object
117 */
118 GLViewer_Object* GLViewer_MimeSource::getObject( QByteArray theArray, QString theType )
119 {
120     if( !theArray.isEmpty() )
121     {
122         if( theType == "GLViewer_MarkerSet" )
123         {
124             GLViewer_MarkerSet* aObject = new GLViewer_MarkerSet(  0, (float)0.0, 0  );
125             if( aObject->initializeFromByteCopy( theArray ) )
126                 return aObject;
127         }
128         else if ( theType == "GLViewer_Polyline" )
129         {
130             GLViewer_Polyline* aObject = new GLViewer_Polyline( 0, (float)0.0, 0 );
131             if( aObject->initializeFromByteCopy( theArray ) )
132                 return aObject;
133         }
134         else if( theType == "GLViewer_TextObject" )
135         {
136             GLViewer_TextObject* aObject = new GLViewer_TextObject( 0, 0, 0, QColor(255,255,255), 0 );
137             if( aObject->initializeFromByteCopy( theArray ) )
138                 return aObject;
139         }
140     }        
141     
142     return NULL;
143 }
144
145 /*!
146   Creates list of objects by its representation (byte array)
147   \param theArray - byte array
148   \param theType - type of object
149 */
150 QValueList<GLViewer_Object*> GLViewer_MimeSource::getObjects( QByteArray theArray, QString theType )
151 {
152     if( !theArray.isEmpty() )
153     {
154         int anISize = sizeof( int );
155         if( theType == "GLViewer_Objects" )
156         {
157             QStringList aTypeList;
158             QValueList<QByteArray> aObjects;
159             QValueList<GLViewer_Object*> aObjectList;
160
161             QValueList<int> aTypeSizeList;
162             QValueList<int> aObjSizeList;
163             int aObjNum = 0;
164             char* aPointer = (char*)&aObjNum;
165
166             int anIndex = 0, j = 0;
167             for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
168                 *aPointer = theArray[anIndex];
169             
170             for( j = 0; j < aObjNum; j++ )
171             {
172                 int aTempVal = 0;
173                 aPointer = (char*)&aTempVal;
174                 for( ; anIndex < anISize*(j+2); anIndex++, aPointer++ )
175                     *aPointer = theArray[anIndex];
176                 aTypeSizeList.append( aTempVal );
177             }
178             
179             int aCurIndex = anIndex;
180             for( j = 0; j < aObjNum; j++ )
181             {
182                 QString aTempStr;
183                 for( ; anIndex < aCurIndex + aTypeSizeList[j]; anIndex++ )
184                 {    
185                     char aLetter = theArray[anIndex];
186                     aTempStr.append( aLetter );
187                 }
188                 aTypeList.append( aTempStr );
189                 aCurIndex = anIndex;
190             }
191
192             for( j = 0; j < aObjNum; j++ )
193             {
194                 int aTempVal = 0;
195                 aPointer = (char*)&aTempVal;
196                 for( ; anIndex < aCurIndex + anISize*(j+1); anIndex++, aPointer++ )
197                     *aPointer = theArray[anIndex];
198                 aObjSizeList.append( aTempVal );
199             }
200
201             aCurIndex = anIndex;
202             for( j = 0; j < aObjNum; j++ )
203             {
204                 QByteArray aTempArray(aObjSizeList[j]);
205                 for( ; anIndex < aCurIndex + aObjSizeList[j]; anIndex++ )
206                     aTempArray[anIndex-aCurIndex] = theArray[anIndex];
207                 aObjects.append( aTempArray );
208                 aCurIndex = anIndex;
209             }
210             
211             for( j = 0; j < aObjNum; j++ )
212                 aObjectList.append( getObject( aObjects[j], aTypeList[j] ) );
213
214             return aObjectList;
215         }
216     }
217     
218     return QValueList<GLViewer_Object*>();    
219 }
220
221 /*!
222   \return format by index
223   \param theIndex - index
224 */
225 const char* GLViewer_MimeSource::format( int theIndex ) const
226 {
227     switch( theIndex )
228     {
229     case 0: return "GLViewer_Objects";
230     //case 1: return "GLViewer_MarkerSet";
231     //case 2: return "GLViewer_Polyline";
232     //case 3: return "GLViewer_TextObject";
233     default: return 0;
234     }
235
236 }
237
238 /*!
239   \return internal byte array
240 */
241 QByteArray GLViewer_MimeSource::encodedData( const char* theObjectType ) const
242 {
243     if( theObjectType == "GLViewer_Objects" )
244         return myByteArray;
245     
246     return QByteArray();
247 }