Salome HOME
Upgrade to paraview 5.4
[modules/gui.git] / src / GLViewer / GLViewer_Context.cxx
index 699df866dbf4d0f38d08b05b203a73f18c9d56a6..ef33787edb8d163f561e6ef856936ce2d5946774 100644 (file)
@@ -1,12 +1,33 @@
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+//  Author : OPEN CASCADE
 // File:      GLViewer_Context.cxx
 // Created:   November, 2004
-// Author:    OCC team
-// Copyright (C) CEA 2004
-
-//================================================================
-// Class       : GLViewer_AspectLine
-// Description : Class for manage of presentations in GLViewer
-//================================================================
+//
+/*!
+  \class GLViewer_AspectLine
+  \brief Class for manage of presentations in GLViewer
+*/
 
 #include "GLViewer_Context.h"
 
 #include "GLViewer_Object.h"
 #include "GLViewer_Viewer2d.h"
 #include "GLViewer_ViewPort2d.h"
+#include "GLViewer_ViewFrame.h"
+
+//QT includes
+#include <QRect>
 
 #include <TColStd_SequenceOfInteger.hxx>
 
 #define TOLERANCE  12
 
-//=======================================================================
-// Function: GLViewer_Context
-// Purpose :
-//=======================================================================
+/*!
+  Constructor
+*/
 GLViewer_Context::GLViewer_Context( GLViewer_Viewer2d* v ) :
        myGLViewer2d( v ),
        myHighlightColor( Quantity_NOC_CYAN1 ),
@@ -40,10 +64,9 @@ GLViewer_Context::GLViewer_Context( GLViewer_Viewer2d* v ) :
   mySelCurIndex = 0;
 }
 
-//=======================================================================
-// Function: ~GLViewer_Context
-// Purpose :
-//=======================================================================
+/*!
+  Destructor
+*/
 GLViewer_Context::~GLViewer_Context()
 {
     myActiveObjects.clear();
@@ -51,10 +74,13 @@ GLViewer_Context::~GLViewer_Context()
     mySelectedObjects.clear();
 }
 
-//=======================================================================
-// Function: MoveTo
-// Purpose :
-//=======================================================================
+/*!
+  Hiilights objects under cursor
+  \param x - X coord of mouse cursor
+  \param y - Y coord of mouse cursor
+  \param byCircle - true if needs round sensitive area around mouse cursor, else rectangle
+  function search object rectangle which intersect with sensitive area and call object highlight method
+*/
 int GLViewer_Context::MoveTo( int xi, int yi, bool byCircle )
 {
     GLfloat x = (GLfloat)xi;
@@ -141,15 +167,13 @@ int GLViewer_Context::MoveTo( int xi, int yi, bool byCircle )
     else if( myLastPicked && isHigh )
     {
         //cout << 3 << endl;
-        //myLastPicked->highlight( x, y, myTolerance, byCircle );
+        myLastPicked->highlight( x, y, myTolerance, byCircle );
+        anUpdatedObjects.append( myLastPicked );
         if( myLastPicked != lastPicked )
         {
             myLastPicked->unhighlight();
-            if( myLastPicked != lastPicked )
-            {
-                myLastPicked = lastPicked;
-                anUpdatedObjects.append( myLastPicked );
-            }
+            myLastPicked = lastPicked;
+            anUpdatedObjects.append( myLastPicked );
         }
     }
 
@@ -158,15 +182,15 @@ int GLViewer_Context::MoveTo( int xi, int yi, bool byCircle )
     if( myLastPickedChanged || myUpdateAll )
         myGLViewer2d->updateAll();
     else
-        myGLViewer2d->activateDrawers( anUpdatedObjects, TRUE, TRUE );
+        myGLViewer2d->activateDrawers( anUpdatedObjects, true, true );
 
     return 0;
 }
 
-//=======================================================================
-// Function: GLViewer_Select
-// Purpose :
-//=======================================================================
+/*! Selects already highlighting object by calling object method select
+  \param Append - true if new selection will be append to existing selection, false - another
+  \param byCircle - true if needs round selection area in complex object
+*/
 int GLViewer_Context::Select( bool Append, bool byCircle )
 {
     ObjList::Iterator it, itEnd, oit, oitEnd;
@@ -187,27 +211,38 @@ int GLViewer_Context::Select( bool Append, bool byCircle )
         if ( !Append )
         {
             for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end() ; it != itEnd; ++it )
-                   if( myLastPicked != *it )
+                    if( myLastPicked != *it )
                 {
-                       updateAll = (*it)->unselect() || updateAll;
-                       aList.append( *it );
+                        updateAll = (*it)->unselect() || updateAll;
+                        aList.append( *it );
                 }
 
             if( updateAll || myUpdateAll )
                 myGLViewer2d->updateAll();
             else
-                myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
+                myGLViewer2d->activateDrawers( aList, true, true );
 
             if( mySelectedObjects.count() != 0 && status == SS_Invalid )
                 status = SS_GlobalChanged;
             mySelectedObjects.clear();
         } 
+        else if( myLastPicked->isSelected() && status != SS_LocalChanged )
+        {
+            mySelectedObjects.removeAll( myLastPicked );
+            myLastPicked->unselect();
+            myGLViewer2d->updateAll();
+
+            if( mySelectedObjects.count() != 0 && status == SS_Invalid )
+              status = SS_GlobalChanged;
+
+            return status;
+        }
 
         if ( myLastPicked->select( myXhigh, myYhigh, myTolerance, GLViewer_Rect(), false, byCircle, Append )
-             && mySelectedObjects.findIndex( myLastPicked ) == -1 )
+             && mySelectedObjects.indexOf( myLastPicked ) == -1 )
         {
             mySelectedObjects.append( myLastPicked );
-            myGLViewer2d->activateDrawer( myLastPicked, TRUE, TRUE );
+            myGLViewer2d->activateDrawer( myLastPicked, true, true );
 
             if( status == SS_Invalid )
                 status = SS_GlobalChanged;
@@ -223,16 +258,16 @@ int GLViewer_Context::Select( bool Append, bool byCircle )
         if ( !Append )
         {
             for( it = mySelectedObjects.begin(), itEnd = mySelectedObjects.end() ; it != itEnd; ++it )
-                   if ( myLastPicked != *it )
+                    if ( myLastPicked != *it )
                 {
-                       updateAll = (*it)->unselect() || updateAll;
-                       aList.append( *it );
+                        updateAll = (*it)->unselect() || updateAll;
+                        aList.append( *it );
                 }
 
             if( updateAll || myUpdateAll )
                 myGLViewer2d->updateAll();
             else
-                myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
+                myGLViewer2d->activateDrawers( aList, true, true );
 
             if( mySelectedObjects.count() != 0 )
                 status = SS_GlobalChanged;
@@ -262,7 +297,7 @@ int GLViewer_Context::Select( bool Append, bool byCircle )
             if( updateAll || myUpdateAll )
                 myGLViewer2d->updateAll();
             else
-                myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
+                myGLViewer2d->activateDrawers( aList, true, true );
 
             if( mySelectedObjects.count() != 0 )
                 status = SS_GlobalChanged;
@@ -284,7 +319,7 @@ int GLViewer_Context::Select( bool Append, bool byCircle )
             {
                 myLastPicked = *oit;
                 mySelectedObjects.append( myLastPicked );
-                myGLViewer2d->activateDrawer( myLastPicked, TRUE, TRUE );
+                myGLViewer2d->activateDrawer( myLastPicked, true, true );
                 status = SS_GlobalChanged;
                 return status;
             }
@@ -294,10 +329,11 @@ int GLViewer_Context::Select( bool Append, bool byCircle )
     return SS_NoChanged;
 }
 
-//=======================================================================
-// Function: GLViewer_SelectByRect
-// Purpose :
-//=======================================================================
+/*! Selects objects on scene by rectangle
+  \param theRect - rectangle of selection
+  \param Append - true if new selection will be append to existing selection, false - another
+  function search object rectangle which intersect with theRect and call object select method
+*/
 int GLViewer_Context::SelectByRect( const QRect& theRect, bool Append )
 {
     GLfloat aXScale;
@@ -340,7 +376,7 @@ int GLViewer_Context::SelectByRect( const QRect& theRect, bool Append )
             isSel = (*it)->isSelected();
         }
 
-        if( isSel && mySelectedObjects.findIndex( *it ) == -1 )
+        if( isSel && mySelectedObjects.indexOf( *it ) == -1 )
         {
             aList.append( *it );
             mySelectedObjects.append( *it );
@@ -351,15 +387,15 @@ int GLViewer_Context::SelectByRect( const QRect& theRect, bool Append )
     if( updateAll || myUpdateAll )
         myGLViewer2d->updateAll();
     else
-        myGLViewer2d->activateDrawers( aList, TRUE, TRUE );
+        myGLViewer2d->activateDrawers( aList, true, true );
 
     return status;
 }
 
-//=======================================================================
-// Function: GLViewer_SetHighlightColor
-// Purpose :
-//=======================================================================
+/*!
+  Sets color of hilighting
+  \param aCol - new color of highlighting
+*/
 void GLViewer_Context::SetHighlightColor( Quantity_NameOfColor aCol )
 {
   myHighlightColor = aCol;
@@ -379,10 +415,10 @@ void GLViewer_Context::SetHighlightColor( Quantity_NameOfColor aCol )
   myGLViewer2d->updateColors( colH, colS);
 }
 
-//=======================================================================
-// Function: GLViewer_SetSelectionColor
-// Purpose :
-//=======================================================================
+/*!
+  Sets color of selection
+  \param aCol - new color of selection
+*/
 void GLViewer_Context::SetSelectionColor( Quantity_NameOfColor aCol )
 {
   mySelectionColor = aCol;
@@ -402,70 +438,66 @@ void GLViewer_Context::SetSelectionColor( Quantity_NameOfColor aCol )
   myGLViewer2d->updateColors( colH, colS);
 }
 
-//=======================================================================
-// Function: GLViewer_NbSelected
-// Purpose :
-//=======================================================================
+/*!
+  \return number of selected objects
+*/
 int GLViewer_Context::NbSelected()
 {
   return mySelectedObjects.count();
 }
 
-//=======================================================================
-// Function: GLViewer_InitSelected
-// Purpose :
-//=======================================================================
+/*!
+  Inits iteration through selected objects
+*/
 void GLViewer_Context::InitSelected()
 {
   mySelCurIndex = 0;
 }
 
-//=======================================================================
-// Function: GLViewer_MoreSelected
-// Purpose :
-//=======================================================================
+/*!
+  Checks if iteration through selected objects may be continued
+*/
 bool GLViewer_Context::MoreSelected()
 {
   return ( mySelCurIndex < NbSelected() );
 }
 
-//=======================================================================
-// Function: GLViewer_NextSelected
-// Purpose :
-//=======================================================================
+/*!
+  Iterates to next selected object
+*/
 bool GLViewer_Context::NextSelected()
 {
   if ( mySelCurIndex >= 0 && mySelCurIndex < NbSelected() )
   {
     mySelCurIndex++;
-    return TRUE;
+    return true;
   }
 
-  return FALSE;
+  return false;
 }
 
-//=======================================================================
-// Function: GLViewer_SelectedObject
-// Purpose :
-//=======================================================================
+/*!
+  \return current selected object (must be used only in cycle as "for( InitSelected(); MoreSelected(); NextSelected() ) {...}" )
+*/
 GLViewer_Object* GLViewer_Context::SelectedObject()
 {
     return mySelectedObjects[ mySelCurIndex ];
 }
 
-//=======================================================================
-// Function: GLViewer_isSelected
-// Purpose :
-//=======================================================================
+/*!
+  \return true if object is selected
+  \param theObj - object to be checked
+*/
 bool  GLViewer_Context::isSelected( GLViewer_Object* theObj )
 {
     return mySelectedObjects.contains( theObj );
 }
 
-//=======================================================================
-// Function: GLViewer_insertObject
-// Purpose :
-//=======================================================================
+/*! Inserts new object in context
+  \param theObject - object to be inserted
+  \param display - true if needs display object immediatly after inserting, else false
+  \param isActive - true if needs inserting object in active list
+*/
 int GLViewer_Context::insertObject( GLViewer_Object* object, bool display, bool isActive )
 {
 //  cout << "GLViewer_Context::insertObject" << endl;
@@ -480,7 +512,7 @@ int GLViewer_Context::insertObject( GLViewer_Object* object, bool display, bool
         {
             //QRect* rect = object->getRect()->toQRect();
             //myGLViewer2d->updateBorders( *rect );
-            myGLViewer2d->activateDrawer( object, FALSE );
+            myGLViewer2d->activateDrawer( object, false );
         }
     }
     else
@@ -489,10 +521,11 @@ int GLViewer_Context::insertObject( GLViewer_Object* object, bool display, bool
     return myActiveObjects.count() + myInactiveObjects.count();
 }
 
-//=======================================================================
-// Function: GLViewer_replaceObject
-// Purpose :
-//=======================================================================
+/*!
+  Replaces object in context
+  \param oldObject - object to be replaced
+  \param newObject - object for replacing
+*/
 bool GLViewer_Context::replaceObject( GLViewer_Object* oldObject, GLViewer_Object* newObject )
 {
     if( !oldObject || !newObject )
@@ -500,14 +533,14 @@ bool GLViewer_Context::replaceObject( GLViewer_Object* oldObject, GLViewer_Objec
 
   if( myActiveObjects.contains( oldObject ) )
   {
-    myActiveObjects.remove( oldObject );
+    myActiveObjects.removeAll( oldObject );
     myActiveObjects.append( newObject );
     return true;
   }
 
   if( myInactiveObjects.contains( oldObject ) )
   {
-    myInactiveObjects.remove( oldObject );
+    myInactiveObjects.removeAll( oldObject );
     myInactiveObjects.append( newObject );
     return true;
   }
@@ -515,10 +548,9 @@ bool GLViewer_Context::replaceObject( GLViewer_Object* oldObject, GLViewer_Objec
   return false;
 }
 
-//=======================================================================
-// Function: GLViewer_updateScales
-// Purpose :
-//=======================================================================
+/*!
+  Updates scales of all objects in context
+*/
 void GLViewer_Context::updateScales( GLfloat scX, GLfloat scY )
 {
   if( scX <= 0 || scY <= 0 )
@@ -533,25 +565,26 @@ void GLViewer_Context::updateScales( GLfloat scX, GLfloat scY )
       (*it)->setScale( scX, scY );
 }
 
-//=======================================================================
-// Function: GLViewer_clearHighlighted
-// Purpose :
-//=======================================================================
-void GLViewer_Context::clearHighlighted()
+/*!
+  Clears hilighting of objects
+  \param updateViewer - if it is true, viewer must be updated
+*/
+void GLViewer_Context::clearHighlighted( bool updateViewer )
 {
   if( myHFlag && myLastPicked )
   {
-      myLastPicked->unhighlight();
-      myLastPicked = 0;
-
+    myLastPicked->unhighlight();
+    myLastPicked = 0;
+    
+    if( updateViewer )
       myGLViewer2d->updateAll();
   }
 }
 
-//=======================================================================
-// Function: GLViewer_clearSelected
-// Purpose :
-//=======================================================================
+/*!
+  Clears selection of objects
+  \param updateViewer - if it is true, viewer must be updated
+*/
 void GLViewer_Context::clearSelected( bool updateViewer )
 {
   if( !mySFlag )
@@ -567,14 +600,14 @@ void GLViewer_Context::clearSelected( bool updateViewer )
   }          
         
   if( updateViewer )
-    myGLViewer2d->activateDrawers( aList, TRUE );
+    myGLViewer2d->activateDrawers( aList, true );
   mySelectedObjects.clear();    
 }
 
-//=======================================================================
-// Function: GLViewer_setSelected
-// Purpose :
-//=======================================================================
+/*!
+  Selects object, other selected objects are left as selected
+  \param updateViewer - if it is true, viewer must be updated
+*/
 void GLViewer_Context::setSelected( GLViewer_Object* object, bool updateViewer )
 {
   if( !object )
@@ -582,34 +615,34 @@ void GLViewer_Context::setSelected( GLViewer_Object* object, bool updateViewer )
 
   if( myActiveObjects.contains( object ) && !mySelectedObjects.contains( object ) )
   {
-    object->setSelected( TRUE );
+    object->setSelected( true );
     mySelectedObjects.append( object );
   }
      
   if( updateViewer )
-    myGLViewer2d->activateDrawer( object, TRUE, TRUE );
+    myGLViewer2d->activateDrawer( object, true, true );
 }
 
-//=======================================================================
-// Function: GLViewer_remSelected
-// Purpose :
-//=======================================================================
+/*!
+  Unselects object, other selected objects are left as selected
+  \param updateViewer - if it is true, viewer must be updated
+*/
 void GLViewer_Context::remSelected( GLViewer_Object* object, bool updateViewer )
 {
   if( !object || !mySelectedObjects.contains( object ) )
     return;
   
-  mySelectedObjects.remove( object );
+  mySelectedObjects.removeAll( object );
   object->unselect();
   
   if( updateViewer )
-    myGLViewer2d->activateDrawer( object, TRUE, TRUE );
+    myGLViewer2d->activateDrawer( object, true, true );
 }
 
-//=======================================================================
-// Function: GLViewer_eraseObject
-// Purpose :
-//=======================================================================
+/*!
+  Erases object in viewer
+  \param theUpdateViewer - if it is true, viewer must be updated
+*/
 void GLViewer_Context::eraseObject( GLViewer_Object* theObject, bool theUpdateViewer )
 {
     if( !theObject || !myActiveObjects.contains( theObject ) )
@@ -623,10 +656,10 @@ void GLViewer_Context::eraseObject( GLViewer_Object* theObject, bool theUpdateVi
         myGLViewer2d->updateAll();
 }
 
-//=======================================================================
-// Function: GLViewer_deleteObject
-// Purpose :
-//=======================================================================
+/*!
+  Deletes object in
+  \param updateViewer - if it is true, viewer must be updated
+*/
 void GLViewer_Context::deleteObject( GLViewer_Object* theObject, bool updateViewer )
 {
     if( !theObject ||
@@ -634,14 +667,14 @@ void GLViewer_Context::deleteObject( GLViewer_Object* theObject, bool updateView
         return;
 
     if( myActiveObjects.contains( theObject ) )      
-        myActiveObjects.remove( theObject );
+        myActiveObjects.removeAll( theObject );
     else if( myInactiveObjects.contains( theObject ) )
-        myInactiveObjects.remove( theObject );
+        myInactiveObjects.removeAll( theObject );
     else 
         return;
      
     if( mySelectedObjects.contains( theObject ) )
-        mySelectedObjects.remove( theObject );
+        mySelectedObjects.removeAll( theObject );
 
     GLViewer_Group* aGroup = theObject->getGroup();
     if( aGroup )
@@ -650,33 +683,34 @@ void GLViewer_Context::deleteObject( GLViewer_Object* theObject, bool updateView
     if( myLastPicked == theObject )
         myLastPicked = 0;
 
-    myGLViewer2d->updateAll();
+    if ( updateViewer )
+      myGLViewer2d->updateAll();
 }
 
-//=======================================================================
-// Function: GLViewer_setActive
-// Purpose :
-//=======================================================================
+/*!
+  Installs active status to object
+  \param theObject
+*/
 bool GLViewer_Context::setActive( GLViewer_Object* theObject )
 {
   if( !theObject || !myInactiveObjects.contains( theObject ) )
     return false;
 
-  myInactiveObjects.remove( theObject );
+  myInactiveObjects.removeAll( theObject );
   myActiveObjects.append( theObject );
   return true;
 }
 
-//=======================================================================
-// Function: GLViewer_setInactive
-// Purpose :
-//=======================================================================
+/*!
+  Installs inactive status to object
+  \param theObject
+*/
 bool GLViewer_Context::setInactive( GLViewer_Object* theObject )
 {
   if( !theObject || !myActiveObjects.contains( theObject ) )
     return false;
 
-  myActiveObjects.remove( theObject );
+  myActiveObjects.removeAll( theObject );
   myInactiveObjects.append( theObject );
   return true;
 }