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