Salome HOME
Dump Python: set correct names to objects in study; provide correct setting of a...
[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   CheckBoxPolygons = new QCheckBox( GroupArguments, "CheckBoxPolygons" );
250   CheckBoxPolygons->setText( tr( "CREATE_POLYGONS_INSTEAD_SPLITTING"  ) );
251   GroupArgumentsLayout->addWidget( CheckBoxPolygons, 3, 0 );
252   
253   // Control for the polyedres creation to obtain conform mesh
254   CheckBoxPolyedrs = new QCheckBox( GroupArguments, "CheckBoxPolyedrs" );
255   CheckBoxPolyedrs->setText( tr( "CREATE_POLYEDRS_NEAR_BOUNDARY"  ) );
256   GroupArgumentsLayout->addWidget( CheckBoxPolyedrs, 4, 0 );
257   
258
259   SMESHGUI_SewingDlgLayout->addWidget( GroupArguments, 1, 0 );
260   
261   /* Initialisations */
262   GroupArguments->show();
263   RadioButton1->setChecked( TRUE );
264   mySelection = Sel;  
265   
266   LineEdit2->setValidator( new SMESHGUI_IdValidator( this, "validator", 1));
267   LineEdit3->setValidator( new SMESHGUI_IdValidator( this, "validator", 1));
268   LineEdit5->setValidator( new SMESHGUI_IdValidator( this, "validator", 1));
269   LineEdit6->setValidator( new SMESHGUI_IdValidator( this, "validator", 1));
270   
271   mySMESHGUI  = SMESHGUI::GetSMESHGUI() ;
272   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
273
274   Init();
275   /* signals and slots connections */
276   connect( buttonOk, SIGNAL( clicked() ),     this, SLOT( ClickOnOk() ) );
277   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
278   connect( buttonApply, SIGNAL( clicked() ), this, SLOT(ClickOnApply() ) );
279   connect( GroupConstructors, SIGNAL(clicked(int) ), SLOT( ConstructorsClicked(int) ) );
280   
281   connect( SelectButton1, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
282   connect( SelectButton2, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
283   connect( SelectButton3, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
284   connect( SelectButton4, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
285   connect( SelectButton5, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
286   connect( SelectButton6, SIGNAL (clicked() ),   this, SLOT( SetEditCurrentArgument() ) ) ;
287
288   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) ) ;
289   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
290   /* to close dialog if study change */
291   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ) ;
292
293   connect( LineEdit1, SIGNAL( textChanged( const QString& )), SLOT( onTextChange( const QString& )));
294   connect( LineEdit2, SIGNAL( textChanged( const QString& )), SLOT( onTextChange( const QString& )));
295   connect( LineEdit3, SIGNAL( textChanged( const QString& )), SLOT( onTextChange( const QString& )));
296   connect( LineEdit4, SIGNAL( textChanged( const QString& )), SLOT( onTextChange( const QString& )));
297   connect( LineEdit5, SIGNAL( textChanged( const QString& )), SLOT( onTextChange( const QString& )));
298   connect( LineEdit6, SIGNAL( textChanged( const QString& )), SLOT( onTextChange( const QString& )));
299   
300   /* Move widget on the botton right corner of main widget */
301   int x, y ;
302   mySMESHGUI->DefineDlgPosition( this, x, y ) ;
303   this->move( x, y ) ;
304   this->show() ; /* displays Dialog */
305   
306   ConstructorsClicked(0);
307   resize(0,0);
308 }
309
310 //=================================================================================
311 // function : ~SMESHGUI_SewingDlg()
312 // purpose  : Destroys the object and frees any allocated resources
313 //=================================================================================
314 SMESHGUI_SewingDlg::~SMESHGUI_SewingDlg()
315 {
316   // no need to delete child widgets, Qt does it all for us
317 }
318
319
320 //=================================================================================
321 // function : Init()
322 // purpose  :
323 //=================================================================================
324 void SMESHGUI_SewingDlg::Init()
325 {
326   myBusy = false;
327
328   myEditCurrentArgument = LineEdit1;
329   LineEdit1->setFocus();
330   myActor     = 0;
331   myMesh = SMESH::SMESH_Mesh::_nil();
332   CheckBoxMerge->setChecked(false);
333   CheckBoxPolygons->setChecked(false);
334   CheckBoxPolyedrs->setChecked(false);
335   SelectionIntoArgument();
336 }
337
338
339 //=================================================================================
340 // function : ConstructorsClicked()
341 // purpose  : Radio button management
342 //=================================================================================
343 void SMESHGUI_SewingDlg::ConstructorsClicked(int constructorId)
344 {
345   disconnect(mySelection, 0, this, 0);
346   mySelection->ClearIObjects();
347   LineEdit1->setText("");
348   LineEdit2->setText("");
349   LineEdit3->setText("");
350   LineEdit4->setText("");
351   LineEdit5->setText("");
352   LineEdit6->setText("");
353   myOk1 = myOk2 = myOk3 = myOk4 = myOk5 = myOk6 = false;
354   myEditCurrentArgument = LineEdit1;
355   myEditCurrentArgument->setFocus();
356
357   if (!TextLabel5->isEnabled())
358     {
359       TextLabel5->setEnabled(true);
360       SelectButton5->setEnabled(true);
361       LineEdit5->setEnabled(true);
362     }
363   else if (!TextLabel6->isEnabled())
364     {
365       TextLabel6->setEnabled(true);
366       SelectButton6->setEnabled(true);
367       LineEdit6->setEnabled(true);
368     }
369
370   if (constructorId == 1 || constructorId == 3) {
371     if (CheckBoxPolygons->isVisible())
372       CheckBoxPolygons->hide();
373     if (CheckBoxPolyedrs->isVisible())
374       CheckBoxPolyedrs->hide();
375   }
376
377   switch(constructorId)
378     {
379     case 0 :
380       { 
381         GroupArguments->setTitle( tr( "SEW_FREE_BORDERS" ) );
382         SubGroup1->setTitle( tr( "BORDER_1" ) );
383         SubGroup2->setTitle( tr( "BORDER_2" ) );
384
385         if (!CheckBoxPolygons->isVisible())
386           CheckBoxPolygons->show();
387         if (!CheckBoxPolyedrs->isVisible())
388           CheckBoxPolyedrs->show();
389
390         break;
391       }
392     case 1 :
393       {
394         GroupArguments->setTitle( tr( "SEW_CONFORM_FREE_BORDERS" ) );
395         SubGroup1->setTitle( tr( "BORDER_1" ) );
396         SubGroup2->setTitle( tr( "BORDER_2" ) );
397
398         TextLabel6->setEnabled(false);
399         SelectButton6->setEnabled(false);
400         LineEdit6->setEnabled(false);
401
402         myOk6 = true;
403
404         break;
405       }
406     case 2 :
407       {
408         GroupArguments->setTitle( tr( "SEW_BORDER_TO_SIDE" ) );
409         SubGroup1->setTitle( tr( "BORDER" ) );
410         SubGroup2->setTitle( tr( "SIDE" ) );
411
412         TextLabel5->setEnabled(false);
413         SelectButton5->setEnabled(false);
414         LineEdit5->setEnabled(false);
415
416         if (!CheckBoxPolygons->isVisible())
417           CheckBoxPolygons->show();
418         if (!CheckBoxPolyedrs->isVisible())
419           CheckBoxPolyedrs->show();
420
421         myOk5 = true;
422
423         break;
424       }
425     case 3 :
426       { 
427         GroupArguments->setTitle( tr( "SEW_SIDE_ELEMENTS" ) );
428         SubGroup1->setTitle( tr( "SIDE_1" ) );
429         SubGroup2->setTitle( tr( "SIDE_2" ) );
430
431         TextLabel1->setText( tr( "SMESH_ID_ELEMENTS" ) );
432         TextLabel2->setText( tr( "NODE1_TO_MERGE" ) );
433         TextLabel3->setText( tr( "NODE2_TO_MERGE" ) );
434         TextLabel4->setText( tr( "SMESH_ID_ELEMENTS" ) );
435         TextLabel5->setText( tr( "NODE1_TO_MERGE" ) );
436         TextLabel6->setText( tr( "NODE2_TO_MERGE" ) );
437
438         LineEdit1->setValidator( new SMESHGUI_IdValidator( this, "validator"));
439         LineEdit4->setValidator( new SMESHGUI_IdValidator( this, "validator"));
440
441         SMESH::SetPointRepresentation(false);
442         QAD_Application::getDesktop()->SetSelectionMode( CellSelection, true );
443         break;
444       }
445     }
446
447   if (constructorId != 3)
448     {
449       TextLabel1->setText( tr( "FIRST_NODE_ID" ) );
450       TextLabel2->setText( tr( "SECOND_NODE_ID" ) );
451       TextLabel3->setText( tr( "LAST_NODE_ID" ) );
452       TextLabel4->setText( tr( "FIRST_NODE_ID" ) );
453       TextLabel5->setText( tr( "SECOND_NODE_ID" ) );
454       TextLabel6->setText( tr( "LAST_NODE_ID" ) );
455
456       LineEdit1->setValidator( new SMESHGUI_IdValidator( this, "validator", 1));
457       LineEdit4->setValidator( new SMESHGUI_IdValidator( this, "validator", 1));
458
459       SMESH::SetPointRepresentation(true);
460       QAD_Application::getDesktop()->SetSelectionMode( NodeSelection, true );
461     }
462
463   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
464 }
465
466
467 //=================================================================================
468 // function : ClickOnApply()
469 // purpose  :
470 //=================================================================================
471 bool SMESHGUI_SewingDlg::ClickOnApply()
472 {
473   if (mySMESHGUI->ActiveStudyLocked())
474     return false;
475   
476   bool aResult = false;
477
478   if ( IsValid() )
479     {
480       bool toMerge = CheckBoxMerge->isChecked();
481       bool toCreatePolygons = CheckBoxPolygons->isChecked();
482       bool toCreatePolyedrs = CheckBoxPolyedrs->isChecked();
483
484       try
485         {
486           SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
487           QApplication::setOverrideCursor(Qt::waitCursor);
488         
489           int aConstructorId = GetConstructorId();
490           SMESH::SMESH_MeshEditor::Sew_Error anError;
491   
492           if (aConstructorId == 0)
493             anError = aMeshEditor->SewFreeBorders(LineEdit1->text().toLong(),
494                                                   LineEdit2->text().toLong(),
495                                                   LineEdit3->text().toLong(),
496                                                   LineEdit4->text().toLong(),
497                                                   LineEdit5->text().toLong(),
498                                                   LineEdit6->text().toLong(),
499                                                   toCreatePolygons,
500                                                   toCreatePolyedrs);
501           else if (aConstructorId == 1)
502             anError = aMeshEditor->SewConformFreeBorders(LineEdit1->text().toLong(),
503                                                          LineEdit2->text().toLong(),
504                                                          LineEdit3->text().toLong(),
505                                                          LineEdit4->text().toLong(),
506                                                          LineEdit5->text().toLong());
507           else if (aConstructorId == 2)
508             anError = aMeshEditor->SewBorderToSide(LineEdit1->text().toLong(),
509                                                    LineEdit2->text().toLong(),
510                                                    LineEdit3->text().toLong(),
511                                                    LineEdit4->text().toLong(),
512                                                    LineEdit6->text().toLong(),
513                                                    toCreatePolygons,
514                                                    toCreatePolyedrs);
515           else if (aConstructorId == 3)
516             {
517               QStringList aListElementsId1 = QStringList::split( " ", LineEdit1->text(), false);
518               QStringList aListElementsId2 = QStringList::split( " ", LineEdit4->text(), false);
519               
520               SMESH::long_array_var anElementsId1 = new SMESH::long_array;
521               SMESH::long_array_var anElementsId2 = new SMESH::long_array;
522       
523               anElementsId1->length( aListElementsId1.count() );
524               anElementsId2->length( aListElementsId2.count() );
525
526               for ( int i = 0; i < aListElementsId1.count(); i++ )
527                 anElementsId1[i] = aListElementsId1[i].toInt();
528               for ( int i = 0; i < aListElementsId2.count(); i++ )
529                 anElementsId2[i] = aListElementsId2[i].toInt();
530
531               anError = aMeshEditor->SewSideElements(anElementsId1.inout(),
532                                                      anElementsId2.inout(),
533                                                      LineEdit2->text().toLong(),
534                                                      LineEdit5->text().toLong(),
535                                                      LineEdit3->text().toLong(),
536                                                      LineEdit6->text().toLong());
537             }
538           aResult = ( anError == SMESH::SMESH_MeshEditor::SEW_OK );
539
540           if (toMerge && aResult)
541             aMeshEditor->MergeEqualElements();
542           
543           QApplication::restoreOverrideCursor();
544
545           if ( !aResult ) {
546             QString msg = tr(QString("ERROR_%1").arg(anError));
547             QAD_MessageBox::warn1(QAD_Application::getDesktop(),
548                                   tr("SMESH_WRN_WARNING"),msg,tr("SMESH_BUT_OK"));
549           }
550         }
551       catch( ... )
552         {
553         }
554       
555       if ( aResult )
556         {
557           Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
558           mySelection->ClearIObjects();
559           SMESH::UpdateView();
560           mySelection->AddIObject( anIO, false );
561           Init();
562           ConstructorsClicked(GetConstructorId());
563         }
564     }
565
566   return aResult;
567 }
568
569 //=================================================================================
570 // function : ClickOnOk()
571 // purpose  :
572 //=================================================================================
573 void SMESHGUI_SewingDlg::ClickOnOk()
574 {
575   if( ClickOnApply())
576     ClickOnCancel() ;
577 }
578
579         
580 //=================================================================================
581 // function : ClickOnCancel()
582 // purpose  :
583 //=================================================================================
584 void SMESHGUI_SewingDlg::ClickOnCancel()
585 {
586   mySelection->ClearIObjects();
587   SMESH::SetPointRepresentation(false);
588   QAD_Application::getDesktop()->SetSelectionMode( ActorSelection );
589   disconnect( mySelection, 0, this, 0 );
590   mySMESHGUI->ResetState() ;
591   reject() ;
592 }
593
594 //=======================================================================
595 //function : onTextChange
596 //purpose  : 
597 //=======================================================================
598
599 void SMESHGUI_SewingDlg::onTextChange(const QString& theNewText)
600 {
601   QLineEdit* send = (QLineEdit*)sender();
602
603   if ( myBusy ) return;
604   myBusy = true;
605
606   if (send)
607     myEditCurrentArgument = send;
608  
609   if      (send == LineEdit1)
610     myOk1 = false;
611   else if (send == LineEdit2)
612     myOk2 = false;
613   else if (send == LineEdit3)
614     myOk3 = false;
615   else if (send == LineEdit4)
616     myOk4 = false;
617   else if (send == LineEdit5)
618     myOk5 = false;
619   else if (send == LineEdit6)
620     myOk6 = false;
621
622   buttonOk->setEnabled( false );
623   buttonApply->setEnabled( false );
624   
625   // hilight entered elements/nodes
626   SMDS_Mesh* aMesh = 0;
627   
628   if ( myActor )
629     aMesh = myActor->GetObject()->GetMesh();
630   else
631     send->clear();
632   
633   if ( aMesh ) {
634     mySelection->ClearIObjects();
635     mySelection->AddIObject( myActor->getIO() );
636   
637     if (GetConstructorId() != 3 || (send != LineEdit1 && send != LineEdit4))
638       {
639         SMESH::SetPointRepresentation(true);
640         QAD_Application::getDesktop()->SetSelectionMode( NodeSelection, true );
641         
642         const SMDS_MeshNode * n = aMesh->FindNode( theNewText.toInt() );
643         if ( n ) {
644           if ( !mySelection->IsIndexSelected( myActor->getIO(), n->GetID() ))
645             mySelection->AddOrRemoveIndex (myActor->getIO(), n->GetID(), true);
646           
647           if      (send == LineEdit1)
648             myOk1 = true;
649           else if (send == LineEdit2)
650             myOk2 = true;
651           else if (send == LineEdit3)
652             myOk3 = true;
653           else if (send == LineEdit4)
654             myOk4 = true;
655           else if (send == LineEdit5)
656             myOk5 = true;
657           else if (send == LineEdit6)
658             myOk6 = true;
659         }
660       }
661     else
662       {
663         SMESH::SetPointRepresentation(false);
664         QAD_Application::getDesktop()->SetSelectionMode( CellSelection, true );
665         
666         QStringList aListId = QStringList::split( " ", theNewText, false);
667         
668         bool isEvenOneExists = false;
669         
670         for ( int i = 0; i < aListId.count(); i++ ) {
671           const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() );
672           if ( e ) {
673             if ( !mySelection->IsIndexSelected( myActor->getIO(), e->GetID() ))
674               mySelection->AddOrRemoveIndex (myActor->getIO(), e->GetID(), true);
675             if (!isEvenOneExists) 
676               isEvenOneExists = true;
677           }
678         }
679         
680         if (isEvenOneExists)
681           {
682             if (send == LineEdit1)
683               myOk1 = true;
684             else if(send == LineEdit4)
685               myOk4 = true;
686           }
687         else
688           send->clear();
689       }
690   }
691   
692   if ( IsValid() ) {
693     buttonOk->setEnabled( true );
694     buttonApply->setEnabled( true );
695   }
696   
697   myBusy = false;
698 }
699
700
701 //=================================================================================
702 // function : SelectionIntoArgument()
703 // purpose  : Called when selection as changed or other case
704 //=================================================================================
705 void SMESHGUI_SewingDlg::SelectionIntoArgument(bool isSelectionChanged)
706 {
707   if ( myBusy ) return;
708   
709   // clear
710   if (isSelectionChanged)
711     myActor = 0;
712   
713   QString aString = "";
714   
715   myBusy = true;
716   myEditCurrentArgument->setText( aString );
717   myBusy = false;
718   
719   if ( !GroupButtons->isEnabled() ) // inactive
720     return;
721   
722   buttonOk->setEnabled( false );
723   buttonApply->setEnabled( false );
724   
725   // get selected mesh
726   int nbSel = mySelection->IObjectCount();
727   if(nbSel != 1)
728     return;
729   
730   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject();
731   myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IO) ;
732   myActor = SMESH::FindActorByEntry( mySelection->firstIObject()->getEntry() );
733   
734   if (myMesh->_is_nil() || !myActor)
735     return;
736   
737   // get selected elements/nodes
738   
739   int aNbUnits = 0;
740   
741   if (GetConstructorId() != 3 || (myEditCurrentArgument != LineEdit1 && myEditCurrentArgument != LineEdit4))
742     {
743       aNbUnits = SMESH::GetNameOfSelectedNodes(mySelection, aString);
744       if(aNbUnits != 1)
745         return;
746     }
747   else
748     {
749       aNbUnits = SMESH::GetNameOfSelectedElements(mySelection, aString);
750       if(aNbUnits < 1) 
751         return;
752     }
753   
754   myBusy = true;
755   myEditCurrentArgument->setText( aString );
756   myBusy = false;
757   
758   // OK
759   if (myEditCurrentArgument == LineEdit1)
760     myOk1 = true;
761   else if(myEditCurrentArgument == LineEdit2)
762     myOk2 = true;
763   else if(myEditCurrentArgument == LineEdit3)
764     myOk3 = true;
765   else if(myEditCurrentArgument == LineEdit4)
766     myOk4 = true;
767   else if(myEditCurrentArgument == LineEdit5)
768     myOk5 = true;
769   else if(myEditCurrentArgument == LineEdit6)
770     myOk6 = true;
771   
772   if(IsValid())
773     {
774       buttonOk->setEnabled( true );
775       buttonApply->setEnabled( true );
776     }
777 }
778
779
780 //=================================================================================
781 // function : SetEditCurrentArgument()
782 // purpose  :
783 //=================================================================================
784 void SMESHGUI_SewingDlg::SetEditCurrentArgument()
785 {
786   QPushButton* send = (QPushButton*)sender();
787   
788   disconnect( mySelection, 0, this, 0 );
789   mySelection->ClearIObjects();
790   
791   if(send == SelectButton1) {
792     myEditCurrentArgument = LineEdit1;
793     myOk1 = false;
794   }
795   else if (send == SelectButton2) {
796     myEditCurrentArgument = LineEdit2;
797     myOk2 = false;
798   }
799   else if (send == SelectButton3) {
800     myEditCurrentArgument = LineEdit3;
801     myOk3 = false;
802   }
803   else if (send == SelectButton4) {
804     myEditCurrentArgument = LineEdit4;
805     myOk4 = false;
806   }
807   else if (send == SelectButton5) {
808     myEditCurrentArgument = LineEdit5;
809     myOk5 = false;
810   }
811   else if (send == SelectButton6) {
812     myEditCurrentArgument = LineEdit6;
813     myOk6 = false;
814   }
815   
816   if (GetConstructorId() != 3 || (send != SelectButton1 && send != SelectButton4))
817     {
818       SMESH::SetPointRepresentation(true);
819       QAD_Application::getDesktop()->SetSelectionMode( NodeSelection, true );
820     }
821   else
822     {
823       SMESH::SetPointRepresentation(false);
824       QAD_Application::getDesktop()->SetSelectionMode( CellSelection, true );
825     }
826   
827   myEditCurrentArgument->setFocus();
828   connect( mySelection, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
829   SelectionIntoArgument(false) ;
830 }
831
832 //=================================================================================
833 // function : DeactivateActiveDialog()
834 // purpose  :
835 //=================================================================================
836 void SMESHGUI_SewingDlg::DeactivateActiveDialog()
837 {
838   if ( GroupConstructors->isEnabled() ) {
839     GroupConstructors->setEnabled(false) ;
840     GroupArguments->setEnabled(false) ;
841     GroupButtons->setEnabled(false) ;
842     mySMESHGUI->ResetState() ;    
843     mySMESHGUI->SetActiveDialogBox(0) ;
844   }
845 }
846
847
848 //=================================================================================
849 // function : ActivateThisDialog()
850 // purpose  :
851 //=================================================================================
852 void SMESHGUI_SewingDlg::ActivateThisDialog()
853 {
854   /* Emit a signal to deactivate the active dialog */
855   mySMESHGUI->EmitSignalDeactivateDialog() ;   
856   GroupConstructors->setEnabled(true) ;
857   GroupArguments->setEnabled(true) ;
858   GroupButtons->setEnabled(true) ;
859   
860   mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
861   
862   ConstructorsClicked(GetConstructorId());
863   SelectionIntoArgument();
864 }
865
866
867 //=================================================================================
868 // function : enterEvent()
869 // purpose  :
870 //=================================================================================
871 void SMESHGUI_SewingDlg::enterEvent(QEvent* e)
872 {
873   if ( GroupConstructors->isEnabled() )
874     return ;  
875   ActivateThisDialog() ;
876 }
877
878
879 //=================================================================================
880 // function : closeEvent()
881 // purpose  :
882 //=================================================================================
883 void SMESHGUI_SewingDlg::closeEvent( QCloseEvent* e )
884 {
885   /* same than click on cancel button */
886   this->ClickOnCancel() ;
887 }
888
889
890 //=======================================================================
891 //function : hideEvent
892 //purpose  : caused by ESC key
893 //=======================================================================
894
895 void SMESHGUI_SewingDlg::hideEvent ( QHideEvent * e )
896 {
897   if ( !isMinimized() )
898     ClickOnCancel();
899 }
900
901
902 //=================================================================================
903 // function : GetConstructorId()
904 // purpose  : 
905 //=================================================================================
906 int SMESHGUI_SewingDlg::GetConstructorId()
907
908   if ( GroupConstructors != NULL && GroupConstructors->selected() != NULL )
909     return GroupConstructors->id( GroupConstructors->selected() );
910   return -1;
911 }
912
913
914 //=================================================================================
915 // function : GetConstructorId()
916 // purpose  : 
917 //=================================================================================
918 bool SMESHGUI_SewingDlg::IsValid()
919 {
920   return (myOk1 && myOk2 && myOk3 && myOk4 && myOk5 && myOk6);
921 }