Salome HOME
Issue 0020580: get rid of QtxDoubleSpinBox class and use SalomeApp_DoubleSpinBox...
[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 <SalomeApp_DoubleSpinBox.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( GroupPoints->Box );
90   initSpinBox( myValEdt, 0., 1., 0.1, "parametric_precision" );
91   myValEdt->setValue( 0.5 );
92   QLabel* aLbl1 = new QLabel( tr( "GEOM_VALUE" ), GroupPoints->Box );
93
94   QGridLayout* l = new QGridLayout( GroupPoints->Box );
95   l->setMargin( 0 ); l->setSpacing( 6 );
96   l->addWidget( rb1, 0, 0, 1, 2 );
97   l->addWidget( rb2, 1, 0, 1, 2 );
98   l->addWidget( aLbl1, 2, 0 );
99   l->addWidget( myValEdt, 2, 1 );
100
101   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
102   layout->setMargin( 0 ); layout->setSpacing( 6 );
103   layout->addWidget( GroupPoints );
104
105   /***************************************************************/
106
107   setHelpFileName( "add_point_on_edge_operation_page.html" );
108
109   Init();
110 }
111
112 //=================================================================================
113 // function : ~RepairGUI_DivideEdgeDlg()
114 // purpose  : Destroys the object and frees any allocated resources
115 //=================================================================================
116 RepairGUI_DivideEdgeDlg::~RepairGUI_DivideEdgeDlg()
117 {
118 }
119
120 //=================================================================================
121 // function : Init()
122 // purpose  :
123 //=================================================================================
124 void RepairGUI_DivideEdgeDlg::Init()
125 {
126   /* init variables */
127   myEditCurrentArgument = GroupPoints->LineEdit1;
128
129   myObject = GEOM::GEOM_Object::_nil();
130   myIndex = -1;
131
132   //myGeomGUI->SetState( 0 );
133   initSelection();
134
135   /* signals and slots connections */
136   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
137   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
138
139   connect( myValEdt,   SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
140
141   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
142   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
143
144   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
145            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
146
147   initName( tr( "DEVIDE_EDGE_NEW_OBJECT_NAME" ) );
148   resize(100,100);
149 }
150
151 //=================================================================================
152 // function : ValueChangedInSpinBox()
153 // purpose  : On change value in spin box
154 //=================================================================================
155 void RepairGUI_DivideEdgeDlg::ValueChangedInSpinBox()
156 {
157   displayPreview();
158 }
159
160 //=================================================================================
161 // function : displayPreview()
162 // purpose  : On display Preview
163 //=================================================================================
164 void RepairGUI_DivideEdgeDlg::displayPreview()
165 {
166   erasePreview();
167   if ( myObject->_is_nil() )
168     return;
169
170   TopoDS_Shape aShape;
171   gp_Pnt aPnt;
172   GEOMBase::GetShape( myObject, aShape, TopAbs_SHAPE );
173
174   if ( myIndex != -1) {
175     TopTools_IndexedMapOfShape aShapes;
176     TopExp::MapShapes(aShape, aShapes);
177     aShape = aShapes.FindKey(myIndex);
178   }
179
180   if (aShape.ShapeType() == TopAbs_EDGE) {
181     Standard_Real aFP, aLP, aP;
182     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFP, aLP);
183     aP = aFP + (aLP - aFP) * myValEdt->value();
184     aPnt = aCurve->Value(aP);
185     BRepBuilderAPI_MakeVertex mkVertex (aPnt);
186     aShape = mkVertex.Shape();
187     // Build prs
188     SALOME_Prs* aPrs = getDisplayer()->BuildPrs( aShape );
189     if ( aPrs != 0 && !aPrs->IsNull() )
190       GEOMBase_Helper::displayPreview( aPrs, false, true );
191   } 
192 }
193
194 //=================================================================================
195 // function : ClickOnOk()
196 // purpose  : Same than click on apply but close this dialog.
197 //=================================================================================
198 void RepairGUI_DivideEdgeDlg::ClickOnOk()
199 {
200   if ( ClickOnApply() )
201     ClickOnCancel();
202 }
203
204
205 //=================================================================================
206 // function : ClickOnApply()
207 // purpose  :
208 //=================================================================================
209 bool RepairGUI_DivideEdgeDlg::ClickOnApply()
210 {
211   if ( !onAccept() )
212     return false;
213
214   initName();
215
216   myEditCurrentArgument->setText( "" );
217   myObject = GEOM::GEOM_Object::_nil();
218   myIndex = -1;
219
220   initSelection();
221
222   return true;
223 }
224
225
226 //=================================================================================
227 // function : SelectionIntoArgument()
228 // purpose  : Called when selection as changed or other case
229 //          : used only by SelectButtonC1A1 (LineEditC1A1)
230 //=================================================================================
231 void RepairGUI_DivideEdgeDlg::SelectionIntoArgument()
232 {
233   myEditCurrentArgument->setText( "" );
234
235   myObject = GEOM::GEOM_Object::_nil();
236   myIndex = -1;
237
238   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
239   SALOME_ListIO aSelList;
240   aSelMgr->selectedObjects(aSelList);
241
242   if ( aSelList.Extent() == 1 ) {
243     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
244     Standard_Boolean aRes;
245     GEOM::GEOM_Object_var aSelectedObj = GEOMBase::ConvertIOinGEOMObject( anIO, aRes );
246     if ( !CORBA::is_nil( aSelectedObj ) && aRes )
247     {
248       TopoDS_Shape aShape;
249       QString aName = GEOMBase::GetName( aSelectedObj );
250       if ( GEOMBase::GetShape( aSelectedObj, aShape, TopAbs_SHAPE ) )
251       {
252         const int aType = aShape.ShapeType();
253         if ( aType <= TopAbs_EDGE ) // edge, wire, face, shell, solid, compound
254         {
255           GEOM::short_array anIndexes;
256
257           TColStd_IndexedMapOfInteger aMap;
258           SalomeApp_Application* anApp =
259             (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
260           anApp->selectionMgr()->GetIndexes( anIO, aMap );
261
262           if ( !aMap.IsEmpty() ) // subshape selection
263           {
264             myIndex = aMap( 1 );
265             myObject = aSelectedObj;
266             myEditCurrentArgument->setText( aName += QString( ":edge_%1" ).arg( myIndex ) );
267           }
268           else if ( aType == TopAbs_EDGE ) // single shape selection
269           {
270             myIndex = -1;
271             myObject = aSelectedObj;
272             myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
273           }
274           else // face, shell, solid or compound was selected, and NOT its subshape.
275           {
276             myIndex = -1;
277             myObject = GEOM::GEOM_Object::_nil();
278           }
279         }
280       }
281     }
282   }
283
284   displayPreview();
285 }
286
287 //=================================================================================
288 // function : SetEditCurrentArgument()
289 // purpose  :
290 //=================================================================================
291 void RepairGUI_DivideEdgeDlg::SetEditCurrentArgument()
292 {
293   if ( sender() == GroupPoints->PushButton1 ) {
294     GroupPoints->LineEdit1->setFocus();
295     myEditCurrentArgument = GroupPoints->LineEdit1;
296   }
297   SelectionIntoArgument();
298 }
299
300
301 //=================================================================================
302 // function : LineEditReturnPressed()
303 // purpose  :
304 //=================================================================================
305 void RepairGUI_DivideEdgeDlg::LineEditReturnPressed()
306 {
307   if ( sender() == GroupPoints->LineEdit1 ) {
308     myEditCurrentArgument = GroupPoints->LineEdit1;
309     GEOMBase_Skeleton::LineEditReturnPressed();
310   }
311 }
312
313
314 //=================================================================================
315 // function : ActivateThisDialog()
316 // purpose  :
317 //=================================================================================
318 void RepairGUI_DivideEdgeDlg::ActivateThisDialog()
319 {
320   GEOMBase_Skeleton::ActivateThisDialog();
321   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
322            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
323
324 //  myObject = GEOM::GEOM_Object::_nil();
325   myIndex = -1;
326
327   //myGeomGUI->SetState( 0 );
328   initSelection();
329 }
330
331 //=================================================================================
332 // function : enterEvent()
333 // purpose  : Mouse enter onto the dialog to activate it
334 //=================================================================================
335 void RepairGUI_DivideEdgeDlg::enterEvent( QEvent* )
336 {
337   if ( !mainFrame()->GroupConstructors->isEnabled() )
338     ActivateThisDialog();
339 }
340
341
342 //=================================================================================
343 // function : createOperation
344 // purpose  :
345 //=================================================================================
346 GEOM::GEOM_IOperations_ptr RepairGUI_DivideEdgeDlg::createOperation()
347 {
348   return getGeomEngine()->GetIHealingOperations( getStudyId() );
349 }
350
351 //=================================================================================
352 // function : isValid
353 // purpose  :
354 //=================================================================================
355 bool RepairGUI_DivideEdgeDlg::isValid( QString& msg )
356 {
357   bool ok = myValEdt->isValid( msg, !IsPreview() );
358   return !myObject->_is_nil() && ok;
359 }
360
361 //=================================================================================
362 // function : execute
363 // purpose  :
364 //=================================================================================
365 bool RepairGUI_DivideEdgeDlg::execute( ObjectList& objects )
366 {
367   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow( getOperation() );
368   GEOM::GEOM_Object_var anObj = anOper->DivideEdge( myObject, myIndex, myValEdt->value(), getIsByParameter() );
369   bool aResult = !anObj->_is_nil();
370   if ( aResult )
371   {
372     if ( !IsPreview() )
373     {
374       QStringList aParameters;
375       aParameters << "";
376       aParameters << myValEdt->text();
377       aParameters << "";
378       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
379     }
380     objects.push_back( anObj._retn() );
381   }
382
383   return aResult;
384 }
385
386 //=================================================================================
387 // function : getIsByParameter
388 // purpose  :
389 //=================================================================================
390 bool RepairGUI_DivideEdgeDlg::getIsByParameter() const
391 {
392   return myIsParameterGr->button( 0 )->isChecked();
393 }
394
395 //=================================================================================
396 // function : initSelection
397 // purpose  :
398 //=================================================================================
399 void RepairGUI_DivideEdgeDlg::initSelection()
400 {
401   GEOM::GEOM_Object_var aNullGeomObject;
402   localSelection( aNullGeomObject, TopAbs_EDGE ); // load local selection on ALL objects
403 }