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