1 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SMESHGUI_SingleEditDlg.cxx
21 // Author : Sergey LITONIN, Open CASCADE S.A.S.
25 #include "SMESHGUI_SingleEditDlg.h"
28 #include "SMESHGUI_Utils.h"
29 #include "SMESHGUI_VTKUtils.h"
30 #include "SMESHGUI_MeshUtils.h"
32 #include <SMESH_Actor.h>
33 #include <SMDS_Mesh.hxx>
35 // SALOME GUI includes
36 #include <LightApp_SelectionMgr.h>
37 #include <LightApp_Application.h>
38 #include <SUIT_ResourceMgr.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_Desktop.h>
41 #include <SUIT_Session.h>
43 #include <SVTK_Selector.h>
44 #include <SVTK_ViewWindow.h>
45 #include <SALOME_ListIO.hxx>
48 #include <TColStd_MapOfInteger.hxx>
49 #include <TColStd_IndexedMapOfInteger.hxx>
52 #include <QVBoxLayout>
53 #include <QHBoxLayout>
55 #include <QPushButton>
66 \brief Simple 'busy state' flag locker.
73 //! Constructor. Sets passed boolean flag to \c true.
74 BusyLocker( bool& busy ) : myBusy( busy ) { myBusy = true; }
75 //! Destructor. Clear external boolean flag passed as parameter to the constructor to \c false.
76 ~BusyLocker() { myBusy = false; }
78 bool& myBusy; //! External 'busy state' boolean flag
82 * Class : SMESHGUI_SingleEditDlg
83 * Description : Inversion of the diagonal of a pseudo-quadrangle formed by
84 * 2 neighboring triangles with 1 common edge
87 //=======================================================================
88 // name : SMESHGUI_SingleEditDlg()
89 // Purpose : Constructor
90 //=======================================================================
91 SMESHGUI_SingleEditDlg
92 ::SMESHGUI_SingleEditDlg(SMESHGUI* theModule)
93 : QDialog(SMESH::GetDesktop(theModule)),
94 mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
95 mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
100 QVBoxLayout* aDlgLay = new QVBoxLayout(this);
101 aDlgLay->setMargin(MARGIN);
102 aDlgLay->setSpacing(SPACING);
104 QWidget* aMainFrame = createMainFrame (this);
105 QWidget* aBtnFrame = createButtonFrame(this);
107 aDlgLay->addWidget(aMainFrame);
108 aDlgLay->addWidget(aBtnFrame);
113 //=======================================================================
114 // name : createMainFrame()
115 // Purpose : Create frame containing dialog's input fields
116 //=======================================================================
117 QWidget* SMESHGUI_SingleEditDlg::createMainFrame (QWidget* theParent)
119 QGroupBox* aMainGrp = new QGroupBox(tr("EDGE_BETWEEN"), theParent);
120 QHBoxLayout* aLay = new QHBoxLayout(aMainGrp);
121 aLay->setMargin(MARGIN);
122 aLay->setSpacing(SPACING);
124 QPixmap aPix (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
126 QLabel* aLab = new QLabel(tr("SMESH_EDGE"), aMainGrp);
127 QPushButton* aBtn = new QPushButton(aMainGrp);
129 myEdge = new QLineEdit(aMainGrp);
130 myEdge->setValidator(new QRegExpValidator(QRegExp("[\\d]*-[\\d]*"), this));
132 aLay->addWidget(aLab);
133 aLay->addWidget(aBtn);
134 aLay->addWidget(myEdge);
139 //=======================================================================
140 // name : createButtonFrame()
141 // Purpose : Create frame containing buttons
142 //=======================================================================
143 QWidget* SMESHGUI_SingleEditDlg::createButtonFrame (QWidget* theParent)
145 QGroupBox* aFrame = new QGroupBox(theParent);
147 myOkBtn = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aFrame);
148 myApplyBtn = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
149 myCloseBtn = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
150 myHelpBtn = new QPushButton(tr("SMESH_BUT_HELP"), aFrame);
152 QHBoxLayout* aLay = new QHBoxLayout(aFrame);
153 aLay->setMargin(MARGIN);
154 aLay->setSpacing(SPACING);
156 aLay->addWidget(myOkBtn);
157 aLay->addSpacing(10);
158 aLay->addWidget(myApplyBtn);
159 aLay->addSpacing(10);
161 aLay->addWidget(myCloseBtn);
162 aLay->addWidget(myHelpBtn);
167 //=======================================================================
169 // Purpose : Verify validity of input data
170 //=======================================================================
171 bool SMESHGUI_SingleEditDlg::isValid (const bool theMess) const
174 return getNodeIds(myEdge->text(), id1, id2);
177 //=======================================================================
178 // name : getNodeIds()
179 // Purpose : Retrieve node ids from string
180 //=======================================================================
181 bool SMESHGUI_SingleEditDlg::getNodeIds (const QString& theStr,
182 int& theId1, int& theId2) const
184 if (!theStr.contains('-'))
188 QString str1 = theStr.section('-', 0, 0, QString::SectionSkipEmpty);
189 QString str2 = theStr.section('-', 1, 1, QString::SectionSkipEmpty);
190 theId1 = str1.toInt(&ok1);
191 theId2 = str2.toInt(&ok2);
196 //=======================================================================
197 // name : ~SMESHGUI_SingleEditDlg()
198 // Purpose : Destructor
199 //=======================================================================
200 SMESHGUI_SingleEditDlg::~SMESHGUI_SingleEditDlg()
204 //=======================================================================
206 // Purpose : Init dialog fields, connect signals and slots, show dialog
207 //=======================================================================
208 void SMESHGUI_SingleEditDlg::Init()
210 mySMESHGUI->SetActiveDialogBox((QDialog*)this);
215 connect(myOkBtn, SIGNAL(clicked()), SLOT(onOk()));
216 connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
217 connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
218 connect(myHelpBtn, SIGNAL(clicked()), SLOT(onHelp()));
220 // selection and SMESHGUI
221 connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
222 connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
223 connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
224 connect(myEdge, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
226 myOkBtn->setEnabled(false);
227 myApplyBtn->setEnabled(false);
230 // set selection mode
231 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
232 aViewWindow->SetSelectionMode(EdgeOfCellSelection);
237 //=======================================================================
239 // Purpose : SLOT called when "Ok" button pressed.
240 // Assign filters VTK viewer and close dialog
241 //=======================================================================
242 void SMESHGUI_SingleEditDlg::onOk()
248 //=======================================================================
250 // Purpose : SLOT called when "Close" button pressed. Close dialog
251 //=======================================================================
252 void SMESHGUI_SingleEditDlg::onClose()
254 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
255 aViewWindow->SetSelectionMode(ActorSelection);
256 //mySelectionMgr->clearSelected();
257 disconnect(mySelectionMgr, 0, this, 0);
258 disconnect(mySMESHGUI, 0, this, 0);
259 mySMESHGUI->ResetState();
263 //=================================================================================
264 // function : onHelp()
266 //=================================================================================
267 void SMESHGUI_SingleEditDlg::onHelp()
269 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
271 app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
275 platform = "winapplication";
277 platform = "application";
279 SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
280 tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
281 arg(app->resourceMgr()->stringValue("ExternalBrowser",
283 arg(myHelpFileName));
287 //=======================================================================
288 //function : findTriangles()
289 //purpose : find triangles sharing theNode1-theNode2 link
290 // THIS IS A PIECE OF SMESH_MeshEditor.cxx
291 // TO DO: make it available in SMDS for ex.
292 //=======================================================================
293 static bool findTriangles (const SMDS_MeshNode * theNode1,
294 const SMDS_MeshNode * theNode2,
295 const SMDS_MeshElement*& theTria1,
296 const SMDS_MeshElement*& theTria2)
298 if (!theNode1 || !theNode2) return false;
300 theTria1 = theTria2 = 0;
302 std::set< const SMDS_MeshElement* > emap;
303 SMDS_ElemIteratorPtr it = theNode1->GetInverseElementIterator();
305 const SMDS_MeshElement* elem = it->next();
306 if (elem->GetType() == SMDSAbs_Face && elem->NbNodes() == 3)
309 it = theNode2->GetInverseElementIterator();
311 const SMDS_MeshElement* elem = it->next();
312 if (elem->GetType() == SMDSAbs_Face &&
313 emap.find(elem) != emap.end())
321 return (theTria1 && theTria2);
324 //=======================================================================
325 //function : onTextChange()
327 //=======================================================================
328 void SMESHGUI_SingleEditDlg::onTextChange (const QString& theNewText)
331 BusyLocker lock(myBusy);
333 myOkBtn->setEnabled(false);
334 myApplyBtn->setEnabled(false);
336 // hilight entered edge
338 if(SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh()){
339 Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
342 mySelectionMgr->setSelectedObjects(aList,false);
344 TColStd_IndexedMapOfInteger selectedIndices;
345 TColStd_MapOfInteger newIndices;
346 mySelector->GetIndex(anIO,selectedIndices);
349 if ( !getNodeIds(myEdge->text(), id1, id2) )
352 const SMDS_MeshNode* aNode1 = aMesh->FindNode( id1 );
353 const SMDS_MeshNode* aNode2 = aMesh->FindNode( id2 );
355 if ( !aNode1 || !aNode2 || aNode1 == aNode2 )
358 // find a triangle and an edge index
359 const SMDS_MeshElement* tria1;
360 const SMDS_MeshElement* tria2;
362 if ( findTriangles(aNode1,aNode2,tria1,tria2) )
364 newIndices.Add(tria1->GetID());
366 const SMDS_MeshNode* a3Nodes[3];
367 SMDS_ElemIteratorPtr it;
369 for (i = 0, it = tria1->nodesIterator(); it->more(); i++) {
370 a3Nodes[ i ] = static_cast<const SMDS_MeshNode*>(it->next());
371 if (i > 0 && ( a3Nodes[ i ] == aNode1 && a3Nodes[ i - 1] == aNode2 ||
372 a3Nodes[ i ] == aNode2 && a3Nodes[ i - 1] == aNode1 ) ) {
377 newIndices.Add(-edgeInd-1);
379 myOkBtn->setEnabled(true);
380 myApplyBtn->setEnabled(true);
382 mySelector->AddOrRemoveIndex(anIO,newIndices, false);
383 SMESH::GetViewWindow(mySMESHGUI)->highlight( anIO, true, true );
388 //=======================================================================
389 // name : onSelectionDone()
390 // Purpose : SLOT called when selection changed
391 //=======================================================================
392 void SMESHGUI_SingleEditDlg::onSelectionDone()
395 BusyLocker lock(myBusy);
397 int anId1 = 0, anId2 = 0;
399 myOkBtn->setEnabled(false);
400 myApplyBtn->setEnabled(false);
403 mySelectionMgr->selectedObjects(aList);
405 if (aList.Extent() != 1) {
410 Handle(SALOME_InteractiveObject) anIO = aList.First();
411 myActor = SMESH::FindActorByEntry(anIO->getEntry());
413 TVisualObjPtr aVisualObj = myActor->GetObject();
414 if(SMDS_Mesh* aMesh = aVisualObj->GetMesh())
416 const SMDS_MeshElement* tria[2];
417 if( SMESH::GetEdgeNodes( mySelector, aVisualObj, anId1, anId2 ) >= 1 &&
418 findTriangles( aMesh->FindNode( anId1 ), aMesh->FindNode( anId2 ), tria[0],tria[1] ) )
420 QString aText = QString("%1-%2").arg(anId1).arg(anId2);
421 myEdge->setText(aText);
423 myOkBtn->setEnabled(true);
424 myApplyBtn->setEnabled(true);
434 //=======================================================================
435 // name : onDeactivate()
436 // Purpose : SLOT called when dialog must be deativated
437 //=======================================================================
438 void SMESHGUI_SingleEditDlg::onDeactivate()
443 //=======================================================================
444 // name : enterEvent()
445 // Purpose : Event filter
446 //=======================================================================
447 void SMESHGUI_SingleEditDlg::enterEvent (QEvent*)
450 mySMESHGUI->EmitSignalDeactivateDialog();
451 if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
452 aViewWindow->SetSelectionMode(EdgeOfCellSelection);
457 //=================================================================================
458 // function : closeEvent()
460 //=================================================================================
461 void SMESHGUI_SingleEditDlg::closeEvent (QCloseEvent*)
466 //=======================================================================
467 //function : hideEvent()
468 //purpose : caused by ESC key
469 //=======================================================================
470 void SMESHGUI_SingleEditDlg::hideEvent (QHideEvent*)
476 //=================================================================================
477 // function : onApply()
478 // purpose : SLOT. Called when apply button is pressed
479 //=================================================================================
480 bool SMESHGUI_SingleEditDlg::onApply()
482 if (mySMESHGUI->isActiveStudyLocked())
484 // verify validity of input data
488 // get mesh, actor and nodes
490 mySelectionMgr->selectedObjects(aList);
492 SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(aList.First());
494 if (aMesh->_is_nil()) {
495 SUIT_MessageBox::information(SMESH::GetDesktop(mySMESHGUI),
497 tr("SMESHG_NO_MESH"));
501 SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
502 int anId1= 0, anId2 = 0;
503 if (aMeshEditor->_is_nil() || !getNodeIds(myEdge->text(), anId1, anId2))
507 bool aResult = process(aMeshEditor.in(), anId1, anId2);
511 mySelector->ClearIndex();
512 mySelectionMgr->setSelectedObjects(aList, false);
520 //=================================================================================
521 // function : keyPressEvent()
523 //=================================================================================
524 void SMESHGUI_SingleEditDlg::keyPressEvent( QKeyEvent* e )
526 QDialog::keyPressEvent( e );
527 if ( e->isAccepted() )
530 if ( e->key() == Qt::Key_F1 ) {
537 * Class : SMESHGUI_TrianglesInversionDlg
538 * Description : Inversion of the diagonal of a pseudo-quadrangle formed by
539 * 2 neighboring triangles with 1 common edge
542 SMESHGUI_TrianglesInversionDlg
543 ::SMESHGUI_TrianglesInversionDlg(SMESHGUI* theModule)
544 : SMESHGUI_SingleEditDlg(theModule)
546 setWindowTitle(tr("CAPTION"));
547 myHelpFileName = "diagonal_inversion_of_elements_page.html";
550 SMESHGUI_TrianglesInversionDlg::~SMESHGUI_TrianglesInversionDlg()
554 bool SMESHGUI_TrianglesInversionDlg::process (SMESH::SMESH_MeshEditor_ptr theMeshEditor,
555 const int theId1, const int theId2)
557 return theMeshEditor->InverseDiag(theId1, theId2);
561 * Class : SMESHGUI_UnionOfTwoTrianglesDlg
562 * Description : Construction of a quadrangle by deletion of the
563 * common border of 2 neighboring triangles
566 SMESHGUI_UnionOfTwoTrianglesDlg
567 ::SMESHGUI_UnionOfTwoTrianglesDlg(SMESHGUI* theModule)
568 : SMESHGUI_SingleEditDlg(theModule)
570 setWindowTitle(tr("CAPTION"));
571 myHelpFileName = "uniting_two_triangles_page.html";
574 SMESHGUI_UnionOfTwoTrianglesDlg::~SMESHGUI_UnionOfTwoTrianglesDlg()
578 bool SMESHGUI_UnionOfTwoTrianglesDlg::process (SMESH::SMESH_MeshEditor_ptr theMeshEditor,
579 const int theId1, const int theId2)
581 return theMeshEditor->DeleteDiag(theId1, theId2);