Salome HOME
Merge from V5_1_main 14/05/2010
[modules/geom.git] / src / BasicGUI / BasicGUI_LineDlg.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   : BasicGUI_LineDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "BasicGUI_LineDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_Session.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS.hxx>
40 #include <TopExp.hxx>
41 #include <TColStd_IndexedMapOfInteger.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
43
44 #include <GEOMImpl_Types.hxx>
45
46 //=================================================================================
47 // class    : BasicGUI_LineDlg()
48 // purpose  : Constructs a BasicGUI_LineDlg 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_LineDlg::BasicGUI_LineDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
54                                     bool modal, Qt::WindowFlags fl )
55   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
56 {
57   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_LINE_2P" ) ) );
58   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
59   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_LINE_2F" ) ) );
60
61   setWindowTitle( tr( "GEOM_LINE_TITLE" ) );
62  
63   /***************************************************************/
64   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_LINE" ) );
65   mainFrame()->RadioButton1->setIcon( image0 );
66   mainFrame()->RadioButton2->setIcon( image2 );
67   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
68   mainFrame()->RadioButton3->close();
69
70   GroupPoints = new DlgRef_2Sel( centralWidget() );
71
72   GroupPoints->GroupBox1->setTitle( tr( "GEOM_POINTS" ) );
73   GroupPoints->TextLabel1->setText( tr( "GEOM_POINT_I" ).arg( 1 ) );
74   GroupPoints->TextLabel2->setText( tr( "GEOM_POINT_I" ).arg( 2 ) );
75   GroupPoints->PushButton1->setIcon( image1 );
76   GroupPoints->PushButton2->setIcon( image1 );
77   GroupPoints->LineEdit1->setReadOnly( true );
78   GroupPoints->LineEdit2->setReadOnly( true );
79   GroupPoints->LineEdit1->setEnabled( true );
80   GroupPoints->LineEdit2->setEnabled( false );
81
82   GroupFaces = new DlgRef_2Sel( centralWidget() );
83   GroupFaces->GroupBox1->setTitle( tr( "GEOM_FACES" ) );
84   GroupFaces->TextLabel1->setText( tr( "GEOM_FACE_I" ).arg( "1" ) );
85   GroupFaces->TextLabel2->setText( tr( "GEOM_FACE_I" ).arg( "2" ) );
86   GroupFaces->PushButton1->setIcon( image1 );
87   GroupFaces->PushButton2->setIcon( image1 );
88   GroupFaces->LineEdit1->setReadOnly( true );
89   GroupFaces->LineEdit2->setReadOnly( true );
90   GroupFaces->LineEdit1->setEnabled( true );
91   GroupFaces->LineEdit2->setEnabled( false );
92
93   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
94   layout->setMargin( 0 ); layout->setSpacing( 6 );
95   layout->addWidget( GroupPoints );
96   layout->addWidget( GroupFaces );
97
98   /***************************************************************/
99
100   setHelpFileName( "create_line_page.html" );
101
102   Init();
103 }
104
105
106 //=================================================================================
107 // function : ~BasicGUI_LineDlg()
108 // purpose  : Destroys the object and frees any allocated resources
109 //=================================================================================
110 BasicGUI_LineDlg::~BasicGUI_LineDlg()
111 {  
112 }
113
114
115 //=================================================================================
116 // function : Init()
117 // purpose  :
118 //=================================================================================
119 void BasicGUI_LineDlg::Init()
120 {
121   /* init variables */
122   myEditCurrentArgument = GroupPoints->LineEdit1;
123   myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
124   globalSelection(); // close local contexts, if any
125   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
126   GroupPoints->PushButton1->setDown(true);
127   
128         
129   /* signals and slots connections */
130   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
131   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
132
133   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
134
135   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
136   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
137
138   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
139   connect( GroupPoints->PushButton2, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
140   connect( GroupFaces->PushButton1,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
141   connect( GroupFaces->PushButton2,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
142
143   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
144   connect( GroupPoints->LineEdit2,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
145   connect( GroupFaces->LineEdit1,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
146   connect( GroupFaces->LineEdit2,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
147
148   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
149            this, SLOT( SelectionIntoArgument() ) );
150   
151   initName( tr( "GEOM_LINE" ) );
152
153   ConstructorsClicked( 0 );
154 }
155
156
157 //=================================================================================
158 // function : ClickOnOk()
159 // purpose  :
160 //=================================================================================
161 void BasicGUI_LineDlg::ClickOnOk()
162 {
163   if ( ClickOnApply() )
164     ClickOnCancel();
165 }
166
167 //=================================================================================
168 // function : ClickOnApply()
169 // purpose  :
170 //=================================================================================
171 bool BasicGUI_LineDlg::ClickOnApply()
172 {
173   if ( !onAccept() )
174     return false;
175   
176   initName();
177
178   myEditCurrentArgument->setText( "" );
179   ConstructorsClicked( getConstructorId() );
180
181   return true;
182 }
183
184 //=================================================================================
185 // function : ConstructorsClicked()
186 // purpose  : Radio button management
187 //=================================================================================
188 void BasicGUI_LineDlg::ConstructorsClicked( int constructorId )
189 {  
190   switch ( constructorId ) {
191   case 0:
192     {
193       globalSelection(); // close local contexts, if any
194       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
195       
196       myEditCurrentArgument = GroupPoints->LineEdit1;
197       myEditCurrentArgument->setText( "" );
198       GroupPoints->LineEdit2->setText( "" );
199       myPoint1 = GEOM::GEOM_Object::_nil();
200       myPoint2 = GEOM::GEOM_Object::_nil();
201       GroupPoints->PushButton1->setDown(true);
202       GroupPoints->PushButton2->setDown(false);
203       GroupPoints->LineEdit1->setEnabled(true);
204       GroupPoints->LineEdit2->setEnabled(false);
205       GroupPoints->show();
206       GroupFaces->hide();
207       break;
208     }
209   case 1:
210     {
211       globalSelection(); // close local contexts, if any
212       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
213
214       myEditCurrentArgument = GroupFaces->LineEdit1;
215       myEditCurrentArgument->setText("");
216       myFace1 = GEOM::GEOM_Object::_nil();
217       myFace2 = GEOM::GEOM_Object::_nil();
218       GroupFaces->PushButton1->setDown(true);
219       GroupFaces->PushButton2->setDown(false);
220       GroupFaces->LineEdit1->setEnabled(true);
221       GroupFaces->LineEdit2->setEnabled(false);
222       GroupPoints->hide();
223       GroupFaces->show();
224       break;
225     }
226   }
227
228   qApp->processEvents();
229   updateGeometry();
230   resize( minimumSizeHint() );
231
232   SelectionIntoArgument();
233 }
234
235 //=================================================================================
236 // function : SelectionIntoArgument()
237 // purpose  : Called when selection as changed or other case
238 //=================================================================================
239 void BasicGUI_LineDlg::SelectionIntoArgument()
240 {
241   myEditCurrentArgument->setText( "" );
242
243   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
244   SALOME_ListIO aSelList;
245   aSelMgr->selectedObjects(aSelList);
246
247   if (aSelList.Extent() != 1) {
248     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint1 = GEOM::GEOM_Object::_nil();
249     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myPoint2 = GEOM::GEOM_Object::_nil();
250     else if (myEditCurrentArgument == GroupFaces->LineEdit1)  myFace1  = GEOM::GEOM_Object::_nil();
251     else if (myEditCurrentArgument == GroupFaces->LineEdit2)  myFace2  = GEOM::GEOM_Object::_nil();
252     displayPreview();
253     return;
254   }
255
256   Standard_Boolean aRes = Standard_False;
257   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aRes);
258   if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
259     QString aName = GEOMBase::GetName( aSelectedObject );
260     TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
261     if ( myEditCurrentArgument == GroupFaces->LineEdit1 ||
262          myEditCurrentArgument == GroupFaces->LineEdit2 )
263       aNeedType = TopAbs_FACE;
264     
265     TopoDS_Shape aShape;
266     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
267       TColStd_IndexedMapOfInteger aMap;
268       aSelMgr->GetIndexes(aSelList.First(), aMap);
269       if ( aMap.Extent() == 1 ) { // Local Selection
270         int anIndex = aMap( 1 );
271         if ( aNeedType == TopAbs_FACE )
272           aName += QString( ":face_%1" ).arg( anIndex );
273         else
274           aName += QString( ":vertex_%1" ).arg( anIndex );
275
276         //Find SubShape Object in Father
277         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
278
279         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
280           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
281           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
282         }
283         else {
284           aSelectedObject = aFindedObject; // get Object from study
285         }
286       }
287       else { // Global Selection
288         if ( aShape.ShapeType() != aNeedType ) {
289           aSelectedObject = GEOM::GEOM_Object::_nil();
290           aName = "";
291         }
292       }
293     }
294
295     myEditCurrentArgument->setText( aName );
296
297     if (!aSelectedObject->_is_nil()) { // clear selection if something selected
298       globalSelection();
299       localSelection( GEOM::GEOM_Object::_nil(), aNeedType );      
300     }
301
302     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
303       myPoint1 = aSelectedObject;
304       if ( !myPoint1->_is_nil() && myPoint2->_is_nil() )
305         GroupPoints->PushButton2->click();
306     }
307     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
308       myPoint2 = aSelectedObject;
309       if ( !myPoint2->_is_nil() && myPoint1->_is_nil() )
310         GroupPoints->PushButton1->click();
311     }
312     else if ( myEditCurrentArgument == GroupFaces->LineEdit1 ) {
313       myFace1 = aSelectedObject;
314       if ( !myFace1->_is_nil() && myFace2->_is_nil() )
315         GroupFaces->PushButton2->click();
316     }
317     else if ( myEditCurrentArgument == GroupFaces->LineEdit2 ) {
318       myFace2 = aSelectedObject;
319       if ( !myFace2->_is_nil() && myFace1->_is_nil() )
320         GroupFaces->PushButton1->click();      
321     }
322   }
323   
324   displayPreview();
325 }
326
327
328 //=================================================================================
329 // function : SetEditCurrentArgument()
330 // purpose  :
331 //=================================================================================
332 void BasicGUI_LineDlg::SetEditCurrentArgument()
333 {
334   QPushButton* send = (QPushButton*)sender();
335   if ( send == GroupPoints->PushButton1 ) {
336     myEditCurrentArgument = GroupPoints->LineEdit1;
337     GroupPoints->PushButton2->setDown(false);
338     GroupPoints->LineEdit1->setEnabled(true);
339     GroupPoints->LineEdit2->setEnabled(false);
340   }
341   else if ( send == GroupPoints->PushButton2 ) {
342     myEditCurrentArgument = GroupPoints->LineEdit2;
343     GroupPoints->PushButton1->setDown(false);
344     GroupPoints->LineEdit1->setEnabled(false);
345     GroupPoints->LineEdit2->setEnabled(true);
346   }
347   else if ( send == GroupFaces->PushButton1 ) {
348     myEditCurrentArgument = GroupFaces->LineEdit1;
349     GroupFaces->PushButton2->setDown(false);
350     GroupFaces->LineEdit1->setEnabled(true);
351     GroupFaces->LineEdit2->setEnabled(false);
352   }
353   else if ( send == GroupFaces->PushButton2 ) {
354     myEditCurrentArgument = GroupFaces->LineEdit2;
355     GroupFaces->PushButton1->setDown(false);
356     GroupFaces->LineEdit1->setEnabled(false);
357     GroupFaces->LineEdit2->setEnabled(true);
358   }
359
360   globalSelection(); // close local selection to clear it
361   TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
362   if ( myEditCurrentArgument == GroupFaces->LineEdit1 || myEditCurrentArgument == GroupFaces->LineEdit2 )
363     aNeedType = TopAbs_FACE;
364   localSelection( GEOM::GEOM_Object::_nil(), aNeedType );
365
366   myEditCurrentArgument->setFocus();
367   //  SelectionIntoArgument();
368   send->setDown(true);
369   displayPreview();
370 }
371
372
373 //=================================================================================
374 // function : LineEditReturnPressed()
375 // purpose  :
376 //=================================================================================
377 void BasicGUI_LineDlg::LineEditReturnPressed()
378 {
379   QLineEdit* send = (QLineEdit*)sender();
380   if      ( send == GroupPoints->LineEdit1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
381   else if ( send == GroupPoints->LineEdit2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
382   else if ( send == GroupFaces->LineEdit1 )  myEditCurrentArgument = GroupFaces->LineEdit1;
383   else if ( send == GroupFaces->LineEdit2 )  myEditCurrentArgument = GroupFaces->LineEdit2;
384   else return;
385   GEOMBase_Skeleton::LineEditReturnPressed();
386 }
387
388
389 //=================================================================================
390 // function : ActivateThisDialog()
391 // purpose  :
392 //=================================================================================
393 void BasicGUI_LineDlg::ActivateThisDialog()
394 {
395   GEOMBase_Skeleton::ActivateThisDialog();
396   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
397            this, SLOT( SelectionIntoArgument() ) );
398
399   ConstructorsClicked( getConstructorId() );
400 }
401
402 //=================================================================================
403 // function : DeactivateActiveDialog()
404 // purpose  : public slot to deactivate if active
405 //=================================================================================
406 void BasicGUI_LineDlg::DeactivateActiveDialog()
407 {
408   GEOMBase_Skeleton::DeactivateActiveDialog();
409 }
410
411 //=================================================================================
412 // function : enterEvent()
413 // purpose  :
414 //=================================================================================
415 void BasicGUI_LineDlg::enterEvent( QEvent* )
416 {
417   if ( !mainFrame()->GroupConstructors->isEnabled() )
418     ActivateThisDialog();
419 }
420
421 //=================================================================================
422 // function : createOperation
423 // purpose  :
424 //=================================================================================
425 GEOM::GEOM_IOperations_ptr BasicGUI_LineDlg::createOperation()
426 {
427   return myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
428 }
429
430 //=================================================================================
431 // function : isValid
432 // purpose  :
433 //=================================================================================
434 bool BasicGUI_LineDlg::isValid( QString& msg )
435 {
436   const int id = getConstructorId();
437   if ( id == 0 )
438     return !myPoint1->_is_nil() && !myPoint2->_is_nil();
439   else if ( id == 1 )
440     return !myFace1->_is_nil() && !myFace2->_is_nil();
441   return false;
442 }
443
444 //=================================================================================
445 // function : execute
446 // purpose  :
447 //=================================================================================
448 bool BasicGUI_LineDlg::execute( ObjectList& objects )
449 {
450   bool res = false;
451   GEOM::GEOM_Object_var anObj;
452   GEOM::GEOM_IBasicOperations_var anOper = GEOM::GEOM_IBasicOperations::_narrow( getOperation() );
453   switch ( getConstructorId() ) {
454   case 0 :
455     anObj = anOper->MakeLineTwoPnt( myPoint1, myPoint2 );
456     res = true;
457     break;
458   case 1 :
459     anObj = anOper->MakeLineTwoFaces( myFace1, myFace2 );
460     res = true;
461     break;
462   }
463   
464   if ( !anObj->_is_nil() )
465     objects.push_back( anObj._retn() );
466
467   return res;
468 }
469
470 //=================================================================================
471 // function : addSubshapeToStudy
472 // purpose  : virtual method to add new SubObjects if local selection
473 //=================================================================================
474 void BasicGUI_LineDlg::addSubshapesToStudy()
475 {
476   QMap<QString, GEOM::GEOM_Object_var> objMap;
477   switch ( getConstructorId() ) {
478   case 0 :
479     objMap[GroupPoints->LineEdit1->text()] = myPoint1;
480     objMap[GroupPoints->LineEdit2->text()] = myPoint2;
481     break;
482   case 1 :
483     objMap[GroupFaces->LineEdit1->text()] = myFace1;
484     objMap[GroupFaces->LineEdit2->text()] = myFace2;
485     break;
486   }
487   addSubshapesToFather( objMap );
488 }