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