1 // File: GLViewer_Context.cxx
2 // Created: November, 2004
4 // Copyright (C) CEA 2004
6 /****************************************************************************
7 ** Class: GLViewer_Context
8 ** Descr: OpenGL Context
10 ** Created: UI team, 20.09.02
11 *****************************************************************************/
13 //#include <GLViewerAfx.h>
15 #include "GLViewer_Context.h"
17 #include "GLViewer_Group.h"
18 #include "GLViewer_Object.h"
19 #include "GLViewer_Viewer2d.h"
20 #include "GLViewer_ViewPort2d.h"
22 #include <TColStd_SequenceOfInteger.hxx>
26 GLViewer_Context::GLViewer_Context( GLViewer_Viewer2d* v ) :
28 myHighlightColor( Quantity_NOC_CYAN1 ),
29 mySelectionColor( Quantity_NOC_RED ),
30 myTolerance( TOLERANCE )
35 myLastPickedChanged = false;
43 GLViewer_Context::~GLViewer_Context()
45 myActiveObjects.clear();
46 myInactiveObjects.clear();
47 mySelectedObjects.clear();
50 int GLViewer_Context::MoveTo( int xi, int yi, bool byCircle )
52 GLfloat x = (GLfloat)xi;
53 GLfloat y = (GLfloat)yi;
54 myGLViewer2d->transPoint( x, y );
59 GLboolean isHigh = GL_FALSE;
60 GLboolean onObject = GL_FALSE;
62 GLViewer_Object* aPrevLastPicked = myLastPicked;
63 GLViewer_Object* lastPicked = 0;
65 ObjList anUpdatedObjects;
67 if( myActiveObjects.isEmpty() )
70 ObjList::iterator it = myActiveObjects.end();
71 ObjList::iterator itEnd = myActiveObjects.begin();
74 GLViewer_Object* object = *it;
76 GLViewer_Rect* rect = object->getUpdateRect();
77 if( rect->contains( GLViewer_Pnt( x, y ) ) )
80 object->highlight( x, y, myTolerance, GL_FALSE );
81 isHigh = object->isHighlighted();
96 myLastPicked = lastPicked;
103 it = myActiveObjects.begin();
104 itEnd = myActiveObjects.end();
106 for( ; it != itEnd; ++it )
107 (*it)->unhighlight();
109 anUpdatedObjects.append( (*it) );
112 myLastPickedChanged = aPrevLastPicked != myLastPicked;
114 if( myLastPickedChanged )
115 myGLViewer2d->updateAll();
120 if( !myLastPicked && isHigh )
123 myLastPicked = lastPicked;
124 anUpdatedObjects.append( myLastPicked );
126 else if( myLastPicked && !isHigh )
129 myLastPicked->unhighlight();
130 anUpdatedObjects.append( myLastPicked );
133 else if( myLastPicked && isHigh )
136 //myLastPicked->highlight( x, y, myTolerance, byCircle );
137 if( myLastPicked != lastPicked )
139 myLastPicked->unhighlight();
140 if( myLastPicked != lastPicked )
142 myLastPicked = lastPicked;
143 anUpdatedObjects.append( myLastPicked );
148 myLastPickedChanged = ( aPrevLastPicked != myLastPicked );
150 if( myLastPickedChanged || myUpdateAll )
151 myGLViewer2d->updateAll();
153 myGLViewer2d->activateDrawers( anUpdatedObjects, TRUE, TRUE );
158 int GLViewer_Context::Select( bool Append, bool byCircle )
160 ObjList::Iterator it, itEnd, oit, oitEnd;
161 SelectionStatus status = SS_Invalid;
163 bool updateAll = false;
168 return status;//invalid
170 if( myHFlag && myLastPicked )
172 if( mySelectedObjects.count() == 1 && mySelectedObjects.first() == myLastPicked )
173 status = SS_LocalChanged;
177 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end() ; it != itEnd; ++it )
178 if( myLastPicked != *it )
180 updateAll = (*it)->unselect() || updateAll;
184 if( updateAll || myUpdateAll )
185 myGLViewer2d->updateAll();
187 myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
189 if( mySelectedObjects.count() != 0 && status == SS_Invalid )
190 status = SS_GlobalChanged;
191 mySelectedObjects.clear();
194 if ( myLastPicked->select( myXhigh, myYhigh, myTolerance, GLViewer_Rect(), false, byCircle, Append )
195 && mySelectedObjects.findIndex( myLastPicked ) == -1 )
197 mySelectedObjects.append( myLastPicked );
198 myGLViewer2d->activateDrawer( myLastPicked, TRUE, TRUE );
200 if( status == SS_Invalid )
201 status = SS_GlobalChanged;
203 else if( status = SS_LocalChanged )
204 status = SS_GlobalChanged;
209 if( myHFlag && !myLastPicked )
213 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end() ; it != itEnd; ++it )
214 if ( myLastPicked != *it )
216 updateAll = (*it)->unselect() || updateAll;
220 if( updateAll || myUpdateAll )
221 myGLViewer2d->updateAll();
223 myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
225 if( mySelectedObjects.count() != 0 )
226 status = SS_GlobalChanged;
228 mySelectedObjects.clear();
238 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )myGLViewer2d->getActiveView()->getViewPort();
239 vp->getScale( aXScale, aYScale );
243 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end() ; it != itEnd; ++it )
244 if( myLastPicked != *it )
246 updateAll = (*it)->unselect() || updateAll;
250 if( updateAll || myUpdateAll )
251 myGLViewer2d->updateAll();
253 myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
255 if( mySelectedObjects.count() != 0 )
256 status = SS_GlobalChanged;
258 mySelectedObjects.clear();
261 for( oit = myActiveObjects.begin(), oitEnd = myActiveObjects.end(); oit != oitEnd; ++oit )
263 (*oit)->setScale( aXScale, aYScale );
264 GLViewer_Rect* rect = (*oit)->getUpdateRect();
266 if( rect->contains( GLViewer_Pnt( myXhigh, myXhigh ) ) )
268 (*oit)->select( myXhigh, myYhigh, myTolerance, GLViewer_Rect(), false, byCircle, Append );
269 isSel = (*oit)->isSelected();
274 mySelectedObjects.append( myLastPicked );
275 myGLViewer2d->activateDrawer( myLastPicked, TRUE, TRUE );
276 status = SS_GlobalChanged;
285 int GLViewer_Context::SelectByRect( const QRect& theRect, bool Append )
289 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )myGLViewer2d->getActiveView()->getViewPort();
290 vp->getScale( aXScale, aYScale );
292 SelectionStatus status = SS_NoChanged;
295 ObjList::Iterator it, itEnd;
297 if ( !mySFlag || myActiveObjects.empty() )
300 bool updateAll = false;
303 if( mySelectedObjects.count() != 0 )
304 status = SS_GlobalChanged;
306 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end(); it != itEnd; ++it )
308 updateAll = (*it)->unselect() || updateAll;
311 mySelectedObjects.clear();
314 for( it = myActiveObjects.begin(), itEnd = myActiveObjects.end(); it != itEnd; ++it )
317 (*it)->setScale( aXScale, aYScale );
318 QRect rect = myGLViewer2d->getQRect( *( (*it)->getRect() ) );
320 if( rect.intersects( theRect ) )
322 GLViewer_Rect aRect = myGLViewer2d->getGLVRect( theRect );
323 (*it)->select( myXhigh, myYhigh, myTolerance, aRect, false, false, Append );
324 isSel = (*it)->isSelected();
327 if( isSel && mySelectedObjects.findIndex( *it ) == -1 )
330 mySelectedObjects.append( *it );
331 status = SS_GlobalChanged;
335 if( updateAll || myUpdateAll )
336 myGLViewer2d->updateAll();
338 myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
343 void GLViewer_Context::SetHighlightColor( Quantity_NameOfColor aCol )
345 myHighlightColor = aCol;
347 Quantity_Color colorH( aCol );
348 int redH = 255 * (int)colorH.Red();
349 int greenH = 255 * (int)colorH.Green();
350 int blueH = 255 * (int)colorH.Blue();
351 QColor colH = QColor( redH, greenH, blueH );
353 Quantity_Color colorS( mySelectionColor );
354 int redS = 255 * (int)colorS.Red();
355 int greenS = 255 * (int)colorS.Green();
356 int blueS = 255 * (int)colorS.Blue();
357 QColor colS = QColor( redS, greenS, blueS );
359 myGLViewer2d->updateColors( colH, colS);
362 void GLViewer_Context::SetSelectionColor( Quantity_NameOfColor aCol )
364 mySelectionColor = aCol;
366 Quantity_Color colorH( myHighlightColor );
367 int redH = 255 * (int)colorH.Red();
368 int greenH = 255 * (int)colorH.Green();
369 int blueH = 255 * (int)colorH.Blue();
370 QColor colH = QColor( redH, greenH, blueH );
372 Quantity_Color colorS( aCol );
373 int redS = 255 * (int)colorS.Red();
374 int greenS = 255 * (int)colorS.Green();
375 int blueS = 255 * (int)colorS.Blue();
376 QColor colS = QColor( redS, greenS, blueS );
378 myGLViewer2d->updateColors( colH, colS);
381 int GLViewer_Context::NbSelected()
383 return mySelectedObjects.count();
386 void GLViewer_Context::InitSelected()
391 bool GLViewer_Context::MoreSelected()
393 return ( mySelCurIndex < NbSelected() );
396 bool GLViewer_Context::NextSelected()
398 if ( mySelCurIndex >= 0 && mySelCurIndex < NbSelected() )
407 GLViewer_Object* GLViewer_Context::SelectedObject()
409 return mySelectedObjects[ mySelCurIndex ];
412 bool GLViewer_Context::isSelected( GLViewer_Object* theObj )
414 return mySelectedObjects.contains( theObj );
417 int GLViewer_Context::insertObject( GLViewer_Object* object, bool display, bool isActive )
419 // cout << "GLViewer_Context::insertObject" << endl;
426 myActiveObjects.append( object );
429 //QRect* rect = object->getRect()->toQRect();
430 //myGLViewer2d->updateBorders( *rect );
431 myGLViewer2d->activateDrawer( object, FALSE );
435 myInactiveObjects.append( object );
437 return myActiveObjects.count() + myInactiveObjects.count();
440 bool GLViewer_Context::replaceObject( GLViewer_Object* oldObject, GLViewer_Object* newObject )
442 if( !oldObject || !newObject )
445 if( myActiveObjects.contains( oldObject ) )
447 myActiveObjects.remove( oldObject );
448 myActiveObjects.append( newObject );
452 if( myInactiveObjects.contains( oldObject ) )
454 myInactiveObjects.remove( oldObject );
455 myInactiveObjects.append( newObject );
462 void GLViewer_Context::updateScales( GLfloat scX, GLfloat scY )
464 if( scX <= 0 || scY <= 0 )
467 ObjList::iterator it, itEnd;
469 for( it = myActiveObjects.begin(), itEnd = myActiveObjects.end(); it != itEnd; ++it )
470 (*it)->setScale( scX, scY );
472 for( it = myInactiveObjects.begin(), itEnd = myInactiveObjects.end(); it != itEnd; ++it )
473 (*it)->setScale( scX, scY );
476 void GLViewer_Context::clearHighlighted()
478 if( myHFlag && myLastPicked )
480 myLastPicked->unhighlight();
483 myGLViewer2d->updateAll();
487 void GLViewer_Context::clearSelected( bool updateViewer )
492 ObjList::Iterator it, itEnd;
495 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end(); it != itEnd; ++it )
502 myGLViewer2d->activateDrawers( aList, TRUE );
503 mySelectedObjects.clear();
506 void GLViewer_Context::setSelected( GLViewer_Object* object, bool updateViewer )
511 if( myActiveObjects.contains( object ) && !mySelectedObjects.contains( object ) )
513 object->setSelected( TRUE );
514 mySelectedObjects.append( object );
518 myGLViewer2d->activateDrawer( object, TRUE, TRUE );
521 void GLViewer_Context::remSelected( GLViewer_Object* object, bool updateViewer )
523 if( !object || !mySelectedObjects.contains( object ) )
526 mySelectedObjects.remove( object );
530 myGLViewer2d->activateDrawer( object, TRUE, TRUE );
533 void GLViewer_Context::eraseObject( GLViewer_Object* theObject, bool theUpdateViewer )
535 if( !theObject || !myActiveObjects.contains( theObject ) )
538 theObject->unhighlight();
539 theObject->unselect();
540 theObject->setVisible( false );
542 if( theUpdateViewer )
543 myGLViewer2d->updateAll();
546 void GLViewer_Context::deleteObject( GLViewer_Object* theObject, bool updateViewer )
549 ( !myActiveObjects.contains( theObject ) && !myInactiveObjects.contains( theObject ) ) )
552 if( myActiveObjects.contains( theObject ) )
553 myActiveObjects.remove( theObject );
554 else if( myInactiveObjects.contains( theObject ) )
555 myInactiveObjects.remove( theObject );
559 if( mySelectedObjects.contains( theObject ) )
560 mySelectedObjects.remove( theObject );
562 GLViewer_Group* aGroup = theObject->getGroup();
564 aGroup->removeObject( theObject );
566 if( myLastPicked == theObject )
569 myGLViewer2d->updateAll();
572 bool GLViewer_Context::setActive( GLViewer_Object* theObject )
574 if( !theObject || !myInactiveObjects.contains( theObject ) )
577 myInactiveObjects.remove( theObject );
578 myActiveObjects.append( theObject );
582 bool GLViewer_Context::setInactive( GLViewer_Object* theObject )
584 if( !theObject || !myActiveObjects.contains( theObject ) )
587 myActiveObjects.remove( theObject );
588 myInactiveObjects.append( theObject );