Back to index

salome-gui  6.5.0
GLViewer_Object.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "GLViewer_Object.h"
00024 #include "GLViewer_Drawer.h"
00025 #include "GLViewer_AspectLine.h"
00026 #include "GLViewer_Text.h"
00027 #include "GLViewer_Group.h"
00028 
00029 #include <SUIT_DataOwner.h>
00030 
00034 GLViewer_Object::GLViewer_Object()
00035 {
00036   myXScale = 1.0; 
00037   myYScale = 1.0;
00038   myXGap = 0;
00039   myYGap = 0;
00040   myZoom = 1.0;
00041 
00042   myIsHigh = GL_FALSE;
00043   myIsSel = GL_FALSE;
00044   
00045   myRect = new GLViewer_Rect();;  
00046   myUpdateRect = new GLViewer_Rect();;  
00047   myGLText = new GLViewer_Text( 0, 0, 0, QColor(0,0,0) );
00048 
00049   myAspectLine = new GLViewer_AspectLine();
00050   myType = "GLViewer_Object";
00051 
00052   myOwner = NULL;
00053   myDrawer = NULL;
00054 
00055   myIsVisible = true;
00056 
00057   isToolTipHTML = false;  
00058 
00059   myGroup = NULL;
00060 }
00061 
00065 GLViewer_Object::~GLViewer_Object()
00066 {
00067   if( myRect )
00068     delete myRect;
00069 
00070   if( myUpdateRect )
00071     delete myUpdateRect;
00072 
00073   if( myGLText )
00074     delete myGLText;
00075 
00076   if( myAspectLine )
00077     delete myAspectLine;
00078 }
00079 
00083 int GLViewer_Object::getPriority() const
00084 {
00085     return myDrawer ? myDrawer->getPriority() : 0;
00086 }
00087 
00092 GLboolean GLViewer_Object::isInside( GLViewer_Rect theRect )
00093 {
00094     return theRect.toQRect().contains( myRect->toQRect() );
00095 }
00096 
00101 GLboolean GLViewer_Object::setZoom( GLfloat zoom, bool, bool )
00102 {
00103     if( myZoom == zoom )
00104         return GL_FALSE;
00105 
00106     myZoom = zoom;
00107     return GL_TRUE;
00108 }
00109 
00114 GLboolean GLViewer_Object::updateZoom( bool zoomIn )
00115 {
00116     float newZoom;
00117     float step = zoomIn ? 1 : -1;
00118     double epsilon = 0.001;
00119 
00120     if( myZoom - 1 > epsilon )
00121         newZoom = ( myZoom * 2 + step ) / 2;
00122     else if( 1 - myZoom > epsilon )
00123         newZoom = 2 / ( 2 / myZoom - step );
00124     else
00125         newZoom = zoomIn ? 3./2. : 2./3.;
00126 
00127     if( newZoom < 0.01 || newZoom > 100.0 )
00128         return GL_FALSE;
00129 
00130     return setZoom( newZoom, true );
00131 }
00132 
00137 QByteArray GLViewer_Object::getByteCopy()
00138 {
00139     int i = 0;
00140     int anISize = sizeof( int );
00141 
00142     const char* aTypeStr = myType.toLatin1().constData();
00143     const char* aToolTipStr = myToolTipText.toLatin1().constData();
00144 
00145     int aTypeLength = myType.length();
00146     int aToolTipLength = myToolTipText.length();
00147 
00148 
00149     QByteArray aGLText = myGLText->getByteCopy();
00150     QByteArray aAspect = myAspectLine->getByteCopy();
00151     
00152     float aRectData[8];
00153     aRectData[ 0 ] = myRect->left();
00154     aRectData[ 1 ] = myRect->top();
00155     aRectData[ 2 ] = myRect->right();
00156     aRectData[ 3 ] = myRect->bottom();
00157     aRectData[ 4 ] = myXScale;
00158     aRectData[ 5 ] = myYScale;
00159     aRectData[ 6 ] = myXGap;
00160     aRectData[ 7 ] = myYGap;
00161     
00162     int sizeOf8Float = sizeof( aRectData );
00163 
00164     QByteArray aResult;
00165     aResult.resize( 2*anISize + sizeOf8Float + 
00166                     aTypeLength + aToolTipLength +
00167                     aGLText.size() + aAspect.size() );
00168     // puts 8 float values into the byte array
00169     char* aPointer = (char*)&aRectData;
00170     for( i = 0; i < sizeOf8Float; i++, aPointer++ )
00171         aResult[i] = *aPointer;
00172     // puts length of type string
00173     aPointer = (char*)&aTypeLength;
00174     for( ; i < anISize + sizeOf8Float; i++, aPointer++ )
00175         aResult[i] = *aPointer;
00176     // puts type string
00177     for( ; i < anISize + sizeOf8Float + aTypeLength; i++ )
00178         aResult[i] = aTypeStr[i - anISize - sizeOf8Float ];
00179     // puts length of tooltiptext string
00180     aPointer = (char*)&aToolTipLength;
00181     for( ; i < 2*anISize + sizeOf8Float + aTypeLength; i++, aPointer++ )
00182         aResult[i] = *aPointer;
00183     // puts tooltiptext string
00184     for( ; i <  2*anISize + sizeOf8Float + aTypeLength + aToolTipLength; i++ )
00185         aResult[ i] = aToolTipStr[i - 2*anISize - sizeOf8Float - aTypeLength];
00186 
00187     int aCurPos = 2*anISize + sizeOf8Float + aTypeLength + aToolTipLength;
00188     // adds aspect byte array
00189     for ( i = aCurPos; i < (int)( aCurPos + aAspect.size() ); i++ )
00190         aResult[i] = aAspect[i - aCurPos];
00191 
00192     aCurPos = aCurPos + aAspect.size();
00193     // adds GL text byte array
00194     for ( i = aCurPos; i < (int)( aCurPos + aGLText.size() ); i++ )
00195         aResult[i] = aGLText[i - aCurPos];    
00196 
00197     aCurPos += aGLText.size();
00198     aPointer = (char*)&myOwner;
00199     for( i = 0; i < sizeof( SUIT_DataOwner* ); i++, aPointer++ )
00200         aResult[ aCurPos + i ] = *aPointer;
00201 
00202     return aResult;
00203 }
00204 
00209 bool GLViewer_Object::initializeFromByteCopy( QByteArray theArray )
00210 {
00211     int i = 0;
00212     int anISize = sizeof( int );
00213     int aFSize = sizeof( GLfloat );
00214     
00215     float aLeft = 0, aTop = 0, aRight = 0, aBottom = 0;    
00216 
00217     //QString aTypeStr, aToolTipStr;
00218     int aTypeLength = 0, aToolTipLength = 0;
00219 
00220     int aSize = theArray.size();
00221 
00222     GLViewer_Text* aGLText = new GLViewer_Text( 0, 0, 0, QColor(255,255,255));
00223     int aGLTextMinSize = (aGLText->getByteCopy()).size();
00224     GLViewer_AspectLine* aAspectLine = new GLViewer_AspectLine();
00225     int aGLAspLineSize = (aAspectLine->getByteCopy()).size();
00226 
00227     QByteArray aGLTextArray, aAspect;
00228     aGLTextArray.resize( aGLAspLineSize );
00229     aAspect.resize( aGLAspLineSize );
00230 
00231     if( aSize < 2*anISize + 8*aFSize + aGLTextMinSize + aGLAspLineSize )
00232         return false;
00233 
00234     char* aPointer = (char*)&aLeft;
00235     for( i = 0; i < aFSize; i++, aPointer++ )
00236         *aPointer = theArray[i];
00237     aPointer = (char*)&aTop;
00238     for( ; i < 2*aFSize; i++, aPointer++ )
00239         *aPointer = theArray[i];
00240     aPointer = (char*)&aRight;
00241     for( ; i < 3*aFSize; i++, aPointer++ )
00242         *aPointer = theArray[i];
00243     aPointer = (char*)&aBottom;
00244     for( ; i < 4*aFSize; i++, aPointer++ )
00245         *aPointer = theArray[i];
00246 
00247     //myRect = new QRect( aLeft, aTop, aRight - aLeft, aBottom - aTop );
00248     myRect = new GLViewer_Rect( aLeft, aRight, aTop, aBottom );
00249 
00250     aPointer = (char*)&myXScale;
00251     for( ; i < 5*aFSize; i++, aPointer++ )
00252         *aPointer = theArray[i];
00253     aPointer = (char*)&myYScale;
00254     for( ; i < 6*aFSize; i++, aPointer++ )
00255         *aPointer = theArray[i];
00256     aPointer = (char*)&myXGap;
00257     for( ; i < 7*aFSize; i++, aPointer++ )
00258         *aPointer = theArray[i];
00259     aPointer = (char*)&myYGap;
00260     for( ; i < 8*aFSize; i++, aPointer++ )
00261         *aPointer = theArray[i];
00262 
00263     myIsHigh = false;
00264     myIsSel = false;
00265     myIsVisible = true;
00266 
00267     aPointer = (char*)&aTypeLength;
00268     for( ; i < anISize + 8*aFSize; i++, aPointer++ )
00269         *aPointer = theArray[i];
00270     myType = "";
00271     for( ; i < anISize + 8*aFSize + aTypeLength; i++ )
00272     {
00273         QChar aChar( theArray[i] );
00274         myType += aChar;
00275     }
00276 
00277     aPointer = (char*)&aToolTipLength;
00278     for( ; i < 2*anISize + 8*aFSize + aTypeLength; i++, aPointer++ )
00279         *aPointer = theArray[i];
00280     myToolTipText= "";
00281     for( ; i < 2*anISize + 8*aFSize + aTypeLength + aToolTipLength; i++ )
00282     {
00283         QChar aChar( theArray[i] );
00284         myToolTipText += aChar;
00285     }
00286     
00287     int aCurPos = 2*anISize + 8*aFSize + aTypeLength + aToolTipLength;
00288     if( aSize - aCurPos < aGLTextMinSize + aGLAspLineSize )
00289         return false;
00290 
00291     for( i = 0; i < aGLAspLineSize; i++ )
00292         aAspect[i] = theArray[ aCurPos + i ];
00293     myAspectLine = GLViewer_AspectLine::fromByteCopy( aAspect );
00294 
00295     aCurPos = aCurPos + aGLAspLineSize;
00296     aGLTextArray.resize( aSize - aCurPos );
00297     for( i = 0; i + aCurPos < aSize; i++ )
00298         aGLTextArray[i] = theArray[ aCurPos + i ];
00299     // replace gl_text pointer by other
00300     if ( myGLText )
00301       delete myGLText;
00302     myGLText = GLViewer_Text::fromByteCopy( aGLTextArray );
00303     
00304     return true;        
00305 }
00306 
00311 void GLViewer_Object::setGroup( GLViewer_Group* theGroup )
00312 {
00313   if ( myGroup == theGroup )
00314     return;
00315 
00316   if( myGroup )
00317     myGroup->removeObject( this );
00318   
00319   myGroup = theGroup;
00320   if( theGroup )
00321     myGroup->addObject( this );
00322 }
00323 
00327 GLViewer_Group* GLViewer_Object::getGroup() const
00328 {
00329   return myGroup;
00330 }