]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_LineDlg.cxx
Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_17Jan08)
[modules/geom.git] / src / BasicGUI / BasicGUI_LineDlg.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2003  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 // File   : BasicGUI_LineDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
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
79   GroupFaces = new DlgRef_2Sel( centralWidget() );
80   GroupFaces->GroupBox1->setTitle( tr( "GEOM_FACES" ) );
81   GroupFaces->TextLabel1->setText( tr( "GEOM_FACE_I" ).arg( "1" ) );
82   GroupFaces->TextLabel2->setText( tr( "GEOM_FACE_I" ).arg( "2" ) );
83   GroupFaces->PushButton1->setIcon( image1 );
84   GroupFaces->PushButton2->setIcon( image1 );
85   GroupFaces->LineEdit1->setReadOnly( true );
86   GroupFaces->LineEdit2->setReadOnly( true );
87
88   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
89   layout->setMargin( 0 ); layout->setSpacing( 6 );
90   layout->addWidget( GroupPoints );
91   layout->addWidget( GroupFaces );
92
93   /***************************************************************/
94
95   setHelpFileName( "create_line_page.html" );
96
97   Init();
98 }
99
100
101 //=================================================================================
102 // function : ~BasicGUI_LineDlg()
103 // purpose  : Destroys the object and frees any allocated resources
104 //=================================================================================
105 BasicGUI_LineDlg::~BasicGUI_LineDlg()
106 {  
107 }
108
109
110 //=================================================================================
111 // function : Init()
112 // purpose  :
113 //=================================================================================
114 void BasicGUI_LineDlg::Init()
115 {
116   /* init variables */
117   //myEditCurrentArgument = GroupPoints->LineEdit1;
118   //myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
119   //globalSelection(); // close local contexts, if any
120   //localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
121         
122   /* signals and slots connections */
123   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
124   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
125
126   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
127
128   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
129   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
130
131   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
132   connect( GroupPoints->PushButton2, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
133   connect( GroupFaces->PushButton1,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
134   connect( GroupFaces->PushButton2,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
135
136   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
137   connect( GroupPoints->LineEdit2,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
138   connect( GroupFaces->LineEdit1,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
139   connect( GroupFaces->LineEdit2,    SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
140
141   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
142            this, SLOT( SelectionIntoArgument() ) );
143   
144   initName( tr( "GEOM_LINE" ) );
145
146   ConstructorsClicked( 0 );
147 }
148
149
150 //=================================================================================
151 // function : ClickOnOk()
152 // purpose  :
153 //=================================================================================
154 void BasicGUI_LineDlg::ClickOnOk()
155 {
156   if ( ClickOnApply() )
157     ClickOnCancel();
158 }
159
160 //=================================================================================
161 // function : ClickOnApply()
162 // purpose  :
163 //=================================================================================
164 bool BasicGUI_LineDlg::ClickOnApply()
165 {
166   if ( !onAccept() )
167     return false;
168   
169   initName();
170
171   myEditCurrentArgument->setText( "" );
172   ConstructorsClicked( getConstructorId() );
173
174   return true;
175 }
176
177 //=================================================================================
178 // function : ConstructorsClicked()
179 // purpose  : Radio button management
180 //=================================================================================
181 void BasicGUI_LineDlg::ConstructorsClicked( int constructorId )
182 {  
183   switch ( constructorId ) {
184   case 0:
185     {
186       globalSelection(); // close local contexts, if any
187       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
188       
189       myEditCurrentArgument = GroupPoints->LineEdit1;
190       myEditCurrentArgument->setText( "" );
191       myPoint1 = GEOM::GEOM_Object::_nil();
192       myPoint2 = GEOM::GEOM_Object::_nil();
193
194       GroupPoints->show();
195       GroupFaces->hide();
196       break;
197     }
198   case 1:
199     {
200       globalSelection(); // close local contexts, if any
201       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
202
203       myEditCurrentArgument = GroupFaces->LineEdit1;
204       myEditCurrentArgument->setText("");
205       myFace1 = GEOM::GEOM_Object::_nil();
206       myFace2 = GEOM::GEOM_Object::_nil();
207       GroupPoints->hide();
208       GroupFaces->show();
209       break;
210     }
211   }
212
213   qApp->processEvents();
214   updateGeometry();
215   resize( minimumSize() );
216
217   SelectionIntoArgument();
218 }
219
220 //=================================================================================
221 // function : SelectionIntoArgument()
222 // purpose  : Called when selection as changed or other case
223 //=================================================================================
224 void BasicGUI_LineDlg::SelectionIntoArgument()
225 {
226   myEditCurrentArgument->setText( "" );
227
228   if ( IObjectCount() != 1 ) {
229     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )      myPoint1 = GEOM::GEOM_Object::_nil();
230     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myPoint2 = GEOM::GEOM_Object::_nil();
231     else if ( myEditCurrentArgument == GroupFaces->LineEdit1 )  myFace1  = GEOM::GEOM_Object::_nil();
232     else if ( myEditCurrentArgument == GroupFaces->LineEdit2 )  myFace2  = GEOM::GEOM_Object::_nil();
233     return;
234   }
235
236   // nbSel == 1 
237   Standard_Boolean aRes = Standard_False;
238   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aRes );
239   if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
240     QString aName = GEOMBase::GetName( aSelectedObject );
241     
242     TopoDS_Shape aShape;
243     if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
244       TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
245       if ( myEditCurrentArgument == GroupFaces->LineEdit1 ||
246            myEditCurrentArgument == GroupFaces->LineEdit2 )
247         aNeedType = TopAbs_FACE;
248
249       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
250       TColStd_IndexedMapOfInteger aMap;
251       aSelMgr->GetIndexes( firstIObject(), aMap );
252       if ( aMap.Extent() == 1 ) { // Local Selection
253         int anIndex = aMap( 1 );
254         if ( aNeedType == TopAbs_FACE )
255           aName += QString( ":face_%1" ).arg( anIndex );
256         else
257           aName += QString( ":vertex_%1" ).arg( anIndex );
258
259         //Find SubShape Object in Father
260         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
261
262         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
263           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
264           aSelectedObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
265         }
266         else {
267           aSelectedObject = aFindedObject; // get Object from study
268         }
269       }
270       else { // Global Selection
271         if ( aShape.ShapeType() != aNeedType ) {
272           aSelectedObject = GEOM::GEOM_Object::_nil();
273           aName = "";
274         }
275       }
276     }
277
278     myEditCurrentArgument->setText( aName );
279
280     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint1 = aSelectedObject;
281     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myPoint2 = aSelectedObject;
282     else if ( myEditCurrentArgument == GroupFaces->LineEdit1 )  myFace1 = aSelectedObject;
283     else if ( myEditCurrentArgument == GroupFaces->LineEdit2 )  myFace2 = aSelectedObject;
284   }
285   
286   displayPreview();
287 }
288
289
290 //=================================================================================
291 // function : SetEditCurrentArgument()
292 // purpose  :
293 //=================================================================================
294 void BasicGUI_LineDlg::SetEditCurrentArgument()
295 {
296   QPushButton* send = (QPushButton*)sender();
297   if      ( send == GroupPoints->PushButton1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
298   else if ( send == GroupPoints->PushButton2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
299   else if ( send == GroupFaces->PushButton1 )  myEditCurrentArgument = GroupFaces->LineEdit1;
300   else if ( send == GroupFaces->PushButton2 )  myEditCurrentArgument = GroupFaces->LineEdit2;
301   myEditCurrentArgument->setFocus();
302   SelectionIntoArgument();
303 }
304
305
306 //=================================================================================
307 // function : LineEditReturnPressed()
308 // purpose  :
309 //=================================================================================
310 void BasicGUI_LineDlg::LineEditReturnPressed()
311 {
312   QLineEdit* send = (QLineEdit*)sender();
313   if      ( send == GroupPoints->LineEdit1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
314   else if ( send == GroupPoints->LineEdit2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
315   else if ( send == GroupFaces->LineEdit1 )  myEditCurrentArgument = GroupFaces->LineEdit1;
316   else if ( send == GroupFaces->LineEdit2 )  myEditCurrentArgument = GroupFaces->LineEdit2;
317   else return;
318   GEOMBase_Skeleton::LineEditReturnPressed();
319 }
320
321
322 //=================================================================================
323 // function : ActivateThisDialog()
324 // purpose  :
325 //=================================================================================
326 void BasicGUI_LineDlg::ActivateThisDialog()
327 {
328   GEOMBase_Skeleton::ActivateThisDialog();
329   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
330            this, SLOT( SelectionIntoArgument() ) );
331
332   ConstructorsClicked( getConstructorId() );
333 }
334
335 //=================================================================================
336 // function : DeactivateActiveDialog()
337 // purpose  : public slot to deactivate if active
338 //=================================================================================
339 void BasicGUI_LineDlg::DeactivateActiveDialog()
340 {
341   GEOMBase_Skeleton::DeactivateActiveDialog();
342 }
343
344 //=================================================================================
345 // function : enterEvent()
346 // purpose  :
347 //=================================================================================
348 void BasicGUI_LineDlg::enterEvent( QEvent* )
349 {
350   if ( !mainFrame()->GroupConstructors->isEnabled() )
351     ActivateThisDialog();
352 }
353
354 //=================================================================================
355 // function : createOperation
356 // purpose  :
357 //=================================================================================
358 GEOM::GEOM_IOperations_ptr BasicGUI_LineDlg::createOperation()
359 {
360   return myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
361 }
362
363 //=================================================================================
364 // function : isValid
365 // purpose  :
366 //=================================================================================
367 bool BasicGUI_LineDlg::isValid( QString& msg )
368 {
369   const int id = getConstructorId();
370   if ( id == 0 )
371     return !myPoint1->_is_nil() && !myPoint2->_is_nil();
372   else if ( id == 1 )
373     return !myFace1->_is_nil() && !myFace2->_is_nil();
374   return false;
375 }
376
377 //=================================================================================
378 // function : execute
379 // purpose  :
380 //=================================================================================
381 bool BasicGUI_LineDlg::execute( ObjectList& objects )
382 {
383   bool res = false;
384   GEOM::GEOM_Object_var anObj;
385   switch ( getConstructorId() ) {
386   case 0 :
387     anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeLineTwoPnt( myPoint1, myPoint2 );
388     res = true;
389     break;
390   case 1 :
391     anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeLineTwoFaces( myFace1, myFace2 );
392     res = true;
393     break;
394   }
395   
396   if ( !anObj->_is_nil() )
397     objects.push_back( anObj._retn() );
398
399   return res;
400 }
401
402 //=================================================================================
403 // function : addSubshapeToStudy
404 // purpose  : virtual method to add new SubObjects if local selection
405 //=================================================================================
406 void BasicGUI_LineDlg::addSubshapesToStudy()
407 {
408   QMap<QString, GEOM::GEOM_Object_var> objMap;
409   switch ( getConstructorId() ) {
410   case 0 :
411     objMap[GroupPoints->LineEdit1->text()] = myPoint1;
412     objMap[GroupPoints->LineEdit2->text()] = myPoint2;
413     break;
414   case 1 :
415     objMap[GroupFaces->LineEdit1->text()] = myFace1;
416     objMap[GroupFaces->LineEdit2->text()] = myFace2;
417     break;
418   }
419   addSubshapesToFather( objMap );
420 }