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