Salome HOME
ee5b866331301e82d24aa06581ca54012148e599
[modules/geom.git] / src / OperationGUI / OperationGUI_Fillet1d2dDlg.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18
19 // GEOM GEOMGUI : GUI for Geometry component
20 // File   : OperationGUI_Fillet1d2dDlg.cxx
21 // Author : DMV, OCN.
22
23 #include "OperationGUI_Fillet1d2dDlg.h"
24
25 #include <DlgRef.h>
26 #include <GeometryGUI.h>
27 #include <GEOMBase.h>
28
29 #include <SUIT_Desktop.h>
30 #include <SUIT_Session.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <SUIT_ViewWindow.h>
33 #include <SUIT_ViewManager.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <OCCViewer_ViewModel.h>
37
38 // OCCT Includes
39 #include <TColStd_MapOfInteger.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41
42 #include <GEOMImpl_Types.hxx>
43
44 //=================================================================================
45 // class    : OperationGUI_Fillet1d2dDlg()
46 // purpose  : Constructs a OperationGUI_Fillet1d2dDlg which is a child of 'parent', with the
47 //            name 'name' and widget flags set to 'f'.
48 //            The dialog will by default be modeless, unless you set 'modal' to
49 //            TRUE to construct a modal dialog.
50 //=================================================================================
51 OperationGUI_Fillet1d2dDlg::OperationGUI_Fillet1d2dDlg (GeometryGUI* theGeometryGUI,
52                                                         QWidget* parent,
53                                                         bool is1D)
54   : GEOMBase_Skeleton(theGeometryGUI, parent, false),
55     myIs1D( is1D )
56 {
57   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
58   QPixmap image0 (aResMgr->loadPixmap("GEOM", myIs1D ? tr("ICON_DLG_FILLET_1D") 
59                                                      : tr("ICON_DLG_FILLET_2D")));
60   QPixmap iconSelect (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
61
62   setWindowTitle(myIs1D ? tr("GEOM_FILLET_1D_TITLE") : tr("GEOM_FILLET_2D_TITLE"));
63
64   /***************************************************************/
65   mainFrame()->GroupConstructors->setTitle(myIs1D ? tr("GEOM_FILLET_1D") : tr("GEOM_FILLET_2D"));
66   mainFrame()->RadioButton1->setIcon(image0);
67   mainFrame()->RadioButton2->close();
68   mainFrame()->RadioButton3->close();
69
70   GroupVertexes = new DlgRef_2Sel1Spin2Check (centralWidget());
71   GroupVertexes->GroupBox1->setTitle(myIs1D ? tr("GEOM_FILLET_1D") : tr("GEOM_FILLET_2D"));
72   GroupVertexes->TextLabel1->setText(myIs1D ? tr("GEOM_PLANAR_EDGE_WIRE") : tr("GEOM_PLANAR_FACE"));
73   GroupVertexes->TextLabel2->setText(tr("GEOM_VERTEXES"));
74   GroupVertexes->TextLabel3->setText(tr("GEOM_RADIUS"));
75   GroupVertexes->PushButton1->setIcon(iconSelect);
76   GroupVertexes->PushButton2->setIcon(iconSelect);
77   GroupVertexes->LineEdit1->setReadOnly(true);
78   GroupVertexes->LineEdit2->setReadOnly(true);
79   if (myIs1D) {
80     GroupVertexes->CheckButton1->setText(tr("GEOM_FILLET_1D_IGNORE_SECANT"));
81     GroupVertexes->CheckButton1->setChecked(true);
82   }
83   else
84     GroupVertexes->CheckButton1->close();
85   GroupVertexes->CheckButton2->close();
86
87   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
88   layout->setMargin(0); layout->setSpacing(6);
89   layout->addWidget(GroupVertexes);
90
91   /***************************************************************/
92
93   // Set range of spinboxes
94   double SpecificStep = 10.0;
95   initSpinBox(GroupVertexes->SpinBox_DX, 0.00001, COORD_MAX, SpecificStep, "length_precision" );
96
97   setHelpFileName(myIs1D ? "fillet1d_operation_page.html" : "fillet2d_operation_page.html");
98
99   // Initialisation
100   Init();
101   resize(100,100);
102 }
103
104 //=================================================================================
105 // function : ~OperationGUI_Fillet1d2dDlg()
106 // purpose  : Destroys the object and frees any allocated resources
107 //=================================================================================
108 OperationGUI_Fillet1d2dDlg::~OperationGUI_Fillet1d2dDlg()
109 {
110 }
111
112 //=================================================================================
113 // function : Init()
114 // purpose  :
115 //=================================================================================
116 void OperationGUI_Fillet1d2dDlg::Init()
117 {
118   // Set Initial values of spinboxes
119   GroupVertexes->SpinBox_DX->setValue(5);
120
121   // Clear line edits
122   GroupVertexes->LineEdit1->setText("");
123   GroupVertexes->LineEdit2->setText("");
124
125   myShape = GEOM::GEOM_Object::_nil();
126
127   myVertexes.Clear();
128
129   showOnlyPreviewControl();
130
131   // signals and slots connections
132
133   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()   ));
134   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
135
136   connect(GroupVertexes->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
137   connect(GroupVertexes->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
138
139   connect(GroupVertexes->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
140   connect(GroupVertexes->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
141
142   connect(GroupVertexes->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
143
144   connect(GroupVertexes->CheckButton1, SIGNAL(toggled(bool)), this, SLOT(processPreview()));
145
146   initName(myIs1D ? tr("GEOM_FILLET_1D") : tr("GEOM_FILLET_2D"));
147   GroupVertexes->PushButton1->click();
148
149   SelectionIntoArgument();
150 }
151
152 //=================================================================================
153 // function : ClickOnOk()
154 // purpose  :
155 //=================================================================================
156 void OperationGUI_Fillet1d2dDlg::ClickOnOk()
157 {
158   setIsApplyAndClose( true );
159   if (ClickOnApply())
160     ClickOnCancel();
161 }
162
163 //=================================================================================
164 // function : ClickOnApply()
165 // purpose  :
166 //=================================================================================
167 bool OperationGUI_Fillet1d2dDlg::ClickOnApply()
168 {
169   if (!onAccept())
170     return false;
171
172   initName();
173   // Reset dialog state
174   GroupVertexes->LineEdit1->setText("");
175   GroupVertexes->LineEdit2->setText("");
176   myShape = GEOM::GEOM_Object::_nil();
177   myVertexes.Clear();
178   GroupVertexes->PushButton1->click();
179
180   return true;
181 }
182
183 //=================================================================================
184 // function : SelectionIntoArgument()
185 // purpose  : Called when selection is changed or on dialog initialization or activation
186 //=================================================================================
187 void OperationGUI_Fillet1d2dDlg::SelectionIntoArgument()
188 {
189   erasePreview();
190   myEditCurrentArgument->setText("");
191
192   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
193   SALOME_ListIO aSelList;
194   aSelMgr->selectedObjects(aSelList);
195
196   // If selection of main object is activated
197   if (myEditCurrentArgument == GroupVertexes->LineEdit1) {
198     myShape = GEOM::GEOM_Object::_nil();
199     if (aSelList.Extent() == 1) {
200       GEOM::GEOM_Object_var anObj =
201         GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
202       
203       if ( !anObj->_is_nil() ) {
204         QString aName = GEOMBase::GetName( anObj );
205         TopoDS_Shape aShape;
206         if ( GEOMBase::GetShape( anObj, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
207           TColStd_IndexedMapOfInteger aMap;
208           aSelMgr->GetIndexes(aSelList.First(), aMap);
209           if ( aMap.Extent() == 1 ) { // Local Selection
210             int anIndex = aMap( 1 );
211             aName += QString( myIs1D ? ":wire_%1" : ":face_%1" ).arg( anIndex );
212    
213             //Find SubShape Object in Father
214             GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( anObj, aName );
215             
216             if ( aFindedObject->_is_nil()) { // Object not found in study
217               GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
218               anObj = aShapesOp->GetSubShape( anObj, anIndex );
219             }
220             else
221               anObj = aFindedObject; // get Object from study
222           }
223           else { // Global Selection
224             if ((myIs1D && aShape.ShapeType() != TopAbs_WIRE) ||
225                 (!myIs1D && aShape.ShapeType() != TopAbs_FACE && aShape.ShapeType() != TopAbs_SHELL)) {
226               anObj = GEOM::GEOM_Object::_nil();
227               aName = "";
228             }
229           }
230         }
231         myShape = anObj;
232         myEditCurrentArgument->setText(aName
233 );
234         processPreview();
235       }
236     }
237   } else if (myEditCurrentArgument == GroupVertexes->LineEdit2) {
238     myVertexes.Clear();
239     bool isPreview = myIs1D;
240     if (aSelList.Extent() == 1) {
241       GEOM::GEOM_Object_var anObj =
242         GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
243
244       if ( !anObj->_is_nil() ) {
245         TColStd_IndexedMapOfInteger anIndexes;
246         aSelMgr->GetIndexes(aSelList.First(), anIndexes);
247
248         if (anIndexes.Extent() > 0) {
249           QString aName;
250           if (anIndexes.Extent() == 1) {
251             int anIndex = anIndexes(1);
252             aName = QString(GEOMBase::GetName(anObj)) + QString(":vertex_%1").arg(anIndex);
253           }
254           else
255             aName = tr("GEOM_MEN_POPUP_NAME").arg(anIndexes.Extent());
256
257           isPreview = true;
258           myEditCurrentArgument->setText(aName);
259           myVertexes = anIndexes;
260         }
261       }
262     }
263     if ( isPreview )
264       processPreview();
265   }
266
267   if (myEditCurrentArgument == GroupVertexes->LineEdit1) {
268     if (!myShape->_is_nil() && myVertexes.Extent() == 0)
269       GroupVertexes->PushButton2->click();
270   }
271 }
272
273 //=================================================================================
274 // function : SetEditCurrentArgument()
275 // purpose  :
276 //=================================================================================
277 void OperationGUI_Fillet1d2dDlg::SetEditCurrentArgument()
278 {
279   QPushButton* send = (QPushButton*)sender();
280
281   if (send == GroupVertexes->PushButton1) {
282     myEditCurrentArgument = GroupVertexes->LineEdit1;
283     GroupVertexes->PushButton2->setDown(false);
284     GroupVertexes->LineEdit2->setEnabled(false);
285   }
286   else if (send == GroupVertexes->PushButton2) {
287     myEditCurrentArgument = GroupVertexes->LineEdit2;
288     GroupVertexes->PushButton1->setDown(false);
289     GroupVertexes->LineEdit1->setEnabled(false);
290   }
291
292   // enable line edit
293   myEditCurrentArgument->setEnabled(true);
294   myEditCurrentArgument->setFocus();
295   // after setFocus(), because it will be setDown(false) when loses focus
296   send->setDown(true);
297
298   activateSelection();
299
300   // seems we need it only to avoid preview disappearing, caused by selection mode change
301   processPreview();
302 }
303
304 //=================================================================================
305 // function : LineEditReturnPressed()
306 // purpose  :
307 //=================================================================================
308 void OperationGUI_Fillet1d2dDlg::LineEditReturnPressed()
309 {
310   QLineEdit* send = (QLineEdit*)sender();
311
312   if (send == GroupVertexes->LineEdit1)
313     myEditCurrentArgument = GroupVertexes->LineEdit1;
314   else if (send == GroupVertexes->LineEdit2)
315     myEditCurrentArgument = GroupVertexes->LineEdit2;
316   else
317     return;
318
319   GEOMBase_Skeleton::LineEditReturnPressed();
320 }
321
322 //=================================================================================
323 // function : ActivateThisDialog()
324 // purpose  :
325 //=================================================================================
326 void OperationGUI_Fillet1d2dDlg::ActivateThisDialog()
327 {
328   GEOMBase_Skeleton::ActivateThisDialog();
329 }
330
331 //=================================================================================
332 // function : enterEvent()
333 // purpose  :
334 //=================================================================================
335 void OperationGUI_Fillet1d2dDlg::enterEvent (QEvent*)
336 {
337   if (!mainFrame()->GroupConstructors->isEnabled())
338     this->ActivateThisDialog();
339 }
340
341 //=================================================================================
342 // function : ValueChangedInSpinBox()
343 // purpose  :
344 //=================================================================================
345 void OperationGUI_Fillet1d2dDlg::ValueChangedInSpinBox (double)
346 {
347   processPreview();
348 }
349
350 //=================================================================================
351 // function : activateSelection
352 // purpose  : Activate selection in accordance with myEditCurrentArgument
353 //=================================================================================
354 void OperationGUI_Fillet1d2dDlg::activateSelection()
355 {
356   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
357   globalSelection();
358   if (myEditCurrentArgument == GroupVertexes->LineEdit1)
359     //localSelection(myShape, myIs1D ? TopAbs_WIRE : TopAbs_FACE);
360     if (myIs1D)
361       globalSelection(GEOM_WIRE);
362     else {
363       TColStd_MapOfInteger aMap;
364       aMap.Add(GEOM_FACE);
365       aMap.Add(GEOM_SHELL);
366       globalSelection(aMap);
367     }
368   else if (!myShape->_is_nil() && myEditCurrentArgument == GroupVertexes->LineEdit2)
369     localSelection(myShape, TopAbs_VERTEX);
370
371   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
372           this, SLOT(SelectionIntoArgument()));
373 }
374
375 //=================================================================================
376 // function : createOperation
377 // purpose  :
378 //=================================================================================
379 GEOM::GEOM_IOperations_ptr OperationGUI_Fillet1d2dDlg::createOperation()
380 {
381   return getGeomEngine()->GetILocalOperations(getStudyId());
382 }
383
384 //=================================================================================
385 // function : isValid()
386 // purpose  : Verify validity of input data
387 //=================================================================================
388 bool OperationGUI_Fillet1d2dDlg::isValid (QString& msg)
389 {
390   bool ok = !myShape->_is_nil();
391   ok = GroupVertexes->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
392   ok = (myIs1D || myVertexes.Extent() > 0) && ok;
393   return ok;
394 }
395
396 //=================================================================================
397 // function : execute
398 // purpose  :
399 //=================================================================================
400 bool OperationGUI_Fillet1d2dDlg::execute (ObjectList& objects)
401 {
402   GEOM::ListOfLong_var aListOfIndexes = new GEOM::ListOfLong;
403   aListOfIndexes->length(myVertexes.Extent());
404
405   for (int i = 1, n = myVertexes.Extent(); i <= n; i++)
406     aListOfIndexes[ i - 1 ] = myVertexes(i);
407
408   GEOM::GEOM_ILocalOperations_var anOper = GEOM::GEOM_ILocalOperations::_narrow(getOperation());
409   GEOM::GEOM_Object_var anObj = myIs1D ?
410     anOper->MakeFillet1D(myShape, getRadius(), aListOfIndexes, GroupVertexes->CheckButton1->isChecked()) : 
411     anOper->MakeFillet2D(myShape, getRadius(), aListOfIndexes);
412
413   if (!anObj->_is_nil())
414     objects.push_back(anObj._retn());
415
416   return true;
417 }
418
419 //=================================================================================
420 // function : getRadius
421 // purpose  : Get radius
422 //=================================================================================
423 double OperationGUI_Fillet1d2dDlg::getRadius() const
424 {
425   return GroupVertexes->SpinBox_DX->value();
426 }