Salome HOME
Mantis issue 0021071: Regression in GetInPlace. A fix by PKV on Partition error.
[modules/geom.git] / src / BuildGUI / BuildGUI_EdgeDlg.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : BuildGUI_EdgeDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "BuildGUI_EdgeDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <GEOMImpl_Types.hxx>
39 #include <TColStd_IndexedMapOfInteger.hxx>
40
41 //=================================================================================
42 // class    : BuildGUI_EdgeDlg()
43 // purpose  : Constructs a BuildGUI_EdgeDlg which is a child of 'parent', with the
44 //            name 'name' and widget flags set to 'f'.
45 //            The dialog will by default be modeless, unless you set 'modal' to
46 //            TRUE to construct a modal dialog.
47 //=================================================================================
48 BuildGUI_EdgeDlg::BuildGUI_EdgeDlg (GeometryGUI* theGeometryGUI, QWidget* parent)
49   : GEOMBase_Skeleton(theGeometryGUI, parent)
50 {
51   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
52   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BUILD_EDGE")));
53   QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BUILD_EDGE_WIRE")));
54
55   setWindowTitle(tr("GEOM_EDGE_TITLE"));
56
57   /***************************************************************/
58   mainFrame()->GroupConstructors->setTitle(tr("GEOM_EDGE"));
59   mainFrame()->RadioButton1->setIcon(image1);
60   mainFrame()->RadioButton2->setIcon(image2);
61   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
62   mainFrame()->RadioButton3->close();
63
64   // two points
65
66   GroupPoints = new DlgRef_2Sel(centralWidget());
67   GroupPoints->GroupBox1->setTitle(tr("GEOM_POINTS"));
68   GroupPoints->TextLabel1->setText(tr("GEOM_POINT_I").arg(1));
69   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_I").arg(2));
70   GroupPoints->PushButton1->setIcon(image0);
71   GroupPoints->PushButton2->setIcon(image0);
72   GroupPoints->LineEdit1->setReadOnly(true);
73   GroupPoints->LineEdit2->setReadOnly(true);
74
75   // wire
76
77   GroupWire = new DlgRef_1Sel2Spin(centralWidget());
78   GroupWire->GroupBox1->setTitle(tr("GEOM_WIRE"));
79   GroupWire->TextLabel1->setText(tr("GEOM_WIRE"));
80   GroupWire->PushButton1->setIcon(image0);
81   GroupWire->LineEdit1->setReadOnly(true);
82   GroupWire->TextLabel2->setText( tr( "GEOM_LINEAR_TOLERANCE" ) );
83   GroupWire->TextLabel3->setText( tr( "GEOM_ANGULAR_TOLERANCE" ) );
84   double SpecificStep = 0.0001;
85   double prec1 = Precision::Confusion();
86   double prec2 = Precision::Angular();
87   initSpinBox(GroupWire->SpinBox_DX, prec1, MAX_NUMBER, SpecificStep, "len_tol_precision" );
88   initSpinBox(GroupWire->SpinBox_DY, prec2, MAX_NUMBER, SpecificStep, "ang_tol_precision" );
89   GroupWire->SpinBox_DX->setValue(prec1);
90   GroupWire->SpinBox_DY->setValue(prec2);
91
92   // layout
93
94   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
95   layout->setMargin(0); layout->setSpacing(6);
96   layout->addWidget(GroupPoints);
97   layout->addWidget(GroupWire);
98   /***************************************************************/
99
100   setHelpFileName("create_edge_page.html");
101
102   // initialisation
103   Init();
104 }
105
106 //=================================================================================
107 // function : ~BuildGUI_EdgeDlg()
108 // purpose  : Destroys the object and frees any allocated resources
109 //=================================================================================
110 BuildGUI_EdgeDlg::~BuildGUI_EdgeDlg()
111 {
112   // no need to delete child widgets, Qt does it all for us
113 }
114
115 //=================================================================================
116 // function : Init()
117 // purpose  :
118 //=================================================================================
119 void BuildGUI_EdgeDlg::Init()
120 {
121   // init variables
122   myPoint1 = myPoint2 = myWire = GEOM::GEOM_Object::_nil();
123   myEditCurrentArgument = GroupPoints->LineEdit1;
124   GroupPoints->PushButton1->setDown(true);
125   globalSelection(); // close local contexts, if any
126   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
127
128   // signals and slots connections
129   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
130   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
131
132   connect( this,      SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
133
134   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
135   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
136
137   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
138   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
139   connect(GroupWire->PushButton1,   SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
140
141   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
142   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
143   connect(GroupWire->LineEdit1,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
144
145   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
146            this, SLOT( SelectionIntoArgument() ) );
147
148   initName(tr("GEOM_EDGE"));
149
150   ConstructorsClicked( 0 );
151 }
152
153 //=================================================================================
154 // function : ClickOnOk()
155 // purpose  :
156 //=================================================================================
157 void BuildGUI_EdgeDlg::ClickOnOk()
158 {
159   if (ClickOnApply())
160     ClickOnCancel();
161 }
162
163 //=================================================================================
164 // function : ClickOnApply()
165 // purpose  :
166 //=================================================================================
167 bool BuildGUI_EdgeDlg::ClickOnApply()
168 {
169   if (!onAccept())
170     return false;
171
172   initName();
173
174   myEditCurrentArgument->setText( "" );
175   ConstructorsClicked( getConstructorId() );
176
177   return true;
178 }
179
180 //=================================================================================
181 // function : ConstructorsClicked()
182 // purpose  : Radio button management
183 //=================================================================================
184 void BuildGUI_EdgeDlg::ConstructorsClicked( int constructorId )
185 {
186   switch ( constructorId ) {
187   case 0:
188     {
189       globalSelection(); // close local contexts, if any
190       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
191
192       myEditCurrentArgument = GroupPoints->LineEdit1;
193       GroupPoints->LineEdit1->setText( "" );
194       GroupPoints->LineEdit2->setText( "" );
195       myPoint1 = GEOM::GEOM_Object::_nil();
196       myPoint2 = GEOM::GEOM_Object::_nil();
197       GroupPoints->PushButton1->setDown(true);
198       GroupPoints->PushButton2->setDown(false);
199       GroupPoints->LineEdit1->setEnabled(true);
200       GroupPoints->LineEdit2->setEnabled(false);
201       GroupPoints->show();
202       GroupWire->hide();
203       break;
204     }
205   case 1:
206     {
207       globalSelection(); // close local contexts, if any
208       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_WIRE );
209
210       myEditCurrentArgument = GroupWire->LineEdit1;
211       GroupWire->LineEdit1->setText("");
212       myWire = GEOM::GEOM_Object::_nil();
213       GroupWire->PushButton1->setDown(true);
214       GroupWire->LineEdit1->setEnabled(true);
215       GroupPoints->hide();
216       GroupWire->show();
217       break;
218     }
219   }
220
221   qApp->processEvents();
222   updateGeometry();
223   resize( minimumSizeHint() );
224   SelectionIntoArgument();
225 }
226
227 //=================================================================================
228 // function : SelectionIntoArgument()
229 // purpose  : Called when selection is changed or on dialog initialization or activation
230 //=================================================================================
231 void BuildGUI_EdgeDlg::SelectionIntoArgument()
232 {
233   erasePreview();
234   myEditCurrentArgument->setText("");
235
236   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
237   SALOME_ListIO aSelList;
238   aSelMgr->selectedObjects(aSelList);
239
240   if (aSelList.Extent() != 1) {
241     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint1 = GEOM::GEOM_Object::_nil();
242     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myPoint2 = GEOM::GEOM_Object::_nil();
243     else if (myEditCurrentArgument == GroupWire->LineEdit1)   myWire = GEOM::GEOM_Object::_nil();
244     displayPreview();
245     return;
246   }
247
248   // nbSel == 1
249   Standard_Boolean testResult = Standard_False;
250   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
251
252   if ( testResult && !aSelectedObject->_is_nil() ) {
253     QString aName = GEOMBase::GetName( aSelectedObject );
254     TopAbs_ShapeEnum aNeedType = myEditCurrentArgument == GroupWire->LineEdit1 ? TopAbs_WIRE : TopAbs_VERTEX;
255
256     TopoDS_Shape aShape;
257     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
258       TColStd_IndexedMapOfInteger aMap;
259       aSelMgr->GetIndexes( aSelList.First(), aMap );
260       if ( aMap.Extent() == 1 ) { // Local Selection
261         int anIndex = aMap( 1 );
262         aName += ( aNeedType == TopAbs_WIRE ? QString( ":wire_%1" ).arg( anIndex ) : QString( ":vertex_%1" ).arg( anIndex ) );
263
264         //Find SubShape Object in Father
265         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
266         if ( CORBA::is_nil( aFindedObject ) ) { // Object not found in study
267           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
268           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
269         }
270         else {
271           aSelectedObject = aFindedObject; // get Object from study
272         }
273       }
274       else { // Global Selection
275         if ( aShape.ShapeType() != aNeedType ) {
276           aSelectedObject = GEOM::GEOM_Object::_nil();
277           aName = "";
278         }
279       }
280     }
281
282     myEditCurrentArgument->setText( aName );
283
284     if (!aSelectedObject->_is_nil()) { // clear selection if something selected
285       globalSelection();
286       localSelection( GEOM::GEOM_Object::_nil(), aNeedType );
287     }
288
289     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
290       myPoint1 = aSelectedObject;
291       if ( !myPoint1->_is_nil() && myPoint2->_is_nil() )
292         GroupPoints->PushButton2->click();
293     }
294     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
295       myPoint2 = aSelectedObject;
296       if ( !myPoint2->_is_nil() && myPoint1->_is_nil() )
297         GroupPoints->PushButton1->click();
298     }
299     else if ( myEditCurrentArgument == GroupWire->LineEdit1 ) {
300       myWire = aSelectedObject;
301     }
302   }
303
304   displayPreview();
305 }
306
307 //=================================================================================
308 // function : SetEditCurrentArgument()
309 // purpose  :
310 //=================================================================================
311 void BuildGUI_EdgeDlg::SetEditCurrentArgument()
312 {
313   QPushButton* send = (QPushButton*)sender();
314
315   if (send == GroupPoints->PushButton1) {
316     myEditCurrentArgument = GroupPoints->LineEdit1;
317     GroupPoints->PushButton2->setDown(false);
318     GroupPoints->LineEdit2->setEnabled(false);
319   }
320   else if (send == GroupPoints->PushButton2) {
321     myEditCurrentArgument = GroupPoints->LineEdit2;
322     GroupPoints->PushButton1->setDown(false);
323     GroupPoints->LineEdit1->setEnabled(false);
324   }
325   else if (send == GroupWire->PushButton1) {
326     myEditCurrentArgument = GroupWire->LineEdit1;
327   }
328
329   // enable line edit
330   myEditCurrentArgument->setEnabled(true);
331   myEditCurrentArgument->setFocus();
332   send->setDown(true);
333   displayPreview();
334 }
335
336 //=================================================================================
337 // function : LineEditReturnPressed()
338 // purpose  :
339 //=================================================================================
340 void BuildGUI_EdgeDlg::LineEditReturnPressed()
341 {
342   QLineEdit* send = (QLineEdit*)sender();
343   if (send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2 || send == GroupWire->LineEdit1 ) {
344     myEditCurrentArgument = send;
345     GEOMBase_Skeleton::LineEditReturnPressed();
346   }
347 }
348
349 //=================================================================================
350 // function : ActivateThisDialog()
351 // purpose  :
352 //=================================================================================
353 void BuildGUI_EdgeDlg::ActivateThisDialog()
354 {
355   GEOMBase_Skeleton::ActivateThisDialog();
356
357   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
358            this, SLOT( SelectionIntoArgument() ) );
359
360   ConstructorsClicked( getConstructorId() );
361 }
362
363 //=================================================================================
364 // function : enterEvent()
365 // purpose  :
366 //=================================================================================
367 void BuildGUI_EdgeDlg::enterEvent (QEvent*)
368 {
369   if (!mainFrame()->GroupConstructors->isEnabled())
370     ActivateThisDialog();
371 }
372
373 //=================================================================================
374 // function : createOperation
375 // purpose  :
376 //=================================================================================
377 GEOM::GEOM_IOperations_ptr BuildGUI_EdgeDlg::createOperation()
378 {
379   return getGeomEngine()->GetIShapesOperations(getStudyId());
380 }
381
382 //=================================================================================
383 // function : isValid
384 // purpose  :
385 //=================================================================================
386 bool BuildGUI_EdgeDlg::isValid (QString& msg)
387 {
388   bool ok = false;
389   if ( getConstructorId() == 0 ) {
390     ok = !myPoint1->_is_nil() && !myPoint2->_is_nil();
391   }
392   else {
393     ok = !myWire->_is_nil();
394     ok = ok && GroupWire->SpinBox_DX->isValid( msg, !IsPreview() );
395     ok = ok && GroupWire->SpinBox_DY->isValid( msg, !IsPreview() );
396   }
397   return ok;
398 }
399
400 //=================================================================================
401 // function : execute
402 // purpose  :
403 //=================================================================================
404 bool BuildGUI_EdgeDlg::execute (ObjectList& objects)
405 {
406   bool res = false;
407   GEOM::GEOM_Object_var anObj;
408
409   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
410
411   switch ( getConstructorId() ) {
412   case 0 :
413     {
414       anObj = anOper->MakeEdge( myPoint1, myPoint2 );
415       res = true;
416       break;
417     }
418   case 1:
419     {
420       double aLinearTolerance   = GroupWire->SpinBox_DX->value();
421       double anAngularTolerance = GroupWire->SpinBox_DY->value();
422       
423       QStringList aParameters;
424       aParameters << GroupWire->SpinBox_DX->text();
425       aParameters << GroupWire->SpinBox_DY->text();
426       
427       anObj = anOper->MakeEdgeWire( myWire, aLinearTolerance, anAngularTolerance );
428       
429       if ( !anObj->_is_nil() && !IsPreview() )
430         anObj->SetParameters( aParameters.join(":").toLatin1().constData() );
431       
432       res = true;
433       break;
434     }
435   }
436
437   if ( !anObj->_is_nil() ) objects.push_back( anObj._retn() );
438
439   return res;
440 }
441
442 //=================================================================================
443 // function : addSubshapeToStudy
444 // purpose  : virtual method to add new SubObjects if local selection
445 //=================================================================================
446 void BuildGUI_EdgeDlg::addSubshapesToStudy()
447 {
448   QMap<QString, GEOM::GEOM_Object_var> objMap;
449   switch ( getConstructorId() ) {
450   case 0 :
451     objMap[GroupPoints->LineEdit1->text()] = myPoint1;
452     objMap[GroupPoints->LineEdit2->text()] = myPoint2;
453     break;
454   case 1 :
455     objMap[GroupWire->LineEdit1->text()] = myWire;
456     break;
457   }
458   addSubshapesToFather( objMap );
459 }