Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / MeasureGUI / MeasureGUI_DistanceDlg.cxx
index 2591b69816106c7265681166db520e37b512d112..a684ce804282220ef60c33962a5ad964273c1410 100644 (file)
@@ -1,6 +1,6 @@
-//  GEOM GEOMGUI : GUI for Geometry component
+//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+//  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
 //
 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+// GEOM GEOMGUI : GUI for Geometry component
+// File   : MeasureGUI_DistanceDlg.cxx
+// Author : Nicolas REJNERI, Open CASCADE S.A.S.
 //
-//
-//  File   : MeasureGUI_DistanceDlg.cxx
-//  Author : Nicolas REJNERI
-//  Module : GEOM
-//  $Header$
-
 #include "MeasureGUI_DistanceDlg.h"
-#include "MeasureGUI_2Sel4LineEdit_QTD.h"
-#include "GEOMBase.h"
-#include "GEOM_Displayer.h"
-#include "DlgRef_SpinBox.h"
-
-#include "SUIT_Session.h"
-#include "SUIT_ViewWindow.h"
-#include "SOCC_Prs.h"
-#include "SOCC_ViewModel.h"
-#include "SalomeApp_Tools.h"
-
+#include "MeasureGUI_Widgets.h"
+
+#include <DlgRef.h>
+#include <GEOMBase.h>
+#include <GeometryGUI.h>
+
+#include <SUIT_Session.h>
+#include <SUIT_Desktop.h>
+#include <SUIT_ResourceMgr.h>
+#include <SUIT_ViewWindow.h>
+#include <SUIT_ViewManager.h>
+#include <SOCC_Prs.h>
+#include <SOCC_ViewModel.h>
+#include <SalomeApp_Tools.h>
+#include <SalomeApp_Application.h>
+#include <LightApp_SelectionMgr.h>
+
+// OCCT Includes
 #include <Geom_Plane.hxx>
-#include <TopoDS_Edge.hxx>
 #include <AIS_LengthDimension.hxx>
 #include <BRepBuilderAPI_MakeEdge.hxx>
 #include <BRepBuilderAPI_MakeVertex.hxx>
-#include <AIS_ListIteratorOfListOfInteractive.hxx>
 #include <gce_MakePln.hxx>
 #include <Precision.hxx>
 
-#include "utilities.h"
-
-#include <qlineedit.h>
-#include <qlabel.h>
-#include <qlayout.h>
-#include <qpushbutton.h>
-#include <qradiobutton.h>
-#include <qbuttongroup.h>
-
 //=================================================================================
 // class    : MeasureGUI_DistanceDlg()
 // purpose  : Constructs a MeasureGUI_DistanceDlg which is a child of 'parent', with the
 //            name 'name' and widget flags set to 'f'.
 //            The dialog will by default be modeless, unless you set 'modal' to
-//            TRUE to construct a modal dialog.
+//            true to construct a modal dialog.
 //=================================================================================
-MeasureGUI_DistanceDlg::MeasureGUI_DistanceDlg( GeometryGUI* GUI, QWidget* parent )
-: MeasureGUI_Skeleton( GUI, parent, "MeasureGUI_DistanceDlg" )
+MeasureGUI_DistanceDlg::MeasureGUI_DistanceDlg (GeometryGUI* GUI, QWidget* parent)
+  : MeasureGUI_Skeleton(GUI, parent)
 {
-  QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap(
-    "GEOM",tr( "ICON_DLG_MINDIST" ) ) );
-  QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap(
-  "GEOM",tr( "ICON_SELECT" ) ) );
+  SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
+  QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_MINDIST")));
+  QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
 
-  setCaption( tr( "GEOM_MINDIST_TITLE" ) );
+  setWindowTitle(tr("GEOM_MINDIST_TITLE"));
 
   /***************************************************************/
-  
-  GroupConstructors->setTitle( tr( "GEOM_DISTANCE" ) );
-  RadioButton1->setPixmap( image0 );
-
-  myGrp = new MeasureGUI_2Sel4LineEdit_QTD( this, "myGrp" );
-  myGrp->GroupBox1->setTitle( tr( "GEOM_MINDIST_OBJ" ) );
-  myGrp->TextLabel1->setText( tr( "GEOM_OBJECT_I" ).arg( "1" ) );
-  myGrp->TextLabel2->setText( tr( "GEOM_OBJECT_I" ).arg( "2" ) );
-  myGrp->TextLabel3->setText( tr( "GEOM_LENGTH" ) );
-  myGrp->TextLabel4->setText( tr( "GEOM_DX" ) );
-  myGrp->TextLabel5->setText( tr( "GEOM_DY" ) );
-  myGrp->TextLabel6->setText( tr( "GEOM_DZ" ) );
-  myGrp->LineEdit3->setReadOnly( TRUE );
-  myGrp->LineEdit4->setReadOnly( TRUE );
-  myGrp->LineEdit5->setReadOnly( TRUE );
-  myGrp->LineEdit6->setReadOnly( TRUE );
-  myGrp->PushButton1->setPixmap( image1 );
-  myGrp->PushButton2->setPixmap( image1 );
-  myGrp->LineEdit1->setReadOnly( true );
-  myGrp->LineEdit2->setReadOnly( true );
-
-  Layout1->addWidget( myGrp, 1, 0 );
-
+  mainFrame()->GroupConstructors->setTitle(tr("GEOM_DISTANCE"));
+  mainFrame()->RadioButton1->setIcon(image0);
+
+  myGrp = new MeasureGUI_2Sel4LineEdit(centralWidget());
+  myGrp->GroupBox1->setTitle(tr("GEOM_MINDIST_OBJ"));
+  myGrp->TextLabel1->setText(tr("GEOM_OBJECT_I").arg("1"));
+  myGrp->TextLabel2->setText(tr("GEOM_OBJECT_I").arg("2"));
+  myGrp->TextLabel3->setText(tr("GEOM_LENGTH"));
+  myGrp->TextLabel4->setText(tr("GEOM_DX"));
+  myGrp->TextLabel5->setText(tr("GEOM_DY"));
+  myGrp->TextLabel6->setText(tr("GEOM_DZ"));
+  myGrp->LineEdit3->setReadOnly(true);
+  myGrp->PushButton1->setIcon(image1);
+  myGrp->PushButton2->setIcon(image1);
+  myGrp->LineEdit1->setReadOnly(true);
+  myGrp->LineEdit2->setReadOnly(true);
+  myGrp->LineEdit4->setReadOnly(true);
+  myGrp->LineEdit5->setReadOnly(true);
+  myGrp->LineEdit6->setReadOnly(true);
+
+  QVBoxLayout* layout = new QVBoxLayout(centralWidget());
+  layout->setMargin(0); layout->setSpacing(6);
+  layout->addWidget(myGrp);
   /***************************************************************/
 
   myHelpFileName = "using_measurement_tools_page.html#min_distance_anchor";
 
-  /* Initialisation */
+  // Initialisation
   Init();
 }
 
-
 //=================================================================================
 // function : ~MeasureGUI_DistanceDlg()
 // purpose  : Destroys the object and frees any allocated resources
@@ -114,44 +105,64 @@ MeasureGUI_DistanceDlg::~MeasureGUI_DistanceDlg()
 {
 }
 
-
 //=================================================================================
 // function : Init()
 // purpose  :
 //=================================================================================
 void MeasureGUI_DistanceDlg::Init()
 {
+  // init variables
+  myGrp->LineEdit1->setText("");
+  myGrp->LineEdit2->setText("");
+  myObj = myObj2 = GEOM::GEOM_Object::_nil();
+
   mySelBtn   = myGrp->PushButton1;
   mySelEdit  = myGrp->LineEdit1;
-  mySelBtn2  = myGrp->PushButton2;
-  mySelEdit2 = myGrp->LineEdit2;
 
-  myEditCurrentArgument = mySelEdit;
+  myEditCurrentArgument = myGrp->LineEdit1;
 
-  connect( mySelEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
-  connect( mySelBtn2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
+  // signals and slots connections
+  connect(myGrp->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
+  connect(myGrp->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
 
   MeasureGUI_Skeleton::Init();
 }
 
-
 //=================================================================================
 // function : SelectionIntoArgument()
 // purpose  : Called when selection has changed
 //=================================================================================
 void MeasureGUI_DistanceDlg::SelectionIntoArgument()
 {
-  Standard_Boolean testResult = Standard_False;
-  GEOM::GEOM_Object_var aSelectedObject =
-    GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
+  LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
+  SALOME_ListIO aSelList;
+  aSelMgr->selectedObjects(aSelList);
+
+  GEOM::GEOM_Object_var aSelectedObject = GEOM::GEOM_Object::_nil();
 
-  if ( !testResult )
-    aSelectedObject = GEOM::GEOM_Object::_nil();
+  if (aSelList.Extent() > 0) {
+    Standard_Boolean testResult = Standard_False;
+    aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
+    if (!testResult)
+      aSelectedObject = GEOM::GEOM_Object::_nil();
+  }
+
+  // clear selection
+  disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
+  myGeomGUI->getApp()->selectionMgr()->clearSelected();
+  connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
+          this, SLOT(SelectionIntoArgument()));
 
-  if ( myEditCurrentArgument == mySelEdit )
-      myObj = aSelectedObject;
-    else
-      myObj2 = aSelectedObject;
+  if (myEditCurrentArgument == myGrp->LineEdit1) {
+    myObj = aSelectedObject;
+    if (!myObj->_is_nil() && myObj2->_is_nil())
+      myGrp->PushButton2->click();
+  }
+  else {
+    myObj2 = aSelectedObject;
+    if (!myObj2->_is_nil() && myObj->_is_nil())
+      myGrp->PushButton1->click();
+  }
 
   processObject();
 }
@@ -162,28 +173,26 @@ void MeasureGUI_DistanceDlg::SelectionIntoArgument()
 //=================================================================================
 void MeasureGUI_DistanceDlg::processObject()
 {
-  myGrp->LineEdit1->setText( !myObj->_is_nil()  ? GEOMBase::GetName( myObj  ) : "" );
-  myGrp->LineEdit2->setText( !myObj2->_is_nil() ? GEOMBase::GetName( myObj2 ) : "" );
+  myGrp->LineEdit1->setText(!myObj->_is_nil()  ? GEOMBase::GetName(myObj ) : "");
+  myGrp->LineEdit2->setText(!myObj2->_is_nil() ? GEOMBase::GetName(myObj2) : "");
 
   gp_Pnt aPnt1, aPnt2;
   double aDist = 0.;
-  if (getParameters(aDist, aPnt1, aPnt2))
-  {
-    myGrp->LineEdit3->setText( DlgRef_SpinBox::PrintDoubleValue( aDist ) );
+  if (getParameters(aDist, aPnt1, aPnt2)) {
+    myGrp->LineEdit3->setText(DlgRef::PrintDoubleValue(aDist));
 
     gp_XYZ aVec = aPnt2.XYZ() - aPnt1.XYZ();
-    myGrp->LineEdit4->setText( DlgRef_SpinBox::PrintDoubleValue( aVec.X() ) );
-    myGrp->LineEdit5->setText( DlgRef_SpinBox::PrintDoubleValue( aVec.Y() ) );
-    myGrp->LineEdit6->setText( DlgRef_SpinBox::PrintDoubleValue( aVec.Z() ) );
+    myGrp->LineEdit4->setText(DlgRef::PrintDoubleValue(aVec.X()));
+    myGrp->LineEdit5->setText(DlgRef::PrintDoubleValue(aVec.Y()));
+    myGrp->LineEdit6->setText(DlgRef::PrintDoubleValue(aVec.Z()));
 
     redisplayPreview();
   }
-  else
-  {
-    myGrp->LineEdit3->setText( "" );
-    myGrp->LineEdit4->setText( "" );
-    myGrp->LineEdit5->setText( "" );
-    myGrp->LineEdit6->setText( "" );
+  else {
+    myGrp->LineEdit3->setText("");
+    myGrp->LineEdit4->setText("");
+    myGrp->LineEdit5->setText("");
+    myGrp->LineEdit6->setText("");
     erasePreview();
   }
 }
@@ -192,34 +201,30 @@ void MeasureGUI_DistanceDlg::processObject()
 // function : getParameters()
 // purpose  : Get distance between objects
 //=================================================================================
-bool MeasureGUI_DistanceDlg::getParametersdouble& theDistance,
+bool MeasureGUI_DistanceDlg::getParameters (double& theDistance,
                                             gp_Pnt& thePnt1,
-                                            gp_Pnt& thePnt2 )
+                                            gp_Pnt& thePnt2)
 {
   QString msg;
-  if ( !isValid( msg ) )
-    return false;
-  else
-  {
-    try
-    {
+  if (isValid(msg)) {
+    try {
       double x1, y1, z1, x2, y2, z2;
-      theDistance = GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetMinDistance(
-        myObj, myObj2, x1, y1, z1, x2, y2, z2 );
+      theDistance = GEOM::GEOM_IMeasureOperations::_narrow(getOperation())->
+        GetMinDistance(myObj, myObj2, x1, y1, z1, x2, y2, z2);
 
-      thePnt1.SetCoord( x1, y1, z1 );
-      thePnt2.SetCoord( x2, y2, z2 );
+      thePnt1.SetCoord(x1, y1, z1);
+      thePnt2.SetCoord(x2, y2, z2);
     }
-    catch( const SALOME::SALOME_Exception& e )
-    {
-      SalomeApp_Tools::QtCatchCorbaException( e );
+    catch(const SALOME::SALOME_Exception& e) {
+      SalomeApp_Tools::QtCatchCorbaException(e);
       return false;
     }
 
     return getOperation()->IsDone();
   }
-}
 
+  return false;
+}
 
 //=================================================================================
 // function : SetEditCurrentArgument()
@@ -227,22 +232,30 @@ bool MeasureGUI_DistanceDlg::getParameters( double& theDistance,
 //=================================================================================
 void MeasureGUI_DistanceDlg::SetEditCurrentArgument()
 {
-  QPushButton* send = ( QPushButton* )sender();
+  QPushButton* send = (QPushButton*)sender();
 
-  if( send == mySelBtn )
-  {
-    mySelEdit->setFocus();
-    myEditCurrentArgument = mySelEdit;
+  if (send == myGrp->PushButton1) {
+    myEditCurrentArgument = myGrp->LineEdit1;
+
+    myGrp->PushButton2->setDown(false);
+    myGrp->LineEdit2->setEnabled(false);
   }
-  else
-  {
-    mySelEdit2->setFocus();
-    myEditCurrentArgument = mySelEdit2;
+  else {
+    myEditCurrentArgument = myGrp->LineEdit2;
+
+    myGrp->PushButton1->setDown(false);
+    myGrp->LineEdit1->setEnabled(false);
   }
 
-  SelectionIntoArgument();
-}
+  // enable line edit
+  myEditCurrentArgument->setEnabled(true);
+  myEditCurrentArgument->setFocus();
+  // after setFocus(), because it will be setDown(false) when loses focus
+  send->setDown(true);
 
+  // seems we need it only to avoid preview disappearing, caused by selection mode change
+  redisplayPreview();
+}
 
 //=================================================================================
 // function : LineEditReturnPressed()
@@ -250,17 +263,19 @@ void MeasureGUI_DistanceDlg::SetEditCurrentArgument()
 //=================================================================================
 void MeasureGUI_DistanceDlg::LineEditReturnPressed()
 {
-  QLineEdit* send = ( QLineEdit* )sender();
+  QLineEdit* send = (QLineEdit*)sender();
 
-  if( send == mySelEdit )
-    myEditCurrentArgument = mySelEdit;
-  else
-    myEditCurrentArgument = mySelEdit2;
+  if (send == myGrp->LineEdit1 || send == myGrp->LineEdit2) {
+    myEditCurrentArgument = send;
 
-  if ( GEOMBase::SelectionByNameInDialogs( this, mySelEdit->text(), selectedIO() ) )
-    mySelEdit->setText( mySelEdit->text() );
-}
+    LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
+    SALOME_ListIO aSelList;
+    aSelMgr->selectedObjects(aSelList);
 
+    if (GEOMBase::SelectionByNameInDialogs(this, myGrp->LineEdit1->text(), aSelList))
+      myGrp->LineEdit1->setText(myGrp->LineEdit1->text());
+  }
+}
 
 //=================================================================================
 // function : buildPrs()
@@ -269,61 +284,57 @@ void MeasureGUI_DistanceDlg::LineEditReturnPressed()
 SALOME_Prs* MeasureGUI_DistanceDlg::buildPrs()
 {
   double aDist = 0.;
-  gp_Pnt aPnt1( 0, 0, 0 ), aPnt2( 0, 0, 0 );
+  gp_Pnt aPnt1(0, 0, 0), aPnt2(0, 0, 0);
 
   SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
 
-  if ( myObj->_is_nil() || myObj2->_is_nil() ||
-       !getParameters( aDist, aPnt1, aPnt2 ) ||
-       vw->getViewManager()->getType() != OCCViewer_Viewer::Type() )
+  if (myObj->_is_nil() || myObj2->_is_nil() ||
+       !getParameters(aDist, aPnt1, aPnt2) ||
+       vw->getViewManager()->getType() != OCCViewer_Viewer::Type())
     return 0;
 
   try
   {
-    if( aDist <= 1.e-9 )
-    {
-      BRepBuilderAPI_MakeVertex aMaker( aPnt1 );
-      return getDisplayer()->BuildPrs( aMaker.Vertex() );
+    if (aDist <= 1.e-9) {
+      BRepBuilderAPI_MakeVertex aMaker(aPnt1);
+      return getDisplayer()->BuildPrs(aMaker.Vertex());
     }
-    else
-    {
-      BRepBuilderAPI_MakeEdge MakeEdge( aPnt1, aPnt2 );
-      TopoDS_Vertex aVert1 = BRepBuilderAPI_MakeVertex( aPnt1 );
-      TopoDS_Vertex aVert2 = BRepBuilderAPI_MakeVertex( aPnt2 );
+    else {
+      BRepBuilderAPI_MakeEdge MakeEdge(aPnt1, aPnt2);
+      TopoDS_Vertex aVert1 = BRepBuilderAPI_MakeVertex(aPnt1);
+      TopoDS_Vertex aVert2 = BRepBuilderAPI_MakeVertex(aPnt2);
 
       QString aLabel;
-      aLabel.sprintf( "%.1f", aDist );
+      aLabel.sprintf("%.1f", aDist);
 
-      gp_Pnt aPnt3( ( aPnt1.X() + aPnt2.X() ) / 2,
-                    ( aPnt1.Y() + aPnt2.Y() ) / 2,
-                    ( aPnt1.Z() + aPnt2.Z() ) / 2 + 100 );
+      gp_Pnt aPnt3((aPnt1.X() + aPnt2.X()) / 2,
+                    (aPnt1.Y() + aPnt2.Y()) / 2,
+                    (aPnt1.Z() + aPnt2.Z()) / 2);
 
-      gp_Vec va( aPnt3, aPnt1 );
-      gp_Vec vb( aPnt3, aPnt2 );
+      gp_Vec va(aPnt3, aPnt1);
+      gp_Vec vb(aPnt3, aPnt2);
 
-      if ( va.IsParallel( vb, Precision::Angular() ) )
-      {
-        aPnt3.SetY( ( aPnt1.Y() + aPnt2.Y() ) / 2 + 100 );
-        aPnt3.SetZ( ( aPnt1.Z() + aPnt2.Z() ) / 2 );
+      if (va.IsParallel(vb, Precision::Angular())) {
+        aPnt3.SetY((aPnt1.Y() + aPnt2.Y()) / 2 + 100);
+        aPnt3.SetZ((aPnt1.Z() + aPnt2.Z()) / 2);
       }
 
-      gce_MakePln gce_MP( aPnt1, aPnt2, aPnt3 );
-      Handle( Geom_Plane ) P = new Geom_Plane( gce_MP.Value() );
+      gce_MakePln gce_MP(aPnt1, aPnt2, aPnt3);
+      Handle(Geom_Plane) P = new Geom_Plane(gce_MP.Value());
 
-      Handle( AIS_LengthDimension ) anIO = new AIS_LengthDimension
-        (aVert1, aVert2, P, aDist, TCollection_ExtendedString((Standard_CString)aLabel.latin1()));
+      Handle(AIS_LengthDimension) anIO = new AIS_LengthDimension(
+        aVert1, aVert2, P, aDist, TCollection_ExtendedString((Standard_CString)aLabel.toLatin1().constData()));
+      anIO->SetArrowSize(aDist/20);
 
-      SOCC_Prs* aPrs =
-        dynamic_cast<SOCC_Prs*>( ((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs( 0 ) );
+      SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
 
-      if ( aPrs )
-        aPrs->AddObject( anIO );
+      if (aPrs)
+        aPrs->AddObject(anIO);
 
       return aPrs;
     }
   }
-  catch( Standard_Failure )
-  {
+  catch(Standard_Failure) {
     return 0;
   }
 }
@@ -332,7 +343,7 @@ SALOME_Prs* MeasureGUI_DistanceDlg::buildPrs()
 // function : isValid()
 // purpose  :
 //=================================================================================
-bool MeasureGUI_DistanceDlg::isValid( QString& msg )
+bool MeasureGUI_DistanceDlg::isValid (QString& msg)
 {
-  return MeasureGUI_Skeleton::isValid( msg ) && !myObj2->_is_nil();
+  return MeasureGUI_Skeleton::isValid(msg) && !myObj2->_is_nil();
 }