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