Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_17Jan08)
[modules/geom.git] / src / GenerationGUI / GenerationGUI_PipeDlg.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   : GenerationGUI_PipeDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "GenerationGUI_PipeDlg.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    : GenerationGUI_PipeDlg()
47 // purpose  : Constructs a GenerationGUI_PipeDlg 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 GenerationGUI_PipeDlg::GenerationGUI_PipeDlg( 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_PIPE" ) ) );
57   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
58
59   setWindowTitle( tr( "GEOM_PIPE_TITLE" ) );
60
61   /***************************************************************/
62   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_PIPE" ) );
63   mainFrame()->RadioButton1->setIcon(image0);
64   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
65   mainFrame()->RadioButton2->close();
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_ARGUMENTS" ) );
72   GroupPoints->TextLabel1->setText( tr( "GEOM_BASE_OBJECT" ) );
73   GroupPoints->TextLabel2->setText( tr( "GEOM_PATH_OBJECT" ) );
74   GroupPoints->PushButton1->setIcon( image1 );
75   GroupPoints->PushButton2->setIcon( image1 );
76   GroupPoints->LineEdit1->setReadOnly( true );
77   GroupPoints->LineEdit2->setReadOnly( true );
78
79   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
80   layout->setMargin( 0 ); layout->setSpacing( 6 );
81   layout->addWidget( GroupPoints );
82   /***************************************************************/
83
84   setHelpFileName( "create_extrusion_alongpath_page.html" );
85
86   /* Initialisations */
87   Init();
88 }
89
90
91 //=================================================================================
92 // function : ~GenerationGUI_PipeDlg()
93 // purpose  : Destroys the object and frees any allocated resources
94 //=================================================================================
95 GenerationGUI_PipeDlg::~GenerationGUI_PipeDlg()
96 {
97   /* no need to delete child widgets, Qt does it all for us */
98 }
99
100
101 //=================================================================================
102 // function : Init()
103 // purpose  :
104 //=================================================================================
105 void GenerationGUI_PipeDlg::Init()
106 {
107   /* init variables */
108   myEditCurrentArgument = GroupPoints->LineEdit1;
109   GroupPoints->LineEdit1->setReadOnly( true );
110   GroupPoints->LineEdit2->setReadOnly( true );
111
112   myOkBase = myOkPath = false;
113
114   /* signals and slots connections */
115   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
116   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
117
118   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
119   connect( GroupPoints->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
120
121   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
122   connect( GroupPoints->LineEdit2,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
123   
124   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
125            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
126
127   initName( tr( "GEOM_PIPE" ) );
128
129   //globalSelection( GEOM_ALLSHAPES );
130 }
131
132
133 //=================================================================================
134 // function : ClickOnOk()
135 // purpose  :
136 //=================================================================================
137 void GenerationGUI_PipeDlg::ClickOnOk()
138 {
139   if ( ClickOnApply() )
140     ClickOnCancel();
141 }
142
143
144 //=================================================================================
145 // function : ClickOnAply()
146 // purpose  :
147 //=================================================================================
148 bool GenerationGUI_PipeDlg::ClickOnApply()
149 {
150   if ( !onAccept() )
151     return false;
152
153   initName();
154   return true;
155 }
156
157
158 //=================================================================================
159 // function : SelectionIntoArgument()
160 // purpose  : Called when selection has changed
161 //=================================================================================
162 void GenerationGUI_PipeDlg::SelectionIntoArgument()
163 {
164   erasePreview();
165   myEditCurrentArgument->setText( "" );
166   
167   if ( IObjectCount() != 1 ) {
168     if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
169       myOkBase = false;
170     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
171       myOkPath = false;
172     return;
173   }
174   
175   // nbSel == 1
176   Standard_Boolean testResult = Standard_False;
177   GEOM::GEOM_Object_ptr aSelectedObject = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
178     
179   if ( !testResult )
180     return;
181   
182   TopoDS_Shape S;
183   
184   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
185     myOkBase = false;
186     
187     if ( !GEOMBase::GetShape( aSelectedObject, S ) ||
188          S.ShapeType() == TopAbs_COMPSOLID || 
189          S.ShapeType() == TopAbs_COMPOUND || 
190          S.ShapeType() == TopAbs_SOLID || 
191          S.ShapeType() == TopAbs_SHAPE ) 
192       return;
193     
194     myBase = aSelectedObject;
195     myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
196     myOkBase = true;
197   }
198   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
199     myOkPath = false;
200     
201     if ( !GEOMBase::GetShape( aSelectedObject, S ) ) 
202       return;
203
204     QString aName = GEOMBase::GetName( aSelectedObject );
205     
206     if ( testResult && !aSelectedObject->_is_nil() && aSelectedObject != myBase ) {
207       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
208       TColStd_IndexedMapOfInteger aMap;
209       
210       aSelMgr->GetIndexes( firstIObject(), aMap );
211       if ( aMap.Extent() == 1 ) {
212         int anIndex = aMap( 1 );
213         aName.append( ":edge_" + QString::number( anIndex ) );
214         
215         //Find SubShape Object in Father
216         GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aName );
217         
218         if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
219           GEOM::GEOM_IShapesOperations_var aShapesOp =
220             getGeomEngine()->GetIShapesOperations( getStudyId() );
221           myPath = aShapesOp->GetSubShape( aSelectedObject, anIndex );
222           myOkPath = true;
223         }
224         else {  // get Object from study
225           myPath = aFindedObject;
226           myOkPath = true;
227         }
228       }
229       else {
230         myOkPath = true;
231         if ( S.ShapeType() != TopAbs_EDGE ) {
232           aSelectedObject = GEOM::GEOM_Object::_nil();
233           aName = "";
234           myOkPath = false;
235         }
236         myPath = aSelectedObject;
237       }
238     }
239     myEditCurrentArgument->setText( aName );
240   }
241   
242   displayPreview();
243 }
244
245
246 //=================================================================================
247 // function : SetEditCurrentArgument()
248 // purpose  :
249 //=================================================================================
250 void GenerationGUI_PipeDlg::SetEditCurrentArgument()
251 {
252   QPushButton* send = (QPushButton*)sender();
253   globalSelection();
254
255   if ( send == GroupPoints->PushButton1 ) {
256     GroupPoints->LineEdit1->setFocus();
257     globalSelection( GEOM_ALLSHAPES );
258     myEditCurrentArgument = GroupPoints->LineEdit1;
259   }
260   else if ( send == GroupPoints->PushButton2 ) {
261     GroupPoints->LineEdit2->setFocus();
262     myEditCurrentArgument = GroupPoints->LineEdit2;
263     globalSelection();
264     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
265   }
266   SelectionIntoArgument();
267 }
268
269
270 //=================================================================================
271 // function : LineEditReturnPressed()
272 // purpose  :
273 //=================================================================================
274 void GenerationGUI_PipeDlg::LineEditReturnPressed()
275 {
276   QLineEdit* send = (QLineEdit*)sender();
277   if ( send == GroupPoints->LineEdit1 ||
278        send == GroupPoints->LineEdit2 ) {
279     myEditCurrentArgument = send;
280     GEOMBase_Skeleton::LineEditReturnPressed();
281   }
282 }
283
284
285 //=================================================================================
286 // function : enterEvent()
287 // purpose  : when mouse enter onto the QWidget
288 //=================================================================================
289 void GenerationGUI_PipeDlg::enterEvent( QEvent* )
290 {
291   if ( !mainFrame()->GroupConstructors->isEnabled() )
292     ActivateThisDialog();
293 }
294
295
296 //=================================================================================
297 // function : ActivateThisDialog()
298 // purpose  :
299 //=================================================================================
300 void GenerationGUI_PipeDlg::ActivateThisDialog()
301 {
302   GEOMBase_Skeleton::ActivateThisDialog();
303   globalSelection( GEOM_ALLSHAPES );
304   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
305            SIGNAL( currentSelectionChanged() ), this, SLOT(SelectionIntoArgument() ) );
306   displayPreview();
307 }
308
309
310 //=================================================================================
311 // function : createOperation
312 // purpose  :
313 //=================================================================================
314 GEOM::GEOM_IOperations_ptr GenerationGUI_PipeDlg::createOperation()
315 {
316   return getGeomEngine()->GetI3DPrimOperations( getStudyId() );
317 }
318
319 //=================================================================================
320 // function : isValid
321 // purpose  :
322 //=================================================================================
323 bool GenerationGUI_PipeDlg::isValid( QString& )
324 {
325   return myOkBase && myOkPath;
326 }
327
328 //=================================================================================
329 // function : execute
330 // purpose  :
331 //=================================================================================
332 bool GenerationGUI_PipeDlg::execute( ObjectList& objects )
333 {
334   GEOM::GEOM_Object_var anObj;
335
336   anObj = GEOM::GEOM_I3DPrimOperations::_narrow(
337     getOperation() )->MakePipe( myBase, myPath );
338
339   if ( !anObj->_is_nil() )
340     objects.push_back( anObj._retn() );
341
342   return true;
343 }
344
345
346 //=================================================================================
347 // function : addSubshapeToStudy
348 // purpose  : virtual method to add new SubObjects if local selection
349 //=================================================================================
350 void GenerationGUI_PipeDlg::addSubshapesToStudy()
351 {
352   QMap<QString, GEOM::GEOM_Object_var> objMap;
353
354   objMap[GroupPoints->LineEdit2->text()] = myPath;
355
356   addSubshapesToFather( objMap );
357 }
358