Salome HOME
Merge from V5_1_4_BR 07/05/2010
[modules/geom.git] / src / RepairGUI / RepairGUI_DivideEdgeDlg.cxx
1 //  Copyright (C) 2007-2010  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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : RepairGUI_DivideEdgeDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "RepairGUI_DivideEdgeDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32 #include <SalomeApp_DoubleSpinBox.h>
33
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_ResourceMgr.h>
38
39 #include <GEOMImpl_Types.hxx>
40
41 #include <TopAbs.hxx>
42 #include <Geom_Curve.hxx>
43 #include <gp_Pnt.hxx>
44 #include <TopoDS.hxx>
45 #include <TopExp.hxx>
46 #include <TopoDS_Edge.hxx>
47 #include <BRep_Tool.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <BRepBuilderAPI_MakeVertex.hxx>
50 #include <TColStd_IndexedMapOfInteger.hxx>
51
52 //=================================================================================
53 // class    : RepairGUI_DivideEdgeDlg()
54 // purpose  : Constructs a RepairGUI_DivideEdgeDlg  which is a child of 'parent', with the
55 //            name 'name' and widget flags set to 'f'.
56 //            The dialog will by default be modeless, unless you set 'modal' to
57 //            TRUE to construct a modal dialog.
58 //=================================================================================
59 RepairGUI_DivideEdgeDlg::RepairGUI_DivideEdgeDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
60                                                   bool modal )
61   : GEOMBase_Skeleton( theGeometryGUI, parent, modal )
62 {
63   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_DIVIDE_EDGE" ) ) );
64   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
65
66   setWindowTitle( tr( "GEOM_DIVIDE_EDGE_TITLE" ) );
67
68   /***************************************************************/
69   mainFrame()->GroupConstructors->setTitle(tr("GEOM_DIVIDE_EDGE_TITLE"));
70   mainFrame()->RadioButton1->setIcon( image0 );
71   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
72   mainFrame()->RadioButton2->close();
73   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
74   mainFrame()->RadioButton3->close();
75
76   GroupPoints = new DlgRef_1SelExt( centralWidget() );
77   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ADD_POINT" ) );
78   GroupPoints->TextLabel1->setText( tr( "GEOM_EDGE" ) );
79   GroupPoints->PushButton1->setIcon( image1 );
80   GroupPoints->LineEdit1->setReadOnly( true );
81
82   QRadioButton* rb1 = new QRadioButton( tr( "GEOM_BY_PARAMETER" ), GroupPoints->Box );
83   QRadioButton* rb2 = new QRadioButton( tr( "GEOM_BY_LENGTH" ),    GroupPoints->Box );
84
85   myIsParameterGr = new QButtonGroup( GroupPoints->Box );
86   myIsParameterGr->addButton( rb1, 0 );
87   myIsParameterGr->addButton( rb2, 1 );
88   rb1->setChecked( true );
89
90   myValEdt = new SalomeApp_DoubleSpinBox( GroupPoints->Box );
91   initSpinBox( myValEdt, 0., 1., 0.1, "parametric_precision" );
92   myValEdt->setValue( 0.5 );
93   QLabel* aLbl1 = new QLabel( tr( "GEOM_VALUE" ), GroupPoints->Box );
94
95   QGridLayout* l = new QGridLayout( GroupPoints->Box );
96   l->setMargin( 0 ); l->setSpacing( 6 );
97   l->addWidget( rb1, 0, 0, 1, 2 );
98   l->addWidget( rb2, 1, 0, 1, 2 );
99   l->addWidget( aLbl1, 2, 0 );
100   l->addWidget( myValEdt, 2, 1 );
101
102   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
103   layout->setMargin( 0 ); layout->setSpacing( 6 );
104   layout->addWidget( GroupPoints );
105
106   /***************************************************************/
107
108   setHelpFileName( "add_point_on_edge_operation_page.html" );
109
110   Init();
111 }
112
113 //=================================================================================
114 // function : ~RepairGUI_DivideEdgeDlg()
115 // purpose  : Destroys the object and frees any allocated resources
116 //=================================================================================
117 RepairGUI_DivideEdgeDlg::~RepairGUI_DivideEdgeDlg()
118 {
119 }
120
121 //=================================================================================
122 // function : Init()
123 // purpose  :
124 //=================================================================================
125 void RepairGUI_DivideEdgeDlg::Init()
126 {
127   /* init variables */
128   myEditCurrentArgument = GroupPoints->LineEdit1;
129
130   myObject = GEOM::GEOM_Object::_nil();
131   myIndex = -1;
132
133   //myGeomGUI->SetState( 0 );
134   initSelection();
135
136   /* signals and slots connections */
137   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
138   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
139
140   connect( myValEdt,   SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
141
142   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
143   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
144
145   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
146            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
147
148   initName( tr( "DEVIDE_EDGE_NEW_OBJECT_NAME" ) );
149   resize(100,100);
150 }
151
152 //=================================================================================
153 // function : ValueChangedInSpinBox()
154 // purpose  : On change value in spin box
155 //=================================================================================
156 void RepairGUI_DivideEdgeDlg::ValueChangedInSpinBox()
157 {
158   displayPreview();
159 }
160
161 //=================================================================================
162 // function : displayPreview()
163 // purpose  : On display Preview
164 //=================================================================================
165 void RepairGUI_DivideEdgeDlg::displayPreview()
166 {
167   erasePreview();
168   if ( myObject->_is_nil() )
169     return;
170
171   TopoDS_Shape aShape;
172   gp_Pnt aPnt;
173   GEOMBase::GetShape( myObject, aShape, TopAbs_SHAPE );
174
175   if ( myIndex != -1) {
176     TopTools_IndexedMapOfShape aShapes;
177     TopExp::MapShapes(aShape, aShapes);
178     aShape = aShapes.FindKey(myIndex);
179   }
180
181   if (aShape.ShapeType() == TopAbs_EDGE) {
182     Standard_Real aFP, aLP, aP;
183     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFP, aLP);
184     aP = aFP + (aLP - aFP) * myValEdt->value();
185     aPnt = aCurve->Value(aP);
186     BRepBuilderAPI_MakeVertex mkVertex (aPnt);
187     aShape = mkVertex.Shape();
188     // Build prs
189     SALOME_Prs* aPrs = getDisplayer()->BuildPrs( aShape );
190     if ( aPrs != 0 && !aPrs->IsNull() )
191       GEOMBase_Helper::displayPreview( aPrs, false, true );
192   } 
193 }
194
195 //=================================================================================
196 // function : ClickOnOk()
197 // purpose  : Same than click on apply but close this dialog.
198 //=================================================================================
199 void RepairGUI_DivideEdgeDlg::ClickOnOk()
200 {
201   if ( ClickOnApply() )
202     ClickOnCancel();
203 }
204
205
206 //=================================================================================
207 // function : ClickOnApply()
208 // purpose  :
209 //=================================================================================
210 bool RepairGUI_DivideEdgeDlg::ClickOnApply()
211 {
212   if ( !onAccept() )
213     return false;
214
215   initName();
216
217   myEditCurrentArgument->setText( "" );
218   myObject = GEOM::GEOM_Object::_nil();
219   myIndex = -1;
220
221   initSelection();
222
223   return true;
224 }
225
226
227 //=================================================================================
228 // function : SelectionIntoArgument()
229 // purpose  : Called when selection as changed or other case
230 //          : used only by SelectButtonC1A1 (LineEditC1A1)
231 //=================================================================================
232 void RepairGUI_DivideEdgeDlg::SelectionIntoArgument()
233 {
234   myEditCurrentArgument->setText( "" );
235
236   myObject = GEOM::GEOM_Object::_nil();
237   myIndex = -1;
238
239   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
240   SALOME_ListIO aSelList;
241   aSelMgr->selectedObjects(aSelList);
242
243   if ( aSelList.Extent() == 1 ) {
244     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
245     Standard_Boolean aRes;
246     GEOM::GEOM_Object_var aSelectedObj = GEOMBase::ConvertIOinGEOMObject( anIO, aRes );
247     if ( !CORBA::is_nil( aSelectedObj ) && aRes )
248     {
249       TopoDS_Shape aShape;
250       QString aName = GEOMBase::GetName( aSelectedObj );
251       if ( GEOMBase::GetShape( aSelectedObj, aShape, TopAbs_SHAPE ) )
252       {
253         const int aType = aShape.ShapeType();
254         if ( aType <= TopAbs_EDGE ) // edge, wire, face, shell, solid, compound
255         {
256           GEOM::short_array anIndexes;
257
258           TColStd_IndexedMapOfInteger aMap;
259           SalomeApp_Application* anApp =
260             (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
261           anApp->selectionMgr()->GetIndexes( anIO, aMap );
262
263           if ( !aMap.IsEmpty() ) // subshape selection
264           {
265             myIndex = aMap( 1 );
266             myObject = aSelectedObj;
267             myEditCurrentArgument->setText( aName += QString( ":edge_%1" ).arg( myIndex ) );
268           }
269           else if ( aType == TopAbs_EDGE ) // single shape selection
270           {
271             myIndex = -1;
272             myObject = aSelectedObj;
273             myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
274           }
275           else // face, shell, solid or compound was selected, and NOT its subshape.
276           {
277             myIndex = -1;
278             myObject = GEOM::GEOM_Object::_nil();
279           }
280         }
281       }
282     }
283   }
284
285   displayPreview();
286 }
287
288 //=================================================================================
289 // function : SetEditCurrentArgument()
290 // purpose  :
291 //=================================================================================
292 void RepairGUI_DivideEdgeDlg::SetEditCurrentArgument()
293 {
294   if ( sender() == GroupPoints->PushButton1 ) {
295     GroupPoints->LineEdit1->setFocus();
296     myEditCurrentArgument = GroupPoints->LineEdit1;
297   }
298   SelectionIntoArgument();
299 }
300
301
302 //=================================================================================
303 // function : LineEditReturnPressed()
304 // purpose  :
305 //=================================================================================
306 void RepairGUI_DivideEdgeDlg::LineEditReturnPressed()
307 {
308   if ( sender() == GroupPoints->LineEdit1 ) {
309     myEditCurrentArgument = GroupPoints->LineEdit1;
310     GEOMBase_Skeleton::LineEditReturnPressed();
311   }
312 }
313
314
315 //=================================================================================
316 // function : ActivateThisDialog()
317 // purpose  :
318 //=================================================================================
319 void RepairGUI_DivideEdgeDlg::ActivateThisDialog()
320 {
321   GEOMBase_Skeleton::ActivateThisDialog();
322   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
323            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
324
325 //  myObject = GEOM::GEOM_Object::_nil();
326   myIndex = -1;
327
328   //myGeomGUI->SetState( 0 );
329   initSelection();
330 }
331
332 //=================================================================================
333 // function : enterEvent()
334 // purpose  : Mouse enter onto the dialog to activate it
335 //=================================================================================
336 void RepairGUI_DivideEdgeDlg::enterEvent( QEvent* )
337 {
338   if ( !mainFrame()->GroupConstructors->isEnabled() )
339     ActivateThisDialog();
340 }
341
342
343 //=================================================================================
344 // function : createOperation
345 // purpose  :
346 //=================================================================================
347 GEOM::GEOM_IOperations_ptr RepairGUI_DivideEdgeDlg::createOperation()
348 {
349   return getGeomEngine()->GetIHealingOperations( getStudyId() );
350 }
351
352 //=================================================================================
353 // function : isValid
354 // purpose  :
355 //=================================================================================
356 bool RepairGUI_DivideEdgeDlg::isValid( QString& msg )
357 {
358   bool ok = myValEdt->isValid( msg, !IsPreview() );
359   return !myObject->_is_nil() && ok;
360 }
361
362 //=================================================================================
363 // function : execute
364 // purpose  :
365 //=================================================================================
366 bool RepairGUI_DivideEdgeDlg::execute( ObjectList& objects )
367 {
368   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow( getOperation() );
369   GEOM::GEOM_Object_var anObj = anOper->DivideEdge( myObject, myIndex, myValEdt->value(), getIsByParameter() );
370   bool aResult = !anObj->_is_nil();
371   if ( aResult )
372   {
373     if ( !IsPreview() )
374     {
375       QStringList aParameters;
376       aParameters << "";
377       aParameters << myValEdt->text();
378       aParameters << "";
379       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
380     }
381     objects.push_back( anObj._retn() );
382   }
383
384   return aResult;
385 }
386
387 //=================================================================================
388 // function : getIsByParameter
389 // purpose  :
390 //=================================================================================
391 bool RepairGUI_DivideEdgeDlg::getIsByParameter() const
392 {
393   return myIsParameterGr->button( 0 )->isChecked();
394 }
395
396 //=================================================================================
397 // function : initSelection
398 // purpose  :
399 //=================================================================================
400 void RepairGUI_DivideEdgeDlg::initSelection()
401 {
402   GEOM::GEOM_Object_var aNullGeomObject;
403   localSelection( aNullGeomObject, TopAbs_EDGE ); // load local selection on ALL objects
404 }