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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESHGUI_AddMeshElementDlg.cxx
25 // Author : Nicolas REJNERI
29 #include "SMESHGUI_AddMeshElementDlg.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34 #include "SMESHGUI_MeshUtils.h"
35 #include "SMESHGUI_IdValidator.h"
36 #include "SMESH_ActorUtils.h"
38 #include "SMDS_Mesh.hxx"
39 #include "SMESH_Actor.h"
41 #include "SUIT_Session.h"
43 #include "SVTK_Selection.h"
44 #include "SVTK_Selector.h"
45 #include "SALOME_ListIO.hxx"
46 #include "SALOME_ListIteratorOfListIO.hxx"
48 #include "SalomeApp_Study.h"
49 #include "SalomeApp_Application.h"
51 #include "SVTK_ViewModel.h"
52 #include "SVTK_ViewWindow.h"
54 #include "utilities.h"
57 #include <TColStd_MapOfInteger.hxx>
58 #include <TColStd_IndexedMapOfInteger.hxx>
62 #include <vtkIdList.h>
63 #include <vtkIntArray.h>
64 #include <vtkCellArray.h>
65 #include <vtkUnsignedCharArray.h>
66 #include <vtkUnstructuredGrid.h>
67 #include <vtkDataSetMapper.h>
68 #include <vtkProperty.h>
71 #include <qbuttongroup.h>
72 #include <qgroupbox.h>
74 #include <qlineedit.h>
75 #include <qpushbutton.h>
76 #include <qradiobutton.h>
80 #include <qwhatsthis.h>
83 #include <qcheckbox.h>
93 class TElementSimulation {
94 SalomeApp_Application* myApplication;
95 SUIT_ViewWindow* myViewWindow;
96 SVTK_ViewWindow* myVTKViewWindow;
98 SALOME_Actor* myPreviewActor;
99 vtkDataSetMapper* myMapper;
100 vtkUnstructuredGrid* myGrid;
103 TElementSimulation (SalomeApp_Application* theApplication)
105 myApplication = theApplication;
106 SUIT_ViewManager* mgr = theApplication->activeViewManager();
108 myViewWindow = mgr->getActiveView();
109 myVTKViewWindow = GetVtkViewWindow(myViewWindow);
111 myGrid = vtkUnstructuredGrid::New();
113 // Create and display actor
114 myMapper = vtkDataSetMapper::New();
115 myMapper->SetInput(myGrid);
117 myPreviewActor = SALOME_Actor::New();
118 myPreviewActor->PickableOff();
119 myPreviewActor->VisibilityOff();
120 myPreviewActor->SetMapper(myMapper);
123 vtkProperty* aProp = vtkProperty::New();
124 GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
125 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
126 myPreviewActor->SetProperty( aProp );
129 vtkProperty* aBackProp = vtkProperty::New();
130 GetColor( "SMESH", "backface_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 0, 255 ) );
131 aBackProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
132 myPreviewActor->SetBackfaceProperty( aBackProp );
135 myVTKViewWindow->AddActor(myPreviewActor);
138 typedef std::vector<vtkIdType> TVTKIds;
139 void SetPosition (SMESH_Actor* theActor,
141 const TVTKIds& theIds)
143 vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
144 myGrid->SetPoints(aGrid->GetPoints());
146 const int* aConn = NULL;
150 static int anIds[] = {0,2,1,3};
156 static int anIds[] = {0,3,2,1,4};
162 static int anIds[] = {0,3,2,1,4,7,6,5};
169 vtkIdList *anIds = vtkIdList::New();
172 for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
173 anIds->InsertId(i,theIds[aConn[i]]);
175 for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
176 anIds->InsertId(i,theIds[i]);
178 myGrid->InsertNextCell(theType,anIds);
187 void SetVisibility (bool theVisibility)
189 myPreviewActor->SetVisibility(theVisibility);
190 RepaintCurrentView();
194 ~TElementSimulation()
196 if (FindVtkViewWindow(myApplication->activeViewManager(), myViewWindow)) {
197 myVTKViewWindow->RemoveActor(myPreviewActor);
199 myPreviewActor->Delete();
201 myMapper->RemoveAllInputs();
209 //=================================================================================
210 // function : SMESHGUI_AddMeshElementDlg()
211 // purpose : constructor
212 //=================================================================================
213 SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI* theModule,
215 SMDSAbs_ElementType ElementType, int nbNodes,
216 bool modal, WFlags fl)
217 : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder |
218 WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),
219 mySMESHGUI( theModule ),
220 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
222 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
223 (SUIT_Session::session()->activeApplication());
225 mySimulation = new SMESH::TElementSimulation (anApp);
226 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
228 // verify nb nodes and type
230 myElementType = ElementType;
231 switch (ElementType) {
233 // if (myNbNodes != 3 && myNbNodes != 4)
237 // if (myNbNodes != 4 && myNbNodes != 8) //(nbNodes < 4 || nbNodes > 8 || nbNodes == 7)
241 myElementType = SMDSAbs_Edge;
248 else if (myNbNodes == 3)
249 elemName = "TRIANGLE";
250 else if (myNbNodes == 4)
251 if (myElementType == SMDSAbs_Face)
252 elemName = "QUADRANGLE";
255 else if (myNbNodes == 8)
257 else if (myElementType == SMDSAbs_Face){
258 elemName = "POLYGON";
262 QString iconName = tr(QString("ICON_DLG_%1").arg(elemName));
263 QString buttonGrTitle = tr(QString("SMESH_%1").arg(elemName));
264 QString caption = tr(QString("SMESH_ADD_%1_TITLE").arg(elemName));
265 QString grBoxTitle = tr(QString("SMESH_ADD_%1").arg(elemName));
267 QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", iconName));
268 QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
271 setName("SMESHGUI_AddMeshElementDlg");
275 setSizeGripEnabled(TRUE);
276 SMESHGUI_AddMeshElementDlgLayout = new QGridLayout(this);
277 SMESHGUI_AddMeshElementDlgLayout->setSpacing(6);
278 SMESHGUI_AddMeshElementDlgLayout->setMargin(11);
280 /***************************************************************/
281 GroupConstructors = new QButtonGroup(this, "GroupConstructors");
282 GroupConstructors->setTitle(buttonGrTitle);
284 GroupConstructors->setExclusive(TRUE);
285 GroupConstructors->setColumnLayout(0, Qt::Vertical);
286 GroupConstructors->layout()->setSpacing(0);
287 GroupConstructors->layout()->setMargin(0);
288 GroupConstructorsLayout = new QGridLayout(GroupConstructors->layout());
289 GroupConstructorsLayout->setAlignment(Qt::AlignTop);
290 GroupConstructorsLayout->setSpacing(6);
291 GroupConstructorsLayout->setMargin(11);
292 Constructor1 = new QRadioButton(GroupConstructors, "Constructor1");
293 Constructor1->setText(tr("" ));
294 Constructor1->setPixmap(image0);
295 Constructor1->setChecked(TRUE);
296 Constructor1->setSizePolicy(QSizePolicy((QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth()));
297 Constructor1->setMinimumSize(QSize(50, 0));
298 GroupConstructorsLayout->addWidget(Constructor1, 0, 0);
299 QSpacerItem* spacer = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
300 GroupConstructorsLayout->addItem(spacer, 0, 1);
301 SMESHGUI_AddMeshElementDlgLayout->addWidget(GroupConstructors, 0, 0);
303 /***************************************************************/
304 GroupButtons = new QGroupBox(this, "GroupButtons");
305 GroupButtons->setGeometry(QRect(10, 10, 281, 48));
306 GroupButtons->setTitle(tr("" ));
307 GroupButtons->setColumnLayout(0, Qt::Vertical);
308 GroupButtons->layout()->setSpacing(0);
309 GroupButtons->layout()->setMargin(0);
310 GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
311 GroupButtonsLayout->setAlignment(Qt::AlignTop);
312 GroupButtonsLayout->setSpacing(6);
313 GroupButtonsLayout->setMargin(11);
314 buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
315 buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
316 buttonCancel->setAutoDefault(TRUE);
317 GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
318 buttonApply = new QPushButton(GroupButtons, "buttonApply");
319 buttonApply->setText(tr("SMESH_BUT_APPLY" ));
320 buttonApply->setAutoDefault(TRUE);
321 GroupButtonsLayout->addWidget(buttonApply, 0, 1);
322 QSpacerItem* spacer_9 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
323 GroupButtonsLayout->addItem(spacer_9, 0, 2);
324 buttonOk = new QPushButton(GroupButtons, "buttonOk");
325 buttonOk->setText(tr("SMESH_BUT_OK" ));
326 buttonOk->setAutoDefault(TRUE);
327 buttonOk->setDefault(TRUE);
328 GroupButtonsLayout->addWidget(buttonOk, 0, 0);
329 SMESHGUI_AddMeshElementDlgLayout->addWidget(GroupButtons, 2, 0);
331 /***************************************************************/
332 GroupC1 = new QGroupBox(this, "GroupC1");
333 GroupC1->setTitle(grBoxTitle);
335 GroupC1->setMinimumSize(QSize(0, 0));
336 GroupC1->setFrameShape(QGroupBox::Box);
337 GroupC1->setFrameShadow(QGroupBox::Sunken);
338 GroupC1->setColumnLayout(0, Qt::Vertical);
339 GroupC1->layout()->setSpacing(0);
340 GroupC1->layout()->setMargin(0);
341 GroupC1Layout = new QGridLayout(GroupC1->layout());
342 GroupC1Layout->setAlignment(Qt::AlignTop);
343 GroupC1Layout->setSpacing(6);
344 GroupC1Layout->setMargin(11);
345 TextLabelC1A1 = new QLabel(GroupC1, "TextLabelC1A1");
346 TextLabelC1A1->setText(tr("SMESH_ID_NODES" ));
347 TextLabelC1A1->setMinimumSize(QSize(50, 0));
348 TextLabelC1A1->setFrameShape(QLabel::NoFrame);
349 TextLabelC1A1->setFrameShadow(QLabel::Plain);
350 GroupC1Layout->addWidget(TextLabelC1A1, 0, 0);
351 SelectButtonC1A1 = new QPushButton(GroupC1, "SelectButtonC1A1");
352 SelectButtonC1A1->setText(tr("" ));
353 SelectButtonC1A1->setPixmap(image1);
354 SelectButtonC1A1->setToggleButton(FALSE);
355 GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
356 LineEditC1A1 = new QLineEdit(GroupC1, "LineEditC1A1");
357 // LineEditC1A1->setReadOnly(TRUE);
359 LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this, "validator", myNbNodes));
360 GroupC1Layout->addWidget(LineEditC1A1, 0, 2);
362 if (myElementType == SMDSAbs_Face) {
363 Reverse = new QCheckBox(GroupC1, "Reverse");
364 Reverse->setText(tr("SMESH_REVERSE" ));
365 GroupC1Layout->addWidget(Reverse, 1, 0);
370 SMESHGUI_AddMeshElementDlgLayout->addWidget(GroupC1, 1, 0);
372 Init(); /* Initialisations */
375 //=================================================================================
376 // function : ~SMESHGUI_AddMeshElementDlg()
377 // purpose : Destroys the object and frees any allocated resources
378 //=================================================================================
379 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
381 // no need to delete child widgets, Qt does it all for us
385 //=================================================================================
388 //=================================================================================
389 void SMESHGUI_AddMeshElementDlg::Init()
392 Constructor1->setChecked(TRUE);
393 myEditCurrentArgument = LineEditC1A1;
394 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
399 /* signals and slots connections */
400 connect(buttonOk, SIGNAL(clicked()), SLOT(ClickOnOk()));
401 connect(buttonCancel, SIGNAL(clicked()), SLOT(ClickOnCancel()));
402 connect(buttonApply, SIGNAL(clicked()), SLOT(ClickOnApply()));
404 connect(SelectButtonC1A1, SIGNAL(clicked()), SLOT(SetEditCurrentArgument()));
405 connect(LineEditC1A1, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
406 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
407 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
408 /* to close dialog if study frame change */
409 connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
412 connect(Reverse, SIGNAL(stateChanged(int)), SLOT(CheckBox(int)));
414 this->show(); // displays Dialog
416 // set selection mode
417 SMESH::SetPointRepresentation(true);
419 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
420 aViewWindow->SetSelectionMode( NodeSelection );
424 SelectionIntoArgument();
427 //=================================================================================
428 // function : ClickOnApply()
430 //=================================================================================
431 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
433 if (myNbOkNodes && !mySMESHGUI->isActiveStudyLocked()) {
435 SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
436 anArrayOfIdeces->length(myNbNodes);
437 bool reverse = (Reverse && Reverse->isChecked());
438 QStringList aListId = QStringList::split(" ", myEditCurrentArgument->text(), false);
439 for (int i = 0; i < aListId.count(); i++)
441 anArrayOfIdeces[i] = aListId[ myNbNodes - i - 1 ].toInt();
443 anArrayOfIdeces[i] = aListId[ i ].toInt();
445 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
446 switch (myElementType) {
448 aMeshEditor->AddEdge(anArrayOfIdeces.inout()); break;
451 aMeshEditor->AddPolygonalFace(anArrayOfIdeces.inout());
453 aMeshEditor->AddFace(anArrayOfIdeces.inout());
458 aMeshEditor->AddVolume(anArrayOfIdeces.inout()); break;
462 SALOME_ListIO aList; aList.Append( myActor->getIO() );
463 mySelector->ClearIndex();
464 mySelectionMgr->setSelectedObjects( aList, false );
467 mySimulation->SetVisibility(false);
469 buttonOk->setEnabled(false);
470 buttonApply->setEnabled(false);
472 myEditCurrentArgument->setText("");
478 //=================================================================================
479 // function : ClickOnOk()
481 //=================================================================================
482 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
484 this->ClickOnApply();
485 this->ClickOnCancel();
489 //=================================================================================
490 // function : ClickOnCancel()
492 //=================================================================================
493 void SMESHGUI_AddMeshElementDlg::ClickOnCancel()
495 //mySelectionMgr->clearSelected();
496 mySimulation->SetVisibility(false);
497 SMESH::SetPointRepresentation(false);
498 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
499 aViewWindow->SetSelectionMode( ActorSelection );
500 disconnect(mySelectionMgr, 0, this, 0);
501 mySMESHGUI->ResetState();
506 //=================================================================================
507 // function : onTextChange()
509 //=================================================================================
510 void SMESHGUI_AddMeshElementDlg::onTextChange (const QString& theNewText)
517 buttonOk->setEnabled(false);
518 buttonApply->setEnabled(false);
520 mySimulation->SetVisibility(false);
522 // hilight entered nodes
523 SMDS_Mesh* aMesh = 0;
525 aMesh = myActor->GetObject()->GetMesh();
528 TColStd_MapOfInteger newIndices;
530 QStringList aListId = QStringList::split(" ", theNewText, false);
532 for (int i = 0; i < aListId.count(); i++) {
533 if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
535 newIndices.Add( n->GetID() );
542 mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
543 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
544 aViewWindow->highlight( myActor->getIO(), true, true );
546 myNbOkNodes = ( allOk && myNbNodes == aListId.count() );
550 if ( !allOk || myElementType != SMDSAbs_Face || aListId.count() < 3 )
553 myNbOkNodes = aListId.count();
558 buttonOk->setEnabled(true);
559 buttonApply->setEnabled(true);
566 //=================================================================================
567 // function : SelectionIntoArgument()
568 // purpose : Called when selection has changed
569 //=================================================================================
570 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
579 myEditCurrentArgument->setText("");
582 if (!GroupButtons->isEnabled()) // inactive
585 buttonOk->setEnabled(false);
586 buttonApply->setEnabled(false);
588 mySimulation->SetVisibility(false);
589 // SMESH::SetPointRepresentation(true);
593 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
595 if (aList.Extent() != 1)
598 Handle(SALOME_InteractiveObject) anIO = aList.First();
599 myMesh = SMESH::GetMeshByIO(anIO);
600 if (myMesh->_is_nil())
603 myActor = SMESH::FindActorByEntry(anIO->getEntry());
607 // get selected nodes
608 QString aString = "";
609 int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,myActor->getIO(),aString);
611 myEditCurrentArgument->setText(aString);
613 if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 ) {
615 } else if (myNbNodes != nbNodes) {
620 myNbOkNodes = nbNodes;
622 buttonOk->setEnabled(true);
623 buttonApply->setEnabled(true);
628 //=================================================================================
629 // function : displaySimulation()
631 //=================================================================================
632 void SMESHGUI_AddMeshElementDlg::displaySimulation()
634 if (myNbOkNodes && GroupButtons->isEnabled()) {
635 SMESH::TElementSimulation::TVTKIds anIds;
636 QStringList aListId = QStringList::split(" ", myEditCurrentArgument->text(), false);
637 for (int i = 0; i < aListId.count(); i++)
638 anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
640 if (Reverse && Reverse->isChecked())
641 reverse(anIds.begin(),anIds.end());
645 switch ( myElementType ) {
646 case SMDSAbs_Face : aType = VTK_POLYGON; break;
651 case 2: aType = VTK_LINE; break;
652 case 3: aType = VTK_TRIANGLE; break;
653 case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
654 case 8: aType = VTK_HEXAHEDRON; break;
659 mySimulation->SetPosition(myActor,aType,anIds);
664 //=================================================================================
665 // function : SetEditCurrentArgument()
667 //=================================================================================
668 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
670 QPushButton* send = (QPushButton*)sender();
671 if (send == SelectButtonC1A1) {
672 LineEditC1A1->setFocus();
673 myEditCurrentArgument = LineEditC1A1;
675 SelectionIntoArgument();
678 //=================================================================================
679 // function : DeactivateActiveDialog()
681 //=================================================================================
682 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
684 if (GroupConstructors->isEnabled()) {
685 GroupConstructors->setEnabled(false);
686 GroupC1->setEnabled(false);
687 GroupButtons->setEnabled(false);
688 mySimulation->SetVisibility(false);
689 mySMESHGUI->ResetState();
690 mySMESHGUI->SetActiveDialogBox(0);
694 //=================================================================================
695 // function : ActivateThisDialog()
697 //=================================================================================
698 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
700 /* Emit a signal to deactivate the active dialog */
701 mySMESHGUI->EmitSignalDeactivateDialog();
703 GroupConstructors->setEnabled(true);
704 GroupC1->setEnabled(true);
705 GroupButtons->setEnabled(true);
707 SMESH::SetPointRepresentation(true);
709 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
710 aViewWindow->SetSelectionMode( NodeSelection );
711 SelectionIntoArgument();
714 //=================================================================================
715 // function : enterEvent()
717 //=================================================================================
718 void SMESHGUI_AddMeshElementDlg::enterEvent (QEvent*)
720 if (GroupConstructors->isEnabled())
722 ActivateThisDialog();
726 //=================================================================================
727 // function : closeEvent()
729 //=================================================================================
730 void SMESHGUI_AddMeshElementDlg::closeEvent (QCloseEvent*)
732 /* same than click on cancel button */
733 this->ClickOnCancel();
737 //=================================================================================
738 // function : hideEvent()
739 // purpose : caused by ESC key
740 //=================================================================================
741 void SMESHGUI_AddMeshElementDlg::hideEvent (QHideEvent*)
747 //=================================================================================
748 // function : CheckBox()
750 //=================================================================================
751 void SMESHGUI_AddMeshElementDlg::CheckBox (int state)
757 mySimulation->SetVisibility(false);