Salome HOME
Fix pb with Object Browser update, when several not-loaded components have data....
[modules/gui.git] / src / SVTK / SVTK_ViewWindow.cxx
index a6d58a5c20a5fec40c50433450464e8b2fda7be5..5260b9732e43ad1e00fbf062bc8fa5c00cfd9e32 100755 (executable)
@@ -1,8 +1,8 @@
-#include "SVTK_Selector.h"
 #include "SALOME_Actor.h"
 
 #include <qapplication.h>
 
+#include <vtkTextProperty.h>
 #include <vtkActorCollection.h>
 #include <vtkRenderWindow.h>
 #include <vtkRenderer.h>
 
 #include "SUIT_Tools.h"
 #include "SUIT_ResourceMgr.h"
-#include "SUIT_FileDlg.h"
 
 #include "VTKViewer_Transform.h"
 #include "VTKViewer_Utilities.h"
 
 #include "SVTK_Trihedron.h"
+#include "SVTK_CubeAxesActor2D.h"
 #include "SVTK_ViewWindow.h"
 #include "SVTK_ViewModel.h"
 #include "SVTK_RenderWindow.h"
@@ -30,6 +30,8 @@
 
 #include "SALOME_ListIteratorOfListIO.hxx"
 
+#include "SVTK_SelectorDef.h"
+
 #include "VTKViewer_Algorithm.h"
 #include "SVTK_Functor.h"
 
 SVTK_ViewWindow
 ::SVTK_ViewWindow( SUIT_Desktop* theDesktop, 
                   SVTK_Viewer* theModel)
-  : SUIT_ViewWindow(theDesktop)
+  : SUIT_ViewWindow( theDesktop ),
+    myTrihedronSize( 100 )
 {
   myModel = theModel;
-  mySelector = new SVTK_Selector();
+  mySelector = new SVTK_SelectorDef();
   connect(this,SIGNAL(selectionChanged()),theModel,SLOT(onSelectionChanged()));
 
   myTransform = VTKViewer_Transform::New();
   myTrihedron = SVTK_Trihedron::New();
+  myCubeAxes  = SVTK_CubeAxesActor2D::New();
   myRenderer  = vtkRenderer::New() ;
 
   myTrihedron->AddToRender( myRenderer );
+  myRenderer->AddProp(myCubeAxes);
 
   myRenderWindow = new SVTK_RenderWindow( this, "RenderWindow" );
   setCentralWidget(myRenderWindow);
@@ -82,11 +87,32 @@ SVTK_ViewWindow
   myRWInteractor->SetInteractorStyle( RWS ); 
   myRWInteractor->Initialize();
 
-  RWS->setTriedron( myTrihedron );
+  //merge with V2_2_0_VISU_improvements:RWS->setTriedron( myTrihedron );
   RWS->FindPokedRenderer( 0, 0 );
 
   SetSelectionMode(ActorSelection);
 
+  vtkTextProperty* tprop = vtkTextProperty::New();
+  tprop->SetColor(1, 1, 1);
+  tprop->ShadowOn();
+  
+  float bnd[6];
+  bnd[0] = bnd[2] = bnd[4] = 0;
+  bnd[1] = bnd[3] = bnd[5] = myTrihedron->GetSize();
+  myCubeAxes->SetLabelFormat("%6.4g");
+  myCubeAxes->SetBounds(bnd);
+  myCubeAxes->SetCamera(myRenderer->GetActiveCamera());
+  myCubeAxes->SetFlyModeToOuterEdges(); // ENK remarks: it must bee
+  myCubeAxes->SetFontFactor(0.8);
+  myCubeAxes->SetAxisTitleTextProperty(tprop);
+  myCubeAxes->SetAxisLabelTextProperty(tprop);
+  myCubeAxes->SetCornerOffset(0);
+  myCubeAxes->SetScaling(0);
+  myCubeAxes->SetNumberOfLabels(5);
+  myCubeAxes->VisibilityOff();
+  myCubeAxes->SetTransform(myTransform);
+  tprop->Delete();
+  
   setCentralWidget( myRenderWindow );
 
   myToolBar = new QToolBar(this);
@@ -130,8 +156,9 @@ SVTK_ViewWindow
   
   //m_RW->Delete() ;
   myRenderer->RemoveAllProps();
-  //m_Renderer->Delete() ;
+  //m_Renderer->Delete();
   myTrihedron->Delete();
+  myCubeAxes->Delete();
 }
 
 //----------------------------------------------------------------------------
@@ -278,6 +305,13 @@ SVTK_ViewWindow
   aAction->setStatusTip(tr("DSC_RESET_VIEW"));
   connect(aAction, SIGNAL(activated()), this, SLOT(onResetView()));
   myActionsMap[ ResetId ] = aAction;
+
+  // onViewTrihedron: Shows - Hides Trihedron
+  aAction = new QtxAction(tr("MNU_SHOW_TRIHEDRON"), aResMgr->loadPixmap( "VTKViewer", tr( "ICON_VTKVIEWER_VIEW_TRIHEDRON" ) ),
+                           tr( "MNU_SHOW_TRIHEDRON" ), 0, this);
+  aAction->setStatusTip(tr("DSC_SHOW_TRIHEDRON"));
+  connect(aAction, SIGNAL(activated()), this, SLOT(onViewTrihedron()));
+  myActionsMap[ ViewTrihedronId ] = aAction;
 }
 
 //----------------------------------------------------------------------------
@@ -286,6 +320,7 @@ SVTK_ViewWindow
 ::createToolBar()
 {
   myActionsMap[DumpId]->addTo(myToolBar);
+  myActionsMap[ViewTrihedronId]->addTo(myToolBar);
 
   SUIT_ToolButton* aScaleBtn = new SUIT_ToolButton(myToolBar);
   aScaleBtn->AddAction(myActionsMap[FitAllId]);
@@ -387,14 +422,23 @@ SVTK_ViewWindow
 ::onResetView()
 {
   int aTrihedronIsVisible = isTrihedronDisplayed();
+  int aCubeAxesIsVisible  = isCubeAxesDisplayed();
+
   myTrihedron->SetVisibility( VTKViewer_Trihedron::eOnlyLineOn );
+  myCubeAxes->SetVisibility(0);
+
   ::ResetCamera(myRenderer,true);  
   vtkCamera* aCamera = myRenderer->GetActiveCamera();
   aCamera->SetPosition(1,-1,1);
   aCamera->SetViewUp(0,0,1);
   ::ResetCamera(myRenderer,true);  
-  if(aTrihedronIsVisible) myTrihedron->VisibilityOn();
+
+  if (aTrihedronIsVisible) myTrihedron->VisibilityOn();
   else myTrihedron->VisibilityOff();
+
+  if (aCubeAxesIsVisible) myCubeAxes->VisibilityOn();
+  else myCubeAxes->VisibilityOff();
+
   static float aCoeff = 3.0;
   aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
   Repaint();
@@ -405,37 +449,43 @@ void
 SVTK_ViewWindow
 ::onFitAll()
 {
-  myRWInteractor->GetSInteractorStyle()->ViewFitAll();
-  Repaint();
-}
+  int aTrihedronWasVisible = false;
+  int aCubeAxesWasVisible = false;
+  if (myTrihedron) {
+    aTrihedronWasVisible = isTrihedronDisplayed();
+    if (aTrihedronWasVisible)
+      myTrihedron->VisibilityOff();
+  }
 
-//----------------------------------------------------------------------------
-void
-SVTK_ViewWindow
-::onDumpView()
-{
-  QApplication::setOverrideCursor( Qt::waitCursor );
-  QPixmap px = QPixmap::grabWindow(myRenderWindow->winId());
-  QApplication::restoreOverrideCursor();
-  
-  QString fileName = SUIT_FileDlg::getFileName(this,
-                                              QString::null,
-                                              tr("VTK_IMAGE_FILES"),
-                                              tr("INF_APP_DUMP_VIEW"),
-                                              false);
-  if (!fileName.isNull()) {
-    QApplication::setOverrideCursor( Qt::waitCursor );
-    QString fmt = SUIT_Tools::extension( fileName ).upper();
-    if (fmt.isEmpty())
-      fmt = QString("BMP"); // default format
-    if (fmt == "JPG")
-      fmt = "JPEG";
-    bool bOk = px.save(fileName, fmt.latin1());
-    QApplication::restoreOverrideCursor();
-    if (!bOk) {
-      SUIT_MessageBox::error1(this, tr("ERROR"), tr("ERR_DOC_CANT_SAVE_FILE"), tr("BUT_OK"));
-    }
+  if (myCubeAxes) {
+    aCubeAxesWasVisible = isCubeAxesDisplayed();
+    if (aCubeAxesWasVisible)
+      myCubeAxes->VisibilityOff();
   }
+
+  if (myTrihedron->GetVisibleActorCount(myRenderer)) {
+    myTrihedron->VisibilityOff();
+    myCubeAxes->VisibilityOff();
+    ::ResetCamera(myRenderer);
+  } else {
+    myTrihedron->SetVisibility(VTKViewer_Trihedron::eOnlyLineOn);
+    myCubeAxes->SetVisibility(2);
+    ::ResetCamera(myRenderer,true);
+  }
+
+  if (aTrihedronWasVisible)
+    myTrihedron->VisibilityOn();
+  else
+    myTrihedron->VisibilityOff();
+
+  if (aCubeAxesWasVisible)
+    myCubeAxes->VisibilityOn();
+  else
+    myCubeAxes->VisibilityOff();
+
+  ::ResetCameraClippingRange(myRenderer);
+
+  Repaint();
 }
 
 //----------------------------------------------------------------
@@ -568,7 +618,7 @@ void
 SVTK_ViewWindow
 ::GetScale( double theScale[3] ) 
 {
-  myTransform->GetScale( theScale );
+  myTransform->GetMatrixScale( theScale );
 }
 
 //----------------------------------------------------------------------------
@@ -576,7 +626,7 @@ void
 SVTK_ViewWindow
 ::SetScale( double theScale[3] ) 
 {
-  myTransform->SetScale( theScale[0], theScale[1], theScale[2] );
+  myTransform->SetMatrixScale( theScale[0], theScale[1], theScale[2] );
   myRWInteractor->Render();
   Repaint();
 }
@@ -589,6 +639,13 @@ SVTK_ViewWindow
   return myTrihedron->GetVisibility() == VTKViewer_Trihedron::eOn;
 }
 
+bool
+SVTK_ViewWindow
+::isCubeAxesDisplayed()
+{
+  return myCubeAxes->GetVisibility() == 1;
+}
+
 //----------------------------------------------------------------------------
 void 
 SVTK_ViewWindow
@@ -605,8 +662,23 @@ SVTK_ViewWindow
   Repaint();
 }
 
+void
+SVTK_ViewWindow
+::onViewCubeAxes()
+{
+  if(!myCubeAxes)
+    return;
+
+  if(isCubeAxesDisplayed())
+    myCubeAxes->VisibilityOff();
+  else
+    myCubeAxes->VisibilityOn();
+
+  Repaint();
+}
+
 //----------------------------------------------------------------------------
-bool
+/*bool
 SVTK_ViewWindow
 ::ComputeTrihedronSize( double& theNewSize, double& theSize )
 {
@@ -632,10 +704,7 @@ SVTK_ViewWindow
     aLength = max( ( aBndBox[ 5 ] - aBndBox[ 4 ] ),aLength );
   }
 
-  static float aSizeInPercents = 105;
-  //QString aSetting = QAD_CONFIG->getSetting( "Viewer:TrihedronSize" );
-  //if ( !aSetting.isEmpty() )
-  //  aSizeInPercents = aSetting.toFloat();
+  float aSizeInPercents = myTrihedronSize;
 
   static float EPS_SIZE = 5.0E-3;
   theSize = myTrihedron->GetSize();
@@ -644,42 +713,96 @@ SVTK_ViewWindow
   // if the new trihedron size have sufficient difference, then apply the value
   return fabs( theNewSize - theSize) > theSize * EPS_SIZE ||
          fabs( theNewSize-theSize ) > theNewSize * EPS_SIZE;
-}
+}*/
 
 //----------------------------------------------------------------------------
-double
-SVTK_ViewWindow
-::GetTrihedronSize() const
+int SVTK_ViewWindow::GetTrihedronSize() const
 {
-  return myTrihedron->GetSize();
+  return myTrihedronSize;
 }
 
-//----------------------------------------------------------------------------
+void SVTK_ViewWindow::SetTrihedronSize( const int sz )
+{
+  if ( myTrihedronSize == sz )
+    return;
+
+  myTrihedronSize = sz;
+  AdjustTrihedrons( true );
+}
+
+/*! If parameter theIsForcedUpdate is true, recalculate parameters for
+ *  trihedron and cube axes, even if trihedron and cube axes is invisible.
+ */
 void
 SVTK_ViewWindow
-::AdjustTrihedrons( const bool theIsForcedUpdate )
+::AdjustTrihedrons(const bool theIsForcedUpdate)
 {
-  if ( !isTrihedronDisplayed() && !theIsForcedUpdate )
+  if ((!isCubeAxesDisplayed() && !isTrihedronDisplayed()) && !theIsForcedUpdate)
     return;
 
+  float bnd[ 6 ];
+  float newbnd[6];
+  newbnd[ 0 ] = newbnd[ 2 ] = newbnd[ 4 ] = VTK_LARGE_FLOAT;
+  newbnd[ 1 ] = newbnd[ 3 ] = newbnd[ 5 ] = -VTK_LARGE_FLOAT;
+
+  myCubeAxes->GetBounds(bnd);
+
   int aVisibleNum = myTrihedron->GetVisibleActorCount( myRenderer );
-  if ( aVisibleNum || theIsForcedUpdate ) {
+  //if (aVisibleNum || theIsForcedUpdate) {
+  if (aVisibleNum) {
     // if the new trihedron size have sufficient difference, then apply the value
-    double aNewSize = 100, anOldSize;
-    if ( ComputeTrihedronSize( aNewSize, anOldSize ) || theIsForcedUpdate ) {
-      myTrihedron->SetSize( aNewSize );
-      // itearte throuh displayed objects and set size if necessary
-      
-      vtkActorCollection* anActors = getRenderer()->GetActors();
-      anActors->InitTraversal();
-      while( vtkActor* anActor = anActors->GetNextActor() ) {
-        if( SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast( anActor ) ) {
-          if ( aSActor->IsResizable() )
-            aSActor->SetSize( 0.5 * aNewSize );
+    double aNewSize = 100, anOldSize=myTrihedron->GetSize();
+    bool aTDisplayed = isTrihedronDisplayed();
+    bool aCDisplayed = isCubeAxesDisplayed();
+    if(aTDisplayed) myTrihedron->VisibilityOff();
+    if(aCDisplayed) myCubeAxes->VisibilityOff();
+
+    SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
+    QString aSetting = aResMgr->stringValue("Viewer", "TrihedronSize", "105");
+    static float aSizeInPercents = aSetting.toFloat();
+
+    //bool isComputeTrihedronSize =
+      ::ComputeTrihedronSize(myRenderer, aNewSize, anOldSize, aSizeInPercents);
+
+    myTrihedron->SetSize( aNewSize );
+
+    // iterate through displayed objects and set size if necessary
+    vtkActorCollection* anActors = getRenderer()->GetActors();
+    anActors->InitTraversal();
+    while (vtkActor* anActor = anActors->GetNextActor())
+    {
+      if (SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast( anActor ))
+      {
+       if (aSActor->IsResizable())
+         aSActor->SetSize( 0.5 * aNewSize );
+        if (aSActor->GetVisibility() && !aSActor->IsInfinitive()) {
+         float *abounds = aSActor->GetBounds();
+          if (abounds[0] > -VTK_LARGE_FLOAT && abounds[1] < VTK_LARGE_FLOAT &&
+              abounds[2] > -VTK_LARGE_FLOAT && abounds[3] < VTK_LARGE_FLOAT &&
+              abounds[4] > -VTK_LARGE_FLOAT && abounds[5] < VTK_LARGE_FLOAT)
+           for (int i = 0; i < 5; i = i + 2) {
+             if (abounds[i] < newbnd[i]) newbnd[i] = abounds[i];
+             if (abounds[i+1] > newbnd[i+1]) newbnd[i+1] = abounds[i+1];
+           }
         }
       }
     }
+    if (aTDisplayed) myTrihedron->VisibilityOn();
+    if (aCDisplayed) myCubeAxes->VisibilityOn();
+    
+  } else {
+     double aSize = myTrihedron->GetSize();
+     newbnd[0] = newbnd[2] = newbnd[4] = 0;
+     newbnd[1] = newbnd[3] = newbnd[5] = aSize;
   }
+  
+  if (newbnd[0] < VTK_LARGE_FLOAT && newbnd[2] < VTK_LARGE_FLOAT && newbnd[4] < VTK_LARGE_FLOAT &&
+      newbnd[1] >-VTK_LARGE_FLOAT && newbnd[3] >-VTK_LARGE_FLOAT && newbnd[5] >-VTK_LARGE_FLOAT) {
+    for(int i=0;i<6;i++) bnd[i] = newbnd[i];
+    myCubeAxes->SetBounds(bnd);
+  }
+  
+  myCubeAxes->SetBounds(bnd);
 
   ::ResetCameraClippingRange(myRenderer);
 }
@@ -692,6 +815,13 @@ SVTK_ViewWindow
   AdjustTrihedrons( false );
 }
 
+void
+SVTK_ViewWindow
+::onAdjustCubeAxes()
+{   
+  AdjustTrihedrons(false);
+}
+
 #define INCREMENT_FOR_OP 10
 
 //=======================================================================
@@ -891,3 +1021,12 @@ SVTK_ViewWindow
   RemoveActor(theActor);
   InsertActor(theActor,true);
 }
+
+//----------------------------------------------------------------------------
+QImage
+SVTK_ViewWindow
+::dumpView()
+{
+  QPixmap px = QPixmap::grabWindow( myRenderWindow->winId() );
+  return px.convertToImage();
+}