Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / OperationGUI / OperationGUI_Fillet2dDlg.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   : OperationGUI_Fillet2dDlg.cxx
23 // Author : DMV, OCN.
24 //
25
26 #include "OperationGUI_Fillet2dDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_Desktop.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_ViewWindow.h>
36 #include <SUIT_ViewManager.h>
37 #include <SalomeApp_Application.h>
38 #include <LightApp_SelectionMgr.h>
39 #include <OCCViewer_ViewModel.h>
40
41 // OCCT Includes
42 #include <TColStd_MapOfInteger.hxx>
43 #include <TColStd_IndexedMapOfInteger.hxx>
44
45 #include <GEOMImpl_Types.hxx>
46
47 //=================================================================================
48 // class    : OperationGUI_Fillet2dDlg()
49 // purpose  : Constructs a OperationGUI_Fillet2dDlg which is a child of 'parent', with the
50 //            name 'name' and widget flags set to 'f'.
51 //            The dialog will by default be modeless, unless you set 'modal' to
52 //            TRUE to construct a modal dialog.
53 //=================================================================================
54 OperationGUI_Fillet2dDlg::OperationGUI_Fillet2dDlg (GeometryGUI* theGeometryGUI, QWidget* parent)
55   : GEOMBase_Skeleton(theGeometryGUI, parent, false)
56 {
57   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
58   QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_FILLET_2D")));
59   QPixmap iconSelect (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
60
61   setWindowTitle(tr("GEOM_FILLET_2D_TITLE"));
62
63   /***************************************************************/
64   mainFrame()->GroupConstructors->setTitle(tr("GEOM_FILLET_2D"));
65   mainFrame()->RadioButton1->setIcon(image0);
66   mainFrame()->RadioButton2->close();
67   mainFrame()->RadioButton3->close();
68
69   GroupVertexes = new DlgRef_2Sel1Spin(centralWidget());
70   GroupVertexes->GroupBox1->setTitle(tr("GEOM_FILLET_2D"));
71   GroupVertexes->TextLabel1->setText(tr("GEOM_PLANAR_FACE"));
72   GroupVertexes->TextLabel2->setText(tr("GEOM_VERTEXES"));
73   GroupVertexes->TextLabel3->setText(tr("GEOM_RADIUS"));
74   GroupVertexes->PushButton1->setIcon(iconSelect);
75   GroupVertexes->PushButton2->setIcon(iconSelect);
76   GroupVertexes->LineEdit1->setReadOnly(true);
77   GroupVertexes->LineEdit2->setReadOnly(true);
78
79   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
80   layout->setMargin(0); layout->setSpacing(6);
81   layout->addWidget(GroupVertexes);
82
83   /***************************************************************/
84
85   // Set range of spinboxes
86   double SpecificStep = 10.0;
87   initSpinBox(GroupVertexes->SpinBox_DX, 0.00001, COORD_MAX, SpecificStep, 5); // VSR: TODO: DBL_DIGITS_DISPLAY
88
89   setHelpFileName("fillet2d_operation_page.html");
90
91   // Initialisation
92   Init();
93   resize(0,0);
94 }
95
96 //=================================================================================
97 // function : ~OperationGUI_Fillet2dDlg()
98 // purpose  : Destroys the object and frees any allocated resources
99 //=================================================================================
100 OperationGUI_Fillet2dDlg::~OperationGUI_Fillet2dDlg()
101 {
102 }
103
104 //=================================================================================
105 // function : Init()
106 // purpose  :
107 //=================================================================================
108 void OperationGUI_Fillet2dDlg::Init()
109 {
110   // Set Initial values of spinboxes
111   GroupVertexes->SpinBox_DX->setValue(5);
112
113   // Clear line edits
114   GroupVertexes->LineEdit1->setText("");
115   GroupVertexes->LineEdit2->setText("");
116
117   myShape = GEOM::GEOM_Object::_nil();
118
119   myVertexes.Clear();
120
121   // signals and slots connections
122
123   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()   ));
124   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
125
126   //  connect(this,          SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
127
128   connect(GroupVertexes->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
129   connect(GroupVertexes->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
130
131   connect(GroupVertexes->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
132   connect(GroupVertexes->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
133
134   connect(GroupVertexes->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
135
136   initName(tr("GEOM_FILLET_2D"));
137   GroupVertexes->PushButton1->click();
138 }
139
140 //=================================================================================
141 // function : ClickOnOk()
142 // purpose  :
143 //=================================================================================
144 void OperationGUI_Fillet2dDlg::ClickOnOk()
145 {
146   if (ClickOnApply())
147     ClickOnCancel();
148 }
149
150 //=================================================================================
151 // function : ClickOnApply()
152 // purpose  :
153 //=================================================================================
154 bool OperationGUI_Fillet2dDlg::ClickOnApply()
155 {
156   if (!onAccept())
157     return false;
158
159   initName();
160   // Reset dialog state
161   GroupVertexes->LineEdit1->setText("");
162   GroupVertexes->LineEdit2->setText("");
163   myShape = GEOM::GEOM_Object::_nil();
164   myVertexes.Clear();
165   GroupVertexes->PushButton1->click();
166
167   return true;
168 }
169
170 //=================================================================================
171 // function : SelectionIntoArgument()
172 // purpose  : Called when selection is changed or on dialog initialization or activation
173 //=================================================================================
174 void OperationGUI_Fillet2dDlg::SelectionIntoArgument()
175 {
176   erasePreview();
177   myEditCurrentArgument->setText("");
178
179   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
180   SALOME_ListIO aSelList;
181   aSelMgr->selectedObjects(aSelList);
182
183   // If selection of main object is activated
184   if (myEditCurrentArgument == GroupVertexes->LineEdit1) {
185     myShape = GEOM::GEOM_Object::_nil();
186     if (aSelList.Extent() == 1) {
187       Standard_Boolean aResult = Standard_False;
188       GEOM::GEOM_Object_var anObj =
189         GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aResult);
190       
191       if (aResult && !anObj->_is_nil()) {
192         QString aName = GEOMBase::GetName( anObj );
193         TopoDS_Shape aShape;
194         if ( GEOMBase::GetShape( anObj, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
195           TColStd_IndexedMapOfInteger aMap;
196           aSelMgr->GetIndexes(aSelList.First(), aMap);
197           if ( aMap.Extent() == 1 ) { // Local Selection
198             int anIndex = aMap( 1 );
199             aName += QString( ":face_%1" ).arg( anIndex );
200    
201             //Find SubShape Object in Father
202             GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( anObj, aName );
203             
204             if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
205               GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
206               anObj = aShapesOp->GetSubShape( anObj, anIndex );
207             }
208             else
209               anObj = aFindedObject; // get Object from study
210           }
211           else { // Global Selection
212             if ( aShape.ShapeType() != TopAbs_FACE ) {
213               anObj = GEOM::GEOM_Object::_nil();
214               aName = "";
215             }
216           }
217         }
218         myShape = anObj;
219         myEditCurrentArgument->setText(aName
220 );
221         displayPreview();
222       }
223     }
224   } else if (myEditCurrentArgument == GroupVertexes->LineEdit2) {
225     myVertexes.Clear();
226     if (aSelList.Extent() == 1) {
227       Standard_Boolean aResult = Standard_False;
228       GEOM::GEOM_Object_var anObj =
229         GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aResult);
230
231       if (aResult && !anObj->_is_nil()) {
232         TColStd_IndexedMapOfInteger anIndexes;
233         aSelMgr->GetIndexes(aSelList.First(), anIndexes);
234
235         if (anIndexes.Extent() > 0) {
236           QString aName;
237           if (anIndexes.Extent() == 1) {
238             int anIndex = anIndexes(1);
239             aName = QString(GEOMBase::GetName(anObj)) + QString(":vertex_%1").arg(anIndex);
240           }
241           else
242             aName = tr("GEOM_MEN_POPUP_NAME").arg(anIndexes.Extent());
243
244           myEditCurrentArgument->setText(aName);
245           myVertexes = anIndexes;
246           displayPreview();
247         }
248       }
249     }
250   }
251
252   if (myEditCurrentArgument == GroupVertexes->LineEdit1) {
253     if (!myShape->_is_nil() && myVertexes.Extent() == 0)
254       GroupVertexes->PushButton2->click();
255   }
256 }
257
258 //=================================================================================
259 // function : SetEditCurrentArgument()
260 // purpose  :
261 //=================================================================================
262 void OperationGUI_Fillet2dDlg::SetEditCurrentArgument()
263 {
264   QPushButton* send = (QPushButton*)sender();
265
266   if (send == GroupVertexes->PushButton1) {
267     myEditCurrentArgument = GroupVertexes->LineEdit1;
268     GroupVertexes->PushButton2->setDown(false);
269     GroupVertexes->LineEdit2->setEnabled(false);
270   }
271   else if (send == GroupVertexes->PushButton2) {
272     myEditCurrentArgument = GroupVertexes->LineEdit2;
273     GroupVertexes->PushButton1->setDown(false);
274     GroupVertexes->LineEdit1->setEnabled(false);
275   }
276
277   // enable line edit
278   myEditCurrentArgument->setEnabled(true);
279   myEditCurrentArgument->setFocus();
280   // after setFocus(), because it will be setDown(false) when loses focus
281   send->setDown(true);
282
283   activateSelection();
284
285   // seems we need it only to avoid preview disappearing, caused by selection mode change
286   displayPreview();
287 }
288
289 //=================================================================================
290 // function : LineEditReturnPressed()
291 // purpose  :
292 //=================================================================================
293 void OperationGUI_Fillet2dDlg::LineEditReturnPressed()
294 {
295   QLineEdit* send = (QLineEdit*)sender();
296
297   if (send == GroupVertexes->LineEdit1)
298     myEditCurrentArgument = GroupVertexes->LineEdit1;
299   else if (send == GroupVertexes->LineEdit2)
300     myEditCurrentArgument = GroupVertexes->LineEdit2;
301   else
302     return;
303
304   GEOMBase_Skeleton::LineEditReturnPressed();
305 }
306
307 //=================================================================================
308 // function : ActivateThisDialog()
309 // purpose  :
310 //=================================================================================
311 void OperationGUI_Fillet2dDlg::ActivateThisDialog()
312 {
313   GEOMBase_Skeleton::ActivateThisDialog();
314 }
315
316 //=================================================================================
317 // function : enterEvent()
318 // purpose  :
319 //=================================================================================
320 void OperationGUI_Fillet2dDlg::enterEvent (QEvent*)
321 {
322   if (!mainFrame()->GroupConstructors->isEnabled())
323     this->ActivateThisDialog();
324 }
325
326 //=================================================================================
327 // function : ValueChangedInSpinBox()
328 // purpose  :
329 //=================================================================================
330 void OperationGUI_Fillet2dDlg::ValueChangedInSpinBox (double)
331 {
332   displayPreview();
333 }
334
335 //=================================================================================
336 // function : activateSelection
337 // purpose  : Activate selection in accordance with myEditCurrentArgument
338 //=================================================================================
339 void OperationGUI_Fillet2dDlg::activateSelection()
340 {
341   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
342   globalSelection();
343   if (myEditCurrentArgument == GroupVertexes->LineEdit1)
344     globalSelection( GEOM_FACE );  //    localSelection(myShape, TopAbs_FACE);
345   else if (!myShape->_is_nil() && myEditCurrentArgument == GroupVertexes->LineEdit2)
346     localSelection(myShape, TopAbs_VERTEX);
347
348   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
349           this, SLOT(SelectionIntoArgument()));
350 }
351
352 //=================================================================================
353 // function : createOperation
354 // purpose  :
355 //=================================================================================
356 GEOM::GEOM_IOperations_ptr OperationGUI_Fillet2dDlg::createOperation()
357 {
358   return getGeomEngine()->GetILocalOperations(getStudyId());
359 }
360
361 //=================================================================================
362 // function : isValid()
363 // purpose  : Verify validity of input data
364 //=================================================================================
365 bool OperationGUI_Fillet2dDlg::isValid (QString&)
366 {
367   return !myShape->_is_nil() && myVertexes.Extent() > 0;
368 }
369
370 //=================================================================================
371 // function : execute
372 // purpose  :
373 //=================================================================================
374 bool OperationGUI_Fillet2dDlg::execute (ObjectList& objects)
375 {
376   GEOM::GEOM_Object_var anObj;
377
378   GEOM::ListOfLong_var aListOfIndexes = new GEOM::ListOfLong;
379   aListOfIndexes->length(myVertexes.Extent());
380
381   for (int i = 1, n = myVertexes.Extent(); i <= n; i++)
382     aListOfIndexes[ i - 1 ] = myVertexes(i);
383
384   anObj = GEOM::GEOM_ILocalOperations::_narrow(getOperation())->
385           MakeFillet2D(myShape, getRadius(), aListOfIndexes);
386
387   if (!anObj->_is_nil())
388     objects.push_back(anObj._retn());
389
390   return true;
391 }
392
393 //=================================================================================
394 // function : getRadius
395 // purpose  : Get radius
396 //=================================================================================
397 double OperationGUI_Fillet2dDlg::getRadius() const
398 {
399   return GroupVertexes ->SpinBox_DX->value();
400 }