Salome HOME
c1fee60878dbe9004f25637685f1a0ae25adcfec
[modules/geom.git] / src / BasicGUI / BasicGUI_LineDlg.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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.nullify();
124   myPoint2.nullify();
125   myFace1.nullify();
126   myFace2.nullify();
127
128   GroupPoints->PushButton1->setDown(true);
129         
130   /* signals and slots connections */
131   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
132   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
133
134   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
135
136   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
137   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
138
139   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
140   connect( GroupPoints->PushButton2, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
141   connect( GroupFaces->PushButton1,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
142   connect( GroupFaces->PushButton2,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
143
144   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
145            this, SLOT( SelectionIntoArgument() ) );
146   
147   initName( tr( "GEOM_LINE" ) );
148
149   ConstructorsClicked( 0 );
150 }
151
152
153 //=================================================================================
154 // function : ClickOnOk()
155 // purpose  :
156 //=================================================================================
157 void BasicGUI_LineDlg::ClickOnOk()
158 {
159   setIsApplyAndClose( true );
160   if ( ClickOnApply() )
161     ClickOnCancel();
162 }
163
164 //=================================================================================
165 // function : ClickOnApply()
166 // purpose  :
167 //=================================================================================
168 bool BasicGUI_LineDlg::ClickOnApply()
169 {
170   if ( !onAccept() )
171     return false;
172   
173   initName();
174
175   myEditCurrentArgument->setText( "" );
176   ConstructorsClicked( getConstructorId() );
177
178   return true;
179 }
180
181 //=================================================================================
182 // function : ConstructorsClicked()
183 // purpose  : Radio button management
184 //=================================================================================
185 void BasicGUI_LineDlg::ConstructorsClicked( int constructorId )
186 {  
187   switch ( constructorId ) {
188   case 0:
189     {
190       myEditCurrentArgument = GroupPoints->LineEdit1;
191       myEditCurrentArgument->setText( "" );
192       GroupPoints->LineEdit2->setText( "" );
193       myPoint1.nullify();
194       myPoint2.nullify();
195       GroupPoints->PushButton1->setDown(true);
196       GroupPoints->PushButton2->setDown(false);
197       GroupPoints->LineEdit1->setEnabled(true);
198       GroupPoints->LineEdit2->setEnabled(false);
199       GroupPoints->show();
200       GroupFaces->hide();
201       break;
202     }
203   case 1:
204     {
205       myEditCurrentArgument = GroupFaces->LineEdit1;
206       myEditCurrentArgument->setText("");
207       myFace1.nullify();
208       myFace2.nullify();
209       GroupFaces->PushButton1->setDown(true);
210       GroupFaces->PushButton2->setDown(false);
211       GroupFaces->LineEdit1->setEnabled(true);
212       GroupFaces->LineEdit2->setEnabled(false);
213       GroupPoints->hide();
214       GroupFaces->show();
215       break;
216     }
217   }
218
219   TopAbs_ShapeEnum aNeedType = ( myEditCurrentArgument == GroupFaces->LineEdit1 ||
220                                  myEditCurrentArgument == GroupFaces->LineEdit2 ) ?
221     TopAbs_FACE : TopAbs_VERTEX;
222   globalSelection(); // close local selection to clear it
223   localSelection( aNeedType );
224
225   qApp->processEvents();
226   updateGeometry();
227   resize( minimumSizeHint() );
228   SelectionIntoArgument();
229 }
230
231 //=================================================================================
232 // function : SelectionIntoArgument()
233 // purpose  : Called when selection as changed or other case
234 //=================================================================================
235 void BasicGUI_LineDlg::SelectionIntoArgument()
236 {
237   myEditCurrentArgument->setText( "" );
238
239   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
240   SALOME_ListIO aSelList;
241   aSelMgr->selectedObjects(aSelList);
242
243   if (aSelList.Extent() != 1) {
244     if      (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint1.nullify();
245     else if (myEditCurrentArgument == GroupPoints->LineEdit2) myPoint2.nullify();
246     else if (myEditCurrentArgument == GroupFaces->LineEdit1)  myFace1.nullify();
247     else if (myEditCurrentArgument == GroupFaces->LineEdit2)  myFace2.nullify();
248     displayPreview(true);
249     return;
250   }
251
252   TopAbs_ShapeEnum aNeedType = ( myEditCurrentArgument == GroupFaces->LineEdit1 ||
253                                  myEditCurrentArgument == GroupFaces->LineEdit2 ) ?
254     TopAbs_FACE : TopAbs_VERTEX;
255   GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
256   TopoDS_Shape aShape;
257   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
258     QString aName = GEOMBase::GetName( aSelectedObject.get() );
259     myEditCurrentArgument->setText( aName );
260     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
261       myPoint1 = aSelectedObject;
262       if ( myPoint1 && !myPoint2 )
263         GroupPoints->PushButton2->click();
264     }
265     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
266       myPoint2 = aSelectedObject;
267       if ( myPoint2 && !myPoint1 )
268         GroupPoints->PushButton1->click();
269     }
270     else if ( myEditCurrentArgument == GroupFaces->LineEdit1 ) {
271       myFace1 = aSelectedObject;
272       if ( myFace1 && !myFace2 )
273         GroupFaces->PushButton2->click();
274     }
275     else if ( myEditCurrentArgument == GroupFaces->LineEdit2 ) {
276       myFace2 = aSelectedObject;
277       if ( myFace2 && !myFace1 )
278         GroupFaces->PushButton1->click();      
279     }
280   }
281   
282   displayPreview(true);
283 }
284
285 //=================================================================================
286 // function : SetEditCurrentArgument()
287 // purpose  :
288 //=================================================================================
289 void BasicGUI_LineDlg::SetEditCurrentArgument()
290 {
291   QPushButton* send = (QPushButton*)sender();
292   if ( send == GroupPoints->PushButton1 ) {
293     myEditCurrentArgument = GroupPoints->LineEdit1;
294     GroupPoints->PushButton2->setDown(false);
295     GroupPoints->LineEdit1->setEnabled(true);
296     GroupPoints->LineEdit2->setEnabled(false);
297   }
298   else if ( send == GroupPoints->PushButton2 ) {
299     myEditCurrentArgument = GroupPoints->LineEdit2;
300     GroupPoints->PushButton1->setDown(false);
301     GroupPoints->LineEdit1->setEnabled(false);
302     GroupPoints->LineEdit2->setEnabled(true);
303   }
304   else if ( send == GroupFaces->PushButton1 ) {
305     myEditCurrentArgument = GroupFaces->LineEdit1;
306     GroupFaces->PushButton2->setDown(false);
307     GroupFaces->LineEdit1->setEnabled(true);
308     GroupFaces->LineEdit2->setEnabled(false);
309   }
310   else if ( send == GroupFaces->PushButton2 ) {
311     myEditCurrentArgument = GroupFaces->LineEdit2;
312     GroupFaces->PushButton1->setDown(false);
313     GroupFaces->LineEdit1->setEnabled(false);
314     GroupFaces->LineEdit2->setEnabled(true);
315   }
316
317   TopAbs_ShapeEnum aNeedType = ( myEditCurrentArgument == GroupFaces->LineEdit1 ||
318                                  myEditCurrentArgument == GroupFaces->LineEdit2 ) ?
319     TopAbs_FACE : TopAbs_VERTEX;
320   globalSelection(); // close local selection to clear it
321   localSelection( aNeedType );
322
323   myEditCurrentArgument->setFocus();
324   //  SelectionIntoArgument();
325   send->setDown(true);
326   displayPreview(true);
327 }
328
329 //=================================================================================
330 // function : ActivateThisDialog()
331 // purpose  :
332 //=================================================================================
333 void BasicGUI_LineDlg::ActivateThisDialog()
334 {
335   GEOMBase_Skeleton::ActivateThisDialog();
336   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
337            this, SLOT( SelectionIntoArgument() ) );
338
339   ConstructorsClicked( getConstructorId() );
340 }
341
342 //=================================================================================
343 // function : DeactivateActiveDialog()
344 // purpose  : public slot to deactivate if active
345 //=================================================================================
346 void BasicGUI_LineDlg::DeactivateActiveDialog()
347 {
348   GEOMBase_Skeleton::DeactivateActiveDialog();
349 }
350
351 //=================================================================================
352 // function : enterEvent()
353 // purpose  :
354 //=================================================================================
355 void BasicGUI_LineDlg::enterEvent( QEvent* )
356 {
357   if ( !mainFrame()->GroupConstructors->isEnabled() )
358     ActivateThisDialog();
359 }
360
361 //=================================================================================
362 // function : createOperation
363 // purpose  :
364 //=================================================================================
365 GEOM::GEOM_IOperations_ptr BasicGUI_LineDlg::createOperation()
366 {
367   return myGeomGUI->GetGeomGen()->GetIBasicOperations();
368 }
369
370 //=================================================================================
371 // function : isValid
372 // purpose  :
373 //=================================================================================
374 bool BasicGUI_LineDlg::isValid( QString& /*msg*/ )
375 {
376   bool ok = false;
377   switch ( getConstructorId() ) {
378   case 0 :
379     ok = myPoint1 && myPoint2;
380     break;
381   case 1:
382     ok = myFace1 && myFace2;
383     break;
384   default:
385     break;
386   }
387   return ok;
388 }
389
390 //=================================================================================
391 // function : execute
392 // purpose  :
393 //=================================================================================
394 bool BasicGUI_LineDlg::execute( ObjectList& objects )
395 {
396   bool res = false;
397   GEOM::GEOM_Object_var anObj;
398   GEOM::GEOM_IBasicOperations_var anOper = GEOM::GEOM_IBasicOperations::_narrow( getOperation() );
399   switch ( getConstructorId() ) {
400   case 0 :
401     anObj = anOper->MakeLineTwoPnt( myPoint1.get(), myPoint2.get() );
402     res = true;
403     break;
404   case 1 :
405     anObj = anOper->MakeLineTwoFaces( myFace1.get(), myFace2.get() );
406     res = true;
407     break;
408   }
409   
410   if ( !anObj->_is_nil() )
411     objects.push_back( anObj._retn() );
412
413   return res;
414 }
415
416 //=================================================================================
417 // function : addSubshapeToStudy
418 // purpose  : virtual method to add new SubObjects if local selection
419 //=================================================================================
420 void BasicGUI_LineDlg::addSubshapesToStudy()
421 {
422   switch ( getConstructorId() ) {
423   case 0 :
424     GEOMBase::PublishSubObject( myPoint1.get() );
425     GEOMBase::PublishSubObject( myPoint2.get() );
426     break;
427   case 1 :
428     GEOMBase::PublishSubObject( myFace1.get() );
429     GEOMBase::PublishSubObject( myFace2.get() );
430     break;
431   default:
432     break;
433   }
434 }
435
436 //=================================================================================
437 // function : getSourceObjects
438 // purpose  : virtual method to get source objects
439 //=================================================================================
440 QList<GEOM::GeomObjPtr> BasicGUI_LineDlg::getSourceObjects()
441 {
442   QList<GEOM::GeomObjPtr> res;
443   res << myPoint1 << myPoint2 << myFace1 << myFace2;
444   return res;
445 }