Salome HOME
Additional change to fix the preview problem
[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       aListForGlue->length( added );
467
468       GEOM::GEOM_Object_var anObj = anOper->MakeGlueFacesByList( myObject, myTolEdt2->value(), aListForGlue.in(), true );
469
470       aResult = !anObj->_is_nil();
471
472       if ( aResult )
473       {
474         if ( !IsPreview() )
475         {
476           QStringList aParameters;
477           aParameters << myTolEdt2->text();
478           anObj->SetParameters(aParameters.join(":").toLatin1().constData());
479         }
480         objects.push_back( anObj._retn() );
481       }
482
483       // Remove from engine useless objects
484       clearTemporary();
485
486       updateButtonState();
487
488       break;
489     } // case 1
490   } // switch
491
492   return aResult;
493 }
494
495 //=================================================================================
496 // function : restoreSubShapes
497 // purpose  :
498 //=================================================================================
499 void RepairGUI_GlueDlg::restoreSubShapes( SALOMEDS::Study_ptr   theStudy,
500                                           SALOMEDS::SObject_ptr theSObject )
501 {
502   if ( mainFrame()->CheckBoxRestoreSS->isChecked() ) {
503     GEOM::find_shape_method aFindMethod = GEOM::FSM_GetInPlace;
504     if ( getConstructorId() == 0 ) // MakeGlueFaces
505       aFindMethod = GEOM::FSM_GetInPlaceByHistory;
506
507     // empty list of arguments means that all arguments should be restored
508     getGeomEngine()->RestoreSubShapesSO( theStudy, theSObject, GEOM::ListOfGO(),
509                                          aFindMethod, /*theInheritFirstArg=*/true,
510                                          mainFrame()->CheckBoxAddPrefix->isChecked() );
511   }
512 }
513
514 //================================================================
515 // Function : clearShapeBufferLocal
516 // Purpose  : 
517 //================================================================
518 void RepairGUI_GlueDlg::clearShapeBufferLocal( GEOM::GEOM_Object_ptr theObj )
519 {
520   if ( CORBA::is_nil( theObj ) )
521     return;
522
523   CORBA::String_var IOR = myGeomGUI->getApp()->orb()->object_to_string( theObj );
524   TCollection_AsciiString asciiIOR( (char *)( IOR.in() ) );
525   myGeomGUI->GetShapeReader().RemoveShapeFromBuffer( asciiIOR );
526
527   if ( !getStudy() || !( getStudy()->studyDS() ) )
528     return;
529
530   _PTR(Study) aStudy = getStudy()->studyDS();
531   _PTR(SObject) aSObj ( aStudy->FindObjectIOR( std::string( IOR.in() ) ) );
532   if ( !aSObj )
533     return;
534
535   _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
536   for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
537     _PTR(GenericAttribute) anAttr;
538     if ( anIt->Value()->FindAttribute(anAttr, "AttributeIOR") ) {
539       _PTR(AttributeIOR) anIOR ( anAttr );
540       TCollection_AsciiString asciiIOR( (char*)anIOR->Value().c_str() );
541       myGeomGUI->GetShapeReader().RemoveShapeFromBuffer( asciiIOR );      
542     }
543   }
544 }
545
546 //================================================================
547 // Function : onAccept
548 // Purpose  : This method should be called from dialog's slots onOk() and onApply()
549 //            It perfroms user input validation, then it 
550 //            performs a proper operation and manages transactions, etc.
551 //================================================================
552 bool RepairGUI_GlueDlg::onAcceptLocal()
553 {
554   if ( !getStudy() || !( getStudy()->studyDS() ) )
555     return false;
556
557   _PTR(Study) aStudy = getStudy()->studyDS();
558
559   bool aLocked = aStudy->GetProperties()->IsLocked();
560   if ( aLocked ) {
561     MESSAGE("GEOMBase_Helper::onAccept - ActiveStudy is locked");
562     SUIT_MessageBox::warning( this, tr( "WRN_WARNING" ), tr( "WRN_STUDY_LOCKED" ), tr( "BUT_OK" ) );
563     return false;
564   }
565
566   QString msg;
567   if ( !isValid( msg ) ) {
568     showError( msg );
569     return false;
570   }
571
572   erasePreview( false );
573
574   try {
575     if ( openCommand() ) {
576       SUIT_OverrideCursor wc;
577       
578       myGeomGUI->getApp()->putInfo( "" );
579       ObjectList objects;
580       
581       if ( !execute( objects ) ) { 
582         wc.suspend();
583         abortCommand();
584         showError();
585       }
586       else {
587         const int nbObjs = objects.size();
588         for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it ) {
589           QString aName = getNewObjectName();
590           if ( nbObjs > 1 ) {
591             if ( aName.isEmpty() )
592               aName = getPrefix( *it );
593             aName = GEOMBase::GetDefaultName( aName );
594           }
595           else {
596             // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
597             if ( aName.isEmpty() )
598               aName = GEOMBase::GetDefaultName( getPrefix( *it ) );
599           }
600           addInStudy( *it, aName.toLatin1().data() );
601           display( *it, false );
602         }
603         
604         if ( nbObjs ) {
605           commitCommand();
606           updateObjBrowser();
607           myGeomGUI->getApp()->putInfo( QObject::tr("GEOM_PRP_DONE") );
608         }
609         else {
610           abortCommand();
611         }
612
613         // JFA 28.12.2004 BEGIN // To enable warnings
614         GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
615         if ( !CORBA::is_nil(anOper) && !anOper->IsDone() ) {
616           wc.suspend();
617           QString msgw = QObject::tr( anOper->GetErrorCode() );
618           SUIT_MessageBox::warning( this, tr( "WRN_WARNING" ), msgw, tr( "BUT_OK" ) );
619         }
620         // JFA 28.12.2004 END
621       }
622     }
623   }
624   catch( const SALOME::SALOME_Exception& e ) {
625     SalomeApp_Tools::QtCatchCorbaException( e );
626     abortCommand();
627   }
628
629   updateViewer();
630   activateSelection();
631   updateButtonState();
632
633   return true;
634 }
635
636
637 //=================================================================================
638 // function : onDetect
639 // purpose  :
640 //=================================================================================
641 void RepairGUI_GlueDlg::onDetect()
642 {
643   clearTemporary();
644   QString msg;
645   if ( !isValid( msg ) ) {
646     showError( msg );
647     return;
648   }
649
650   buttonOk()->setEnabled( false );
651   buttonApply()->setEnabled( false );
652   globalSelection( GEOM_ALLSHAPES );
653
654   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
655   GEOM::ListOfGO_var aList = anOper->GetGlueFaces( myObject.in(), myTolEdt2->value() );
656   
657   for ( int i = 0, n = aList->length(); i < n; i++ ) 
658     myTmpObjs << GEOM::GeomObjPtr( aList[i].in() );
659   
660   if ( !myTmpObjs.isEmpty()  ) {
661     msg = tr( "FACES_FOR_GLUING_ARE_DETECTED" ).arg( myTmpObjs.count() );
662     mySubShapesChk->setChecked( true );
663   }
664   else {
665     msg = tr( "THERE_ARE_NO_FACES_FOR_GLUING" );
666   }
667   
668   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
669            this, SLOT( SelectionIntoArgument() ) ) ;
670   SUIT_MessageBox::information( this, tr( "GEOM_FREE_BOUNDS_TLT" ), msg, tr( "Close" ) );
671   updateButtonState();
672   activateSelection();
673 }
674
675 //=================================================================================
676 // function : activateSelection
677 // purpose  : Redisplay preview and Activate selection
678 //=================================================================================
679 void RepairGUI_GlueDlg::activateSelection()
680 {
681   erasePreview( false );
682   
683   int anId = getConstructorId();
684   if ( anId == 0 )  { 
685     // Case of whole gluing
686     disconnect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
687                 this, SLOT( SelectionIntoArgument() ) );
688     
689     globalSelection( GEOM_ALLSHAPES );
690     if ( myObject->_is_nil()) 
691       SelectionIntoArgument();
692
693     connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
694              this, SLOT( SelectionIntoArgument() ) );
695   } 
696   else {
697     // Second case of gluing
698     if ( !mySubShapesChk->isChecked() ) 
699       globalSelection( GEOM_ALLSHAPES );
700     else {
701       displayPreview( true, true, false, false, 2/*line width*/, 1/*display mode*/, Quantity_NOC_RED );
702       disconnect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
703                   this, SLOT( SelectionIntoArgument() ) ) ;
704       globalSelection( GEOM_PREVIEW );
705       connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
706                this, SLOT( SelectionIntoArgument() ) ) ;
707     } 
708   }
709   updateViewer();
710 }
711
712 //=================================================================================
713 // function : updateButtonState
714 // purpose  : Update button state 
715 //=================================================================================
716 void RepairGUI_GlueDlg::updateButtonState()
717 {
718   int anId = getConstructorId();
719   bool hasMainObj = !myObject->_is_nil();
720   if ( anId == 0 ) {
721     buttonOk()->setEnabled( hasMainObj );
722     buttonApply()->setEnabled( hasMainObj );
723   }
724   else
725   {
726     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
727     SALOME_ListIO aSelList;
728     aSelMgr->selectedObjects(aSelList);
729
730     SALOME_ListIteratorOfListIO it (aSelList);
731     bool wasSelected = it.More() > 0;
732     bool wasDetected = !myTmpObjs.isEmpty();
733     buttonOk()->setEnabled( hasMainObj && wasDetected && wasSelected );
734     buttonApply()->setEnabled( hasMainObj && wasDetected && wasSelected );
735     mySubShapesChk->setEnabled( hasMainObj && wasDetected );
736     myDetectBtn->setEnabled( hasMainObj );
737     if ( !hasMainObj || !wasDetected )
738       mySubShapesChk->setChecked( false );
739   }
740 }
741
742 //=================================================================================
743 // function : clearTemporary
744 // purpose  : Remove temporary objects from engine
745 //=================================================================================
746 void RepairGUI_GlueDlg::clearTemporary()
747 {
748   myTmpObjs.clear();
749 }
750
751 //=================================================================================
752 // function : onTolerChanged
753 // purpose  : Remove temporary objects from engine
754 //=================================================================================
755 void RepairGUI_GlueDlg::onTolerChanged( double /*theVal*/ )
756 {
757   clearTemporary();
758   activateSelection();
759   updateButtonState();
760 }
761
762 //=================================================================================
763 // function : onSubShapesChk
764 // purpose  : Update selection mode
765 //=================================================================================
766 void RepairGUI_GlueDlg::onSubShapesChk()
767 {
768   if ( !mySubShapesChk->isChecked() )
769     clearTemporary();
770   activateSelection();
771   updateButtonState();
772 }
773
774 //=================================================================================
775 // function : ClickOnCancel
776 // purpose  : Remove temporary objects from engine and call method of base class
777 //=================================================================================
778 void RepairGUI_GlueDlg::ClickOnCancel()
779 {
780   clearTemporary();
781   GEOMBase_Skeleton::ClickOnCancel();
782 }