Salome HOME
IMP 23078: [CEA 1498] Sewing of meshes without having to set the nodes ids
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_SewingDlg.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_SewingDlg.cxx
25 // Author : Michael ZORIN, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_SewingDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_IdPreview.h"
32 #include "SMESHGUI_IdValidator.h"
33 #include "SMESHGUI_MergeDlg.h"
34 #include "SMESHGUI_MeshUtils.h"
35 #include "SMESHGUI_PreVisualObj.h"
36 #include "SMESHGUI_SpinBox.h"
37 #include "SMESHGUI_Utils.h"
38 #include "SMESHGUI_VTKUtils.h"
39
40 #include <SMDS_Mesh.hxx>
41 #include <SMESH_Actor.h>
42 #include <SMESH_TypeDefs.hxx>
43
44 // SALOME GUI includes
45 #include <LightApp_Application.h>
46 #include <LightApp_SelectionMgr.h>
47 #include <SALOME_ListIO.hxx>
48 #include <SUIT_Desktop.h>
49 #include <SUIT_MessageBox.h>
50 #include <SUIT_OverrideCursor.h>
51 #include <SUIT_ResourceMgr.h>
52 #include <SUIT_Session.h>
53 #include <SVTK_ViewModel.h>
54 #include <SVTK_ViewWindow.h>
55 #include <SalomeApp_IntSpinBox.h>
56
57 // OCCT includes
58 #include <TColStd_MapOfInteger.hxx>
59
60 // Qt includes
61 #include <QApplication>
62 #include <QButtonGroup>
63 #include <QCheckBox>
64 #include <QGridLayout>
65 #include <QGroupBox>
66 #include <QHBoxLayout>
67 #include <QKeyEvent>
68 #include <QLabel>
69 #include <QLineEdit>
70 #include <QListWidget>
71 #include <QPushButton>
72 #include <QRadioButton>
73 #include <QToolButton>
74 #include <QVBoxLayout>
75
76 #define SPACING 6
77 #define MARGIN  11
78
79 namespace
80 {
81   enum ActionType { MODE_AUTO=0, MODE_MANUAL,
82                     MOVE_LEFT_1=0, MOVE_RIGHT_1, MOVE_LEFT_2, MOVE_RIGHT_2,
83                     GROUP_COLOR=Qt::UserRole, GROUP_INDEX };
84 }
85
86 //=================================================================================
87 /*!
88  * \brief Dispalayer of free borders
89  */
90 //=================================================================================
91
92 struct SMESHGUI_SewingDlg::BorderGroupDisplayer
93 {
94   const SMESH::ListOfFreeBorders& myBorders;
95   const SMESH::FreeBordersGroup&  myGroup;
96   QColor                          myColor;
97   SMESH::SMESH_Mesh_ptr           myMesh;
98
99   std::vector< SMESH_Actor* >     myPartActors;
100   SVTK_ViewWindow*                myViewWindow;
101   SMESHGUI_IdPreview              myIdPreview;
102
103   BorderGroupDisplayer( const SMESH::CoincidentFreeBorders& borders,
104                         int                                 groupIndex,
105                         QColor                              color,
106                         SMESH::SMESH_Mesh_ptr               mesh);
107   ~BorderGroupDisplayer();
108   void Hide();
109   void ShowGroup( bool wholeBorders );
110   void ShowPart( int partIndex, bool toEdit );
111   void Update();
112
113 private:
114   void getPartEnds( int partIndex, std::vector<int> & ids, std::list<gp_XYZ>& coords);
115 };
116
117 //=================================================================================
118 // class    : SMESHGUI_SewingDlg()
119 // purpose  :
120 //=================================================================================
121 SMESHGUI_SewingDlg::SMESHGUI_SewingDlg( SMESHGUI* theModule )
122   : QDialog( SMESH::GetDesktop( theModule ) ),
123     mySMESHGUI( theModule ),
124     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
125 {
126   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
127   QPixmap image0 (mgr->loadPixmap("SMESH", tr("ICON_SMESH_SEWING_FREEBORDERS")));
128   QPixmap image1 (mgr->loadPixmap("SMESH", tr("ICON_SMESH_SEWING_CONFORM_FREEBORDERS")));
129   QPixmap image2 (mgr->loadPixmap("SMESH", tr("ICON_SMESH_SEWING_BORDERTOSIDE")));
130   QPixmap image3 (mgr->loadPixmap("SMESH", tr("ICON_SMESH_SEWING_SIDEELEMENTS")));
131   QPixmap image4 (mgr->loadPixmap("SMESH", tr("ICON_SELECT")));
132   QPixmap IconRemove(mgr->loadPixmap("SMESH", tr("ICON_REMOVE")));
133
134   setModal(false);
135   setAttribute(Qt::WA_DeleteOnClose, true);
136   setWindowTitle(tr("SMESH_SEWING"));
137   setSizeGripEnabled(true);
138
139   QVBoxLayout* SMESHGUI_SewingDlgLayout = new QVBoxLayout(this);
140   SMESHGUI_SewingDlgLayout->setSpacing(SPACING);
141   SMESHGUI_SewingDlgLayout->setMargin(MARGIN);
142
143   /***************************************************************/
144   ConstructorsBox = new QGroupBox(tr("SMESH_SEWING"), this);
145   GroupConstructors = new QButtonGroup(this);
146   QHBoxLayout* ConstructorsBoxLayout = new QHBoxLayout(ConstructorsBox);
147   ConstructorsBoxLayout->setSpacing(SPACING);
148   ConstructorsBoxLayout->setMargin(MARGIN);
149
150   RadioButton1 = new QRadioButton(ConstructorsBox);
151   RadioButton1->setIcon(image0);
152   RadioButton2 = new QRadioButton(ConstructorsBox);
153   RadioButton2->setIcon(image1);
154   RadioButton3 = new QRadioButton(ConstructorsBox);
155   RadioButton3->setIcon(image2);
156   RadioButton4 = new QRadioButton(ConstructorsBox);
157   RadioButton4->setIcon(image3);
158
159   ConstructorsBoxLayout->addWidget(RadioButton1);
160   ConstructorsBoxLayout->addWidget(RadioButton2);
161   ConstructorsBoxLayout->addWidget(RadioButton3);
162   ConstructorsBoxLayout->addWidget(RadioButton4);
163   GroupConstructors->addButton(RadioButton1, 0);
164   GroupConstructors->addButton(RadioButton2, 1);
165   GroupConstructors->addButton(RadioButton3, 2);
166   GroupConstructors->addButton(RadioButton4, 3);
167
168   /***************************************************************/
169   GroupArguments = new QGroupBox(this);
170   QVBoxLayout* GroupArgumentsLayout = new QVBoxLayout(GroupArguments);
171   GroupArgumentsLayout->setSpacing(SPACING);
172   GroupArgumentsLayout->setMargin(MARGIN);
173
174   // First subgroup
175   SubGroup1 = new QGroupBox(GroupArguments);
176   QGridLayout* SubGroup1Layout = new QGridLayout(SubGroup1);
177   SubGroup1Layout->setSpacing(SPACING);
178   SubGroup1Layout->setMargin(MARGIN);
179
180   // Controls of the first subgroup
181   TextLabel1 = new QLabel(SubGroup1);
182   SelectButton1  = new QPushButton(SubGroup1);
183   SelectButton1->setIcon(image4);
184   LineEdit1 = new QLineEdit(SubGroup1);
185
186   TextLabel2 = new QLabel(SubGroup1);
187   SelectButton2  = new QPushButton(SubGroup1);
188   SelectButton2->setIcon(image4);
189   LineEdit2 = new QLineEdit(SubGroup1);
190
191   TextLabel3 = new QLabel(SubGroup1);
192   SelectButton3  = new QPushButton(SubGroup1);
193   SelectButton3->setIcon(image4);
194   LineEdit3 = new QLineEdit(SubGroup1);
195
196   SubGroup1Layout->addWidget(TextLabel1,    0, 0);
197   SubGroup1Layout->addWidget(SelectButton1, 0, 1);
198   SubGroup1Layout->addWidget(LineEdit1,     0, 2);
199   SubGroup1Layout->addWidget(TextLabel2,    1, 0);
200   SubGroup1Layout->addWidget(SelectButton2, 1, 1);
201   SubGroup1Layout->addWidget(LineEdit2,     1, 2);
202   SubGroup1Layout->addWidget(TextLabel3,    2, 0);
203   SubGroup1Layout->addWidget(SelectButton3, 2, 1);
204   SubGroup1Layout->addWidget(LineEdit3,     2, 2);
205
206   // Second subgroup
207   SubGroup2 = new QGroupBox(GroupArguments);
208   QGridLayout* SubGroup2Layout = new QGridLayout(SubGroup2);
209   SubGroup2Layout->setSpacing(SPACING);
210   SubGroup2Layout->setMargin(MARGIN);
211
212   // Controls of the first subgroup
213   TextLabel4 = new QLabel(SubGroup2);
214   SelectButton4  = new QPushButton(SubGroup2);
215   SelectButton4->setIcon(image4);
216   LineEdit4 = new QLineEdit(SubGroup2);
217
218   TextLabel5 = new QLabel(SubGroup2);
219   SelectButton5  = new QPushButton(SubGroup2);
220   SelectButton5->setIcon(image4);
221   LineEdit5 = new QLineEdit(SubGroup2);
222
223   TextLabel6 = new QLabel(SubGroup2);
224   SelectButton6  = new QPushButton(SubGroup2);
225   SelectButton6->setIcon(image4);
226   LineEdit6 = new QLineEdit(SubGroup2);
227
228   SubGroup2Layout->addWidget(TextLabel4,    0, 0);
229   SubGroup2Layout->addWidget(SelectButton4, 0, 1);
230   SubGroup2Layout->addWidget(LineEdit4,     0, 2);
231   SubGroup2Layout->addWidget(TextLabel5,    1, 0);
232   SubGroup2Layout->addWidget(SelectButton5, 1, 1);
233   SubGroup2Layout->addWidget(LineEdit5,     1, 2);
234   SubGroup2Layout->addWidget(TextLabel6,    2, 0);
235   SubGroup2Layout->addWidget(SelectButton6, 2, 1);
236   SubGroup2Layout->addWidget(LineEdit6,     2, 2);
237
238   // Control for the merging equal elements
239   CheckBoxMerge = new QCheckBox(tr("MERGE_EQUAL_ELEMENTS"), GroupArguments);
240
241   // Control for the polygons creation instead of splitting
242   CheckBoxPolygons = new QCheckBox(tr("CREATE_POLYGONS_INSTEAD_SPLITTING"), GroupArguments);
243   
244   // Control for the polyedres creation to obtain conform mesh
245   CheckBoxPolyedrs = new QCheckBox(tr("CREATE_POLYEDRS_NEAR_BOUNDARY"), GroupArguments);
246
247   /***************************************************************/
248   // Controls to switch free borders mode ( auto || manual )
249
250   ModeGroup = new QGroupBox( tr( "SMESH_MODE" ), GroupArguments );
251   ModeButGrp = new QButtonGroup( ModeGroup );
252   QHBoxLayout* aModeGroupLayout = new QHBoxLayout( ModeGroup );
253   aModeGroupLayout->setMargin( MARGIN );
254   aModeGroupLayout->setSpacing( SPACING );
255
256   QRadioButton* rb1 = new QRadioButton( tr( "SMESH_AUTOMATIC" ), ModeGroup );
257   QRadioButton* rb2 = new QRadioButton( tr( "SMESH_MANUAL"    ), ModeGroup );
258   ModeButGrp->addButton( rb1, MODE_AUTO );
259   ModeButGrp->addButton( rb2, MODE_MANUAL );
260   aModeGroupLayout->addWidget( rb1 );
261   aModeGroupLayout->addWidget( rb2 );
262   rb1->setChecked(true);
263
264   /***************************************************************/
265   // Controls for detecting coincident free borders
266
267   SewFreeBordersWidget = new QWidget( GroupArguments );
268   QVBoxLayout* aSewFreeBordersLayout = new QVBoxLayout( SewFreeBordersWidget );
269   aSewFreeBordersLayout->setMargin( 0 );
270   aSewFreeBordersLayout->setSpacing( SPACING );
271
272   // Tolerance
273   QWidget* TolAndAuto = new QWidget(SewFreeBordersWidget);
274   QLabel* TextLabelTolerance = new QLabel(tr("SMESH_TOLERANCE"), TolAndAuto);
275   SpinBoxTolerance = new SMESHGUI_SpinBox(TolAndAuto);
276   SpinBoxTolerance->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
277   SpinBoxTolerance->RangeStepAndValidator(0.0, COORD_MAX, 0.00001, "len_tol_precision");
278   SpinBoxTolerance->SetValue(0.); // auto-tolerance
279
280   // Auto Sewing
281   AutoSewCheck = new QCheckBox(tr("AUTO_SEWING"), TolAndAuto);
282   AutoSewCheck->setChecked( true );
283
284   // mesh
285   QGroupBox* GroupMesh = new QGroupBox(tr("SMESH_SELECT_WHOLE_MESH"), SewFreeBordersWidget);
286   QHBoxLayout* GroupMeshLayout = new QHBoxLayout(GroupMesh);
287   GroupMeshLayout->setSpacing(SPACING);
288   GroupMeshLayout->setMargin(MARGIN);
289
290   QLabel* TextLabelName = new QLabel(tr("SMESH_NAME"), GroupMesh);
291   //SelectMeshButton = new QPushButton(GroupMesh);
292   //SelectMeshButton->setIcon(IconSelect);
293   LineEditMesh = new QLineEdit(GroupMesh);
294   LineEditMesh->setReadOnly(true);
295
296   GroupMeshLayout->addWidget(TextLabelName);
297   //GroupMeshLayout->addWidget(SelectMeshButton);
298   GroupMeshLayout->addWidget(LineEditMesh);
299
300   QGridLayout* TolAndAutoLayout = new QGridLayout( TolAndAuto );
301   TolAndAutoLayout->setSpacing(SPACING);
302   TolAndAutoLayout->setMargin(0);
303   TolAndAutoLayout->addWidget(GroupMesh,          0, 0, 1, 2 );
304   TolAndAutoLayout->addWidget(TextLabelTolerance, 1, 0 );
305   TolAndAutoLayout->addWidget(SpinBoxTolerance,   1, 1 );
306   TolAndAutoLayout->addWidget(AutoSewCheck,       2, 0 );
307
308   aSewFreeBordersLayout->addWidget( TolAndAuto );
309
310   /******************/
311   // Coincident group
312   GroupCoincidentWidget = new QWidget(SewFreeBordersWidget);
313   QGridLayout* GroupCoincidentLayout = new QGridLayout(GroupCoincidentWidget);
314   GroupCoincidentLayout->setSpacing(SPACING);
315   GroupCoincidentLayout->setMargin(0);
316
317   QGroupBox* GroupCoincident = new QGroupBox(tr("COINCIDENT_FREE_BORDERS"), GroupCoincidentWidget);
318   QGridLayout* aCoincidentLayout = new QGridLayout(GroupCoincident);
319   aCoincidentLayout->setSpacing(SPACING);
320   aCoincidentLayout->setMargin(MARGIN);
321
322   /*******/
323   // borders
324   ListCoincident = new QListWidget(GroupCoincident);
325   ListCoincident->setSelectionMode(QListWidget::ExtendedSelection);
326
327   DetectButton      = new QPushButton(tr("DETECT"),           GroupCoincident);
328   RemoveGroupButton = new QPushButton(tr("SMESH_BUT_REMOVE"), GroupCoincident);
329
330   SelectAllCheck = new QCheckBox(tr("SELECT_ALL"), GroupCoincident);
331
332   aCoincidentLayout->addWidget(ListCoincident,    0, 0, 4, 2);
333   aCoincidentLayout->addWidget(DetectButton,      1, 2);
334   aCoincidentLayout->addWidget(RemoveGroupButton, 3, 2);
335   aCoincidentLayout->addWidget(SelectAllCheck,    4, 0);
336   aCoincidentLayout->setRowMinimumHeight(1, 10);
337   aCoincidentLayout->setRowStretch      (1, 5);
338
339   GroupCoincidentLayout->addWidget( GroupCoincident );
340
341   /*****************************************/
342   // Controls for editing the selected group
343
344   QGroupBox* GroupEdit = new QGroupBox(tr("EDIT_SELECTED_GROUP"), GroupCoincidentWidget);
345   QGridLayout* GroupEditLayout = new QGridLayout(GroupEdit);
346   GroupEditLayout->setSpacing(SPACING);
347   GroupEditLayout->setMargin(MARGIN);
348
349   ListEdit = new QListWidget(GroupEdit);
350   ListEdit->setFlow( QListView::LeftToRight );
351   ListEdit->setSelectionMode(QListWidget::ExtendedSelection);
352   SetFirstButton = new QPushButton(GroupEdit);
353   SetFirstButton->setIcon(QPixmap(SMESHGUI_MergeDlg::IconFirst()));
354   RemoveElemButton = new QPushButton(GroupEdit);
355   RemoveElemButton->setIcon(IconRemove);
356
357   MoveBorderEndsButGrp = new QButtonGroup( GroupEdit );
358   QToolButton* moveBut1 = new QToolButton( GroupEdit );
359   QToolButton* moveBut2 = new QToolButton( GroupEdit );
360   QToolButton* moveBut3 = new QToolButton( GroupEdit );
361   QToolButton* moveBut4 = new QToolButton( GroupEdit );
362   moveBut1->setArrowType( Qt::LeftArrow );
363   moveBut2->setArrowType( Qt::RightArrow );
364   moveBut3->setArrowType( Qt::LeftArrow );
365   moveBut4->setArrowType( Qt::RightArrow );
366   MoveBorderEndsButGrp->addButton( moveBut1, MOVE_LEFT_1 );
367   MoveBorderEndsButGrp->addButton( moveBut2, MOVE_RIGHT_1 );
368   MoveBorderEndsButGrp->addButton( moveBut3, MOVE_LEFT_2 );
369   MoveBorderEndsButGrp->addButton( moveBut4, MOVE_RIGHT_2 );
370
371   SwapBut  = new QPushButton( "<->", GroupEdit );
372   BorderEndLine[0] = new QLineEdit( GroupEdit );
373   BorderEndLine[1] = new QLineEdit( GroupEdit );
374   BorderEndLine[0]->setReadOnly(true);
375   BorderEndLine[1]->setReadOnly(true);
376   QLabel* StepLabel = new QLabel(tr("STEP"), GroupEdit );
377   StepSpin = new SalomeApp_IntSpinBox( 1, 100000, 1, GroupEdit,
378                                        /*acceptNames=*/false, /*showTip=*/false );
379   StepSpin->setValue( 1 );
380
381   GroupEditLayout->addWidget(ListEdit,         0, 0, 1, 8);
382   GroupEditLayout->addWidget(SetFirstButton,   0, 8);
383   GroupEditLayout->addWidget(RemoveElemButton, 0, 9);
384   GroupEditLayout->addWidget(moveBut1,         1, 0);
385   GroupEditLayout->addWidget(BorderEndLine[0], 1, 1);
386   GroupEditLayout->addWidget(moveBut2,         1, 2);
387   GroupEditLayout->addWidget(moveBut3,         1, 3);
388   GroupEditLayout->addWidget(BorderEndLine[1], 1, 4);
389   GroupEditLayout->addWidget(moveBut4,         1, 5);
390   GroupEditLayout->setColumnStretch(              6, 5 );
391   GroupEditLayout->addWidget(SwapBut,          1, 7);
392   GroupEditLayout->addWidget(StepLabel,        1, 8);
393   GroupEditLayout->addWidget(StepSpin,         1, 9);
394
395   GroupCoincidentLayout->addWidget( GroupEdit );
396   aSewFreeBordersLayout->addWidget( GroupCoincidentWidget );
397
398   // layout
399   GroupArgumentsLayout->addWidget(ModeGroup);
400   GroupArgumentsLayout->addWidget(SubGroup1);
401   GroupArgumentsLayout->addWidget(SubGroup2);
402   GroupArgumentsLayout->addWidget(SewFreeBordersWidget);
403   GroupArgumentsLayout->addWidget(CheckBoxMerge);
404   GroupArgumentsLayout->addWidget(CheckBoxPolygons);
405   GroupArgumentsLayout->addWidget(CheckBoxPolyedrs);
406
407   /***************************************************************/
408   GroupButtons = new QGroupBox(this);
409   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
410   GroupButtonsLayout->setSpacing(SPACING);
411   GroupButtonsLayout->setMargin(MARGIN);
412
413   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
414   buttonOk->setAutoDefault(true);
415   buttonOk->setDefault(true);
416   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
417   buttonApply->setAutoDefault(true);
418   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
419   buttonCancel->setAutoDefault(true);
420   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
421   buttonHelp->setAutoDefault(true);
422
423   GroupButtonsLayout->addWidget(buttonOk);
424   GroupButtonsLayout->addSpacing(10);
425   GroupButtonsLayout->addWidget(buttonApply);
426   GroupButtonsLayout->addSpacing(10);
427   GroupButtonsLayout->addStretch();
428   GroupButtonsLayout->addWidget(buttonCancel);
429   GroupButtonsLayout->addWidget(buttonHelp);
430
431   /***************************************************************/
432   SMESHGUI_SewingDlgLayout->addWidget(ConstructorsBox);
433   SMESHGUI_SewingDlgLayout->addWidget(GroupArguments);
434   SMESHGUI_SewingDlgLayout->addWidget(GroupButtons);
435   //SMESHGUI_SewingDlgLayout->setStretch( 2, 10 );
436
437   /* Initialisations */
438   RadioButton1->setChecked(true);
439
440   LineEdit2->setValidator(new SMESHGUI_IdValidator(this, 1));
441   LineEdit3->setValidator(new SMESHGUI_IdValidator(this, 1));
442   LineEdit5->setValidator(new SMESHGUI_IdValidator(this, 1));
443   LineEdit6->setValidator(new SMESHGUI_IdValidator(this, 1));
444
445   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
446
447   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
448
449   myHelpFileName = "sewing_meshes_page.html";
450
451   myActor = 0;
452   setDisplayMode();
453
454   Init();
455
456   /* signals and slots connections */
457   connect(buttonOk,     SIGNAL(clicked()), this, SLOT(ClickOnOk()));
458   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
459   connect(buttonApply,  SIGNAL(clicked()), this, SLOT(ClickOnApply()));
460   connect(buttonHelp,   SIGNAL(clicked()), this, SLOT(ClickOnHelp()));
461   connect(GroupConstructors, SIGNAL(buttonClicked(int)), SLOT(ConstructorsClicked(int)));
462
463   connect(SelectButton1, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
464   connect(SelectButton2, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
465   connect(SelectButton3, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
466   connect(SelectButton4, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
467   connect(SelectButton5, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
468   connect(SelectButton6, SIGNAL (clicked()),   this, SLOT(SetEditCurrentArgument()));
469
470   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
471   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
472   /* to close dialog if study change */
473   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()),      this, SLOT(reject()));
474   connect(mySMESHGUI, SIGNAL(SignalActivatedViewManager()), this, SLOT(onOpenView()));
475   connect(mySMESHGUI, SIGNAL(SignalCloseView()),            this, SLOT(onCloseView()));
476
477   connect(LineEdit1, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
478   connect(LineEdit2, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
479   connect(LineEdit3, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
480   connect(LineEdit4, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
481   connect(LineEdit5, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
482   connect(LineEdit6, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
483
484   connect(ModeButGrp,           SIGNAL(buttonClicked(int)),     SLOT(onModeChange(int)));
485   connect(AutoSewCheck,         SIGNAL(stateChanged(int)),      SLOT(onAutoSew(int)));
486   connect(DetectButton,         SIGNAL(clicked()),              SLOT(onDetectClicked()));
487   connect(RemoveGroupButton,    SIGNAL(clicked()),              SLOT(onRemoveGroupClicked()));
488   connect(ListCoincident,       SIGNAL(itemSelectionChanged()), SLOT(onSelectGroup()));
489   connect(SelectAllCheck,       SIGNAL(stateChanged(int)),      SLOT(onSelectAll(int)));
490   connect(ListEdit,             SIGNAL(itemSelectionChanged()), SLOT(onSelectBorderPartFromGroup()));
491   connect(SetFirstButton,       SIGNAL(clicked()),              SLOT(onSetFirstClicked()));
492   connect(RemoveElemButton,     SIGNAL(clicked()),              SLOT(onRemoveElemClicked()));
493   connect(MoveBorderEndsButGrp, SIGNAL(buttonClicked(int)),     SLOT(onMoveBorderEnd(int)));
494   connect(SwapBut,              SIGNAL(clicked()),              SLOT(onSwapClicked()));
495
496   ConstructorsClicked(0);
497 }
498
499 //=================================================================================
500 // function : ~SMESHGUI_SewingDlg()
501 // purpose  : Destroys the object and frees any allocated resources
502 //=================================================================================
503 SMESHGUI_SewingDlg::~SMESHGUI_SewingDlg()
504 {
505   for ( size_t i = 0; i < myBorderDisplayers.size(); ++i )
506   {
507     delete myBorderDisplayers[ i ];
508     myBorderDisplayers[ i ] = 0;
509   }
510   myBorderDisplayers.clear();
511 }
512
513 //=================================================================================
514 // function : Init()
515 // purpose  :
516 //=================================================================================
517 void SMESHGUI_SewingDlg::Init()
518 {
519   myBusy = false;
520
521   if ( LineEdit1->isVisible() )
522     myEditCurrentArgument = LineEdit1;
523   else
524     myEditCurrentArgument = LineEditMesh;
525   myEditCurrentArgument->setFocus();
526   //myActor = 0;
527   myMesh = SMESH::SMESH_Mesh::_nil();
528   // CheckBoxMerge->setChecked(false);
529   // CheckBoxPolygons->setChecked(false);
530   // CheckBoxPolyedrs->setChecked(false);
531   SelectionIntoArgument();
532 }
533
534 //=================================================================================
535 // function : ConstructorsClicked()
536 // purpose  : Radio button management
537 //=================================================================================
538 void SMESHGUI_SewingDlg::ConstructorsClicked (int constructorId)
539 {
540   disconnect(mySelectionMgr, 0, this, 0);
541   SALOME_ListIO io;
542   mySelectionMgr->selectedObjects( io );
543   mySelectionMgr->clearSelected();
544   LineEdit1->setText("");
545   LineEdit2->setText("");
546   LineEdit3->setText("");
547   LineEdit4->setText("");
548   LineEdit5->setText("");
549   LineEdit6->setText("");
550   myOk1 = myOk2 = myOk3 = myOk4 = myOk5 = myOk6 = false;
551   myEditCurrentArgument = LineEdit1;
552   myEditCurrentArgument->setFocus();
553
554   if (!TextLabel5->isEnabled()) {
555     TextLabel5->setEnabled(true);
556     SelectButton5->setEnabled(true);
557     LineEdit5->setEnabled(true);
558   } else if (!TextLabel6->isEnabled()) {
559     TextLabel6->setEnabled(true);
560     SelectButton6->setEnabled(true);
561     LineEdit6->setEnabled(true);
562   }
563
564   if (constructorId == 1 || constructorId == 3) {
565     if (CheckBoxPolygons->isVisible())
566       CheckBoxPolygons->hide();
567     if (CheckBoxPolyedrs->isVisible())
568       CheckBoxPolyedrs->hide();
569   }
570
571   if (( !SubGroup1->isVisible() ) &&
572       ( constructorId != 0 || ModeButGrp->checkedId() == MODE_MANUAL ))
573   {
574     SubGroup1->show();
575     SubGroup2->show();
576   }
577
578   if ( constructorId != 0 )
579   {
580     ModeGroup->hide();
581     SewFreeBordersWidget->hide();
582   }
583
584   bool isNodeSelection = true;
585
586   switch (constructorId) {
587   case 0:
588   {
589     GroupArguments->setTitle(tr("SEW_FREE_BORDERS"));
590     SubGroup1->setTitle(tr("BORDER_1"));
591     SubGroup2->setTitle(tr("BORDER_2"));
592
593     if (!CheckBoxPolygons->isVisible())
594       CheckBoxPolygons->show();
595     if (!CheckBoxPolyedrs->isVisible())
596       CheckBoxPolyedrs->show();
597
598     if ( !ModeGroup->isVisible() )
599     {
600       ModeGroup->show();
601     }
602     onModeChange( ModeButGrp->checkedId() );
603
604     isNodeSelection = ( ModeButGrp->checkedId() == MODE_MANUAL );
605
606     break;
607   }
608   case 1:
609   {
610     GroupArguments->setTitle(tr("SEW_CONFORM_FREE_BORDERS"));
611     SubGroup1->setTitle(tr("BORDER_1"));
612     SubGroup2->setTitle(tr("BORDER_2"));
613
614     TextLabel6->setEnabled(false);
615     SelectButton6->setEnabled(false);
616     LineEdit6->setEnabled(false);
617
618     myOk6 = true;
619
620     break;
621   }
622   case 2:
623   {
624     GroupArguments->setTitle(tr("SEW_BORDER_TO_SIDE"));
625     SubGroup1->setTitle(tr("BORDER"));
626     SubGroup2->setTitle(tr("SIDE"));
627
628     TextLabel5->setEnabled(false);
629     SelectButton5->setEnabled(false);
630     LineEdit5->setEnabled(false);
631
632     if (!CheckBoxPolygons->isVisible())
633       CheckBoxPolygons->show();
634     if (!CheckBoxPolyedrs->isVisible())
635       CheckBoxPolyedrs->show();
636
637     myOk5 = true;
638
639     break;
640   }
641   case 3:
642   {
643     GroupArguments->setTitle(tr("SEW_SIDE_ELEMENTS"));
644     SubGroup1->setTitle(tr("SIDE_1"));
645     SubGroup2->setTitle(tr("SIDE_2"));
646
647     TextLabel1->setText(tr("SMESH_ID_ELEMENTS"));
648     TextLabel2->setText(tr("NODE1_TO_MERGE"));
649     TextLabel3->setText(tr("NODE2_TO_MERGE"));
650     TextLabel4->setText(tr("SMESH_ID_ELEMENTS"));
651     TextLabel5->setText(tr("NODE1_TO_MERGE"));
652     TextLabel6->setText(tr("NODE2_TO_MERGE"));
653
654     LineEdit1->setValidator(new SMESHGUI_IdValidator(this));
655     LineEdit4->setValidator(new SMESHGUI_IdValidator(this));
656
657     isNodeSelection = false;
658     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
659       aViewWindow->SetSelectionMode(CellSelection);
660     break;
661   }
662   }
663
664   if (constructorId != 3) {
665     TextLabel1->setText(tr("FIRST_NODE_ID"));
666     TextLabel2->setText(tr("SECOND_NODE_ID"));
667     TextLabel3->setText(tr("LAST_NODE_ID"));
668     TextLabel4->setText(tr("FIRST_NODE_ID"));
669     TextLabel5->setText(tr("SECOND_NODE_ID"));
670     TextLabel6->setText(tr("LAST_NODE_ID"));
671
672     LineEdit1->setValidator(new SMESHGUI_IdValidator(this, 1));
673     LineEdit4->setValidator(new SMESHGUI_IdValidator(this, 1));
674   }
675
676   if ( isNodeSelection )
677   {
678     SMESH::SetPointRepresentation(true);
679     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
680       aViewWindow->SetSelectionMode(NodeSelection);
681   }
682
683   UpdateButtons();
684
685   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
686   mySelectionMgr->setSelectedObjects( io );
687
688   QApplication::instance()->processEvents();
689   updateGeometry();
690
691   resize(100,100);
692 }
693
694 //=======================================================================
695 //function : storeDisplayMode
696 //purpose  : save representation of a mesh and switch it to wireframe mode
697 //=======================================================================
698
699 void SMESHGUI_SewingDlg::setDisplayMode()
700 {
701   myStoredEntityMode = 0;
702   myStoredRepresentation = -1;
703   if ( myActor )
704   {
705     myStoredEntityMode     = myActor->GetEntityMode();
706     myStoredRepresentation = myActor->GetRepresentation();
707
708     myActor->SetEntityMode( myStoredEntityMode & ~SMESH_Actor::eVolumes );
709     myActor->SetRepresentation( SMESH_Actor::eEdge );
710   }
711 }
712
713 //=======================================================================
714 //function : restoreDisplayMode
715 //purpose  : restore representation of a mesh
716 //=======================================================================
717
718 void SMESHGUI_SewingDlg::restoreDisplayMode()
719 {
720   if ( myActor && myStoredEntityMode )
721   {
722     if ( myActor->GetEntityMode() == ( myStoredEntityMode & ~SMESH_Actor::eVolumes ))
723       myActor->SetEntityMode( myStoredEntityMode );
724
725     if ( myActor->GetRepresentation() == SMESH_Actor::eEdge )
726       myActor->SetRepresentation( myStoredRepresentation );
727
728     myStoredEntityMode = 0;
729     myStoredRepresentation = -1;
730   }
731 }
732
733 //=======================================================================
734 //function : onModeChange
735 //purpose  : SLOT called when mode (auto or manual) of Sew free borders change
736 //=======================================================================
737
738 void SMESHGUI_SewingDlg::onModeChange( int mode )
739 {
740   if ( mode == MODE_MANUAL )
741   {
742     myEditCurrentArgument = LineEdit1;
743     if ( !SubGroup1->isVisible() )
744       SubGroup1->show(), SubGroup2->show();
745     SewFreeBordersWidget->hide();
746   }
747   else
748   {
749     myEditCurrentArgument = LineEditMesh;
750     SubGroup1->hide(), SubGroup2->hide();
751     if ( !SewFreeBordersWidget->isVisible() )
752       SewFreeBordersWidget->show();
753   }
754   onAutoSew( AutoSewCheck->isChecked() );
755
756   QApplication::instance()->processEvents();
757   updateGeometry();
758
759   resize(100,100);
760 }
761
762 //=======================================================================
763 //function : onAutoSew
764 //purpose  : SLOT called when Auto Sewing check box is checked
765 //=======================================================================
766
767 void SMESHGUI_SewingDlg::onAutoSew( int isAuto )
768 {
769   GroupCoincidentWidget->setVisible( !isAuto );
770
771   QApplication::instance()->processEvents();
772
773   SewFreeBordersWidget->hide();
774   if ( ModeButGrp->checkedId() == MODE_AUTO )
775     SewFreeBordersWidget->show();
776
777   UpdateButtons();
778
779   updateGeometry();
780   resize(minimumSizeHint());
781 }
782
783 //=======================================================================
784 //function : haveBorders
785 //purpose  : Returns true if myBorders have been initialized
786 //=======================================================================
787
788 bool SMESHGUI_SewingDlg::haveBorders()
789 {
790   return ( & myBorders.in() &&
791            myBorders->borders.length() &&
792            myBorders->coincidentGroups.length() );
793 }
794
795 //=======================================================================
796 //function : getGroupText
797 //purpose  : Returns a text of a given group of coincident free borders
798 //=======================================================================
799
800 QString SMESHGUI_SewingDlg::getPartText(const SMESH::FreeBorderPart& aPART)
801 {
802   QString text;
803   if ( 0 <= aPART.border && aPART.border < myBorders->borders.length() )
804   {
805     const SMESH::FreeBorder& aBRD = myBorders->borders[ aPART.border ];
806     if ( 0 <= aPART.node1    && aPART.node1 < aBRD.nodeIDs.length() &&
807          0 <= aPART.nodeLast && aPART.nodeLast < aBRD.nodeIDs.length() )
808     {
809       text += QString("( %1 %2 ) ")
810         .arg( aBRD.nodeIDs[ aPART.node1 ] )
811         .arg( aBRD.nodeIDs[ aPART.nodeLast ] );
812     }
813   }
814   return text;
815 }
816
817 //=======================================================================
818 //function : getGroupText
819 //purpose  : Returns a text of a given group of coincident free borders
820 //=======================================================================
821
822 QString SMESHGUI_SewingDlg::getGroupText(int groupIndex)
823 {
824   QString text;
825
826   if ( haveBorders()   &&
827        groupIndex >= 0 &&
828        groupIndex < myBorders->coincidentGroups.length() )
829   {
830     const SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ groupIndex ];
831
832     for ( CORBA::ULong iP = 0; iP < aGRP.length(); ++iP )
833     {
834       QString partText = getPartText( aGRP[ iP ]);
835       if ( partText.isEmpty() )
836         return "";
837       text += partText;
838     }
839   }
840   return text;
841 }
842
843 //=======================================================================
844 //function : onDetectClicked
845 //purpose  : SLOT called when [Detect] is clicked
846 //=======================================================================
847
848 void SMESHGUI_SewingDlg::onDetectClicked()
849 {
850   ListCoincident->clear();
851
852   if ( myMesh->_is_nil() )
853     return;
854
855   SUIT_OverrideCursor wc;
856
857   SMESH::SMESH_MeshEditor_var editor = myMesh->GetMeshEditor();
858   myBorders = editor->FindCoincidentFreeBorders( SpinBoxTolerance->GetValue() );
859   if ( !haveBorders() )
860     return;
861
862   for ( size_t i = 0; i < myBorderDisplayers.size(); ++i )
863   {
864     delete myBorderDisplayers[ i ];
865     myBorderDisplayers[ i ] = 0;
866   }
867   myBorderDisplayers.resize( myBorders->coincidentGroups.length(), 0 );
868
869   for ( CORBA::ULong i = 0; i < myBorders->coincidentGroups.length(); ++i )
870   {
871     QString groupText = getGroupText( i );
872     if ( groupText.isEmpty() )
873       continue;
874
875     QColor groupColor;
876     groupColor.setHsvF( float(i) / myBorders->coincidentGroups.length(), 1., 1. );
877     QPixmap icon( QSize( 20, 20 ));
878     icon.fill( groupColor );
879
880     QListWidgetItem * item = new QListWidgetItem( icon, groupText, ListCoincident );
881     item->setData( GROUP_COLOR, groupColor );
882     item->setData( GROUP_INDEX, i );
883   }
884
885   onSelectGroup();
886
887   UpdateButtons();
888 }
889
890 //=======================================================================
891 //function : onRemoveGroupClicked
892 //purpose  :
893 //=======================================================================
894
895 void SMESHGUI_SewingDlg::onRemoveGroupClicked()
896 {
897   QList<QListWidgetItem*> selItems = ListCoincident->selectedItems();
898   for ( int i = 0; i < selItems.count(); ++i )
899   {
900     QListWidgetItem* item = selItems[ i ];
901     item->setSelected( false );
902     int groupIndex = item->data( GROUP_INDEX ).toInt();
903     delete item;
904     myBorderDisplayers[ groupIndex ]->Hide();
905     SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
906     aGRP.length( 0 );
907   }
908   UpdateButtons();
909 }
910
911 //=======================================================================
912 //function : showGroup
913 //purpose  : display a group of coincident free borders in the Viewer
914 //=======================================================================
915
916 void SMESHGUI_SewingDlg::showGroup( QListWidgetItem* item )
917 {
918   if ( !item ||
919        item->listWidget() != ListCoincident ||
920        !haveBorders() )
921     return;
922
923   int    groupIndex = item->data( GROUP_INDEX ).toInt();
924   QColor groupColor = item->data( GROUP_COLOR ).value<QColor>();
925   if ( groupIndex >= 0       &&
926        groupIndex < myBorders->coincidentGroups.length() )
927   {
928     if ( !myBorderDisplayers[ groupIndex ])
929       myBorderDisplayers[ groupIndex ] = new BorderGroupDisplayer( myBorders, groupIndex, groupColor, myMesh );
930     bool wholeBorders = setCurrentGroup();
931     myBorderDisplayers[ groupIndex ]->ShowGroup( wholeBorders );
932   }
933 }
934
935 //=======================================================================
936 //function : setCurrentGroup
937 //purpose  : set index of a current free border group to myCurGroupIndex
938 //=======================================================================
939
940 bool SMESHGUI_SewingDlg::setCurrentGroup()
941 {
942   if ( !haveBorders() )
943     return false;
944
945   QList<QListWidgetItem*> selItems = ListCoincident->selectedItems();
946   if ( selItems.count() != 1 )
947     return false;
948   
949   myCurGroupIndex = selItems[0]->data( GROUP_INDEX ).toInt();
950
951   return ( myCurGroupIndex >= 0 && myCurGroupIndex < myBorders->coincidentGroups.length() );
952 }
953
954 //=======================================================================
955 //function : setCurrentPart
956 //purpose  : set index of a current free border of a current group to myCurPartIndex
957 //=======================================================================
958
959 bool SMESHGUI_SewingDlg::setCurrentPart()
960 {
961   if ( !setCurrentGroup() )
962     return false;
963
964   if ( ListEdit->selectedItems().count() != 1 )
965     return false;
966
967   myCurPartIndex = ListEdit->currentRow();
968   const SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
969
970   return ( myCurPartIndex >= 0 && myCurPartIndex < aGRP.length() );
971 }
972
973 //=======================================================================
974 //function : onSelectGroup
975 //purpose  : SLOT called when selection of coincident free borders change
976 //=======================================================================
977
978 void SMESHGUI_SewingDlg::onSelectGroup()
979 {
980   if ( myBusy )
981     return;
982   ListEdit->clear();
983   BorderEndLine[0]->clear();
984   BorderEndLine[1]->clear();
985   for ( size_t i = 0; i < myBorderDisplayers.size(); ++i )
986     if ( myBorderDisplayers[ i ])
987       myBorderDisplayers[ i ]->Hide();
988
989   QList<QListWidgetItem*> selItems = ListCoincident->selectedItems();
990
991   RemoveGroupButton->setEnabled( selItems.count() > 0 );
992
993   onSelectBorderPartFromGroup(); // enable buttons
994
995   if ( !haveBorders() )
996     return;
997
998   SelectAllCheck->blockSignals( true );
999   if ( ListCoincident->count() != selItems.count() )
1000     SelectAllCheck->setChecked( false );
1001   SelectAllCheck->blockSignals( false );
1002
1003   if ( selItems.empty() ) // nothing selected - show all
1004     for ( int i = 0; i < ListCoincident->count(); ++i )
1005       showGroup( ListCoincident->item( i ));
1006   else
1007     for ( int i = 0; i < selItems.count(); ++i )
1008       showGroup( selItems[ i ]);
1009
1010   if ( setCurrentGroup() ) // edit a selected group
1011   {
1012     const SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
1013     for ( CORBA::ULong iP = 0; iP < aGRP.length(); ++iP )
1014       new QListWidgetItem( getPartText( aGRP[ iP ]), ListEdit );
1015   }
1016   SMESH::RepaintCurrentView();
1017 }
1018
1019 //=======================================================================
1020 //function : onSelectAll
1021 //purpose  : SLOT called when Select All is checked
1022 //=======================================================================
1023
1024 void SMESHGUI_SewingDlg::onSelectAll(int isOn)
1025 {
1026   if ( isOn )
1027     ListCoincident->selectAll();
1028   else
1029     ListCoincident->clearSelection();
1030 }
1031
1032 //=======================================================================
1033 //function : onSelectBorderPartFromGroup
1034 //purpose  : SLOT called when selection of borders in an edited group changes
1035 //=======================================================================
1036
1037 void SMESHGUI_SewingDlg::onSelectBorderPartFromGroup()
1038 {
1039   if ( myBusy ) return;
1040   BorderEndLine[0]->setText("");
1041   BorderEndLine[1]->setText("");
1042   MoveBorderEndsButGrp->button( MOVE_LEFT_1  )->setEnabled( false );
1043   MoveBorderEndsButGrp->button( MOVE_RIGHT_1 )->setEnabled( false );
1044   MoveBorderEndsButGrp->button( MOVE_LEFT_2  )->setEnabled( false );
1045   MoveBorderEndsButGrp->button( MOVE_RIGHT_2 )->setEnabled( false );
1046   SwapBut->setEnabled( false );
1047   SetFirstButton->setEnabled( false );
1048   RemoveElemButton->setEnabled ( ListEdit->count() > 2 );
1049
1050   if ( !setCurrentGroup() )
1051     return;
1052
1053   if ( !myBorderDisplayers[ myCurGroupIndex ]) return;
1054   myBorderDisplayers[ myCurGroupIndex ]->Hide();
1055
1056   QList<QListWidgetItem*> selItems = ListEdit->selectedItems();
1057   bool editPart = ( setCurrentPart() );
1058   for ( int i = 0; i < selItems.count(); ++i )
1059     myBorderDisplayers[ myCurGroupIndex ]->ShowPart( ListEdit->row( selItems[i] ), editPart );
1060
1061   if ( selItems.isEmpty() )
1062     myBorderDisplayers[ myCurGroupIndex ]->ShowGroup( /*wholeBorders=*/ true );
1063
1064   if ( editPart )
1065   {
1066     SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
1067     SMESH::FreeBorderPart&   aPRT = aGRP[ myCurPartIndex ];
1068     SMESH::FreeBorder&       aBRD = myBorders->borders[ aPRT.border ];
1069
1070     BorderEndLine[0]->setText( QString::number( aBRD.nodeIDs[ aPRT.node1 ]));
1071     BorderEndLine[1]->setText( QString::number( aBRD.nodeIDs[ aPRT.nodeLast ]));
1072     SwapBut->setEnabled( true );
1073     SetFirstButton->setEnabled( myCurPartIndex > 0 );
1074
1075     int      size = (int) aBRD.nodeIDs.length();
1076     bool isClosed = ( aBRD.nodeIDs[0] == aBRD.nodeIDs[ size-1 ]);
1077     if ( !isClosed )
1078     {
1079       bool isFwd = ( Abs( aPRT.node2 - aPRT.node1 ) == 1 ) ? aPRT.node2 > aPRT.node1 : aPRT.node2 < aPRT.node1;
1080       int dn     = ( isFwd ? +1 : -1 ) * StepSpin->value();
1081       MoveBorderEndsButGrp->button( MOVE_LEFT_1  )->
1082         setEnabled( 0 <= aPRT.node1-dn && aPRT.node1-dn < size );
1083       MoveBorderEndsButGrp->button( MOVE_RIGHT_1 )->
1084         setEnabled( 0 <= aPRT.node1+dn && aPRT.node1+dn < size );
1085       MoveBorderEndsButGrp->button( MOVE_LEFT_2  )->
1086         setEnabled( 0 <= aPRT.nodeLast-dn && aPRT.nodeLast-dn < size );
1087       MoveBorderEndsButGrp->button( MOVE_RIGHT_2  )->
1088         setEnabled( 0 <= aPRT.nodeLast+dn && aPRT.nodeLast+dn < size );
1089     }
1090     else
1091     {
1092       MoveBorderEndsButGrp->button( MOVE_LEFT_1  )->setEnabled( true );
1093       MoveBorderEndsButGrp->button( MOVE_RIGHT_1 )->setEnabled( true );
1094       MoveBorderEndsButGrp->button( MOVE_LEFT_2  )->setEnabled( true );
1095       MoveBorderEndsButGrp->button( MOVE_RIGHT_2 )->setEnabled( true );
1096     }
1097   }
1098   SMESH::RepaintCurrentView();
1099 }
1100
1101 //=======================================================================
1102 //function : onGroupChange
1103 //purpose  : Update after modification of a current group by the user
1104 //=======================================================================
1105
1106 void SMESHGUI_SewingDlg::onGroupChange( bool partChange )
1107 {
1108   ListCoincident->currentItem()->setText( getGroupText( myCurGroupIndex ));
1109
1110   const SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
1111   for ( int i = 0; i < ListEdit->count(); ++i )
1112     ListEdit->item( i )->setText( getPartText( aGRP[ i ]));
1113
1114   myBorderDisplayers[ myCurGroupIndex ]->Update();
1115
1116   if ( partChange )
1117     onSelectBorderPartFromGroup();
1118 }
1119
1120 //=======================================================================
1121 //function : onSetFirstClicked
1122 //purpose  : STOL called when |<< is clicked
1123 //=======================================================================
1124
1125 void SMESHGUI_SewingDlg::onSetFirstClicked()
1126 {
1127   if ( !setCurrentPart() || myCurPartIndex == 0 || ListEdit->count() == 0 )
1128     return;
1129
1130   SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
1131
1132   SMESH::FreeBorderPart new1st = aGRP[ myCurPartIndex ];
1133   for ( ; myCurPartIndex > 0; --myCurPartIndex )
1134     aGRP[ myCurPartIndex ] = aGRP[ myCurPartIndex - 1 ];
1135
1136   aGRP[ 0 ] = new1st;
1137
1138   onGroupChange();
1139
1140   myBusy = true;
1141   ListEdit->clearSelection();
1142   myBusy = false;
1143   ListEdit->setCurrentItem( ListEdit->item(0) );//ListEdit->item(0)->setSelected(true);
1144 }
1145
1146 //=======================================================================
1147 //function : onRemoveElemClicked
1148 //purpose  : 
1149 //=======================================================================
1150
1151 void SMESHGUI_SewingDlg::onRemoveElemClicked()
1152 {
1153   if ( !setCurrentGroup() )
1154     return;
1155
1156   SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
1157
1158   QList<QListWidgetItem*> selItems = ListEdit->selectedItems();
1159   for ( int i = 0; i < selItems.count(); ++i )
1160   {
1161     int part = ListEdit->row( selItems[i] );
1162     for ( ; part + 1 < aGRP.length(); ++part )
1163       aGRP[ part ] = aGRP[ part + 1 ];
1164     aGRP.length( aGRP.length() - 1 );
1165     delete selItems[i];
1166   }
1167
1168   if ( aGRP.length() == 0 )
1169     onRemoveGroupClicked();
1170   else
1171     onGroupChange( /*partChange=*/true );
1172 }
1173
1174 //=======================================================================
1175 //function : onMoveBorderEnd
1176 //purpose  : 
1177 //=======================================================================
1178
1179 void SMESHGUI_SewingDlg::onMoveBorderEnd(int button)
1180 {
1181   if ( !setCurrentPart() )
1182     return;
1183
1184   SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
1185   SMESH::FreeBorderPart&   aPRT = aGRP[ myCurPartIndex ];
1186   SMESH::FreeBorder&       aBRD = myBorders->borders[ aPRT.border ];
1187   int size = (int) aBRD.nodeIDs.length();
1188
1189   bool isClosed = ( aBRD.nodeIDs[0] == aBRD.nodeIDs[ size-1 ]);
1190   if ( isClosed ) --size;
1191
1192   bool isFwd = ( Abs( aPRT.node2 - aPRT.node1 ) == 1 ) ? aPRT.node2 > aPRT.node1 : aPRT.node2 < aPRT.node1;
1193   int dn     = ( isFwd ? +1 : -1 ) * StepSpin->value();
1194   if ( button == MOVE_LEFT_1 || button == MOVE_LEFT_2 )
1195     dn *= -1;
1196
1197   switch ( button ) {
1198   case MOVE_LEFT_1:
1199   case MOVE_RIGHT_1:
1200     if (( isClosed ) ||
1201         ( 0 <= aPRT.node1+dn && aPRT.node1+dn < size ))
1202     {
1203       aPRT.node1 = ( aPRT.node1 + size + dn ) % size;
1204       aPRT.node2 = ( aPRT.node2 + size + dn ) % size;
1205       break;
1206     }
1207   case MOVE_LEFT_2:
1208   case MOVE_RIGHT_2:
1209     if (( isClosed ) ||
1210         ( 0 <= aPRT.nodeLast+dn && aPRT.nodeLast+dn < size ))
1211     {
1212       aPRT.nodeLast = ( aPRT.nodeLast + size + dn ) % size;
1213       break;
1214     }
1215   default:
1216     return; // impossible to move
1217   }
1218   
1219   onGroupChange( /*partChange=*/true );
1220 }
1221
1222 //=======================================================================
1223 //function : onSwapClicked
1224 //purpose  : SLOT called when <-> is clicked
1225 //=======================================================================
1226
1227 void SMESHGUI_SewingDlg::onSwapClicked()
1228 {
1229   if ( !setCurrentPart() )
1230     return;
1231
1232   SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
1233   SMESH::FreeBorderPart&   aPRT = aGRP[ myCurPartIndex ];
1234   SMESH::FreeBorder&       aBRD = myBorders->borders[ aPRT.border ];
1235   int size = (int) aBRD.nodeIDs.length();
1236
1237   bool isClosed = ( aBRD.nodeIDs[0] == aBRD.nodeIDs[ size-1 ]);
1238   if ( isClosed ) --size;
1239
1240   bool isFwd = ( Abs( aPRT.node2 - aPRT.node1 ) == 1 ) ? aPRT.node2 > aPRT.node1 : aPRT.node2 < aPRT.node1;
1241
1242   std::swap( aPRT.nodeLast, aPRT.node1 );
1243
1244   aPRT.node2 = ( aPRT.node1 + ( isFwd ? -1 : +1 ) + size ) % size;
1245
1246   onGroupChange( /*partChange=*/true );
1247 }
1248
1249 //=================================================================================
1250 // function : ClickOnApply()
1251 // purpose  :
1252 //=================================================================================
1253 bool SMESHGUI_SewingDlg::ClickOnApply()
1254 {
1255   if (mySMESHGUI->isActiveStudyLocked())
1256     return false;
1257
1258   bool aResult = false;
1259
1260   if (IsValid())
1261   {
1262     bool toMerge          = CheckBoxMerge->isChecked();
1263     bool toCreatePolygons = CheckBoxPolygons->isChecked();
1264     bool toCreatePolyedrs = CheckBoxPolyedrs->isChecked();
1265     try {
1266       SUIT_OverrideCursor aWaitCursor;
1267       SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
1268
1269       int aConstructorId = GetConstructorId();
1270       SMESH::SMESH_MeshEditor::Sew_Error anError;
1271
1272       if (aConstructorId == 0)
1273       {
1274         if ( ModeButGrp->checkedId() == MODE_MANUAL )
1275         {
1276           anError = aMeshEditor->SewFreeBorders(LineEdit1->text().toLong(),
1277                                                 LineEdit2->text().toLong(),
1278                                                 LineEdit3->text().toLong(),
1279                                                 LineEdit4->text().toLong(),
1280                                                 LineEdit5->text().toLong(),
1281                                                 LineEdit6->text().toLong(),
1282                                                 toCreatePolygons,
1283                                                 toCreatePolyedrs);
1284         }
1285         else
1286         {
1287           int nbCoincGroups = ListCoincident->count();
1288           if ( AutoSewCheck->isChecked() )
1289           {
1290             myBorders     = aMeshEditor->FindCoincidentFreeBorders( SpinBoxTolerance->GetValue() );
1291             nbCoincGroups = myBorders->coincidentGroups.length();
1292           }
1293           CORBA::Short nbSewed = aMeshEditor->SewCoincidentFreeBorders( myBorders.inout(),
1294                                                                         toCreatePolygons,
1295                                                                         toCreatePolyedrs);
1296           QString msg;
1297           if ( nbCoincGroups == 0 )
1298             msg = tr("NO_BORDERS_TO_SEW");
1299           else if ( nbSewed < nbCoincGroups )
1300             msg = tr("NOT_ALL_BORDERS_SEWED").arg( nbSewed ).arg( nbCoincGroups );
1301           else
1302             msg = tr("ALL_BORDERS_SEWED").arg( nbSewed );
1303           SUIT_MessageBox::information( this, tr("SMESH_INFORMATION"), msg );
1304
1305           anError = SMESH::SMESH_MeshEditor::SEW_OK;
1306         }
1307       }
1308       else if (aConstructorId == 1)
1309       {
1310         anError = aMeshEditor->SewConformFreeBorders(LineEdit1->text().toLong(),
1311                                                      LineEdit2->text().toLong(),
1312                                                      LineEdit3->text().toLong(),
1313                                                      LineEdit4->text().toLong(),
1314                                                      LineEdit5->text().toLong());
1315       }
1316       else if (aConstructorId == 2)
1317       {
1318         anError = aMeshEditor->SewBorderToSide(LineEdit1->text().toLong(),
1319                                                LineEdit2->text().toLong(),
1320                                                LineEdit3->text().toLong(),
1321                                                LineEdit4->text().toLong(),
1322                                                LineEdit6->text().toLong(),
1323                                                toCreatePolygons,
1324                                                toCreatePolyedrs);
1325       }
1326       else if (aConstructorId == 3) {
1327         QStringList aListElementsId1 = LineEdit1->text().split(" ", QString::SkipEmptyParts);
1328         QStringList aListElementsId2 = LineEdit4->text().split(" ", QString::SkipEmptyParts);
1329
1330         SMESH::long_array_var anElementsId1 = new SMESH::long_array;
1331         SMESH::long_array_var anElementsId2 = new SMESH::long_array;
1332
1333         anElementsId1->length(aListElementsId1.count());
1334         anElementsId2->length(aListElementsId2.count());
1335
1336         for (int i = 0; i < aListElementsId1.count(); i++)
1337           anElementsId1[i] = aListElementsId1[i].toInt();
1338         for (int i = 0; i < aListElementsId2.count(); i++)
1339           anElementsId2[i] = aListElementsId2[i].toInt();
1340
1341         anError = aMeshEditor->SewSideElements(anElementsId1.inout(),
1342                                                anElementsId2.inout(),
1343                                                LineEdit2->text().toLong(),
1344                                                LineEdit5->text().toLong(),
1345                                                LineEdit3->text().toLong(),
1346                                                LineEdit6->text().toLong());
1347       }
1348       aResult = (anError == SMESH::SMESH_MeshEditor::SEW_OK);
1349
1350       if (toMerge && aResult)
1351         aMeshEditor->MergeEqualElements();
1352
1353       if (!aResult) {
1354         QString msg = tr(QString("ERROR_%1").arg(anError).toLatin1().data());
1355         SUIT_MessageBox::warning(this, tr("SMESH_WRN_WARNING"), msg);
1356       }
1357     } catch (...) {
1358     }
1359
1360     if (aResult) {
1361
1362       if ( myActor )
1363       {
1364         Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
1365         SALOME_ListIO aList;
1366         aList.Append(anIO);
1367         mySelectionMgr->setSelectedObjects(aList, false);
1368         SMESH::UpdateView();
1369       }
1370       Init();
1371       ConstructorsClicked(GetConstructorId());
1372
1373       SMESHGUI::Modified();
1374     }
1375   }
1376
1377   return aResult;
1378 }
1379
1380 //=================================================================================
1381 // function : ClickOnOk()
1382 // purpose  :
1383 //=================================================================================
1384 void SMESHGUI_SewingDlg::ClickOnOk()
1385 {
1386   if (ClickOnApply())
1387     reject();
1388 }
1389
1390 //=================================================================================
1391 // function : onOpenView()
1392 // purpose  :
1393 //=================================================================================
1394 void SMESHGUI_SewingDlg::onOpenView()
1395 {
1396   if ( mySelector ) {
1397     SMESH::SetPointRepresentation(false);
1398   }
1399   else {
1400     mySelector = SMESH::GetViewWindow( mySMESHGUI )->GetSelector();
1401     ActivateThisDialog();
1402   }
1403 }
1404
1405 //=================================================================================
1406 // function : onCloseView()
1407 // purpose  :
1408 //=================================================================================
1409 void SMESHGUI_SewingDlg::onCloseView()
1410 {
1411   DeactivateActiveDialog();
1412   mySelector = 0;
1413
1414   for ( size_t i = 0; i < myBorderDisplayers.size(); ++i )
1415   {
1416     delete myBorderDisplayers[ i ];
1417     myBorderDisplayers[ i ] = 0;
1418   }
1419   myBorderDisplayers.clear();
1420 }
1421
1422 //=================================================================================
1423 // function : reject()
1424 // purpose  :
1425 //=================================================================================
1426 void SMESHGUI_SewingDlg::reject()
1427 {
1428   restoreDisplayMode();
1429   //mySelectionMgr->clearSelected();
1430   SMESH::SetPointRepresentation(false);
1431   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1432     aViewWindow->SetSelectionMode(ActorSelection);
1433   disconnect(mySelectionMgr, 0, this, 0);
1434   mySMESHGUI->ResetState();
1435   QDialog::reject();
1436 }
1437
1438 //=================================================================================
1439 // function : ClickOnHelp()
1440 // purpose  :
1441 //=================================================================================
1442 void SMESHGUI_SewingDlg::ClickOnHelp()
1443 {
1444   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
1445   if (app) 
1446     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
1447   else {
1448     QString platform;
1449 #ifdef WIN32
1450     platform = "winapplication";
1451 #else
1452     platform = "application";
1453 #endif
1454     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
1455                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
1456                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
1457                                                                  platform)).
1458                              arg(myHelpFileName));
1459   }
1460 }
1461
1462 //=======================================================================
1463 //function : onTextChange
1464 //purpose  :
1465 //=======================================================================
1466 void SMESHGUI_SewingDlg::onTextChange (const QString& theNewText)
1467 {
1468   QLineEdit* send = (QLineEdit*)sender();
1469
1470   if (myBusy) return;
1471   myBusy = true;
1472
1473   if (send)
1474     myEditCurrentArgument = send;
1475
1476   if      (send == LineEdit1)
1477     myOk1 = false;
1478   else if (send == LineEdit2)
1479     myOk2 = false;
1480   else if (send == LineEdit3)
1481     myOk3 = false;
1482   else if (send == LineEdit4)
1483     myOk4 = false;
1484   else if (send == LineEdit5)
1485     myOk5 = false;
1486   else if (send == LineEdit6)
1487     myOk6 = false;
1488
1489   // hilight entered elements/nodes
1490   SMDS_Mesh* aMesh = 0;
1491
1492   if (myActor)
1493     aMesh = myActor->GetObject()->GetMesh();
1494   else
1495     send->clear();
1496
1497   if (aMesh) {
1498     TColStd_MapOfInteger newIndices;
1499     
1500     if (GetConstructorId() != 3 || (send != LineEdit1 && send != LineEdit4)) {
1501       SMESH::SetPointRepresentation(true);
1502
1503       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1504         aViewWindow->SetSelectionMode(NodeSelection);
1505
1506       const SMDS_MeshNode * n = aMesh->FindNode(theNewText.toInt());
1507       if (n) {
1508         newIndices.Add(n->GetID());
1509         mySelector->AddOrRemoveIndex(myActor->getIO(), newIndices, false);
1510         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1511           aViewWindow->highlight( myActor->getIO(), true, true );
1512         
1513         if      (send == LineEdit1)
1514           myOk1 = true;
1515         else if (send == LineEdit2)
1516           myOk2 = true;
1517         else if (send == LineEdit3)
1518           myOk3 = true;
1519         else if (send == LineEdit4)
1520           myOk4 = true;
1521         else if (send == LineEdit5)
1522           myOk5 = true;
1523         else if (send == LineEdit6)
1524           myOk6 = true;
1525       }
1526     } else {
1527       SMESH::SetPointRepresentation(false);
1528
1529       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1530         aViewWindow->SetSelectionMode(CellSelection);
1531
1532       QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
1533
1534       bool isEvenOneExists = false;
1535
1536       for (int i = 0; i < aListId.count(); i++) {
1537         const SMDS_MeshElement * e = aMesh->FindElement(aListId[ i ].toInt());
1538         if (e) 
1539           newIndices.Add(e->GetID());
1540         
1541           if (!isEvenOneExists)
1542             isEvenOneExists = true;
1543       }
1544       
1545       mySelector->AddOrRemoveIndex(myActor->getIO(), newIndices, false);
1546       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1547         aViewWindow->highlight( myActor->getIO(), true, true );
1548       
1549       if (isEvenOneExists) {
1550         if (send == LineEdit1)
1551           myOk1 = true;
1552         else if(send == LineEdit4)
1553           myOk4 = true;
1554       } else {
1555         send->clear();
1556       }
1557     }
1558   }
1559
1560   UpdateButtons();
1561
1562   myBusy = false;
1563 }
1564
1565 //=================================================================================
1566 // function : SelectionIntoArgument()
1567 // purpose  : Called when selection as changed or other case
1568 //=================================================================================
1569 void SMESHGUI_SewingDlg::SelectionIntoArgument (bool isSelectionChanged)
1570 {
1571   if (myBusy) return;
1572
1573   // clear
1574   restoreDisplayMode();
1575   if (isSelectionChanged)
1576     myActor = 0;
1577
1578   QString aString = "";
1579
1580   myBusy = true;
1581   myEditCurrentArgument->setText(aString);
1582   ListCoincident->clear();
1583   ListEdit->clear();
1584   myBusy = false;
1585
1586   onSelectGroup(); // erase preview
1587
1588   if (!GroupButtons->isEnabled()) // inactive
1589     return;
1590
1591   buttonOk->setEnabled(false);
1592   buttonApply->setEnabled(false);
1593   DetectButton->setEnabled(false);
1594
1595   // get selected mesh
1596   SALOME_ListIO aList;
1597   mySelectionMgr->selectedObjects(aList);
1598
1599   int nbSel = aList.Extent();
1600   if (nbSel != 1)
1601     return;
1602
1603   Handle(SALOME_InteractiveObject) IO = aList.First();
1604   myMesh  = SMESH::GetMeshByIO(IO);
1605   myActor = SMESH::FindActorByEntry(aList.First()->getEntry());
1606   if (myMesh->_is_nil())
1607     return;
1608
1609   CheckBoxPolyedrs->setEnabled( myMesh->NbVolumes() > 0 );
1610
1611   if ( myEditCurrentArgument == LineEditMesh )
1612   {
1613     LineEditMesh->setText( IO->getName() );
1614     ListCoincident->clear();
1615     if ( AutoSewCheck->isChecked() )
1616     {
1617       buttonOk->setEnabled(true);
1618       buttonApply->setEnabled(true);
1619     }
1620     DetectButton->setEnabled( myMesh->NbFaces() > 0 );
1621     setDisplayMode();
1622     return;
1623   }
1624
1625   if (!myActor)
1626     return;
1627
1628   // get selected elements/nodes
1629   int aNbUnits = 0;
1630   if (( GetConstructorId() != 3 ) ||
1631       ( myEditCurrentArgument != LineEdit1 && myEditCurrentArgument != LineEdit4))
1632   {
1633     aNbUnits = SMESH::GetNameOfSelectedNodes(mySelector, IO, aString);
1634     if (aNbUnits != 1)
1635       return;
1636   }
1637   else {
1638     aNbUnits = SMESH::GetNameOfSelectedElements(mySelector, IO, aString);
1639     if (aNbUnits < 1)
1640       return;
1641   }
1642
1643   myBusy = true;
1644   myEditCurrentArgument->setText(aString);
1645   myBusy = false;
1646
1647   // OK
1648   if (myEditCurrentArgument == LineEdit1)
1649     myOk1 = true;
1650   else if (myEditCurrentArgument == LineEdit2)
1651     myOk2 = true;
1652   else if (myEditCurrentArgument == LineEdit3)
1653     myOk3 = true;
1654   else if (myEditCurrentArgument == LineEdit4)
1655     myOk4 = true;
1656   else if (myEditCurrentArgument == LineEdit5)
1657     myOk5 = true;
1658   else if (myEditCurrentArgument == LineEdit6)
1659     myOk6 = true;
1660
1661   UpdateButtons();
1662 }
1663
1664 //=================================================================================
1665 // function : SetEditCurrentArgument()
1666 // purpose  :
1667 //=================================================================================
1668 void SMESHGUI_SewingDlg::SetEditCurrentArgument()
1669 {
1670   QPushButton* send = (QPushButton*)sender();
1671
1672   disconnect(mySelectionMgr, 0, this, 0);
1673   mySelectionMgr->clearSelected();
1674
1675   if (send == SelectButton1) {
1676     myEditCurrentArgument = LineEdit1;
1677     myOk1 = false;
1678   }
1679   else if (send == SelectButton2) {
1680     myEditCurrentArgument = LineEdit2;
1681     myOk2 = false;
1682   }
1683   else if (send == SelectButton3) {
1684     myEditCurrentArgument = LineEdit3;
1685     myOk3 = false;
1686   }
1687   else if (send == SelectButton4) {
1688     myEditCurrentArgument = LineEdit4;
1689     myOk4 = false;
1690   }
1691   else if (send == SelectButton5) {
1692     myEditCurrentArgument = LineEdit5;
1693     myOk5 = false;
1694   }
1695   else if (send == SelectButton6) {
1696     myEditCurrentArgument = LineEdit6;
1697     myOk6 = false;
1698   }
1699
1700   if (GetConstructorId() != 3 || (send != SelectButton1 && send != SelectButton4)) {
1701     SMESH::SetPointRepresentation(true);
1702
1703     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1704       aViewWindow->SetSelectionMode(NodeSelection);
1705
1706   } else {
1707     SMESH::SetPointRepresentation(false);
1708     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1709       aViewWindow->SetSelectionMode(CellSelection);
1710   }
1711
1712   myEditCurrentArgument->setFocus();
1713   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
1714   SelectionIntoArgument(false);
1715 }
1716
1717 //=================================================================================
1718 // function : DeactivateActiveDialog()
1719 // purpose  :
1720 //=================================================================================
1721 void SMESHGUI_SewingDlg::DeactivateActiveDialog()
1722 {
1723   if (ConstructorsBox->isEnabled()) {
1724     ConstructorsBox->setEnabled(false);
1725     GroupArguments->setEnabled(false);
1726     GroupButtons->setEnabled(false);
1727     mySMESHGUI->ResetState();
1728     mySMESHGUI->SetActiveDialogBox(0);
1729   }
1730 }
1731
1732 //=================================================================================
1733 // function : ActivateThisDialog()
1734 // purpose  :
1735 //=================================================================================
1736 void SMESHGUI_SewingDlg::ActivateThisDialog()
1737 {
1738   /* Emit a signal to deactivate the active dialog */
1739   mySMESHGUI->EmitSignalDeactivateDialog();
1740   ConstructorsBox->setEnabled(true);
1741   GroupArguments->setEnabled(true);
1742   GroupButtons->setEnabled(true);
1743
1744   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
1745
1746   ConstructorsClicked(GetConstructorId());
1747   SelectionIntoArgument();
1748 }
1749
1750 //=================================================================================
1751 // function : enterEvent()
1752 // purpose  :
1753 //=================================================================================
1754 void SMESHGUI_SewingDlg::enterEvent (QEvent* e)
1755 {
1756   if (!ConstructorsBox->isEnabled()) {
1757     SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
1758     if ( aViewWindow && !mySelector) {
1759       mySelector = aViewWindow->GetSelector();
1760     }
1761     ActivateThisDialog();
1762   }
1763 }
1764
1765 //=================================================================================
1766 // function : GetConstructorId()
1767 // purpose  :
1768 //=================================================================================
1769 int SMESHGUI_SewingDlg::GetConstructorId()
1770 {
1771   return GroupConstructors->checkedId();
1772 }
1773
1774 //=================================================================================
1775 // function : GetConstructorId()
1776 // purpose  :
1777 //=================================================================================
1778 bool SMESHGUI_SewingDlg::IsValid()
1779 {
1780   if ( myMesh->_is_nil() )
1781     return false;
1782
1783   if ( GetConstructorId() == 0 && ModeButGrp->checkedId() == MODE_AUTO )
1784   {
1785     if ( AutoSewCheck->isChecked() )
1786       return true;
1787
1788     int nbGroups = 0;
1789     if ( haveBorders() )
1790       for ( int i = 0; i < ListCoincident->count(); ++i )
1791       {
1792         int groupIndex = ListCoincident->item(i)->data( GROUP_INDEX ).toInt();
1793         nbGroups += ( !getGroupText( groupIndex ).isEmpty() );
1794       }
1795     return nbGroups > 0;
1796   }
1797   return (myOk1 && myOk2 && myOk3 && myOk4 && myOk5 && myOk6);
1798 }
1799
1800 //=======================================================================
1801 //function : UpdateButtons
1802 //purpose  : activate [Apply] buttons
1803 //=======================================================================
1804
1805 void SMESHGUI_SewingDlg::UpdateButtons()
1806 {
1807   bool ok = IsValid();
1808   buttonOk->setEnabled( ok );
1809   buttonApply->setEnabled( ok );
1810 }
1811
1812 //=================================================================================
1813 // function : keyPressEvent()
1814 // purpose  :
1815 //=================================================================================
1816 void SMESHGUI_SewingDlg::keyPressEvent( QKeyEvent* e )
1817 {
1818   QDialog::keyPressEvent( e );
1819   if ( e->isAccepted() )
1820     return;
1821
1822   if ( e->key() == Qt::Key_F1 ) {
1823     e->accept();
1824     ClickOnHelp();
1825   }
1826 }
1827
1828 SMESHGUI_SewingDlg::
1829 BorderGroupDisplayer::BorderGroupDisplayer( const SMESH::CoincidentFreeBorders& borders,
1830                                             int                                 groupIndex,
1831                                             QColor                              color,
1832                                             SMESH::SMESH_Mesh_ptr               mesh):
1833   myBorders   ( borders.borders ),
1834   myGroup     ( borders.coincidentGroups[ groupIndex ]),
1835   myColor     ( color ),
1836   myMesh      ( mesh ),
1837   myViewWindow( SMESH::GetCurrentVtkView() ),
1838   myIdPreview ( myViewWindow )
1839 {
1840   Update();
1841 }
1842
1843 SMESHGUI_SewingDlg::BorderGroupDisplayer::~BorderGroupDisplayer()
1844 {
1845   for ( size_t i = 0; i < myPartActors.size(); ++i )
1846   {
1847     if ( myPartActors[ i ]) {
1848       myViewWindow->RemoveActor( myPartActors[i] );
1849       myPartActors[i]->Delete();
1850     }
1851   }
1852   myIdPreview.SetPointsLabeled(false);
1853   //myViewWindow->Repaint();
1854 }
1855
1856 void SMESHGUI_SewingDlg::BorderGroupDisplayer::Hide()
1857 {
1858   for ( size_t i = 0; i < myPartActors.size(); ++i )
1859     if ( myPartActors[ i ])
1860       myPartActors[ i ]->SetVisibility(false);
1861
1862   myIdPreview.SetPointsLabeled(false);
1863 }
1864
1865 void SMESHGUI_SewingDlg::BorderGroupDisplayer::ShowGroup( bool wholeBorders )
1866 {
1867   std::vector<int> ids;
1868   std::list<gp_XYZ> coords;
1869   for ( size_t i = 0; i < myPartActors.size(); ++i )
1870     if ( myPartActors[ i ])
1871     {
1872       myPartActors[ i ]->SetPointRepresentation( wholeBorders );
1873       myPartActors[ i ]->SetVisibility( true );
1874       if ( wholeBorders )
1875         getPartEnds( i, ids, coords );
1876     }
1877   if ( wholeBorders )
1878     myIdPreview.SetElemsData( ids, coords );
1879   myIdPreview.SetPointsLabeled( wholeBorders, true );
1880 }
1881
1882 void SMESHGUI_SewingDlg::BorderGroupDisplayer::ShowPart( int partIndex, bool toEdit )
1883 {
1884   if ( partIndex < (int) myPartActors.size() )
1885   {
1886     myPartActors[partIndex]->SetVisibility(true);
1887     myPartActors[partIndex]->SetPointRepresentation(toEdit);
1888
1889     if ( toEdit )
1890     {
1891       std::vector<int> ids;
1892       std::list<gp_XYZ> coords;
1893       getPartEnds( partIndex, ids, coords );
1894
1895       myIdPreview.SetElemsData( ids, coords );
1896       myIdPreview.SetPointsLabeled( true, /*show=*/true );
1897     }
1898   }
1899 }
1900
1901 void SMESHGUI_SewingDlg::BorderGroupDisplayer::getPartEnds( int                partIndex,
1902                                                             std::vector<int> & ids,
1903                                                             std::list<gp_XYZ>& coords)
1904 {
1905   const SMESH::FreeBorderPart& aPART = myGroup  [ partIndex ];
1906   const SMESH::FreeBorder&      aBRD = myBorders[ aPART.border ];
1907
1908   ids.push_back( aBRD.nodeIDs[ aPART.node1 ]);
1909   ids.push_back( aBRD.nodeIDs[ aPART.nodeLast ]);
1910
1911   SMDS_Mesh* mesh = myPartActors[ partIndex ]->GetObject()->GetMesh();
1912
1913   coords.push_back( SMESH_TNodeXYZ( mesh->FindNode( aPART.node1+1 )));
1914   coords.push_back( SMESH_TNodeXYZ( mesh->FindNode( aPART.nodeLast+1 )));
1915 }
1916
1917 void SMESHGUI_SewingDlg::BorderGroupDisplayer::Update()
1918 {
1919   Hide();
1920   myPartActors.resize( myGroup.length(), 0 );
1921
1922   for ( size_t i = 0; i < myPartActors.size(); ++i )
1923   {
1924     TVisualObjPtr obj;
1925     if ( myPartActors[ i ])
1926       obj = myPartActors[ i ]->GetObject();
1927     else
1928       obj = TVisualObjPtr( new SMESHGUI_PreVisualObj() );
1929     SMDS_Mesh* mesh = obj->GetMesh();
1930     mesh->Clear();
1931
1932     // add nodes
1933     const SMESH::FreeBorderPart& aPRT = myGroup[ i ];
1934     const SMESH::FreeBorder&     aBRD = myBorders[ aPRT.border ];
1935     for ( CORBA::ULong iN = 0; iN < aBRD.nodeIDs.length(); ++iN )
1936     {
1937       SMESH::double_array_var xyz = myMesh->GetNodeXYZ( aBRD.nodeIDs[ iN ]);
1938       if ( xyz->length() == 3 )
1939         mesh->AddNode( xyz[0], xyz[1], xyz[2] );
1940     }
1941
1942     // add edges
1943     bool isFwd = ( Abs( aPRT.node2 - aPRT.node1 ) == 1 ) ? aPRT.node2 > aPRT.node1 : aPRT.node2 < aPRT.node1;
1944     int dn     = isFwd ? +1 : -1;
1945     int size   = (int) aBRD.nodeIDs.length();
1946     int n2, n1 = aPRT.node1;
1947     for ( n2 = n1 + dn; ( n2 >= 0 && n2 < size ); n2 += dn )
1948     {
1949       mesh->AddEdgeWithID( n1+1, n2+1, mesh->NbEdges() + 1 );
1950       n1 = n2;
1951       if ( n2 == aPRT.nodeLast )
1952         break;
1953     }
1954     if ( n2 % size != aPRT.nodeLast )
1955     {
1956       if ( n2 < 0 ) n1 = size;
1957       else          n1 = 0;
1958       for ( n2 = n1 + dn; ( n2 >= 0 && n2 < size ); n2 += dn )
1959       {
1960         mesh->AddEdgeWithID( n1+1, n2+1, mesh->NbEdges() + 1 );
1961         n1 = n2;
1962         if ( n2 == aPRT.nodeLast )
1963           break;
1964       }
1965     }
1966
1967     if ( !myPartActors[ i ]) // TVisualObj must be filled before actor creation
1968     {
1969       myPartActors[ i ] = SMESH_Actor::New( obj, "", "", 1 );
1970       myPartActors[ i ]->SetEdgeColor( myColor.redF(), myColor.greenF(), myColor.blueF() );
1971       myPartActors[ i ]->SetLineWidth( 3 * SMESH::GetFloat("SMESH:element_width",1));
1972       myPartActors[ i ]->SetPickable( false );
1973       myPartActors[ i ]->SetNodeColor( myColor.redF(), myColor.greenF(), myColor.blueF() );
1974       myPartActors[ i ]->SetMarkerStd( VTK::MT_POINT, 13 );
1975       myViewWindow->AddActor( myPartActors[ i ]);
1976       myViewWindow->Repaint();
1977     }
1978   }
1979 }
1980