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