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