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 "VTKViewer_ViewFrame.h"
39 #include "SMDS_Mesh.hxx"
40 #include "SMESH_Actor.h"
42 #include "QAD_Application.h"
43 #include "QAD_Desktop.h"
44 #include "QAD_MessageBox.h"
46 #include "utilities.h"
49 #include <vtkIdList.h>
50 #include <vtkIntArray.h>
51 #include <vtkCellArray.h>
52 #include <vtkUnsignedCharArray.h>
53 #include <vtkUnstructuredGrid.h>
54 #include <vtkDataSetMapper.h>
57 #include <qbuttongroup.h>
58 #include <qgroupbox.h>
60 #include <qlineedit.h>
61 #include <qpushbutton.h>
62 #include <qradiobutton.h>
66 #include <qwhatsthis.h>
69 #include <qcheckbox.h>
73 #include <TColStd_IndexedMapOfInteger.hxx>
79 class TElementSimulation{
81 QAD_StudyFrame* myStudyFrame;
82 VTKViewer_ViewFrame* myViewFrame;
84 SALOME_Actor *myPreviewActor;
85 vtkDataSetMapper* myMapper;
86 vtkUnstructuredGrid* myGrid;
90 TElementSimulation(QAD_Study* theStudy):
92 myStudyFrame(theStudy->getActiveStudyFrame()),
93 myViewFrame(GetVtkViewFrame(theStudy->getActiveStudyFrame()))
95 myGrid = vtkUnstructuredGrid::New();
97 // Create and display actor
98 myMapper = vtkDataSetMapper::New();
99 myMapper->SetInput( myGrid );
101 myPreviewActor = SALOME_Actor::New();
102 myPreviewActor->PickableOff();
103 myPreviewActor->VisibilityOff();
104 myPreviewActor->SetMapper( myMapper );
106 vtkProperty* aProp = vtkProperty::New();
108 anRGB[0] = GetFloat("SMESH:SettingsFillColorRed", 0)/255.;
109 anRGB[1] = GetFloat("SMESH:SettingsFillColorGreen", 170)/255.;
110 anRGB[2] = GetFloat("SMESH:SettingsFillColorBlue", 255)/255.;
111 aProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
112 myPreviewActor->SetProperty( aProp );
115 vtkProperty* aBackProp = vtkProperty::New();
116 anRGB[0] = GetFloat("SMESH:SettingsBackFaceColorRed", 0)/255.;
117 anRGB[1] = GetFloat("SMESH:SettingsBackFaceColorGreen", 0)/255.;
118 anRGB[2] = GetFloat("SMESH:SettingsBackFaceColorBlue", 255)/255.;
119 aBackProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
120 myPreviewActor->SetBackfaceProperty( aBackProp );
123 myViewFrame->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;
139 static int anIds[] = {0,2,1,3};
144 static int anIds[] = {0,3,2,1,4};
148 case VTK_HEXAHEDRON:{
149 static int anIds[] = {0,3,2,1,4,7,6,5};
155 vtkIdList *anIds = vtkIdList::New();
158 for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
159 anIds->InsertId(i,theIds[aConn[i]]);
161 for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
162 anIds->InsertId(i,theIds[i]);
164 myGrid->InsertNextCell(theType,anIds);
173 void SetVisibility(bool theVisibility){
174 myPreviewActor->SetVisibility(theVisibility);
175 RepaintCurrentView();
179 ~TElementSimulation(){
180 if(FindVtkViewFrame(myStudy,myStudyFrame)){
181 myViewFrame->RemoveActor(myPreviewActor);
183 myPreviewActor->Delete();
185 myMapper->RemoveAllInputs();
195 //=================================================================================
196 // class : SMESHGUI_AddMeshElementDlg()
198 //=================================================================================
199 SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( QWidget* parent, const char* name,
200 SALOME_Selection* Sel,
201 SMDSAbs_ElementType ElementType, int nbNodes,
202 bool modal, WFlags fl )
203 : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu |
204 Qt::WDestructiveClose)
206 mySimulation = new SMESH::TElementSimulation(SMESH::GetActiveStudy());
208 // verify nb nodes and type
210 myElementType = ElementType;
211 switch ( ElementType ) {
213 if ( myNbNodes != 3 && myNbNodes != 4 )
217 if ( myNbNodes != 4 && myNbNodes != 8 ) //(nbNodes < 4 || nbNodes > 8 || nbNodes == 7)
221 myElementType = SMDSAbs_Edge;
226 switch ( myNbNodes ) {
227 case 2: elemName = "EDGE"; break;
228 case 3: elemName = "TRIANGLE"; break;
230 myElementType == SMDSAbs_Face ? elemName = "QUADRANGLE" : elemName = "TETRAS"; break;
236 QString iconName = tr( QString("ICON_DLG_%1").arg(elemName) );
237 QString buttonGrTitle = tr( QString("SMESH_%1").arg(elemName) );
238 QString caption = tr( QString("SMESH_ADD_%1_TITLE").arg(elemName) );
239 QString grBoxTitle = tr( QString("SMESH_ADD_%1").arg(elemName) );
241 QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH", iconName ));
242 QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH", tr("ICON_SELECT")));
245 setName( "SMESHGUI_AddMeshElementDlg" );
247 setCaption( caption );
249 setSizeGripEnabled( TRUE );
250 SMESHGUI_AddMeshElementDlgLayout = new QGridLayout( this );
251 SMESHGUI_AddMeshElementDlgLayout->setSpacing( 6 );
252 SMESHGUI_AddMeshElementDlgLayout->setMargin( 11 );
254 /***************************************************************/
255 GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
256 GroupConstructors->setTitle( buttonGrTitle );
258 GroupConstructors->setExclusive( TRUE );
259 GroupConstructors->setColumnLayout(0, Qt::Vertical );
260 GroupConstructors->layout()->setSpacing( 0 );
261 GroupConstructors->layout()->setMargin( 0 );
262 GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
263 GroupConstructorsLayout->setAlignment( Qt::AlignTop );
264 GroupConstructorsLayout->setSpacing( 6 );
265 GroupConstructorsLayout->setMargin( 11 );
266 Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
267 Constructor1->setText( tr( "" ) );
268 Constructor1->setPixmap( image0 );
269 Constructor1->setChecked( TRUE );
270 Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
271 Constructor1->setMinimumSize( QSize( 50, 0 ) );
272 GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
273 QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
274 GroupConstructorsLayout->addItem( spacer, 0, 1 );
275 SMESHGUI_AddMeshElementDlgLayout->addWidget( GroupConstructors, 0, 0 );
277 /***************************************************************/
278 GroupButtons = new QGroupBox( this, "GroupButtons" );
279 GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) );
280 GroupButtons->setTitle( tr( "" ) );
281 GroupButtons->setColumnLayout(0, Qt::Vertical );
282 GroupButtons->layout()->setSpacing( 0 );
283 GroupButtons->layout()->setMargin( 0 );
284 GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
285 GroupButtonsLayout->setAlignment( Qt::AlignTop );
286 GroupButtonsLayout->setSpacing( 6 );
287 GroupButtonsLayout->setMargin( 11 );
288 buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
289 buttonCancel->setText( tr( "SMESH_BUT_CLOSE" ) );
290 buttonCancel->setAutoDefault( TRUE );
291 GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
292 buttonApply = new QPushButton( GroupButtons, "buttonApply" );
293 buttonApply->setText( tr( "SMESH_BUT_APPLY" ) );
294 buttonApply->setAutoDefault( TRUE );
295 GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
296 QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
297 GroupButtonsLayout->addItem( spacer_9, 0, 2 );
298 buttonOk = new QPushButton( GroupButtons, "buttonOk" );
299 buttonOk->setText( tr( "SMESH_BUT_OK" ) );
300 buttonOk->setAutoDefault( TRUE );
301 buttonOk->setDefault( TRUE );
302 GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
303 SMESHGUI_AddMeshElementDlgLayout->addWidget( GroupButtons, 2, 0 );
305 /***************************************************************/
306 GroupC1 = new QGroupBox( this, "GroupC1" );
307 GroupC1->setTitle( grBoxTitle );
309 GroupC1->setMinimumSize( QSize( 0, 0 ) );
310 GroupC1->setFrameShape( QGroupBox::Box );
311 GroupC1->setFrameShadow( QGroupBox::Sunken );
312 GroupC1->setColumnLayout(0, Qt::Vertical );
313 GroupC1->layout()->setSpacing( 0 );
314 GroupC1->layout()->setMargin( 0 );
315 GroupC1Layout = new QGridLayout( GroupC1->layout() );
316 GroupC1Layout->setAlignment( Qt::AlignTop );
317 GroupC1Layout->setSpacing( 6 );
318 GroupC1Layout->setMargin( 11 );
319 TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
320 TextLabelC1A1->setText( tr( "SMESH_ID_NODES" ) );
321 TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
322 TextLabelC1A1->setFrameShape( QLabel::NoFrame );
323 TextLabelC1A1->setFrameShadow( QLabel::Plain );
324 GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
325 SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
326 SelectButtonC1A1->setText( tr( "" ) );
327 SelectButtonC1A1->setPixmap( image1 );
328 SelectButtonC1A1->setToggleButton( FALSE );
329 GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
330 LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
331 // LineEditC1A1->setReadOnly( TRUE );
332 LineEditC1A1->setValidator( new SMESHGUI_IdValidator( this, "validator", myNbNodes ));
333 GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
335 if ( myElementType == SMDSAbs_Face ) {
336 Reverse = new QCheckBox( GroupC1, "Reverse" );
337 Reverse->setText( tr( "SMESH_REVERSE" ) );
338 GroupC1Layout->addWidget( Reverse, 1, 0 );
343 SMESHGUI_AddMeshElementDlgLayout->addWidget( GroupC1, 1, 0 );
345 Init(Sel) ; /* Initialisations */
348 //=================================================================================
349 // function : ~SMESHGUI_AddMeshElementDlg()
350 // purpose : Destroys the object and frees any allocated resources
351 //=================================================================================
352 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
354 // no need to delete child widgets, Qt does it all for us
359 //=================================================================================
362 //=================================================================================
363 void SMESHGUI_AddMeshElementDlg::Init( SALOME_Selection* Sel )
367 Constructor1->setChecked( TRUE );
368 myEditCurrentArgument = LineEditC1A1 ;
370 mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
371 mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
376 /* signals and slots connections */
377 connect(buttonOk, SIGNAL( clicked() ), SLOT( ClickOnOk() ) );
378 connect(buttonCancel, SIGNAL( clicked() ), SLOT( ClickOnCancel() ) ) ;
379 connect(buttonApply, SIGNAL( clicked() ), SLOT(ClickOnApply() ) );
381 connect(SelectButtonC1A1, SIGNAL( clicked() ), SLOT( SetEditCurrentArgument() ) ) ;
382 connect(LineEditC1A1, SIGNAL( textChanged(const QString&) ), SLOT(onTextChange(const QString&)));
383 connect(mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), SLOT( DeactivateActiveDialog()));
384 connect(mySelection, SIGNAL( currentSelectionChanged() ), SLOT( SelectionIntoArgument() ) );
385 /* to close dialog if study frame change */
386 connect( mySMESHGUI, SIGNAL ( SignalStudyFrameChanged() ), SLOT( ClickOnCancel() ) ) ;
389 connect( Reverse, SIGNAL(stateChanged(int)), SLOT(CheckBox(int)) );
391 /* Move widget on the botton right corner of main widget */
393 mySMESHGUI->DefineDlgPosition( this, x, y ) ;
395 this->show() ; /* displays Dialog */
397 // set selection mode
398 SMESH::SetPointRepresentation(true);
399 QAD_Application::getDesktop()->SetSelectionMode( NodeSelection, true );
403 SelectionIntoArgument();
406 //=================================================================================
407 // function : ClickOnApply()
409 //=================================================================================
410 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
412 if ( myOkNodes && !mySMESHGUI->ActiveStudyLocked() ) {
415 SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
416 anArrayOfIdeces->length( myNbNodes );
417 bool reverse = ( Reverse && Reverse->isChecked() );
418 QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text(), false);
419 for ( int i = 0; i < aListId.count(); i++ )
421 anArrayOfIdeces[i] = aListId[ myNbNodes - i - 1 ].toInt();
423 anArrayOfIdeces[i] = aListId[ i ].toInt();
425 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
426 switch ( myElementType ) {
428 aMeshEditor->AddEdge(anArrayOfIdeces.inout()); break;
430 aMeshEditor->AddFace(anArrayOfIdeces.inout()); break;
432 aMeshEditor->AddVolume(anArrayOfIdeces.inout()); break;
436 mySelection->ClearIObjects();
437 mySelection->AddIObject( myActor->getIO(), false );
440 mySimulation->SetVisibility(false);
442 buttonOk->setEnabled( false );
443 buttonApply->setEnabled( false );
445 myEditCurrentArgument->setText("");
451 //=================================================================================
452 // function : ClickOnOk()
454 //=================================================================================
455 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
457 this->ClickOnApply() ;
458 this->ClickOnCancel() ;
462 //=================================================================================
463 // function : ClickOnCancel()
465 //=================================================================================
466 void SMESHGUI_AddMeshElementDlg::ClickOnCancel()
468 mySelection->ClearIObjects();
469 mySimulation->SetVisibility(false);
470 SMESH::SetPointRepresentation(false);
471 QAD_Application::getDesktop()->SetSelectionMode( ActorSelection );
472 disconnect( mySelection, 0, this, 0 );
473 mySMESHGUI->ResetState() ;
478 //=======================================================================
479 //function : onTextChange
481 //=======================================================================
483 void SMESHGUI_AddMeshElementDlg::onTextChange(const QString& theNewText)
485 if ( myBusy ) return;
490 buttonOk->setEnabled( false );
491 buttonApply->setEnabled( false );
493 mySimulation->SetVisibility(false);
495 // hilight entered nodes
496 SMDS_Mesh* aMesh = 0;
498 aMesh = myActor->GetObject()->GetMesh();
501 mySelection->ClearIObjects();
502 mySelection->AddIObject( myActor->getIO() );
504 QStringList aListId = QStringList::split( " ", theNewText, false);
506 for ( int i = 0; i < aListId.count(); i++ ) {
507 const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() );
509 if ( mySelection->IsIndexSelected( myActor->getIO(), n->GetID() ))
512 mySelection->AddOrRemoveIndex (myActor->getIO(), n->GetID(), true);
517 myOkNodes = (allOk && myNbNodes == aListId.count() );
520 buttonOk->setEnabled( true );
521 buttonApply->setEnabled( true );
529 //=================================================================================
530 // function : SelectionIntoArgument()
531 // purpose : Called when selection has changed
532 //=================================================================================
533 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
535 if ( myBusy ) return;
543 myEditCurrentArgument->setText( "" );
546 if ( !GroupButtons->isEnabled() ) // inactive
549 buttonOk->setEnabled( false );
550 buttonApply->setEnabled( false );
552 mySimulation->SetVisibility(false);
553 // SMESH::SetPointRepresentation(true);
557 int nbSel = mySelection->IObjectCount();
561 myMesh = SMESH::GetMeshByIO( mySelection->firstIObject() );
562 if ( myMesh->_is_nil() )
565 myActor = SMESH::FindActorByEntry( mySelection->firstIObject()->getEntry() );
569 // get selected nodes
571 QString aString = "";
572 int nbNodes = SMESH::GetNameOfSelectedNodes(mySelection, aString) ;
574 myEditCurrentArgument->setText( aString );
576 if ( myNbNodes != nbNodes )
583 buttonOk->setEnabled( true );
584 buttonApply->setEnabled( true );
589 //=======================================================================
590 //function : displaySimulation
592 //=======================================================================
594 void SMESHGUI_AddMeshElementDlg::displaySimulation()
596 if ( myOkNodes && GroupButtons->isEnabled() )
598 SMESH::TElementSimulation::TVTKIds anIds;
599 QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text(), false);
600 for ( int i = 0; i < aListId.count(); i++ )
601 anIds.push_back( myActor->GetObject()->GetNodeVTKId( aListId[ i ].toInt() ));
603 if ( Reverse && Reverse->isChecked() )
604 reverse(anIds.begin(),anIds.end());
607 switch ( myNbNodes ) {
608 case 2: aType = VTK_LINE; break;
609 case 3: aType = VTK_TRIANGLE; break;
610 case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
611 case 8: aType = VTK_HEXAHEDRON; break;
615 mySimulation->SetPosition(myActor,aType,anIds);
620 //=================================================================================
621 // function : SetEditCurrentArgument()
623 //=================================================================================
624 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
626 QPushButton* send = (QPushButton*)sender();
627 if(send == SelectButtonC1A1) {
628 LineEditC1A1->setFocus() ;
629 myEditCurrentArgument = LineEditC1A1;
631 SelectionIntoArgument() ;
634 //=================================================================================
635 // function : DeactivateActiveDialog()
637 //=================================================================================
638 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
640 if ( GroupConstructors->isEnabled() ) {
641 GroupConstructors->setEnabled(false) ;
642 GroupC1->setEnabled(false) ;
643 GroupButtons->setEnabled(false) ;
644 mySimulation->SetVisibility(false);
645 mySMESHGUI->ResetState() ;
646 mySMESHGUI->SetActiveDialogBox(0) ;
652 //=================================================================================
653 // function : ActivateThisDialog()
655 //=================================================================================
656 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
658 /* Emit a signal to deactivate the active dialog */
659 mySMESHGUI->EmitSignalDeactivateDialog() ;
661 GroupConstructors->setEnabled(true) ;
662 GroupC1->setEnabled(true) ;
663 GroupButtons->setEnabled(true) ;
665 SMESH::SetPointRepresentation(true);
666 QAD_Application::getDesktop()->SetSelectionMode( NodeSelection, true );
668 SelectionIntoArgument();
671 //=================================================================================
672 // function : enterEvent()
674 //=================================================================================
675 void SMESHGUI_AddMeshElementDlg::enterEvent(QEvent* e)
677 if ( GroupConstructors->isEnabled() )
679 ActivateThisDialog() ;
684 //=================================================================================
685 // function : closeEvent()
687 //=================================================================================
688 void SMESHGUI_AddMeshElementDlg::closeEvent( QCloseEvent* e )
690 /* same than click on cancel button */
691 this->ClickOnCancel() ;
694 //=======================================================================
695 //function : hideEvent
696 //purpose : caused by ESC key
697 //=======================================================================
699 void SMESHGUI_AddMeshElementDlg::hideEvent ( QHideEvent * e )
701 if ( !isMinimized() )
705 //=======================================================================
706 //function : CheckBox
708 //=======================================================================
710 void SMESHGUI_AddMeshElementDlg::CheckBox( int state )
716 mySimulation->SetVisibility(false);