Salome HOME
Remove unneeded xml files for PV3DViewer
[modules/gui.git] / src / Qtx / QtxActionMenuMgr.cxx
index 8e29512927efab055ea5c0f6d8e709bdc0b02ee7..de7e5bbab0da28e09705f4781788fa184cb00021 100644 (file)
@@ -1,24 +1,25 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  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
+// 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.
+// 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.
+// 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
+// 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
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 // File:      QtxActionMenuMgr.cxx
 // Author:    Alexander SOLOVYOV, Sergey TELKOV
 //
@@ -71,9 +72,9 @@ QtxActionMenuMgr::MenuNode::MenuNode()
   \param _group menu node group ID
 */
 QtxActionMenuMgr::MenuNode::MenuNode( MenuNode* p,
-                                     const int _id,
-                                     const int _idx,
-                                     const int _group )
+                                      const int _id,
+                                      const int _idx,
+                                      const int _group )
 : parent( p ), id( _id ), idx( _idx ), group( _group ), visible( true ), emptyEnabled( 0 )
 {
   if ( p )
@@ -311,7 +312,8 @@ int QtxActionMenuMgr::insert( QAction* a, const int pId, const int group, const
   \param idx menu index inside the menu group
   \return action ID
 */
-int QtxActionMenuMgr::insert( const QString& title, const int pId, const int group, const int id, const int idx )
+int QtxActionMenuMgr::insert( const QString& title, const int pId, const int group,
+                              const int id, const int idx,  QMenu* _menu)
 {
   MenuNode* pNode = pId == -1 ? myRoot : find( pId );
   if ( !pNode )
@@ -332,7 +334,11 @@ int QtxActionMenuMgr::insert( const QString& title, const int pId, const int gro
 
   int gid = (id == -1 || eNode ) ? generateId() : id;
 
-  QMenu* menu = new QMenu( 0 );
+  QMenu* menu;
+  if (_menu)
+    menu = _menu;
+  else
+    menu = new QMenu( 0 );
   QAction* ma = menu->menuAction();
   ma->setText( title );
 
@@ -491,8 +497,19 @@ void QtxActionMenuMgr::remove( const int id, const int pId, const int group )
       delNodes.append( *it );
   }
 
+  QWidget* mW = menuWidget( pNode );
   for ( NodeList::iterator itr = delNodes.begin(); itr != delNodes.end(); ++itr )
+  {
+    int id = (*itr)->id;
+    if( mW && menuAction( id ) )
+    {
+      mW->removeAction( menuAction( id ) );
+      myMenus.remove( id );
+    }
+    else if( mW && itemAction( id ) )
+      mW->removeAction( itemAction( id ) );
     pNode->children.removeAll( *itr );
+  }
 
   triggerUpdate( pNode->id, false );
 }
@@ -857,8 +874,6 @@ void QtxActionMenuMgr::updateMenu( MenuNode* startNode, const bool rec, const bo
   if ( !mw )
     return;
 
-  bool filled = checkWidget( mw );
-
   // first remove all own actions and collect foreign ones
   QMap< QAction*, QList<QAction*> > foreign;
   QAction* a;
@@ -886,7 +901,7 @@ void QtxActionMenuMgr::updateMenu( MenuNode* startNode, const bool rec, const bo
     {
       a = foralit.next();
       if ( !mw->actions().contains( a ) )
-       foralit.remove();
+        foralit.remove();
     }
   }
   QList<QAction*> alist = mw->actions();
@@ -949,13 +964,14 @@ void QtxActionMenuMgr::updateMenu( MenuNode* startNode, const bool rec, const bo
     foreach( a, formapit.value() )
       mw->insertAction( preva, a );
   }
-  
+
   // remove extra separators
   simplifySeparators( mw );
 
   // update parent menu if necessary
-  if ( updParent && node->parent && filled != checkWidget( mw ) )
+  if ( updParent && node->parent ) {
     updateMenu( node->parent, false );
+  }
 }
 
 /*!
@@ -984,8 +1000,10 @@ bool QtxActionMenuMgr::ownAction( QAction* a, MenuNode* node ) const
   for ( NodeList::const_iterator iter = node->children.begin(); iter != node->children.end(); ++iter )
   {
     QAction* mya = itemAction( (*iter)->id );
-    if ( !mya ) mya = menuAction( (*iter)->id );
-    if ( mya && mya == a ) return true;
+    if ( !mya )
+      mya = menuAction( (*iter)->id );
+    if ( mya && mya == a )
+      return true;
   }
   return false;
 }
@@ -1000,7 +1018,12 @@ bool QtxActionMenuMgr::checkWidget( QWidget* wid ) const
   if ( !wid )
     return false;
 
-  return !wid->actions().isEmpty();
+  bool res = false;
+  QList<QAction*> lst = wid->actions();
+  for ( QList<QAction*>::const_iterator it = lst.begin(); it != lst.end() && !res; ++it ) {
+    res = !(*it)->isSeparator() && (*it)->isVisible();
+  }
+  return res;
 }
 
 /*!
@@ -1161,7 +1184,7 @@ void QtxActionMenuMgr::setEmptyEnabled( const int id, const bool enable )
   if ( node && menuAction( id ) ) {
     int old = node->emptyEnabled;
     node->emptyEnabled += enable ? 1 : -1;
-    if ( old <= 0 && enable || old > 0 && !enable ) // update menu only if enabled state has been changed
+    if ( ( old <= 0 && enable ) || ( old > 0 && !enable ) ) // update menu only if enabled state has been changed
       updateMenu( node, true, true );
   }
 }
@@ -1181,6 +1204,25 @@ void QtxActionMenuMgr::triggerUpdate( const int id, const bool rec )
   QtxActionMgr::triggerUpdate();
 }
 
+/*!
+  \brief Called when action is changed.
+
+  Schedule delayed update for parent menu of changed action.
+*/
+void QtxActionMenuMgr::actionChanged( int id )
+{
+  NodeList aNodes;
+  find( id, aNodes );
+
+  for ( NodeList::iterator it = aNodes.begin(); it != aNodes.end(); ++it )
+  {
+    MenuNode* node = *it;
+    if ( node->visible ) {
+      triggerUpdate( node->parent ? node->parent->id : myRoot->id, false );
+    }
+  }
+}
+
 /*!
   \brief Called when delayed content update is performed.