Salome HOME
6058c6e82f7970b8ead84acf8ec9a37653e17b7b
[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
25 //  Module : GEOM
26 //  $Header$
27 //
28 #include "BasicGUI_LineDlg.h"
29
30 #include "SUIT_Desktop.h"
31 #include "SUIT_Session.h"
32 #include "SalomeApp_Application.h"
33 #include "LightApp_SelectionMgr.h"
34
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS.hxx>
37 #include <TopExp.hxx>
38 #include <TColStd_IndexedMapOfInteger.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40
41 #include <qlabel.h>
42
43 #include "GEOMImpl_Types.hxx"
44
45 #include "utilities.h"
46
47 using namespace std;
48
49 //=================================================================================
50 // class    : BasicGUI_LineDlg()
51 // purpose  : Constructs a BasicGUI_LineDlg which is a child of 'parent', with the 
52 //            name 'name' and widget flags set to 'f'.
53 //            The dialog will by default be modeless, unless you set 'modal' to
54 //            TRUE to construct a modal dialog.
55 //=================================================================================
56 BasicGUI_LineDlg::BasicGUI_LineDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
57                                    const char* name, bool modal, WFlags fl)
58   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
59                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
60 {
61   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM",tr("ICON_DLG_LINE_2P")));
62   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM",tr("ICON_SELECT")));
63   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM",tr("ICON_DLG_LINE_2F")));
64
65   setCaption(tr("GEOM_LINE_TITLE"));
66
67   QGroupBox* aFrame = new QGroupBox( 1, Qt::Horizontal, this );
68   aFrame->setInsideMargin( 0 );
69   aFrame->setFrameStyle( QFrame::NoFrame );
70
71   /***************************************************************/
72   GroupConstructors->setTitle(tr("GEOM_LINE"));
73   RadioButton1->setPixmap(image0);
74   RadioButton2->setPixmap(image2);
75   RadioButton3->close(TRUE);
76
77   GroupPoints = new DlgRef_2Sel_QTD( aFrame, "GroupPoints");
78   GroupPoints->GroupBox1->setTitle(tr("GEOM_POINTS"));
79   GroupPoints->TextLabel1->setText(tr("GEOM_POINT_I").arg("1"));
80   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_I").arg("2"));
81   GroupPoints->PushButton1->setPixmap(image1);
82   GroupPoints->PushButton2->setPixmap(image1);
83   GroupPoints->LineEdit1->setReadOnly( true );
84   GroupPoints->LineEdit2->setReadOnly( true );
85
86   GroupFaces = new DlgRef_2Sel_QTD( aFrame, "GroupFaces");
87   GroupFaces->GroupBox1->setTitle(tr("GEOM_FACES"));
88   GroupFaces->TextLabel1->setText(tr("GEOM_FACE_I").arg("1"));
89   GroupFaces->TextLabel2->setText(tr("GEOM_FACE_I").arg("2"));
90   GroupFaces->PushButton1->setPixmap(image1);
91   GroupFaces->PushButton2->setPixmap(image1);
92   GroupFaces->LineEdit1->setReadOnly( true );
93   GroupFaces->LineEdit2->setReadOnly( true );
94
95   Layout1->addWidget(aFrame, 2, 0);
96   /***************************************************************/
97
98   setHelpFileName("create_line_page.html");
99
100   Init();
101 }
102
103 //=================================================================================
104 // function : ~BasicGUI_LineDlg()
105 // purpose  : Destroys the object and frees any allocated resources
106 //=================================================================================
107 BasicGUI_LineDlg::~BasicGUI_LineDlg()
108 {  
109 }
110
111 //=================================================================================
112 // function : Init()
113 // purpose  :
114 //=================================================================================
115 void BasicGUI_LineDlg::Init()
116 {
117   /* init variables */
118   //myEditCurrentArgument = GroupPoints->LineEdit1;
119   //myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
120   //globalSelection(); // close local contexts, if any
121   //localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
122         
123   /* signals and slots connections */
124   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
125   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
126   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
127   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
128
129   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
130   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
131
132   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
133   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
134   connect(GroupFaces->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
135   connect(GroupFaces->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
136
137   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
138   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
139   connect(GroupFaces->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
140   connect(GroupFaces->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
141
142   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
143           this, SLOT(SelectionIntoArgument()));
144   
145   ConstructorsClicked(0);
146   initName( tr("GEOM_LINE") );
147 }
148
149 //=================================================================================
150 // function : ClickOnOk()
151 // purpose  :
152 //=================================================================================
153 void BasicGUI_LineDlg::ClickOnOk()
154 {
155   if ( ClickOnApply() )
156     ClickOnCancel();
157 }
158
159 //=================================================================================
160 // function : ClickOnCancel()
161 // purpose  :
162 //=================================================================================
163 void BasicGUI_LineDlg::ClickOnCancel()
164 {
165   GEOMBase_Skeleton::ClickOnCancel();
166 }
167
168 //=================================================================================
169 // function : ClickOnApply()
170 // purpose  :
171 //=================================================================================
172 bool BasicGUI_LineDlg::ClickOnApply()
173 {
174
175   if ( !onAccept() )
176     return false;
177
178   initName();
179   myEditCurrentArgument->setText("");
180   ConstructorsClicked(getConstructorId());
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   {
192   case 0:
193     {
194       globalSelection(); // close local contexts, if any
195       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
196
197       myEditCurrentArgument = GroupPoints->LineEdit1;
198       myEditCurrentArgument->setText("");
199       myPoint1 = GEOM::GEOM_Object::_nil();
200       myPoint2 = GEOM::GEOM_Object::_nil();
201       GroupPoints->show();
202       GroupFaces->hide();
203       break;
204     }
205   case 1:
206     {
207       globalSelection(); // close local contexts, if any
208       localSelection( GEOM::GEOM_Object::_nil(), TopAbs_FACE );
209
210       myEditCurrentArgument = GroupFaces->LineEdit1;
211       myEditCurrentArgument->setText("");
212       myFace1 = GEOM::GEOM_Object::_nil();
213       myFace2 = GEOM::GEOM_Object::_nil();
214       GroupPoints->hide();
215       GroupFaces->show();
216       break;
217     }
218   }
219
220   updateGeometry();
221   resize(minimumSize());
222   SelectionIntoArgument();
223 }
224
225 //=================================================================================
226 // function : SelectionIntoArgument()
227 // purpose  : Called when selection as changed or other case
228 //=================================================================================
229 void BasicGUI_LineDlg::SelectionIntoArgument()
230 {
231   myEditCurrentArgument->setText("");
232
233   if (IObjectCount() != 1) 
234   {
235     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint1 = GEOM::GEOM_Object::_nil();
236     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myPoint2 = GEOM::GEOM_Object::_nil();
237     else if ( myEditCurrentArgument == GroupFaces->LineEdit1 ) myFace1 = GEOM::GEOM_Object::_nil();
238     else if ( myEditCurrentArgument == GroupFaces->LineEdit2 ) myFace2 = GEOM::GEOM_Object::_nil();
239     return;
240   }
241
242   // nbSel == 1 
243   Standard_Boolean aRes = Standard_False;
244   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(firstIObject(), aRes);
245   if (!CORBA::is_nil(aSelectedObject) && aRes)
246   {
247     QString aName = GEOMBase::GetName(aSelectedObject);
248     
249     TopoDS_Shape aShape;
250     if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
251     {
252       TopAbs_ShapeEnum aNeedType = TopAbs_VERTEX;
253       if (myEditCurrentArgument == GroupFaces->LineEdit1 ||
254           myEditCurrentArgument == GroupFaces->LineEdit2)
255         aNeedType = TopAbs_FACE;
256
257       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
258       TColStd_IndexedMapOfInteger aMap;
259       aSelMgr->GetIndexes(firstIObject(), aMap);
260       if (aMap.Extent() == 1) // Local Selection
261       {
262         int anIndex = aMap( 1 );
263         if (aNeedType == TopAbs_FACE)
264           aName += QString(":face_%1").arg(anIndex);
265         else
266           aName += QString(":vertex_%1").arg(anIndex);
267
268         //Find SubShape Object in Father
269         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
270
271         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
272           GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
273           aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
274         }
275         else
276           aSelectedObject = aFindedObject; // get Object from study
277       }
278       else // Global Selection
279       {
280         if (aShape.ShapeType() != aNeedType) {
281           aSelectedObject = GEOM::GEOM_Object::_nil();
282           aName = "";
283         }
284       }
285     }
286
287     myEditCurrentArgument->setText(aName);
288
289     if      ( myEditCurrentArgument == GroupPoints->LineEdit1 ) myPoint1 = aSelectedObject;
290     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) myPoint2 = aSelectedObject;
291     else if ( myEditCurrentArgument == GroupFaces->LineEdit1 )  myFace1 = aSelectedObject;
292     else if ( myEditCurrentArgument == GroupFaces->LineEdit2 )  myFace2 = aSelectedObject;
293   }
294   
295   displayPreview();
296 }
297
298 //=================================================================================
299 // function : SetEditCurrentArgument()
300 // purpose  :
301 //=================================================================================
302 void BasicGUI_LineDlg::SetEditCurrentArgument()
303 {
304   QPushButton* send = (QPushButton*)sender();
305   if      ( send == GroupPoints->PushButton1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
306   else if ( send == GroupPoints->PushButton2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
307   else if ( send == GroupFaces->PushButton1 ) myEditCurrentArgument = GroupFaces->LineEdit1;
308   else if ( send == GroupFaces->PushButton2 ) myEditCurrentArgument = GroupFaces->LineEdit2;
309   myEditCurrentArgument->setFocus();
310   SelectionIntoArgument();
311 }
312
313 //=================================================================================
314 // function : LineEditReturnPressed()
315 // purpose  :
316 //=================================================================================
317 void BasicGUI_LineDlg::LineEditReturnPressed()
318 {
319   QLineEdit* send = (QLineEdit*)sender();
320   if      ( send == GroupPoints->LineEdit1 ) myEditCurrentArgument = GroupPoints->LineEdit1;
321   else if ( send == GroupPoints->LineEdit2 ) myEditCurrentArgument = GroupPoints->LineEdit2;
322   else if ( send == GroupFaces->LineEdit1 ) myEditCurrentArgument = GroupFaces->LineEdit1;
323   else if ( send == GroupFaces->LineEdit2 ) myEditCurrentArgument = GroupFaces->LineEdit2;
324   else return;
325   GEOMBase_Skeleton::LineEditReturnPressed();
326 }
327
328 //=================================================================================
329 // function : ActivateThisDialog()
330 // purpose  :
331 //=================================================================================
332 void BasicGUI_LineDlg::ActivateThisDialog()
333 {
334   GEOMBase_Skeleton::ActivateThisDialog();
335   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
336           this, SLOT(SelectionIntoArgument()));
337
338   ConstructorsClicked(getConstructorId());
339 }
340
341 //=================================================================================
342 // function : DeactivateActiveDialog()
343 // purpose  : public slot to deactivate if active
344 //=================================================================================
345 void BasicGUI_LineDlg::DeactivateActiveDialog()
346 {
347   GEOMBase_Skeleton::DeactivateActiveDialog();
348 }
349
350 //=================================================================================
351 // function : enterEvent()
352 // purpose  :
353 //=================================================================================
354 void BasicGUI_LineDlg::enterEvent(QEvent* e)
355 {
356   if ( !GroupConstructors->isEnabled() )
357     ActivateThisDialog();
358 }
359
360 //=================================================================================
361 // function : createOperation
362 // purpose  :
363 //=================================================================================
364 GEOM::GEOM_IOperations_ptr BasicGUI_LineDlg::createOperation()
365 {
366   return myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
367 }
368
369 //=================================================================================
370 // function : isValid
371 // purpose  :
372 //=================================================================================
373 bool BasicGUI_LineDlg::isValid( QString& msg )
374 {
375   const int id = getConstructorId();
376   if ( id == 0 )
377     return !myPoint1->_is_nil() && !myPoint2->_is_nil();
378   else if ( id == 1 )
379     return !myFace1->_is_nil() && !myFace2->_is_nil();
380   return false;
381 }
382
383 //=================================================================================
384 // function : execute
385 // purpose  :
386 //=================================================================================
387 bool BasicGUI_LineDlg::execute( ObjectList& objects )
388 {
389   bool res = false;
390   GEOM::GEOM_Object_var anObj;
391   switch ( getConstructorId() )
392     {
393     case 0 :
394       {
395         anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeLineTwoPnt( myPoint1, myPoint2 );
396         res = true;
397         break;
398       }
399     case 1 :
400       {
401         anObj = GEOM::GEOM_IBasicOperations::_narrow( getOperation() )->MakeLineTwoFaces( myFace1, myFace2 );
402         res = true;
403         break;
404       }
405     }
406   if ( !anObj->_is_nil() )
407     objects.push_back( anObj._retn() );
408
409   return res;
410 }
411
412 //=================================================================================
413 // function : closeEvent
414 // purpose  :
415 //=================================================================================
416 void BasicGUI_LineDlg::closeEvent( QCloseEvent* e )
417 {
418   GEOMBase_Skeleton::closeEvent( e );
419 }
420
421 //=================================================================================
422 // function : addSubshapeToStudy
423 // purpose  : virtual method to add new SubObjects if local selection
424 //=================================================================================
425 void BasicGUI_LineDlg::addSubshapesToStudy()
426 {
427   QMap<QString, GEOM::GEOM_Object_var> objMap;
428   switch ( getConstructorId() )
429     {
430     case 0 :
431       objMap[GroupPoints->LineEdit1->text()] = myPoint1;
432       objMap[GroupPoints->LineEdit2->text()] = myPoint2;
433       break;
434     case 1 :
435       objMap[GroupFaces->LineEdit1->text()] = myFace1;
436       objMap[GroupFaces->LineEdit2->text()] = myFace2;
437       break;
438     }
439   addSubshapesToFather( objMap );
440 }