Salome HOME
0021672: [CEA 565] Dump Study from script
[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
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_2Sel1Spin(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
81   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
82   layout->setMargin(0); layout->setSpacing(6);
83   layout->addWidget(GroupVertexes);
84
85   /***************************************************************/
86
87   // Set range of spinboxes
88   double SpecificStep = 10.0;
89   initSpinBox(GroupVertexes->SpinBox_DX, 0.00001, COORD_MAX, SpecificStep, "length_precision" );
90
91   setHelpFileName(myIs1D ? "fillet1d_operation_page.html" : "fillet2d_operation_page.html");
92
93   // Initialisation
94   Init();
95   resize(100,100);
96 }
97
98 //=================================================================================
99 // function : ~OperationGUI_Fillet1d2dDlg()
100 // purpose  : Destroys the object and frees any allocated resources
101 //=================================================================================
102 OperationGUI_Fillet1d2dDlg::~OperationGUI_Fillet1d2dDlg()
103 {
104 }
105
106 //=================================================================================
107 // function : Init()
108 // purpose  :
109 //=================================================================================
110 void OperationGUI_Fillet1d2dDlg::Init()
111 {
112   // Set Initial values of spinboxes
113   GroupVertexes->SpinBox_DX->setValue(5);
114
115   // Clear line edits
116   GroupVertexes->LineEdit1->setText("");
117   GroupVertexes->LineEdit2->setText("");
118
119   myShape = GEOM::GEOM_Object::_nil();
120
121   myVertexes.Clear();
122
123   showOnlyPreviewControl();
124
125   // signals and slots connections
126
127   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()   ));
128   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
129
130   //  connect(this,          SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
131
132   connect(GroupVertexes->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
133   connect(GroupVertexes->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
134
135   connect(GroupVertexes->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
136   connect(GroupVertexes->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
137
138   connect(GroupVertexes->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
139
140   initName(myIs1D ? tr("GEOM_FILLET_1D") : tr("GEOM_FILLET_2D"));
141   GroupVertexes->PushButton1->click();
142
143   SelectionIntoArgument();
144 }
145
146 //=================================================================================
147 // function : ClickOnOk()
148 // purpose  :
149 //=================================================================================
150 void OperationGUI_Fillet1d2dDlg::ClickOnOk()
151 {
152   setIsApplyAndClose( true );
153   if (ClickOnApply())
154     ClickOnCancel();
155 }
156
157 //=================================================================================
158 // function : ClickOnApply()
159 // purpose  :
160 //=================================================================================
161 bool OperationGUI_Fillet1d2dDlg::ClickOnApply()
162 {
163   if (!onAccept())
164     return false;
165
166   initName();
167   // Reset dialog state
168   GroupVertexes->LineEdit1->setText("");
169   GroupVertexes->LineEdit2->setText("");
170   myShape = GEOM::GEOM_Object::_nil();
171   myVertexes.Clear();
172   GroupVertexes->PushButton1->click();
173
174   return true;
175 }
176
177 //=================================================================================
178 // function : SelectionIntoArgument()
179 // purpose  : Called when selection is changed or on dialog initialization or activation
180 //=================================================================================
181 void OperationGUI_Fillet1d2dDlg::SelectionIntoArgument()
182 {
183   erasePreview();
184   myEditCurrentArgument->setText("");
185
186   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
187   SALOME_ListIO aSelList;
188   aSelMgr->selectedObjects(aSelList);
189
190   // If selection of main object is activated
191   if (myEditCurrentArgument == GroupVertexes->LineEdit1) {
192     myShape = GEOM::GEOM_Object::_nil();
193     if (aSelList.Extent() == 1) {
194       GEOM::GEOM_Object_var anObj =
195         GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
196       
197       if ( !anObj->_is_nil() ) {
198         QString aName = GEOMBase::GetName( anObj );
199         TopoDS_Shape aShape;
200         if ( GEOMBase::GetShape( anObj, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
201           TColStd_IndexedMapOfInteger aMap;
202           aSelMgr->GetIndexes(aSelList.First(), aMap);
203           if ( aMap.Extent() == 1 ) { // Local Selection
204             int anIndex = aMap( 1 );
205             aName += QString( myIs1D ? ":wire_%1" : ":face_%1" ).arg( anIndex );
206    
207             //Find SubShape Object in Father
208             GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( anObj, aName );
209             
210             if ( aFindedObject->_is_nil()) { // Object not found in study
211               GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
212               anObj = aShapesOp->GetSubShape( anObj, anIndex );
213             }
214             else
215               anObj = aFindedObject; // get Object from study
216           }
217           else { // Global Selection
218             if ((myIs1D && aShape.ShapeType() != TopAbs_WIRE) ||
219                 (!myIs1D && aShape.ShapeType() != TopAbs_FACE && aShape.ShapeType() != TopAbs_SHELL)) {
220               anObj = GEOM::GEOM_Object::_nil();
221               aName = "";
222             }
223           }
224         }
225         myShape = anObj;
226         myEditCurrentArgument->setText(aName
227 );
228         processPreview();
229       }
230     }
231   } else if (myEditCurrentArgument == GroupVertexes->LineEdit2) {
232     myVertexes.Clear();
233     bool isPreview = myIs1D;
234     if (aSelList.Extent() == 1) {
235       GEOM::GEOM_Object_var anObj =
236         GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
237
238       if ( !anObj->_is_nil() ) {
239         TColStd_IndexedMapOfInteger anIndexes;
240         aSelMgr->GetIndexes(aSelList.First(), anIndexes);
241
242         if (anIndexes.Extent() > 0) {
243           QString aName;
244           if (anIndexes.Extent() == 1) {
245             int anIndex = anIndexes(1);
246             aName = QString(GEOMBase::GetName(anObj)) + QString(":vertex_%1").arg(anIndex);
247           }
248           else
249             aName = tr("GEOM_MEN_POPUP_NAME").arg(anIndexes.Extent());
250
251           isPreview = true;
252           myEditCurrentArgument->setText(aName);
253           myVertexes = anIndexes;
254         }
255       }
256     }
257     if ( isPreview )
258       processPreview();
259   }
260
261   if (myEditCurrentArgument == GroupVertexes->LineEdit1) {
262     if (!myShape->_is_nil() && myVertexes.Extent() == 0)
263       GroupVertexes->PushButton2->click();
264   }
265 }
266
267 //=================================================================================
268 // function : SetEditCurrentArgument()
269 // purpose  :
270 //=================================================================================
271 void OperationGUI_Fillet1d2dDlg::SetEditCurrentArgument()
272 {
273   QPushButton* send = (QPushButton*)sender();
274
275   if (send == GroupVertexes->PushButton1) {
276     myEditCurrentArgument = GroupVertexes->LineEdit1;
277     GroupVertexes->PushButton2->setDown(false);
278     GroupVertexes->LineEdit2->setEnabled(false);
279   }
280   else if (send == GroupVertexes->PushButton2) {
281     myEditCurrentArgument = GroupVertexes->LineEdit2;
282     GroupVertexes->PushButton1->setDown(false);
283     GroupVertexes->LineEdit1->setEnabled(false);
284   }
285
286   // enable line edit
287   myEditCurrentArgument->setEnabled(true);
288   myEditCurrentArgument->setFocus();
289   // after setFocus(), because it will be setDown(false) when loses focus
290   send->setDown(true);
291
292   activateSelection();
293
294   // seems we need it only to avoid preview disappearing, caused by selection mode change
295   processPreview();
296 }
297
298 //=================================================================================
299 // function : LineEditReturnPressed()
300 // purpose  :
301 //=================================================================================
302 void OperationGUI_Fillet1d2dDlg::LineEditReturnPressed()
303 {
304   QLineEdit* send = (QLineEdit*)sender();
305
306   if (send == GroupVertexes->LineEdit1)
307     myEditCurrentArgument = GroupVertexes->LineEdit1;
308   else if (send == GroupVertexes->LineEdit2)
309     myEditCurrentArgument = GroupVertexes->LineEdit2;
310   else
311     return;
312
313   GEOMBase_Skeleton::LineEditReturnPressed();
314 }
315
316 //=================================================================================
317 // function : ActivateThisDialog()
318 // purpose  :
319 //=================================================================================
320 void OperationGUI_Fillet1d2dDlg::ActivateThisDialog()
321 {
322   GEOMBase_Skeleton::ActivateThisDialog();
323 }
324
325 //=================================================================================
326 // function : enterEvent()
327 // purpose  :
328 //=================================================================================
329 void OperationGUI_Fillet1d2dDlg::enterEvent (QEvent*)
330 {
331   if (!mainFrame()->GroupConstructors->isEnabled())
332     this->ActivateThisDialog();
333 }
334
335 //=================================================================================
336 // function : ValueChangedInSpinBox()
337 // purpose  :
338 //=================================================================================
339 void OperationGUI_Fillet1d2dDlg::ValueChangedInSpinBox (double)
340 {
341   processPreview();
342 }
343
344 //=================================================================================
345 // function : activateSelection
346 // purpose  : Activate selection in accordance with myEditCurrentArgument
347 //=================================================================================
348 void OperationGUI_Fillet1d2dDlg::activateSelection()
349 {
350   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
351   globalSelection();
352   if (myEditCurrentArgument == GroupVertexes->LineEdit1)
353     //localSelection(myShape, myIs1D ? TopAbs_WIRE : TopAbs_FACE);
354     if (myIs1D)
355       globalSelection(GEOM_WIRE);
356     else {
357       TColStd_MapOfInteger aMap;
358       aMap.Add(GEOM_FACE);
359       aMap.Add(GEOM_SHELL);
360       globalSelection(aMap);
361     }
362   else if (!myShape->_is_nil() && myEditCurrentArgument == GroupVertexes->LineEdit2)
363     localSelection(myShape, TopAbs_VERTEX);
364
365   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
366           this, SLOT(SelectionIntoArgument()));
367 }
368
369 //=================================================================================
370 // function : createOperation
371 // purpose  :
372 //=================================================================================
373 GEOM::GEOM_IOperations_ptr OperationGUI_Fillet1d2dDlg::createOperation()
374 {
375   return getGeomEngine()->GetILocalOperations(getStudyId());
376 }
377
378 //=================================================================================
379 // function : isValid()
380 // purpose  : Verify validity of input data
381 //=================================================================================
382 bool OperationGUI_Fillet1d2dDlg::isValid (QString& msg)
383 {
384   bool ok = !myShape->_is_nil();
385   ok = GroupVertexes->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
386   ok = (myIs1D || myVertexes.Extent() > 0) && ok;
387   return ok;
388 }
389
390 //=================================================================================
391 // function : execute
392 // purpose  :
393 //=================================================================================
394 bool OperationGUI_Fillet1d2dDlg::execute (ObjectList& objects)
395 {
396   GEOM::ListOfLong_var aListOfIndexes = new GEOM::ListOfLong;
397   aListOfIndexes->length(myVertexes.Extent());
398
399   for (int i = 1, n = myVertexes.Extent(); i <= n; i++)
400     aListOfIndexes[ i - 1 ] = myVertexes(i);
401
402   GEOM::GEOM_ILocalOperations_var anOper = GEOM::GEOM_ILocalOperations::_narrow(getOperation());
403   GEOM::GEOM_Object_var anObj = myIs1D ?
404     anOper->MakeFillet1D(myShape, getRadius(), aListOfIndexes) : 
405     anOper->MakeFillet2D(myShape, getRadius(), aListOfIndexes);
406
407   if (!anObj->_is_nil())
408     objects.push_back(anObj._retn());
409
410   return true;
411 }
412
413 //=================================================================================
414 // function : getRadius
415 // purpose  : Get radius
416 //=================================================================================
417 double OperationGUI_Fillet1d2dDlg::getRadius() const
418 {
419   return GroupVertexes ->SpinBox_DX->value();
420 }