Salome HOME
Advanced feature for sewing - polygons creation instead of splitting.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_SewingDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESHGUI_SewingDlg.cxx
25 //  Author : Michael ZORIN
26 //  Module : SMESH
27 //  $Header: 
28
29 #include "SMESHGUI_SewingDlg.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34 #include "SMESHGUI_IdValidator.h"
35 #include "SMESH_Actor.h"
36 #include "SMDS_Mesh.hxx"
37
38 #include "GEOMBase.h"
39
40 #include "QAD_Application.h"
41 #include "QAD_Desktop.h"
42 #include "QAD_MessageBox.h"
43 #include "utilities.h"
44
45 // QT Includes
46 #include <qapplication.h>
47 #include <qbuttongroup.h>
48 #include <qgroupbox.h>
49 #include <qlabel.h>
50 #include <qlineedit.h>
51 #include <qpushbutton.h>
52 #include <qradiobutton.h>
53 #include <qcheckbox.h>
54 #include <qlayout.h>
55 #include <qpixmap.h>
56
57 using namespace std;
58
59 //=================================================================================
60 // class    : SMESHGUI_SewingDlg()
61 // purpose  : 
62 //=================================================================================
63 SMESHGUI_SewingDlg::SMESHGUI_SewingDlg( QWidget* parent, const char* name, SALOME_Selection* Sel,
64                                               bool modal, WFlags fl )
65   : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu |
66              Qt::WDestructiveClose)
67 {
68   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SMESH_SEWING_FREEBORDERS")));
69   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SMESH_SEWING_CONFORM_FREEBORDERS")));
70   QPixmap image2(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SMESH_SEWING_BORDERTOSIDE")));
71   QPixmap image3(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SMESH_SEWING_SIDEELEMENTS")));
72   QPixmap image4(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
73
74   if ( !name )
75     setName( "SMESHGUI_SewingDlg" );
76   resize( 303, 185 ); 
77   setCaption( tr( "SMESH_SEWING" ) );
78   setSizeGripEnabled( TRUE );
79   SMESHGUI_SewingDlgLayout = new QGridLayout( this ); 
80   SMESHGUI_SewingDlgLayout->setSpacing( 6 );
81   SMESHGUI_SewingDlgLayout->setMargin( 11 );
82
83   /***************************************************************/
84   GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
85   GroupConstructors->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5, (QSizePolicy::SizeType)0, 0, 0, GroupConstructors->sizePolicy().hasHeightForWidth() ) );
86   GroupConstructors->setTitle( tr( "SMESH_SEWING"  ) );
87   GroupConstructors->setExclusive( TRUE );
88   GroupConstructors->setColumnLayout(0, Qt::Vertical );
89   GroupConstructors->layout()->setSpacing( 0 );
90   GroupConstructors->layout()->setMargin( 0 );
91   GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
92   GroupConstructorsLayout->setAlignment( Qt::AlignTop );
93   GroupConstructorsLayout->setSpacing( 6 );
94   GroupConstructorsLayout->setMargin( 11 );
95   QHBoxLayout* RBLayout = new QHBoxLayout( 0, 0, 6, "Layout2");
96   RadioButton1= new QRadioButton( GroupConstructors, "RadioButton1" );
97   RadioButton1->setText( tr( ""  ) );
98   RadioButton1->setPixmap( image0 );
99   RBLayout->addWidget( RadioButton1);
100   RadioButton2= new QRadioButton( GroupConstructors, "RadioButton2" );
101   RadioButton2->setText( tr( ""  ) );
102   RadioButton2->setPixmap( image1 );
103   RBLayout->addWidget( RadioButton2);
104   RadioButton3= new QRadioButton( GroupConstructors, "RadioButton3" );
105   RadioButton3->setText( tr( ""  ) );
106   RadioButton3->setPixmap( image2 );
107   RBLayout->addWidget( RadioButton3);
108   RadioButton4= new QRadioButton( GroupConstructors, "RadioButton4" );
109   RadioButton4->setText( tr( ""  ) );
110   RadioButton4->setPixmap( image3 );
111   RBLayout->addWidget( RadioButton4);
112   GroupConstructorsLayout->addLayout( RBLayout, 0, 0 );
113   SMESHGUI_SewingDlgLayout->addWidget( GroupConstructors, 0, 0 );
114   
115   /***************************************************************/
116   GroupButtons = new QGroupBox( this, "GroupButtons" );
117   GroupButtons->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)7, (QSizePolicy::SizeType)0, 0, 0, GroupButtons->sizePolicy().hasHeightForWidth() ) );
118   GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) ); 
119   GroupButtons->setTitle( tr( ""  ) );
120   GroupButtons->setColumnLayout(0, Qt::Vertical );
121   GroupButtons->layout()->setSpacing( 0 );
122   GroupButtons->layout()->setMargin( 0 );
123   GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
124   GroupButtonsLayout->setAlignment( Qt::AlignTop );
125   GroupButtonsLayout->setSpacing( 6 );
126   GroupButtonsLayout->setMargin( 11 );
127   buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
128   buttonCancel->setText( tr( "SMESH_BUT_CLOSE"  ) );
129   buttonCancel->setAutoDefault( TRUE );
130   GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
131   buttonApply = new QPushButton( GroupButtons, "buttonApply" );
132   buttonApply->setText( tr( "SMESH_BUT_APPLY"  ) );
133   buttonApply->setAutoDefault( TRUE );
134   GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
135   QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
136   GroupButtonsLayout->addItem( spacer_9, 0, 2 );
137   buttonOk = new QPushButton( GroupButtons, "buttonOk" );
138   buttonOk->setText( tr( "SMESH_BUT_OK"  ) );
139   buttonOk->setAutoDefault( TRUE );
140   buttonOk->setDefault( TRUE );
141   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
142   SMESHGUI_SewingDlgLayout->addWidget( GroupButtons, 2, 0 );
143
144   /***************************************************************/
145   GroupArguments = new QGroupBox( this, "GroupArguments" );
146   GroupArguments->setColumnLayout(0, Qt::Vertical );
147   GroupArguments->layout()->setSpacing( 0 );
148   GroupArguments->layout()->setMargin( 0 );
149   GroupArgumentsLayout = new QGridLayout( GroupArguments->layout() );
150   GroupArgumentsLayout->setAlignment( Qt::AlignTop );
151   GroupArgumentsLayout->setSpacing( 6 );
152   GroupArgumentsLayout->setMargin( 11 );
153
154   // First subgroup
155   SubGroup1 = new QGroupBox( GroupArguments, "SubGroup1" );
156   SubGroup1->setColumnLayout(0, Qt::Vertical );
157   SubGroup1->layout()->setSpacing( 0 );
158   SubGroup1->layout()->setMargin( 0 );
159   QGridLayout* SubGroup1Layout = new QGridLayout( SubGroup1->layout() );
160   SubGroup1Layout->setAlignment( Qt::AlignTop );
161   SubGroup1Layout->setSpacing( 6 );
162   SubGroup1Layout->setMargin( 11 );
163   
164   // Controls of the first subgroup
165   TextLabel1 = new QLabel( SubGroup1, "TextLabel1" );
166   TextLabel1->setFixedWidth(104);
167   SubGroup1Layout->addWidget( TextLabel1, 0, 0 );
168   
169   SelectButton1  = new QPushButton( SubGroup1, "SelectButton1" );
170   SelectButton1->setPixmap( image4 );
171   SubGroup1Layout->addWidget( SelectButton1, 0, 1 );
172   
173   LineEdit1 = new QLineEdit( SubGroup1, "LineEdit1" );
174   SubGroup1Layout->addWidget( LineEdit1, 0, 2 );
175   
176   TextLabel2 = new QLabel( SubGroup1, "TextLabel2" );
177   SubGroup1Layout->addWidget( TextLabel2, 1, 0 );
178   
179   SelectButton2  = new QPushButton( SubGroup1, "SelectButton2" );
180   SelectButton2->setPixmap( image4 );
181   SubGroup1Layout->addWidget( SelectButton2, 1, 1 );
182   
183   LineEdit2 = new QLineEdit( SubGroup1, "LineEdit2" );
184   SubGroup1Layout->addWidget( LineEdit2, 1, 2 );
185
186   TextLabel3 = new QLabel( SubGroup1, "TextLabel3" );
187   SubGroup1Layout->addWidget( TextLabel3, 2, 0 );
188   
189   SelectButton3  = new QPushButton( SubGroup1, "SelectButton3" );
190   SelectButton3->setPixmap( image4 );
191   SubGroup1Layout->addWidget( SelectButton3, 2, 1 );
192   
193   LineEdit3 = new QLineEdit( SubGroup1, "LineEdit3" );
194   SubGroup1Layout->addWidget( LineEdit3, 2, 2 );
195
196
197   // Second subgroup
198   SubGroup2 = new QGroupBox( GroupArguments, "SubGroup2" );
199   SubGroup2->setColumnLayout(0, Qt::Vertical );
200   SubGroup2->layout()->setSpacing( 0 );
201   SubGroup2->layout()->setMargin( 0 );
202   QGridLayout* SubGroup2Layout = new QGridLayout( SubGroup2->layout() );
203   SubGroup2Layout->setAlignment( Qt::AlignTop );
204   SubGroup2Layout->setSpacing( 6 );
205   SubGroup2Layout->setMargin( 11 );
206   
207   // Controls of the first subgroup
208   TextLabel4 = new QLabel( SubGroup2, "TextLabel4" );
209   SubGroup2Layout->addWidget( TextLabel4, 0, 0 );
210   
211   SelectButton4  = new QPushButton( SubGroup2, "SelectButton4" );
212   SelectButton4->setPixmap( image4 );
213   SubGroup2Layout->addWidget( SelectButton4, 0, 1 );
214   
215   LineEdit4 = new QLineEdit( SubGroup2, "LineEdit4" );
216   SubGroup2Layout->addWidget( LineEdit4, 0, 2 );
217   
218   TextLabel5 = new QLabel( SubGroup2, "TextLabel5" );
219   SubGroup2Layout->addWidget( TextLabel5, 1, 0 );
220   
221   SelectButton5  = new QPushButton( SubGroup2, "SelectButton5" );
222   SelectButton5->setPixmap( image4 );
223   SubGroup2Layout->addWidget( SelectButton5, 1, 1 );
224   
225   LineEdit5 = new QLineEdit( SubGroup2, "LineEdit5" );
226   SubGroup2Layout->addWidget( LineEdit5, 1, 2 );
227
228   TextLabel6 = new QLabel( SubGroup2, "TextLabel6" );
229   SubGroup2Layout->addWidget( TextLabel6, 2, 0 );
230   
231   SelectButton6  = new QPushButton( SubGroup2, "SelectButton6" );
232   SelectButton6->setPixmap( image4 );
233   SubGroup2Layout->addWidget( SelectButton6, 2, 1 );
234   
235   LineEdit6 = new QLineEdit( SubGroup2, "LineEdit6" );
236   SubGroup2Layout->addWidget( LineEdit6, 2, 2 );
237
238   
239   // Add subgroups to the group of arguments
240   GroupArgumentsLayout->addWidget( SubGroup1, 0, 0 );
241   GroupArgumentsLayout->addWidget( SubGroup2, 1, 0 );
242
243   // Control for the merging equal elements
244   CheckBoxMerge = new QCheckBox( GroupArguments, "CheckBoxMerge" );
245   CheckBoxMerge->setText( tr( "MERGE_EQUAL_ELEMENTS"  ) );
246   GroupArgumentsLayout->addWidget( CheckBoxMerge, 2, 0 );
247   
248   // Control for the polygons creation instead of splitting
249   CheckBoxPoly = new QCheckBox( GroupArguments, "CheckBoxPoly" );
250   CheckBoxPoly->setText( tr( "CREATE_POLYGONS_INSTEAD_SPLITTING"  ) );
251   GroupArgumentsLayout->addWidget( CheckBoxPoly, 3, 0 );
252   
253
254   SMESHGUI_SewingDlgLayout->addWidget( GroupArguments, 1, 0 );
255   
256   /* Initialisations */
257   GroupArguments->show();
258   RadioButton1->setChecked( TRUE );
259   mySelection = Sel;  
260   
261   LineEdit2->setValidator( new SMESHGUI_IdValidator( this, "validator", 1));
262   LineEdit3->setValidator( new SMESHGUI_IdValidator( this, "validator", 1));
263   LineEdit5->setValidator( new SMESHGUI_IdValidator( this, "validator", 1));
264   LineEdit6->setValidator( new SMESHGUI_IdValidator( this, "validator", 1));
265   
266   mySMESHGUI  = SMESHGUI::GetSMESHGUI() ;
267   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
268
269   Init();
270   /* signals and slots connections */
271   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
272   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
273   connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
274   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
275   
276   connect( SelectButton1, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
277   connect( SelectButton2, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
278   connect( SelectButton3, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
279   connect( SelectButton4, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
280   connect( SelectButton5, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
281   connect( SelectButton6, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
282
283   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
284   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
285   /* to close dialog if study change */
286   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
287
288   connect( LineEdit1, SIGNAL( textChanged( const QString& )), SLOT( onTextChange( const QString& )));
289   connect( LineEdit2, SIGNAL( textChanged( const QString& )), SLOT( onTextChange( const QString& )));
290   connect( LineEdit3, SIGNAL( textChanged( const QString& )), SLOT( onTextChange( const QString& )));
291   connect( LineEdit4, SIGNAL( textChanged( const QString& )), SLOT( onTextChange( const QString& )));
292   connect( LineEdit5, SIGNAL( textChanged( const QString& )), SLOT( onTextChange( const QString& )));
293   connect( LineEdit6, SIGNAL( textChanged( const QString& )), SLOT( onTextChange( const QString& )));
294   
295   /* Move widget on the botton right corner of main widget */
296   int x, y ;
297   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
298   this->move( x, y ) ;
299   this->show() ; /* displays Dialog */
300   
301   ConstructorsClicked(0);
302   resize(0,0);
303 }
304
305 //=================================================================================
306 // function : ~SMESHGUI_SewingDlg()
307 // purpose  : Destroys the object and frees any allocated resources
308 //=================================================================================
309 SMESHGUI_SewingDlg::~SMESHGUI_SewingDlg()
310 {
311   // no need to delete child widgets, Qt does it all for us
312 }
313
314
315 //=================================================================================
316 // function : Init()
317 // purpose  :
318 //=================================================================================
319 void SMESHGUI_SewingDlg::Init()
320 {
321   myBusy = false;
322
323   myEditCurrentArgument = LineEdit1;
324   LineEdit1->setFocus();
325   myActor     = 0;
326   myMesh = SMESH::SMESH_Mesh::_nil();
327   CheckBoxMerge->setChecked(false);
328   CheckBoxPoly->setChecked(false);
329   SelectionIntoArgument();
330 }
331
332
333 //=================================================================================
334 // function : ConstructorsClicked()
335 // purpose  : Radio button management
336 //=================================================================================
337 void SMESHGUI_SewingDlg::ConstructorsClicked(int constructorId)
338 {
339   disconnect(mySelection, 0, this, 0);
340   mySelection->ClearIObjects();
341   LineEdit1->setText("");
342   LineEdit2->setText("");
343   LineEdit3->setText("");
344   LineEdit4->setText("");
345   LineEdit5->setText("");
346   LineEdit6->setText("");
347   myOk1 = myOk2 = myOk3 = myOk4 = myOk5 = myOk6 = false;
348   myEditCurrentArgument = LineEdit1;
349   myEditCurrentArgument->setFocus();
350   
351   if (!TextLabel5->isEnabled())
352     {
353       TextLabel5->setEnabled(true);
354       SelectButton5->setEnabled(true);
355       LineEdit5->setEnabled(true);
356     }
357   else if (!TextLabel6->isEnabled())
358     {
359       TextLabel6->setEnabled(true);
360       SelectButton6->setEnabled(true);
361       LineEdit6->setEnabled(true);
362     }
363   
364   if (constructorId != 0 && CheckBoxPoly->isVisible())
365     CheckBoxPoly->hide();
366         
367   switch(constructorId)
368     {
369     case 0 :
370       { 
371         GroupArguments->setTitle( tr( "SEW_FREE_BORDERS" ) );
372         SubGroup1->setTitle( tr( "BORDER_1" ) );
373         SubGroup2->setTitle( tr( "BORDER_2" ) );
374
375         if (!CheckBoxPoly->isVisible())
376           CheckBoxPoly->show();
377         
378         break;
379       }
380     case 1 :
381       {
382         GroupArguments->setTitle( tr( "SEW_CONFORM_FREE_BORDERS" ) );
383         SubGroup1->setTitle( tr( "BORDER_1" ) );
384         SubGroup2->setTitle( tr( "BORDER_2" ) );
385         
386         TextLabel6->setEnabled(false);
387         SelectButton6->setEnabled(false);
388         LineEdit6->setEnabled(false);
389         
390         myOk6 = true;
391           
392         break;
393       }
394     case 2 :
395       {
396         GroupArguments->setTitle( tr( "SEW_BORDER_TO_SIDE" ) );
397         SubGroup1->setTitle( tr( "BORDER" ) );
398         SubGroup2->setTitle( tr( "SIDE" ) );
399
400         TextLabel5->setEnabled(false);
401         SelectButton5->setEnabled(false);
402         LineEdit5->setEnabled(false);
403         
404         myOk5 = true;
405
406         break;
407       }
408     case 3 :
409       { 
410         GroupArguments->setTitle( tr( "SEW_SIDE_ELEMENTS" ) );
411         SubGroup1->setTitle( tr( "SIDE_1" ) );
412         SubGroup2->setTitle( tr( "SIDE_2" ) );
413         
414         TextLabel1->setText( tr( "SMESH_ID_ELEMENTS" ) );
415         TextLabel2->setText( tr( "NODE1_TO_MERGE" ) );
416         TextLabel3->setText( tr( "NODE2_TO_MERGE" ) );
417         TextLabel4->setText( tr( "SMESH_ID_ELEMENTS" ) );
418         TextLabel5->setText( tr( "NODE1_TO_MERGE" ) );
419         TextLabel6->setText( tr( "NODE2_TO_MERGE" ) );
420
421         LineEdit1->setValidator( new SMESHGUI_IdValidator( this, "validator"));
422         LineEdit4->setValidator( new SMESHGUI_IdValidator( this, "validator"));
423
424         SMESH::SetPointRepresentation(false);
425         QAD_Application::getDesktop()->SetSelectionMode( CellSelection, true );
426         break;
427       }
428     }
429   
430   if (constructorId != 3)
431     {
432       TextLabel1->setText( tr( "FIRST_NODE_ID" ) );
433       TextLabel2->setText( tr( "SECOND_NODE_ID" ) );
434       TextLabel3->setText( tr( "LAST_NODE_ID" ) );
435       TextLabel4->setText( tr( "FIRST_NODE_ID" ) );
436       TextLabel5->setText( tr( "SECOND_NODE_ID" ) );
437       TextLabel6->setText( tr( "LAST_NODE_ID" ) );
438       
439       LineEdit1->setValidator( new SMESHGUI_IdValidator( this, "validator", 1));
440       LineEdit4->setValidator( new SMESHGUI_IdValidator( this, "validator", 1));
441
442       SMESH::SetPointRepresentation(true);
443       QAD_Application::getDesktop()->SetSelectionMode( NodeSelection, true );
444     }
445   
446   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
447 }
448
449
450 //=================================================================================
451 // function : ClickOnApply()
452 // purpose  :
453 //=================================================================================
454 bool SMESHGUI_SewingDlg::ClickOnApply()
455 {
456   if (mySMESHGUI->ActiveStudyLocked())
457     return false;
458   
459   bool aResult = false;
460
461   if ( IsValid() )
462     {
463       bool toMerge = CheckBoxMerge->isChecked();
464       bool toCreatePoly = CheckBoxPoly->isChecked();
465     
466       try
467         {
468           SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
469           QApplication::setOverrideCursor(Qt::waitCursor);
470         
471           int aConstructorId = GetConstructorId();
472           SMESH::SMESH_MeshEditor::Sew_Error anError;
473   
474           if (aConstructorId == 0)
475             anError = aMeshEditor->SewFreeBorders(LineEdit1->text().toLong(),
476                                                   LineEdit2->text().toLong(),
477                                                   LineEdit3->text().toLong(),
478                                                   LineEdit4->text().toLong(),
479                                                   LineEdit5->text().toLong(),
480                                                   LineEdit6->text().toLong(),
481                                                   toCreatePoly);
482           else if (aConstructorId == 1)
483             anError = aMeshEditor->SewConformFreeBorders(LineEdit1->text().toLong(),
484                                                          LineEdit2->text().toLong(),
485                                                          LineEdit3->text().toLong(),
486                                                          LineEdit4->text().toLong(),
487                                                          LineEdit5->text().toLong());
488           else if (aConstructorId == 2)
489             anError = aMeshEditor->SewBorderToSide(LineEdit1->text().toLong(),
490                                                    LineEdit2->text().toLong(),
491                                                    LineEdit3->text().toLong(),
492                                                    LineEdit4->text().toLong(),
493                                                    LineEdit6->text().toLong());
494           else if (aConstructorId == 3)
495             {
496               QStringList aListElementsId1 = QStringList::split( " ", LineEdit1->text(), false);
497               QStringList aListElementsId2 = QStringList::split( " ", LineEdit4->text(), false);
498               
499               SMESH::long_array_var anElementsId1 = new SMESH::long_array;
500               SMESH::long_array_var anElementsId2 = new SMESH::long_array;
501       
502               anElementsId1->length( aListElementsId1.count() );
503               anElementsId2->length( aListElementsId2.count() );
504
505               for ( int i = 0; i < aListElementsId1.count(); i++ )
506                 anElementsId1[i] = aListElementsId1[i].toInt();
507               for ( int i = 0; i < aListElementsId2.count(); i++ )
508                 anElementsId2[i] = aListElementsId2[i].toInt();
509
510               anError = aMeshEditor->SewSideElements(anElementsId1.inout(),
511                                                      anElementsId2.inout(),
512                                                      LineEdit2->text().toLong(),
513                                                      LineEdit5->text().toLong(),
514                                                      LineEdit3->text().toLong(),
515                                                      LineEdit6->text().toLong());
516             }
517           aResult = ( anError == SMESH::SMESH_MeshEditor::SEW_OK );
518
519           if (toMerge && aResult)
520             aMeshEditor->MergeEqualElements();
521           
522           QApplication::restoreOverrideCursor();
523
524           if ( !aResult ) {
525             QString msg = tr(QString("ERROR_%1").arg(anError));
526             QAD_MessageBox::warn1(QAD_Application::getDesktop(),
527                                   tr("SMESH_WRN_WARNING"),msg,tr("SMESH_BUT_OK"));
528           }
529         }
530       catch( ... )
531         {
532         }
533       
534       if ( aResult )
535         {
536           Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
537           mySelection->ClearIObjects();
538           SMESH::UpdateView();
539           mySelection->AddIObject( anIO, false );
540           Init();
541           ConstructorsClicked(GetConstructorId());
542         }
543     }
544
545   return aResult;
546 }
547
548 //=================================================================================
549 // function : ClickOnOk()
550 // purpose  :
551 //=================================================================================
552 void SMESHGUI_SewingDlg::ClickOnOk()
553 {
554   if( ClickOnApply())
555     ClickOnCancel() ;
556 }
557
558         
559 //=================================================================================
560 // function : ClickOnCancel()
561 // purpose  :
562 //=================================================================================
563 void SMESHGUI_SewingDlg::ClickOnCancel()
564 {
565   mySelection->ClearIObjects();
566   SMESH::SetPointRepresentation(false);
567   QAD_Application::getDesktop()->SetSelectionMode( ActorSelection );
568   disconnect( mySelection, 0, this, 0 );
569   mySMESHGUI->ResetState() ;
570   reject() ;
571 }
572
573 //=======================================================================
574 //function : onTextChange
575 //purpose  : 
576 //=======================================================================
577
578 void SMESHGUI_SewingDlg::onTextChange(const QString& theNewText)
579 {
580   QLineEdit* send = (QLineEdit*)sender();
581
582   if ( myBusy ) return;
583   myBusy = true;
584
585   if (send)
586     myEditCurrentArgument = send;
587  
588   if      (send == LineEdit1)
589     myOk1 = false;
590   else if (send == LineEdit2)
591     myOk2 = false;
592   else if (send == LineEdit3)
593     myOk3 = false;
594   else if (send == LineEdit4)
595     myOk4 = false;
596   else if (send == LineEdit5)
597     myOk5 = false;
598   else if (send == LineEdit6)
599     myOk6 = false;
600
601   buttonOk->setEnabled( false );
602   buttonApply->setEnabled( false );
603   
604   // hilight entered elements/nodes
605   SMDS_Mesh* aMesh = 0;
606   
607   if ( myActor )
608     aMesh = myActor->GetObject()->GetMesh();
609   else
610     send->clear();
611   
612   if ( aMesh ) {
613     mySelection->ClearIObjects();
614     mySelection->AddIObject( myActor->getIO() );
615   
616     if (GetConstructorId() != 3 || (send != LineEdit1 && send != LineEdit4))
617       {
618         SMESH::SetPointRepresentation(true);
619         QAD_Application::getDesktop()->SetSelectionMode( NodeSelection, true );
620         
621         const SMDS_MeshNode * n = aMesh->FindNode( theNewText.toInt() );
622         if ( n ) {
623           if ( !mySelection->IsIndexSelected( myActor->getIO(), n->GetID() ))
624             mySelection->AddOrRemoveIndex (myActor->getIO(), n->GetID(), true);
625           
626           if      (send == LineEdit1)
627             myOk1 = true;
628           else if (send == LineEdit2)
629             myOk2 = true;
630           else if (send == LineEdit3)
631             myOk3 = true;
632           else if (send == LineEdit4)
633             myOk4 = true;
634           else if (send == LineEdit5)
635             myOk5 = true;
636           else if (send == LineEdit6)
637             myOk6 = true;
638         }
639       }
640     else
641       {
642         SMESH::SetPointRepresentation(false);
643         QAD_Application::getDesktop()->SetSelectionMode( CellSelection, true );
644         
645         QStringList aListId = QStringList::split( " ", theNewText, false);
646         
647         bool isEvenOneExists = false;
648         
649         for ( int i = 0; i < aListId.count(); i++ ) {
650           const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() );
651           if ( e ) {
652             if ( !mySelection->IsIndexSelected( myActor->getIO(), e->GetID() ))
653               mySelection->AddOrRemoveIndex (myActor->getIO(), e->GetID(), true);
654             if (!isEvenOneExists) 
655               isEvenOneExists = true;
656           }
657         }
658         
659         if (isEvenOneExists)
660           {
661             if (send == LineEdit1)
662               myOk1 = true;
663             else if(send == LineEdit4)
664               myOk4 = true;
665           }
666         else
667           send->clear();
668       }
669   }
670   
671   if ( IsValid() ) {
672     buttonOk->setEnabled( true );
673     buttonApply->setEnabled( true );
674   }
675   
676   myBusy = false;
677 }
678
679
680 //=================================================================================
681 // function : SelectionIntoArgument()
682 // purpose  : Called when selection as changed or other case
683 //=================================================================================
684 void SMESHGUI_SewingDlg::SelectionIntoArgument(bool isSelectionChanged)
685 {
686   if ( myBusy ) return;
687   
688   // clear
689   if (isSelectionChanged)
690     myActor = 0;
691   
692   QString aString = "";
693   
694   myBusy = true;
695   myEditCurrentArgument->setText( aString );
696   myBusy = false;
697   
698   if ( !GroupButtons->isEnabled() ) // inactive
699     return;
700   
701   buttonOk->setEnabled( false );
702   buttonApply->setEnabled( false );
703   
704   // get selected mesh
705   int nbSel = mySelection->IObjectCount();
706   if(nbSel != 1)
707     return;
708   
709   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject();
710   myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO) ;
711   myActor = SMESH::FindActorByEntry( mySelection->firstIObject()->getEntry() );
712   
713   if (myMesh->_is_nil() || !myActor)
714     return;
715   
716   // get selected elements/nodes
717   
718   int aNbUnits = 0;
719   
720   if (GetConstructorId() != 3 || (myEditCurrentArgument != LineEdit1 && myEditCurrentArgument != LineEdit4))
721     {
722       aNbUnits = SMESH::GetNameOfSelectedNodes(mySelection, aString);
723       if(aNbUnits != 1)
724         return;
725     }
726   else
727     {
728       aNbUnits = SMESH::GetNameOfSelectedElements(mySelection, aString);
729       if(aNbUnits < 1) 
730         return;
731     }
732   
733   myBusy = true;
734   myEditCurrentArgument->setText( aString );
735   myBusy = false;
736   
737   // OK
738   if (myEditCurrentArgument == LineEdit1)
739     myOk1 = true;
740   else if(myEditCurrentArgument == LineEdit2)
741     myOk2 = true;
742   else if(myEditCurrentArgument == LineEdit3)
743     myOk3 = true;
744   else if(myEditCurrentArgument == LineEdit4)
745     myOk4 = true;
746   else if(myEditCurrentArgument == LineEdit5)
747     myOk5 = true;
748   else if(myEditCurrentArgument == LineEdit6)
749     myOk6 = true;
750   
751   if(IsValid())
752     {
753       buttonOk->setEnabled( true );
754       buttonApply->setEnabled( true );
755     }
756 }
757
758
759 //=================================================================================
760 // function : SetEditCurrentArgument()
761 // purpose  :
762 //=================================================================================
763 void SMESHGUI_SewingDlg::SetEditCurrentArgument()
764 {
765   QPushButton* send = (QPushButton*)sender();
766   
767   disconnect( mySelection, 0, this, 0 );
768   mySelection->ClearIObjects();
769   
770   if(send == SelectButton1) {
771     myEditCurrentArgument = LineEdit1;
772     myOk1 = false;
773   }
774   else if (send == SelectButton2) {
775     myEditCurrentArgument = LineEdit2;
776     myOk2 = false;
777   }
778   else if (send == SelectButton3) {
779     myEditCurrentArgument = LineEdit3;
780     myOk3 = false;
781   }
782   else if (send == SelectButton4) {
783     myEditCurrentArgument = LineEdit4;
784     myOk4 = false;
785   }
786   else if (send == SelectButton5) {
787     myEditCurrentArgument = LineEdit5;
788     myOk5 = false;
789   }
790   else if (send == SelectButton6) {
791     myEditCurrentArgument = LineEdit6;
792     myOk6 = false;
793   }
794   
795   if (GetConstructorId() != 3 || (send != SelectButton1 && send != SelectButton4))
796     {
797       SMESH::SetPointRepresentation(true);
798       QAD_Application::getDesktop()->SetSelectionMode( NodeSelection, true );
799     }
800   else
801     {
802       SMESH::SetPointRepresentation(false);
803       QAD_Application::getDesktop()->SetSelectionMode( CellSelection, true );
804     }
805   
806   myEditCurrentArgument->setFocus();
807   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
808   SelectionIntoArgument(false) ;
809 }
810
811 //=================================================================================
812 // function : DeactivateActiveDialog()
813 // purpose  :
814 //=================================================================================
815 void SMESHGUI_SewingDlg::DeactivateActiveDialog()
816 {
817   if ( GroupConstructors->isEnabled() ) {
818     GroupConstructors->setEnabled(false) ;
819     GroupArguments->setEnabled(false) ;
820     GroupButtons->setEnabled(false) ;
821     mySMESHGUI->ResetState() ;    
822     mySMESHGUI->SetActiveDialogBox(0) ;
823   }
824 }
825
826
827 //=================================================================================
828 // function : ActivateThisDialog()
829 // purpose  :
830 //=================================================================================
831 void SMESHGUI_SewingDlg::ActivateThisDialog()
832 {
833   /* Emit a signal to deactivate the active dialog */
834   mySMESHGUI->EmitSignalDeactivateDialog() ;   
835   GroupConstructors->setEnabled(true) ;
836   GroupArguments->setEnabled(true) ;
837   GroupButtons->setEnabled(true) ;
838   
839   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
840   
841   ConstructorsClicked(GetConstructorId());
842   SelectionIntoArgument();
843 }
844
845
846 //=================================================================================
847 // function : enterEvent()
848 // purpose  :
849 //=================================================================================
850 void SMESHGUI_SewingDlg::enterEvent(QEvent* e)
851 {
852   if ( GroupConstructors->isEnabled() )
853     return ;  
854   ActivateThisDialog() ;
855 }
856
857
858 //=================================================================================
859 // function : closeEvent()
860 // purpose  :
861 //=================================================================================
862 void SMESHGUI_SewingDlg::closeEvent( QCloseEvent* e )
863 {
864   /* same than click on cancel button */
865   this->ClickOnCancel() ;
866 }
867
868
869 //=======================================================================
870 //function : hideEvent
871 //purpose  : caused by ESC key
872 //=======================================================================
873
874 void SMESHGUI_SewingDlg::hideEvent ( QHideEvent * e )
875 {
876   if ( !isMinimized() )
877     ClickOnCancel();
878 }
879
880
881 //=================================================================================
882 // function : GetConstructorId()
883 // purpose  : 
884 //=================================================================================
885 int SMESHGUI_SewingDlg::GetConstructorId()
886
887   if ( GroupConstructors != NULL && GroupConstructors->selected() != NULL )
888     return GroupConstructors->id( GroupConstructors->selected() );
889   return -1;
890 }
891
892
893 //=================================================================================
894 // function : GetConstructorId()
895 // purpose  : 
896 //=================================================================================
897 bool SMESHGUI_SewingDlg::IsValid()
898 {
899   return (myOk1 && myOk2 && myOk3 && myOk4 && myOk5 && myOk6);
900 }