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