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;
450 aMeshEditor->AddFace(anArrayOfIdeces.inout()); break;
452 aMeshEditor->AddVolume(anArrayOfIdeces.inout()); break;
456 SALOME_ListIO aList; aList.Append( myActor->getIO() );
457 mySelector->ClearIndex();
458 mySelectionMgr->setSelectedObjects( aList, false );
461 mySimulation->SetVisibility(false);
463 buttonOk->setEnabled(false);
464 buttonApply->setEnabled(false);
466 myEditCurrentArgument->setText("");
472 //=================================================================================
473 // function : ClickOnOk()
475 //=================================================================================
476 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
478 this->ClickOnApply();
479 this->ClickOnCancel();
483 //=================================================================================
484 // function : ClickOnCancel()
486 //=================================================================================
487 void SMESHGUI_AddMeshElementDlg::ClickOnCancel()
489 //mySelectionMgr->clearSelected();
490 mySimulation->SetVisibility(false);
491 SMESH::SetPointRepresentation(false);
492 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
493 aViewWindow->SetSelectionMode( ActorSelection );
494 disconnect(mySelectionMgr, 0, this, 0);
495 mySMESHGUI->ResetState();
500 //=================================================================================
501 // function : onTextChange()
503 //=================================================================================
504 void SMESHGUI_AddMeshElementDlg::onTextChange (const QString& theNewText)
511 buttonOk->setEnabled(false);
512 buttonApply->setEnabled(false);
514 mySimulation->SetVisibility(false);
516 // hilight entered nodes
517 SMDS_Mesh* aMesh = 0;
519 aMesh = myActor->GetObject()->GetMesh();
522 TColStd_MapOfInteger newIndices;
524 QStringList aListId = QStringList::split(" ", theNewText, false);
526 for (int i = 0; i < aListId.count(); i++) {
527 if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
529 newIndices.Add( n->GetID() );
536 mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
537 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
538 aViewWindow->highlight( myActor->getIO(), true, true );
540 myNbOkNodes = ( allOk && myNbNodes == aListId.count() );
544 if ( !allOk || myElementType != SMDSAbs_Face || aListId.count() < 3 )
547 myNbOkNodes = aListId.count();
552 buttonOk->setEnabled(true);
553 buttonApply->setEnabled(true);
560 //=================================================================================
561 // function : SelectionIntoArgument()
562 // purpose : Called when selection has changed
563 //=================================================================================
564 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
573 myEditCurrentArgument->setText("");
576 if (!GroupButtons->isEnabled()) // inactive
579 buttonOk->setEnabled(false);
580 buttonApply->setEnabled(false);
582 mySimulation->SetVisibility(false);
583 // SMESH::SetPointRepresentation(true);
587 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
589 if (aList.Extent() != 1)
592 Handle(SALOME_InteractiveObject) anIO = aList.First();
593 myMesh = SMESH::GetMeshByIO(anIO);
594 if (myMesh->_is_nil())
597 myActor = SMESH::FindActorByEntry(anIO->getEntry());
601 // get selected nodes
602 QString aString = "";
603 int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,myActor->getIO(),aString);
605 myEditCurrentArgument->setText(aString);
607 if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 ) {
609 } else if (myNbNodes != nbNodes) {
614 myNbOkNodes = nbNodes;
616 buttonOk->setEnabled(true);
617 buttonApply->setEnabled(true);
622 //=================================================================================
623 // function : displaySimulation()
625 //=================================================================================
626 void SMESHGUI_AddMeshElementDlg::displaySimulation()
628 if (myNbOkNodes && GroupButtons->isEnabled()) {
629 SMESH::TElementSimulation::TVTKIds anIds;
630 QStringList aListId = QStringList::split(" ", myEditCurrentArgument->text(), false);
631 for (int i = 0; i < aListId.count(); i++)
632 anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
634 if (Reverse && Reverse->isChecked())
635 reverse(anIds.begin(),anIds.end());
639 switch ( myElementType ) {
640 case SMDSAbs_Face : aType = VTK_POLYGON; break;
645 case 2: aType = VTK_LINE; break;
646 case 3: aType = VTK_TRIANGLE; break;
647 case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
648 case 8: aType = VTK_HEXAHEDRON; break;
653 mySimulation->SetPosition(myActor,aType,anIds);
658 //=================================================================================
659 // function : SetEditCurrentArgument()
661 //=================================================================================
662 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
664 QPushButton* send = (QPushButton*)sender();
665 if (send == SelectButtonC1A1) {
666 LineEditC1A1->setFocus();
667 myEditCurrentArgument = LineEditC1A1;
669 SelectionIntoArgument();
672 //=================================================================================
673 // function : DeactivateActiveDialog()
675 //=================================================================================
676 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
678 if (GroupConstructors->isEnabled()) {
679 GroupConstructors->setEnabled(false);
680 GroupC1->setEnabled(false);
681 GroupButtons->setEnabled(false);
682 mySimulation->SetVisibility(false);
683 mySMESHGUI->ResetState();
684 mySMESHGUI->SetActiveDialogBox(0);
688 //=================================================================================
689 // function : ActivateThisDialog()
691 //=================================================================================
692 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
694 /* Emit a signal to deactivate the active dialog */
695 mySMESHGUI->EmitSignalDeactivateDialog();
697 GroupConstructors->setEnabled(true);
698 GroupC1->setEnabled(true);
699 GroupButtons->setEnabled(true);
701 SMESH::SetPointRepresentation(true);
703 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
704 aViewWindow->SetSelectionMode( NodeSelection );
705 SelectionIntoArgument();
708 //=================================================================================
709 // function : enterEvent()
711 //=================================================================================
712 void SMESHGUI_AddMeshElementDlg::enterEvent (QEvent*)
714 if (GroupConstructors->isEnabled())
716 ActivateThisDialog();
720 //=================================================================================
721 // function : closeEvent()
723 //=================================================================================
724 void SMESHGUI_AddMeshElementDlg::closeEvent (QCloseEvent*)
726 /* same than click on cancel button */
727 this->ClickOnCancel();
731 //=================================================================================
732 // function : hideEvent()
733 // purpose : caused by ESC key
734 //=================================================================================
735 void SMESHGUI_AddMeshElementDlg::hideEvent (QHideEvent*)
741 //=================================================================================
742 // function : CheckBox()
744 //=================================================================================
745 void SMESHGUI_AddMeshElementDlg::CheckBox (int state)
751 mySimulation->SetVisibility(false);