1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESHGUI_AddMeshElementDlg.cxx
23 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
27 #include "SMESHGUI_AddMeshElementDlg.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_VTKUtils.h"
32 #include "SMESHGUI_MeshUtils.h"
33 #include "SMESHGUI_IdValidator.h"
35 #include <SMESH_Actor.h>
36 #include <SMESH_ActorUtils.h>
37 #include <SMDS_Mesh.hxx>
40 #include <SUIT_Desktop.h>
41 #include <SUIT_Session.h>
42 #include <SUIT_ResourceMgr.h>
43 #include <SUIT_MessageBox.h>
44 #include <SUIT_ViewManager.h>
45 #include <LightApp_SelectionMgr.h>
46 #include <SALOME_ListIO.hxx>
47 #include <SalomeApp_Application.h>
48 #include <SVTK_ViewModel.h>
49 #include <SVTK_ViewWindow.h>
52 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
55 #include <TColStd_MapOfInteger.hxx>
59 #include <vtkIdList.h>
60 #include <vtkUnstructuredGrid.h>
61 #include <vtkDataSetMapper.h>
62 #include <vtkProperty.h>
68 #include <QPushButton>
69 #include <QRadioButton>
70 #include <QHBoxLayout>
71 #include <QVBoxLayout>
72 #include <QGridLayout>
82 class TElementSimulation
84 SalomeApp_Application* myApplication;
85 SUIT_ViewWindow* myViewWindow;
86 SVTK_ViewWindow* myVTKViewWindow;
88 SALOME_Actor* myPreviewActor;
89 vtkDataSetMapper* myMapper;
90 vtkUnstructuredGrid* myGrid;
93 TElementSimulation (SalomeApp_Application* theApplication)
95 myApplication = theApplication;
96 SUIT_ViewManager* mgr = theApplication->activeViewManager();
98 myViewWindow = mgr->getActiveView();
99 myVTKViewWindow = GetVtkViewWindow(myViewWindow);
101 myGrid = vtkUnstructuredGrid::New();
103 // Create and display actor
104 myMapper = vtkDataSetMapper::New();
105 myMapper->SetInput(myGrid);
107 myPreviewActor = SALOME_Actor::New();
108 myPreviewActor->PickableOff();
109 myPreviewActor->VisibilityOff();
110 myPreviewActor->SetMapper(myMapper);
112 vtkFloatingPointType anRGB[3];
113 vtkProperty* aProp = vtkProperty::New();
114 GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
115 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
116 myPreviewActor->SetProperty( aProp );
119 vtkProperty* aBackProp = vtkProperty::New();
120 GetColor( "SMESH", "backface_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 0, 255 ) );
121 aBackProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
122 myPreviewActor->SetBackfaceProperty( aBackProp );
125 myVTKViewWindow->AddActor(myPreviewActor);
128 typedef std::vector<vtkIdType> TVTKIds;
129 void SetPosition (SMESH_Actor* theActor,
131 const TVTKIds& theIds)
133 vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
134 myGrid->SetPoints(aGrid->GetPoints());
136 const int* aConn = NULL;
140 static int anIds[] = {0,2,1,3};
146 static int anIds[] = {0,3,2,1,4};
152 static int anIds[] = {0,3,2,1,4,7,6,5};
159 vtkIdList *anIds = vtkIdList::New();
162 for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
163 anIds->InsertId(i,theIds[aConn[i]]);
165 for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
166 anIds->InsertId(i,theIds[i]);
168 myGrid->InsertNextCell(theType,anIds);
177 void SetVisibility (bool theVisibility)
179 myPreviewActor->SetVisibility(theVisibility);
180 RepaintCurrentView();
184 ~TElementSimulation()
186 if (FindVtkViewWindow(myApplication->activeViewManager(), myViewWindow)) {
187 myVTKViewWindow->RemoveActor(myPreviewActor);
189 myPreviewActor->Delete();
191 myMapper->RemoveAllInputs();
199 //=================================================================================
200 // function : SMESHGUI_AddMeshElementDlg()
201 // purpose : constructor
202 //=================================================================================
203 SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI* theModule,
204 SMDSAbs_ElementType ElementType,
206 : QDialog( SMESH::GetDesktop( theModule ) ),
207 mySMESHGUI( theModule ),
208 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
211 setAttribute( Qt::WA_DeleteOnClose, true );
213 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
214 (SUIT_Session::session()->activeApplication());
216 mySimulation = new SMESH::TElementSimulation (anApp);
217 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
219 // verify nb nodes and type
221 myElementType = ElementType;
222 switch (ElementType) {
224 // if (myNbNodes != 3 && myNbNodes != 4)
228 // if (myNbNodes != 4 && myNbNodes != 8) //(nbNodes < 4 || nbNodes > 8 || nbNodes == 7)
232 myElementType = SMDSAbs_Edge;
237 if (myNbNodes == 2) {
239 myHelpFileName = "adding_nodes_and_elements_page.html#adding_edges_anchor";
241 else if (myNbNodes == 3) {
242 elemName = "TRIANGLE";
243 myHelpFileName = "adding_nodes_and_elements_page.html#adding_triangles_anchor";
245 else if (myNbNodes == 4)
246 if (myElementType == SMDSAbs_Face) {
247 elemName = "QUADRANGLE";
248 myHelpFileName = "adding_nodes_and_elements_page.html#adding_quadrangles_anchor";
252 myHelpFileName = "adding_nodes_and_elements_page.html#adding_tetrahedrons_anchor";
254 else if (myNbNodes == 8) {
256 myHelpFileName = "adding_nodes_and_elements_page.html#adding_hexahedrons_anchor";
258 else if (myElementType == SMDSAbs_Face) {
259 elemName = "POLYGON";
261 myHelpFileName = "adding_nodes_and_elements_page.html#adding_polygons_anchor";
263 else if (myElementType == SMDSAbs_Volume) {
264 myHelpFileName = "adding_nodes_and_elements_page.html#adding_polyhedrons_anchor";
267 QString iconName = tr(QString("ICON_DLG_%1").arg(elemName).toLatin1().data());
268 QString buttonGrTitle = tr(QString("SMESH_%1").arg(elemName).toLatin1().data());
269 QString caption = tr(QString("SMESH_ADD_%1_TITLE").arg(elemName).toLatin1().data());
270 QString grBoxTitle = tr(QString("SMESH_ADD_%1").arg(elemName).toLatin1().data());
272 QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", iconName));
273 QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
275 setWindowTitle(caption);
276 setSizeGripEnabled(true);
278 QVBoxLayout* aTopLayout = new QVBoxLayout(this);
279 aTopLayout->setSpacing(SPACING);
280 aTopLayout->setMargin(MARGIN);
282 /***************************************************************/
283 GroupConstructors = new QGroupBox(buttonGrTitle, this);
284 QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
285 GroupConstructorsLayout->setSpacing(SPACING);
286 GroupConstructorsLayout->setMargin(MARGIN);
288 Constructor1 = new QRadioButton(GroupConstructors);
289 Constructor1->setIcon(image0);
290 Constructor1->setChecked(true);
292 GroupConstructorsLayout->addWidget(Constructor1);
293 GroupConstructorsLayout->addStretch();
295 /***************************************************************/
296 GroupC1 = new QGroupBox(grBoxTitle, this);
297 QGridLayout* GroupC1Layout = new QGridLayout(GroupC1);
298 GroupC1Layout->setSpacing(SPACING);
299 GroupC1Layout->setMargin(MARGIN);
301 TextLabelC1A1 = new QLabel(tr("SMESH_ID_NODES"), GroupC1);
302 SelectButtonC1A1 = new QPushButton(GroupC1);
303 SelectButtonC1A1->setIcon(image1);
304 LineEditC1A1 = new QLineEdit(GroupC1);
305 // LineEditC1A1->setReadOnly(true);
307 LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this, myNbNodes));
309 Reverse = myElementType == SMDSAbs_Face ? new QCheckBox(tr("SMESH_REVERSE"), GroupC1) : 0;
311 GroupC1Layout->addWidget(TextLabelC1A1, 0, 0);
312 GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
313 GroupC1Layout->addWidget(LineEditC1A1, 0, 2);
314 if ( Reverse ) GroupC1Layout->addWidget(Reverse, 1, 0, 1, 3);
316 /***************************************************************/
317 GroupButtons = new QGroupBox(this);
318 QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
319 GroupButtonsLayout->setSpacing(SPACING);
320 GroupButtonsLayout->setMargin(MARGIN);
322 buttonOk = new QPushButton(tr("SMESH_BUT_OK"), GroupButtons);
323 buttonOk->setAutoDefault(true);
324 buttonOk->setDefault(true);
325 buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
326 buttonApply->setAutoDefault(true);
327 buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
328 buttonCancel->setAutoDefault(true);
329 buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
330 buttonHelp->setAutoDefault(true);
332 GroupButtonsLayout->addWidget(buttonOk);
333 GroupButtonsLayout->addSpacing(10);
334 GroupButtonsLayout->addWidget(buttonApply);
335 GroupButtonsLayout->addSpacing(10);
336 GroupButtonsLayout->addStretch();
337 GroupButtonsLayout->addWidget(buttonCancel);
338 GroupButtonsLayout->addWidget(buttonHelp);
340 /***************************************************************/
341 aTopLayout->addWidget(GroupConstructors);
342 aTopLayout->addWidget(GroupC1);
343 aTopLayout->addWidget(GroupButtons);
345 Init(); /* Initialisations */
348 //=================================================================================
349 // function : ~SMESHGUI_AddMeshElementDlg()
350 // purpose : Destroys the object and frees any allocated resources
351 //=================================================================================
352 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
357 //=================================================================================
360 //=================================================================================
361 void SMESHGUI_AddMeshElementDlg::Init()
364 Constructor1->setChecked(true);
365 myEditCurrentArgument = LineEditC1A1;
366 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
371 /* signals and slots connections */
372 connect(buttonOk, SIGNAL(clicked()), SLOT(ClickOnOk()));
373 connect(buttonCancel, SIGNAL(clicked()), SLOT(ClickOnCancel()));
374 connect(buttonApply, SIGNAL(clicked()), SLOT(ClickOnApply()));
375 connect(buttonHelp, SIGNAL(clicked()), SLOT(ClickOnHelp()));
377 connect(SelectButtonC1A1, SIGNAL(clicked()), SLOT(SetEditCurrentArgument()));
378 connect(LineEditC1A1, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
379 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
380 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
381 /* to close dialog if study frame change */
382 connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
385 connect(Reverse, SIGNAL(stateChanged(int)), SLOT(CheckBox(int)));
387 // set selection mode
388 SMESH::SetPointRepresentation(true);
390 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
391 aViewWindow->SetSelectionMode( NodeSelection );
395 SelectionIntoArgument();
398 //=================================================================================
399 // function : ClickOnApply()
401 //=================================================================================
402 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
404 if (myNbOkNodes && !mySMESHGUI->isActiveStudyLocked()) {
406 SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
407 anArrayOfIdeces->length(myNbNodes);
408 bool reverse = (Reverse && Reverse->isChecked());
409 QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
410 for (int i = 0; i < aListId.count(); i++)
412 anArrayOfIdeces[i] = aListId[ myNbNodes - i - 1 ].toInt();
414 anArrayOfIdeces[i] = aListId[ i ].toInt();
416 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
417 switch (myElementType) {
419 aMeshEditor->AddEdge(anArrayOfIdeces.inout()); break;
422 aMeshEditor->AddPolygonalFace(anArrayOfIdeces.inout());
424 aMeshEditor->AddFace(anArrayOfIdeces.inout());
429 aMeshEditor->AddVolume(anArrayOfIdeces.inout()); break;
433 SALOME_ListIO aList; aList.Append( myActor->getIO() );
434 mySelector->ClearIndex();
435 mySelectionMgr->setSelectedObjects( aList, false );
438 mySimulation->SetVisibility(false);
440 buttonOk->setEnabled(false);
441 buttonApply->setEnabled(false);
443 myEditCurrentArgument->setText("");
449 //=================================================================================
450 // function : ClickOnOk()
452 //=================================================================================
453 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
459 //=================================================================================
460 // function : ClickOnCancel()
462 //=================================================================================
463 void SMESHGUI_AddMeshElementDlg::ClickOnCancel()
465 //mySelectionMgr->clearSelected();
466 mySimulation->SetVisibility(false);
467 SMESH::SetPointRepresentation(false);
468 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
469 aViewWindow->SetSelectionMode( ActorSelection );
470 disconnect(mySelectionMgr, 0, this, 0);
471 mySMESHGUI->ResetState();
475 //=================================================================================
476 // function : ClickOnHelp()
478 //=================================================================================
479 void SMESHGUI_AddMeshElementDlg::ClickOnHelp()
481 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
483 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
487 platform = "winapplication";
489 platform = "application";
491 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
492 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
493 arg(app->resourceMgr()->stringValue("ExternalBrowser",
495 arg(myHelpFileName));
499 //=================================================================================
500 // function : onTextChange()
502 //=================================================================================
503 void SMESHGUI_AddMeshElementDlg::onTextChange (const QString& theNewText)
510 buttonOk->setEnabled(false);
511 buttonApply->setEnabled(false);
513 mySimulation->SetVisibility(false);
515 // hilight entered nodes
516 SMDS_Mesh* aMesh = 0;
518 aMesh = myActor->GetObject()->GetMesh();
521 TColStd_MapOfInteger newIndices;
523 QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
525 for (int i = 0; i < aListId.count(); i++) {
526 if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
528 newIndices.Add( n->GetID() );
535 mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
536 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
537 aViewWindow->highlight( myActor->getIO(), true, true );
539 myNbOkNodes = ( allOk && myNbNodes == aListId.count() );
543 if ( !allOk || myElementType != SMDSAbs_Face || aListId.count() < 3 )
546 myNbOkNodes = aListId.count();
551 buttonOk->setEnabled(true);
552 buttonApply->setEnabled(true);
559 //=================================================================================
560 // function : SelectionIntoArgument()
561 // purpose : Called when selection has changed
562 //=================================================================================
563 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
572 myEditCurrentArgument->setText("");
575 if (!GroupButtons->isEnabled()) // inactive
578 buttonOk->setEnabled(false);
579 buttonApply->setEnabled(false);
581 mySimulation->SetVisibility(false);
582 // SMESH::SetPointRepresentation(true);
586 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
588 if (aList.Extent() != 1)
591 Handle(SALOME_InteractiveObject) anIO = aList.First();
592 myMesh = SMESH::GetMeshByIO(anIO);
593 if (myMesh->_is_nil())
596 myActor = SMESH::FindActorByEntry(anIO->getEntry());
600 // get selected nodes
601 QString aString = "";
602 int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,myActor->getIO(),aString);
604 myEditCurrentArgument->setText(aString);
606 if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 ) {
608 } else if (myNbNodes != nbNodes) {
613 myNbOkNodes = nbNodes;
615 buttonOk->setEnabled(true);
616 buttonApply->setEnabled(true);
621 //=================================================================================
622 // function : displaySimulation()
624 //=================================================================================
625 void SMESHGUI_AddMeshElementDlg::displaySimulation()
627 if (myNbOkNodes && GroupButtons->isEnabled()) {
628 SMESH::TElementSimulation::TVTKIds anIds;
629 QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
630 for (int i = 0; i < aListId.count(); i++)
631 anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
633 if (Reverse && Reverse->isChecked())
634 reverse(anIds.begin(),anIds.end());
638 switch ( myElementType ) {
639 case SMDSAbs_Face : aType = VTK_POLYGON; break;
644 case 2: aType = VTK_LINE; break;
645 case 3: aType = VTK_TRIANGLE; break;
646 case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
647 case 8: aType = VTK_HEXAHEDRON; break;
652 mySimulation->SetPosition(myActor,aType,anIds);
657 //=================================================================================
658 // function : SetEditCurrentArgument()
660 //=================================================================================
661 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
663 QPushButton* send = (QPushButton*)sender();
664 if (send == SelectButtonC1A1) {
665 LineEditC1A1->setFocus();
666 myEditCurrentArgument = LineEditC1A1;
668 SelectionIntoArgument();
671 //=================================================================================
672 // function : DeactivateActiveDialog()
674 //=================================================================================
675 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
677 if (GroupConstructors->isEnabled()) {
678 GroupConstructors->setEnabled(false);
679 GroupC1->setEnabled(false);
680 GroupButtons->setEnabled(false);
681 mySimulation->SetVisibility(false);
682 mySMESHGUI->ResetState();
683 mySMESHGUI->SetActiveDialogBox(0);
687 //=================================================================================
688 // function : ActivateThisDialog()
690 //=================================================================================
691 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
693 /* Emit a signal to deactivate the active dialog */
694 mySMESHGUI->EmitSignalDeactivateDialog();
696 GroupConstructors->setEnabled(true);
697 GroupC1->setEnabled(true);
698 GroupButtons->setEnabled(true);
700 SMESH::SetPointRepresentation(true);
702 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
703 aViewWindow->SetSelectionMode( NodeSelection );
704 SelectionIntoArgument();
707 //=================================================================================
708 // function : enterEvent()
710 //=================================================================================
711 void SMESHGUI_AddMeshElementDlg::enterEvent (QEvent*)
713 if (GroupConstructors->isEnabled())
715 ActivateThisDialog();
718 //=================================================================================
719 // function : closeEvent()
721 //=================================================================================
722 void SMESHGUI_AddMeshElementDlg::closeEvent (QCloseEvent*)
724 /* same than click on cancel button */
728 //=================================================================================
729 // function : hideEvent()
730 // purpose : caused by ESC key
731 //=================================================================================
732 void SMESHGUI_AddMeshElementDlg::hideEvent (QHideEvent*)
738 //=================================================================================
739 // function : CheckBox()
741 //=================================================================================
742 void SMESHGUI_AddMeshElementDlg::CheckBox (int state)
748 mySimulation->SetVisibility(false);
753 //=================================================================================
754 // function : keyPressEvent()
756 //=================================================================================
757 void SMESHGUI_AddMeshElementDlg::keyPressEvent( QKeyEvent* e )
759 QDialog::keyPressEvent( e );
760 if ( e->isAccepted() )
763 if ( e->key() == Qt::Key_F1 ) {