Salome HOME
22752: [EDF] Provide explicit feedback on what has been done by Shape Processing...
[modules/geom.git] / src / RepairGUI / RepairGUI_LimitToleranceDlg.cxx
1 // Copyright (C) 2007-2014  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
46 #include <GEOMImpl_Types.hxx>
47
48 #include <TCollection_AsciiString.hxx>
49
50 #define DEFAULT_TOLERANCE_VALUE 1e-07
51
52 //=================================================================================
53 // class    : RepairGUI_LimitToleranceDlg()
54 // purpose  : Constructs a RepairGUI_LimitToleranceDlg  which is a child of 'parent', with the
55 //            name 'name' and widget flags set to 'f'.
56 //            The dialog will by default be modeless, unless you set 'modal' to
57 //            TRUE to construct a modal dialog.
58 //=================================================================================
59 RepairGUI_LimitToleranceDlg::RepairGUI_LimitToleranceDlg(GeometryGUI* theGeometryGUI,
60                                                          QWidget* parent, bool modal)
61   : GEOMBase_Skeleton(theGeometryGUI, parent, modal)
62 {
63   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_LIMIT_TOLERANCE")));
64   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
65
66   setWindowTitle(tr("GEOM_LIMIT_TOLERANCE_TITLE"));
67
68   /***************************************************************/
69   mainFrame()->GroupConstructors->setTitle(tr("GEOM_LIMIT_TOLERANCE_TITLE"));
70   mainFrame()->RadioButton1->setIcon(image0);
71   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
72   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
73   mainFrame()->RadioButton2->close();
74   mainFrame()->RadioButton3->close();
75
76   GroupPoints = new DlgRef_1SelExt(centralWidget());
77   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
78   GroupPoints->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
79   GroupPoints->PushButton1->setIcon(image1);
80   GroupPoints->LineEdit1->setReadOnly(true);
81
82   QLabel* aTolLab = new QLabel(tr("GEOM_TOLERANCE"), GroupPoints->Box);
83   myTolEdt = new SalomeApp_DoubleSpinBox(GroupPoints->Box);
84   initSpinBox(myTolEdt, 0., 100., DEFAULT_TOLERANCE_VALUE, "len_tol_precision");
85   myTolEdt->setValue(DEFAULT_TOLERANCE_VALUE);
86
87   QGridLayout* boxLayout = new QGridLayout(GroupPoints->Box);
88   boxLayout->setMargin(0); boxLayout->setSpacing(6);
89   boxLayout->addWidget(aTolLab,  0, 0);
90   boxLayout->addWidget(myTolEdt, 0, 2);
91
92   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
93   layout->setMargin(0); layout->setSpacing(6);
94   layout->addWidget(GroupPoints);
95
96   setHelpFileName("limit_tolerance_operation_page.html");
97
98   Init();
99 }
100
101
102 //=================================================================================
103 // function : ~RepairGUI_LimitToleranceDlg()
104 // purpose  : Destroys the object and frees any allocated resources
105 //=================================================================================
106 RepairGUI_LimitToleranceDlg::~RepairGUI_LimitToleranceDlg()
107 {
108 }
109
110
111 //=================================================================================
112 // function : Init()
113 // purpose  :
114 //=================================================================================
115 void RepairGUI_LimitToleranceDlg::Init()
116 {
117   /* init variables */
118   myEditCurrentArgument = GroupPoints->LineEdit1;
119
120   myObject = GEOM::GEOM_Object::_nil();
121
122   //myGeomGUI->SetState(0);
123   //globalSelection(GEOM_COMPOUND);
124
125   mainFrame()->GroupBoxPublish->show();
126   //Hide preview checkbox
127   mainFrame()->CheckBoxPreview->hide();
128
129   /* signals and slots connections */
130   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
131   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
132
133   connect(GroupPoints->PushButton1,  SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
134   connect(GroupPoints->LineEdit1,    SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
135
136   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
137           this, SLOT(SelectionIntoArgument()));
138
139   initName(tr("LIMIT_TOLERANCE_NEW_OBJ_NAME"));
140
141   ConstructorsClicked(0);
142
143   activateSelection();
144   updateButtonState();
145 }
146
147
148 //=================================================================================
149 // function : ConstructorsClicked()
150 // purpose  : Radio button management
151 //=================================================================================
152 void RepairGUI_LimitToleranceDlg::ConstructorsClicked(int constructorId)
153 {
154   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
155
156   GroupPoints->show();
157   GroupPoints->LineEdit1->setText("");
158   myEditCurrentArgument = GroupPoints->LineEdit1;
159   myEditCurrentArgument->setFocus();
160
161   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
162            this, SLOT(SelectionIntoArgument()));
163
164   qApp->processEvents();
165   updateGeometry();
166   resize(minimumSizeHint());
167
168   updateButtonState();
169   activateSelection();
170   SelectionIntoArgument();
171 }
172
173
174 //=================================================================================
175 // function : ClickOnOk()
176 // purpose  : Same than click on apply but close this dialog.
177 //=================================================================================
178 void RepairGUI_LimitToleranceDlg::ClickOnOk()
179 {
180   setIsApplyAndClose( true );
181   if (ClickOnApply())
182     ClickOnCancel();
183 }
184
185 //=================================================================================
186 // function : ClickOnApply()
187 // purpose  :
188 //=================================================================================
189 bool RepairGUI_LimitToleranceDlg::ClickOnApply()
190 {
191   if (!onAcceptLocal())
192     return false;
193
194   initName();
195
196   ConstructorsClicked(0);
197
198   return true;
199 }
200
201 //=================================================================================
202 // function : SelectionIntoArgument()
203 // purpose  : Called when selection
204 //=================================================================================
205 void RepairGUI_LimitToleranceDlg::SelectionIntoArgument()
206 {
207   myEditCurrentArgument->setText("");
208   myObject = GEOM::GEOM_Object::_nil();
209
210   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
211   SALOME_ListIO aSelList;
212   aSelMgr->selectedObjects(aSelList);
213
214   if (aSelList.Extent() == 1) {
215     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
216     myObject = GEOMBase::ConvertIOinGEOMObject( anIO );
217     if ( !CORBA::is_nil( myObject ) )
218       myEditCurrentArgument->setText(GEOMBase::GetName(myObject));
219   }
220   updateButtonState();
221 }
222
223 //=================================================================================
224 // function : SetEditCurrentArgument()
225 // purpose  :
226 //=================================================================================
227 void RepairGUI_LimitToleranceDlg::SetEditCurrentArgument()
228 {
229   const QObject* send = sender();
230   if (send == GroupPoints->PushButton1)  {
231     myEditCurrentArgument->setFocus();
232     SelectionIntoArgument();
233   }
234 }
235
236 //=================================================================================
237 // function : LineEditReturnPressed()
238 // purpose  :
239 //=================================================================================
240 void RepairGUI_LimitToleranceDlg::LineEditReturnPressed()
241 {
242   const QObject* send = sender();
243   if (send == GroupPoints->LineEdit1) {
244     myEditCurrentArgument = GroupPoints->LineEdit1;
245     GEOMBase_Skeleton::LineEditReturnPressed();
246   }
247 }
248
249 //=================================================================================
250 // function : ActivateThisDialog()
251 // purpose  :
252 //=================================================================================
253 void RepairGUI_LimitToleranceDlg::ActivateThisDialog()
254 {
255   GEOMBase_Skeleton::ActivateThisDialog();
256   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
257            this, SLOT(SelectionIntoArgument()));
258   activateSelection();
259 }
260
261 //=================================================================================
262 // function : enterEvent()
263 // purpose  : Mouse enter onto the dialog to activate it
264 //=================================================================================
265 void RepairGUI_LimitToleranceDlg::enterEvent(QEvent*)
266 {
267   if (!mainFrame()->GroupConstructors->isEnabled())
268     ActivateThisDialog();
269 }
270
271 //=================================================================================
272 // function : createOperation
273 // purpose  :
274 //=================================================================================
275 GEOM::GEOM_IOperations_ptr RepairGUI_LimitToleranceDlg::createOperation()
276 {
277   return getGeomEngine()->GetIHealingOperations(getStudyId());
278 }
279
280 //=================================================================================
281 // function : isValid
282 // purpose  :
283 //=================================================================================
284 bool RepairGUI_LimitToleranceDlg::isValid(QString& msg)
285 {
286   double v = myTolEdt->value();
287   bool ok = myTolEdt->isValid(msg, true);
288   return !myObject->_is_nil() && (v > 0.) && ok;
289 }
290
291 //=================================================================================
292 // function : execute
293 // purpose  :
294 //=================================================================================
295 bool RepairGUI_LimitToleranceDlg::execute(ObjectList& objects)
296 {
297   bool aResult = false;
298   objects.clear();
299
300   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow(getOperation());
301   GEOM::GEOM_Object_var anObj = anOper->LimitTolerance(myObject, myTolEdt->value());
302   aResult = !anObj->_is_nil();
303   if (aResult) {
304     QStringList aParameters;
305     aParameters << myTolEdt->text();
306     anObj->SetParameters(aParameters.join(":").toLatin1().constData());
307     if ( !IsPreview() )
308       RepairGUI::ShowStatistics( anOper, this );
309     objects.push_back(anObj._retn());
310   }
311
312   return aResult;
313 }
314
315 //================================================================
316 // Function : onAccept
317 // Purpose  : This method should be called from dialog's slots onOk() and onApply()
318 //            It perfroms user input validation, then it
319 //            performs a proper operation and manages transactions, etc.
320 //================================================================
321 bool RepairGUI_LimitToleranceDlg::onAcceptLocal()
322 {
323   if (!getStudy() || !(getStudy()->studyDS()))
324     return false;
325
326   _PTR(Study) aStudy = getStudy()->studyDS();
327
328   bool aLocked = aStudy->GetProperties()->IsLocked();
329   if (aLocked) {
330     MESSAGE("GEOMBase_Helper::onAccept - ActiveStudy is locked");
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.toLatin1().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::Study_ptr   theStudy,
437                                                    SALOMEDS::SObject_ptr theSObject)
438 {
439   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
440     // empty list of arguments means that all arguments should be restored
441     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
442                                         GEOM::FSM_GetInPlace, /*theInheritFirstArg=*/true,
443                                         mainFrame()->CheckBoxAddPrefix->isChecked());
444   }
445 }