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