Salome HOME
IPAL19834: Incorrect result after make Partition of 2 objects. A fix by PKV.
[modules/geom.git] / src / BasicGUI / BasicGUI_ArcDlg.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : BasicGUI_ArcDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "BasicGUI_ArcDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_Session.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <TColStd_IndexedMapOfInteger.hxx>
38 #include <TopTools_IndexedMapOfShape.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS.hxx>
42 #include <TopExp.hxx>
43
44 #include <GEOMImpl_Types.hxx>
45
46 //=================================================================================
47 // class    : BasicGUI_ArcDlg()
48 // purpose  : Constructs a BasicGUI_ArcDlg which is a child of 'parent', with the
49 //            name 'name' and widget flags set to 'f'.
50 //            The dialog will by default be modeless, unless you set 'modal' to
51 //            TRUE to construct a modal dialog.
52 //=================================================================================
53 BasicGUI_ArcDlg::BasicGUI_ArcDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
54                                   bool modal, Qt::WindowFlags fl )
55   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
56 {
57   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
58   QPixmap image0( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_ARC" ) ) );
59   QPixmap image1( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_ARC_CENTER" ) ) );
60   QPixmap image2( aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
61
62   setWindowTitle( tr( "GEOM_ARC_TITLE" ) );
63
64   /***************************************************************/
65   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_ARC" ) );
66   mainFrame()->RadioButton1->setIcon( image0 );
67   mainFrame()->RadioButton2->setIcon( image1 );
68   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
69   mainFrame()->RadioButton3->close();
70
71   Group3Pnts = new DlgRef_3Sel( centralWidget() );
72
73   Group3Pnts->GroupBox1->setTitle( tr( "GEOM_POINTS" ) );
74   Group3Pnts->TextLabel1->setText( tr( "GEOM_POINT_I" ).arg( 1 ) );
75   Group3Pnts->TextLabel2->setText( tr( "GEOM_POINT_I" ).arg( 2 ) );
76   Group3Pnts->TextLabel3->setText( tr( "GEOM_POINT_I" ).arg( 3 ) );
77
78   Group3Pnts->LineEdit1->setReadOnly( true );
79   Group3Pnts->LineEdit2->setReadOnly( true );
80   Group3Pnts->LineEdit3->setReadOnly( true );
81
82   Group3Pnts->PushButton1->setIcon(image2);
83   Group3Pnts->PushButton2->setIcon(image2);
84   Group3Pnts->PushButton3->setIcon(image2);
85
86
87   Group3Pnts2 = new DlgRef_3Sel1Check( centralWidget() );
88
89   Group3Pnts2->GroupBox1->setTitle( tr( "GEOM_POINTS" ) );
90   Group3Pnts2->TextLabel1->setText( tr( "GEOM_CENTER_POINT" ) );
91   Group3Pnts2->TextLabel2->setText( tr( "GEOM_POINT_I" ).arg( "Start" ) );
92   Group3Pnts2->TextLabel3->setText( tr( "GEOM_POINT_I" ).arg( "End" ) );
93
94   Group3Pnts2->LineEdit1->setReadOnly( true );
95   Group3Pnts2->LineEdit2->setReadOnly( true );
96   Group3Pnts2->LineEdit3->setReadOnly( true );
97
98   Group3Pnts2->PushButton1->setIcon( image2 );
99   Group3Pnts2->PushButton2->setIcon( image2 );
100   Group3Pnts2->PushButton3->setIcon( image2 );
101   
102   Group3Pnts2->CheckButton1->setText( tr( "GEOM_REVERSE" ) );
103
104   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
105   layout->setMargin( 0 ); layout->setSpacing( 6 );
106   layout->addWidget( Group3Pnts );
107   layout->addWidget( Group3Pnts2 );
108
109   setHelpFileName( "create_arc_page.html" );
110
111   Init();
112 }
113
114
115 //=================================================================================
116 // function : ~BasicGUI_ArcDlg()
117 // purpose  : Destroys the object and frees any allocated resources
118 //=================================================================================
119 BasicGUI_ArcDlg::~BasicGUI_ArcDlg()
120 {
121 }
122
123
124 //=================================================================================
125 // function : Init()
126 // purpose  :
127 //=================================================================================
128 void BasicGUI_ArcDlg::Init()
129 {
130   /* init variables */
131   myEditCurrentArgument = Group3Pnts->LineEdit1;
132
133   myPoint1 = myPoint2 = myPoint3 = GEOM::GEOM_Object::_nil();
134   Group3Pnts2->CheckButton1->setChecked( false );
135
136   /* signals and slots connections */
137   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
138   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
139  
140   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
141  
142   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
143   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
144
145   connect( Group3Pnts->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
146   connect( Group3Pnts->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
147   connect( Group3Pnts->PushButton3, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
148
149   connect( Group3Pnts->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
150   connect( Group3Pnts->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
151   connect( Group3Pnts->LineEdit3, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
152   
153   connect( Group3Pnts2->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
154   connect( Group3Pnts2->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
155   connect( Group3Pnts2->PushButton3, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
156
157   connect( Group3Pnts2->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
158   connect( Group3Pnts2->LineEdit2, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
159   connect( Group3Pnts2->LineEdit3, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
160   
161   connect( Group3Pnts2->CheckButton1, SIGNAL( toggled( bool ) ), this, SLOT( ReverseSense() ) );
162
163   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
164            this, SLOT( SelectionIntoArgument() ) );
165
166   initName( tr( "GEOM_ARC" ) ); 
167   
168
169   ConstructorsClicked( 0 );
170 }
171
172
173 //=================================================================================
174 // function : ClickOnOk()
175 // purpose  :
176 //=================================================================================
177 void BasicGUI_ArcDlg::ClickOnOk()
178 {
179   if ( ClickOnApply() )
180     ClickOnCancel();
181 }
182
183 //=================================================================================
184 // function : ClickOnApply()
185 // purpose  :
186 //=================================================================================
187 bool BasicGUI_ArcDlg::ClickOnApply()
188 {
189   if ( !onAccept() )
190     return false;
191
192   initName();
193
194   return true;
195 }
196
197
198 //=================================================================================
199 // function : SelectionIntoArgument()
200 // purpose  : Called when selection as changed or other case
201 //=================================================================================
202 void BasicGUI_ArcDlg::SelectionIntoArgument()
203 {
204   if ( getConstructorId() != 0 && getConstructorId() != 1 )
205     return;
206   
207   myEditCurrentArgument->setText( "" );
208   if ( IObjectCount() != 1 ) {
209     switch ( getConstructorId() ) {
210     case 0:
211       if      ( myEditCurrentArgument == Group3Pnts->LineEdit1 )   myPoint1 = GEOM::GEOM_Object::_nil();
212       else if ( myEditCurrentArgument == Group3Pnts->LineEdit2 )   myPoint2 = GEOM::GEOM_Object::_nil();
213       else if ( myEditCurrentArgument == Group3Pnts->LineEdit3 )   myPoint3 = GEOM::GEOM_Object::_nil();
214       return;
215       break;
216     case 1:
217       if      ( myEditCurrentArgument == Group3Pnts2->LineEdit1 )   myPoint1 = GEOM::GEOM_Object::_nil();
218       else if ( myEditCurrentArgument == Group3Pnts2->LineEdit2 )   myPoint2 = GEOM::GEOM_Object::_nil();
219       else if ( myEditCurrentArgument == Group3Pnts2->LineEdit3 )   myPoint3 = GEOM::GEOM_Object::_nil();
220       return;
221       break;
222     }
223   }   
224   // nbSel == 1
225   Standard_Boolean aRes = Standard_False;
226   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
227   if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
228     QString aName = GEOMBase::GetName(aSelectedObject);
229
230     // Get Selected object if selected subshape
231     TopoDS_Shape aShape;
232     if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
233     {
234       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
235       TColStd_IndexedMapOfInteger aMap;
236       aSelMgr->GetIndexes(firstIObject(), aMap);
237       if (aMap.Extent() == 1) // Local Selection
238       {
239         int anIndex = aMap(1);
240         aName += QString(":vertex_%1").arg(anIndex);
241
242         //Find SubShape Object in Father
243         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
244
245         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
246           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
247           aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
248         }
249         else
250           aSelectedObject = aFindedObject; // get Object from study
251       }
252       else // Global Selection
253       {
254         if (aShape.ShapeType() != TopAbs_VERTEX) {
255           aSelectedObject = GEOM::GEOM_Object::_nil();
256           aName = "";
257         }
258       }
259     }
260
261     myEditCurrentArgument->setText(aName);
262
263     switch ( getConstructorId() ) {
264     case 0:
265       if      ( myEditCurrentArgument == Group3Pnts->LineEdit1 )   myPoint1 = aSelectedObject;
266       else if ( myEditCurrentArgument == Group3Pnts->LineEdit2 )   myPoint2 = aSelectedObject;
267       else if ( myEditCurrentArgument == Group3Pnts->LineEdit3 )   myPoint3 = aSelectedObject;
268       break;
269     case 1:
270       if      ( myEditCurrentArgument == Group3Pnts2->LineEdit1 )   myPoint1 = aSelectedObject;
271       else if ( myEditCurrentArgument == Group3Pnts2->LineEdit2 )   myPoint2 = aSelectedObject;
272       else if ( myEditCurrentArgument == Group3Pnts2->LineEdit3 )   myPoint3 = aSelectedObject;
273       break;
274     }
275   }
276
277   displayPreview();
278 }
279
280
281 //=================================================================================
282 // function : SetEditCurrentArgument()
283 // purpose  :
284 //=================================================================================
285 void BasicGUI_ArcDlg::SetEditCurrentArgument()
286 {
287   QPushButton* send = (QPushButton*)sender();
288   switch ( getConstructorId() ) {
289   case 0:
290     if      ( send == Group3Pnts->PushButton1 )   myEditCurrentArgument = Group3Pnts->LineEdit1;
291     else if ( send == Group3Pnts->PushButton2 )   myEditCurrentArgument = Group3Pnts->LineEdit2;
292     else if ( send == Group3Pnts->PushButton3 )   myEditCurrentArgument = Group3Pnts->LineEdit3;
293     break;
294   case 1:
295     if      ( send == Group3Pnts2->PushButton1 )   myEditCurrentArgument = Group3Pnts2->LineEdit1;
296     else if ( send == Group3Pnts2->PushButton2 )   myEditCurrentArgument = Group3Pnts2->LineEdit2;
297     else if ( send == Group3Pnts2->PushButton3 )   myEditCurrentArgument = Group3Pnts2->LineEdit3;
298     break;
299   }
300   myEditCurrentArgument->setFocus();
301   SelectionIntoArgument();
302 }
303
304
305 //=================================================================================
306 // function : LineEditReturnPressed()
307 // purpose  :
308 //=================================================================================
309 void BasicGUI_ArcDlg::LineEditReturnPressed()
310 {
311   QLineEdit* send = (QLineEdit*)sender();
312   if ( send == Group3Pnts->LineEdit1 || send == Group3Pnts->LineEdit2 || send == Group3Pnts->LineEdit3 ||
313        send == Group3Pnts2->LineEdit1 || send == Group3Pnts2->LineEdit2 || send == Group3Pnts2->LineEdit3 ) {
314     myEditCurrentArgument = send;
315     GEOMBase_Skeleton::LineEditReturnPressed();
316   }
317 }
318
319
320 //=================================================================================
321 // function : ActivateThisDialog()
322 // purpose  :
323 //=================================================================================
324 void BasicGUI_ArcDlg::ActivateThisDialog()
325 {
326   GEOMBase_Skeleton::ActivateThisDialog();
327
328   connect( myGeomGUI->getApp()->selectionMgr(),
329            SIGNAL( currentSelectionChanged() ),
330            this,
331            SLOT(SelectionIntoArgument() ) );
332
333   ConstructorsClicked( getConstructorId() );
334 }
335
336 //=================================================================================
337 // function : enterEvent()
338 // purpose  :
339 //=================================================================================
340 void BasicGUI_ArcDlg::enterEvent( QEvent* )
341 {
342   if ( !mainFrame()->GroupConstructors->isEnabled() )
343     ActivateThisDialog();
344 }
345
346 //=================================================================================
347 // function : createOperation
348 // purpose  :
349 //=================================================================================
350 GEOM::GEOM_IOperations_ptr BasicGUI_ArcDlg::createOperation()
351 {
352   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
353 }
354
355 //=================================================================================
356 // function : isEqual
357 // purpose  : it may also be needed to check for min distance between gp_Pnt-s...
358 //=================================================================================
359 static bool isEqual( const GEOM::GEOM_Object_var& thePnt1, const GEOM::GEOM_Object_var& thePnt2 )
360 {
361   return thePnt1->_is_equivalent( thePnt2 );
362 }
363
364 //=================================================================================
365 // function : isValid
366 // purpose  :
367 //=================================================================================
368 bool BasicGUI_ArcDlg::isValid( QString& msg )
369 {
370   switch (getConstructorId()) {
371   case 0:
372     {
373       if (Group3Pnts->LineEdit1->text().trimmed().isEmpty() ||
374           Group3Pnts->LineEdit2->text().trimmed().isEmpty() ||
375           Group3Pnts->LineEdit3->text().trimmed().isEmpty())
376         return false;
377       break;
378     }
379   case 1:
380     {
381       if (Group3Pnts2->LineEdit1->text().trimmed().isEmpty() ||
382           Group3Pnts2->LineEdit2->text().trimmed().isEmpty() ||
383           Group3Pnts2->LineEdit3->text().trimmed().isEmpty())
384         return false;
385       break;
386     }
387   }
388   return !myPoint1->_is_nil() && !myPoint2->_is_nil() && !myPoint3->_is_nil() &&
389     !isEqual( myPoint1, myPoint2 ) && !isEqual( myPoint1, myPoint3 ) && !isEqual( myPoint2, myPoint3 );
390 }
391
392 //=================================================================================
393 // function : execute
394 // purpose  :
395 //=================================================================================
396 bool BasicGUI_ArcDlg::execute( ObjectList& objects )
397 {
398   bool res = false;
399   GEOM::GEOM_Object_var anObj;
400
401   switch ( getConstructorId() ) {
402   case 0:
403     {
404       if ( !CORBA::is_nil( myPoint1 ) && !CORBA::is_nil( myPoint2 ) && !CORBA::is_nil( myPoint3 ) ) {
405         anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeArc( myPoint1, myPoint2, myPoint3 );
406         res = true;
407       }
408       break;
409     }
410   case 1:
411     {
412       bool Sense = Group3Pnts2->CheckButton1->isChecked();
413       if ( !CORBA::is_nil( myPoint1 ) && !CORBA::is_nil( myPoint2 ) && !CORBA::is_nil( myPoint3 ) ) {
414         anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeArcCenter( myPoint1, myPoint2, myPoint3, Sense );
415         res = true;
416       }
417       break;
418     }
419   }
420   if ( !anObj->_is_nil() ) {
421     objects.push_back( anObj._retn() );
422   }
423   return res;
424 }
425
426 //=================================================================================
427 // function : ConstructorsClicked()
428 // purpose  : Radio button management
429 //=================================================================================
430 void BasicGUI_ArcDlg::ConstructorsClicked( int constructorId )
431 {
432   disconnect( myGeomGUI->getApp()->selectionMgr(), 0, this, 0 );
433
434   switch ( constructorId ) {
435   case 0:
436     {
437       globalSelection(); // close local contexts, if any
438       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX ); //Select Vertex on All Shapes
439
440       Group3Pnts2->hide();
441       Group3Pnts->show();
442
443       Group3Pnts->LineEdit1->setText( Group3Pnts2->LineEdit1->text() );
444       Group3Pnts->LineEdit2->setText( Group3Pnts2->LineEdit2->text() );
445       Group3Pnts->LineEdit3->setText( Group3Pnts2->LineEdit3->text() );
446
447       myEditCurrentArgument = Group3Pnts->LineEdit1;
448       break;
449     }
450   case 1:
451     {
452       globalSelection(); // close local contexts, if any
453       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX ); //Select Vertex on All Shapes
454
455       Group3Pnts->hide();
456       Group3Pnts2->show();
457
458       Group3Pnts2->LineEdit1->setText( Group3Pnts->LineEdit1->text() );
459       Group3Pnts2->LineEdit2->setText( Group3Pnts->LineEdit2->text() );
460       Group3Pnts2->LineEdit3->setText( Group3Pnts->LineEdit3->text() );
461       
462       myEditCurrentArgument = Group3Pnts2->LineEdit1;
463       break;
464     }
465   }
466
467   qApp->processEvents();
468   updateGeometry();
469   resize( minimumSize() );
470
471   myEditCurrentArgument->setFocus();
472   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
473            this, SLOT( SelectionIntoArgument() ) );
474
475   if ( CORBA::is_nil( myPoint1 ) )
476     SelectionIntoArgument();
477
478   displayPreview();
479 }
480
481 //=================================================================================
482 // function : ReverseSense()
483 // purpose  : Orientation of the arc
484 //=================================================================================
485 void BasicGUI_ArcDlg::ReverseSense()
486 {
487   displayPreview();
488 }
489
490 //=================================================================================
491 // function : addSubshapeToStudy
492 // purpose  : virtual method to add new SubObjects if local selection
493 //=================================================================================
494 void BasicGUI_ArcDlg::addSubshapesToStudy()
495 {
496   QMap<QString, GEOM::GEOM_Object_var> objMap;
497
498   switch ( getConstructorId() ) {
499   case 0:
500     objMap[Group3Pnts->LineEdit1->text()] = myPoint1;
501     objMap[Group3Pnts->LineEdit2->text()] = myPoint2;
502     objMap[Group3Pnts->LineEdit3->text()] = myPoint3;
503     break;
504   case 1:
505     objMap[Group3Pnts2->LineEdit1->text()] = myPoint1;
506     objMap[Group3Pnts2->LineEdit2->text()] = myPoint2;
507     objMap[Group3Pnts2->LineEdit3->text()] = myPoint3;
508     break;
509   }
510   addSubshapesToFather( objMap );
511 }