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