]> SALOME platform Git repositories - modules/gui.git/blob - src/GLViewer/GLViewer_Object.cxx
Salome HOME
6afe3be259603e258319633ae88afe62bd2d3233
[modules/gui.git] / src / GLViewer / GLViewer_Object.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  Author : OPEN CASCADE
23 //#include <GLViewerAfx.h>
24 //
25 #include "GLViewer_Object.h"
26 #include "GLViewer_Drawer.h"
27 #include "GLViewer_AspectLine.h"
28 #include "GLViewer_Text.h"
29 #include "GLViewer_Group.h"
30
31 #include <SUIT_DataOwner.h>
32
33 //#include <cmath>
34 //using namespace std;
35
36 /*!
37   Default constructor
38 */
39 GLViewer_Object::GLViewer_Object()
40 {
41   myXScale = 1.0; 
42   myYScale = 1.0;
43   myXGap = 0;
44   myYGap = 0;
45   myZoom = 1.0;
46
47   myIsHigh = GL_FALSE;
48   myIsSel = GL_FALSE;
49   
50   myRect = new GLViewer_Rect();;  
51   myUpdateRect = new GLViewer_Rect();;  
52   myGLText = new GLViewer_Text( 0, 0, 0, QColor(0,0,0) );
53
54   myAspectLine = new GLViewer_AspectLine();
55   myType = "GLViewer_Object";
56
57   myOwner = NULL;
58   myDrawer = NULL;
59
60   myIsVisible = true;
61
62   isToolTipHTML = false;  
63
64   myGroup = NULL;
65 }
66
67 /*!
68   Destructor
69 */
70 GLViewer_Object::~GLViewer_Object()
71 {
72   if( myRect )
73     delete myRect;
74
75   if( myUpdateRect )
76     delete myUpdateRect;
77
78   if( myGLText )
79     delete myGLText;
80
81   if( myAspectLine )
82     delete myAspectLine;
83 }
84
85 /*!
86   \return priority of object
87 */
88 int GLViewer_Object::getPriority() const
89 {
90     return myDrawer ? myDrawer->getPriority() : 0;
91 }
92
93 /*!
94   \return true if object is inside rectangle
95   \param theRect - rectangle
96 */
97 GLboolean GLViewer_Object::isInside( GLViewer_Rect theRect )
98 {
99     return theRect.toQRect().contains( myRect->toQRect() );
100 }
101
102 /*!
103   Sets zoom factor
104   \param zoom - zoom factor
105 */
106 GLboolean GLViewer_Object::setZoom( GLfloat zoom, bool, bool )
107 {
108     if( myZoom == zoom )
109         return GL_FALSE;
110
111     myZoom = zoom;
112     return GL_TRUE;
113 }
114
115 /*!
116   Performs zoom change by step
117   \param zoomIn - to increase to decrease zoom
118 */
119 GLboolean GLViewer_Object::updateZoom( bool zoomIn )
120 {
121     float newZoom;
122     float step = zoomIn ? 1 : -1;
123     double epsilon = 0.001;
124
125     if( myZoom - 1 > epsilon )
126         newZoom = ( myZoom * 2 + step ) / 2;
127     else if( 1 - myZoom > epsilon )
128         newZoom = 2 / ( 2 / myZoom - step );
129     else
130         newZoom = zoomIn ? 3./2. : 2./3.;
131
132     if( newZoom < 0.01 || newZoom > 100.0 )
133         return GL_FALSE;
134
135     return setZoom( newZoom, true );
136 }
137
138 /*!
139   Codes object as byte copy
140   \return byte array
141 */
142 QByteArray GLViewer_Object::getByteCopy()
143 {
144     int i = 0;
145     int anISize = sizeof( int );
146
147     const char* aTypeStr = myType.toLatin1().constData();
148     const char* aToolTipStr = myToolTipText.toLatin1().constData();
149
150     int aTypeLength = myType.length();
151     int aToolTipLength = myToolTipText.length();
152
153
154     QByteArray aGLText = myGLText->getByteCopy();
155     QByteArray aAspect = myAspectLine->getByteCopy();
156     
157     float aRectData[8];
158     aRectData[ 0 ] = myRect->left();
159     aRectData[ 1 ] = myRect->top();
160     aRectData[ 2 ] = myRect->right();
161     aRectData[ 3 ] = myRect->bottom();
162     aRectData[ 4 ] = myXScale;
163     aRectData[ 5 ] = myYScale;
164     aRectData[ 6 ] = myXGap;
165     aRectData[ 7 ] = myYGap;
166     
167     int sizeOf8Float = sizeof( aRectData );
168
169     QByteArray aResult;
170     aResult.resize( 2*anISize + sizeOf8Float + 
171                     aTypeLength + aToolTipLength +
172                     aGLText.size() + aAspect.size() );
173     // puts 8 float values into the byte array
174     char* aPointer = (char*)&aRectData;
175     for( i = 0; i < sizeOf8Float; i++, aPointer++ )
176         aResult[i] = *aPointer;
177     // puts length of type string
178     aPointer = (char*)&aTypeLength;
179     for( ; i < anISize + sizeOf8Float; i++, aPointer++ )
180         aResult[i] = *aPointer;
181     // puts type string
182     for( ; i < anISize + sizeOf8Float + aTypeLength; i++ )
183         aResult[i] = aTypeStr[i - anISize - sizeOf8Float ];
184     // puts length of tooltiptext string
185     aPointer = (char*)&aToolTipLength;
186     for( ; i < 2*anISize + sizeOf8Float + aTypeLength; i++, aPointer++ )
187         aResult[i] = *aPointer;
188     // puts tooltiptext string
189     for( ; i <  2*anISize + sizeOf8Float + aTypeLength + aToolTipLength; i++ )
190         aResult[ i] = aToolTipStr[i - 2*anISize - sizeOf8Float - aTypeLength];
191
192     int aCurPos = 2*anISize + sizeOf8Float + aTypeLength + aToolTipLength;
193     // adds aspect byte array
194     for ( i = aCurPos; i < (int)( aCurPos + aAspect.size() ); i++ )
195         aResult[i] = aAspect[i - aCurPos];
196
197     aCurPos = aCurPos + aAspect.size();
198     // adds GL text byte array
199     for ( i = aCurPos; i < (int)( aCurPos + aGLText.size() ); i++ )
200         aResult[i] = aGLText[i - aCurPos];    
201
202     aCurPos += aGLText.size();
203     aPointer = (char*)&myOwner;
204     for( i = 0; i < sizeof( SUIT_DataOwner* ); i++, aPointer++ )
205         aResult[ aCurPos + i ] = *aPointer;
206
207     return aResult;
208 }
209
210 /*!
211   Initialize object by byte array
212   \param theArray - byte array
213 */
214 bool GLViewer_Object::initializeFromByteCopy( QByteArray theArray )
215 {
216     int i = 0;
217     int anISize = sizeof( int );
218     int aFSize = sizeof( GLfloat );
219     
220     float aLeft = 0, aTop = 0, aRight = 0, aBottom = 0;    
221
222     //QString aTypeStr, aToolTipStr;
223     int aTypeLength = 0, aToolTipLength = 0;
224
225     int aSize = theArray.size();
226
227     GLViewer_Text* aGLText = new GLViewer_Text( 0, 0, 0, QColor(255,255,255));
228     int aGLTextMinSize = (aGLText->getByteCopy()).size();
229     GLViewer_AspectLine* aAspectLine = new GLViewer_AspectLine();
230     int aGLAspLineSize = (aAspectLine->getByteCopy()).size();
231
232     QByteArray aGLTextArray, aAspect;
233     aGLTextArray.resize( aGLAspLineSize );
234     aAspect.resize( aGLAspLineSize );
235
236     if( aSize < 2*anISize + 8*aFSize + aGLTextMinSize + aGLAspLineSize )
237         return false;
238
239     char* aPointer = (char*)&aLeft;
240     for( i = 0; i < aFSize; i++, aPointer++ )
241         *aPointer = theArray[i];
242     aPointer = (char*)&aTop;
243     for( ; i < 2*aFSize; i++, aPointer++ )
244         *aPointer = theArray[i];
245     aPointer = (char*)&aRight;
246     for( ; i < 3*aFSize; i++, aPointer++ )
247         *aPointer = theArray[i];
248     aPointer = (char*)&aBottom;
249     for( ; i < 4*aFSize; i++, aPointer++ )
250         *aPointer = theArray[i];
251
252     //myRect = new QRect( aLeft, aTop, aRight - aLeft, aBottom - aTop );
253     myRect = new GLViewer_Rect( aLeft, aRight, aTop, aBottom );
254
255     aPointer = (char*)&myXScale;
256     for( ; i < 5*aFSize; i++, aPointer++ )
257         *aPointer = theArray[i];
258     aPointer = (char*)&myYScale;
259     for( ; i < 6*aFSize; i++, aPointer++ )
260         *aPointer = theArray[i];
261     aPointer = (char*)&myXGap;
262     for( ; i < 7*aFSize; i++, aPointer++ )
263         *aPointer = theArray[i];
264     aPointer = (char*)&myYGap;
265     for( ; i < 8*aFSize; i++, aPointer++ )
266         *aPointer = theArray[i];
267
268     myIsHigh = false;
269     myIsSel = false;
270     myIsVisible = true;
271
272     aPointer = (char*)&aTypeLength;
273     for( ; i < anISize + 8*aFSize; i++, aPointer++ )
274         *aPointer = theArray[i];
275     myType = "";
276     for( ; i < anISize + 8*aFSize + aTypeLength; i++ )
277     {
278         QChar aChar( theArray[i] );
279         myType += aChar;
280     }
281
282     aPointer = (char*)&aToolTipLength;
283     for( ; i < 2*anISize + 8*aFSize + aTypeLength; i++, aPointer++ )
284         *aPointer = theArray[i];
285     myToolTipText= "";
286     for( ; i < 2*anISize + 8*aFSize + aTypeLength + aToolTipLength; i++ )
287     {
288         QChar aChar( theArray[i] );
289         myToolTipText += aChar;
290     }
291     
292     int aCurPos = 2*anISize + 8*aFSize + aTypeLength + aToolTipLength;
293     if( aSize - aCurPos < aGLTextMinSize + aGLAspLineSize )
294         return false;
295
296     for( i = 0; i < aGLAspLineSize; i++ )
297         aAspect[i] = theArray[ aCurPos + i ];
298     myAspectLine = GLViewer_AspectLine::fromByteCopy( aAspect );
299
300     aCurPos = aCurPos + aGLAspLineSize;
301     aGLTextArray.resize( aSize - aCurPos );
302     for( i = 0; i + aCurPos < aSize; i++ )
303         aGLTextArray[i] = theArray[ aCurPos + i ];
304     // replace gl_text pointer by other
305     if ( myGLText )
306       delete myGLText;
307     myGLText = GLViewer_Text::fromByteCopy( aGLTextArray );
308     
309     return true;        
310 }
311
312 /*!
313   Sets object's group
314   \param theGroup - group
315 */
316 void GLViewer_Object::setGroup( GLViewer_Group* theGroup )
317 {
318   if ( myGroup == theGroup )
319     return;
320
321   if( myGroup )
322     myGroup->removeObject( this );
323   
324   myGroup = theGroup;
325   if( theGroup )
326     myGroup->addObject( this );
327 }
328
329 /*!
330   \return object's group
331 */
332 GLViewer_Group* GLViewer_Object::getGroup() const
333 {
334   return myGroup;
335 }