]> SALOME platform Git repositories - modules/gui.git/blob - src/GLViewer/GLViewer_MimeSource.cxx
Salome HOME
This file is given from DESCARTES project
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
18 //
19 //  Author : OPEN CASCADE
20 //
21
22 /***************************************************************************
23 **  Class:   GLViewer_MimeSource
24 **  Descr:   Needs for a work with QClipboard
25 **  Module:  GLViewer
26 **  Created: UI team, 22.03.04
27 ****************************************************************************/
28
29 //#include <GLViewerAfx.h>
30 #include "GLViewer_MimeSource.h"
31 #include "GLViewer_BaseObjects.h"
32
33 //#include <cmath>
34 //using namespace std;
35
36 GLViewer_MimeSource::~GLViewer_MimeSource()
37 {
38 }
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 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
113 //If you want to use new class, following two method must be redefined
114 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
115 GLViewer_Object* GLViewer_MimeSource::getObject( QByteArray theArray, QString theType )
116 {
117     if( !theArray.isEmpty() )
118     {
119         if( theType == "GLViewer_MarkerSet" )
120         {
121             GLViewer_MarkerSet* aObject = new GLViewer_MarkerSet(  0, (float)0.0, 0  );
122             if( aObject->initializeFromByteCopy( theArray ) )
123                 return aObject;
124         }
125         else if ( theType == "GLViewer_Polyline" )
126         {
127             GLViewer_Polyline* aObject = new GLViewer_Polyline( 0, (float)0.0, 0 );
128             if( aObject->initializeFromByteCopy( theArray ) )
129                 return aObject;
130         }
131         else if( theType == "GLViewer_TextObject" )
132         {
133             GLViewer_TextObject* aObject = new GLViewer_TextObject( 0, 0, 0, QColor(255,255,255), 0 );
134             if( aObject->initializeFromByteCopy( theArray ) )
135                 return aObject;
136         }
137     }        
138     
139     return NULL;
140 }
141
142 QValueList<GLViewer_Object*> GLViewer_MimeSource::getObjects( QByteArray theArray, QString theType )
143 {
144     if( !theArray.isEmpty() )
145     {
146         int anISize = sizeof( int );
147         if( theType == "GLViewer_Objects" )
148         {
149             QStringList aTypeList;
150             QValueList<QByteArray> aObjects;
151             QValueList<GLViewer_Object*> aObjectList;
152
153             QValueList<int> aTypeSizeList;
154             QValueList<int> aObjSizeList;
155             int aObjNum = 0;
156             char* aPointer = (char*)&aObjNum;
157
158             int anIndex = 0, j = 0;
159             for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
160                 *aPointer = theArray[anIndex];
161             
162             for( j = 0; j < aObjNum; j++ )
163             {
164                 int aTempVal = 0;
165                 aPointer = (char*)&aTempVal;
166                 for( ; anIndex < anISize*(j+2); anIndex++, aPointer++ )
167                     *aPointer = theArray[anIndex];
168                 aTypeSizeList.append( aTempVal );
169             }
170             
171             int aCurIndex = anIndex;
172             for( j = 0; j < aObjNum; j++ )
173             {
174                 QString aTempStr;
175                 for( ; anIndex < aCurIndex + aTypeSizeList[j]; anIndex++ )
176                 {    
177                     char aLetter = theArray[anIndex];
178                     aTempStr.append( aLetter );
179                 }
180                 aTypeList.append( aTempStr );
181                 aCurIndex = anIndex;
182             }
183
184             for( j = 0; j < aObjNum; j++ )
185             {
186                 int aTempVal = 0;
187                 aPointer = (char*)&aTempVal;
188                 for( ; anIndex < aCurIndex + anISize*(j+1); anIndex++, aPointer++ )
189                     *aPointer = theArray[anIndex];
190                 aObjSizeList.append( aTempVal );
191             }
192
193             aCurIndex = anIndex;
194             for( j = 0; j < aObjNum; j++ )
195             {
196                 QByteArray aTempArray(aObjSizeList[j]);
197                 for( ; anIndex < aCurIndex + aObjSizeList[j]; anIndex++ )
198                     aTempArray[anIndex-aCurIndex] = theArray[anIndex];
199                 aObjects.append( aTempArray );
200                 aCurIndex = anIndex;
201             }
202             
203             for( j = 0; j < aObjNum; j++ )
204                 aObjectList.append( getObject( aObjects[j], aTypeList[j] ) );
205
206             return aObjectList;
207         }
208     }
209     
210     return QValueList<GLViewer_Object*>();    
211 }
212
213 const char* GLViewer_MimeSource::format( int theIndex ) const
214 {
215     switch( theIndex )
216     {
217     case 0: return "GLViewer_Objects";
218     //case 1: return "GLViewer_MarkerSet";
219     //case 2: return "GLViewer_Polyline";
220     //case 3: return "GLViewer_TextObject";
221     default: return 0;
222     }
223
224 }
225
226 QByteArray GLViewer_MimeSource::encodedData( const char* theObjectType ) const
227 {
228     if( theObjectType == "GLViewer_Objects" )
229         return myByteArray;
230     
231     return QByteArray();
232 }