Salome HOME
04cabff706580a8c8a2c5f743a9960e9ed92fd9b
[modules/gui.git] / src / GLViewer / GLViewer_BaseDrawers.cxx
1 // Copyright (C) 2007-2014  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_BaseDrawers.cxx
25 // Created:   November, 2004
26 //#include <GLViewerAfx.h>
27 //
28 #include "GLViewer_BaseDrawers.h"
29 #include "GLViewer_Object.h"
30 #include "GLViewer_Text.h"
31 #include "GLViewer_AspectLine.h"
32 #include "GLViewer_BaseObjects.h"
33
34 // Qt includes
35 #include <QColor>
36
37
38 #ifndef WIN32
39 #include <GL/glx.h>
40 #endif
41
42 GLfloat sin_table[SEGMENTS];
43 GLfloat cos_table[SEGMENTS];
44
45 /*!
46   Default constructor
47 */
48 GLViewer_MarkerDrawer::GLViewer_MarkerDrawer()
49 : GLViewer_Drawer()
50 {
51     GLfloat angle = 0.0;
52     for ( int i = 0; i < SEGMENTS; i++ )
53     {
54         sin_table[i] = sin( angle );
55         cos_table[i] = cos( angle );
56         angle += float( STEP );
57     }
58     myObjectType = "GLViewer_MarkerSet";
59 }
60
61 /*!
62   Destructor
63 */
64 GLViewer_MarkerDrawer::~GLViewer_MarkerDrawer()
65 {
66 }
67
68 /*! Draws object in GLViewer
69   \param xScale - current scale along X-direction
70   \param yScale - current scale along Y-direction
71   \param onlyUpdate - = true if only update highlight-select information
72 */
73 void GLViewer_MarkerDrawer::create( float xScale, float yScale, bool onlyUpdate )
74 {
75     QList<int>::Iterator it;
76     QList<int>::Iterator EndIt;
77     QList<GLViewer_Object*>::Iterator anObjectIt = myObjects.begin();
78     QList<GLViewer_Object*>::Iterator anEndObjectIt = myObjects.end();
79
80     myXScale = xScale;
81     myYScale = yScale;
82
83     QColor colorN, colorH, colorS;
84
85     GLViewer_MarkerSet* aMarkerSet = NULL;
86     GLViewer_AspectLine* anAspectLine = NULL;
87
88     for( ; anObjectIt != anEndObjectIt; anObjectIt++ )
89     {
90         aMarkerSet = ( GLViewer_MarkerSet* )(*anObjectIt);
91         anAspectLine = aMarkerSet->getAspectLine();
92         anAspectLine->getLineColors( colorN, colorH, colorS );
93
94         float* aXCoord = aMarkerSet->getXCoord();
95         float* anYCoord = aMarkerSet->getYCoord();
96         float aRadius = aMarkerSet->getMarkerSize();
97
98         QList<int> aHNumbers, anUHNumbers, aSelNumbers, anUSelNumbers;
99         aMarkerSet->exportNumbers( aHNumbers, anUHNumbers, aSelNumbers, anUSelNumbers );
100
101         if( onlyUpdate )
102         {
103             EndIt = anUHNumbers.end();
104             for( it = anUHNumbers.begin(); it != EndIt; ++it )
105             {
106                 drawMarker( aXCoord[*it], anYCoord[*it], aRadius, colorN, anAspectLine );
107             }
108
109             EndIt = anUSelNumbers.end();
110             for( it = anUSelNumbers.begin(); it != EndIt; ++it )
111                 drawMarker( aXCoord[*it], anYCoord[*it], aRadius, colorN, anAspectLine );
112
113             EndIt = aSelNumbers.end();
114             for( it = aSelNumbers.begin(); it != EndIt; ++it )
115                 drawMarker( aXCoord[*it], anYCoord[*it], aRadius, colorS, anAspectLine );
116
117             EndIt = aHNumbers.end();
118             for( it = aHNumbers.begin(); it != EndIt; ++it )
119             {
120                 drawMarker( aXCoord[*it], anYCoord[*it], aRadius, colorH, anAspectLine );
121             }
122         }
123         else
124         {
125             int aNumber = aMarkerSet->getNumMarkers();
126             for( int i = 0; i < aNumber; i++ )
127                 drawMarker( aXCoord[i], anYCoord[i], aRadius, colorN, anAspectLine );
128
129             EndIt = anUSelNumbers.end();
130             for( it = anUSelNumbers.begin(); it != EndIt; ++it )
131                 drawMarker( aXCoord[*it], anYCoord[*it], aRadius, colorN, anAspectLine );
132
133             EndIt = aSelNumbers.end();
134             for( it = aSelNumbers.begin(); it != EndIt; ++it )
135                 drawMarker( aXCoord[*it], anYCoord[*it], aRadius, colorS, anAspectLine );
136         }
137         if( aMarkerSet->getGLText()->getText() != "" )
138         {
139             //float aXPos = 0, anYPos = 0;
140             //aMarkerSet->getGLText()->getPosition( aXPos, anYPos );
141             //drawText( aMarkerSet->getGLText()->getText(), aXPos, anYPos, colorN, &aMarkerSet->getGLText()->getFont(), aMarkerSet->getGLText()->getSeparator() );
142             drawText( aMarkerSet );
143         }
144     }
145 }
146
147 /*! Draws marker
148   \param theXCoord - X position
149   \param theYCoord - Y position
150   \param theRadius - radius
151   \param theColor - color
152   \param theAspectLine - line aspect
153 */
154 void GLViewer_MarkerDrawer::drawMarker( float& theXCoord, float& theYCoord,
155                                      float& theRadius, QColor& theColor, GLViewer_AspectLine* theAspectLine )
156 {
157     glColor3f( ( GLfloat )theColor.red() / 255, 
158                ( GLfloat )theColor.green() / 255, 
159                ( GLfloat )theColor.blue() / 255 );
160
161     glLineWidth( theAspectLine->getLineWidth() );
162
163     if ( theAspectLine->getLineType() == 0 )
164         glBegin( GL_LINE_LOOP );
165     else
166         glBegin( GL_LINE_STRIP);
167
168     for ( int i = 0; i < SEGMENTS; i++ )
169         glVertex2f( theXCoord + cos_table[i] * theRadius / myXScale,
170                     theYCoord + sin_table[i] * theRadius / myYScale );
171     glEnd();
172 }
173
174
175 /*!
176   Default constructor
177 */
178 GLViewer_PolylineDrawer::GLViewer_PolylineDrawer()
179 :GLViewer_Drawer()
180 {
181     myObjectType = "GLViewer_Polyline";
182 }
183
184 /*!
185   Destructor
186 */
187 GLViewer_PolylineDrawer::~GLViewer_PolylineDrawer()
188 {
189 }
190
191 /*! Draws object in GLViewer
192   \param xScale - current scale along X-direction
193   \param yScale - current scale along Y-direction
194   \param onlyUpdate - = true if only update highlight-select information
195 */
196 void GLViewer_PolylineDrawer::create( float xScale, float yScale, bool onlyUpdate )
197 {
198     QList<GLViewer_Object*>::Iterator aObjectIt = myObjects.begin();
199     QList<GLViewer_Object*>::Iterator aObjectEndIt = myObjects.end();
200     
201     myXScale = xScale;
202     myYScale = yScale;
203
204     QColor color, colorN, colorH, colorS;
205     GLViewer_AspectLine* anAspect = NULL;
206     GLViewer_Polyline* aPolyline = NULL;
207     for( ; aObjectIt != aObjectEndIt; aObjectIt++ )
208     {
209         anAspect = (*aObjectIt)->getAspectLine();
210         aPolyline = (GLViewer_Polyline*)(*aObjectIt);
211
212
213         anAspect->getLineColors( colorN, colorH, colorS );
214         if( onlyUpdate )
215         {
216             if( aPolyline->isHighlighted() )
217                 color = colorH;
218             else if( aPolyline->isSelected() )
219                 color = colorS;
220             else
221                 color = colorN;
222         }
223         else
224         {
225             if( aPolyline->isSelected() )
226                 color = colorS;
227             else
228                 color = colorN;
229         }
230
231         float* aXCoord = aPolyline->getXCoord();
232         float* anYCoord = aPolyline->getYCoord();
233         int aSize = aPolyline->getNumber();        
234
235         glColor3f( ( GLfloat )color.red() / 255, 
236                    ( GLfloat )color.green() / 255, 
237                    ( GLfloat )color.blue() / 255 );
238
239         glLineWidth( anAspect->getLineWidth() );
240
241         if ( anAspect->getLineType() == 0 )
242             glBegin( GL_LINE_LOOP );
243         else
244             glBegin( GL_LINE_STRIP);
245
246         for( int i = 0; i < aSize ; i++ )
247              glVertex2f( aXCoord[ i ], anYCoord[ i ] );        
248  
249         if( aPolyline->isClosed() )
250             glVertex2f( aXCoord[ 0 ], anYCoord[ 0 ] );
251
252         glEnd();       
253
254         if( aPolyline->getGLText()->getText() != "" )
255         {
256             //float aXPos = 0, anYPos = 0;
257             //aPolyline->getGLText()->getPosition( aXPos, anYPos );
258             //drawText( aPolyline->getGLText()->getText(), aXPos, anYPos, color, &aPolyline->getGLText()->getFont(), aPolyline->getGLText()->getSeparator() );
259           drawText( aPolyline );
260         }
261     }
262 }
263
264 /*!
265   Default constructor
266 */
267 GLViewer_TextDrawer::GLViewer_TextDrawer()
268 : GLViewer_Drawer()
269 {
270     myObjectType = "GLViewer_TextObject";
271 }
272
273 /*!
274   Destructor
275 */
276 GLViewer_TextDrawer::~GLViewer_TextDrawer()
277 {
278 }
279
280 /*! Draws object in GLViewer
281   \param xScale - current scale along X-direction
282   \param yScale - current scale along Y-direction
283   \param onlyUpdate - = true if only update highlight-select information
284 */
285 void GLViewer_TextDrawer::create( float xScale, float yScale, bool onlyUpdate )
286 {
287     QList<GLViewer_Object*>::Iterator aObjectIt = myObjects.begin();
288     QList<GLViewer_Object*>::Iterator aObjectEndIt = myObjects.end();
289     
290     myXScale = xScale;
291     myYScale = yScale;
292
293     QColor color, colorN, colorH, colorS;
294     GLViewer_AspectLine* anAspect = NULL;    
295     GLViewer_TextObject* anObject = NULL;
296     //float aXPos = 0, anYPos = 0;
297     for( ; aObjectIt != aObjectEndIt; aObjectIt++ )
298     {
299         anObject = (GLViewer_TextObject*)(*aObjectIt);
300         anAspect = anObject->getAspectLine();    
301
302         anAspect->getLineColors( colorN, colorH, colorS );
303         if( onlyUpdate )
304         {
305             if( anObject->isHighlighted() )
306                 color = colorH;
307             else if( anObject->isSelected() )
308                 color = colorS;
309             else
310                 color = colorN;
311         }
312         else
313         {
314             if( anObject->isSelected() )
315                 color = colorS;
316             else
317                 color = colorN;
318         }        
319         
320         //anObject->getGLText()->getPosition( aXPos, anYPos );
321         //drawText( anObject->getGLText()->getText(), aXPos, anYPos, color, &(anObject->getGLText()->getFont()), anObject->getGLText()->getSeparator() );
322         drawText( anObject );
323     }
324 }
325
326 /*!
327   Updates objects after updating font
328 */
329 void GLViewer_TextDrawer::updateObjects()
330 {
331     QList<GLViewer_Object*>::Iterator aObjectIt = myObjects.begin();
332     QList<GLViewer_Object*>::Iterator aObjectEndIt = myObjects.end();
333     for( ; aObjectIt != aObjectEndIt; aObjectIt++ )
334         (*aObjectIt)->compute();
335 }