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