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)
207 mySimulation = new SMESH::TElementSimulation(SMESH::GetActiveStudy());
209 // verify nb nodes and type
211 myElementType = ElementType;
212 switch ( ElementType ) {
217 myElementType = SMDSAbs_Edge;
224 else if (myNbNodes == 3)
225 elemName = "TRIANGLE";
226 else if (myNbNodes == 4)
227 if (myElementType == SMDSAbs_Face)
228 elemName = "QUADRANGLE";
231 else if (myNbNodes == 8)
233 else if (myElementType == SMDSAbs_Face){
234 elemName = "POLYGON";
238 QString iconName = tr( QString("ICON_DLG_%1").arg(elemName) );
239 QString buttonGrTitle = tr( QString("SMESH_%1").arg(elemName) );
240 QString caption = tr( QString("SMESH_ADD_%1_TITLE").arg(elemName) );
241 QString grBoxTitle = tr( QString("SMESH_ADD_%1").arg(elemName) );
243 QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH", iconName ));
244 QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH", tr("ICON_SELECT")));
247 setName( "SMESHGUI_AddMeshElementDlg" );
249 setCaption( caption );
251 setSizeGripEnabled( TRUE );
252 SMESHGUI_AddMeshElementDlgLayout = new QGridLayout( this );
253 SMESHGUI_AddMeshElementDlgLayout->setSpacing( 6 );
254 SMESHGUI_AddMeshElementDlgLayout->setMargin( 11 );
256 /***************************************************************/
257 GroupConstructors = new QButtonGroup( this, "GroupConstructors" );
258 GroupConstructors->setTitle( buttonGrTitle );
260 GroupConstructors->setExclusive( TRUE );
261 GroupConstructors->setColumnLayout(0, Qt::Vertical );
262 GroupConstructors->layout()->setSpacing( 0 );
263 GroupConstructors->layout()->setMargin( 0 );
264 GroupConstructorsLayout = new QGridLayout( GroupConstructors->layout() );
265 GroupConstructorsLayout->setAlignment( Qt::AlignTop );
266 GroupConstructorsLayout->setSpacing( 6 );
267 GroupConstructorsLayout->setMargin( 11 );
268 Constructor1 = new QRadioButton( GroupConstructors, "Constructor1" );
269 Constructor1->setText( tr( "" ) );
270 Constructor1->setPixmap( image0 );
271 Constructor1->setChecked( TRUE );
272 Constructor1->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth() ) );
273 Constructor1->setMinimumSize( QSize( 50, 0 ) );
274 GroupConstructorsLayout->addWidget( Constructor1, 0, 0 );
275 QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
276 GroupConstructorsLayout->addItem( spacer, 0, 1 );
277 SMESHGUI_AddMeshElementDlgLayout->addWidget( GroupConstructors, 0, 0 );
279 /***************************************************************/
280 GroupButtons = new QGroupBox( this, "GroupButtons" );
281 GroupButtons->setGeometry( QRect( 10, 10, 281, 48 ) );
282 GroupButtons->setTitle( tr( "" ) );
283 GroupButtons->setColumnLayout(0, Qt::Vertical );
284 GroupButtons->layout()->setSpacing( 0 );
285 GroupButtons->layout()->setMargin( 0 );
286 GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
287 GroupButtonsLayout->setAlignment( Qt::AlignTop );
288 GroupButtonsLayout->setSpacing( 6 );
289 GroupButtonsLayout->setMargin( 11 );
290 buttonCancel = new QPushButton( GroupButtons, "buttonCancel" );
291 buttonCancel->setText( tr( "SMESH_BUT_CLOSE" ) );
292 buttonCancel->setAutoDefault( TRUE );
293 GroupButtonsLayout->addWidget( buttonCancel, 0, 3 );
294 buttonApply = new QPushButton( GroupButtons, "buttonApply" );
295 buttonApply->setText( tr( "SMESH_BUT_APPLY" ) );
296 buttonApply->setAutoDefault( TRUE );
297 GroupButtonsLayout->addWidget( buttonApply, 0, 1 );
298 QSpacerItem* spacer_9 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
299 GroupButtonsLayout->addItem( spacer_9, 0, 2 );
300 buttonOk = new QPushButton( GroupButtons, "buttonOk" );
301 buttonOk->setText( tr( "SMESH_BUT_OK" ) );
302 buttonOk->setAutoDefault( TRUE );
303 buttonOk->setDefault( TRUE );
304 GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
305 SMESHGUI_AddMeshElementDlgLayout->addWidget( GroupButtons, 2, 0 );
307 /***************************************************************/
308 GroupC1 = new QGroupBox( this, "GroupC1" );
309 GroupC1->setTitle( grBoxTitle );
311 GroupC1->setMinimumSize( QSize( 0, 0 ) );
312 GroupC1->setFrameShape( QGroupBox::Box );
313 GroupC1->setFrameShadow( QGroupBox::Sunken );
314 GroupC1->setColumnLayout(0, Qt::Vertical );
315 GroupC1->layout()->setSpacing( 0 );
316 GroupC1->layout()->setMargin( 0 );
317 GroupC1Layout = new QGridLayout( GroupC1->layout() );
318 GroupC1Layout->setAlignment( Qt::AlignTop );
319 GroupC1Layout->setSpacing( 6 );
320 GroupC1Layout->setMargin( 11 );
321 TextLabelC1A1 = new QLabel( GroupC1, "TextLabelC1A1" );
322 TextLabelC1A1->setText( tr( "SMESH_ID_NODES" ) );
323 TextLabelC1A1->setMinimumSize( QSize( 50, 0 ) );
324 TextLabelC1A1->setFrameShape( QLabel::NoFrame );
325 TextLabelC1A1->setFrameShadow( QLabel::Plain );
326 GroupC1Layout->addWidget( TextLabelC1A1, 0, 0 );
327 SelectButtonC1A1 = new QPushButton( GroupC1, "SelectButtonC1A1" );
328 SelectButtonC1A1->setText( tr( "" ) );
329 SelectButtonC1A1->setPixmap( image1 );
330 SelectButtonC1A1->setToggleButton( FALSE );
331 GroupC1Layout->addWidget( SelectButtonC1A1, 0, 1 );
332 LineEditC1A1 = new QLineEdit( GroupC1, "LineEditC1A1" );
333 // LineEditC1A1->setReadOnly( TRUE );
335 LineEditC1A1->setValidator( new SMESHGUI_IdValidator( this, "validator", myNbNodes));
336 GroupC1Layout->addWidget( LineEditC1A1, 0, 2 );
338 if ( myElementType == SMDSAbs_Face ) {
339 Reverse = new QCheckBox( GroupC1, "Reverse" );
340 Reverse->setText( tr( "SMESH_REVERSE" ) );
341 GroupC1Layout->addWidget( Reverse, 1, 0 );
346 SMESHGUI_AddMeshElementDlgLayout->addWidget( GroupC1, 1, 0 );
348 Init(Sel) ; /* Initialisations */
351 //=================================================================================
352 // function : ~SMESHGUI_AddMeshElementDlg()
353 // purpose : Destroys the object and frees any allocated resources
354 //=================================================================================
355 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
357 // no need to delete child widgets, Qt does it all for us
362 //=================================================================================
365 //=================================================================================
366 void SMESHGUI_AddMeshElementDlg::Init( SALOME_Selection* Sel )
370 Constructor1->setChecked( TRUE );
371 myEditCurrentArgument = LineEditC1A1 ;
373 mySMESHGUI = SMESHGUI::GetSMESHGUI() ;
374 mySMESHGUI->SetActiveDialogBox( (QDialog*)this ) ;
379 /* signals and slots connections */
380 connect(buttonOk, SIGNAL( clicked() ), SLOT( ClickOnOk() ) );
381 connect(buttonCancel, SIGNAL( clicked() ), SLOT( ClickOnCancel() ) ) ;
382 connect(buttonApply, SIGNAL( clicked() ), SLOT(ClickOnApply() ) );
384 connect(SelectButtonC1A1, SIGNAL( clicked() ), SLOT( SetEditCurrentArgument() ) ) ;
385 connect(LineEditC1A1, SIGNAL( textChanged(const QString&) ), SLOT(onTextChange(const QString&)));
386 connect(mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), SLOT( DeactivateActiveDialog()));
387 connect(mySelection, SIGNAL( currentSelectionChanged() ), SLOT( SelectionIntoArgument() ) );
388 /* to close dialog if study frame change */
389 connect( mySMESHGUI, SIGNAL ( SignalStudyFrameChanged() ), SLOT( ClickOnCancel() ) ) ;
392 connect( Reverse, SIGNAL(stateChanged(int)), SLOT(CheckBox(int)) );
394 /* Move widget on the botton right corner of main widget */
396 mySMESHGUI->DefineDlgPosition( this, x, y ) ;
398 this->show() ; /* displays Dialog */
400 // set selection mode
401 SMESH::SetPointRepresentation(true);
402 QAD_Application::getDesktop()->SetSelectionMode( NodeSelection, true );
404 SelectionIntoArgument();
409 //=================================================================================
410 // function : ClickOnApply()
412 //=================================================================================
413 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
415 if ( myOkNodes && !mySMESHGUI->ActiveStudyLocked() ) {
418 SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
419 anArrayOfIdeces->length( myNbNodes );
420 bool reverse = ( Reverse && Reverse->isChecked() );
421 QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text(), false);
422 for ( int i = 0; i < aListId.count(); i++ )
424 anArrayOfIdeces[i] = aListId[ myNbNodes - i - 1 ].toInt();
426 anArrayOfIdeces[i] = aListId[ i ].toInt();
428 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
429 switch ( myElementType ) {
431 aMeshEditor->AddEdge(anArrayOfIdeces.inout()); break;
433 aMeshEditor->AddFace(anArrayOfIdeces.inout()); break;
435 aMeshEditor->AddVolume(anArrayOfIdeces.inout()); break;
439 mySelection->ClearIObjects();
440 mySelection->AddIObject( myActor->getIO(), false );
443 mySimulation->SetVisibility(false);
445 buttonOk->setEnabled( false );
446 buttonApply->setEnabled( false );
448 myEditCurrentArgument->setText("");
454 //=================================================================================
455 // function : ClickOnOk()
457 //=================================================================================
458 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
460 this->ClickOnApply() ;
461 this->ClickOnCancel() ;
465 //=================================================================================
466 // function : ClickOnCancel()
468 //=================================================================================
469 void SMESHGUI_AddMeshElementDlg::ClickOnCancel()
471 mySelection->ClearIObjects();
472 mySimulation->SetVisibility(false);
473 SMESH::SetPointRepresentation(false);
474 QAD_Application::getDesktop()->SetSelectionMode( ActorSelection );
475 disconnect( mySelection, 0, this, 0 );
476 mySMESHGUI->ResetState() ;
481 //=======================================================================
482 //function : onTextChange
484 //=======================================================================
486 void SMESHGUI_AddMeshElementDlg::onTextChange(const QString& theNewText)
488 if ( myBusy ) return;
493 buttonOk->setEnabled( false );
494 buttonApply->setEnabled( false );
496 mySimulation->SetVisibility(false);
498 // hilight entered nodes
499 SMDS_Mesh* aMesh = 0;
501 aMesh = myActor->GetObject()->GetMesh();
504 mySelection->ClearIObjects();
505 mySelection->AddIObject( myActor->getIO() );
507 QStringList aListId = QStringList::split( " ", theNewText, false);
509 for ( int i = 0; i < aListId.count(); i++ ) {
510 const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() );
512 if ( mySelection->IsIndexSelected( myActor->getIO(), n->GetID() ))
515 mySelection->AddOrRemoveIndex (myActor->getIO(), n->GetID(), true);
521 bool aNodesOK = false;
522 if (myIsPoly && myElementType == SMDSAbs_Face && aListId.count() >=3 ){
523 myNbNodes = aListId.count();
525 } else if (!myIsPoly){
526 aNodesOK = (myNbNodes == aListId.count());
528 myOkNodes = (allOk && aNodesOK);//myNbNodes == aListId.count() );
531 buttonOk->setEnabled( true );
532 buttonApply->setEnabled( true );
540 //=================================================================================
541 // function : SelectionIntoArgument()
542 // purpose : Called when selection has changed
543 //=================================================================================
544 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
546 if ( myBusy ) return;
554 myEditCurrentArgument->setText( "" );
557 if ( !GroupButtons->isEnabled() ) // inactive
560 buttonOk->setEnabled( false );
561 buttonApply->setEnabled( false );
563 mySimulation->SetVisibility(false);
564 // SMESH::SetPointRepresentation(true);
568 int nbSel = mySelection->IObjectCount();
572 myMesh = SMESH::GetMeshByIO( mySelection->firstIObject() );
573 if ( myMesh->_is_nil() )
576 myActor = SMESH::FindActorByEntry( mySelection->firstIObject()->getEntry() );
580 // get selected nodes
582 QString aString = "";
583 int nbNodes = SMESH::GetNameOfSelectedNodes(mySelection, aString) ;
585 myEditCurrentArgument->setText( aString );
587 if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 ) {
589 } else if (myNbNodes != nbNodes) {
597 buttonOk->setEnabled( true );
598 buttonApply->setEnabled( true );
603 //=======================================================================
604 //function : displaySimulation
606 //=======================================================================
608 void SMESHGUI_AddMeshElementDlg::displaySimulation()
610 if ( myOkNodes && GroupButtons->isEnabled() )
612 SMESH::TElementSimulation::TVTKIds anIds;
613 QStringList aListId = QStringList::split( " ", myEditCurrentArgument->text(), false);
614 for ( int i = 0; i < aListId.count(); i++ )
615 anIds.push_back( myActor->GetObject()->GetNodeVTKId( aListId[ i ].toInt() ));
617 if ( Reverse && Reverse->isChecked() )
618 reverse(anIds.begin(),anIds.end());
622 switch ( myElementType ) {
623 case SMDSAbs_Face : aType = VTK_POLYGON; break;
627 switch ( myNbNodes ) {
628 case 2: aType = VTK_LINE; break;
629 case 3: aType = VTK_TRIANGLE; break;
630 case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
631 case 8: aType = VTK_HEXAHEDRON; break;
636 mySimulation->SetPosition(myActor,aType,anIds);
641 //=================================================================================
642 // function : SetEditCurrentArgument()
644 //=================================================================================
645 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
647 QPushButton* send = (QPushButton*)sender();
648 if(send == SelectButtonC1A1) {
649 LineEditC1A1->setFocus() ;
650 myEditCurrentArgument = LineEditC1A1;
652 SelectionIntoArgument() ;
655 //=================================================================================
656 // function : DeactivateActiveDialog()
658 //=================================================================================
659 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
661 if ( GroupConstructors->isEnabled() ) {
662 GroupConstructors->setEnabled(false) ;
663 GroupC1->setEnabled(false) ;
664 GroupButtons->setEnabled(false) ;
665 mySimulation->SetVisibility(false);
666 mySMESHGUI->ResetState() ;
667 mySMESHGUI->SetActiveDialogBox(0) ;
673 //=================================================================================
674 // function : ActivateThisDialog()
676 //=================================================================================
677 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
679 /* Emit a signal to deactivate the active dialog */
680 mySMESHGUI->EmitSignalDeactivateDialog() ;
682 GroupConstructors->setEnabled(true) ;
683 GroupC1->setEnabled(true) ;
684 GroupButtons->setEnabled(true) ;
686 SMESH::SetPointRepresentation(true);
687 QAD_Application::getDesktop()->SetSelectionMode( NodeSelection, true );
689 SelectionIntoArgument();
692 //=================================================================================
693 // function : enterEvent()
695 //=================================================================================
696 void SMESHGUI_AddMeshElementDlg::enterEvent(QEvent* e)
698 if ( GroupConstructors->isEnabled() )
700 ActivateThisDialog() ;
705 //=================================================================================
706 // function : closeEvent()
708 //=================================================================================
709 void SMESHGUI_AddMeshElementDlg::closeEvent( QCloseEvent* e )
711 /* same than click on cancel button */
712 this->ClickOnCancel() ;
715 //=======================================================================
716 //function : hideEvent
717 //purpose : caused by ESC key
718 //=======================================================================
720 void SMESHGUI_AddMeshElementDlg::hideEvent ( QHideEvent * e )
722 if ( !isMinimized() )
726 //=======================================================================
727 //function : CheckBox
729 //=======================================================================
731 void SMESHGUI_AddMeshElementDlg::CheckBox( int state )
737 mySimulation->SetVisibility(false);