1 // File: GLViewer_Context.cxx
2 // Created: November, 2004
4 // Copyright (C) CEA 2004
6 //================================================================
7 // Class : GLViewer_AspectLine
8 // Description : Class for manage of presentations in GLViewer
9 //================================================================
11 #include "GLViewer_Context.h"
13 #include "GLViewer_Group.h"
14 #include "GLViewer_Object.h"
15 #include "GLViewer_Viewer2d.h"
16 #include "GLViewer_ViewPort2d.h"
18 #include <TColStd_SequenceOfInteger.hxx>
22 //=======================================================================
23 // Function: GLViewer_Context
25 //=======================================================================
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 //=======================================================================
44 // Function: ~GLViewer_Context
46 //=======================================================================
47 GLViewer_Context::~GLViewer_Context()
49 myActiveObjects.clear();
50 myInactiveObjects.clear();
51 mySelectedObjects.clear();
54 //=======================================================================
57 //=======================================================================
58 int GLViewer_Context::MoveTo( int xi, int yi, bool byCircle )
60 GLfloat x = (GLfloat)xi;
61 GLfloat y = (GLfloat)yi;
62 myGLViewer2d->transPoint( x, y );
67 GLboolean isHigh = GL_FALSE;
68 GLboolean onObject = GL_FALSE;
70 GLViewer_Object* aPrevLastPicked = myLastPicked;
71 GLViewer_Object* lastPicked = 0;
73 ObjList anUpdatedObjects;
75 if( myActiveObjects.isEmpty() )
78 ObjList::iterator it = myActiveObjects.end();
79 ObjList::iterator itEnd = myActiveObjects.begin();
82 GLViewer_Object* object = *it;
84 GLViewer_Rect* rect = object->getUpdateRect();
85 if( rect->contains( GLViewer_Pnt( x, y ) ) )
88 object->highlight( x, y, myTolerance, GL_FALSE );
89 isHigh = object->isHighlighted();
104 myLastPicked = lastPicked;
111 it = myActiveObjects.begin();
112 itEnd = myActiveObjects.end();
114 for( ; it != itEnd; ++it )
115 (*it)->unhighlight();
117 anUpdatedObjects.append( (*it) );
120 myLastPickedChanged = aPrevLastPicked != myLastPicked;
122 if( myLastPickedChanged )
123 myGLViewer2d->updateAll();
128 if( !myLastPicked && isHigh )
131 myLastPicked = lastPicked;
132 anUpdatedObjects.append( myLastPicked );
134 else if( myLastPicked && !isHigh )
137 myLastPicked->unhighlight();
138 anUpdatedObjects.append( myLastPicked );
141 else if( myLastPicked && isHigh )
144 //myLastPicked->highlight( x, y, myTolerance, byCircle );
145 if( myLastPicked != lastPicked )
147 myLastPicked->unhighlight();
148 if( myLastPicked != lastPicked )
150 myLastPicked = lastPicked;
151 anUpdatedObjects.append( myLastPicked );
156 myLastPickedChanged = ( aPrevLastPicked != myLastPicked );
158 if( myLastPickedChanged || myUpdateAll )
159 myGLViewer2d->updateAll();
161 myGLViewer2d->activateDrawers( anUpdatedObjects, TRUE, TRUE );
166 //=======================================================================
169 //=======================================================================
170 int GLViewer_Context::Select( bool Append, bool byCircle )
172 ObjList::Iterator it, itEnd, oit, oitEnd;
173 SelectionStatus status = SS_Invalid;
175 bool updateAll = false;
180 return status;//invalid
182 if( myHFlag && myLastPicked )
184 if( mySelectedObjects.count() == 1 && mySelectedObjects.first() == myLastPicked )
185 status = SS_LocalChanged;
189 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end() ; it != itEnd; ++it )
190 if( myLastPicked != *it )
192 updateAll = (*it)->unselect() || updateAll;
196 if( updateAll || myUpdateAll )
197 myGLViewer2d->updateAll();
199 myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
201 if( mySelectedObjects.count() != 0 && status == SS_Invalid )
202 status = SS_GlobalChanged;
203 mySelectedObjects.clear();
206 if ( myLastPicked->select( myXhigh, myYhigh, myTolerance, GLViewer_Rect(), false, byCircle, Append )
207 && mySelectedObjects.findIndex( myLastPicked ) == -1 )
209 mySelectedObjects.append( myLastPicked );
210 myGLViewer2d->activateDrawer( myLastPicked, TRUE, TRUE );
212 if( status == SS_Invalid )
213 status = SS_GlobalChanged;
215 else if( status == SS_LocalChanged )
216 status = SS_GlobalChanged;
221 if( myHFlag && !myLastPicked )
225 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end() ; it != itEnd; ++it )
226 if ( myLastPicked != *it )
228 updateAll = (*it)->unselect() || updateAll;
232 if( updateAll || myUpdateAll )
233 myGLViewer2d->updateAll();
235 myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
237 if( mySelectedObjects.count() != 0 )
238 status = SS_GlobalChanged;
240 mySelectedObjects.clear();
250 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )myGLViewer2d->getActiveView()->getViewPort();
251 vp->getScale( aXScale, aYScale );
255 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end() ; it != itEnd; ++it )
256 if( myLastPicked != *it )
258 updateAll = (*it)->unselect() || updateAll;
262 if( updateAll || myUpdateAll )
263 myGLViewer2d->updateAll();
265 myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
267 if( mySelectedObjects.count() != 0 )
268 status = SS_GlobalChanged;
270 mySelectedObjects.clear();
273 for( oit = myActiveObjects.begin(), oitEnd = myActiveObjects.end(); oit != oitEnd; ++oit )
275 (*oit)->setScale( aXScale, aYScale );
276 GLViewer_Rect* rect = (*oit)->getUpdateRect();
278 if( rect->contains( GLViewer_Pnt( myXhigh, myXhigh ) ) )
280 (*oit)->select( myXhigh, myYhigh, myTolerance, GLViewer_Rect(), false, byCircle, Append );
281 isSel = (*oit)->isSelected();
286 mySelectedObjects.append( myLastPicked );
287 myGLViewer2d->activateDrawer( myLastPicked, TRUE, TRUE );
288 status = SS_GlobalChanged;
297 //=======================================================================
298 // Function: SelectByRect
300 //=======================================================================
301 int GLViewer_Context::SelectByRect( const QRect& theRect, bool Append )
305 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )myGLViewer2d->getActiveView()->getViewPort();
306 vp->getScale( aXScale, aYScale );
308 SelectionStatus status = SS_NoChanged;
311 ObjList::Iterator it, itEnd;
313 if ( !mySFlag || myActiveObjects.empty() )
316 bool updateAll = false;
319 if( mySelectedObjects.count() != 0 )
320 status = SS_GlobalChanged;
322 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end(); it != itEnd; ++it )
324 updateAll = (*it)->unselect() || updateAll;
327 mySelectedObjects.clear();
330 for( it = myActiveObjects.begin(), itEnd = myActiveObjects.end(); it != itEnd; ++it )
333 (*it)->setScale( aXScale, aYScale );
334 QRect rect = myGLViewer2d->getQRect( *( (*it)->getRect() ) );
336 if( rect.intersects( theRect ) )
338 GLViewer_Rect aRect = myGLViewer2d->getGLVRect( theRect );
339 (*it)->select( myXhigh, myYhigh, myTolerance, aRect, false, false, Append );
340 isSel = (*it)->isSelected();
343 if( isSel && mySelectedObjects.findIndex( *it ) == -1 )
346 mySelectedObjects.append( *it );
347 status = SS_GlobalChanged;
351 if( updateAll || myUpdateAll )
352 myGLViewer2d->updateAll();
354 myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
359 //=======================================================================
360 // Function: SetHighlightColor
362 //=======================================================================
363 void GLViewer_Context::SetHighlightColor( Quantity_NameOfColor aCol )
365 myHighlightColor = aCol;
367 Quantity_Color colorH( aCol );
368 int redH = 255 * (int)colorH.Red();
369 int greenH = 255 * (int)colorH.Green();
370 int blueH = 255 * (int)colorH.Blue();
371 QColor colH = QColor( redH, greenH, blueH );
373 Quantity_Color colorS( mySelectionColor );
374 int redS = 255 * (int)colorS.Red();
375 int greenS = 255 * (int)colorS.Green();
376 int blueS = 255 * (int)colorS.Blue();
377 QColor colS = QColor( redS, greenS, blueS );
379 myGLViewer2d->updateColors( colH, colS);
382 //=======================================================================
383 // Function: SetSelectionColor
385 //=======================================================================
386 void GLViewer_Context::SetSelectionColor( Quantity_NameOfColor aCol )
388 mySelectionColor = aCol;
390 Quantity_Color colorH( myHighlightColor );
391 int redH = 255 * (int)colorH.Red();
392 int greenH = 255 * (int)colorH.Green();
393 int blueH = 255 * (int)colorH.Blue();
394 QColor colH = QColor( redH, greenH, blueH );
396 Quantity_Color colorS( aCol );
397 int redS = 255 * (int)colorS.Red();
398 int greenS = 255 * (int)colorS.Green();
399 int blueS = 255 * (int)colorS.Blue();
400 QColor colS = QColor( redS, greenS, blueS );
402 myGLViewer2d->updateColors( colH, colS);
405 //=======================================================================
406 // Function: NbSelected
408 //=======================================================================
409 int GLViewer_Context::NbSelected()
411 return mySelectedObjects.count();
414 //=======================================================================
415 // Function: InitSelected
417 //=======================================================================
418 void GLViewer_Context::InitSelected()
423 //=======================================================================
424 // Function: MoreSelected
426 //=======================================================================
427 bool GLViewer_Context::MoreSelected()
429 return ( mySelCurIndex < NbSelected() );
432 //=======================================================================
433 // Function: NextSelected
435 //=======================================================================
436 bool GLViewer_Context::NextSelected()
438 if ( mySelCurIndex >= 0 && mySelCurIndex < NbSelected() )
447 //=======================================================================
448 // Function: SelectedObject
450 //=======================================================================
451 GLViewer_Object* GLViewer_Context::SelectedObject()
453 return mySelectedObjects[ mySelCurIndex ];
456 //=======================================================================
457 // Function: isSelected
459 //=======================================================================
460 bool GLViewer_Context::isSelected( GLViewer_Object* theObj )
462 return mySelectedObjects.contains( theObj );
465 //=======================================================================
466 // Function: insertObject
468 //=======================================================================
469 int GLViewer_Context::insertObject( GLViewer_Object* object, bool display, bool isActive )
471 // cout << "GLViewer_Context::insertObject" << endl;
478 myActiveObjects.append( object );
481 //QRect* rect = object->getRect()->toQRect();
482 //myGLViewer2d->updateBorders( *rect );
483 myGLViewer2d->activateDrawer( object, FALSE );
487 myInactiveObjects.append( object );
489 return myActiveObjects.count() + myInactiveObjects.count();
492 //=======================================================================
493 // Function: replaceObject
495 //=======================================================================
496 bool GLViewer_Context::replaceObject( GLViewer_Object* oldObject, GLViewer_Object* newObject )
498 if( !oldObject || !newObject )
501 if( myActiveObjects.contains( oldObject ) )
503 myActiveObjects.remove( oldObject );
504 myActiveObjects.append( newObject );
508 if( myInactiveObjects.contains( oldObject ) )
510 myInactiveObjects.remove( oldObject );
511 myInactiveObjects.append( newObject );
518 //=======================================================================
519 // Function: updateScales
521 //=======================================================================
522 void GLViewer_Context::updateScales( GLfloat scX, GLfloat scY )
524 if( scX <= 0 || scY <= 0 )
527 ObjList::iterator it, itEnd;
529 for( it = myActiveObjects.begin(), itEnd = myActiveObjects.end(); it != itEnd; ++it )
530 (*it)->setScale( scX, scY );
532 for( it = myInactiveObjects.begin(), itEnd = myInactiveObjects.end(); it != itEnd; ++it )
533 (*it)->setScale( scX, scY );
536 //=======================================================================
537 // Function: clearHighlighted
539 //=======================================================================
540 void GLViewer_Context::clearHighlighted()
542 if( myHFlag && myLastPicked )
544 myLastPicked->unhighlight();
547 myGLViewer2d->updateAll();
551 //=======================================================================
552 // Function: clearSelected
554 //=======================================================================
555 void GLViewer_Context::clearSelected( bool updateViewer )
560 ObjList::Iterator it, itEnd;
563 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end(); it != itEnd; ++it )
570 myGLViewer2d->activateDrawers( aList, TRUE );
571 mySelectedObjects.clear();
574 //=======================================================================
575 // Function: setSelected
577 //=======================================================================
578 void GLViewer_Context::setSelected( GLViewer_Object* object, bool updateViewer )
583 if( myActiveObjects.contains( object ) && !mySelectedObjects.contains( object ) )
585 object->setSelected( TRUE );
586 mySelectedObjects.append( object );
590 myGLViewer2d->activateDrawer( object, TRUE, TRUE );
593 //=======================================================================
594 // Function: remSelected
596 //=======================================================================
597 void GLViewer_Context::remSelected( GLViewer_Object* object, bool updateViewer )
599 if( !object || !mySelectedObjects.contains( object ) )
602 mySelectedObjects.remove( object );
606 myGLViewer2d->activateDrawer( object, TRUE, TRUE );
609 //=======================================================================
610 // Function: eraseObject
612 //=======================================================================
613 void GLViewer_Context::eraseObject( GLViewer_Object* theObject, bool theUpdateViewer )
615 if( !theObject || !myActiveObjects.contains( theObject ) )
618 theObject->unhighlight();
619 theObject->unselect();
620 theObject->setVisible( false );
622 if( theUpdateViewer )
623 myGLViewer2d->updateAll();
626 //=======================================================================
627 // Function: deleteObject
629 //=======================================================================
630 void GLViewer_Context::deleteObject( GLViewer_Object* theObject, bool updateViewer )
633 ( !myActiveObjects.contains( theObject ) && !myInactiveObjects.contains( theObject ) ) )
636 if( myActiveObjects.contains( theObject ) )
637 myActiveObjects.remove( theObject );
638 else if( myInactiveObjects.contains( theObject ) )
639 myInactiveObjects.remove( theObject );
643 if( mySelectedObjects.contains( theObject ) )
644 mySelectedObjects.remove( theObject );
646 GLViewer_Group* aGroup = theObject->getGroup();
648 aGroup->removeObject( theObject );
650 if( myLastPicked == theObject )
653 myGLViewer2d->updateAll();
656 //=======================================================================
657 // Function: setActive
659 //=======================================================================
660 bool GLViewer_Context::setActive( GLViewer_Object* theObject )
662 if( !theObject || !myInactiveObjects.contains( theObject ) )
665 myInactiveObjects.remove( theObject );
666 myActiveObjects.append( theObject );
670 //=======================================================================
671 // Function: setInactive
673 //=======================================================================
674 bool GLViewer_Context::setInactive( GLViewer_Object* theObject )
676 if( !theObject || !myActiveObjects.contains( theObject ) )
679 myActiveObjects.remove( theObject );
680 myInactiveObjects.append( theObject );