Salome HOME
86ba2a45fddb18f7f530913ef0cdf62854806d18
[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   static GLuint                   loadTexture( const QString& fileName );
255
256   //! Draw square texture
257   /*!
258    *\param texture - the texture ID
259    *\param size    - the size of texture
260    *\param x       - x coord
261    *\param y       - y coord
262   */
263   void                            drawTexture( GLuint texture, GLint size, GLfloat x, GLfloat y );
264
265   //! Draw text string
266   /*!
267    *\param text              - the text string
268    *\param xPos              - x coord
269    *\param yPos              - y coord
270    *\param color             - text color
271    *\param aFont             - base font of text
272    *\param theSeparator      - letter separator
273    *\param DisplayTextFormat - text format
274   */
275   void                            drawText( const QString& text,
276                                             GLfloat xPos,
277                                             GLfloat yPos,
278                                             const QColor& color,
279                                             QFont* aFont,
280                                             int theSeparator,
281                                             DisplayTextFormat = DTF_BITMAP );
282
283   //! Draw text string
284   /*!
285    *\param text      - the text string
286    *\param x         - x coord
287    *\param y         - y coord
288    *\param hPosition - horizontal alignment
289    *\param vPosition - vertical alignment
290    *\param color     - text color
291    *\param smallFont - font format
292   */
293   void                            drawGLText( QString text,
294                                               float x,
295                                               float y,
296                                               int hPosition = GLText_Center,
297                                               int vPosition = GLText_Center,
298                                               QColor color = Qt::black,
299                                               bool smallFont = false );
300
301   //! Sets a default font to be used by drawGLText method
302   /*!
303    *\param font      - the default font
304   */
305   inline void                     setFont( const QFont& font ) { myFont = font; }
306
307   //! Returns a default font used by drawGLText method
308   inline QFont                    font() const { return myFont; }
309
310   //! Sets a default text displaying format to be used by drawGLText method
311   /*!
312    *\param format    - the default text displaying format
313   */
314   inline void                     setTextFormat( DisplayTextFormat format ) { myTextFormat = format; }
315
316   //! Returns a default text displaying format used by drawGLText method
317   inline DisplayTextFormat        textFormat() const { return myTextFormat; }
318
319   //! Draw rectangle with predefined color
320   static void                     drawRectangle( GLViewer_Rect* theRect, QColor = Qt::black );
321
322 protected:
323   //! Draw object text
324   virtual void                    drawText( GLViewer_Object* theObject );
325
326   //! X Scale factor
327   float                           myXScale;
328   //! Y scale factor
329   float                           myYScale;
330   
331   //! List of objects
332   QValueList<GLViewer_Object*>    myObjects;
333   //! List generated textures
334   GLuint                          myTextList;
335   
336   //! Type of supporting object
337   QString                         myObjectType;
338   //! Dislay priority
339   int                             myPriority;
340
341   //! Default font for drawGLText() method
342   QFont                           myFont;
343   //! Default text displaying format for drawGLText() method
344   DisplayTextFormat               myTextFormat;
345 };
346
347 #ifdef WNT
348 #pragma warning ( default:4251 )
349 #endif
350
351 #endif // GLVIEWER_DRAWER_H