Salome HOME
0021209: Bug GetInPlace with faces. Integrate self-intersections checker.
[modules/geom.git] / src / EntityGUI / EntityGUI_SketcherDlg.cxx
index 92e9edfc23e7e07b1e34176bb454fb215015ffbc..e3ec23f224823ad4dc1eac56f1284259edb9cdc7 100644 (file)
@@ -28,6 +28,9 @@
 #include "EntityGUI_Widgets.h"
 #include <SalomeApp_DoubleSpinBox.h>
 
+#include <OCCViewer_ViewPort3d.h>
+#include <OCCViewer_ViewWindow.h>
+
 #include <GEOMBase.h>
 #include <GeometryGUI.h>
 #include <GEOMImpl_Types.hxx>
@@ -51,6 +54,7 @@
 #include <TopoDS.hxx>
 #include <BRepBuilderAPI_Transform.hxx>
 #include <BRepBuilderAPI_MakeWire.hxx>
+#include <BRepBuilderAPI_MakeVertex.hxx>
 #include <Sketcher_Profile.hxx>
 
 #include <SalomeApp_Study.h>
@@ -76,6 +80,7 @@ EntityGUI_SketcherDlg::EntityGUI_SketcherDlg( GeometryGUI* GUI, QWidget* parent,
     myGeometryGUI( GUI ),
     myLineWidth( lineWidth )
 {
+  SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
   setModal( modal );
   setAttribute( Qt::WA_DeleteOnClose );
 
@@ -86,19 +91,23 @@ EntityGUI_SketcherDlg::EntityGUI_SketcherDlg( GeometryGUI* GUI, QWidget* parent,
   topLayout->setMargin( 9 ); topLayout->setSpacing( 6 );
 
   MainWidget->buttonCancel->setText( tr( "GEOM_BUT_CANCEL" ) );
-  MainWidget->buttonEnd->setText( tr( "GEOM_BUT_END_SKETCH" ) );
+  MainWidget->buttonEnd->setText( tr( "GEOM_BUT_CLOSE" ) );
   MainWidget->buttonClose->setText( tr( "GEOM_BUT_CLOSE_SKETCH" ) );
   MainWidget->buttonHelp->setText( tr( "GEOM_BUT_HELP" ) );
 
-  QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
-  QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_UNDO" ) ) );
-  QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_REDO" ) ) );
+  QPixmap image0( aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT"      ) ) );
+  QPixmap image1( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_UNDO"    ) ) );
+  QPixmap image2( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_REDO"    ) ) );
+  QPixmap image3( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_LINE_2P" ) ) );
+  QPixmap image4( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_ARC"     ) ) );
 
   setWindowTitle( tr( "GEOM_SKETCHER_TITLE" ) );
 
   MainWidget->GroupConstructors->setTitle( tr( "GEOM_SKETCHER_EL" ) );
-  MainWidget->RadioButton1->setText( tr( "GEOM_SKETCHER_SEGMENT" ) );
-  MainWidget->RadioButton2->setText( tr( "GEOM_SKETCHER_ARC" ) );
+  MainWidget->RadioButton1->setText( "" );
+  MainWidget->RadioButton1->setIcon( image3 );
+  MainWidget->RadioButton2->setText( "" );
+  MainWidget->RadioButton2->setIcon( image4 );
   MainWidget->GroupDest->setTitle( tr( "GEOM_SKETCHER_DEST" ) );
   MainWidget->GroupDest1->setTitle( tr( "GEOM_SKETCHER_TYPE" ) );
   MainWidget->RB_Dest1->setText( tr( "GEOM_SKETCHER_POINT" ) );
@@ -148,7 +157,7 @@ EntityGUI_SketcherDlg::EntityGUI_SketcherDlg( GeometryGUI* GUI, QWidget* parent,
 
   GroupD2 = new EntityGUI_Dir2( MainWidget->DestCnt );
 
-  GroupD2->GroupDir2->setTitle( tr( "GEOM_SKETCHER_DIR" ) );
+  GroupD2->GroupDir2->setTitle( tr( "GEOM_SKETCHER_DIST" ) );
   GroupD2->RB_Dir21->setText( tr( "GEOM_SKETCHER_LENGTH" ) );
   GroupD2->RB_Dir22->setText( tr( "GEOM_SKETCHER_X" ) );
   GroupD2->RB_Dir23->setText( tr( "GEOM_SKETCHER_Y" ) );
@@ -345,27 +354,27 @@ bool EntityGUI_SketcherDlg::eventFilter (QObject* object, QEvent* event)
 {
   if (event->type() == QEvent::KeyPress) {
     QKeyEvent* ke = (QKeyEvent*)event;
-    if (ke->key() == Qt::Key_Return) {
+    if ( ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter ) {
       if (object == Group1Spin->SpinBox_DX) {
-        Group1Spin->buttonApply->animateClick();
+        Group1Spin->buttonApply->click();
         return true;
       } else if (object == Group1Sel1Spin->SpinBox_DX) {
-        Group1Sel1Spin->buttonApply->animateClick();
+        Group1Sel1Spin->buttonApply->click();
         return true;
       } else if (object == Group2Spin->SpinBox_DX ||
                  object == Group2Spin->SpinBox_DY) {
-        Group2Spin->buttonApply->animateClick();
+        Group2Spin->buttonApply->click();
         return true;
       } else if (object == Group3Spin->SpinBox_DX ||
                  object == Group3Spin->SpinBox_DY ||
                  object == Group3Spin->SpinBox_DZ) {
-        Group3Spin->buttonApply->animateClick();
+        Group3Spin->buttonApply->click();
         return true;
       } else if (object == Group4Spin->SpinBox_DX ||
                  object == Group4Spin->SpinBox_DY ||
                  object == Group4Spin->SpinBox_DZ ||
                  object == Group4Spin->SpinBox_DS) {
-        Group4Spin->buttonApply->animateClick();
+        Group4Spin->buttonApply->click();
         return true;
       }
     }
@@ -384,6 +393,7 @@ bool EntityGUI_SketcherDlg::eventFilter (QObject* object, QEvent* event)
 void EntityGUI_SketcherDlg::Init()
 {
   /* init variables */
+  autoApply = false;
   myEditCurrentArgument = Group1Sel->LineEdit1;
   myCommand.append( "Sketcher" );
   myUndoCommand.append( "Sketcher" );
@@ -451,6 +461,8 @@ void EntityGUI_SketcherDlg::InitClick()
   Group2Spin->hide();
   Group3Spin->hide();
   Group4Spin->hide();
+  globalSelection(); // close local selection to clear it
+  localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
 }
 
 
@@ -966,7 +978,7 @@ void EntityGUI_SketcherDlg::ClickOnEnd()
       myCommand.append( ":WW" );
   }
   else {
-    // PAL16008 (Sketcher Validation should be equal to Apply&Close)
+    /*// PAL16008 (Sketcher Validation should be equal to Apply&Close)
     if ( ( Group1Spin->buttonApply->isEnabled() && Group1Spin->isVisible() ) ||
          ( Group2Spin->buttonApply->isEnabled() && Group2Spin->isVisible() ) ||
          ( Group3Spin->buttonApply->isEnabled() && Group3Spin->isVisible() ) ||
@@ -975,7 +987,7 @@ void EntityGUI_SketcherDlg::ClickOnEnd()
          ( Group2Sel->buttonApply->isEnabled()  && Group2Sel->isVisible()  ) ||
          ( Group1Sel1Spin->buttonApply->isEnabled() && Group1Sel1Spin->isVisible() ) )  {     
       ClickOnApply();
-    }
+    }*/
     myIsAllAdded = true;
   }
 
@@ -1035,7 +1047,7 @@ bool EntityGUI_SketcherDlg::ClickOnApply()
     (Group4Spin->SpinBox_DX)->setFocus();
     (Group4Spin->SpinBox_DX)->selectAll();
   }
-
+  
   return true;
 }
 
@@ -1173,10 +1185,6 @@ void EntityGUI_SketcherDlg::SelectionIntoArgument()
 
   int nbSel = aSelList.Extent();
   MESSAGE("NbSel = "<<nbSel)
-  if (nbSel == 0){
-    myX=tmpX;
-    myY=tmpY;
-  }
   if (nbSel == 1 && myEditCurrentArgument == Group1Sel->LineEdit1) {
     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
     if ( !CORBA::is_nil(aSelectedObject) ) {
@@ -1352,10 +1360,19 @@ void EntityGUI_SketcherDlg::SelectionIntoArgument()
       }
     }
   }
-
-
-
-  GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
+  if (nbSel == 0){                 // If no object selected
+    myX=tmpX;                      // Don't change the point coordinates
+    myY=tmpY;                      // and don't redisplay the preview
+  }
+  else if(!autoApply){
+    GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
+  }
+  if ( autoApply ){    
+    ClickOnApply();
+    autoApply = false;
+  }
+  this->activateWindow();
+  
 }
 
 
@@ -1444,6 +1461,7 @@ void EntityGUI_SketcherDlg::DeactivateActiveDialog()
 //=================================================================================
 void EntityGUI_SketcherDlg::ActivateThisDialog()
 {
+  MESSAGE("EntityGUI_SketcherDlg::ActivateThisDialog()")
   myGeometryGUI->EmitSignalDeactivateDialog();
   setEnabled( true );
   myGeometryGUI->SetActiveDialogBox( this );
@@ -1473,7 +1491,7 @@ void EntityGUI_SketcherDlg::ActivateThisDialog()
      myEditCurrentArgument = Group2Sel->LineEdit2;
      myEditCurrentArgument->setFocus();
    }
-
+   
   GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
 }
 
@@ -1502,7 +1520,6 @@ void EntityGUI_SketcherDlg::closeEvent( QCloseEvent* e )
   QDialog::closeEvent( e );
 }
 
-//rnc TODO
 //=================================================================================
 // function : OnPointSelected
 // purpose  :
@@ -1510,24 +1527,60 @@ void EntityGUI_SketcherDlg::closeEvent( QCloseEvent* e )
 void EntityGUI_SketcherDlg::OnPointSelected(Qt::KeyboardModifiers modifiers, const gp_Pnt& thePnt)
 {
   MESSAGE("EntityGUI_SketcherDlg::OnPointSelected")
+  
+  SUIT_ViewWindow*       theViewWindow  = getDesktop()->activeWindow();
+  OCCViewer_ViewPort3d*  vp             = ((OCCViewer_ViewWindow*)theViewWindow)->getViewPort();
+  QString                theImgFileName = vp->backgroundImageFilename();
+  
+  double x, y;
+  x = y = 0;
+  
+  BRepBuilderAPI_MakeVertex mkVertex (thePnt);
+  TopoDS_Shape aShape = mkVertex.Shape();
+  
+  // Taking into account LocalCS
+  gp_Ax3 aWPlane = GetActiveLocalCS();
+  gp_Trsf aTrans;
+
+  aTrans.SetTransformation(aWPlane);
+  BRepBuilderAPI_Transform aTransformation (aShape, aTrans, Standard_False);
+  aShape = aTransformation.Shape();
+  
+  gp_Pnt aTrsfPnt; 
+  GEOMBase::VertexToPoint( aShape, aTrsfPnt );
+  
+  autoApply = ( (getPnt2ConstructorId() == 1) && (!theImgFileName.isEmpty()) );  // If no additional argument needed after selection and there is a backgroundimage
+                                                                                 // -> apply automatically 
+  
+  if ( getPnt1ConstructorId() == 0 ){                    // Relative selection mode
+    x = aTrsfPnt.X() - myLastX1;
+    y = aTrsfPnt.Y() - myLastY1;
+  }
+  else {
+    x = aTrsfPnt.X();
+    y = aTrsfPnt.Y();
+  }
   switch (getPnt2ConstructorId()){
     case 1:
-      Group2Spin->SpinBox_DX->setValue( thePnt.X() );
-      Group2Spin->SpinBox_DY->setValue( thePnt.Y() ); 
+      Group2Spin->SpinBox_DX->setValue( x );
+      Group2Spin->SpinBox_DY->setValue( y );
+      Group2Spin->buttonApply->setFocus();               // Previous setFocus (during preview) may have been inoperative if it was disabled 
       break;
     case 0:
-      Group3Spin->SpinBox_DX->setValue( thePnt.X() );
-      Group3Spin->SpinBox_DY->setValue( thePnt.Y() );
+      Group3Spin->SpinBox_DX->setValue( x );
+      Group3Spin->SpinBox_DY->setValue( y );
+      Group3Spin->buttonApply->setFocus(); 
       break;
     case 2:
       if (modifiers == Qt::MetaModifier){                // Select center with Meta key
-        Group4Spin->SpinBox_DX->setValue( thePnt.X() );
-        Group4Spin->SpinBox_DY->setValue( thePnt.Y() );
+        Group4Spin->SpinBox_DX->setValue( x );
+        Group4Spin->SpinBox_DY->setValue( y );
       }
-      else{                                              // The select end point
-        Group4Spin->SpinBox_DZ->setValue( thePnt.X() );
-        Group4Spin->SpinBox_DS->setValue( thePnt.Y() );
+      else{                                              // Select end point
+        Group4Spin->SpinBox_DZ->setValue( x );
+        Group4Spin->SpinBox_DS->setValue( y );
       }
+      Group4Spin->buttonApply->setFocus(); 
       break;
   }
 }
@@ -1818,8 +1871,29 @@ void EntityGUI_SketcherDlg::ValueChangedInSpinBox( double newValue )
       myLengthStr = vsStr;
     }
   }
-
-  GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
+  
+  if (!autoApply) 
+    GEOMBase_Helper::displayPreview( true, false, true, true, myLineWidth );
+  
+  double x, y, xc, yc;
+  x = y = xc = yc = 0.0;
+  if ( mySketchType == PT_ABS_CENTER || mySketchType == PT_REL_CENTER )
+  {
+    if ( mySketchType == PT_REL_CENTER ){
+      x  = myDX  + myLastX1;
+      y  = myDY  + myLastY1;
+      xc = myDXc + myLastX1;
+      yc = myDYc + myLastY1;
+    }
+    else {
+      x  = myX ;
+      y  = myY ;
+      xc = myXc;
+      yc = myYc;
+    }
+    displayPntPreview(xc,yc);
+    displayPntPreview(x,y);
+  }
 }
 
 
@@ -2025,7 +2099,7 @@ bool EntityGUI_SketcherDlg::execute( ObjectList& objects )
     Format = 'g';                                  // g --> DigNum is the maximum number of significant digits 
     
   QString aParameters;
-
+  
   if ( mySketchState == FIRST_POINT ) {
     myLastX2 = myX;
     myLastY2 = myY;
@@ -2139,31 +2213,24 @@ bool EntityGUI_SketcherDlg::execute( ObjectList& objects )
 
     if ( Group1Sel->isVisible() ) {
       Group1Sel->buttonApply->setEnabled( true );
-      //Group1Sel->buttonApply->setFocus();
     }
     if ( Group2Sel->isVisible() ) {
       Group2Sel->buttonApply->setEnabled( true );
-      //Group2Sel->buttonApply->setFocus();
     }
     if ( Group1Sel1Spin->isVisible() ) {
-      Group1Sel1Spin->buttonApply->setEnabled( true );
-      //Group1Sel1Spin->buttonApply->setFocus();
+      Group1Sel1Spin->buttonApply->setEnabled( true );;
     }
     if ( Group1Spin->isVisible() ) {
       Group1Spin->buttonApply->setEnabled( true );
-      //Group1Spin->buttonApply->setFocus();
     }
     if ( Group2Spin->isVisible() ) {
       Group2Spin->buttonApply->setEnabled( true );
-      //Group2Spin->buttonApply->setFocus();
     }
     if ( Group3Spin->isVisible() ) {
       Group3Spin->buttonApply->setEnabled( true );
-      //Group3Spin->buttonApply->setFocus();
     }
     if ( Group4Spin->isVisible() ) {
       Group4Spin->buttonApply->setEnabled( true );
-      //Group4Spin->buttonApply->setFocus();
     }
   }
 
@@ -2246,6 +2313,41 @@ void EntityGUI_SketcherDlg::displayPreview( GEOM::GEOM_Object_ptr object,
   getDisplayer()->SetToActivate( true );
 }
 
+//================================================================
+// Function : displayPntPreview
+// Purpose  : creates a TopoDS_VERTEX and display a preview of it
+//================================================================
+void EntityGUI_SketcherDlg::displayPntPreview(const double x,
+                                              const double y,
+                                              bool append,
+                                              bool update
+                                             )
+{
+  // Get globalCS and working plane
+  gp_Ax3 globalCS = myLCSList.first(); //gp_Ax3(aOrigin, aDirZ, aDirX);
+  gp_Ax3 aWPlane  = GetActiveLocalCS();
+  
+  // Build point in localCS
+  gp_Pnt aPnt = gp_Pnt(x,y,0.0);
+  
+  // Get transfomation from local to global CS
+  gp_Trsf aTrans;
+  aTrans.SetTransformation(aWPlane, globalCS);
+  
+  BRepBuilderAPI_MakeVertex mkVertex (aPnt);
+  TopoDS_Shape aLocalVertex = mkVertex.Shape();
+  
+  // Perform transformation
+  BRepBuilderAPI_Transform aTransformation (aLocalVertex, aTrans, Standard_False);
+  TopoDS_Shape aGlobalVertex = aTransformation.Shape();
+  
+  // Build prs with vertex in globalCS
+  SALOME_Prs* aPrs = getDisplayer()->BuildPrs( aGlobalVertex );
+  if ( aPrs != 0 && !aPrs->IsNull() )
+    GEOMBase_Helper::displayPreview( aPrs, append, update );
+  
+}
+
 //================================================================
 // Function : createShapes
 // Purpose  : Create applyed wire, and last segment from entry object
@@ -2419,7 +2521,6 @@ void EntityGUI_SketcherDlg::FindLocalCS()
   }
 }
 
-//rnc TODO
 //=================================================================================
 // function : getPnt1ConstructorId()
 // purpose  :
@@ -2427,7 +2528,7 @@ void EntityGUI_SketcherDlg::FindLocalCS()
 int EntityGUI_SketcherDlg::getPnt1ConstructorId() const
 { 
   int buttonId = GroupPt->ButtonGroup->checkedId(); 
-  MESSAGE("buttonId = "<<buttonId)
+//   MESSAGE("buttonId = "<<buttonId)
   return buttonId;
 }
 
@@ -2438,7 +2539,7 @@ int EntityGUI_SketcherDlg::getPnt1ConstructorId() const
 int EntityGUI_SketcherDlg::getPnt2ConstructorId() const
 { 
   int buttonId = GroupPt2->ButtonGroup->checkedId(); 
-  MESSAGE("buttonId = "<<buttonId)
+//   MESSAGE("buttonId = "<<buttonId)
   return buttonId;
 }