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