Salome HOME
PAL10125 - by double click on reference original object becomes selected
[modules/gui.git] / src / GLViewer / GLViewer_MimeSource.cxx
1 /***************************************************************************
2 **  Class:   GLViewer_MimeSource
3 **  Descr:   Needs for a work with QClipboard
4 **  Module:  GLViewer
5 **  Created: UI team, 22.03.04
6 ****************************************************************************/
7
8 //#include <GLViewerAfx.h>
9 #include "GLViewer_MimeSource.h"
10 #include "GLViewer_BaseObjects.h"
11
12 //#include <cmath>
13 //using namespace std;
14
15 GLViewer_MimeSource::~GLViewer_MimeSource()
16 {
17 }
18
19 bool GLViewer_MimeSource::setObjects( QValueList<GLViewer_Object*> theObjects )
20 {
21     if( !theObjects.empty() )
22     {
23         QStringList aObjectsType;
24         QValueList<QByteArray> aObjects;
25         QValueList<GLViewer_Object*>::const_iterator anIt = theObjects.begin();
26         QValueList<GLViewer_Object*>::const_iterator anEndIt = theObjects.end();
27
28         int aObjByteSize = 0;
29         for( ; anIt != anEndIt; anIt++ )
30         {
31             aObjects.append( (*anIt)->getByteCopy() );
32             aObjByteSize += aObjects.last().size();
33             aObjectsType.append( (*anIt)->getObjectType() );
34         }
35
36         int anISize = sizeof( int );
37         QString aTypes = aObjectsType.join("");
38         int aStrByteSize = aTypes.length();
39         int aObjNum = aObjects.count();
40
41         myByteArray = QByteArray( anISize * (1 + 2*aObjNum) + aStrByteSize + aObjByteSize );
42
43         int anIndex = 0, j = 0;
44         char* aPointer = (char*)&aObjNum;
45         for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
46             myByteArray[anIndex] = *aPointer;
47         
48         QStringList::const_iterator aStrIt = aObjectsType.begin();
49         QStringList::const_iterator aEndStrIt = aObjectsType.end();
50         for( j = 1; aStrIt != aEndStrIt; aStrIt++, j++ )
51         {
52             int aStrLen = (*aStrIt).length();
53             aPointer = (char*)&aStrLen;
54             for( ; anIndex < anISize*( 1 + j ); anIndex++, aPointer++ )
55                 myByteArray[anIndex] = *aPointer;
56         }
57
58         int aCurIndex = anIndex;
59         const char* aStr = aTypes.data();
60
61         for( j = 0 ; anIndex < aCurIndex + aStrByteSize; aPointer++, anIndex++, j++ )
62             myByteArray[anIndex] = aStr[j];
63
64         aCurIndex = anIndex;
65         QValueList<QByteArray>::iterator anObjIt = aObjects.begin();
66         QValueList<QByteArray>::iterator anEndObjIt = aObjects.end();
67         for( j = 1; anObjIt != anEndObjIt; anObjIt++, j++ )
68         {
69             int aObjLen = (int)((*anObjIt).size());
70             aPointer = (char*)&aObjLen;
71             for( ; anIndex < aCurIndex + anISize*j; anIndex++, aPointer++ )
72                 myByteArray[anIndex] = *aPointer;
73         }
74
75         aCurIndex = anIndex;
76         anObjIt = aObjects.begin();
77
78         for( ; anObjIt != anEndObjIt; anObjIt++ )
79         {
80             int aObjLen = (int)((*anObjIt).size());
81             for( j = 0 ; anIndex < aCurIndex + aObjLen; anIndex++, aPointer++, j++ )
82                 myByteArray[anIndex] = (*anObjIt)[j];
83             aCurIndex = anIndex;
84         }
85      
86         return true;
87     }
88
89     return false;
90 }
91 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
92 //If you want to use new class, following two method must be redefined
93 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
94 GLViewer_Object* GLViewer_MimeSource::getObject( QByteArray theArray, QString theType )
95 {
96     if( !theArray.isEmpty() )
97     {
98         if( theType == "GLViewer_MarkerSet" )
99         {
100             GLViewer_MarkerSet* aObject = new GLViewer_MarkerSet(  0, (float)0.0, 0  );
101             if( aObject->initializeFromByteCopy( theArray ) )
102                 return aObject;
103         }
104         else if ( theType == "GLViewer_Polyline" )
105         {
106             GLViewer_Polyline* aObject = new GLViewer_Polyline( 0, (float)0.0, 0 );
107             if( aObject->initializeFromByteCopy( theArray ) )
108                 return aObject;
109         }
110         else if( theType == "GLViewer_TextObject" )
111         {
112             GLViewer_TextObject* aObject = new GLViewer_TextObject( 0, 0, 0, QColor(255,255,255), 0 );
113             if( aObject->initializeFromByteCopy( theArray ) )
114                 return aObject;
115         }
116     }        
117     
118     return NULL;
119 }
120
121 QValueList<GLViewer_Object*> GLViewer_MimeSource::getObjects( QByteArray theArray, QString theType )
122 {
123     if( !theArray.isEmpty() )
124     {
125         int anISize = sizeof( int );
126         if( theType == "GLViewer_Objects" )
127         {
128             QStringList aTypeList;
129             QValueList<QByteArray> aObjects;
130             QValueList<GLViewer_Object*> aObjectList;
131
132             QValueList<int> aTypeSizeList;
133             QValueList<int> aObjSizeList;
134             int aObjNum = 0;
135             char* aPointer = (char*)&aObjNum;
136
137             int anIndex = 0, j = 0;
138             for( anIndex = 0; anIndex < anISize; anIndex++, aPointer++ )
139                 *aPointer = theArray[anIndex];
140             
141             for( j = 0; j < aObjNum; j++ )
142             {
143                 int aTempVal = 0;
144                 aPointer = (char*)&aTempVal;
145                 for( ; anIndex < anISize*(j+2); anIndex++, aPointer++ )
146                     *aPointer = theArray[anIndex];
147                 aTypeSizeList.append( aTempVal );
148             }
149             
150             int aCurIndex = anIndex;
151             for( j = 0; j < aObjNum; j++ )
152             {
153                 QString aTempStr;
154                 for( ; anIndex < aCurIndex + aTypeSizeList[j]; anIndex++ )
155                 {    
156                     char aLetter = theArray[anIndex];
157                     aTempStr.append( aLetter );
158                 }
159                 aTypeList.append( aTempStr );
160                 aCurIndex = anIndex;
161             }
162
163             for( j = 0; j < aObjNum; j++ )
164             {
165                 int aTempVal = 0;
166                 aPointer = (char*)&aTempVal;
167                 for( ; anIndex < aCurIndex + anISize*(j+1); anIndex++, aPointer++ )
168                     *aPointer = theArray[anIndex];
169                 aObjSizeList.append( aTempVal );
170             }
171
172             aCurIndex = anIndex;
173             for( j = 0; j < aObjNum; j++ )
174             {
175                 QByteArray aTempArray(aObjSizeList[j]);
176                 for( ; anIndex < aCurIndex + aObjSizeList[j]; anIndex++ )
177                     aTempArray[anIndex-aCurIndex] = theArray[anIndex];
178                 aObjects.append( aTempArray );
179                 aCurIndex = anIndex;
180             }
181             
182             for( j = 0; j < aObjNum; j++ )
183                 aObjectList.append( getObject( aObjects[j], aTypeList[j] ) );
184
185             return aObjectList;
186         }
187     }
188     
189     return QValueList<GLViewer_Object*>();    
190 }
191
192 const char* GLViewer_MimeSource::format( int theIndex ) const
193 {
194     switch( theIndex )
195     {
196     case 0: return "GLViewer_Objects";
197     //case 1: return "GLViewer_MarkerSet";
198     //case 2: return "GLViewer_Polyline";
199     //case 3: return "GLViewer_TextObject";
200     default: return 0;
201     }
202
203 }
204
205 QByteArray GLViewer_MimeSource::encodedData( const char* theObjectType ) const
206 {
207     if( theObjectType == "GLViewer_Objects" )
208         return myByteArray;
209     
210     return QByteArray();
211 }