Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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   Font for GLViewer_Drawer, Drawing bitmap and texture fonts in GLViewer
104 */
105
106 class GLVIEWER_API GLViewer_TexFont
107 {
108 public:
109   //! A default constructor
110   GLViewer_TexFont();
111   //! A constructor
112   /*
113   * \param theFont         - a base font
114   * \param theSeparator    - separator between letters
115   * \param theIsResizeable - specifies whether text drawn by this object can be scaled along with the scene
116   * \param theMinMagFilter - min/mag filter, affects text sharpness
117   */
118   GLViewer_TexFont( QFont* theFont, 
119                     int theSeparator = 2, 
120                     bool theIsResizeable = false, 
121                     GLuint theMinMagFilter = GL_LINEAR/*_ATTENUATION*/ );
122   //! A destructor
123   ~GLViewer_TexFont();
124   
125   //! Generating font texture
126   bool            generateTexture();
127   //! Drawing string theStr in point with coords theX and theY
128   void            drawString( QString  theStr,
129                               GLdouble theX = 0.0,
130                               GLdouble theY = 0.0,
131                               GLfloat  theScale = 1.0 );
132   
133   //! Returns separator between letters
134   int             getSeparator(){ return mySeparator; }
135   //! Installing separator between letters
136   void            setSeparator( int theSeparator ){ mySeparator = theSeparator; }
137   
138   //! Returns width of string in pixels
139   int             getStringWidth( QString theString );
140   //! Returns height of string in pixels
141   int             getStringHeight();
142   
143   //! Clears all generated fonts
144   static void     clearTextBases();
145
146   //! Map for strorage generated texture fonts
147   static QMap<GLViewer_TexFindId,GLViewer_TexIdStored> TexFontBase;
148   //! Map for strorage generated bitmaps fonts
149   static QMap<GLViewer_TexFindId,GLuint>               BitmapFontCache;
150
151 private:
152   //! Initializes font parameters
153   void            init();
154   
155 private:
156   //! Number of characters in the font texture
157   int             myNbSymbols;
158   //! Array of letter width
159   int*            myWidths;
160   //! Array of letter positions in texture
161   int*            myPositions;
162   //! Pointer to base font
163   QFont           myQFont;
164   //! Font texture ID
165   GLuint          myTexFont;
166   //! Font texture width
167   int             myTexFontWidth;
168   //! Font texture height
169   int             myTexFontHeight;
170   //! Separator between letters
171   int             mySeparator;
172   //! Flag controlling scalability of this texmapped font
173   bool            myIsResizeable;
174   //! Min/mag filter
175   GLuint          myMinMagFilter;
176   //! Font height
177   int             myFontHeight;
178   //! Diagnostic information
179   int             myMaxRowWidth;
180 };
181
182 /*! 
183   \class GLViewer_Drawer
184   Drawer for GLViewer_Objects.
185   Drawer creates only one times per one type of object
186 */
187 class GLVIEWER_API GLViewer_Drawer
188 {
189 public:
190   //! Text position relatively object
191   enum
192   {
193     GLText_Center = 0,
194     GLText_Left,
195     GLText_Right,
196     GLText_Top,
197     GLText_Bottom
198   };
199
200   // Objects status ( needs for change colors )
201   //enum ObjectStatus
202   //{
203   //  OS_Normal = 0,
204   //  OS_Highlighted,
205   //  OS_Selected
206   //};
207   
208   // 
209   //enum ClosedStatus
210   //{
211   //  CS_CLOSED = 0,
212   //  CS_OPEN = 1
213   //};  
214
215   //! A constructor
216   GLViewer_Drawer();
217   //! A destructor
218   virtual ~GLViewer_Drawer();
219   
220   //! Main method which drawing object in GLViewer
221   /*
222   *\param xScale - current scale along X-direction
223   *\param yScale - current scale along Y-direction
224   *\param onlyUpdate - = true if only update highlight-select information
225   */
226   virtual void                    create( float xScale, float yScale, bool onlyUpdate ) = 0;  
227   
228   //! Adds object to drawer display list
229   virtual void                    addObject( GLViewer_Object* theObject ){ myObjects.append( theObject ); }
230   //! Clears drawer display list
231   virtual void                    clear(){ myObjects.clear(); }
232   
233   //! Returns object type (needs for dynamic search of right drawer ) 
234   QString                         getObjectType() const { return myObjectType; }
235
236   //! Returns object priority
237   int                             getPriority() const { return myPriority; }
238
239         //! The function enables and disables antialiasing in Open GL (for points, lines and polygons).
240         void                            setAntialiasing(const bool on);
241   
242   //! Clears all generated textures
243   static void                     destroyAllTextures();
244   
245   //! A function translate object in to HPGL file on disk
246   /*!
247    *\param hFile     the name of PostScript file chosen by user
248    *\param aViewerCS the GLViewer_CoordSystem of window
249    *\param aHPGLCS   the GLViewer_CoordSystem of PostScript page
250   */
251   virtual bool                    translateToHPGL( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aHPGLCS );
252   
253   //! A function translate object in to PostScript file on disk
254   /*!
255    *\param hFile     the name of PostScript file chosen by user
256    *\param aViewerCS the GLViewer_CoordSystem of window
257    *\param aPSCS     the GLViewer_CoordSystem of PostScript page
258   */
259   virtual bool                    translateToPS( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aPSCS ); 
260   
261 #ifdef WIN32
262   //! A function translate object in to EMF file on disk
263   /*!
264    *\warning WIN32 only
265    *
266    *\param dc        the name of HDC associated with file chosen by user
267    *\param aViewerCS the GLViewer_CoordSystem of window
268    *\param aEMFCS    the GLViewer_CoordSystem of EMF page
269   */
270   virtual bool                    translateToEMF( HDC hDC, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aEMFCS );
271 #endif
272   
273   //! Loads texture from file
274   /*!
275    *\param fileName - the name of texture file
276    *\param x_size   - the horizontal size of picture ( less or equal texture horizontal size )
277    *\param y_size   - the vertical size of picture ( less or equal texture vertical size )
278    *\param t_size   - the size of texture ( texture vertical size equals texture horizontal size )
279   */
280   static GLuint                   loadTexture( const QString& fileName,
281                                                GLint* x_size = 0,
282                                                GLint* y_size = 0,
283                                                GLint* t_size = 0);
284
285   //! Draw square texture
286   /*!
287    *\param texture - the texture ID
288    *\param size    - the size of square texture
289    *\param x       - x coord
290    *\param y       - y coord
291   */
292   void                            drawTexture( GLuint texture,
293                                                GLint size,
294                                                GLfloat x,
295                                                GLfloat y );
296
297   //! Draw texture
298   /*!
299    *\param texture - the texture ID
300    *\param x_size  - the horizontal size of texture
301    *\param y_size  - the vertical size of texture
302    *\param x       - x coord
303    *\param y       - y coord
304   */
305   void                            drawTexture( GLuint texture,
306                                                GLint x_size,
307                                                GLint y_size,
308                                                GLfloat x,
309                                                GLfloat y );
310
311   //! Draw texture part
312   /*!
313    *\param texture - the texture ID
314    *\param x_ratio - the horizontal ratio of texture part
315    *\param y_ratio - the vertical ratio of texture part
316    *\param x_size  - the horizontal size of texture
317    *\param y_size  - the vertical size of texture
318    *\param x       - x coord
319    *\param y       - y coord
320    *\param scale   - common scale factor ( if = 0, use drawer scales )
321   */
322   void                            drawTexturePart( GLuint texture,
323                                                    GLfloat x_ratio,
324                                                    GLfloat y_ratio,
325                                                    GLfloat x_size,
326                                                    GLfloat y_size,
327                                                    GLfloat x,
328                                                    GLfloat y,
329                                                    GLfloat scale = 0 );
330
331
332
333   //! Draw text string
334   /*!
335    *\param text              - the text string
336    *\param xPos              - x coord
337    *\param yPos              - y coord
338    *\param color             - text color
339    *\param aFont             - base font of text
340    *\param theSeparator      - letter separator
341    *\param DisplayTextFormat - text format
342   */
343   void                            drawText( const QString& text,
344                                             GLfloat xPos,
345                                                                           GLfloat yPos,
346                                             const QColor& color,
347                                             QFont* aFont,
348                                             int theSeparator,
349                                             DisplayTextFormat = DTF_BITMAP );
350
351   //! Draw text string
352   /*!
353    *\param text      - the text string
354    *\param x         - x coord
355    *\param y         - y coord
356    *\param hPosition - horizontal alignment
357    *\param vPosition - vertical alignment
358    *\param color     - text color
359    *\param smallFont - font format
360   */
361   void                            drawGLText( QString text,
362                                                                             float x,
363                                                                             float y,
364                                               int hPosition = GLText_Center,
365                                               int vPosition = GLText_Center,
366                                               QColor color = Qt::black,
367                                               bool smallFont = false );
368
369   //! Sets a default font to be used by drawGLText method
370   /*!
371    *\param font      - the default font
372   */
373   inline void                     setFont( const QFont& font ) { myFont = font; }
374
375   //! Returns a default font used by drawGLText method
376   inline QFont                    font() const { return myFont; }
377
378   //! Sets a default text displaying format to be used by drawGLText method
379   /*!
380    *\param format    - the default text displaying format
381   */
382   inline void                     setTextFormat( const DisplayTextFormat format ) { myTextFormat = format; }
383
384   //! Returns a default text displaying format used by drawGLText method
385   inline DisplayTextFormat        textFormat() const { return myTextFormat; }
386
387   //! Sets a text string displaying scale factor (used only with text format DTF_TEXTURE_SCALABLE)
388   /*!
389    *\param factor    - scale factor
390   */
391   inline void                     setTextScale( const GLfloat factor ) { myTextScale = factor; }
392
393   //! Returns a text string displaying scale factor
394   inline GLfloat                  textScale() const { return myTextScale; }
395
396   //! Returns a rectangle of text (without viewer scale)
397   GLViewer_Rect                   textRect( const QString& ) const;
398
399
400   //! Draw rectangle with predefined color
401   static void                     drawRectangle( GLViewer_Rect* theRect, QColor = Qt::black );
402
403 protected:
404   //! Draw basic primitives: rectangle, contour, polygon, vertex, cross, arrow
405   //* with predefined color
406   static void                     drawRectangle( GLViewer_Rect*, GLfloat, GLfloat = 0, QColor = Qt::black, 
407                                                  bool = false, QColor = Qt::white );
408   static void                     drawContour( GLViewer_Rect*, QColor, GLfloat, GLushort, bool );
409   static void                     drawContour( const GLViewer_PntList&, QColor, GLfloat );
410   static void                     drawPolygon( GLViewer_Rect*, QColor, GLushort, bool );
411   static void                     drawPolygon( const GLViewer_PntList&, QColor );
412   static void                     drawVertex( GLfloat, GLfloat, QColor );
413   static void                     drawCross( GLfloat, GLfloat, QColor );
414   static void                     drawArrow( const GLfloat red, const GLfloat green, const GLfloat blue,
415                                              GLfloat, GLfloat, GLfloat, GLfloat,
416                                              GLfloat, GLfloat, GLfloat, GLboolean = GL_FALSE );
417
418   //! Draw object text
419   virtual void                    drawText( GLViewer_Object* theObject );
420
421   //! X Scale factor
422   float                           myXScale;
423   //! Y scale factor
424   float                           myYScale;
425   
426   //! List of objects
427   QValueList<GLViewer_Object*>    myObjects;
428   //! List generated textures
429   GLuint                          myTextList;
430   
431   //! Type of supporting object
432   QString                         myObjectType;
433   //! Dislay priority
434   int                             myPriority;
435
436   //! Default font for drawGLText() method
437   QFont                           myFont;
438   //! Default text displaying format for drawGLText() method
439   DisplayTextFormat               myTextFormat;
440
441   //! Scale factor for text string draw, by default 0.125
442   //! (used only with text format DTF_TEXTURE_SCALABLE)
443   GLfloat                         myTextScale;
444 };
445
446 #ifdef WNT
447 #pragma warning ( default:4251 )
448 #endif
449
450 #endif // GLVIEWER_DRAWER_H