1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // Author : OPEN CASCADE
23 //#include <GLViewerAfx.h>
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"
31 #include <SUIT_DataOwner.h>
34 //using namespace std;
39 GLViewer_Object::GLViewer_Object()
50 myRect = new GLViewer_Rect();;
51 myUpdateRect = new GLViewer_Rect();;
52 myGLText = new GLViewer_Text( 0, 0, 0, QColor(0,0,0) );
54 myAspectLine = new GLViewer_AspectLine();
55 myType = "GLViewer_Object";
62 isToolTipHTML = false;
70 GLViewer_Object::~GLViewer_Object()
86 \return priority of object
88 int GLViewer_Object::getPriority() const
90 return myDrawer ? myDrawer->getPriority() : 0;
94 \return true if object is inside rectangle
95 \param theRect - rectangle
97 GLboolean GLViewer_Object::isInside( GLViewer_Rect theRect )
99 return theRect.toQRect().contains( myRect->toQRect() );
104 \param zoom - zoom factor
106 GLboolean GLViewer_Object::setZoom( GLfloat zoom, bool, bool )
116 Performs zoom change by step
117 \param zoomIn - to increase to decrease zoom
119 GLboolean GLViewer_Object::updateZoom( bool zoomIn )
122 float step = zoomIn ? 1 : -1;
123 double epsilon = 0.001;
125 if( myZoom - 1 > epsilon )
126 newZoom = ( myZoom * 2 + step ) / 2;
127 else if( 1 - myZoom > epsilon )
128 newZoom = 2 / ( 2 / myZoom - step );
130 newZoom = zoomIn ? 3./2. : 2./3.;
132 if( newZoom < 0.01 || newZoom > 100.0 )
135 return setZoom( newZoom, true );
139 Codes object as byte copy
142 QByteArray GLViewer_Object::getByteCopy()
145 int anISize = sizeof( int );
147 const char* aTypeStr = myType.toLatin1().constData();
148 const char* aToolTipStr = myToolTipText.toLatin1().constData();
150 int aTypeLength = myType.length();
151 int aToolTipLength = myToolTipText.length();
154 QByteArray aGLText = myGLText->getByteCopy();
155 QByteArray aAspect = myAspectLine->getByteCopy();
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;
167 int sizeOf8Float = sizeof( aRectData );
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;
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];
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];
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];
202 aCurPos += aGLText.size();
203 aPointer = (char*)&myOwner;
204 for( i = 0; i < sizeof( SUIT_DataOwner* ); i++, aPointer++ )
205 aResult[ aCurPos + i ] = *aPointer;
211 Initialize object by byte array
212 \param theArray - byte array
214 bool GLViewer_Object::initializeFromByteCopy( QByteArray theArray )
217 int anISize = sizeof( int );
218 int aFSize = sizeof( GLfloat );
220 float aLeft = 0, aTop = 0, aRight = 0, aBottom = 0;
222 //QString aTypeStr, aToolTipStr;
223 int aTypeLength = 0, aToolTipLength = 0;
225 int aSize = theArray.size();
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();
232 QByteArray aGLTextArray, aAspect;
233 aGLTextArray.resize( aGLAspLineSize );
234 aAspect.resize( aGLAspLineSize );
236 if( aSize < 2*anISize + 8*aFSize + aGLTextMinSize + aGLAspLineSize )
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];
252 //myRect = new QRect( aLeft, aTop, aRight - aLeft, aBottom - aTop );
253 myRect = new GLViewer_Rect( aLeft, aRight, aTop, aBottom );
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];
272 aPointer = (char*)&aTypeLength;
273 for( ; i < anISize + 8*aFSize; i++, aPointer++ )
274 *aPointer = theArray[i];
276 for( ; i < anISize + 8*aFSize + aTypeLength; i++ )
278 QChar aChar( theArray[i] );
282 aPointer = (char*)&aToolTipLength;
283 for( ; i < 2*anISize + 8*aFSize + aTypeLength; i++, aPointer++ )
284 *aPointer = theArray[i];
286 for( ; i < 2*anISize + 8*aFSize + aTypeLength + aToolTipLength; i++ )
288 QChar aChar( theArray[i] );
289 myToolTipText += aChar;
292 int aCurPos = 2*anISize + 8*aFSize + aTypeLength + aToolTipLength;
293 if( aSize - aCurPos < aGLTextMinSize + aGLAspLineSize )
296 for( i = 0; i < aGLAspLineSize; i++ )
297 aAspect[i] = theArray[ aCurPos + i ];
298 myAspectLine = GLViewer_AspectLine::fromByteCopy( aAspect );
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
307 myGLText = GLViewer_Text::fromByteCopy( aGLTextArray );
314 \param theGroup - group
316 void GLViewer_Object::setGroup( GLViewer_Group* theGroup )
318 if ( myGroup == theGroup )
322 myGroup->removeObject( this );
326 myGroup->addObject( this );
330 \return object's group
332 GLViewer_Group* GLViewer_Object::getGroup() const