Salome HOME
Synchronize adm files
[modules/geom.git] / src / RepairGUI / RepairGUI_DivideEdgeDlg.cxx
1 // Copyright (C) 2007-2014  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   : 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   SelectionIntoArgument();
151 }
152
153 //=================================================================================
154 // function : ValueChangedInSpinBox()
155 // purpose  : On change value in spin box
156 //=================================================================================
157 void RepairGUI_DivideEdgeDlg::ValueChangedInSpinBox()
158 {
159   displayPreview();
160 }
161
162 //=================================================================================
163 // function : displayPreview()
164 // purpose  : On display Preview
165 //=================================================================================
166 void RepairGUI_DivideEdgeDlg::displayPreview()
167 {
168   erasePreview();
169   if ( myObject->_is_nil() )
170     return;
171
172   TopoDS_Shape aShape;
173   gp_Pnt aPnt;
174   GEOMBase::GetShape( myObject, aShape, TopAbs_SHAPE );
175
176   if ( myIndex != -1) {
177     TopTools_IndexedMapOfShape aShapes;
178     TopExp::MapShapes(aShape, aShapes);
179     aShape = aShapes.FindKey(myIndex);
180   }
181
182   if (aShape.ShapeType() == TopAbs_EDGE) {
183     Standard_Real aFP, aLP, aP;
184     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFP, aLP);
185     aP = aFP + (aLP - aFP) * myValEdt->value();
186     aPnt = aCurve->Value(aP);
187     BRepBuilderAPI_MakeVertex mkVertex (aPnt);
188     aShape = mkVertex.Shape();
189     // Build prs
190     SALOME_Prs* aPrs = getDisplayer()->BuildPrs( aShape );
191     if ( aPrs != 0 && !aPrs->IsNull() )
192       GEOMBase_Helper::displayPreview( aPrs, false, true );
193   } 
194 }
195
196 //=================================================================================
197 // function : ClickOnOk()
198 // purpose  : Same than click on apply but close this dialog.
199 //=================================================================================
200 void RepairGUI_DivideEdgeDlg::ClickOnOk()
201 {
202   setIsApplyAndClose( true );
203   if ( ClickOnApply() )
204     ClickOnCancel();
205 }
206
207
208 //=================================================================================
209 // function : ClickOnApply()
210 // purpose  :
211 //=================================================================================
212 bool RepairGUI_DivideEdgeDlg::ClickOnApply()
213 {
214   if ( !onAccept() )
215     return false;
216
217   initName();
218
219   myEditCurrentArgument->setText( "" );
220   myObject = GEOM::GEOM_Object::_nil();
221   myIndex = -1;
222
223   initSelection();
224
225   return true;
226 }
227
228
229 //=================================================================================
230 // function : SelectionIntoArgument()
231 // purpose  : Called when selection as changed or other case
232 //          : used only by SelectButtonC1A1 (LineEditC1A1)
233 //=================================================================================
234 void RepairGUI_DivideEdgeDlg::SelectionIntoArgument()
235 {
236   myEditCurrentArgument->setText( "" );
237
238   myObject = GEOM::GEOM_Object::_nil();
239   myIndex = -1;
240
241   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
242   SALOME_ListIO aSelList;
243   aSelMgr->selectedObjects(aSelList);
244
245   if ( aSelList.Extent() == 1 ) {
246     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
247     GEOM::GEOM_Object_var aSelectedObj = GEOMBase::ConvertIOinGEOMObject( anIO );
248     if ( !CORBA::is_nil( aSelectedObj ) )
249     {
250       TopoDS_Shape aShape;
251       QString aName = GEOMBase::GetName( aSelectedObj );
252       if ( GEOMBase::GetShape( aSelectedObj, aShape, TopAbs_SHAPE ) )
253       {
254         const int aType = aShape.ShapeType();
255         if ( aType <= TopAbs_EDGE ) // edge, wire, face, shell, solid, compound
256         {
257           GEOM::short_array anIndexes;
258
259           TColStd_IndexedMapOfInteger aMap;
260           SalomeApp_Application* anApp =
261             (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
262           anApp->selectionMgr()->GetIndexes( anIO, aMap );
263
264           if ( !aMap.IsEmpty() ) // sub-shape selection
265           {
266             myIndex = aMap( 1 );
267             myObject = aSelectedObj;
268             myEditCurrentArgument->setText( aName += QString( ":edge_%1" ).arg( myIndex ) );
269           }
270           else if ( aType == TopAbs_EDGE ) // single shape selection
271           {
272             myIndex = -1;
273             myObject = aSelectedObj;
274             myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
275           }
276           else // face, shell, solid or compound was selected, and NOT its sub-shape.
277           {
278             myIndex = -1;
279             myObject = GEOM::GEOM_Object::_nil();
280           }
281         }
282       }
283     }
284   }
285
286   displayPreview();
287 }
288
289 //=================================================================================
290 // function : SetEditCurrentArgument()
291 // purpose  :
292 //=================================================================================
293 void RepairGUI_DivideEdgeDlg::SetEditCurrentArgument()
294 {
295   if ( sender() == GroupPoints->PushButton1 ) {
296     GroupPoints->LineEdit1->setFocus();
297     myEditCurrentArgument = GroupPoints->LineEdit1;
298   }
299   SelectionIntoArgument();
300 }
301
302
303 //=================================================================================
304 // function : LineEditReturnPressed()
305 // purpose  :
306 //=================================================================================
307 void RepairGUI_DivideEdgeDlg::LineEditReturnPressed()
308 {
309   if ( sender() == GroupPoints->LineEdit1 ) {
310     myEditCurrentArgument = GroupPoints->LineEdit1;
311     GEOMBase_Skeleton::LineEditReturnPressed();
312   }
313 }
314
315
316 //=================================================================================
317 // function : ActivateThisDialog()
318 // purpose  :
319 //=================================================================================
320 void RepairGUI_DivideEdgeDlg::ActivateThisDialog()
321 {
322   GEOMBase_Skeleton::ActivateThisDialog();
323   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
324            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
325
326 //  myObject = GEOM::GEOM_Object::_nil();
327   myIndex = -1;
328
329   //myGeomGUI->SetState( 0 );
330   initSelection();
331 }
332
333 //=================================================================================
334 // function : enterEvent()
335 // purpose  : Mouse enter onto the dialog to activate it
336 //=================================================================================
337 void RepairGUI_DivideEdgeDlg::enterEvent( QEvent* )
338 {
339   if ( !mainFrame()->GroupConstructors->isEnabled() )
340     ActivateThisDialog();
341 }
342
343
344 //=================================================================================
345 // function : createOperation
346 // purpose  :
347 //=================================================================================
348 GEOM::GEOM_IOperations_ptr RepairGUI_DivideEdgeDlg::createOperation()
349 {
350   return getGeomEngine()->GetIHealingOperations( getStudyId() );
351 }
352
353 //=================================================================================
354 // function : isValid
355 // purpose  :
356 //=================================================================================
357 bool RepairGUI_DivideEdgeDlg::isValid( QString& msg )
358 {
359   bool ok = myValEdt->isValid( msg, !IsPreview() );
360   return !myObject->_is_nil() && ok;
361 }
362
363 //=================================================================================
364 // function : execute
365 // purpose  :
366 //=================================================================================
367 bool RepairGUI_DivideEdgeDlg::execute( ObjectList& objects )
368 {
369   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow( getOperation() );
370   GEOM::GEOM_Object_var anObj = anOper->DivideEdge( myObject, myIndex, myValEdt->value(), getIsByParameter() );
371   bool aResult = !anObj->_is_nil();
372   if ( aResult )
373   {
374     if ( !IsPreview() )
375     {
376       QStringList aParameters;
377       aParameters << "";
378       aParameters << myValEdt->text();
379       aParameters << "";
380       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
381     }
382     objects.push_back( anObj._retn() );
383   }
384
385   return aResult;
386 }
387
388 //=================================================================================
389 // function : getIsByParameter
390 // purpose  :
391 //=================================================================================
392 bool RepairGUI_DivideEdgeDlg::getIsByParameter() const
393 {
394   return myIsParameterGr->button( 0 )->isChecked();
395 }
396
397 //=================================================================================
398 // function : initSelection
399 // purpose  :
400 //=================================================================================
401 void RepairGUI_DivideEdgeDlg::initSelection()
402 {
403   GEOM::GEOM_Object_var aNullGeomObject;
404   localSelection( aNullGeomObject, TopAbs_EDGE ); // load local selection on ALL objects
405 }