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