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