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