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