Salome HOME
Improve Action managers for case with actions visibility change V8_3_asterstudy_v0.8.4
authorstv <sergey.telkov@opencascade.com>
Thu, 15 Jun 2017 11:26:56 +0000 (14:26 +0300)
committerstv <sergey.telkov@opencascade.com>
Thu, 15 Jun 2017 11:26:56 +0000 (14:26 +0300)
src/Qtx/Qtx.cxx
src/Qtx/QtxActionMenuMgr.cxx
src/Qtx/QtxActionMenuMgr.h
src/Qtx/QtxActionMgr.cxx
src/Qtx/QtxActionMgr.h

index d596874ce2988a8647757aed20e31913fb0f7b8e..09e879e34cbb18157376850ac6b417de0181a159 100755 (executable)
@@ -277,26 +277,31 @@ void Qtx::simplifySeparators( QWidget* wid, const bool recursive )
   if ( items.isEmpty() )
     return;
 
-  QList<QAction*> toRemove;
-  for ( int i = 1; i < items.count(); i++ )
+  bool action = false;
+  for ( int i = 0; i < items.count(); i++ )
   {
-    if ( items[i]->isSeparator() && items[i - 1]->isSeparator() )
-      toRemove.append( items[i] );
-
-    if ( recursive && items[i]->menu() )
-      simplifySeparators( items[i]->menu(), recursive );
+    QAction* a = items[i];
+    if ( a->isSeparator() ) {
+      a->setVisible(action);
+      action = false;
+    }
+    else if ( a->isVisible() ) {
+      action = true;
+      if ( recursive && a->menu() )
+       simplifySeparators( a->menu(), recursive );
+    }
   }
 
-  for ( QList<QAction*>::iterator it = toRemove.begin(); it != toRemove.end(); ++it )
-    wid->removeAction( *it );
-
-  items = wid->actions();
-  if ( !items.isEmpty() && items[0]->isSeparator() )
-    wid->removeAction( items[0] );
-
-  items = wid->actions();
-  if ( !items.isEmpty() && items[items.count() - 1]->isSeparator() )
-    wid->removeAction( items[items.count() - 1] );
+  action = false;
+  for ( int i = items.count() - 1; i > 0; i-- ) {
+    QAction* a = items[i];
+    if ( a->isSeparator() ) {
+      a->setVisible(action);
+      action = false;
+    }
+    else if ( a->isVisible() )
+      action = true;
+  }
 }
 
 /*!
index a4972a755060fa82bab86a716c7ed6f4c6886332..54704d0c73584779f250f4d71e0822d43913b47b 100644 (file)
@@ -874,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;
@@ -966,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 );
+  }
 }
 
 /*!
@@ -1019,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;
 }
 
 /*!
@@ -1200,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.
 
index fd99a0d105cc7dce4971e7875486e0a1232475bf..dcd3f2967a0edf7bf5e13ce32527480777b56b24 100644 (file)
@@ -132,6 +132,7 @@ protected:
   void         updateMenu( MenuNode* = 0, const bool = true, const bool = true );
   virtual void internalUpdate();
   virtual void updateContent();
+  virtual void actionChanged( int );
 
 private:
   bool         ownAction( QAction*, MenuNode* ) const;
index 528ae83491e4f08a56c60ee922755e7e8d43d049..9fb7bb6ad49a3ab03d480ff48c059914ccc46fec 100644 (file)
@@ -156,6 +156,8 @@ int QtxActionMgr::registerAction( QAction* a, const int userId )
 
   myActions.insert( theId, a );
 
+  connect( a, SIGNAL( changed() ), this, SLOT( onActionChanged() ) );
+
   return theId;
 }
 
@@ -166,8 +168,11 @@ int QtxActionMgr::registerAction( QAction* a, const int userId )
 */
 void QtxActionMgr::unRegisterAction( const int id )
 {
-  if( contains( id ) )
+  if ( contains( id ) ) {
+    disconnect( myActions[id], SIGNAL( changed() ),
+               this, SLOT( onActionChanged() ) );
     myActions.remove( id );
+  }
 }
 
 /*!
@@ -419,6 +424,13 @@ void QtxActionMgr::updateContent()
 {
 }
 
+/*!
+  \brief Internal action changing response operation.
+*/
+void QtxActionMgr::actionChanged( int )
+{
+}
+
 /*!
   \brief Called when delayed update is performed (via timer event).
 
@@ -430,6 +442,21 @@ void QtxActionMgr::onUpdateContent()
   updateContent();
 }
 
+/*!
+  \brief Called when one of the registered actions changed.
+
+  Calls virtual method actionChanged() which can be redefined in the
+  subclasses to customize reaction on this.
+*/
+void QtxActionMgr::onActionChanged()
+{
+  QAction* a = ::qobject_cast<QAction*>( sender() );
+
+  int id = actionId( a );
+  if ( id != -1 )
+    actionChanged( id );
+}
+
 /*!
   \class QtxActionMgr::Reader
   \brief Generic actions description files reader class.
index a1a0e66353752de3c6a2bed6d8ffeb5581a674eb..7cb0a6e96a0778ec6e356d1cf537cb4db5d19e3f 100644 (file)
@@ -88,8 +88,10 @@ protected:
 
   void             triggerUpdate();
   virtual void     updateContent();
+  virtual void     actionChanged( int );
 
 private slots:
+  void             onActionChanged();
   void             onUpdateContent();
 
 private: