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