Salome HOME
6e917d53021da02567c3fdec160f4dfc173c122d
[modules/geom.git] / src / RepairGUI / RepairGUI_LimitToleranceDlg.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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   : RepairGUI_LimitToleranceDlg.cxx
23
24 #include "RepairGUI_LimitToleranceDlg.h"
25
26 #include "DlgRef.h"
27 #include "GeometryGUI.h"
28 #include "GEOMBase.h"
29 #include "RepairGUI.h"
30
31 #include <SalomeApp_DoubleSpinBox.h>
32 #include <SalomeApp_Application.h>
33 #include <LightApp_SelectionMgr.h>
34 #include <SalomeApp_Study.h>
35 #include <SalomeApp_Tools.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_OverrideCursor.h>
40 #include <SUIT_ResourceMgr.h>
41 #include <SUIT_ViewWindow.h>
42 #include <SUIT_ViewManager.h>
43 #include <OCCViewer_ViewModel.h>
44 #include <SALOME_ListIO.hxx>
45 #include "utilities.h"
46
47 #include <GEOMImpl_Types.hxx>
48
49 #include <TCollection_AsciiString.hxx>
50
51 #define DEFAULT_TOLERANCE_VALUE 1e-07
52
53 //=================================================================================
54 // class    : RepairGUI_LimitToleranceDlg()
55 // purpose  : Constructs a RepairGUI_LimitToleranceDlg  which is a child of 'parent', with the
56 //            name 'name' and widget flags set to 'f'.
57 //            The dialog will by default be modeless, unless you set 'modal' to
58 //            TRUE to construct a modal dialog.
59 //=================================================================================
60 RepairGUI_LimitToleranceDlg::RepairGUI_LimitToleranceDlg(GeometryGUI* theGeometryGUI,
61                                                          QWidget* parent, bool modal)
62   : GEOMBase_Skeleton(theGeometryGUI, parent, modal)
63 {
64   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_LIMIT_TOLERANCE")));
65   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
66
67   setWindowTitle(tr("GEOM_LIMIT_TOLERANCE_TITLE"));
68
69   /***************************************************************/
70   mainFrame()->GroupConstructors->setTitle(tr("GEOM_LIMIT_TOLERANCE_TITLE"));
71   mainFrame()->RadioButton1->setIcon(image0);
72   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
73   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
74   mainFrame()->RadioButton2->close();
75   mainFrame()->RadioButton3->close();
76
77   GroupPoints = new DlgRef_1SelExt(centralWidget());
78   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
79   GroupPoints->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
80   GroupPoints->PushButton1->setIcon(image1);
81   GroupPoints->LineEdit1->setReadOnly(true);
82
83   QLabel* aTolLab = new QLabel(tr("GEOM_TOLERANCE"), GroupPoints->Box);
84   myTolEdt = new SalomeApp_DoubleSpinBox(GroupPoints->Box);
85   initSpinBox(myTolEdt, 0., 100., DEFAULT_TOLERANCE_VALUE, "len_tol_precision");
86   myTolEdt->setValue(DEFAULT_TOLERANCE_VALUE);
87
88   QGridLayout* boxLayout = new QGridLayout(GroupPoints->Box);
89   boxLayout->setMargin(0); boxLayout->setSpacing(6);
90   boxLayout->addWidget(aTolLab,  0, 0);
91   boxLayout->addWidget(myTolEdt, 0, 2);
92
93   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
94   layout->setMargin(0); layout->setSpacing(6);
95   layout->addWidget(GroupPoints);
96
97   setHelpFileName("limit_tolerance_operation_page.html");
98
99   Init();
100 }
101
102
103 //=================================================================================
104 // function : ~RepairGUI_LimitToleranceDlg()
105 // purpose  : Destroys the object and frees any allocated resources
106 //=================================================================================
107 RepairGUI_LimitToleranceDlg::~RepairGUI_LimitToleranceDlg()
108 {
109 }
110
111
112 //=================================================================================
113 // function : Init()
114 // purpose  :
115 //=================================================================================
116 void RepairGUI_LimitToleranceDlg::Init()
117 {
118   /* init variables */
119   myEditCurrentArgument = GroupPoints->LineEdit1;
120
121   myObject = GEOM::GEOM_Object::_nil();
122
123   //myGeomGUI->SetState(0);
124   //globalSelection(GEOM_COMPOUND);
125
126   mainFrame()->GroupBoxPublish->show();
127   //Hide preview checkbox
128   mainFrame()->CheckBoxPreview->hide();
129
130   /* signals and slots connections */
131   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
132   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
133
134   connect(GroupPoints->PushButton1,  SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
135   connect(GroupPoints->LineEdit1,    SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
136
137   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
138           this, SLOT(SelectionIntoArgument()));
139
140   initName(tr("LIMIT_TOLERANCE_NEW_OBJ_NAME"));
141
142   ConstructorsClicked(0);
143
144   activateSelection();
145   updateButtonState();
146 }
147
148
149 //=================================================================================
150 // function : ConstructorsClicked()
151 // purpose  : Radio button management
152 //=================================================================================
153 void RepairGUI_LimitToleranceDlg::ConstructorsClicked(int /*constructorId*/)
154 {
155   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
156
157   GroupPoints->show();
158   GroupPoints->LineEdit1->setText("");
159   myEditCurrentArgument = GroupPoints->LineEdit1;
160   myEditCurrentArgument->setFocus();
161
162   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
163            this, SLOT(SelectionIntoArgument()));
164
165   qApp->processEvents();
166   updateGeometry();
167   resize(minimumSizeHint());
168
169   updateButtonState();
170   activateSelection();
171   SelectionIntoArgument();
172 }
173
174
175 //=================================================================================
176 // function : ClickOnOk()
177 // purpose  : Same than click on apply but close this dialog.
178 //=================================================================================
179 void RepairGUI_LimitToleranceDlg::ClickOnOk()
180 {
181   setIsApplyAndClose( true );
182   if (ClickOnApply())
183     ClickOnCancel();
184 }
185
186 //=================================================================================
187 // function : ClickOnApply()
188 // purpose  :
189 //=================================================================================
190 bool RepairGUI_LimitToleranceDlg::ClickOnApply()
191 {
192   if (!onAcceptLocal())
193     return false;
194
195   initName();
196
197   ConstructorsClicked(0);
198
199   return true;
200 }
201
202 //=================================================================================
203 // function : SelectionIntoArgument()
204 // purpose  : Called when selection
205 //=================================================================================
206 void RepairGUI_LimitToleranceDlg::SelectionIntoArgument()
207 {
208   myEditCurrentArgument->setText("");
209   myObject = GEOM::GEOM_Object::_nil();
210
211   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
212   SALOME_ListIO aSelList;
213   aSelMgr->selectedObjects(aSelList);
214
215   if (aSelList.Extent() == 1) {
216     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
217     myObject = GEOMBase::ConvertIOinGEOMObject( anIO );
218     if ( !CORBA::is_nil( myObject ) )
219       myEditCurrentArgument->setText(GEOMBase::GetName(myObject));
220   }
221   updateButtonState();
222 }
223
224 //=================================================================================
225 // function : SetEditCurrentArgument()
226 // purpose  :
227 //=================================================================================
228 void RepairGUI_LimitToleranceDlg::SetEditCurrentArgument()
229 {
230   const QObject* send = sender();
231   if (send == GroupPoints->PushButton1)  {
232     myEditCurrentArgument->setFocus();
233     SelectionIntoArgument();
234   }
235 }
236
237 //=================================================================================
238 // function : LineEditReturnPressed()
239 // purpose  :
240 //=================================================================================
241 void RepairGUI_LimitToleranceDlg::LineEditReturnPressed()
242 {
243   const QObject* send = sender();
244   if (send == GroupPoints->LineEdit1) {
245     myEditCurrentArgument = GroupPoints->LineEdit1;
246     GEOMBase_Skeleton::LineEditReturnPressed();
247   }
248 }
249
250 //=================================================================================
251 // function : ActivateThisDialog()
252 // purpose  :
253 //=================================================================================
254 void RepairGUI_LimitToleranceDlg::ActivateThisDialog()
255 {
256   GEOMBase_Skeleton::ActivateThisDialog();
257   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
258            this, SLOT(SelectionIntoArgument()));
259   activateSelection();
260 }
261
262 //=================================================================================
263 // function : enterEvent()
264 // purpose  : Mouse enter onto the dialog to activate it
265 //=================================================================================
266 void RepairGUI_LimitToleranceDlg::enterEvent(QEvent*)
267 {
268   if (!mainFrame()->GroupConstructors->isEnabled())
269     ActivateThisDialog();
270 }
271
272 //=================================================================================
273 // function : createOperation
274 // purpose  :
275 //=================================================================================
276 GEOM::GEOM_IOperations_ptr RepairGUI_LimitToleranceDlg::createOperation()
277 {
278   return getGeomEngine()->GetIHealingOperations();
279 }
280
281 //=================================================================================
282 // function : isValid
283 // purpose  :
284 //=================================================================================
285 bool RepairGUI_LimitToleranceDlg::isValid(QString& msg)
286 {
287   double v = myTolEdt->value();
288   bool ok = myTolEdt->isValid(msg, true);
289   return !myObject->_is_nil() && (v > 0.) && ok;
290 }
291
292 //=================================================================================
293 // function : execute
294 // purpose  :
295 //=================================================================================
296 bool RepairGUI_LimitToleranceDlg::execute(ObjectList& objects)
297 {
298   bool aResult = false;
299   objects.clear();
300
301   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow(getOperation());
302   GEOM::GEOM_Object_var anObj = anOper->LimitTolerance(myObject, myTolEdt->value());
303   aResult = !anObj->_is_nil();
304   if (aResult) {
305     QStringList aParameters;
306     aParameters << myTolEdt->text();
307     anObj->SetParameters(aParameters.join(":").toUtf8().constData());
308     if ( !IsPreview() )
309       RepairGUI::ShowStatistics( anOper, this );
310     objects.push_back(anObj._retn());
311   }
312
313   return aResult;
314 }
315
316 //================================================================
317 // Function : onAccept
318 // Purpose  : This method should be called from dialog's slots onOk() and onApply()
319 //            It performs user input validation, then it
320 //            performs a proper operation and manages transactions, etc.
321 //================================================================
322 bool RepairGUI_LimitToleranceDlg::onAcceptLocal()
323 {
324   if (!getStudy() || !(getStudy()->studyDS()))
325     return false;
326
327   _PTR(Study) aStudy = getStudy()->studyDS();
328
329   bool aLocked = aStudy->GetProperties()->IsLocked();
330   if (aLocked) {
331     SUIT_MessageBox::warning(this, tr("WRN_WARNING"), tr("WRN_STUDY_LOCKED"), tr("BUT_OK"));
332     return false;
333   }
334
335   QString msg;
336   if (!isValid(msg)) {
337     showError(msg);
338     return false;
339   }
340
341   try {
342     if (openCommand()) {
343       SUIT_OverrideCursor wc;
344
345       myGeomGUI->getApp()->putInfo("");
346       ObjectList objects;
347
348       if (!execute(objects)) {
349         wc.suspend();
350         abortCommand();
351         showError();
352       }
353       else {
354         const int nbObjs = objects.size();
355         for (ObjectList::iterator it = objects.begin(); it != objects.end(); ++it) {
356           QString aName = getNewObjectName();
357           if (nbObjs > 1) {
358             if (aName.isEmpty())
359               aName = getPrefix(*it);
360             aName = GEOMBase::GetDefaultName(aName);
361           }
362           else {
363             // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
364             if (aName.isEmpty())
365               aName = GEOMBase::GetDefaultName(getPrefix(*it));
366           }
367           addInStudy(*it, aName.toUtf8().data());
368           display(*it, false);
369         }
370
371         if (nbObjs) {
372           commitCommand();
373           updateObjBrowser();
374           myGeomGUI->getApp()->putInfo(QObject::tr("GEOM_PRP_DONE"));
375         }
376         else {
377           abortCommand();
378         }
379
380         // JFA 28.12.2004 BEGIN // To enable warnings
381         GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow(getOperation());
382         if (!CORBA::is_nil(anOper) && !anOper->IsDone()) {
383           wc.suspend();
384           QString msgw = QObject::tr(anOper->GetErrorCode());
385           SUIT_MessageBox::warning(this, tr("WRN_WARNING"), msgw, tr("BUT_OK"));
386         }
387         // JFA 28.12.2004 END
388       }
389     }
390   }
391   catch(const SALOME::SALOME_Exception& e) {
392     SalomeApp_Tools::QtCatchCorbaException(e);
393     abortCommand();
394   }
395
396   updateViewer();
397   activateSelection();
398   updateButtonState();
399
400   return true;
401 }
402
403 //=================================================================================
404 // function : activateSelection
405 // purpose  : Activate selection
406 //=================================================================================
407 void RepairGUI_LimitToleranceDlg::activateSelection()
408 {
409   disconnect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
410              this, SLOT(SelectionIntoArgument()));
411
412   globalSelection(GEOM_ALLSHAPES);
413   if (myObject->_is_nil())
414     SelectionIntoArgument();
415
416   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
417           this, SLOT(SelectionIntoArgument()));
418   updateViewer();
419 }
420
421 //=================================================================================
422 // function : updateButtonState
423 // purpose  : Update button state
424 //=================================================================================
425 void RepairGUI_LimitToleranceDlg::updateButtonState()
426 {
427   bool hasMainObj = !myObject->_is_nil();
428   buttonOk()->setEnabled(hasMainObj);
429   buttonApply()->setEnabled(hasMainObj);
430 }
431
432 //=================================================================================
433 // function : restoreSubShapes
434 // purpose  :
435 //=================================================================================
436 void RepairGUI_LimitToleranceDlg::restoreSubShapes(SALOMEDS::SObject_ptr theSObject)
437 {
438   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
439     // empty list of arguments means that all arguments should be restored
440     getGeomEngine()->RestoreSubShapesSO(theSObject, GEOM::ListOfGO(),
441                                         GEOM::FSM_GetInPlace, /*theInheritFirstArg=*/true,
442                                         mainFrame()->CheckBoxAddPrefix->isChecked());
443   }
444 }
445
446 //=================================================================================
447 // function : getSourceObjects
448 // purpose  : virtual method to get source objects
449 //=================================================================================
450 QList<GEOM::GeomObjPtr> RepairGUI_LimitToleranceDlg::getSourceObjects()
451 {
452   QList<GEOM::GeomObjPtr> res;
453   GEOM::GeomObjPtr aGeomObjPtr(myObject);
454   res << aGeomObjPtr;
455   return res;
456 }