]> SALOME platform Git repositories - modules/geom.git/blob - src/BuildGUI/BuildGUI_EdgeDlg.cxx
Salome HOME
Code refactoring
[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   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
250
251   if ( !aSelectedObject->_is_nil() ) {
252     QString aName = GEOMBase::GetName( aSelectedObject );
253     TopAbs_ShapeEnum aNeedType = myEditCurrentArgument == GroupWire->LineEdit1 ? TopAbs_WIRE : TopAbs_VERTEX;
254
255     TopoDS_Shape aShape;
256     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
257       TColStd_IndexedMapOfInteger aMap;
258       aSelMgr->GetIndexes( aSelList.First(), aMap );
259       if ( aMap.Extent() == 1 ) { // Local Selection
260         int anIndex = aMap( 1 );
261         aName += ( aNeedType == TopAbs_WIRE ? QString( ":wire_%1" ).arg( anIndex ) : QString( ":vertex_%1" ).arg( anIndex ) );
262
263         //Find SubShape Object in Father
264         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
265         if ( CORBA::is_nil( aFindedObject ) ) { // Object not found in study
266           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
267           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
268         }
269         else {
270           aSelectedObject = aFindedObject; // get Object from study
271         }
272       }
273       else { // Global Selection
274         if ( aShape.ShapeType() != aNeedType ) {
275           aSelectedObject = GEOM::GEOM_Object::_nil();
276           aName = "";
277         }
278       }
279     }
280
281     myEditCurrentArgument->setText( aName );
282
283     if (!aSelectedObject->_is_nil()) { // clear selection if something selected
284       globalSelection();
285       localSelection( GEOM::GEOM_Object::_nil(), aNeedType );
286     }
287
288     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
289       myPoint1 = aSelectedObject;
290       if ( !myPoint1->_is_nil() && myPoint2->_is_nil() )
291         GroupPoints->PushButton2->click();
292     }
293     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
294       myPoint2 = aSelectedObject;
295       if ( !myPoint2->_is_nil() && myPoint1->_is_nil() )
296         GroupPoints->PushButton1->click();
297     }
298     else if ( myEditCurrentArgument == GroupWire->LineEdit1 ) {
299       myWire = aSelectedObject;
300     }
301   }
302
303   displayPreview();
304 }
305
306 //=================================================================================
307 // function : SetEditCurrentArgument()
308 // purpose  :
309 //=================================================================================
310 void BuildGUI_EdgeDlg::SetEditCurrentArgument()
311 {
312   QPushButton* send = (QPushButton*)sender();
313
314   if (send == GroupPoints->PushButton1) {
315     myEditCurrentArgument = GroupPoints->LineEdit1;
316     GroupPoints->PushButton2->setDown(false);
317     GroupPoints->LineEdit2->setEnabled(false);
318   }
319   else if (send == GroupPoints->PushButton2) {
320     myEditCurrentArgument = GroupPoints->LineEdit2;
321     GroupPoints->PushButton1->setDown(false);
322     GroupPoints->LineEdit1->setEnabled(false);
323   }
324   else if (send == GroupWire->PushButton1) {
325     myEditCurrentArgument = GroupWire->LineEdit1;
326   }
327
328   // enable line edit
329   myEditCurrentArgument->setEnabled(true);
330   myEditCurrentArgument->setFocus();
331   send->setDown(true);
332   displayPreview();
333 }
334
335 //=================================================================================
336 // function : LineEditReturnPressed()
337 // purpose  :
338 //=================================================================================
339 void BuildGUI_EdgeDlg::LineEditReturnPressed()
340 {
341   QLineEdit* send = (QLineEdit*)sender();
342   if (send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2 || send == GroupWire->LineEdit1 ) {
343     myEditCurrentArgument = send;
344     GEOMBase_Skeleton::LineEditReturnPressed();
345   }
346 }
347
348 //=================================================================================
349 // function : ActivateThisDialog()
350 // purpose  :
351 //=================================================================================
352 void BuildGUI_EdgeDlg::ActivateThisDialog()
353 {
354   GEOMBase_Skeleton::ActivateThisDialog();
355
356   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
357            this, SLOT( SelectionIntoArgument() ) );
358
359   ConstructorsClicked( getConstructorId() );
360 }
361
362 //=================================================================================
363 // function : enterEvent()
364 // purpose  :
365 //=================================================================================
366 void BuildGUI_EdgeDlg::enterEvent (QEvent*)
367 {
368   if (!mainFrame()->GroupConstructors->isEnabled())
369     ActivateThisDialog();
370 }
371
372 //=================================================================================
373 // function : createOperation
374 // purpose  :
375 //=================================================================================
376 GEOM::GEOM_IOperations_ptr BuildGUI_EdgeDlg::createOperation()
377 {
378   return getGeomEngine()->GetIShapesOperations(getStudyId());
379 }
380
381 //=================================================================================
382 // function : isValid
383 // purpose  :
384 //=================================================================================
385 bool BuildGUI_EdgeDlg::isValid (QString& msg)
386 {
387   bool ok = false;
388   if ( getConstructorId() == 0 ) {
389     ok = !myPoint1->_is_nil() && !myPoint2->_is_nil();
390   }
391   else {
392     ok = !myWire->_is_nil();
393     ok = ok && GroupWire->SpinBox_DX->isValid( msg, !IsPreview() );
394     ok = ok && GroupWire->SpinBox_DY->isValid( msg, !IsPreview() );
395   }
396   return ok;
397 }
398
399 //=================================================================================
400 // function : execute
401 // purpose  :
402 //=================================================================================
403 bool BuildGUI_EdgeDlg::execute (ObjectList& objects)
404 {
405   bool res = false;
406   GEOM::GEOM_Object_var anObj;
407
408   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
409
410   switch ( getConstructorId() ) {
411   case 0 :
412     {
413       anObj = anOper->MakeEdge( myPoint1, myPoint2 );
414       res = true;
415       break;
416     }
417   case 1:
418     {
419       double aLinearTolerance   = GroupWire->SpinBox_DX->value();
420       double anAngularTolerance = GroupWire->SpinBox_DY->value();
421       
422       QStringList aParameters;
423       aParameters << GroupWire->SpinBox_DX->text();
424       aParameters << GroupWire->SpinBox_DY->text();
425       
426       anObj = anOper->MakeEdgeWire( myWire, aLinearTolerance, anAngularTolerance );
427       
428       if ( !anObj->_is_nil() && !IsPreview() )
429         anObj->SetParameters( aParameters.join(":").toLatin1().constData() );
430       
431       res = true;
432       break;
433     }
434   }
435
436   if ( !anObj->_is_nil() ) objects.push_back( anObj._retn() );
437
438   return res;
439 }
440
441 //=================================================================================
442 // function : addSubshapeToStudy
443 // purpose  : virtual method to add new SubObjects if local selection
444 //=================================================================================
445 void BuildGUI_EdgeDlg::addSubshapesToStudy()
446 {
447   QMap<QString, GEOM::GEOM_Object_var> objMap;
448   switch ( getConstructorId() ) {
449   case 0 :
450     objMap[GroupPoints->LineEdit1->text()] = myPoint1;
451     objMap[GroupPoints->LineEdit2->text()] = myPoint2;
452     break;
453   case 1 :
454     objMap[GroupWire->LineEdit1->text()] = myWire;
455     break;
456   }
457   addSubshapesToFather( objMap );
458 }