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