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