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