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