src/GLViewer/GLViewer_Drawer.h

Go to the documentation of this file.
00001 //  Copyright (C) 2005 OPEN CASCADE
00002 //
00003 //  This library is free software; you can redistribute it and/or
00004 //  modify it under the terms of the GNU Lesser General Public
00005 //  License as published by the Free Software Foundation; either
00006 //  version 2.1 of the License.
00007 //
00008 //  This library is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 //  Lesser General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU Lesser General Public
00014 //  License along with this library; if not, write to the Free Software
00015 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 //  Author : OPEN CASCADE
00020 //
00021 
00022 // File:      GLViewer_Drawer.h
00023 // Created:   November, 2004
00024 
00025 #ifndef GLVIEWER_DRAWER_H
00026 #define GLVIEWER_DRAWER_H
00027 
00028 #ifdef WNT
00029 #include "windows.h"
00030 #endif
00031 
00032 #include <qcolor.h>
00033 #include <qobject.h>
00034 #include <qfile.h>
00035 #include <qfont.h>
00036 #include <qgl.h>
00037 
00038 #include <GL/gl.h>
00039 
00040 #include "GLViewer.h"
00041 #include "GLViewer_Defs.h"
00042 #include "GLViewer_Geom.h"
00043 
00044 class GLViewer_Object;
00045 class GLViewer_Rect;
00046 class GLViewer_CoordSystem;
00047 
00048 #ifdef WNT
00049 #pragma warning( disable:4251 )
00050 #endif
00051 
00055 struct GLVIEWER_API GLViewer_TexIdStored
00056 {
00058   GLuint      myTexFontId;
00060   int         myTexFontWidth;
00062   int         myTexFontHeight;
00063 };
00064 
00069 struct GLVIEWER_API GLViewer_TexFindId
00070 {
00072   QString     myFontFamily;
00074   bool        myIsBold;
00076   bool        myIsItal;
00078   bool        myIsUndl;
00080   int         myPointSize;
00082   int         myViewPortId;
00084   bool operator < (const GLViewer_TexFindId theStruct) const 
00085   { 
00086     if ( myViewPortId != theStruct.myViewPortId )
00087       return myViewPortId < theStruct.myViewPortId;
00088     else if ( myPointSize != theStruct.myPointSize )
00089       return myPointSize < theStruct.myPointSize;
00090     else if ( myIsBold != theStruct.myIsBold )
00091       return myIsBold < theStruct.myIsBold;
00092     else if ( myIsItal != theStruct.myIsItal )
00093       return myIsItal < theStruct.myIsItal;
00094     else if ( myIsUndl != theStruct.myIsUndl )
00095       return myIsUndl < theStruct.myIsUndl;
00096     else
00097       return myFontFamily < theStruct.myFontFamily;
00098   }
00099 };
00100 
00106 class GLVIEWER_API GLViewer_TexFont
00107 {
00108 public:
00110   GLViewer_TexFont();
00112   /*
00113   * \param theFont         - a base font
00114   * \param theSeparator    - separator between letters
00115   * \param theIsResizeable - specifies whether text drawn by this object can be scaled along with the scene
00116   * \param theMinMagFilter - min/mag filter, affects text sharpness
00117   */
00118   GLViewer_TexFont( QFont* theFont, 
00119                     int theSeparator = 2, 
00120                     bool theIsResizeable = false, 
00121                     GLuint theMinMagFilter = GL_LINEAR/*_ATTENUATION*/ );
00123   ~GLViewer_TexFont();
00124   
00126   bool            generateTexture();
00128   void            drawString( QString  theStr,
00129                               GLdouble theX = 0.0,
00130                               GLdouble theY = 0.0,
00131                               GLfloat  theScale = 1.0 );
00132   
00134   int             getSeparator(){ return mySeparator; }
00136   void            setSeparator( int theSeparator ){ mySeparator = theSeparator; }
00137   
00139   int             getStringWidth( QString theString );
00141   int             getStringHeight();
00142   
00144   static void     clearTextBases();
00145 
00147   static QMap<GLViewer_TexFindId,GLViewer_TexIdStored> TexFontBase;
00149   static QMap<GLViewer_TexFindId,GLuint>               BitmapFontCache;
00150 
00151 private:
00153   void            init();
00154   
00155 private:
00157   int             myNbSymbols;
00159   int*            myWidths;
00161   int*            myPositions;
00163   QFont           myQFont;
00165   GLuint          myTexFont;
00167   int             myTexFontWidth;
00169   int             myTexFontHeight;
00171   int             mySeparator;
00173   bool            myIsResizeable;
00175   GLuint          myMinMagFilter;
00177   int             myFontHeight;
00179   int             myMaxRowWidth;
00180 };
00181 
00187 class GLVIEWER_API GLViewer_Drawer
00188 {
00189 public:
00191   enum
00192   {
00193     GLText_Center = 0,
00194     GLText_Left,
00195     GLText_Right,
00196     GLText_Top,
00197     GLText_Bottom
00198   };
00199 
00200   // Objects status ( needs for change colors )
00201   //enum ObjectStatus
00202   //{
00203   //  OS_Normal = 0,
00204   //  OS_Highlighted,
00205   //  OS_Selected
00206   //};
00207   
00208   // 
00209   //enum ClosedStatus
00210   //{
00211   //  CS_CLOSED = 0,
00212   //  CS_OPEN = 1
00213   //};  
00214 
00216   GLViewer_Drawer();
00218   virtual ~GLViewer_Drawer();
00219   
00221   /*
00222   *\param xScale - current scale along X-direction
00223   *\param yScale - current scale along Y-direction
00224   *\param onlyUpdate - = true if only update highlight-select information
00225   */
00226   virtual void                    create( float xScale, float yScale, bool onlyUpdate ) = 0;  
00227   
00229   virtual void                    addObject( GLViewer_Object* theObject ){ myObjects.append( theObject ); }
00231   virtual void                    clear(){ myObjects.clear(); }
00232   
00234   QString                         getObjectType() const { return myObjectType; }
00235 
00237   int                             getPriority() const { return myPriority; }
00238 
00240      void                            setAntialiasing(const bool on);
00241   
00243   static void                     destroyAllTextures();
00244   
00246 
00251   virtual bool                    translateToHPGL( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aHPGLCS );
00252   
00254 
00259   virtual bool                    translateToPS( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aPSCS ); 
00260   
00261 #ifdef WIN32
00262 
00263 
00270   virtual bool                    translateToEMF( HDC hDC, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aEMFCS );
00271 #endif
00272   
00274 
00280   static GLuint                   loadTexture( const QString& fileName,
00281                                                GLint* x_size = 0,
00282                                                GLint* y_size = 0,
00283                                                GLint* t_size = 0);
00284 
00286 
00292   void                            drawTexture( GLuint texture,
00293                                                GLint size,
00294                                                GLfloat x,
00295                                                GLfloat y );
00296 
00298 
00305   void                            drawTexture( GLuint texture,
00306                                                GLint x_size,
00307                                                GLint y_size,
00308                                                GLfloat x,
00309                                                GLfloat y );
00310 
00312 
00322   void                            drawTexturePart( GLuint texture,
00323                                                    GLfloat x_ratio,
00324                                                    GLfloat y_ratio,
00325                                                    GLfloat x_size,
00326                                                    GLfloat y_size,
00327                                                    GLfloat x,
00328                                                    GLfloat y,
00329                                                    GLfloat scale = 0 );
00330 
00331 
00332 
00334 
00343   void                            drawText( const QString& text,
00344                                             GLfloat xPos,
00345                                                            GLfloat yPos,
00346                                             const QColor& color,
00347                                             QFont* aFont,
00348                                             int theSeparator,
00349                                             DisplayTextFormat = DTF_BITMAP );
00350 
00352 
00361   void                            drawGLText( QString text,
00362                                                              float x,
00363                                                              float y,
00364                                               int hPosition = GLText_Center,
00365                                               int vPosition = GLText_Center,
00366                                               QColor color = Qt::black,
00367                                               bool smallFont = false );
00368 
00370 
00373   inline void                     setFont( const QFont& font ) { myFont = font; }
00374 
00376   inline QFont                    font() const { return myFont; }
00377 
00379 
00382   inline void                     setTextFormat( const DisplayTextFormat format ) { myTextFormat = format; }
00383 
00385   inline DisplayTextFormat        textFormat() const { return myTextFormat; }
00386 
00388 
00391   inline void                     setTextScale( const GLfloat factor ) { myTextScale = factor; }
00392 
00394   inline GLfloat                  textScale() const { return myTextScale; }
00395 
00397   GLViewer_Rect                   textRect( const QString& ) const;
00398 
00399 
00401   static void                     drawRectangle( GLViewer_Rect* theRect, QColor = Qt::black );
00402 
00403 protected:
00405   //* with predefined color
00406   static void                     drawRectangle( GLViewer_Rect*, GLfloat, GLfloat = 0, QColor = Qt::black, 
00407                                bool = false, QColor = Qt::white );
00408   static void                     drawContour( GLViewer_Rect*, QColor, GLfloat, GLushort, bool );
00409   static void                     drawContour( const GLViewer_PntList&, QColor, GLfloat );
00410   static void                     drawPolygon( GLViewer_Rect*, QColor, GLushort, bool );
00411   static void                     drawPolygon( const GLViewer_PntList&, QColor );
00412   static void                     drawVertex( GLfloat, GLfloat, QColor );
00413   static void                     drawCross( GLfloat, GLfloat, QColor );
00414   static void                     drawArrow( const GLfloat red, const GLfloat green, const GLfloat blue,
00415                               GLfloat, GLfloat, GLfloat, GLfloat,
00416                               GLfloat, GLfloat, GLfloat, GLboolean = GL_FALSE );
00417 
00419   virtual void                    drawText( GLViewer_Object* theObject );
00420 
00422   float                           myXScale;
00424   float                           myYScale;
00425   
00427   QValueList<GLViewer_Object*>    myObjects;
00429   GLuint                          myTextList;
00430   
00432   QString                         myObjectType;
00434   int                             myPriority;
00435 
00437   QFont                           myFont;
00439   DisplayTextFormat               myTextFormat;
00440 
00443   GLfloat                         myTextScale;
00444 };
00445 
00446 #ifdef WNT
00447 #pragma warning ( default:4251 )
00448 #endif
00449 
00450 #endif // GLVIEWER_DRAWER_H