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