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