Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddQuadraticElementDlg.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include "SMESHGUI_AddQuadraticElementDlg.h"
21
22 #include "SMESHGUI.h"
23 #include "SMESHGUI_Utils.h"
24 #include "SMESHGUI_VTKUtils.h"
25 #include "SMESHGUI_MeshUtils.h"
26 #include "SMESHGUI_IdValidator.h"
27 #include "SMESH_ActorUtils.h"
28
29 #include "SMDS_Mesh.hxx"
30 #include "SMESH_Actor.h"
31
32 #include "SUIT_Session.h"
33 #include "SUIT_MessageBox.h"
34 #include "LightApp_Application.h"
35
36 #include "SVTK_Selection.h"
37 #include "SVTK_Selector.h"
38 #include "SALOME_ListIO.hxx"
39 #include "SALOME_ListIteratorOfListIO.hxx"
40
41 #include "SalomeApp_Study.h"
42 #include "SalomeApp_Application.h"
43
44 #include "SVTK_ViewModel.h"
45 #include "SVTK_ViewWindow.h"
46
47 #include "utilities.h"
48
49 // OCCT Includes
50 #include <TColStd_MapOfInteger.hxx>
51 #include <TColStd_IndexedMapOfInteger.hxx>
52
53 // VTK Includes
54 #include <vtkCell.h>
55 #include <vtkIdList.h>
56 #include <vtkIntArray.h>
57 #include <vtkCellArray.h>
58 #include <vtkUnsignedCharArray.h>
59 #include <vtkUnstructuredGrid.h>
60 #include <vtkDataSetMapper.h>
61 #include <vtkProperty.h>
62
63 #include <vtkQuadraticEdge.h>
64 #include <vtkQuadraticTriangle.h>
65 #include <vtkQuadraticQuad.h>
66 #include <vtkQuadraticHexahedron.h>
67 #include <vtkQuadraticTetra.h>
68
69 // QT Includes
70 #include <qbuttongroup.h>
71 #include <qgroupbox.h>
72 #include <qlabel.h>
73 #include <qlineedit.h>
74 #include <qpushbutton.h>
75 #include <qradiobutton.h>
76 #include <qlayout.h>
77 #include <qpixmap.h>
78 #include <qcheckbox.h>
79
80 // STL includes
81 #include <list>
82
83 using namespace std;
84
85 namespace SMESH {
86
87   void ReverseConnectivity( vector<int> & ids, int type )
88   {
89     // for reverse connectivity of other types keeping the first id, see
90     // void SMESH_VisualObjDef::buildElemPrs() in SMESH_Object.cxx:900
91     const int* conn = 0;
92    
93     switch ( type ) {
94     case QUAD_TETRAHEDRON: {
95       static int aConn[] = {0,2,1,3,6,5,4,7,9,8};
96       conn = aConn;
97       break;
98     }
99     case QUAD_PYRAMID: {
100       static int aConn[] = {0,3,2,1,4,8,7,6,5,9,12,11,10};
101       conn = aConn;
102       break;
103     }
104     case QUAD_PENTAHEDRON: {
105       static int aConn[] = {0,2,1,3,5,4,8,7,6,11,10,9,12,14,13};
106       conn = aConn;
107       break;
108     }
109     case QUAD_HEXAHEDRON: {
110       static int aConn[] = {0,3,2,1,4,7,6,5,11,10,9,8,15,14,13,12,16,19,18,17};
111       conn = aConn;
112       break;
113     }
114     case QUAD_EDGE: {
115       static int aConn[] = {1,0,2};
116       conn = aConn;
117       break;
118     }
119     case QUAD_TRIANGLE: {
120       static int aConn[] = {0,2,1,5,4,3};
121       conn = aConn;
122       break;
123     }
124     case QUAD_QUADRANGLE: {
125       static int aConn[] = {0,3,2,1,7,6,5,4};
126       conn = aConn;
127       break;
128     }
129     default:;
130     }
131     if ( !conn ) {
132       reverse( ids.begin(), ids.end() );
133     }
134     else {
135       vector<int> aRevIds( ids.size() );
136       for ( int i = 0; i < ids.size(); i++)
137         aRevIds[ i ] = ids[ conn[ i ]];
138       ids = aRevIds;
139     }
140   }
141
142   class TElementSimulation {
143     SalomeApp_Application* myApplication;
144     SUIT_ViewWindow* myViewWindow;
145     SVTK_ViewWindow* myVTKViewWindow;
146
147     SALOME_Actor* myPreviewActor;
148     vtkDataSetMapper* myMapper;
149     vtkUnstructuredGrid* myGrid;
150     //vtkProperty* myBackProp, *myProp;
151
152     vtkFloatingPointType myRGB[3], myBackRGB[3];
153
154   public:
155     TElementSimulation (SalomeApp_Application* theApplication)
156     {
157       myApplication = theApplication;
158       SUIT_ViewManager* mgr = theApplication->activeViewManager();
159       if (!mgr) return;
160       myViewWindow = mgr->getActiveView();
161       myVTKViewWindow = GetVtkViewWindow(myViewWindow);
162
163       myGrid = vtkUnstructuredGrid::New();
164
165       // Create and display actor
166       myMapper = vtkDataSetMapper::New();
167       myMapper->SetInput(myGrid);
168
169       myPreviewActor = SALOME_Actor::New();
170       myPreviewActor->PickableOff();
171       myPreviewActor->VisibilityOff();
172       myPreviewActor->SetMapper(myMapper);
173
174       vtkProperty* myProp = vtkProperty::New();
175       GetColor( "SMESH", "fill_color", myRGB[0], myRGB[1], myRGB[2], QColor( 0, 170, 255 ) );
176       myProp->SetColor( myRGB[0], myRGB[1], myRGB[2] );
177       myPreviewActor->SetProperty( myProp );
178       myProp->Delete();
179
180       vtkProperty* myBackProp = vtkProperty::New();
181       GetColor( "SMESH", "backface_color", myBackRGB[0], myBackRGB[1], myBackRGB[2], QColor( 0, 0, 255 ) );
182       myBackProp->SetColor( myBackRGB[0], myBackRGB[1], myBackRGB[2] );
183       myPreviewActor->SetBackfaceProperty( myBackProp );
184       myBackProp->Delete();
185
186       myVTKViewWindow->AddActor(myPreviewActor);
187     }
188
189     typedef std::vector<vtkIdType> TVTKIds;
190     void SetPosition (SMESH_Actor* theActor,
191                       const int    theType,
192                       TVTKIds&     theIds,
193                       const int    theMode,
194                       const bool   theReverse)
195     {
196       vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
197       myGrid->SetPoints(aGrid->GetPoints());
198
199       //add points
200
201       vtkIdType aType = 0;
202
203       switch (theType) {
204       case QUAD_EDGE:
205         aType = VTK_QUADRATIC_EDGE;
206         break;
207       case QUAD_TRIANGLE:
208         aType = VTK_QUADRATIC_TRIANGLE; 
209         break;
210       case QUAD_QUADRANGLE:
211         aType = VTK_QUADRATIC_QUAD; 
212         break;
213       case QUAD_TETRAHEDRON:
214         aType = VTK_QUADRATIC_TETRA; 
215         break;
216       case QUAD_PYRAMID:
217         //aType = VTK_QUADRATIC_PYRAMID; // NOT SUPPORTED IN VTK4.2
218         aType = VTK_CONVEX_POINT_SET;
219         break;
220       case QUAD_PENTAHEDRON:
221         //aType = VTK_QUADRATIC_WEDGE; // NOT SUPPORTED IN VTK4.2
222         aType = VTK_CONVEX_POINT_SET;
223         break; 
224       case QUAD_HEXAHEDRON:
225         aType = VTK_QUADRATIC_HEXAHEDRON;
226         break;
227       }
228
229       // take care of orientation
230       if ( aType == VTK_CONVEX_POINT_SET ) {
231         if ( theReverse && theMode == VTK_SURFACE ) {
232           //myPreviewActor->GetProperty()->SetColor( myBackRGB[0], myBackRGB[1], myBackRGB[2] );
233         }
234       }
235       else {
236         // VTK cell connectivity opposites the MED one for volumic elements
237         if ( theIds.size() > 8 ? !theReverse : theReverse ) {
238           ReverseConnectivity( theIds, theType );
239         }
240       }
241             
242       myGrid->Reset();
243       vtkIdList *anIds = vtkIdList::New();
244       
245       for (int i = 0, iEnd = theIds.size(); i < iEnd; i++) {
246         anIds->InsertId(i,theIds[i]);
247         //std::cout << i<< ": " << theIds[i] << std::endl;
248       }
249       
250       myGrid->InsertNextCell(aType,anIds);
251       anIds->Delete();
252       
253       myGrid->Modified();
254
255       myPreviewActor->GetMapper()->Update();
256       myPreviewActor->SetRepresentation( theMode );
257       SetVisibility(true);
258
259       // restore normal orientation
260       if ( aType == VTK_CONVEX_POINT_SET ) {
261         if ( theReverse  && theMode == VTK_SURFACE ) {
262           //myPreviewActor->GetProperty()->SetColor( myRGB[0], myRGB[1], myRGB[2] );
263         }
264       }
265     }
266
267
268     void SetVisibility (bool theVisibility)
269     {
270       myPreviewActor->SetVisibility(theVisibility);
271       RepaintCurrentView();
272     }
273
274
275     ~TElementSimulation()
276     {
277       if (FindVtkViewWindow(myApplication->activeViewManager(), myViewWindow)) {
278         myVTKViewWindow->RemoveActor(myPreviewActor);
279       }
280       myPreviewActor->Delete();
281
282       myMapper->RemoveAllInputs();
283       myMapper->Delete();
284
285       myGrid->Delete();
286
287 //       myProp->Delete();
288 //       myBackProp->Delete();
289     }
290   };
291 }
292
293
294 // Define the sequences of ids
295 static int FirstEdgeIds[] = {0};
296 static int LastEdgeIds[] =  {1};
297
298 static int FirstTriangleIds[] = {0,1,2};
299 static int LastTriangleIds[] =  {1,2,0};
300
301 static int FirstQuadrangleIds[] = {0,1,2,3};
302 static int LastQuadrangleIds[] =  {1,2,3,0};
303
304 static int FirstTetrahedronIds[] = {0,1,2,3,3,3};
305 static int LastTetrahedronIds[] =  {1,2,0,0,1,2};
306
307 static int FirstPyramidIds[] = {0,1,2,3,4,4,4,4};
308 static int LastPyramidIds[] =  {1,2,3,0,0,1,2,3};
309
310 static int FirstPentahedronIds[] = {0,1,2,3,4,5,0,1,2};
311 static int LastPentahedronIds[] =  {1,2,0,4,5,3,3,4,5};
312
313 static int FirstHexahedronIds[] = {0,1,2,3,4,5,6,7,0,1,2,3};
314 static int LastHexahedronIds[] =  {1,2,3,0,5,6,7,4,4,5,6,7};
315
316
317
318 //=================================================================================
319 // function : SMESHGUI_AddQuadraticElementDlg()
320 // purpose  : constructor
321 //=================================================================================
322 SMESHGUI_AddQuadraticElementDlg::SMESHGUI_AddQuadraticElementDlg( SMESHGUI* theModule,
323                                                                   const int theType,
324                                                                   const char* name,
325                                                                   bool modal, WFlags fl)
326      : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder |
327                 WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),
328      mySMESHGUI( theModule ),
329      mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
330      myType( theType )
331 {
332   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
333     (SUIT_Session::session()->activeApplication());
334   
335   mySimulation = new SMESH::TElementSimulation (anApp);
336   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
337
338   QString anElementName;
339
340   switch ( myType ) {
341   case QUAD_EDGE:
342     anElementName = QString("QUADRATIC_EDGE");
343     break;
344   case QUAD_TRIANGLE:
345     anElementName = QString("QUADRATIC_TRIANGLE");
346     break; 
347   case QUAD_QUADRANGLE:
348     anElementName = QString("QUADRATIC_QUADRANGLE");
349     break;
350   case QUAD_TETRAHEDRON:
351     anElementName = QString("QUADRATIC_TETRAHEDRON");
352     break;
353   case QUAD_PYRAMID:
354     anElementName = QString("QUADRATIC_PYRAMID");
355     break;
356   case QUAD_PENTAHEDRON:
357     anElementName = QString("QUADRATIC_PENTAHEDRON");
358     break;
359   case QUAD_HEXAHEDRON:
360     anElementName = QString("QUADRATIC_HEXAHEDRON");
361     break;
362   default:
363     myType = QUAD_EDGE;
364     anElementName = QString("QUADRATIC_EDGE");
365   }
366
367   QString iconName           = tr(QString("ICON_DLG_%1").arg(anElementName));
368   QString caption            = tr(QString("SMESH_ADD_%1_TITLE").arg(anElementName));
369   QString argumentsGrTitle   = tr(QString("SMESH_ADD_%1").arg(anElementName));
370   QString constructorGrTitle = tr(QString("SMESH_%1").arg(anElementName));
371   
372   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", iconName));
373   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
374
375   if (!name)
376     setName("SMESHGUI_AddQuadraticElementDlg");
377   setCaption(caption);
378   
379   setSizeGripEnabled(TRUE);
380   QGridLayout* aDialogLayout = new QGridLayout(this);
381   aDialogLayout->setSpacing(6);
382   aDialogLayout->setMargin(11);
383
384   /***************************************************************/
385   GroupConstructors = new QButtonGroup(this, "GroupConstructors");
386   GroupConstructors->setTitle(constructorGrTitle);
387
388   GroupConstructors->setExclusive(TRUE);
389   GroupConstructors->setColumnLayout(0, Qt::Vertical);
390   GroupConstructors->layout()->setSpacing(0);
391   GroupConstructors->layout()->setMargin(0);
392   GroupConstructors->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
393   QGridLayout* aGroupConstructorsLayout = new QGridLayout(GroupConstructors->layout());
394   aGroupConstructorsLayout->setAlignment(Qt::AlignTop);
395   aGroupConstructorsLayout->setSpacing(6);
396   aGroupConstructorsLayout->setMargin(11);
397   myRadioButton1 = new QRadioButton(GroupConstructors, "myRadioButton1");
398   myRadioButton1->setText(tr("" ));
399   myRadioButton1->setPixmap(image0);
400   myRadioButton1->setChecked(TRUE);
401   myRadioButton1->setSizePolicy(QSizePolicy((QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, myRadioButton1->sizePolicy().hasHeightForWidth()));
402   aGroupConstructorsLayout->addWidget(myRadioButton1, 0, 0);
403   aGroupConstructorsLayout->addItem( new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 1);
404   aDialogLayout->addWidget(GroupConstructors, 0, 0);
405
406   /***************************************************************/
407   GroupArguments = new QGroupBox(this, "GroupArguments");
408   GroupArguments->setTitle(argumentsGrTitle);
409   GroupArguments->setColumnLayout(0, Qt::Vertical);
410   GroupArguments->layout()->setSpacing(0);
411   GroupArguments->layout()->setMargin(0);
412   GroupArguments->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding));
413   QGridLayout* aGroupArgumentsLayout = new QGridLayout(GroupArguments->layout());
414   aGroupArgumentsLayout->setAlignment(Qt::AlignTop);
415   aGroupArgumentsLayout->setSpacing(6);
416   aGroupArgumentsLayout->setMargin(11);
417   QLabel* aCornerNodesLabel = new QLabel(GroupArguments, "aCornerNodesLabel");
418   aCornerNodesLabel->setText(tr("SMESH_CORNER_NODES" ));
419   aGroupArgumentsLayout->addWidget(aCornerNodesLabel, 0, 0);
420   mySelectButton = new QPushButton(GroupArguments, "mySelectButton");
421   mySelectButton->setPixmap(image1);
422   aGroupArgumentsLayout->addWidget(mySelectButton, 0, 1);
423   myCornerNodes = new QLineEdit(GroupArguments, "myCornerNodes");
424   aGroupArgumentsLayout->addWidget(myCornerNodes, 0, 2);
425
426   myTable = new QTable(GroupArguments);
427   aGroupArgumentsLayout->addMultiCellWidget(myTable, 1, 1, 0, 2);
428  
429   myReverseCB = new QCheckBox(GroupArguments, "myReverseCB");
430   myReverseCB->setText(tr("SMESH_REVERSE" ));
431   aGroupArgumentsLayout->addWidget(myReverseCB, 2, 0);
432  
433   aDialogLayout->addWidget(GroupArguments, 1, 0);
434
435   
436   /***************************************************************/
437   GroupButtons = new QGroupBox(this, "GroupButtons");
438   GroupButtons->setColumnLayout(0, Qt::Vertical);
439   GroupButtons->layout()->setSpacing(0);
440   GroupButtons->layout()->setMargin(0);
441   GroupButtons->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
442   QGridLayout* aGroupButtonsLayout = new QGridLayout(GroupButtons->layout());
443   aGroupButtonsLayout->setAlignment(Qt::AlignTop);
444   aGroupButtonsLayout->setSpacing(6);
445   aGroupButtonsLayout->setMargin(11);
446   buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
447   buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
448   buttonCancel->setAutoDefault(TRUE);
449   aGroupButtonsLayout->addWidget(buttonCancel, 0, 3);
450   buttonApply = new QPushButton(GroupButtons, "buttonApply");
451   buttonApply->setText(tr("SMESH_BUT_APPLY" ));
452   buttonApply->setAutoDefault(TRUE);
453   aGroupButtonsLayout->addWidget(buttonApply, 0, 1);
454   aGroupButtonsLayout->addItem( new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 2);
455   buttonOk = new QPushButton(GroupButtons, "buttonOk");
456   buttonOk->setText(tr("SMESH_BUT_OK" ));
457   buttonOk->setAutoDefault(TRUE);
458   buttonOk->setDefault(TRUE);
459   aGroupButtonsLayout->addWidget(buttonOk, 0, 0);
460   buttonHelp = new QPushButton(GroupButtons, "buttonHelp");
461   buttonHelp->setText(tr("SMESH_BUT_HELP" ));
462   buttonHelp->setAutoDefault(TRUE);
463   aGroupButtonsLayout->addWidget(buttonHelp, 0, 4);
464
465   aDialogLayout->addWidget(GroupButtons, 2, 0);
466
467   Init(); /* Initialisations */
468 }
469
470 //=================================================================================
471 // function : ~SMESHGUI_AddQuadraticElementDlg()
472 // purpose  : Destroys the object and frees any allocated resources
473 //=================================================================================
474 SMESHGUI_AddQuadraticElementDlg::~SMESHGUI_AddQuadraticElementDlg()
475 {
476   // no need to delete child widgets, Qt does it all for us
477   delete mySimulation;
478 }
479
480 //=================================================================================
481 // function : Init()
482 // purpose  :
483 //=================================================================================
484 void SMESHGUI_AddQuadraticElementDlg::Init()
485 {
486   GroupArguments->show();
487   myRadioButton1->setChecked(TRUE);
488   myIsEditCorners = true;
489   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
490   
491   myActor = 0;
492
493   int aNumRows;
494
495   switch (myType) {
496   case QUAD_EDGE:
497     aNumRows = 1;
498     myNbCorners = 2;
499     myHelpFileName = "/adding_quadratic_nodes_and_elements.htm#?"; //Adding_edges
500     break;
501   case QUAD_TRIANGLE:
502     aNumRows = 3;
503     myNbCorners = 3;
504     myHelpFileName = "/adding_quadratic_nodes_and_elements.htm#?"; //Adding_triangles
505     break;
506   case QUAD_QUADRANGLE:
507     aNumRows = 4;
508     myNbCorners = 4;
509     myHelpFileName = "/adding_quadratic_nodes_and_elements.htm#?"; //Adding_quadrangles
510     break;
511   case QUAD_TETRAHEDRON:
512     aNumRows = 6;
513     myNbCorners = 4;
514     myHelpFileName = "/adding_quadratic_nodes_and_elements.htm#?"; //Adding_tetrahedrons
515     break;
516   case QUAD_PYRAMID:
517     aNumRows = 8;
518     myNbCorners = 5;
519     myHelpFileName = "/adding_quadratic_nodes_and_elements.htm#?"; //Adding_pyramids
520     break;
521   case QUAD_PENTAHEDRON:
522     aNumRows = 9;
523     myNbCorners = 6;
524     myHelpFileName = "/adding_quadratic_nodes_and_elements.htm#?"; //Adding_pentahedrons
525     break; 
526   case QUAD_HEXAHEDRON:
527     aNumRows = 12;
528     myNbCorners = 8;
529     myHelpFileName = "/adding_quadratic_nodes_and_elements.htm#?"; //Adding_hexahedrons
530     break;
531   }
532     
533   myCornerNodes->setValidator(new SMESHGUI_IdValidator(this, "validator", myNbCorners));
534
535   /* initialize table */
536   myTable->setNumCols(3);
537   myTable->setNumRows(aNumRows);
538
539   QStringList aColLabels;
540   aColLabels.append(tr("SMESH_FIRST"));
541   aColLabels.append(tr("SMESH_MIDDLE"));
542   aColLabels.append(tr("SMESH_LAST"));
543   myTable->setColumnLabels(aColLabels);
544   
545   for ( int col = 0; col < myTable->numCols(); col++ )
546     myTable->setColumnWidth(col, 80);
547
548   myTable->setColumnReadOnly(0, true);
549   myTable->setColumnReadOnly(2, true);
550
551   myTable->setEnabled( false );
552   
553   for ( int row = 0; row < myTable->numRows(); row++ )
554     {
555       SMESHGUI_IdEditItem* anEditItem = new SMESHGUI_IdEditItem( myTable, QTableItem::OnTyping, "" );
556       anEditItem->setReplaceable(false);
557       myTable->setItem(row, 1, anEditItem);
558     }
559   
560   /* signals and slots connections */
561   connect(mySelectButton, SIGNAL(clicked()), SLOT(SetEditCorners()));
562   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
563   connect(myTable,        SIGNAL(doubleClicked(int, int, int, const QPoint&)), SLOT(onCellDoubleClicked(int, int, int, const QPoint&)));
564   connect(myTable,        SIGNAL(valueChanged (int, int)), SLOT(onCellTextChange(int, int)));
565   connect(myCornerNodes,  SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
566   connect(myReverseCB,    SIGNAL(stateChanged(int)), SLOT(onReverse(int)));
567
568   connect(buttonOk, SIGNAL(clicked()),     SLOT(ClickOnOk()));
569   connect(buttonCancel, SIGNAL(clicked()), SLOT(ClickOnCancel()));
570   connect(buttonApply, SIGNAL(clicked()),  SLOT(ClickOnApply()));
571   connect(buttonHelp, SIGNAL(clicked()),   SLOT(ClickOnHelp()));
572
573   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
574   connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
575
576   this->show(); // displays Dialog
577
578   // set selection mode
579   SMESH::SetPointRepresentation(true);
580
581   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
582     aViewWindow->SetSelectionMode( NodeSelection );
583
584   myBusy = false;
585
586   SetEditCorners();
587 }
588
589 //=================================================================================
590 // function : ClickOnApply()
591 // purpose  :
592 //=================================================================================
593 void SMESHGUI_AddQuadraticElementDlg::ClickOnApply()
594 {
595   if (IsValid() && !mySMESHGUI->isActiveStudyLocked()) {
596     myBusy = true;
597     
598     vector<int> anIds;
599
600     switch (myType) {
601     case QUAD_EDGE:
602       anIds.push_back(myTable->text(0, 0).toInt());
603       anIds.push_back(myTable->text(0, 2).toInt());
604       anIds.push_back(myTable->text(0, 1).toInt());
605       break;
606     case QUAD_TRIANGLE:
607     case QUAD_QUADRANGLE:
608     case QUAD_TETRAHEDRON:
609     case QUAD_PYRAMID:
610     case QUAD_PENTAHEDRON:
611     case QUAD_HEXAHEDRON:
612       for ( int row = 0; row < myNbCorners; row++ )
613         anIds.push_back(myTable->text(row, 0).toInt());
614       for ( int row = 0; row < myTable->numRows(); row++ )
615         anIds.push_back(myTable->text(row, 1).toInt());
616       break;
617     }
618     if ( myReverseCB->isChecked())
619       SMESH::ReverseConnectivity( anIds, myType );
620     
621     int aNumberOfIds =  anIds.size();
622     SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
623     anArrayOfIdeces->length( aNumberOfIds );
624     
625     for (int i = 0; i < aNumberOfIds; i++)
626       anArrayOfIdeces[i] = anIds[ i ];
627
628     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
629     switch (myType) {
630     case QUAD_EDGE:
631       aMeshEditor->AddEdge(anArrayOfIdeces.inout()); break;
632     case QUAD_TRIANGLE:
633     case QUAD_QUADRANGLE:
634       aMeshEditor->AddFace(anArrayOfIdeces.inout()); break;
635     case QUAD_TETRAHEDRON:
636     case QUAD_PYRAMID:
637     case QUAD_PENTAHEDRON: 
638     case QUAD_HEXAHEDRON:
639       aMeshEditor->AddVolume(anArrayOfIdeces.inout()); break;
640     }
641     
642     SALOME_ListIO aList; aList.Append( myActor->getIO() );
643     mySelector->ClearIndex();
644     mySelectionMgr->setSelectedObjects( aList, false );
645
646     SMESH::UpdateView();
647     mySimulation->SetVisibility(false);
648     
649     buttonOk->setEnabled(false);
650     buttonApply->setEnabled(false);
651
652     UpdateTable();
653     SetEditCorners();
654
655     myBusy = false;
656   }
657 }
658
659 //=================================================================================
660 // function : ClickOnOk()
661 // purpose  :
662 //=================================================================================
663 void SMESHGUI_AddQuadraticElementDlg::ClickOnOk()
664 {
665   ClickOnApply();
666   ClickOnCancel();
667   return;
668 }
669
670 //=================================================================================
671 // function : ClickOnCancel()
672 // purpose  :
673 //=================================================================================
674 void SMESHGUI_AddQuadraticElementDlg::ClickOnCancel()
675 {
676   mySelectionMgr->clearSelected();
677   mySimulation->SetVisibility(false);
678   SMESH::SetPointRepresentation(false);
679   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
680     aViewWindow->SetSelectionMode( ActorSelection );
681   disconnect(mySelectionMgr, 0, this, 0);
682   mySMESHGUI->ResetState();
683   reject();
684   return;
685 }
686
687 //=================================================================================
688 // function : ClickOnHelp()
689 // purpose  :
690 //=================================================================================
691 void SMESHGUI_AddQuadraticElementDlg::ClickOnHelp()
692 {
693   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
694   if (app) 
695     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
696   else {
697     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
698                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
699                            arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
700                            QObject::tr("BUT_OK"));
701   }
702 }
703
704 //=================================================================================
705 // function : onTextChange()
706 // purpose  :
707 //=================================================================================
708 void SMESHGUI_AddQuadraticElementDlg::onTextChange (const QString& theNewText)
709 {
710   if (myBusy) return;
711   myBusy = true;
712   
713   buttonOk->setEnabled(false);
714   buttonApply->setEnabled(false);
715
716   mySimulation->SetVisibility(false);
717
718   // hilight entered nodes
719   SMDS_Mesh* aMesh = 0;
720   if (myActor)
721     aMesh = myActor->GetObject()->GetMesh();
722
723   if (aMesh) {
724     TColStd_MapOfInteger newIndices;
725     
726     QStringList aListId = QStringList::split(" ", theNewText, false);
727     bool allOk = true;
728     for (int i = 0; i < aListId.count(); i++) {
729       if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
730         newIndices.Add( n->GetID() );
731       else
732         {
733           allOk = false;
734           break;
735         }
736     }
737     
738     mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
739     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
740       aViewWindow->highlight( myActor->getIO(), true, true );
741     
742     if ( sender() == myCornerNodes )
743       UpdateTable( allOk );
744   }
745   
746   if( IsValid() ) {
747     buttonOk->setEnabled(true);
748     buttonApply->setEnabled(true);
749   }
750
751   if ( sender() == myTable )
752     displaySimulation();
753   
754   myBusy = false;
755 }
756
757 //=================================================================================
758 // function : SelectionIntoArgument()
759 // purpose  : Called when selection has changed
760 //=================================================================================
761 void SMESHGUI_AddQuadraticElementDlg::SelectionIntoArgument()
762 {
763   if (myBusy) return;
764   
765   if ( myIsEditCorners )
766     {
767       // clear
768       myActor = 0;
769       
770       myBusy = true;
771       myCornerNodes->setText("");
772       myBusy = false;
773       
774       if (!GroupButtons->isEnabled()) // inactive
775         return;
776       
777       buttonOk->setEnabled(false);
778       buttonApply->setEnabled(false);
779       
780       mySimulation->SetVisibility(false);
781       
782       // get selected mesh
783       SALOME_ListIO aList;
784       mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
785       
786       if (aList.Extent() != 1)
787         {
788           UpdateTable();
789           return;
790         }
791       
792       Handle(SALOME_InteractiveObject) anIO = aList.First();
793       myMesh = SMESH::GetMeshByIO(anIO);
794       if (myMesh->_is_nil())
795         return;
796       
797       myActor = SMESH::FindActorByEntry(anIO->getEntry());
798   
799     }
800   
801   if (!myActor)
802     return;
803   
804   // get selected nodes
805   QString aString = "";
806   int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,myActor->getIO(),aString);
807   
808   if ( myIsEditCorners )
809     {
810       myBusy = true;
811       myCornerNodes->setText(aString);
812       myBusy = false;
813       
814       UpdateTable();
815     }
816   else if ( myTable->isEnabled() && nbNodes == 1 )
817     {
818       myBusy = true;
819       int theRow = myTable->currentRow(), theCol = myTable->currentColumn();
820       if ( theCol == 1 )
821         myTable->setText(theRow, 1, aString);
822       myBusy = false;
823     }
824   
825   if ( IsValid() )
826     {
827       buttonOk->setEnabled( true );
828       buttonApply->setEnabled( true );
829     }
830
831   displaySimulation();
832 }
833
834 //=================================================================================
835 // function : displaySimulation()
836 // purpose  :
837 //=================================================================================
838 void SMESHGUI_AddQuadraticElementDlg::displaySimulation()
839 {
840   if (!myIsEditCorners) {
841     SMESH::TElementSimulation::TVTKIds anIds;
842     
843     // Collect ids from the dialog
844     int anID;
845     bool ok;
846     int aDisplayMode = VTK_SURFACE;
847
848     if ( myType == QUAD_EDGE )
849       {
850         anIds.push_back( myActor->GetObject()->GetNodeVTKId( myTable->text(0, 0).toInt() ) );
851         anIds.push_back( myActor->GetObject()->GetNodeVTKId( myTable->text(0, 2).toInt() ) );
852         anID = (myTable->text(0, 1)).toInt(&ok);
853         if (!ok) anID = (myTable->text(0, 0)).toInt();
854         anIds.push_back( myActor->GetObject()->GetNodeVTKId(anID) );
855         aDisplayMode = VTK_WIREFRAME;
856       }
857     else
858       {
859         for ( int row = 0; row < myNbCorners; row++ )
860           anIds.push_back( myActor->GetObject()->GetNodeVTKId( myTable->text(row, 0).toInt() ) );
861         
862         for ( int row = 0; row < myTable->numRows(); row++ )
863           {
864             anID = (myTable->text(row, 1)).toInt(&ok);
865             if (!ok) {
866               anID = (myTable->text(row, 0)).toInt();
867               aDisplayMode = VTK_WIREFRAME;
868             }
869             anIds.push_back( myActor->GetObject()->GetNodeVTKId(anID) );
870           }
871       }
872     
873     mySimulation->SetPosition(myActor,myType,anIds,aDisplayMode,myReverseCB->isChecked());
874     SMESH::UpdateView();
875   }
876 }
877
878 //=================================================================================
879 // function : SetEditCorners()
880 // purpose  :
881 //=================================================================================
882 void SMESHGUI_AddQuadraticElementDlg::SetEditCorners()
883 {
884   myCornerNodes->setFocus();
885   myIsEditCorners = true;
886   
887   SelectionIntoArgument();
888 }
889
890 //=================================================================================
891 // function : DeactivateActiveDialog()
892 // purpose  :
893 //=================================================================================
894 void SMESHGUI_AddQuadraticElementDlg::DeactivateActiveDialog()
895 {
896   if (GroupConstructors->isEnabled()) {
897     GroupConstructors->setEnabled(false);
898     GroupArguments->setEnabled(false);
899     GroupButtons->setEnabled(false);
900     mySimulation->SetVisibility(false);
901     mySMESHGUI->ResetState();
902     mySMESHGUI->SetActiveDialogBox(0);
903   }
904 }
905
906 //=================================================================================
907 // function : ActivateThisDialog()
908 // purpose  :
909 //=================================================================================
910 void SMESHGUI_AddQuadraticElementDlg::ActivateThisDialog()
911 {
912   /* Emit a signal to deactivate the active dialog */
913   mySMESHGUI->EmitSignalDeactivateDialog();
914
915   GroupConstructors->setEnabled(true);
916   GroupArguments->setEnabled(true);
917   GroupButtons->setEnabled(true);
918
919   SMESH::SetPointRepresentation(true);
920
921   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
922     aViewWindow->SetSelectionMode( NodeSelection );
923   SelectionIntoArgument();
924 }
925
926 //=================================================================================
927 // function : enterEvent()
928 // purpose  :
929 //=================================================================================
930 void SMESHGUI_AddQuadraticElementDlg::enterEvent (QEvent*)
931 {
932   if (GroupConstructors->isEnabled())
933     return;
934   ActivateThisDialog();
935   return;
936 }
937
938 //=================================================================================
939 // function : closeEvent()
940 // purpose  :
941 //=================================================================================
942 void SMESHGUI_AddQuadraticElementDlg::closeEvent (QCloseEvent*)
943 {
944   /* same than click on cancel button */
945   ClickOnCancel();
946   return;
947 }
948
949 //=================================================================================
950 // function : hideEvent()
951 // purpose  : caused by ESC key
952 //=================================================================================
953 void SMESHGUI_AddQuadraticElementDlg::hideEvent (QHideEvent*)
954 {
955   if (!isMinimized())
956     ClickOnCancel();
957 }
958
959 //=================================================================================
960 // function : onReverse()
961 // purpose  :
962 //=================================================================================
963 void SMESHGUI_AddQuadraticElementDlg::onReverse (int state)
964 {
965   if (!IsValid())
966     return;
967
968   if (state >= 0) {
969     mySimulation->SetVisibility(false);
970     displaySimulation();
971   }
972 }
973
974
975 //=================================================================================
976 // function : IsValid()
977 // purpose  :
978 //=================================================================================
979 bool SMESHGUI_AddQuadraticElementDlg::IsValid()
980 {
981   SMDS_Mesh* aMesh = 0;
982   if (myActor)
983     aMesh = myActor->GetObject()->GetMesh();
984   if (!aMesh)
985     return false;
986
987   bool ok;
988   
989   for ( int row = 0; row < myTable->numRows(); row++ )
990     {
991       int anID =  (myTable->text(row, 1)).toInt(&ok);
992       if ( !ok )
993         return false;
994       
995       const SMDS_MeshNode * aNode = aMesh->FindNode(anID);
996       if ( !aNode )
997         return false;
998     }
999   
1000   return true;
1001 }
1002
1003 //=================================================================================
1004 // function : UpdateTable()
1005 // purpose  :
1006 //=================================================================================
1007 void SMESHGUI_AddQuadraticElementDlg::UpdateTable( bool theConersValidity )
1008 {
1009   QStringList aListCorners = QStringList::split(" ", myCornerNodes->text(), false);
1010   
1011   if ( aListCorners.count() == myNbCorners && theConersValidity )
1012     {
1013       myTable->setEnabled( true );
1014       
1015       // clear the Middle column 
1016       for ( int row = 0; row < myTable->numRows(); row++ )
1017         myTable->setText( row, 1, "");
1018       
1019       int* aFirstColIds;
1020       int* aLastColIds;
1021
1022       switch (myType) {
1023       case QUAD_EDGE:
1024         aFirstColIds = FirstEdgeIds;
1025         aLastColIds  = LastEdgeIds;
1026         break;
1027       case QUAD_TRIANGLE:
1028         aFirstColIds = FirstTriangleIds;
1029         aLastColIds  = LastTriangleIds;
1030         break;
1031       case QUAD_QUADRANGLE:
1032         aFirstColIds = FirstQuadrangleIds;
1033         aLastColIds  = LastQuadrangleIds;
1034         break;
1035       case QUAD_TETRAHEDRON:
1036         aFirstColIds = FirstTetrahedronIds;
1037         aLastColIds  = LastTetrahedronIds;
1038         break;
1039       case QUAD_PYRAMID:
1040         aFirstColIds = FirstPyramidIds;
1041         aLastColIds  = LastPyramidIds;
1042         break;
1043       case QUAD_PENTAHEDRON:
1044         aFirstColIds = FirstPentahedronIds;
1045         aLastColIds  = LastPentahedronIds;
1046         break; 
1047       case QUAD_HEXAHEDRON:
1048         aFirstColIds = FirstHexahedronIds;
1049         aLastColIds  = LastHexahedronIds;
1050         break;
1051       }
1052       
1053       // fill the First and the Last columns
1054       for (int i = 0, iEnd = myTable->numRows(); i < iEnd; i++)
1055         myTable->setText( i, 0, aListCorners[ aFirstColIds[i] ] );
1056       
1057       for (int i = 0, iEnd = myTable->numRows(); i < iEnd; i++)
1058         myTable->setText( i, 2, aListCorners[ aLastColIds[i] ] );
1059     }
1060   else
1061     {
1062       // clear table
1063       for ( int row = 0; row < myTable->numRows(); row++ )
1064         for ( int col = 0; col < myTable->numCols(); col++ )
1065           myTable->setText(row, col, "");
1066       
1067       myTable->setEnabled( false );
1068     }
1069 }
1070
1071
1072 //=================================================================================
1073 // function : onTableActivate()
1074 // purpose  :
1075 //=================================================================================
1076 void SMESHGUI_AddQuadraticElementDlg::onCellDoubleClicked( int theRow, int theCol, int theButton, const QPoint& theMousePos )
1077 {
1078   if ( theButton == 1 && theCol == 1 )
1079     myIsEditCorners = false;
1080   
1081   displaySimulation();
1082   return;
1083 }
1084
1085
1086 //=================================================================================
1087 // function : onCellTextChange()
1088 // purpose  :
1089 //=================================================================================
1090 void SMESHGUI_AddQuadraticElementDlg::onCellTextChange(int theRow, int theCol)
1091 {
1092   onTextChange( myTable->text(theRow, theCol) );
1093 }
1094
1095
1096 QWidget* SMESHGUI_IdEditItem::createEditor() const
1097 {
1098   QLineEdit *aLineEdit = new QLineEdit(text(), table()->viewport());
1099   aLineEdit->setValidator( new SMESHGUI_IdValidator(table()->viewport(), "validator", 1) );
1100   return aLineEdit;
1101 }