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