1 // Copyright (C) 2005 OPEN CASCADE
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.
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.
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
17 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
19 // Author : OPEN CASCADE
22 // File: GLViewer_Context.cxx
23 // Created: November, 2004
25 //================================================================
26 // Class : GLViewer_AspectLine
27 // Description : Class for manage of presentations in GLViewer
28 //================================================================
30 #include "GLViewer_Context.h"
32 #include "GLViewer_Group.h"
33 #include "GLViewer_Object.h"
34 #include "GLViewer_Viewer2d.h"
35 #include "GLViewer_ViewPort2d.h"
37 #include <TColStd_SequenceOfInteger.hxx>
41 //=======================================================================
42 // Function: GLViewer_Context
44 //=======================================================================
45 GLViewer_Context::GLViewer_Context( GLViewer_Viewer2d* v ) :
47 myHighlightColor( Quantity_NOC_CYAN1 ),
48 mySelectionColor( Quantity_NOC_RED ),
49 myTolerance( TOLERANCE )
54 myLastPickedChanged = false;
62 //=======================================================================
63 // Function: ~GLViewer_Context
65 //=======================================================================
66 GLViewer_Context::~GLViewer_Context()
68 myActiveObjects.clear();
69 myInactiveObjects.clear();
70 mySelectedObjects.clear();
73 //=======================================================================
76 //=======================================================================
77 int GLViewer_Context::MoveTo( int xi, int yi, bool byCircle )
79 GLfloat x = (GLfloat)xi;
80 GLfloat y = (GLfloat)yi;
81 myGLViewer2d->transPoint( x, y );
86 GLboolean isHigh = GL_FALSE;
87 GLboolean onObject = GL_FALSE;
89 GLViewer_Object* aPrevLastPicked = myLastPicked;
90 GLViewer_Object* lastPicked = 0;
92 ObjList anUpdatedObjects;
94 if( myActiveObjects.isEmpty() )
97 ObjList::iterator it = myActiveObjects.end();
98 ObjList::iterator itEnd = myActiveObjects.begin();
101 GLViewer_Object* object = *it;
103 GLViewer_Rect* rect = object->getUpdateRect();
104 if( rect->contains( GLViewer_Pnt( x, y ) ) )
107 object->highlight( x, y, myTolerance, GL_FALSE );
108 isHigh = object->isHighlighted();
123 myLastPicked = lastPicked;
130 it = myActiveObjects.begin();
131 itEnd = myActiveObjects.end();
133 for( ; it != itEnd; ++it )
134 (*it)->unhighlight();
136 anUpdatedObjects.append( (*it) );
139 myLastPickedChanged = aPrevLastPicked != myLastPicked;
141 if( myLastPickedChanged )
142 myGLViewer2d->updateAll();
147 if( !myLastPicked && isHigh )
150 myLastPicked = lastPicked;
151 anUpdatedObjects.append( myLastPicked );
153 else if( myLastPicked && !isHigh )
156 myLastPicked->unhighlight();
157 anUpdatedObjects.append( myLastPicked );
160 else if( myLastPicked && isHigh )
163 myLastPicked->highlight( x, y, myTolerance, byCircle );
164 anUpdatedObjects.append( myLastPicked );
165 if( myLastPicked != lastPicked )
167 myLastPicked->unhighlight();
168 myLastPicked = lastPicked;
169 anUpdatedObjects.append( myLastPicked );
173 myLastPickedChanged = ( aPrevLastPicked != myLastPicked );
175 if( myLastPickedChanged || myUpdateAll )
176 myGLViewer2d->updateAll();
178 myGLViewer2d->activateDrawers( anUpdatedObjects, TRUE, TRUE );
183 //=======================================================================
186 //=======================================================================
187 int GLViewer_Context::Select( bool Append, bool byCircle )
189 ObjList::Iterator it, itEnd, oit, oitEnd;
190 SelectionStatus status = SS_Invalid;
192 bool updateAll = false;
197 return status;//invalid
199 if( myHFlag && myLastPicked )
201 if( mySelectedObjects.count() == 1 && mySelectedObjects.first() == myLastPicked )
202 status = SS_LocalChanged;
206 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end() ; it != itEnd; ++it )
207 if( myLastPicked != *it )
209 updateAll = (*it)->unselect() || updateAll;
213 if( updateAll || myUpdateAll )
214 myGLViewer2d->updateAll();
216 myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
218 if( mySelectedObjects.count() != 0 && status == SS_Invalid )
219 status = SS_GlobalChanged;
220 mySelectedObjects.clear();
222 else if( myLastPicked->isSelected() && status != SS_LocalChanged )
224 mySelectedObjects.remove( myLastPicked );
225 myLastPicked->unselect();
226 myGLViewer2d->updateAll();
228 if( mySelectedObjects.count() != 0 && status == SS_Invalid )
229 status = SS_GlobalChanged;
234 if ( myLastPicked->select( myXhigh, myYhigh, myTolerance, GLViewer_Rect(), false, byCircle, Append )
235 && mySelectedObjects.findIndex( myLastPicked ) == -1 )
237 mySelectedObjects.append( myLastPicked );
238 myGLViewer2d->activateDrawer( myLastPicked, TRUE, TRUE );
240 if( status == SS_Invalid )
241 status = SS_GlobalChanged;
243 else if( status == SS_LocalChanged )
244 status = SS_GlobalChanged;
249 if( myHFlag && !myLastPicked )
253 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end() ; it != itEnd; ++it )
254 if ( myLastPicked != *it )
256 updateAll = (*it)->unselect() || updateAll;
260 if( updateAll || myUpdateAll )
261 myGLViewer2d->updateAll();
263 myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
265 if( mySelectedObjects.count() != 0 )
266 status = SS_GlobalChanged;
268 mySelectedObjects.clear();
278 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )myGLViewer2d->getActiveView()->getViewPort();
279 vp->getScale( aXScale, aYScale );
283 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end() ; it != itEnd; ++it )
284 if( myLastPicked != *it )
286 updateAll = (*it)->unselect() || updateAll;
290 if( updateAll || myUpdateAll )
291 myGLViewer2d->updateAll();
293 myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
295 if( mySelectedObjects.count() != 0 )
296 status = SS_GlobalChanged;
298 mySelectedObjects.clear();
301 for( oit = myActiveObjects.begin(), oitEnd = myActiveObjects.end(); oit != oitEnd; ++oit )
303 (*oit)->setScale( aXScale, aYScale );
304 GLViewer_Rect* rect = (*oit)->getUpdateRect();
306 if( rect->contains( GLViewer_Pnt( myXhigh, myXhigh ) ) )
308 (*oit)->select( myXhigh, myYhigh, myTolerance, GLViewer_Rect(), false, byCircle, Append );
309 isSel = (*oit)->isSelected();
314 mySelectedObjects.append( myLastPicked );
315 myGLViewer2d->activateDrawer( myLastPicked, TRUE, TRUE );
316 status = SS_GlobalChanged;
325 //=======================================================================
326 // Function: SelectByRect
328 //=======================================================================
329 int GLViewer_Context::SelectByRect( const QRect& theRect, bool Append )
333 GLViewer_ViewPort2d* vp = ( GLViewer_ViewPort2d* )myGLViewer2d->getActiveView()->getViewPort();
334 vp->getScale( aXScale, aYScale );
336 SelectionStatus status = SS_NoChanged;
339 ObjList::Iterator it, itEnd;
341 if ( !mySFlag || myActiveObjects.empty() )
344 bool updateAll = false;
347 if( mySelectedObjects.count() != 0 )
348 status = SS_GlobalChanged;
350 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end(); it != itEnd; ++it )
352 updateAll = (*it)->unselect() || updateAll;
355 mySelectedObjects.clear();
358 for( it = myActiveObjects.begin(), itEnd = myActiveObjects.end(); it != itEnd; ++it )
361 (*it)->setScale( aXScale, aYScale );
362 QRect rect = myGLViewer2d->getQRect( *( (*it)->getRect() ) );
364 if( rect.intersects( theRect ) )
366 GLViewer_Rect aRect = myGLViewer2d->getGLVRect( theRect );
367 (*it)->select( myXhigh, myYhigh, myTolerance, aRect, false, false, Append );
368 isSel = (*it)->isSelected();
371 if( isSel && mySelectedObjects.findIndex( *it ) == -1 )
374 mySelectedObjects.append( *it );
375 status = SS_GlobalChanged;
379 if( updateAll || myUpdateAll )
380 myGLViewer2d->updateAll();
382 myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
387 //=======================================================================
388 // Function: SetHighlightColor
390 //=======================================================================
391 void GLViewer_Context::SetHighlightColor( Quantity_NameOfColor aCol )
393 myHighlightColor = aCol;
395 Quantity_Color colorH( aCol );
396 int redH = 255 * (int)colorH.Red();
397 int greenH = 255 * (int)colorH.Green();
398 int blueH = 255 * (int)colorH.Blue();
399 QColor colH = QColor( redH, greenH, blueH );
401 Quantity_Color colorS( mySelectionColor );
402 int redS = 255 * (int)colorS.Red();
403 int greenS = 255 * (int)colorS.Green();
404 int blueS = 255 * (int)colorS.Blue();
405 QColor colS = QColor( redS, greenS, blueS );
407 myGLViewer2d->updateColors( colH, colS);
410 //=======================================================================
411 // Function: SetSelectionColor
413 //=======================================================================
414 void GLViewer_Context::SetSelectionColor( Quantity_NameOfColor aCol )
416 mySelectionColor = aCol;
418 Quantity_Color colorH( myHighlightColor );
419 int redH = 255 * (int)colorH.Red();
420 int greenH = 255 * (int)colorH.Green();
421 int blueH = 255 * (int)colorH.Blue();
422 QColor colH = QColor( redH, greenH, blueH );
424 Quantity_Color colorS( aCol );
425 int redS = 255 * (int)colorS.Red();
426 int greenS = 255 * (int)colorS.Green();
427 int blueS = 255 * (int)colorS.Blue();
428 QColor colS = QColor( redS, greenS, blueS );
430 myGLViewer2d->updateColors( colH, colS);
433 //=======================================================================
434 // Function: NbSelected
436 //=======================================================================
437 int GLViewer_Context::NbSelected()
439 return mySelectedObjects.count();
442 //=======================================================================
443 // Function: InitSelected
445 //=======================================================================
446 void GLViewer_Context::InitSelected()
451 //=======================================================================
452 // Function: MoreSelected
454 //=======================================================================
455 bool GLViewer_Context::MoreSelected()
457 return ( mySelCurIndex < NbSelected() );
460 //=======================================================================
461 // Function: NextSelected
463 //=======================================================================
464 bool GLViewer_Context::NextSelected()
466 if ( mySelCurIndex >= 0 && mySelCurIndex < NbSelected() )
475 //=======================================================================
476 // Function: SelectedObject
478 //=======================================================================
479 GLViewer_Object* GLViewer_Context::SelectedObject()
481 return mySelectedObjects[ mySelCurIndex ];
484 //=======================================================================
485 // Function: isSelected
487 //=======================================================================
488 bool GLViewer_Context::isSelected( GLViewer_Object* theObj )
490 return mySelectedObjects.contains( theObj );
493 //=======================================================================
494 // Function: insertObject
496 //=======================================================================
497 int GLViewer_Context::insertObject( GLViewer_Object* object, bool display, bool isActive )
499 // cout << "GLViewer_Context::insertObject" << endl;
506 myActiveObjects.append( object );
509 //QRect* rect = object->getRect()->toQRect();
510 //myGLViewer2d->updateBorders( *rect );
511 myGLViewer2d->activateDrawer( object, FALSE );
515 myInactiveObjects.append( object );
517 return myActiveObjects.count() + myInactiveObjects.count();
520 //=======================================================================
521 // Function: replaceObject
523 //=======================================================================
524 bool GLViewer_Context::replaceObject( GLViewer_Object* oldObject, GLViewer_Object* newObject )
526 if( !oldObject || !newObject )
529 if( myActiveObjects.contains( oldObject ) )
531 myActiveObjects.remove( oldObject );
532 myActiveObjects.append( newObject );
536 if( myInactiveObjects.contains( oldObject ) )
538 myInactiveObjects.remove( oldObject );
539 myInactiveObjects.append( newObject );
546 //=======================================================================
547 // Function: updateScales
549 //=======================================================================
550 void GLViewer_Context::updateScales( GLfloat scX, GLfloat scY )
552 if( scX <= 0 || scY <= 0 )
555 ObjList::iterator it, itEnd;
557 for( it = myActiveObjects.begin(), itEnd = myActiveObjects.end(); it != itEnd; ++it )
558 (*it)->setScale( scX, scY );
560 for( it = myInactiveObjects.begin(), itEnd = myInactiveObjects.end(); it != itEnd; ++it )
561 (*it)->setScale( scX, scY );
564 //=======================================================================
565 // Function: clearHighlighted
567 //=======================================================================
568 void GLViewer_Context::clearHighlighted( bool updateViewer )
570 if( myHFlag && myLastPicked )
572 myLastPicked->unhighlight();
576 myGLViewer2d->updateAll();
580 //=======================================================================
581 // Function: clearSelected
583 //=======================================================================
584 void GLViewer_Context::clearSelected( bool updateViewer )
589 ObjList::Iterator it, itEnd;
592 for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end(); it != itEnd; ++it )
599 myGLViewer2d->activateDrawers( aList, TRUE );
600 mySelectedObjects.clear();
603 //=======================================================================
604 // Function: setSelected
606 //=======================================================================
607 void GLViewer_Context::setSelected( GLViewer_Object* object, bool updateViewer )
612 if( myActiveObjects.contains( object ) && !mySelectedObjects.contains( object ) )
614 object->setSelected( TRUE );
615 mySelectedObjects.append( object );
619 myGLViewer2d->activateDrawer( object, TRUE, TRUE );
622 //=======================================================================
623 // Function: remSelected
625 //=======================================================================
626 void GLViewer_Context::remSelected( GLViewer_Object* object, bool updateViewer )
628 if( !object || !mySelectedObjects.contains( object ) )
631 mySelectedObjects.remove( object );
635 myGLViewer2d->activateDrawer( object, TRUE, TRUE );
638 //=======================================================================
639 // Function: eraseObject
641 //=======================================================================
642 void GLViewer_Context::eraseObject( GLViewer_Object* theObject, bool theUpdateViewer )
644 if( !theObject || !myActiveObjects.contains( theObject ) )
647 theObject->unhighlight();
648 theObject->unselect();
649 theObject->setVisible( false );
651 if( theUpdateViewer )
652 myGLViewer2d->updateAll();
655 //=======================================================================
656 // Function: deleteObject
658 //=======================================================================
659 void GLViewer_Context::deleteObject( GLViewer_Object* theObject, bool updateViewer )
662 ( !myActiveObjects.contains( theObject ) && !myInactiveObjects.contains( theObject ) ) )
665 if( myActiveObjects.contains( theObject ) )
666 myActiveObjects.remove( theObject );
667 else if( myInactiveObjects.contains( theObject ) )
668 myInactiveObjects.remove( theObject );
672 if( mySelectedObjects.contains( theObject ) )
673 mySelectedObjects.remove( theObject );
675 GLViewer_Group* aGroup = theObject->getGroup();
677 aGroup->removeObject( theObject );
679 if( myLastPicked == theObject )
683 myGLViewer2d->updateAll();
686 //=======================================================================
687 // Function: setActive
689 //=======================================================================
690 bool GLViewer_Context::setActive( GLViewer_Object* theObject )
692 if( !theObject || !myInactiveObjects.contains( theObject ) )
695 myInactiveObjects.remove( theObject );
696 myActiveObjects.append( theObject );
700 //=======================================================================
701 // Function: setInactive
703 //=======================================================================
704 bool GLViewer_Context::setInactive( GLViewer_Object* theObject )
706 if( !theObject || !myActiveObjects.contains( theObject ) )
709 myActiveObjects.remove( theObject );
710 myInactiveObjects.append( theObject );