Salome HOME
Merge from OCC_development_generic_2006
[modules/gui.git] / src / GLViewer / GLViewer_Drawer.h
1 //  Copyright (C) 2005 OPEN CASCADE
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
18 //
19 //  Author : OPEN CASCADE
20 //
21
22 // File:      GLViewer_Drawer.h
23 // Created:   November, 2004
24
25 #ifndef GLVIEWER_DRAWER_H
26 #define GLVIEWER_DRAWER_H
27
28 #ifdef WNT
29 #include "windows.h"
30 #endif
31
32 #include <qcolor.h>
33 #include <qobject.h>
34 #include <qfile.h>
35 #include <qfont.h>
36 #include <qgl.h>
37
38 #include <GL/gl.h>
39
40 #include "GLViewer.h"
41 #include "GLViewer_Defs.h"
42 #include "GLViewer_Geom.h"
43
44 class GLViewer_Object;
45 class GLViewer_Rect;
46 class GLViewer_CoordSystem;
47
48 #ifdef WNT
49 #pragma warning( disable:4251 )
50 #endif
51 /*! 
52  * Struct GLViewer_TexIdStored
53  * Structure for store information about texture
54  */
55 struct GLVIEWER_API GLViewer_TexIdStored
56 {
57   //! Texture ID
58   GLuint      myTexFontId;
59   //! Texture width
60   int         myTexFontWidth;
61   //! texture height
62   int         myTexFontHeight;
63 };
64
65 /*! 
66  * Struct GLViewer_TexFindId
67  * Structure for srorage information about texture font
68  */
69 struct GLVIEWER_API GLViewer_TexFindId
70 {
71   //! Font family description
72   QString     myFontFamily;
73   //! Bold parameter
74   bool        myIsBold;
75   //! Italic parameter
76   bool        myIsItal;
77   //! Underline parameter
78   bool        myIsUndl;
79   //! Font Size
80   int         myPointSize;
81   //! View POrt ID
82   int         myViewPortId;
83   //! Overloaded operator for using struct as MAP key
84   bool operator < (const GLViewer_TexFindId theStruct) const 
85   { 
86     if ( myViewPortId != theStruct.myViewPortId )
87       return myViewPortId < theStruct.myViewPortId;
88     else if ( myPointSize != theStruct.myPointSize )
89       return myPointSize < theStruct.myPointSize;
90     else if ( myIsBold != theStruct.myIsBold )
91       return myIsBold < theStruct.myIsBold;
92     else if ( myIsItal != theStruct.myIsItal )
93       return myIsItal < theStruct.myIsItal;
94     else if ( myIsUndl != theStruct.myIsUndl )
95       return myIsUndl < theStruct.myIsUndl;
96     else
97       return myFontFamily < theStruct.myFontFamily;
98   }
99 };
100
101 /***************************************************************************
102 **  Class:   GLViewer_TexFont
103 **  Descr:   Font for GLViewer_Drawer
104 **  Module:  GLViewer
105 **  Created: UI team, 03.10.01
106 ****************************************************************************/
107
108 /*! 
109  * Class GLViewer_TexFont
110  * Drawing bitmap and texture fonts in GLViewer
111  */
112 class GLVIEWER_API GLViewer_TexFont
113 {
114 public:
115   //! A default constructor
116   GLViewer_TexFont();
117   //! A constructor
118   /*
119   * \param theFont         - a base font
120   * \param theSeparator    - separator between letters
121   * \param theIsResizeable - specifies whether text drawn by this object can be scaled along with the scene
122   * \param theMinMagFilter - min/mag filter, affects text sharpness
123   */
124   GLViewer_TexFont( QFont* theFont, 
125                     int theSeparator = 2, 
126                     bool theIsResizeable = false, 
127                     GLuint theMinMagFilter = GL_LINEAR/*_ATTENUATION*/ );
128   //! A destructor
129   ~GLViewer_TexFont();
130   
131   //! Generating font texture
132   bool            generateTexture();
133   //! Drawing string theStr in point with coords theX and theY
134   void            drawString( QString  theStr,
135                               GLdouble theX = 0.0,
136                               GLdouble theY = 0.0,
137                               GLfloat  theScale = 1.0 );
138   
139   //! Returns separator between letters
140   int             getSeparator(){ return mySeparator; }
141   //! Installing separator between letters
142   void            setSeparator( int theSeparator ){ mySeparator = theSeparator; }
143   
144   //! Returns width of string in pixels
145   int             getStringWidth( QString theString );
146   //! Returns height of string in pixels
147   int             getStringHeight();
148   
149   //! Clears all generated fonts
150   static void     clearTextBases();
151
152   //! Map for strorage generated texture fonts
153   static QMap<GLViewer_TexFindId,GLViewer_TexIdStored> TexFontBase;
154   //! Map for strorage generated bitmaps fonts
155   static QMap<GLViewer_TexFindId,GLuint>               BitmapFontCache;
156
157 private:
158   //! Initializes font parameters
159   void            init();
160   
161 private:
162   //! Number of characters in the font texture
163   int             myNbSymbols;
164   //! Array of letter width
165   int*            myWidths;
166   //! Array of letter positions in texture
167   int*            myPositions;
168   //! Pointer to base font
169   QFont           myQFont;
170   //! Font texture ID
171   GLuint          myTexFont;
172   //! Font texture width
173   int             myTexFontWidth;
174   //! Font texture height
175   int             myTexFontHeight;
176   //! Separator between letters
177   int             mySeparator;
178   //! Flag controlling scalability of this texmapped font
179   bool            myIsResizeable;
180   //! Min/mag filter
181   GLuint          myMinMagFilter;
182   //! Font height
183   int             myFontHeight;
184   //! Diagnostic information
185   int             myMaxRowWidth;
186 };
187
188 /***************************************************************************
189 **  Class:   GLViewer_Drawer
190 **  Descr:   Drawer for GLObject
191 **  Module:  GLViewer
192 **  Created: UI team, 03.10.01
193 ****************************************************************************/
194 /*! 
195  * Class GLViewer_Drawer
196  * Drawer for GLViewer_Objects.
197  * Drawer creates only one times per one type of object
198  */
199 class GLVIEWER_API GLViewer_Drawer
200 {
201 public:
202   //! Text position relatively object
203   enum
204   {
205     GLText_Center = 0,
206     GLText_Left,
207     GLText_Right,
208     GLText_Top,
209     GLText_Bottom
210   };
211
212   // Objects status ( needs for change colors )
213   //enum ObjectStatus
214   //{
215   //  OS_Normal = 0,
216   //  OS_Highlighted,
217   //  OS_Selected
218   //};
219   
220   // 
221   //enum ClosedStatus
222   //{
223   //  CS_CLOSED = 0,
224   //  CS_OPEN = 1
225   //};  
226
227   //! A constructor
228   GLViewer_Drawer();
229   //! A destructor
230   virtual ~GLViewer_Drawer();
231   
232   //! Main method which drawing object in GLViewer
233   /*
234   *\param xScale - current scale along X-direction
235   *\param yScale - current scale along Y-direction
236   *\param onlyUpdate - = true if only update highlight-select information
237   */
238   virtual void                    create( float xScale, float yScale, bool onlyUpdate ) = 0;  
239   
240   //! Adds object to drawer display list
241   virtual void                    addObject( GLViewer_Object* theObject ){ myObjects.append( theObject ); }
242   //! Clears drawer display list
243   virtual void                    clear(){ myObjects.clear(); }
244   
245   //! Returns object type (needs for dynamic search of right drawer ) 
246   QString                         getObjectType() const { return myObjectType; }
247
248   //! Returns object priority
249   int                             getPriority() const { return myPriority; }
250
251         //! The function enables and disables antialiasing in Open GL (for points, lines and polygons).
252         void                            setAntialiasing(const bool on);
253   
254   //! Clears all generated textures
255   static void                     destroyAllTextures();
256   
257   //! A function translate object in to HPGL file on disk
258   /*!
259    *\param hFile     the name of PostScript file chosen by user
260    *\param aViewerCS the GLViewer_CoordSystem of window
261    *\param aHPGLCS   the GLViewer_CoordSystem of PostScript page
262   */
263   virtual bool                    translateToHPGL( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aHPGLCS );
264   
265   //! A function translate object in to PostScript file on disk
266   /*!
267    *\param hFile     the name of PostScript file chosen by user
268    *\param aViewerCS the GLViewer_CoordSystem of window
269    *\param aPSCS     the GLViewer_CoordSystem of PostScript page
270   */
271   virtual bool                    translateToPS( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aPSCS ); 
272   
273 #ifdef WIN32
274   //! A function translate object in to EMF file on disk
275   /*!
276    *\warning WIN32 only
277    *
278    *\param dc        the name of HDC associated with file chosen by user
279    *\param aViewerCS the GLViewer_CoordSystem of window
280    *\param aEMFCS    the GLViewer_CoordSystem of EMF page
281   */
282   virtual bool                    translateToEMF( HDC hDC, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aEMFCS );
283 #endif
284   
285   //! Loads texture from file
286   /*!
287    *\param fileName - the name of texture file
288    *\param x_size   - the horizontal size of picture ( less or equal texture horizontal size )
289    *\param y_size   - the vertical size of picture ( less or equal texture vertical size )
290    *\param t_size   - the size of texture ( texture vertical size equals texture horizontal size )
291   */
292   static GLuint                   loadTexture( const QString& fileName,
293                                                GLint* x_size = 0,
294                                                GLint* y_size = 0,
295                                                GLint* t_size = 0);
296
297   //! Draw square texture
298   /*!
299    *\param texture - the texture ID
300    *\param size    - the size of square texture
301    *\param x       - x coord
302    *\param y       - y coord
303   */
304   void                            drawTexture( GLuint texture,
305                                                GLint size,
306                                                GLfloat x,
307                                                GLfloat y );
308
309   //! Draw texture
310   /*!
311    *\param texture - the texture ID
312    *\param x_size  - the horizontal size of texture
313    *\param y_size  - the vertical size of texture
314    *\param x       - x coord
315    *\param y       - y coord
316   */
317   void                            drawTexture( GLuint texture,
318                                                GLint x_size,
319                                                GLint y_size,
320                                                GLfloat x,
321                                                GLfloat y );
322
323   //! Draw texture part
324   /*!
325    *\param texture - the texture ID
326    *\param x_ratio - the horizontal ratio of texture part
327    *\param y_ratio - the vertical ratio of texture part
328    *\param x_size  - the horizontal size of texture
329    *\param y_size  - the vertical size of texture
330    *\param x       - x coord
331    *\param y       - y coord
332    *\param scale   - common scale factor ( if = 0, use drawer scales )
333   */
334   void                            drawTexturePart( GLuint texture,
335                                                    GLfloat x_ratio,
336                                                    GLfloat y_ratio,
337                                                    GLfloat x_size,
338                                                    GLfloat y_size,
339                                                    GLfloat x,
340                                                    GLfloat y,
341                                                    GLfloat scale = 0 );
342
343
344
345   //! Draw text string
346   /*!
347    *\param text              - the text string
348    *\param xPos              - x coord
349    *\param yPos              - y coord
350    *\param color             - text color
351    *\param aFont             - base font of text
352    *\param theSeparator      - letter separator
353    *\param DisplayTextFormat - text format
354   */
355   void                            drawText( const QString& text,
356                                             GLfloat xPos,
357                                                                           GLfloat yPos,
358                                             const QColor& color,
359                                             QFont* aFont,
360                                             int theSeparator,
361                                             DisplayTextFormat = DTF_BITMAP );
362
363   //! Draw text string
364   /*!
365    *\param text      - the text string
366    *\param x         - x coord
367    *\param y         - y coord
368    *\param hPosition - horizontal alignment
369    *\param vPosition - vertical alignment
370    *\param color     - text color
371    *\param smallFont - font format
372   */
373   void                            drawGLText( QString text,
374                                                                             float x,
375                                                                             float y,
376                                               int hPosition = GLText_Center,
377                                               int vPosition = GLText_Center,
378                                               QColor color = Qt::black,
379                                               bool smallFont = false );
380
381   //! Sets a default font to be used by drawGLText method
382   /*!
383    *\param font      - the default font
384   */
385   inline void                     setFont( const QFont& font ) { myFont = font; }
386
387   //! Returns a default font used by drawGLText method
388   inline QFont                    font() const { return myFont; }
389
390   //! Sets a default text displaying format to be used by drawGLText method
391   /*!
392    *\param format    - the default text displaying format
393   */
394   inline void                     setTextFormat( const DisplayTextFormat format ) { myTextFormat = format; }
395
396   //! Returns a default text displaying format used by drawGLText method
397   inline DisplayTextFormat        textFormat() const { return myTextFormat; }
398
399   //! Sets a text string displaying scale factor (used only with text format DTF_TEXTURE_SCALABLE)
400   /*!
401    *\param factor    - scale factor
402   */
403   inline void                     setTextScale( const GLfloat factor ) { myTextScale = factor; }
404
405   //! Returns a text string displaying scale factor
406   inline GLfloat                  textScale() const { return myTextScale; }
407
408   //! Returns a rectangle of text (without viewer scale)
409   GLViewer_Rect                   textRect( const QString& ) const;
410
411
412   //! Draw rectangle with predefined color
413   static void                     drawRectangle( GLViewer_Rect* theRect, QColor = Qt::black );
414
415 protected:
416   //! Draw basic primitives: rectangle, contour, polygon, vertex, cross, arrow
417   //* with predefined color
418   static void                     drawRectangle( GLViewer_Rect*, GLfloat, GLfloat = 0, QColor = Qt::black, 
419                                                  bool = false, QColor = Qt::white );
420   static void                     drawContour( GLViewer_Rect*, QColor, GLfloat, GLushort, bool );
421   static void                     drawContour( const GLViewer_PntList&, QColor, GLfloat );
422   static void                     drawPolygon( GLViewer_Rect*, QColor, GLushort, bool );
423   static void                     drawPolygon( const GLViewer_PntList&, QColor );
424   static void                     drawVertex( GLfloat, GLfloat, QColor );
425   static void                     drawCross( GLfloat, GLfloat, QColor );
426   static void                     drawArrow( const GLfloat red, const GLfloat green, const GLfloat blue,
427                                              GLfloat, GLfloat, GLfloat, GLfloat,
428                                              GLfloat, GLfloat, GLfloat, GLboolean = GL_FALSE );
429
430   //! Draw object text
431   virtual void                    drawText( GLViewer_Object* theObject );
432
433   //! X Scale factor
434   float                           myXScale;
435   //! Y scale factor
436   float                           myYScale;
437   
438   //! List of objects
439   QValueList<GLViewer_Object*>    myObjects;
440   //! List generated textures
441   GLuint                          myTextList;
442   
443   //! Type of supporting object
444   QString                         myObjectType;
445   //! Dislay priority
446   int                             myPriority;
447
448   //! Default font for drawGLText() method
449   QFont                           myFont;
450   //! Default text displaying format for drawGLText() method
451   DisplayTextFormat               myTextFormat;
452
453   //! Scale factor for text string draw, by default 0.125
454   //! (used only with text format DTF_TEXTURE_SCALABLE)
455   GLfloat                         myTextScale;
456 };
457
458 #ifdef WNT
459 #pragma warning ( default:4251 )
460 #endif
461
462 #endif // GLVIEWER_DRAWER_H