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