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