Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / GLViewer / GLViewer_Object.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_Object.h"
24 #include "GLViewer_Drawer.h"
25 #include "GLViewer_AspectLine.h"
26 #include "GLViewer_Geom.h"
27 #include "GLViewer_Text.h"
28 #include "GLViewer_Group.h"
29
30 //#include <cmath>
31 //using namespace std;
32
33 /*!
34   Default constructor
35 */
36 GLViewer_Object::GLViewer_Object()
37 {
38   myXScale = 1.0; 
39   myYScale = 1.0;
40   myXGap = 0;
41   myYGap = 0;
42   myZoom = 1.0;
43
44   myIsHigh = GL_FALSE;
45   myIsSel = GL_FALSE;
46   
47   myRect = new GLViewer_Rect();;  
48   myUpdateRect = new GLViewer_Rect();;  
49   myGLText = new GLViewer_Text( 0, 0, 0, QColor(0,0,0) );
50
51   myAspectLine = new GLViewer_AspectLine();
52   myType = "GLViewer_Object";
53
54   myOwner = NULL;
55   myDrawer = NULL;
56
57   myIsVisible = true;
58
59   isToolTipHTML = false;  
60
61   myGroup = NULL;
62 }
63
64 /*!
65   Destructor
66 */
67 GLViewer_Object::~GLViewer_Object()
68 {
69   if( myRect )
70     delete myRect;
71
72   if( myUpdateRect )
73     delete myUpdateRect;
74
75   if( myGLText )
76     delete myGLText;
77
78   if( myAspectLine )
79     delete myAspectLine;
80 }
81
82 /*!
83   \return priority of object
84 */
85 int GLViewer_Object::getPriority() const
86 {
87     return myDrawer ? myDrawer->getPriority() : 0;
88 }
89
90 /*!
91   \return true if object is inside rectangle
92   \param theRect - rectangle
93 */
94 GLboolean GLViewer_Object::isInside( GLViewer_Rect theRect )
95 {
96     return theRect.toQRect().contains( myRect->toQRect() );
97 }
98
99 /*!
100   Sets zoom factor
101   \param zoom - zoom factor
102 */
103 GLboolean GLViewer_Object::setZoom( GLfloat zoom, bool, bool )
104 {
105     if( myZoom == zoom )
106         return GL_FALSE;
107
108     myZoom = zoom;
109     return GL_TRUE;
110 }
111
112 /*!
113   Performs zoom change by step
114   \param zoomIn - to increase to decrease zoom
115 */
116 GLboolean GLViewer_Object::updateZoom( bool zoomIn )
117 {
118     float newZoom;
119     float step = zoomIn ? 1 : -1;
120     double epsilon = 0.001;
121
122     if( myZoom - 1 > epsilon )
123         newZoom = ( myZoom * 2 + step ) / 2;
124     else if( 1 - myZoom > epsilon )
125         newZoom = 2 / ( 2 / myZoom - step );
126     else
127         newZoom = zoomIn ? 3./2. : 2./3.;
128
129     if( newZoom < 0.01 || newZoom > 100.0 )
130         return GL_FALSE;
131
132     return setZoom( newZoom, true );
133 }
134
135 /*!
136   Codes object as byte copy
137   \return byte array
138 */
139 QByteArray GLViewer_Object::getByteCopy()
140 {
141     int i = 0;
142     int anISize = sizeof( int );
143
144     const char* aTypeStr = myType.data();
145     const char* aToolTipStr = myToolTipText.data();
146
147     int aTypeLength = myType.length();
148     int aToolTipLength = myToolTipText.length();
149
150
151     QByteArray aGLText = myGLText->getByteCopy();
152     QByteArray aAspect = myAspectLine->getByteCopy();
153     
154     float aRectData[8];
155     aRectData[ 0 ] = myRect->left();
156     aRectData[ 1 ] = myRect->top();
157     aRectData[ 2 ] = myRect->right();
158     aRectData[ 3 ] = myRect->bottom();
159     aRectData[ 4 ] = myXScale;
160     aRectData[ 5 ] = myYScale;
161     aRectData[ 6 ] = myXGap;
162     aRectData[ 7 ] = myYGap;
163     
164     int sizeOf8Float = sizeof( aRectData );
165
166     QByteArray aResult( 2*anISize + sizeOf8Float + 
167                         aTypeLength + aToolTipLength +
168                         aGLText.size() + aAspect.size() );
169     // puts 8 float values into the byte array
170     char* aPointer = (char*)&aRectData;
171     for( i = 0; i < sizeOf8Float; i++, aPointer++ )
172         aResult[i] = *aPointer;
173     // puts length of type string
174     aPointer = (char*)&aTypeLength;
175     for( ; i < anISize + sizeOf8Float; i++, aPointer++ )
176         aResult[i] = *aPointer;
177     // puts type string
178     for( ; i < anISize + sizeOf8Float + aTypeLength; i++ )
179         aResult[i] = aTypeStr[i - anISize - sizeOf8Float ];
180     // puts length of tooltiptext string
181     aPointer = (char*)&aToolTipLength;
182     for( ; i < 2*anISize + sizeOf8Float + aTypeLength; i++, aPointer++ )
183         aResult[i] = *aPointer;
184     // puts tooltiptext string
185     for( ; i <  2*anISize + sizeOf8Float + aTypeLength + aToolTipLength; i++ )
186         aResult[ i] = aToolTipStr[i - 2*anISize - sizeOf8Float - aTypeLength];
187
188     int aCurPos = 2*anISize + sizeOf8Float + aTypeLength + aToolTipLength;
189     // adds aspect byte array
190     for ( i = aCurPos; i < (int)( aCurPos + aAspect.size() ); i++ )
191         aResult[i] = aAspect[i - aCurPos];
192
193     aCurPos = aCurPos + aAspect.size();
194     // adds GL text byte array
195     for ( i = aCurPos; i < (int)( aCurPos + aGLText.size() ); i++ )
196         aResult[i] = aGLText[i - aCurPos];    
197
198     aCurPos += aGLText.size();
199     aPointer = (char*)&myOwner;
200     for( i = 0; i < sizeof( SUIT_DataOwner* ); i++, aPointer++ )
201         aResult[ aCurPos + i ] = *aPointer;
202
203     return aResult;
204 }
205
206 /*!
207   Initialize object by byte array
208   \param theArray - byte array
209 */
210 bool GLViewer_Object::initializeFromByteCopy( QByteArray theArray )
211 {
212     int i = 0;
213     int anISize = sizeof( int );
214     int aFSize = sizeof( GLfloat );
215     
216     float aLeft = 0, aTop = 0, aRight = 0, aBottom = 0;    
217
218     //QString aTypeStr, aToolTipStr;
219     int aTypeLength = 0, aToolTipLength = 0;
220
221     int aSize = theArray.size();
222
223     GLViewer_Text* aGLText = new GLViewer_Text( 0, 0, 0, QColor(255,255,255));
224     int aGLTextMinSize = (aGLText->getByteCopy()).size();
225     GLViewer_AspectLine* aAspectLine = new GLViewer_AspectLine();
226     int aGLAspLineSize = (aAspectLine->getByteCopy()).size();
227
228     QByteArray aGLTextArray, aAspect( aGLAspLineSize );
229
230     if( aSize < 2*anISize + 8*aFSize + aGLTextMinSize + aGLAspLineSize )
231         return false;
232
233     char* aPointer = (char*)&aLeft;
234     for( i = 0; i < aFSize; i++, aPointer++ )
235         *aPointer = theArray[i];
236     aPointer = (char*)&aTop;
237     for( ; i < 2*aFSize; i++, aPointer++ )
238         *aPointer = theArray[i];
239     aPointer = (char*)&aRight;
240     for( ; i < 3*aFSize; i++, aPointer++ )
241         *aPointer = theArray[i];
242     aPointer = (char*)&aBottom;
243     for( ; i < 4*aFSize; i++, aPointer++ )
244         *aPointer = theArray[i];
245
246     //myRect = new QRect( aLeft, aTop, aRight - aLeft, aBottom - aTop );
247     myRect = new GLViewer_Rect( aLeft, aRight, aTop, aBottom );
248
249     aPointer = (char*)&myXScale;
250     for( ; i < 5*aFSize; i++, aPointer++ )
251         *aPointer = theArray[i];
252     aPointer = (char*)&myYScale;
253     for( ; i < 6*aFSize; i++, aPointer++ )
254         *aPointer = theArray[i];
255     aPointer = (char*)&myXGap;
256     for( ; i < 7*aFSize; i++, aPointer++ )
257         *aPointer = theArray[i];
258     aPointer = (char*)&myYGap;
259     for( ; i < 8*aFSize; i++, aPointer++ )
260         *aPointer = theArray[i];
261
262     myIsHigh = false;
263     myIsSel = false;
264     myIsVisible = true;
265
266     aPointer = (char*)&aTypeLength;
267     for( ; i < anISize + 8*aFSize; i++, aPointer++ )
268         *aPointer = theArray[i];
269     myType = "";
270     for( ; i < anISize + 8*aFSize + aTypeLength; i++ )
271     {
272         QChar aChar( theArray[i] );
273         myType += aChar;
274     }
275
276     aPointer = (char*)&aToolTipLength;
277     for( ; i < 2*anISize + 8*aFSize + aTypeLength; i++, aPointer++ )
278         *aPointer = theArray[i];
279     myToolTipText= "";
280     for( ; i < 2*anISize + 8*aFSize + aTypeLength + aToolTipLength; i++ )
281     {
282         QChar aChar( theArray[i] );
283         myToolTipText += aChar;
284     }
285     
286     int aCurPos = 2*anISize + 8*aFSize + aTypeLength + aToolTipLength;
287     if( aSize - aCurPos < aGLTextMinSize + aGLAspLineSize )
288         return false;
289
290     for( i = 0; i < aGLAspLineSize; i++ )
291         aAspect[i] = theArray[ aCurPos + i ];
292     myAspectLine = GLViewer_AspectLine::fromByteCopy( aAspect );
293
294     aCurPos = aCurPos + aGLAspLineSize;
295     aGLTextArray.resize( aSize - aCurPos );
296     for( i = 0; i + aCurPos < aSize; i++ )
297         aGLTextArray[i] = theArray[ aCurPos + i ];
298     // replace gl_text pointer by other
299     if ( myGLText )
300       delete myGLText;
301     myGLText = GLViewer_Text::fromByteCopy( aGLTextArray );
302     
303     return true;        
304 }
305
306 /*!
307   Sets object's group
308   \param theGroup - group
309 */
310 void GLViewer_Object::setGroup( GLViewer_Group* theGroup )
311 {
312   if ( myGroup == theGroup )
313     return;
314
315   if( myGroup )
316     myGroup->removeObject( this );
317   
318   myGroup = theGroup;
319   if( theGroup )
320     myGroup->addObject( this );
321 }
322
323 /*!
324   \return object's group
325 */
326 GLViewer_Group* GLViewer_Object::getGroup() const
327 {
328   return myGroup;
329 }