Back to index

salome-gui  6.5.0
GLViewer_BaseDrawers.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 //  Author : OPEN CASCADE
00024 // File:      GLViewer_BaseDrawers.cxx
00025 // Created:   November, 2004
00026 //#include <GLViewerAfx.h>
00027 //
00028 #include "GLViewer_BaseDrawers.h"
00029 #include "GLViewer_Object.h"
00030 #include "GLViewer_Text.h"
00031 #include "GLViewer_AspectLine.h"
00032 #include "GLViewer_BaseObjects.h"
00033 
00034 // Qt includes
00035 #include <QColor>
00036 
00037 
00038 #ifndef WIN32
00039 #include <GL/glx.h>
00040 #endif
00041 
00042 GLfloat sin_table[SEGMENTS];
00043 GLfloat cos_table[SEGMENTS];
00044 
00048 GLViewer_MarkerDrawer::GLViewer_MarkerDrawer()
00049 : GLViewer_Drawer()
00050 {
00051     GLfloat angle = 0.0;
00052     for ( int i = 0; i < SEGMENTS; i++ )
00053     {
00054         sin_table[i] = sin( angle );
00055         cos_table[i] = cos( angle );
00056         angle += float( STEP );
00057     }
00058     myObjectType = "GLViewer_MarkerSet";
00059 }
00060 
00064 GLViewer_MarkerDrawer::~GLViewer_MarkerDrawer()
00065 {
00066 }
00067 
00073 void GLViewer_MarkerDrawer::create( float xScale, float yScale, bool onlyUpdate )
00074 {
00075     QList<int>::Iterator it;
00076     QList<int>::Iterator EndIt;
00077     QList<GLViewer_Object*>::Iterator anObjectIt = myObjects.begin();
00078     QList<GLViewer_Object*>::Iterator anEndObjectIt = myObjects.end();
00079 
00080     myXScale = xScale;
00081     myYScale = yScale;
00082 
00083     QColor colorN, colorH, colorS;
00084 
00085     GLViewer_MarkerSet* aMarkerSet = NULL;
00086     GLViewer_AspectLine* anAspectLine = NULL;
00087 
00088     for( ; anObjectIt != anEndObjectIt; anObjectIt++ )
00089     {
00090         aMarkerSet = ( GLViewer_MarkerSet* )(*anObjectIt);
00091         anAspectLine = aMarkerSet->getAspectLine();
00092         anAspectLine->getLineColors( colorN, colorH, colorS );
00093 
00094         float* aXCoord = aMarkerSet->getXCoord();
00095         float* anYCoord = aMarkerSet->getYCoord();
00096         float aRadius = aMarkerSet->getMarkerSize();
00097 
00098         QList<int> aHNumbers, anUHNumbers, aSelNumbers, anUSelNumbers;
00099         aMarkerSet->exportNumbers( aHNumbers, anUHNumbers, aSelNumbers, anUSelNumbers );
00100 
00101         if( onlyUpdate )
00102         {
00103             EndIt = anUHNumbers.end();
00104             for( it = anUHNumbers.begin(); it != EndIt; ++it )
00105             {
00106                 drawMarker( aXCoord[*it], anYCoord[*it], aRadius, colorN, anAspectLine );
00107             }
00108 
00109             EndIt = anUSelNumbers.end();
00110             for( it = anUSelNumbers.begin(); it != EndIt; ++it )
00111                 drawMarker( aXCoord[*it], anYCoord[*it], aRadius, colorN, anAspectLine );
00112 
00113             EndIt = aSelNumbers.end();
00114             for( it = aSelNumbers.begin(); it != EndIt; ++it )
00115                 drawMarker( aXCoord[*it], anYCoord[*it], aRadius, colorS, anAspectLine );
00116 
00117             EndIt = aHNumbers.end();
00118             for( it = aHNumbers.begin(); it != EndIt; ++it )
00119             {
00120                 drawMarker( aXCoord[*it], anYCoord[*it], aRadius, colorH, anAspectLine );
00121             }
00122         }
00123         else
00124         {
00125             int aNumber = aMarkerSet->getNumMarkers();
00126             for( int i = 0; i < aNumber; i++ )
00127                 drawMarker( aXCoord[i], anYCoord[i], aRadius, colorN, anAspectLine );
00128 
00129             EndIt = anUSelNumbers.end();
00130             for( it = anUSelNumbers.begin(); it != EndIt; ++it )
00131                 drawMarker( aXCoord[*it], anYCoord[*it], aRadius, colorN, anAspectLine );
00132 
00133             EndIt = aSelNumbers.end();
00134             for( it = aSelNumbers.begin(); it != EndIt; ++it )
00135                 drawMarker( aXCoord[*it], anYCoord[*it], aRadius, colorS, anAspectLine );
00136         }
00137         if( aMarkerSet->getGLText()->getText() != "" )
00138         {
00139             //float aXPos = 0, anYPos = 0;
00140             //aMarkerSet->getGLText()->getPosition( aXPos, anYPos );
00141             //drawText( aMarkerSet->getGLText()->getText(), aXPos, anYPos, colorN, &aMarkerSet->getGLText()->getFont(), aMarkerSet->getGLText()->getSeparator() );
00142             drawText( aMarkerSet );
00143         }
00144     }
00145 }
00146 
00154 void GLViewer_MarkerDrawer::drawMarker( float& theXCoord, float& theYCoord,
00155                                      float& theRadius, QColor& theColor, GLViewer_AspectLine* theAspectLine )
00156 {
00157     glColor3f( ( GLfloat )theColor.red() / 255, 
00158                ( GLfloat )theColor.green() / 255, 
00159                ( GLfloat )theColor.blue() / 255 );
00160 
00161     glLineWidth( theAspectLine->getLineWidth() );
00162 
00163     if ( theAspectLine->getLineType() == 0 )
00164         glBegin( GL_LINE_LOOP );
00165     else
00166         glBegin( GL_LINE_STRIP);
00167 
00168     for ( int i = 0; i < SEGMENTS; i++ )
00169         glVertex2f( theXCoord + cos_table[i] * theRadius / myXScale,
00170                     theYCoord + sin_table[i] * theRadius / myYScale );
00171     glEnd();
00172 }
00173 
00174 
00178 GLViewer_PolylineDrawer::GLViewer_PolylineDrawer()
00179 :GLViewer_Drawer()
00180 {
00181     myObjectType = "GLViewer_Polyline";
00182 }
00183 
00187 GLViewer_PolylineDrawer::~GLViewer_PolylineDrawer()
00188 {
00189 }
00190 
00196 void GLViewer_PolylineDrawer::create( float xScale, float yScale, bool onlyUpdate )
00197 {
00198     QList<GLViewer_Object*>::Iterator aObjectIt = myObjects.begin();
00199     QList<GLViewer_Object*>::Iterator aObjectEndIt = myObjects.end();
00200     
00201     myXScale = xScale;
00202     myYScale = yScale;
00203 
00204     QColor color, colorN, colorH, colorS;
00205     GLViewer_AspectLine* anAspect = NULL;
00206     GLViewer_Polyline* aPolyline = NULL;
00207     for( ; aObjectIt != aObjectEndIt; aObjectIt++ )
00208     {
00209         anAspect = (*aObjectIt)->getAspectLine();
00210         aPolyline = (GLViewer_Polyline*)(*aObjectIt);
00211 
00212 
00213         anAspect->getLineColors( colorN, colorH, colorS );
00214         if( onlyUpdate )
00215         {
00216             if( aPolyline->isHighlighted() )
00217                 color = colorH;
00218             else if( aPolyline->isSelected() )
00219                 color = colorS;
00220             else
00221                 color = colorN;
00222         }
00223         else
00224         {
00225             if( aPolyline->isSelected() )
00226                 color = colorS;
00227             else
00228                 color = colorN;
00229         }
00230 
00231         float* aXCoord = aPolyline->getXCoord();
00232         float* anYCoord = aPolyline->getYCoord();
00233         int aSize = aPolyline->getNumber();        
00234 
00235         glColor3f( ( GLfloat )color.red() / 255, 
00236                    ( GLfloat )color.green() / 255, 
00237                    ( GLfloat )color.blue() / 255 );
00238 
00239         glLineWidth( anAspect->getLineWidth() );
00240 
00241         if ( anAspect->getLineType() == 0 )
00242             glBegin( GL_LINE_LOOP );
00243         else
00244             glBegin( GL_LINE_STRIP);
00245 
00246         for( int i = 0; i < aSize ; i++ )
00247              glVertex2f( aXCoord[ i ], anYCoord[ i ] );        
00248  
00249         if( aPolyline->isClosed() )
00250             glVertex2f( aXCoord[ 0 ], anYCoord[ 0 ] );
00251 
00252         glEnd();       
00253 
00254         if( aPolyline->getGLText()->getText() != "" )
00255         {
00256             //float aXPos = 0, anYPos = 0;
00257             //aPolyline->getGLText()->getPosition( aXPos, anYPos );
00258             //drawText( aPolyline->getGLText()->getText(), aXPos, anYPos, color, &aPolyline->getGLText()->getFont(), aPolyline->getGLText()->getSeparator() );
00259           drawText( aPolyline );
00260         }
00261     }
00262 }
00263 
00267 GLViewer_TextDrawer::GLViewer_TextDrawer()
00268 : GLViewer_Drawer()
00269 {
00270     myObjectType = "GLViewer_TextObject";
00271 }
00272 
00276 GLViewer_TextDrawer::~GLViewer_TextDrawer()
00277 {
00278 }
00279 
00285 void GLViewer_TextDrawer::create( float xScale, float yScale, bool onlyUpdate )
00286 {
00287     QList<GLViewer_Object*>::Iterator aObjectIt = myObjects.begin();
00288     QList<GLViewer_Object*>::Iterator aObjectEndIt = myObjects.end();
00289     
00290     myXScale = xScale;
00291     myYScale = yScale;
00292 
00293     QColor color, colorN, colorH, colorS;
00294     GLViewer_AspectLine* anAspect = NULL;    
00295     GLViewer_TextObject* anObject = NULL;
00296     //float aXPos = 0, anYPos = 0;
00297     for( ; aObjectIt != aObjectEndIt; aObjectIt++ )
00298     {
00299         anObject = (GLViewer_TextObject*)(*aObjectIt);
00300         anAspect = anObject->getAspectLine();    
00301 
00302         anAspect->getLineColors( colorN, colorH, colorS );
00303         if( onlyUpdate )
00304         {
00305             if( anObject->isHighlighted() )
00306                 color = colorH;
00307             else if( anObject->isSelected() )
00308                 color = colorS;
00309             else
00310                 color = colorN;
00311         }
00312         else
00313         {
00314             if( anObject->isSelected() )
00315                 color = colorS;
00316             else
00317                 color = colorN;
00318         }        
00319         
00320         //anObject->getGLText()->getPosition( aXPos, anYPos );
00321         //drawText( anObject->getGLText()->getText(), aXPos, anYPos, color, &(anObject->getGLText()->getFont()), anObject->getGLText()->getSeparator() );
00322         drawText( anObject );
00323     }
00324 }
00325 
00329 void GLViewer_TextDrawer::updateObjects()
00330 {
00331     QList<GLViewer_Object*>::Iterator aObjectIt = myObjects.begin();
00332     QList<GLViewer_Object*>::Iterator aObjectEndIt = myObjects.end();
00333     for( ; aObjectIt != aObjectEndIt; aObjectIt++ )
00334         (*aObjectIt)->compute();
00335 }