Salome HOME
HYDRO bug 17: add HYDRO version in About dialog.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Module.cxx
index 3993bfc452439a3e169a6a5f739c53841edf5261..c355b51cc17d09b81ec638636f5c95c53e4c5e58 100644 (file)
 #include "HYDROGUI_ObjSelector.h"
 #include "HYDROGUI_Operations.h"
 #include "HYDROGUI_PrsImage.h"
+#include "HYDROGUI_Tool.h"
 #include "HYDROGUI_UpdateFlags.h"
 
+#include <HYDROData_Image.h>
+
+#include <HYDROOperations_Factory.h>
+
 #include <GraphicsView_ViewFrame.h>
 #include <GraphicsView_ViewManager.h>
 #include <GraphicsView_ViewPort.h>
 #include <GraphicsView_Viewer.h>
 
+#include <ImageComposer_CutOperator.h>
+#include <ImageComposer_CropOperator.h>
+#include <ImageComposer_FuseOperator.h>
+
 #include <LightApp_Application.h>
 #include <LightApp_GVSelector.h>
 #include <LightApp_SelectionMgr.h>
 #include <LightApp_UpdateFlags.h>
 
+#include <SALOME_Event.h>
+
 #include <SUIT_Study.h>
 #include <SUIT_ViewManager.h>
 
+#include <QAction>
 #include <QApplication>
+#include <QGraphicsSceneMouseEvent>
+#include <QMenu>
+
+static int ViewManagerId = 0;
 
 extern "C" HYDRO_EXPORT CAM_Module* createModule()
 {
   return new HYDROGUI_Module();
 }
 
+extern "C" HYDRO_EXPORT char* getModuleVersion()
+{
+  return (char*)HYDRO_VERSION;
+}
+
 HYDROGUI_Module::HYDROGUI_Module()
 : LightApp_Module( "HYDRO" ),
   myDisplayer( 0 ),
@@ -92,6 +113,8 @@ bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
   setMenuShown( true );
   setToolShown( true );
 
+  update( UF_All );
+
   updateCommandsStatus();
 
   return aRes;
@@ -99,6 +122,14 @@ bool HYDROGUI_Module::activateModule( SUIT_Study* theStudy )
 
 bool HYDROGUI_Module::deactivateModule( SUIT_Study* theStudy )
 {
+  ViewManagerMapIterator anIter( myViewManagerMap );
+  while( anIter.hasNext() )
+    if( SUIT_ViewManager* aViewManager = anIter.next().value().first )
+      getApp()->removeViewManager( aViewManager );
+  myViewManagerMap.clear();
+
+  myObjectStateMap.clear();
+
   setMenuShown( false );
   setToolShown( false );
 
@@ -117,6 +148,136 @@ void HYDROGUI_Module::viewManagers( QStringList& theTypesList ) const
   theTypesList << GraphicsView_Viewer::Type();
 }
 
+void HYDROGUI_Module::contextMenuPopup( const QString& theClient,
+                                        QMenu* theMenu,
+                                        QString& theTitle )
+{
+  HYDROGUI_DataModel* aModel = getDataModel();
+
+  size_t aViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( this );
+
+  bool anIsSelection = false;
+  bool anIsVisibleInSelection = false;
+  bool anIsHiddenInSelection = false;
+
+  bool anIsImage = false;
+  bool anIsImportedImage = false;
+  bool anIsCompositeImage = false;
+  bool anIsFusedImage = false;
+  bool anIsCutImage = false;
+  bool anIsSplittedImage = false;
+  bool anIsMustBeUpdatedImage = false;
+  bool anIsPolyline = false;
+  bool anIsVisualState = false;
+
+  HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( this );
+  for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
+  {
+    Handle(HYDROData_Object) anObject = aSeq.Value( anIndex );
+    if( !anObject.IsNull() )
+    {
+      anIsSelection = true;
+
+      bool aVisibility = isObjectVisible( aViewId, anObject );
+      anIsVisibleInSelection |= aVisibility;
+      anIsHiddenInSelection |= !aVisibility;
+
+      if( anObject->GetKind() == KIND_IMAGE )
+      {
+        anIsImage = true;
+        Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anObject );
+        if( !anImage.IsNull() )
+        {
+          anIsImportedImage = anImage->HasTrsfPoints() && !anImage->IsSelfSplitted();
+          anIsCompositeImage = anImage->NbReferences() > 0;
+          if( HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory() )
+          {
+            if( ImageComposer_Operator* anOperator = aFactory->Operator( anImage ) )
+            {
+              if( dynamic_cast<ImageComposer_FuseOperator*>( anOperator ) )
+                anIsFusedImage = true;
+              else if( dynamic_cast<ImageComposer_CutOperator*>( anOperator ) )
+                anIsCutImage = true;
+              else if( dynamic_cast<ImageComposer_CropOperator*>( anOperator ) )
+                anIsSplittedImage = true;
+            }
+          }
+          anIsMustBeUpdatedImage = anImage->MustBeUpdated();
+        }
+      }
+      else if( anObject->GetKind() == KIND_POLYLINE )
+        anIsPolyline = true;
+      else if( anObject->GetKind() == KIND_VISUAL_STATE )
+        anIsVisualState = true;
+    }
+  }
+
+  if( aSeq.IsEmpty() )
+  {
+    theMenu->addAction( action( SaveVisualStateId ) );
+    theMenu->addSeparator();
+  }
+
+  if( anIsSelection && anIsMustBeUpdatedImage )
+  {
+    theMenu->addAction( action( UpdateImageId ) );
+    theMenu->addSeparator();
+  }
+
+  if( anIsSelection && aSeq.Length() == 1 )
+  {
+    if( anIsImage )
+    {
+      if( anIsImportedImage )
+        theMenu->addAction( action( EditImportedImageId ) );
+      else if( anIsCompositeImage )
+      {
+        if( anIsFusedImage )
+          theMenu->addAction( action( EditFusedImageId ) );
+        else if( anIsCutImage )
+          theMenu->addAction( action( EditCutImageId ) );
+        else if( anIsSplittedImage )
+          theMenu->addAction( action( EditSplittedImageId ) );
+      }
+
+      theMenu->addAction( action( ObserveImageId ) );
+      theMenu->addAction( action( ExportImageId ) );
+      theMenu->addSeparator();
+    }
+    else if( anIsPolyline )
+    {
+      theMenu->addAction( action( EditPolylineId ) );
+      theMenu->addSeparator();
+    }
+    else if( anIsVisualState )
+    {
+      theMenu->addAction( action( SaveVisualStateId ) );
+      theMenu->addAction( action( LoadVisualStateId ) );
+      theMenu->addSeparator();
+    }
+  }
+
+  if( anIsSelection )
+  {
+    theMenu->addAction( action( DeleteId ) );
+    theMenu->addSeparator();
+  }
+
+  if( anIsSelection && ( anIsImage || anIsPolyline ) )
+  {
+    if( anIsHiddenInSelection )
+      theMenu->addAction( action( ShowId ) );
+    theMenu->addAction( action( ShowOnlyId ) );
+    if( anIsVisibleInSelection )
+      theMenu->addAction( action( HideId ) );
+    theMenu->addSeparator();
+  }
+
+  theMenu->addAction( action( ShowAllId ) );
+  theMenu->addAction( action( HideAllId ) );
+  theMenu->addSeparator();
+}
+
 void HYDROGUI_Module::update( const int flags )
 {
   if( !isUpdateEnabled() )
@@ -128,21 +289,25 @@ void HYDROGUI_Module::update( const int flags )
   // from one of the methods called below
   setUpdateEnabled( false );
 
-  if( ( flags & UF_Model ) && getDataModel() )
+  if( ( flags & UF_Viewer ) )
+    updateGV( flags & UF_GV_Init,
+              flags & UF_GV_Forced );
+
+  if( ( flags & UF_Model ) && getDataModel() && getApp() )
   {
     getDataModel()->update( getStudyId() );
-  }
-  else
-  {
-    /* to do
-    if( ( flags & UF_ObjBrowser ) && getObjectBrowser() )
-      updateObjectBrowser();
-    */
+
+    // Temporary workaround to prevent breaking
+    // the selection in the object browser.
+    // Note: processEvents() should be called after updateGV(),
+    // otherwise the application crashes from time to time.
+    qApp->processEvents(); 
+    getApp()->updateObjectBrowser( true );
   }
 
-  if( ( flags & UF_Viewer ) )
-    updateGV( flags & UF_GV_Init,
-              flags & UF_GV_Forced );
+  // Object browser is currently updated by using UF_Model flag
+  //if( ( flags & UF_ObjBrowser ) && getApp() )
+  //  getApp()->updateObjectBrowser( true );
 
   if( ( flags & UF_Controls ) && getApp() )
     getApp()->updateActions();
@@ -172,27 +337,138 @@ HYDROGUI_Displayer* HYDROGUI_Module::getDisplayer() const
   return myDisplayer;
 }
 
-GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theViewerId ) const
+GraphicsView_Viewer* HYDROGUI_Module::getViewer( const int theId ) const
 {
-  ViewManagerList aViewManagerList;
-  getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewManagerList );
-
-  ViewManagerList::iterator anIter, anIterEnd = aViewManagerList.end();
-  for( anIter = aViewManagerList.begin(); anIter != anIterEnd; anIter++ )
+  if( myViewManagerMap.contains( theId ) )
   {
+    ViewManagerInfo anInfo = myViewManagerMap[ theId ];
     GraphicsView_ViewManager* aViewManager =
-      dynamic_cast<GraphicsView_ViewManager*>( *anIter );
-    if( aViewManager && aViewManager->getId() == theViewerId )
+      dynamic_cast<GraphicsView_ViewManager*>( anInfo.first );
+    if( aViewManager )
       return aViewManager->getViewer();
   }
   return NULL;
 }
 
+int HYDROGUI_Module::getViewManagerId( SUIT_ViewManager* theViewManager )
+{
+  ViewManagerMapIterator anIter( myViewManagerMap );
+  while( anIter.hasNext() )
+  {
+    int anId = anIter.next().key();
+    const ViewManagerInfo& anInfo = anIter.value();
+    if( anInfo.first == theViewManager )
+      return anId;
+  }
+  return -1;
+}
+
+HYDROGUI_Module::ViewManagerRole HYDROGUI_Module::getViewManagerRole( SUIT_ViewManager* theViewManager )
+{
+  int anId = getViewManagerId( theViewManager );
+  if( anId != -1 )
+  {
+    const ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
+    return anInfo.second;
+  }
+  return VMR_Unknown;
+}
+
+void HYDROGUI_Module::setViewManagerRole( SUIT_ViewManager* theViewManager,
+                                          const ViewManagerRole theRole )
+{
+  int anId = getViewManagerId( theViewManager );
+  if( anId != -1 )
+  {
+    ViewManagerInfo& anInfo = myViewManagerMap[ anId ];
+    anInfo.second = theRole;
+  }
+}
+
+bool HYDROGUI_Module::isObjectVisible( const int theViewId,
+                                       const Handle(HYDROData_Object)& theObject )
+{
+  if( theObject.IsNull() )
+    return false;
+
+  ViewId2Name2ObjectStateMap::const_iterator anIter1 = myObjectStateMap.find( theViewId );
+  if( anIter1 != myObjectStateMap.end() )
+  {
+    const Name2ObjectStateMap& aName2ObjectStateMap = anIter1.value();
+    Name2ObjectStateMap::const_iterator anIter2 = aName2ObjectStateMap.find( theObject->GetName());
+    if( anIter2 != aName2ObjectStateMap.end() )
+    {
+      const ObjectState& anObjectState = anIter2.value();
+      return anObjectState.Visibility;
+    }
+  }
+  return false;
+}
+
+void HYDROGUI_Module::setObjectVisible( const int theViewId,
+                                        const Handle(HYDROData_Object)& theObject,
+                                        const bool theState )
+{
+  if( !theObject.IsNull() )
+  {
+    Name2ObjectStateMap& aName2ObjectStateMap = myObjectStateMap[ theViewId ];
+    ObjectState& anObjectState = aName2ObjectStateMap[ theObject->GetName() ];
+    anObjectState.Visibility = theState;
+  }
+}
+
 CAM_DataModel* HYDROGUI_Module::createDataModel()
 {
   return new HYDROGUI_DataModel( this );
 }
 
+void HYDROGUI_Module::customEvent( QEvent* e )
+{
+  int aType = e->type();
+  if ( aType == NewViewEvent )
+  {
+    SALOME_CustomEvent* ce = ( SALOME_CustomEvent* )e;
+    if( GraphicsView_ViewFrame* aViewFrame = ( GraphicsView_ViewFrame* )ce->data() )
+    {
+      if( GraphicsView_Viewer* aViewer = dynamic_cast<GraphicsView_Viewer*>( aViewFrame->getViewer() ) )
+      {
+        if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
+        {
+          aViewPort->setInteractionFlag( GraphicsView_ViewPort::TraceBoundingRect );
+          aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu );
+          aViewPort->setInteractionFlag( GraphicsView_ViewPort::ImmediateSelection );
+
+          //ouv: temporarily commented
+          //aViewPort->setViewLabelPosition( GraphicsView_ViewPort::VLP_BottomLeft, true );
+        }
+
+        SUIT_ViewManager* aViewManager = aViewer->getViewManager();
+        ViewManagerRole aRole = getViewManagerRole( aViewManager );
+        if( aRole != VMR_TransformImage )
+          update( UF_Viewer );
+
+        aViewer->activateTransform( GraphicsView_Viewer::FitAll );
+      }
+    }
+  }
+}
+
+bool HYDROGUI_Module::eventFilter( QObject* theObj, QEvent* theEvent )
+{
+  QEvent::Type aType = theEvent->type();
+  if( theObj->inherits( "GraphicsView_ViewFrame" ) )
+  {
+    if( aType == QEvent::Show )
+    {
+      SALOME_CustomEvent* e = new SALOME_CustomEvent( NewViewEvent );
+      e->setData( theObj );
+      QApplication::postEvent( this, e );
+      theObj->removeEventFilter( this );
+    }
+  }
+  return LightApp_Module::eventFilter( theObj, theEvent );
+}
+
 void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
 {
   LightApp_Module::onViewManagerAdded( theViewManager );
@@ -204,6 +480,9 @@ void HYDROGUI_Module::onViewManagerAdded( SUIT_ViewManager* theViewManager )
     connect( theViewManager, SIGNAL( viewCreated( SUIT_ViewWindow* ) ),
              this, SLOT( onViewCreated( SUIT_ViewWindow* ) ) );
   }
+
+  ViewManagerInfo anInfo( theViewManager, VMR_General );
+  myViewManagerMap.insert( ViewManagerId++, anInfo );
 }
 
 void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
@@ -211,10 +490,72 @@ void HYDROGUI_Module::onViewManagerRemoved( SUIT_ViewManager* theViewManager )
   LightApp_Module::onViewManagerRemoved( theViewManager );
 
   createSelector( theViewManager ); // replace the default selector
+
+  int anId = getViewManagerId( theViewManager );
+  if( anId != -1 )
+    myViewManagerMap.remove( anId );
 }
 
-void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theWnd )
+void HYDROGUI_Module::onViewCreated( SUIT_ViewWindow* theViewWindow )
 {
+  if( theViewWindow && theViewWindow->inherits( "GraphicsView_ViewFrame" ) )
+  {
+    if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( theViewWindow ) )
+    {
+      aViewFrame->installEventFilter( this );
+
+      GraphicsView_ViewPort* aViewPort = aViewFrame->getViewPort();
+
+      connect( aViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ),
+               this, SLOT( onViewPortMouseEvent( QGraphicsSceneMouseEvent* ) ) );
+      return;
+    }
+  }
+}
+
+void HYDROGUI_Module::onViewPortMouseEvent( QGraphicsSceneMouseEvent* theEvent )
+{
+  if( GraphicsView_ViewPort* aViewPort = qobject_cast<GraphicsView_ViewPort*>( sender() ) )
+  {
+    SUIT_ViewManager* aViewManager = 0;
+
+    QObject* aParent = aViewPort;
+    while( aParent = aParent->parent() )
+    {
+      if( GraphicsView_ViewFrame* aViewFrame = dynamic_cast<GraphicsView_ViewFrame*>( aParent ) )
+      {
+        if( GraphicsView_Viewer* aViewer = aViewFrame->getViewer() )
+        {
+          aViewManager = aViewer->getViewManager();
+          break;
+        }
+      }
+    }
+
+    if( !aViewManager )
+      return;
+
+    double aMouseX = theEvent->scenePos().x();
+    double aMouseY = theEvent->scenePos().y();
+
+    ViewManagerRole aRole = getViewManagerRole( aViewManager );
+    if( aRole == VMR_General )
+    {
+      int aXDeg = 0, aYDeg = 0;
+      int aXMin = 0, aYMin = 0;
+      double aXSec = 0, aYSec = 0;
+      HYDROGUI_Tool::DoubleToLambert( aMouseX, aXDeg, aXMin, aXSec );
+      HYDROGUI_Tool::DoubleToLambert( aMouseY, aYDeg, aYMin, aYSec );
+
+      QString aDegSymbol( QChar( 0x00B0 ) );
+      QString aXStr = QString( "%1%2 %3' %4\"" ).arg( aXDeg ).arg( aDegSymbol ).arg( aXMin ).arg( aXSec );
+      QString aYStr = QString( "%1%2 %3' %4\"" ).arg( aYDeg ).arg( aDegSymbol ).arg( aYMin ).arg( aYSec );
+
+      aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( aXStr ).arg( aYStr ) );
+    }
+    else if( aRole == VMR_TransformImage )
+      aViewPort->setViewLabelText( QString( "X: %1\nY: %2" ).arg( (int)aMouseX ).arg( (int)aMouseY ) );
+  }
 }
 
 void HYDROGUI_Module::updateGV( const bool theIsInit,
@@ -223,17 +564,19 @@ void HYDROGUI_Module::updateGV( const bool theIsInit,
   if( !getDisplayer() )
     return;
 
-  ViewManagerList aViewManagerList;
-  getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewManagerList );
+  QList<int> aViewManagerIdList;
 
-  ViewManagerList::iterator anIter, anIterEnd = aViewManagerList.end();
-  for( anIter = aViewManagerList.begin(); anIter != anIterEnd; anIter++ )
+  // currently, all views are updated
+  ViewManagerMapIterator anIter( myViewManagerMap );
+  while( anIter.hasNext() )
   {
-    GraphicsView_ViewManager* aViewManager =
-      dynamic_cast<GraphicsView_ViewManager*>( *anIter );
-    if( aViewManager )
-      getDisplayer()->UpdateAll( aViewManager->getId(), theIsInit, theIsForced );
+    int anId = anIter.next().key();
+    aViewManagerIdList.append( anId );
   }
+
+  QListIterator<int> anIdIter( aViewManagerIdList );
+  while( anIdIter.hasNext() )
+    getDisplayer()->UpdateAll( anIdIter.next(), theIsInit, theIsForced );
 }
 
 void HYDROGUI_Module::createSelector( SUIT_ViewManager* theViewManager )