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>
70 #include <qbuttongroup.h>
71 #include <qgroupbox.h>
73 #include <qlineedit.h>
74 #include <qpushbutton.h>
75 #include <qradiobutton.h>
79 #include <qwhatsthis.h>
82 #include <qcheckbox.h>
92 class TElementSimulation {
93 SalomeApp_Application* myApplication;
94 SUIT_ViewWindow* myViewWindow;
95 SVTK_ViewWindow* myVTKViewWindow;
97 SALOME_Actor* myPreviewActor;
98 vtkDataSetMapper* myMapper;
99 vtkUnstructuredGrid* myGrid;
102 TElementSimulation (SalomeApp_Application* theApplication)
104 myApplication = theApplication;
105 SUIT_ViewManager* mgr = theApplication->activeViewManager();
107 myViewWindow = mgr->getActiveView();
108 myVTKViewWindow = GetVtkViewWindow(myViewWindow);
110 myGrid = vtkUnstructuredGrid::New();
112 // Create and display actor
113 myMapper = vtkDataSetMapper::New();
114 myMapper->SetInput(myGrid);
116 myPreviewActor = SALOME_Actor::New();
117 myPreviewActor->PickableOff();
118 myPreviewActor->VisibilityOff();
119 myPreviewActor->SetMapper(myMapper);
122 vtkProperty* aProp = vtkProperty::New();
123 GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
124 aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
125 myPreviewActor->SetProperty( aProp );
128 vtkProperty* aBackProp = vtkProperty::New();
129 GetColor( "SMESH", "backface_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 0, 255 ) );
130 aBackProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
131 myPreviewActor->SetBackfaceProperty( aBackProp );
134 myVTKViewWindow->AddActor(myPreviewActor);
137 typedef std::vector<vtkIdType> TVTKIds;
138 void SetPosition (SMESH_Actor* theActor,
140 const TVTKIds& theIds)
142 vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
143 myGrid->SetPoints(aGrid->GetPoints());
145 const int* aConn = NULL;
149 static int anIds[] = {0,2,1,3};
155 static int anIds[] = {0,3,2,1,4};
161 static int anIds[] = {0,3,2,1,4,7,6,5};
168 vtkIdList *anIds = vtkIdList::New();
171 for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
172 anIds->InsertId(i,theIds[aConn[i]]);
174 for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
175 anIds->InsertId(i,theIds[i]);
177 myGrid->InsertNextCell(theType,anIds);
186 void SetVisibility (bool theVisibility)
188 myPreviewActor->SetVisibility(theVisibility);
189 RepaintCurrentView();
193 ~TElementSimulation()
195 if (FindVtkViewWindow(myApplication->activeViewManager(), myViewWindow)) {
196 myVTKViewWindow->RemoveActor(myPreviewActor);
198 myPreviewActor->Delete();
200 myMapper->RemoveAllInputs();
208 //=================================================================================
209 // function : SMESHGUI_AddMeshElementDlg()
210 // purpose : constructor
211 //=================================================================================
212 SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI* theModule,
214 SMDSAbs_ElementType ElementType, int nbNodes,
215 bool modal, WFlags fl)
216 : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder |
217 WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),
218 mySMESHGUI( theModule ),
219 mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
221 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
222 (SUIT_Session::session()->activeApplication());
224 mySimulation = new SMESH::TElementSimulation (anApp);
225 mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
227 // verify nb nodes and type
229 myElementType = ElementType;
230 switch (ElementType) {
232 // if (myNbNodes != 3 && myNbNodes != 4)
236 // if (myNbNodes != 4 && myNbNodes != 8) //(nbNodes < 4 || nbNodes > 8 || nbNodes == 7)
240 myElementType = SMDSAbs_Edge;
247 else if (myNbNodes == 3)
248 elemName = "TRIANGLE";
249 else if (myNbNodes == 4)
250 if (myElementType == SMDSAbs_Face)
251 elemName = "QUADRANGLE";
254 else if (myNbNodes == 8)
256 else if (myElementType == SMDSAbs_Face){
257 elemName = "POLYGON";
261 QString iconName = tr(QString("ICON_DLG_%1").arg(elemName));
262 QString buttonGrTitle = tr(QString("SMESH_%1").arg(elemName));
263 QString caption = tr(QString("SMESH_ADD_%1_TITLE").arg(elemName));
264 QString grBoxTitle = tr(QString("SMESH_ADD_%1").arg(elemName));
266 QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", iconName));
267 QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
270 setName("SMESHGUI_AddMeshElementDlg");
274 setSizeGripEnabled(TRUE);
275 SMESHGUI_AddMeshElementDlgLayout = new QGridLayout(this);
276 SMESHGUI_AddMeshElementDlgLayout->setSpacing(6);
277 SMESHGUI_AddMeshElementDlgLayout->setMargin(11);
279 /***************************************************************/
280 GroupConstructors = new QButtonGroup(this, "GroupConstructors");
281 GroupConstructors->setTitle(buttonGrTitle);
283 GroupConstructors->setExclusive(TRUE);
284 GroupConstructors->setColumnLayout(0, Qt::Vertical);
285 GroupConstructors->layout()->setSpacing(0);
286 GroupConstructors->layout()->setMargin(0);
287 GroupConstructorsLayout = new QGridLayout(GroupConstructors->layout());
288 GroupConstructorsLayout->setAlignment(Qt::AlignTop);
289 GroupConstructorsLayout->setSpacing(6);
290 GroupConstructorsLayout->setMargin(11);
291 Constructor1 = new QRadioButton(GroupConstructors, "Constructor1");
292 Constructor1->setText(tr("" ));
293 Constructor1->setPixmap(image0);
294 Constructor1->setChecked(TRUE);
295 Constructor1->setSizePolicy(QSizePolicy((QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth()));
296 Constructor1->setMinimumSize(QSize(50, 0));
297 GroupConstructorsLayout->addWidget(Constructor1, 0, 0);
298 QSpacerItem* spacer = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
299 GroupConstructorsLayout->addItem(spacer, 0, 1);
300 SMESHGUI_AddMeshElementDlgLayout->addWidget(GroupConstructors, 0, 0);
302 /***************************************************************/
303 GroupButtons = new QGroupBox(this, "GroupButtons");
304 GroupButtons->setGeometry(QRect(10, 10, 281, 48));
305 GroupButtons->setTitle(tr("" ));
306 GroupButtons->setColumnLayout(0, Qt::Vertical);
307 GroupButtons->layout()->setSpacing(0);
308 GroupButtons->layout()->setMargin(0);
309 GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
310 GroupButtonsLayout->setAlignment(Qt::AlignTop);
311 GroupButtonsLayout->setSpacing(6);
312 GroupButtonsLayout->setMargin(11);
313 buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
314 buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
315 buttonCancel->setAutoDefault(TRUE);
316 GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
317 buttonApply = new QPushButton(GroupButtons, "buttonApply");
318 buttonApply->setText(tr("SMESH_BUT_APPLY" ));
319 buttonApply->setAutoDefault(TRUE);
320 GroupButtonsLayout->addWidget(buttonApply, 0, 1);
321 QSpacerItem* spacer_9 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
322 GroupButtonsLayout->addItem(spacer_9, 0, 2);
323 buttonOk = new QPushButton(GroupButtons, "buttonOk");
324 buttonOk->setText(tr("SMESH_BUT_OK" ));
325 buttonOk->setAutoDefault(TRUE);
326 buttonOk->setDefault(TRUE);
327 GroupButtonsLayout->addWidget(buttonOk, 0, 0);
328 SMESHGUI_AddMeshElementDlgLayout->addWidget(GroupButtons, 2, 0);
330 /***************************************************************/
331 GroupC1 = new QGroupBox(this, "GroupC1");
332 GroupC1->setTitle(grBoxTitle);
334 GroupC1->setMinimumSize(QSize(0, 0));
335 GroupC1->setFrameShape(QGroupBox::Box);
336 GroupC1->setFrameShadow(QGroupBox::Sunken);
337 GroupC1->setColumnLayout(0, Qt::Vertical);
338 GroupC1->layout()->setSpacing(0);
339 GroupC1->layout()->setMargin(0);
340 GroupC1Layout = new QGridLayout(GroupC1->layout());
341 GroupC1Layout->setAlignment(Qt::AlignTop);
342 GroupC1Layout->setSpacing(6);
343 GroupC1Layout->setMargin(11);
344 TextLabelC1A1 = new QLabel(GroupC1, "TextLabelC1A1");
345 TextLabelC1A1->setText(tr("SMESH_ID_NODES" ));
346 TextLabelC1A1->setMinimumSize(QSize(50, 0));
347 TextLabelC1A1->setFrameShape(QLabel::NoFrame);
348 TextLabelC1A1->setFrameShadow(QLabel::Plain);
349 GroupC1Layout->addWidget(TextLabelC1A1, 0, 0);
350 SelectButtonC1A1 = new QPushButton(GroupC1, "SelectButtonC1A1");
351 SelectButtonC1A1->setText(tr("" ));
352 SelectButtonC1A1->setPixmap(image1);
353 SelectButtonC1A1->setToggleButton(FALSE);
354 GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
355 LineEditC1A1 = new QLineEdit(GroupC1, "LineEditC1A1");
356 // LineEditC1A1->setReadOnly(TRUE);
358 LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this, "validator", myNbNodes));
359 GroupC1Layout->addWidget(LineEditC1A1, 0, 2);
361 if (myElementType == SMDSAbs_Face) {
362 Reverse = new QCheckBox(GroupC1, "Reverse");
363 Reverse->setText(tr("SMESH_REVERSE" ));
364 GroupC1Layout->addWidget(Reverse, 1, 0);
369 SMESHGUI_AddMeshElementDlgLayout->addWidget(GroupC1, 1, 0);
371 Init(); /* Initialisations */
374 //=================================================================================
375 // function : ~SMESHGUI_AddMeshElementDlg()
376 // purpose : Destroys the object and frees any allocated resources
377 //=================================================================================
378 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
380 // no need to delete child widgets, Qt does it all for us
384 //=================================================================================
387 //=================================================================================
388 void SMESHGUI_AddMeshElementDlg::Init()
391 Constructor1->setChecked(TRUE);
392 myEditCurrentArgument = LineEditC1A1;
393 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
398 /* signals and slots connections */
399 connect(buttonOk, SIGNAL(clicked()), SLOT(ClickOnOk()));
400 connect(buttonCancel, SIGNAL(clicked()), SLOT(ClickOnCancel()));
401 connect(buttonApply, SIGNAL(clicked()), SLOT(ClickOnApply()));
403 connect(SelectButtonC1A1, SIGNAL(clicked()), SLOT(SetEditCurrentArgument()));
404 connect(LineEditC1A1, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
405 connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
406 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
407 /* to close dialog if study frame change */
408 connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
411 connect(Reverse, SIGNAL(stateChanged(int)), SLOT(CheckBox(int)));
413 // Move widget on the botton right corner of main widget
415 mySMESHGUI->DefineDlgPosition(this, x, y);
417 this->show(); // displays Dialog
419 // set selection mode
420 SMESH::SetPointRepresentation(true);
422 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
423 aViewWindow->SetSelectionMode( NodeSelection );
427 SelectionIntoArgument();
430 //=================================================================================
431 // function : ClickOnApply()
433 //=================================================================================
434 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
436 if (myNbOkNodes && !mySMESHGUI->isActiveStudyLocked()) {
438 SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
439 anArrayOfIdeces->length(myNbNodes);
440 bool reverse = (Reverse && Reverse->isChecked());
441 QStringList aListId = QStringList::split(" ", myEditCurrentArgument->text(), false);
442 for (int i = 0; i < aListId.count(); i++)
444 anArrayOfIdeces[i] = aListId[ myNbNodes - i - 1 ].toInt();
446 anArrayOfIdeces[i] = aListId[ i ].toInt();
448 SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
449 switch (myElementType) {
451 aMeshEditor->AddEdge(anArrayOfIdeces.inout()); break;
453 aMeshEditor->AddFace(anArrayOfIdeces.inout()); break;
455 aMeshEditor->AddVolume(anArrayOfIdeces.inout()); break;
459 SALOME_ListIO aList; aList.Append( myActor->getIO() );
460 mySelectionMgr->setSelectedObjects( aList, false );
463 mySimulation->SetVisibility(false);
465 buttonOk->setEnabled(false);
466 buttonApply->setEnabled(false);
468 myEditCurrentArgument->setText("");
474 //=================================================================================
475 // function : ClickOnOk()
477 //=================================================================================
478 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
480 this->ClickOnApply();
481 this->ClickOnCancel();
485 //=================================================================================
486 // function : ClickOnCancel()
488 //=================================================================================
489 void SMESHGUI_AddMeshElementDlg::ClickOnCancel()
491 mySelectionMgr->clearSelected();
492 mySimulation->SetVisibility(false);
493 SMESH::SetPointRepresentation(false);
494 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
495 aViewWindow->SetSelectionMode( ActorSelection );
496 disconnect(mySelectionMgr, 0, this, 0);
497 mySMESHGUI->ResetState();
502 //=================================================================================
503 // function : onTextChange()
505 //=================================================================================
506 void SMESHGUI_AddMeshElementDlg::onTextChange (const QString& theNewText)
513 buttonOk->setEnabled(false);
514 buttonApply->setEnabled(false);
516 mySimulation->SetVisibility(false);
518 // hilight entered nodes
519 SMDS_Mesh* aMesh = 0;
521 aMesh = myActor->GetObject()->GetMesh();
524 TColStd_MapOfInteger newIndices;
526 QStringList aListId = QStringList::split(" ", theNewText, false);
527 for (int i = 0; i < aListId.count(); i++) {
528 if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
530 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 bool aNodesOK = false;
540 if (myIsPoly && myElementType == SMDSAbs_Face && aListId.count() >=3 ){
541 myNbOkNodes = aListId.count();
547 buttonOk->setEnabled(true);
548 buttonApply->setEnabled(true);
555 //=================================================================================
556 // function : SelectionIntoArgument()
557 // purpose : Called when selection has changed
558 //=================================================================================
559 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
568 myEditCurrentArgument->setText("");
571 if (!GroupButtons->isEnabled()) // inactive
574 buttonOk->setEnabled(false);
575 buttonApply->setEnabled(false);
577 mySimulation->SetVisibility(false);
578 // SMESH::SetPointRepresentation(true);
582 mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
584 if (aList.Extent() != 1)
587 Handle(SALOME_InteractiveObject) anIO = aList.First();
588 myMesh = SMESH::GetMeshByIO(anIO);
589 if (myMesh->_is_nil())
592 myActor = SMESH::FindActorByEntry(anIO->getEntry());
596 // get selected nodes
597 QString aString = "";
598 int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,myActor->getIO(),aString);
600 myEditCurrentArgument->setText(aString);
602 if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 ) {
604 } else if (myNbNodes != nbNodes) {
609 myNbOkNodes = nbNodes;
611 buttonOk->setEnabled(true);
612 buttonApply->setEnabled(true);
617 //=================================================================================
618 // function : displaySimulation()
620 //=================================================================================
621 void SMESHGUI_AddMeshElementDlg::displaySimulation()
623 if (myNbOkNodes && GroupButtons->isEnabled()) {
624 SMESH::TElementSimulation::TVTKIds anIds;
625 QStringList aListId = QStringList::split(" ", myEditCurrentArgument->text(), false);
626 for (int i = 0; i < aListId.count(); i++)
627 anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
629 if (Reverse && Reverse->isChecked())
630 reverse(anIds.begin(),anIds.end());
634 switch ( myElementType ) {
635 case SMDSAbs_Face : aType = VTK_POLYGON; break;
640 case 2: aType = VTK_LINE; break;
641 case 3: aType = VTK_TRIANGLE; break;
642 case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
643 case 8: aType = VTK_HEXAHEDRON; break;
648 mySimulation->SetPosition(myActor,aType,anIds);
653 //=================================================================================
654 // function : SetEditCurrentArgument()
656 //=================================================================================
657 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
659 QPushButton* send = (QPushButton*)sender();
660 if (send == SelectButtonC1A1) {
661 LineEditC1A1->setFocus();
662 myEditCurrentArgument = LineEditC1A1;
664 SelectionIntoArgument();
667 //=================================================================================
668 // function : DeactivateActiveDialog()
670 //=================================================================================
671 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
673 if (GroupConstructors->isEnabled()) {
674 GroupConstructors->setEnabled(false);
675 GroupC1->setEnabled(false);
676 GroupButtons->setEnabled(false);
677 mySimulation->SetVisibility(false);
678 mySMESHGUI->ResetState();
679 mySMESHGUI->SetActiveDialogBox(0);
683 //=================================================================================
684 // function : ActivateThisDialog()
686 //=================================================================================
687 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
689 /* Emit a signal to deactivate the active dialog */
690 mySMESHGUI->EmitSignalDeactivateDialog();
692 GroupConstructors->setEnabled(true);
693 GroupC1->setEnabled(true);
694 GroupButtons->setEnabled(true);
696 SMESH::SetPointRepresentation(true);
698 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
699 aViewWindow->SetSelectionMode( NodeSelection );
700 SelectionIntoArgument();
703 //=================================================================================
704 // function : enterEvent()
706 //=================================================================================
707 void SMESHGUI_AddMeshElementDlg::enterEvent (QEvent*)
709 if (GroupConstructors->isEnabled())
711 ActivateThisDialog();
715 //=================================================================================
716 // function : closeEvent()
718 //=================================================================================
719 void SMESHGUI_AddMeshElementDlg::closeEvent (QCloseEvent*)
721 /* same than click on cancel button */
722 this->ClickOnCancel();
726 //=================================================================================
727 // function : hideEvent()
728 // purpose : caused by ESC key
729 //=================================================================================
730 void SMESHGUI_AddMeshElementDlg::hideEvent (QHideEvent*)
736 //=================================================================================
737 // function : CheckBox()
739 //=================================================================================
740 void SMESHGUI_AddMeshElementDlg::CheckBox (int state)
746 mySimulation->SetVisibility(false);