Salome HOME
Update copyrights 2014.
[modules/geom.git] / src / RepairGUI / RepairGUI_GlueDlg.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
23 // GEOM RepairGUI : GUI for Geometry component
24 // File   : RepairGUI_GlueDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26
27 #include "RepairGUI_GlueDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32 #include <SalomeApp_DoubleSpinBox.h>
33
34 #include <SalomeApp_Application.h>
35 #include <SalomeApp_Study.h>
36 #include <SalomeApp_Tools.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <SUIT_Session.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_OverrideCursor.h>
42 #include <SUIT_ResourceMgr.h>
43 #include <SUIT_ViewWindow.h>
44 #include <SUIT_ViewManager.h>
45 #include <OCCViewer_ViewModel.h>
46 #include <SALOME_ListIteratorOfListIO.hxx>
47
48 #include <GEOMImpl_Types.hxx>
49
50 #include <TCollection_AsciiString.hxx>
51
52 #define DEFAULT_TOLERANCE_VALUE 1e-07
53
54 //=================================================================================
55 // class    : RepairGUI_GlueDlg()
56 // purpose  : Constructs a RepairGUI_GlueDlg  which is a child of 'parent', with the
57 //            name 'name' and widget flags set to 'f'.
58 //            The dialog will by default be modeless, unless you set 'modal' to
59 //            TRUE to construct a modal dialog.
60 //=================================================================================
61 RepairGUI_GlueDlg::RepairGUI_GlueDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
62                                      bool modal, TopAbs_ShapeEnum theGlueMode)
63   : GEOMBase_Skeleton(theGeometryGUI, parent, modal),
64     myCurrConstrId(-1), myGlueMode(theGlueMode)
65 {
66   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
67   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_GLUE_FACES")));
68   QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_GLUE_FACES2")));
69   QPixmap image3 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_GLUE_EDGES")));
70   QPixmap image4 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_GLUE_EDGES2")));
71
72   QString aTitle;
73   if (theGlueMode == TopAbs_FACE)
74     aTitle = tr("GEOM_GLUE_FACES_TITLE");
75   else if (theGlueMode == TopAbs_EDGE)
76     aTitle = tr("GEOM_GLUE_EDGES_TITLE");
77   setWindowTitle(aTitle);
78
79   /***************************************************************/
80   mainFrame()->GroupConstructors->setTitle(aTitle);
81   if (theGlueMode == TopAbs_FACE) {
82     mainFrame()->RadioButton1->setIcon(image1);
83     mainFrame()->RadioButton2->setIcon(image2);
84   }
85   else if (theGlueMode == TopAbs_EDGE) {
86     mainFrame()->RadioButton1->setIcon(image3);
87     mainFrame()->RadioButton2->setIcon(image4);
88   }
89   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
90   mainFrame()->RadioButton3->close();
91
92   GroupPoints = new DlgRef_1SelExt(centralWidget());
93   GroupPoints->GroupBox1->setTitle(tr("GEOM_GLUE"));
94   GroupPoints->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
95   GroupPoints->PushButton1->setIcon(image0);
96   GroupPoints->LineEdit1->setReadOnly(true);
97
98   QLabel* aTolLab = new QLabel(tr("GEOM_TOLERANCE"), GroupPoints->Box);
99   myTolEdt = new SalomeApp_DoubleSpinBox(GroupPoints->Box);
100   initSpinBox(myTolEdt, 0., 100., DEFAULT_TOLERANCE_VALUE, "len_tol_precision");
101   myTolEdt->setValue(DEFAULT_TOLERANCE_VALUE);
102
103   QGridLayout* boxLayout = new QGridLayout(GroupPoints->Box);
104   boxLayout->setMargin(0); boxLayout->setSpacing(6);
105   boxLayout->addWidget(aTolLab,  0, 0);
106   boxLayout->addWidget(myTolEdt, 0, 2);
107   /***************************************************************/
108
109   GroupPoints2 = new DlgRef_1SelExt(centralWidget());
110   GroupPoints2->GroupBox1->setTitle(tr("GEOM_GLUE"));
111   GroupPoints2->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
112   GroupPoints2->PushButton1->setIcon(image0);
113   GroupPoints2->LineEdit1->setReadOnly(true);
114
115   QLabel* aTolLab2 = new QLabel(tr("GEOM_TOLERANCE"), GroupPoints2->Box);
116   myTolEdt2 = new SalomeApp_DoubleSpinBox(GroupPoints2->Box);
117   initSpinBox(myTolEdt2, 0., 100., DEFAULT_TOLERANCE_VALUE, "len_tol_precision");
118   myTolEdt2->setValue(DEFAULT_TOLERANCE_VALUE);
119
120   QString aGlueString (" [%1]");
121   QString aSelString;
122   if (theGlueMode == TopAbs_FACE) {
123     aGlueString = aGlueString.arg(tr("GLUE_FACES"));
124     aSelString = tr("SELECT_FACES");
125   }
126   else if (theGlueMode == TopAbs_EDGE) {
127     aGlueString = aGlueString.arg(tr("GLUE_EDGES"));
128     aSelString = tr("SELECT_EDGES");
129   }
130   myDetectBtn = new QPushButton (tr("GEOM_DETECT") + aGlueString, GroupPoints2->Box);
131   mySubShapesChk = new QCheckBox (aSelString, GroupPoints2->Box);
132   myGlueAllEdgesChk = 0;
133
134   boxLayout = new QGridLayout(GroupPoints2->Box);
135   boxLayout->setMargin(0); boxLayout->setSpacing(6);
136   boxLayout->addWidget(aTolLab2,    0, 0);
137   boxLayout->addWidget(myTolEdt2,   0, 2);
138   boxLayout->addWidget(myDetectBtn, 1, 0, 1, 3);
139   boxLayout->addWidget(mySubShapesChk, 2, 0, 1, 3);
140
141   if (theGlueMode == TopAbs_FACE) {
142     myGlueAllEdgesChk = new QCheckBox (tr("GLUE_ALL_EDGES"), GroupPoints2->Box);
143     boxLayout->addWidget(myGlueAllEdgesChk, 3, 0, 1, 3);
144     myGlueAllEdgesChk->setChecked(false);
145   }
146
147   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
148   layout->setMargin(0); layout->setSpacing(6);
149   layout->addWidget(GroupPoints);
150   layout->addWidget(GroupPoints2);
151
152   /***************************************************************/
153
154   QString aHelpFileName; 
155   switch ( myGlueMode ) {
156     case TopAbs_EDGE:
157       {
158         aHelpFileName = "glue_edges_operation_page.html";
159         break;
160       }
161     case TopAbs_FACE:
162       {
163         aHelpFileName = "glue_faces_operation_page.html";
164         break;
165       }
166   }
167   setHelpFileName(aHelpFileName);
168
169   // Disable second way of gluing if OCC viewer is not active one
170   if (myGeomGUI->getApp()->desktop()->activeWindow()->getViewManager()->getType()
171        != OCCViewer_Viewer::Type())
172     mainFrame()->RadioButton2->setEnabled(false);
173
174   Init();
175 }
176
177 //=================================================================================
178 // function : ~RepairGUI_GlueDlg()
179 // purpose  : Destroys the object and frees any allocated resources
180 //=================================================================================
181 RepairGUI_GlueDlg::~RepairGUI_GlueDlg()
182 {
183 }
184
185 //=================================================================================
186 // function : Init()
187 // purpose  :
188 //=================================================================================
189 void RepairGUI_GlueDlg::Init()
190 {
191   /* init variables */
192   myEditCurrentArgument = GroupPoints->LineEdit1;
193
194   myObject = GEOM::GEOM_Object::_nil();
195
196   //myGeomGUI->SetState(0);
197   //globalSelection(GEOM_COMPOUND);
198
199   mainFrame()->GroupBoxPublish->show();
200   //Hide preview checkbox
201   mainFrame()->CheckBoxPreview->hide();
202
203   /* signals and slots connections */
204   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
205   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
206
207   connect(this,          SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
208
209   connect(GroupPoints->PushButton1,  SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
210   connect(GroupPoints->LineEdit1,    SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
211
212   connect(GroupPoints2->PushButton1, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
213   connect(GroupPoints2->LineEdit1,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
214   connect(myTolEdt2, SIGNAL(valueChanged(double)), this, SLOT(onTolerChanged(double)));
215   connect(mySubShapesChk, SIGNAL(stateChanged(int)), this, SLOT(onSubShapesChk()));
216
217   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
218            this, SLOT(SelectionIntoArgument()));
219
220   connect(myDetectBtn, SIGNAL(clicked()), this, SLOT(onDetect()));
221
222   initName(tr("GLUE_NEW_OBJ_NAME"));
223
224   ConstructorsClicked(0);
225
226   activateSelection();
227   updateButtonState();
228 }
229
230 //=================================================================================
231 // function : ConstructorsClicked()
232 // purpose  : Radio button management
233 //=================================================================================
234 void RepairGUI_GlueDlg::ConstructorsClicked(int constructorId)
235 {
236   if (myCurrConstrId == constructorId)
237     return;
238
239   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
240
241   switch (constructorId) {
242   case 0:
243     GroupPoints2->hide();
244     GroupPoints->show();
245     GroupPoints->LineEdit1->setText("");
246     myEditCurrentArgument = GroupPoints->LineEdit1;
247
248     if (myCurrConstrId >= 0) {
249       // i.e. it is not initialisation
250       // copy tolerance from previous tolerance field
251       myTolEdt->setValue(myTolEdt2->value());
252     }
253     break;
254   case 1:
255     GroupPoints->hide();
256     GroupPoints2->show();
257     GroupPoints->LineEdit1->setText("");
258     myEditCurrentArgument = GroupPoints2->LineEdit1;
259
260     if (myCurrConstrId >= 0) {
261       // i.e. it is not initialisation
262       // copy tolerance from previous tolerance field
263       myTolEdt2->setValue(myTolEdt->value());
264       mySubShapesChk->setChecked(false);
265       clearTemporary();
266     }
267     break;
268   }
269
270   myCurrConstrId = constructorId;
271
272   myEditCurrentArgument->setFocus();
273
274   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
275            this, SLOT(SelectionIntoArgument()));
276
277   qApp->processEvents();
278   updateGeometry();
279   resize(minimumSizeHint());
280
281   processPreview();
282   updateButtonState();
283   activateSelection();
284   SelectionIntoArgument();
285 }
286
287 //=================================================================================
288 // function : ClickOnOk()
289 // purpose  : Same than click on apply but close this dialog.
290 //=================================================================================
291 void RepairGUI_GlueDlg::ClickOnOk()
292 {
293   setIsApplyAndClose(true);
294   if (ClickOnApply())
295     ClickOnCancel();
296 }
297
298 //=================================================================================
299 // function : ClickOnApply()
300 // purpose  :
301 //=================================================================================
302 bool RepairGUI_GlueDlg::ClickOnApply()
303 {
304   if (!onAcceptLocal())
305     return false;
306
307   initName();
308
309   //GroupPoints->LineEdit1->setText("");
310   //myObject = GEOM::GEOM_Object::_nil();
311
312   //globalSelection(GEOM_COMPOUND);
313
314   ConstructorsClicked(getConstructorId());
315
316   return true;
317 }
318
319
320 //=================================================================================
321 // function : SelectionIntoArgument()
322 // purpose  : Called when selection
323 //=================================================================================
324 void RepairGUI_GlueDlg::SelectionIntoArgument()
325 {
326   if (mySubShapesChk->isChecked() &&  getConstructorId() == 1) {
327     updateButtonState();
328     return;
329   }
330
331   erasePreview();
332   myEditCurrentArgument->setText("");
333   myObject = GEOM::GEOM_Object::_nil();
334
335   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
336   SALOME_ListIO aSelList;
337   aSelMgr->selectedObjects(aSelList);
338
339   if (aSelList.Extent() == 1) {
340     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
341     myObject = GEOMBase::ConvertIOinGEOMObject(anIO);
342     if (!CORBA::is_nil(myObject))
343       myEditCurrentArgument->setText(GEOMBase::GetName(myObject));
344   }
345   updateButtonState();
346 }
347
348 //=================================================================================
349 // function : SetEditCurrentArgument()
350 // purpose  :
351 //=================================================================================
352 void RepairGUI_GlueDlg::SetEditCurrentArgument()
353 {
354   const QObject* send = sender();
355   if (send == GroupPoints->PushButton1 || send == GroupPoints2->PushButton1)  {
356     myEditCurrentArgument->setFocus();
357     SelectionIntoArgument();
358   }
359 }
360
361 //=================================================================================
362 // function : LineEditReturnPressed()
363 // purpose  :
364 //=================================================================================
365 void RepairGUI_GlueDlg::LineEditReturnPressed()
366 {
367   const QObject* send = sender();
368   if (send == GroupPoints->LineEdit1) {
369     myEditCurrentArgument = GroupPoints->LineEdit1;
370     GEOMBase_Skeleton::LineEditReturnPressed();
371   }
372   else if (send == GroupPoints2->LineEdit1) {
373     myEditCurrentArgument = GroupPoints2->LineEdit1;
374     GEOMBase_Skeleton::LineEditReturnPressed();
375   }
376 }
377
378 //=================================================================================
379 // function : ActivateThisDialog()
380 // purpose  :
381 //=================================================================================
382 void RepairGUI_GlueDlg::ActivateThisDialog()
383 {
384   GEOMBase_Skeleton::ActivateThisDialog();
385   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
386            this, SLOT(SelectionIntoArgument()));
387
388   //GroupPoints->LineEdit1->setText("");
389   //GroupPoints2->LineEdit1->setText("");
390   //myObject = GEOM::GEOM_Object::_nil();
391
392   //myGeomGUI->SetState(0);
393   //globalSelection(GEOM_COMPOUND);
394   activateSelection();
395 }
396
397 //=================================================================================
398 // function : enterEvent()
399 // purpose  : Mouse enter onto the dialog to activate it
400 //=================================================================================
401 void RepairGUI_GlueDlg::enterEvent(QEvent*)
402 {
403   if (!mainFrame()->GroupConstructors->isEnabled())
404     ActivateThisDialog();
405 }
406
407 //=================================================================================
408 // function : createOperation
409 // purpose  :
410 //=================================================================================
411 GEOM::GEOM_IOperations_ptr RepairGUI_GlueDlg::createOperation()
412 {
413   return getGeomEngine()->GetIShapesOperations(getStudyId());
414 }
415
416 //=================================================================================
417 // function : isValid
418 // purpose  :
419 //=================================================================================
420 bool RepairGUI_GlueDlg::isValid(QString& msg)
421 {
422   bool ok = true;
423   double v = 0;
424   switch (getConstructorId())
425   {
426   case 0:
427     v = myTolEdt->value();
428     ok = myTolEdt->isValid(msg, !IsPreview());
429     break;
430   case 1:
431     v = myTolEdt2->value();
432     ok = myTolEdt2->isValid(msg, !IsPreview());
433     break;
434   }
435   return !myObject->_is_nil() && (IsPreview() || v > 0.) && ok;
436 }
437
438 //=================================================================================
439 // function : execute
440 // purpose  :
441 //=================================================================================
442 bool RepairGUI_GlueDlg::execute(ObjectList& objects)
443 {
444   bool aResult = false;
445   objects.clear();
446
447   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow(getOperation());
448   switch (getConstructorId()) {
449   case 0:
450     {
451       GEOM::GEOM_Object_var anObj;
452       if (myGlueMode == TopAbs_FACE)
453         anObj = anOper->MakeGlueFaces(myObject, myTolEdt->value(), true);
454       else if (myGlueMode == TopAbs_EDGE)
455         anObj = anOper->MakeGlueEdges(myObject, myTolEdt->value());
456
457       aResult = !anObj->_is_nil();
458       if (aResult && !IsPreview())
459       {
460         QStringList aParameters;
461         aParameters << myTolEdt->text();
462         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
463
464         objects.push_back(anObj._retn());
465       }
466       break;
467     }
468   case 1:
469     {
470       if (IsPreview()) {
471         // if this method is used for displaying preview then we must detect glue faces/edges only
472         for (int i = 0; i < myTmpObjs.count(); i++) {
473           myTmpObjs[i].get()->Register(); // increment counter, since calling function will call UnRegister()
474           objects.push_back(myTmpObjs[i].copy());
475         }
476         return !myTmpObjs.isEmpty();
477       } // IsPreview
478
479       // Make glue faces/edges by list.
480       // Iterate through myTmpObjs and verifies where each object is currently selected or not.
481       QSet<QString> selected;
482
483       // Get names of selected objects
484       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
485       SALOME_ListIO aSelList;
486       aSelMgr->selectedObjects(aSelList);
487
488       SALOME_ListIteratorOfListIO it (aSelList);
489       for (; it.More(); it.Next())
490         selected.insert(it.Value()->getName());
491
492       // Iterate through result and select objects with names from selection
493       // ObjectList toRemoveFromEnggine;
494
495       // make glue faces/edges
496       GEOM::ListOfGO_var aListForGlue = new GEOM::ListOfGO();
497       aListForGlue->length(myTmpObjs.count());
498       int added = 0;
499       for (int i = 0; i < myTmpObjs.count(); i++) {
500         CORBA::String_var tmpior = myGeomGUI->getApp()->orb()->object_to_string(myTmpObjs[i].get());
501         if (selected.contains(tmpior.in()))
502           aListForGlue[ added++ ] = myTmpObjs[i].copy();
503       }
504       aListForGlue->length(added);
505
506       GEOM::GEOM_Object_var anObj;
507       if (myGlueMode == TopAbs_FACE) {
508         bool doGlueAllEdges = myGlueAllEdgesChk->isChecked();
509         anObj = anOper->MakeGlueFacesByList(myObject, myTolEdt2->value(), aListForGlue.in(),
510                                             true, doGlueAllEdges);
511       }
512       else if (myGlueMode == TopAbs_EDGE)
513         anObj = anOper->MakeGlueEdgesByList(myObject, myTolEdt2->value(), aListForGlue.in());
514
515       aResult = !anObj->_is_nil();
516
517       if (aResult) {
518         if (!IsPreview()) {
519           QStringList aParameters;
520           aParameters << myTolEdt2->text();
521           anObj->SetParameters(aParameters.join(":").toLatin1().constData());
522         }
523         objects.push_back(anObj._retn());
524       }
525
526       // Remove from engine useless objects
527       clearTemporary();
528
529       updateButtonState();
530
531       break;
532     } // case 1
533   } // switch
534
535   return aResult;
536 }
537
538 //=================================================================================
539 // function : restoreSubShapes
540 // purpose  :
541 //=================================================================================
542 void RepairGUI_GlueDlg::restoreSubShapes(SALOMEDS::Study_ptr   theStudy,
543                                           SALOMEDS::SObject_ptr theSObject)
544 {
545   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
546     GEOM::find_shape_method aFindMethod = GEOM::FSM_GetInPlace;
547     if (getConstructorId() == 0) // MakeGlueFaces or MakeGlueEdges
548       aFindMethod = GEOM::FSM_GetInPlaceByHistory;
549
550     // empty list of arguments means that all arguments should be restored
551     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
552                                          aFindMethod, /*theInheritFirstArg=*/true,
553                                          mainFrame()->CheckBoxAddPrefix->isChecked());
554   }
555 }
556
557 //================================================================
558 // Function : clearShapeBufferLocal
559 // Purpose  :
560 //================================================================
561 void RepairGUI_GlueDlg::clearShapeBufferLocal(GEOM::GEOM_Object_ptr theObj)
562 {
563   if (CORBA::is_nil(theObj))
564     return;
565
566   CORBA::String_var IOR = myGeomGUI->getApp()->orb()->object_to_string(theObj);
567   TCollection_AsciiString asciiIOR((char *)(IOR.in()));
568   myGeomGUI->GetShapeReader().RemoveShapeFromBuffer(asciiIOR);
569
570   if (!getStudy() || !(getStudy()->studyDS()))
571     return;
572
573   _PTR(Study) aStudy = getStudy()->studyDS();
574   _PTR(SObject) aSObj (aStudy->FindObjectIOR(std::string(IOR.in())));
575   if (!aSObj)
576     return;
577
578   _PTR(ChildIterator) anIt (aStudy->NewChildIterator(aSObj));
579   for (anIt->InitEx(true); anIt->More(); anIt->Next()) {
580     _PTR(GenericAttribute) anAttr;
581     if (anIt->Value()->FindAttribute(anAttr, "AttributeIOR")) {
582       _PTR(AttributeIOR) anIOR (anAttr);
583       TCollection_AsciiString asciiIOR((char*)anIOR->Value().c_str());
584       myGeomGUI->GetShapeReader().RemoveShapeFromBuffer(asciiIOR);
585     }
586   }
587 }
588
589 //================================================================
590 // Function : onAccept
591 // Purpose  : This method should be called from dialog's slots onOk() and onApply()
592 //            It perfroms user input validation, then it
593 //            performs a proper operation and manages transactions, etc.
594 //================================================================
595 bool RepairGUI_GlueDlg::onAcceptLocal()
596 {
597   if (!getStudy() || !(getStudy()->studyDS()))
598     return false;
599
600   _PTR(Study) aStudy = getStudy()->studyDS();
601
602   bool aLocked = aStudy->GetProperties()->IsLocked();
603   if (aLocked) {
604     MESSAGE("GEOMBase_Helper::onAccept - ActiveStudy is locked");
605     SUIT_MessageBox::warning(this, tr("WRN_WARNING"), tr("WRN_STUDY_LOCKED"), tr("BUT_OK"));
606     return false;
607   }
608
609   QString msg;
610   if (!isValid(msg)) {
611     showError(msg);
612     return false;
613   }
614
615   erasePreview(false);
616
617   try {
618     if (openCommand()) {
619       SUIT_OverrideCursor wc;
620
621       myGeomGUI->getApp()->putInfo("");
622       ObjectList objects;
623
624       if (!execute(objects)) {
625         wc.suspend();
626         abortCommand();
627         showError();
628       }
629       else {
630         const int nbObjs = objects.size();
631         for (ObjectList::iterator it = objects.begin(); it != objects.end(); ++it) {
632           QString aName = getNewObjectName();
633           if (nbObjs > 1) {
634             if (aName.isEmpty())
635               aName = getPrefix(*it);
636             aName = GEOMBase::GetDefaultName(aName);
637           }
638           else {
639             // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
640             if (aName.isEmpty())
641               aName = GEOMBase::GetDefaultName(getPrefix(*it));
642           }
643           addInStudy(*it, aName.toLatin1().data());
644           display(*it, false);
645         }
646
647         if (nbObjs) {
648           commitCommand();
649           updateObjBrowser();
650           myGeomGUI->getApp()->putInfo(QObject::tr("GEOM_PRP_DONE"));
651         }
652         else {
653           abortCommand();
654         }
655
656         // JFA 28.12.2004 BEGIN // To enable warnings
657         GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow(getOperation());
658         if (!CORBA::is_nil(anOper) && !anOper->IsDone()) {
659           wc.suspend();
660           QString msgw = QObject::tr(anOper->GetErrorCode());
661           SUIT_MessageBox::warning(this, tr("WRN_WARNING"), msgw, tr("BUT_OK"));
662         }
663         // JFA 28.12.2004 END
664       }
665     }
666   }
667   catch(const SALOME::SALOME_Exception& e) {
668     SalomeApp_Tools::QtCatchCorbaException(e);
669     abortCommand();
670   }
671
672   updateViewer();
673   activateSelection();
674   updateButtonState();
675
676   return true;
677 }
678
679 //=================================================================================
680 // function : onDetect
681 // purpose  :
682 //=================================================================================
683 void RepairGUI_GlueDlg::onDetect()
684 {
685   clearTemporary();
686   QString msg;
687   if (!isValid(msg)) {
688     showError(msg);
689     return;
690   }
691
692   buttonOk()->setEnabled(false);
693   buttonApply()->setEnabled(false);
694   globalSelection(GEOM_ALLSHAPES);
695
696   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow(getOperation());
697   GEOM::ListOfGO_var aList;
698   if (myGlueMode == TopAbs_FACE)
699     aList = anOper->GetGlueFaces(myObject.in(), myTolEdt2->value());
700   else if (myGlueMode == TopAbs_EDGE)
701     aList = anOper->GetGlueEdges(myObject.in(), myTolEdt2->value());
702
703   for (int i = 0, n = aList->length(); i < n; i++)
704     myTmpObjs << GEOM::GeomObjPtr(aList[i].in());
705
706   if (!myTmpObjs.isEmpty()) {
707     if (myGlueMode == TopAbs_FACE)
708       msg = tr("FACES_FOR_GLUING_ARE_DETECTED").arg(myTmpObjs.count());
709     else if (myGlueMode == TopAbs_EDGE)
710       msg = tr("EDGES_FOR_GLUING_ARE_DETECTED").arg(myTmpObjs.count());
711     mySubShapesChk->setChecked(true);
712   }
713   else {
714     if (myGlueMode == TopAbs_FACE)
715       msg = tr("THERE_ARE_NO_FACES_FOR_GLUING");
716     else if (myGlueMode == TopAbs_EDGE)
717       msg = tr("THERE_ARE_NO_EDGES_FOR_GLUING");
718   }
719
720   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
721           this, SLOT(SelectionIntoArgument())) ;
722   SUIT_MessageBox::information(this, tr("GEOM_FREE_BOUNDS_TLT"), msg, tr("Close"));
723   updateButtonState();
724   activateSelection();
725 }
726
727 //=================================================================================
728 // function : activateSelection
729 // purpose  : Redisplay preview and Activate selection
730 //=================================================================================
731 void RepairGUI_GlueDlg::activateSelection()
732 {
733   erasePreview(false);
734
735   int anId = getConstructorId();
736   if (anId == 0) {
737     // Case of whole gluing
738     disconnect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
739                 this, SLOT(SelectionIntoArgument()));
740
741     globalSelection(GEOM_ALLSHAPES);
742     if (myObject->_is_nil())
743       SelectionIntoArgument();
744
745     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
746              this, SLOT(SelectionIntoArgument()));
747   }
748   else {
749     // Second case of gluing
750     if (!mySubShapesChk->isChecked())
751       globalSelection(GEOM_ALLSHAPES);
752     else {
753       displayPreview(true, true, false, false, 2/*line width*/, 1/*display mode*/, Quantity_NOC_RED);
754       disconnect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
755                   this, SLOT(SelectionIntoArgument())) ;
756       globalSelection(GEOM_PREVIEW);
757       connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
758                this, SLOT(SelectionIntoArgument())) ;
759     }
760   }
761   updateViewer();
762 }
763
764 //=================================================================================
765 // function : updateButtonState
766 // purpose  : Update button state
767 //=================================================================================
768 void RepairGUI_GlueDlg::updateButtonState()
769 {
770   int anId = getConstructorId();
771   bool hasMainObj = !myObject->_is_nil();
772   if (anId == 0) {
773     buttonOk()->setEnabled(hasMainObj);
774     buttonApply()->setEnabled(hasMainObj);
775   }
776   else
777   {
778     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
779     SALOME_ListIO aSelList;
780     aSelMgr->selectedObjects(aSelList);
781
782     SALOME_ListIteratorOfListIO it (aSelList);
783     bool wasSelected = it.More() > 0;
784     bool wasDetected = !myTmpObjs.isEmpty();
785     buttonOk()->setEnabled(hasMainObj && wasDetected && wasSelected);
786     buttonApply()->setEnabled(hasMainObj && wasDetected && wasSelected);
787     mySubShapesChk->setEnabled(hasMainObj && wasDetected);
788     myDetectBtn->setEnabled(hasMainObj);
789     if (!hasMainObj || !wasDetected)
790       mySubShapesChk->setChecked(false);
791   }
792 }
793
794 //=================================================================================
795 // function : clearTemporary
796 // purpose  : Remove temporary objects from engine
797 //=================================================================================
798 void RepairGUI_GlueDlg::clearTemporary()
799 {
800   myTmpObjs.clear();
801 }
802
803 //=================================================================================
804 // function : onTolerChanged
805 // purpose  : Remove temporary objects from engine
806 //=================================================================================
807 void RepairGUI_GlueDlg::onTolerChanged(double /*theVal*/)
808 {
809   clearTemporary();
810   activateSelection();
811   updateButtonState();
812 }
813
814 //=================================================================================
815 // function : onSubShapesChk
816 // purpose  : Update selection mode
817 //=================================================================================
818 void RepairGUI_GlueDlg::onSubShapesChk()
819 {
820   if (!mySubShapesChk->isChecked())
821     clearTemporary();
822   activateSelection();
823   updateButtonState();
824 }
825
826 //=================================================================================
827 // function : ClickOnCancel
828 // purpose  : Remove temporary objects from engine and call method of base class
829 //=================================================================================
830 void RepairGUI_GlueDlg::ClickOnCancel()
831 {
832   clearTemporary();
833   GEOMBase_Skeleton::ClickOnCancel();
834 }