Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_SingleEditDlg.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File   : SMESHGUI_SingleEditDlg.cxx
24 // Author : Sergey LITONIN, Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "SMESHGUI_SingleEditDlg.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_VTKUtils.h"
32 #include "SMESHGUI_MeshUtils.h"
33
34 #include <SMESH_Actor.h>
35 #include <SMDS_Mesh.hxx>
36
37 // SALOME GUI includes
38 #include <LightApp_SelectionMgr.h>
39 #include <LightApp_Application.h>
40 #include <SUIT_ResourceMgr.h>
41 #include <SUIT_MessageBox.h>
42 #include <SUIT_Desktop.h>
43 #include <SUIT_Session.h>
44
45 #include <SVTK_Selector.h>
46 #include <SVTK_ViewWindow.h>
47 #include <SALOME_ListIO.hxx>
48
49 // OCCT includes
50 #include <TColStd_MapOfInteger.hxx>
51 #include <TColStd_IndexedMapOfInteger.hxx>
52
53 // Qt includes
54 #include <QVBoxLayout>
55 #include <QHBoxLayout>
56 #include <QLineEdit>
57 #include <QPushButton>
58 #include <QGroupBox>
59 #include <QLabel>
60 #include <QValidator>
61 #include <QKeyEvent>
62
63 #define SPACING 6
64 #define MARGIN  11
65
66 /*!
67   \class BusyLocker
68   \brief Simple 'busy state' flag locker.
69   \internal
70 */
71
72 class BusyLocker
73 {
74 public:
75   //! Constructor. Sets passed boolean flag to \c true.
76   BusyLocker( bool& busy ) : myBusy( busy ) { myBusy = true; }
77   //! Destructor. Clear external boolean flag passed as parameter to the constructor to \c false.
78   ~BusyLocker() { myBusy = false; }
79 private:
80   bool& myBusy; //! External 'busy state' boolean flag
81 };
82
83 /*!
84  *  Class       : SMESHGUI_SingleEditDlg
85  *  Description : Inversion of the diagonal of a pseudo-quadrangle formed by
86  *                2 neighboring triangles with 1 common edge
87  */
88
89 //=======================================================================
90 // name    : SMESHGUI_SingleEditDlg()
91 // Purpose : Constructor
92 //=======================================================================
93 SMESHGUI_SingleEditDlg
94 ::SMESHGUI_SingleEditDlg(SMESHGUI* theModule)
95   : QDialog(SMESH::GetDesktop(theModule)),
96     mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
97     mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
98     mySMESHGUI(theModule)
99 {
100   setModal(false);
101
102   QVBoxLayout* aDlgLay = new QVBoxLayout(this);
103   aDlgLay->setMargin(MARGIN);
104   aDlgLay->setSpacing(SPACING);
105
106   QWidget* aMainFrame = createMainFrame  (this);
107   QWidget* aBtnFrame  = createButtonFrame(this);
108
109   aDlgLay->addWidget(aMainFrame);
110   aDlgLay->addWidget(aBtnFrame);
111
112   Init();
113 }
114
115 //=======================================================================
116 // name    : createMainFrame()
117 // Purpose : Create frame containing dialog's input fields
118 //=======================================================================
119 QWidget* SMESHGUI_SingleEditDlg::createMainFrame (QWidget* theParent)
120 {
121   QGroupBox* aMainGrp = new QGroupBox(tr("EDGE_BETWEEN"), theParent);
122   QHBoxLayout* aLay = new QHBoxLayout(aMainGrp);
123   aLay->setMargin(MARGIN);
124   aLay->setSpacing(SPACING);
125
126   QPixmap aPix (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
127
128   QLabel* aLab = new QLabel(tr("SMESH_EDGE"), aMainGrp);
129   QPushButton* aBtn = new QPushButton(aMainGrp);
130   aBtn->setIcon(aPix);
131   myEdge = new QLineEdit(aMainGrp);
132   myEdge->setValidator(new QRegExpValidator(QRegExp("[\\d]*-[\\d]*"), this));
133
134   aLay->addWidget(aLab);
135   aLay->addWidget(aBtn);
136   aLay->addWidget(myEdge);
137
138   return aMainGrp;
139 }
140
141 //=======================================================================
142 // name    : createButtonFrame()
143 // Purpose : Create frame containing buttons
144 //=======================================================================
145 QWidget* SMESHGUI_SingleEditDlg::createButtonFrame (QWidget* theParent)
146 {
147   QGroupBox* aFrame = new QGroupBox(theParent);
148
149   myOkBtn     = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aFrame);
150   myApplyBtn  = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
151   myCloseBtn  = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
152   myHelpBtn   = new QPushButton(tr("SMESH_BUT_HELP"),  aFrame);
153
154   QHBoxLayout* aLay = new QHBoxLayout(aFrame);
155   aLay->setMargin(MARGIN);
156   aLay->setSpacing(SPACING);
157
158   aLay->addWidget(myOkBtn);
159   aLay->addSpacing(10);
160   aLay->addWidget(myApplyBtn);
161   aLay->addSpacing(10);
162   aLay->addStretch();
163   aLay->addWidget(myCloseBtn);
164   aLay->addWidget(myHelpBtn);
165
166   return aFrame;
167 }
168
169 //=======================================================================
170 // name    : isValid()
171 // Purpose : Verify validity of input data
172 //=======================================================================
173 bool SMESHGUI_SingleEditDlg::isValid (const bool theMess) const
174 {
175   int id1, id2;
176   return getNodeIds(myEdge->text(), id1, id2);
177 }
178
179 //=======================================================================
180 // name    : getNodeIds()
181 // Purpose : Retrieve node ids from string
182 //=======================================================================
183 bool SMESHGUI_SingleEditDlg::getNodeIds (const QString& theStr,
184                                          int& theId1, int&  theId2) const
185 {
186   if (!theStr.contains('-'))
187     return false;
188
189   bool ok1, ok2;
190   QString str1 = theStr.section('-', 0, 0, QString::SectionSkipEmpty);
191   QString str2 = theStr.section('-', 1, 1, QString::SectionSkipEmpty);
192   theId1 = str1.toInt(&ok1);
193   theId2 = str2.toInt(&ok2);
194
195   return ok1 & ok2;
196 }
197
198 //=======================================================================
199 // name    : ~SMESHGUI_SingleEditDlg()
200 // Purpose : Destructor
201 //=======================================================================
202 SMESHGUI_SingleEditDlg::~SMESHGUI_SingleEditDlg()
203 {
204 }
205
206 //=======================================================================
207 // name    : Init()
208 // Purpose : Init dialog fields, connect signals and slots, show dialog
209 //=======================================================================
210 void SMESHGUI_SingleEditDlg::Init()
211 {
212   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
213   myBusy = false;
214   myActor = 0;
215
216   // main buttons
217   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
218   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
219   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
220   connect(myHelpBtn,  SIGNAL(clicked()), SLOT(onHelp()));
221
222   // selection and SMESHGUI
223   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
224   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
225   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
226   connect(myEdge, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
227
228   myOkBtn->setEnabled(false);
229   myApplyBtn->setEnabled(false);
230   setEnabled(true);
231
232   // set selection mode
233   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
234     aViewWindow->SetSelectionMode(EdgeOfCellSelection);
235
236   onSelectionDone();
237 }
238
239 //=======================================================================
240 // name    : onOk()
241 // Purpose : SLOT called when "Ok" button pressed.
242 //           Assign filters VTK viewer and close dialog
243 //=======================================================================
244 void SMESHGUI_SingleEditDlg::onOk()
245 {
246   if (onApply())
247     onClose();
248 }
249
250 //=======================================================================
251 // name    : onClose()
252 // Purpose : SLOT called when "Close" button pressed. Close dialog
253 //=======================================================================
254 void SMESHGUI_SingleEditDlg::onClose()
255 {
256   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
257     aViewWindow->SetSelectionMode(ActorSelection);
258   //mySelectionMgr->clearSelected();
259   disconnect(mySelectionMgr, 0, this, 0);
260   disconnect(mySMESHGUI, 0, this, 0);
261   mySMESHGUI->ResetState();
262   reject();
263 }
264
265 //=================================================================================
266 // function : onHelp()
267 // purpose  :
268 //=================================================================================
269 void SMESHGUI_SingleEditDlg::onHelp()
270 {
271   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
272   if (app) 
273     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
274   else {
275     QString platform;
276 #ifdef WIN32
277     platform = "winapplication";
278 #else
279     platform = "application";
280 #endif
281     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
282                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
283                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
284                                                                  platform)).
285                              arg(myHelpFileName));
286   }
287 }
288
289 //=======================================================================
290 //function : findTriangles()
291 //purpose  : find triangles sharing theNode1-theNode2 link
292 //           THIS IS A PIECE OF SMESH_MeshEditor.cxx
293 //           TO DO: make it available in SMDS for ex.
294 //=======================================================================
295 static bool findTriangles (const SMDS_MeshNode *    theNode1,
296                            const SMDS_MeshNode *    theNode2,
297                            const SMDS_MeshElement*& theTria1,
298                            const SMDS_MeshElement*& theTria2)
299 {
300   if (!theNode1 || !theNode2) return false;
301
302   theTria1 = theTria2 = 0;
303
304   std::set< const SMDS_MeshElement* > emap;
305   SMDS_ElemIteratorPtr it = theNode1->GetInverseElementIterator();
306   while (it->more()) {
307     const SMDS_MeshElement* elem = it->next();
308     if (elem->GetType() == SMDSAbs_Face && elem->NbNodes() == 3)
309       emap.insert(elem);
310   }
311   it = theNode2->GetInverseElementIterator();
312   while (it->more()) {
313     const SMDS_MeshElement* elem = it->next();
314     if (elem->GetType() == SMDSAbs_Face &&
315          emap.find(elem) != emap.end())
316     {
317       if (theTria1) {
318         theTria2 = elem;
319         break;
320       } else {
321         theTria1 = elem;
322       }
323     }
324   }
325   return (theTria1 && theTria2);
326 }
327
328 //=======================================================================
329 //function : onTextChange()
330 //purpose  :
331 //=======================================================================
332 void SMESHGUI_SingleEditDlg::onTextChange (const QString& theNewText)
333 {
334   if (myBusy) return;
335   BusyLocker lock(myBusy);
336
337   myOkBtn->setEnabled(false);
338   myApplyBtn->setEnabled(false);
339
340   // hilight entered edge
341   if(myActor){
342     if(SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh()){
343       Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
344       SALOME_ListIO aList;
345       aList.Append(anIO);
346       mySelectionMgr->setSelectedObjects(aList,false);
347       
348       TColStd_IndexedMapOfInteger selectedIndices;
349       TColStd_MapOfInteger newIndices;
350       mySelector->GetIndex(anIO,selectedIndices);
351
352       int id1, id2;
353       if ( !getNodeIds(myEdge->text(), id1, id2) )
354         return;
355
356       const SMDS_MeshNode* aNode1 = aMesh->FindNode( id1 );
357       const SMDS_MeshNode* aNode2 = aMesh->FindNode( id2 );
358
359       if ( !aNode1 || !aNode2 || aNode1 == aNode2 )
360         return;
361
362       // find a triangle and an edge index
363       const SMDS_MeshElement* tria1;
364       const SMDS_MeshElement* tria2;
365
366       if ( findTriangles(aNode1,aNode2,tria1,tria2) )
367       {
368         newIndices.Add(tria1->GetID());
369
370         const SMDS_MeshNode* a3Nodes[3];
371         SMDS_ElemIteratorPtr it;
372         int edgeInd = 2, i;
373         for (i = 0, it = tria1->nodesIterator(); it->more(); i++) {
374           a3Nodes[ i ] = static_cast<const SMDS_MeshNode*>(it->next());
375           if (i > 0 && ( (a3Nodes[ i ] == aNode1 && a3Nodes[ i - 1] == aNode2) ||
376                          (a3Nodes[ i ] == aNode2 && a3Nodes[ i - 1] == aNode1) ) ) {
377             edgeInd = i - 1;
378             break;
379           }
380         }
381         newIndices.Add(-edgeInd-1);
382         
383         myOkBtn->setEnabled(true);
384         myApplyBtn->setEnabled(true);
385       }
386       mySelector->AddOrRemoveIndex(anIO,newIndices, false);
387       SMESH::GetViewWindow(mySMESHGUI)->highlight( anIO, true, true );
388     }
389   }
390 }
391
392 //=======================================================================
393 // name    : onSelectionDone()
394 // Purpose : SLOT called when selection changed
395 //=======================================================================
396 void SMESHGUI_SingleEditDlg::onSelectionDone()
397 {
398   if (myBusy) return;
399   BusyLocker lock(myBusy);
400
401   int anId1 = 0, anId2 = 0;
402
403   myOkBtn->setEnabled(false);
404   myApplyBtn->setEnabled(false);
405
406   SALOME_ListIO aList;
407   mySelectionMgr->selectedObjects(aList);
408
409   if (aList.Extent() != 1) {
410     myEdge->clear();
411     return;
412   }
413
414   Handle(SALOME_InteractiveObject) anIO = aList.First();
415   myActor = SMESH::FindActorByEntry(anIO->getEntry());
416   if(myActor){
417     TVisualObjPtr aVisualObj = myActor->GetObject();
418     if(SMDS_Mesh* aMesh = aVisualObj->GetMesh())
419     {
420       const SMDS_MeshElement* tria[2];
421       if( SMESH::GetEdgeNodes( mySelector, aVisualObj, anId1, anId2 ) >= 1 &&
422           findTriangles( aMesh->FindNode( anId1 ), aMesh->FindNode( anId2 ), tria[0],tria[1] ) )
423       {
424         QString aText = QString("%1-%2").arg(anId1).arg(anId2);
425         myEdge->setText(aText);
426         
427         myOkBtn->setEnabled(true);
428         myApplyBtn->setEnabled(true);
429       }
430       else
431       {
432         myEdge->clear();
433       }
434     }
435   }
436 }
437
438 //=======================================================================
439 // name    : onDeactivate()
440 // Purpose : SLOT called when dialog must be deativated
441 //=======================================================================
442 void SMESHGUI_SingleEditDlg::onDeactivate()
443 {
444   setEnabled(false);
445 }
446
447 //=======================================================================
448 // name    : enterEvent()
449 // Purpose : Event filter
450 //=======================================================================
451 void SMESHGUI_SingleEditDlg::enterEvent (QEvent*)
452 {
453   if (!isEnabled()) {
454     mySMESHGUI->EmitSignalDeactivateDialog();
455     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
456       aViewWindow->SetSelectionMode(EdgeOfCellSelection);
457     setEnabled(true);
458   }
459 }
460
461 //=================================================================================
462 // function : closeEvent()
463 // purpose  :
464 //=================================================================================
465 void SMESHGUI_SingleEditDlg::closeEvent (QCloseEvent*)
466 {
467   onClose();
468 }
469
470 //=======================================================================
471 //function : hideEvent()
472 //purpose  : caused by ESC key
473 //=======================================================================
474 void SMESHGUI_SingleEditDlg::hideEvent (QHideEvent*)
475 {
476   if (!isMinimized())
477     onClose();
478 }
479
480 //=================================================================================
481 // function : onApply()
482 // purpose  : SLOT. Called when apply button is pressed
483 //=================================================================================
484 bool SMESHGUI_SingleEditDlg::onApply()
485 {
486   if (mySMESHGUI->isActiveStudyLocked())
487     return false;
488   // verify validity of input data
489   if (!isValid(true))
490     return false;
491
492   // get mesh, actor and nodes
493   SALOME_ListIO aList;
494   mySelectionMgr->selectedObjects(aList);
495
496   SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(aList.First());
497
498   if (aMesh->_is_nil()) {
499     SUIT_MessageBox::information(SMESH::GetDesktop(mySMESHGUI), 
500                                  tr("SMESH_ERROR"),
501                                  tr("SMESHG_NO_MESH"));
502     return false;
503   }
504
505   SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
506   int anId1= 0, anId2 = 0;
507   if (aMeshEditor->_is_nil() || !getNodeIds(myEdge->text(), anId1, anId2))
508     return false;
509
510   // perform operation
511   bool aResult = process(aMeshEditor.in(), anId1, anId2);
512
513   // update actor
514   if (aResult) {
515     mySelector->ClearIndex();
516     mySelectionMgr->setSelectedObjects(aList, false);
517     onSelectionDone();
518     SMESH::UpdateView();
519     SMESHGUI::Modified();
520   }
521
522   return aResult;
523 }
524
525 //=================================================================================
526 // function : keyPressEvent()
527 // purpose  :
528 //=================================================================================
529 void SMESHGUI_SingleEditDlg::keyPressEvent( QKeyEvent* e )
530 {
531   QDialog::keyPressEvent( e );
532   if ( e->isAccepted() )
533     return;
534
535   if ( e->key() == Qt::Key_F1 ) {
536     e->accept();
537     onHelp();
538   }
539 }
540
541 /*!
542  *  Class       : SMESHGUI_TrianglesInversionDlg
543  *  Description : Inversion of the diagonal of a pseudo-quadrangle formed by
544  *                2 neighboring triangles with 1 common edge
545  */
546
547 SMESHGUI_TrianglesInversionDlg
548 ::SMESHGUI_TrianglesInversionDlg(SMESHGUI* theModule)
549 : SMESHGUI_SingleEditDlg(theModule)
550 {
551   setWindowTitle(tr("CAPTION"));
552   myHelpFileName = "diagonal_inversion_of_elements_page.html";
553 }
554
555 SMESHGUI_TrianglesInversionDlg::~SMESHGUI_TrianglesInversionDlg()
556 {
557 }
558
559 bool SMESHGUI_TrianglesInversionDlg::process (SMESH::SMESH_MeshEditor_ptr theMeshEditor,
560                                               const int theId1, const int theId2)
561 {
562   return theMeshEditor->InverseDiag(theId1, theId2);
563 }
564
565 /*!
566  *  Class       : SMESHGUI_UnionOfTwoTrianglesDlg
567  *  Description : Construction of a quadrangle by deletion of the
568  *                common border of 2 neighboring triangles
569  */
570
571 SMESHGUI_UnionOfTwoTrianglesDlg
572 ::SMESHGUI_UnionOfTwoTrianglesDlg(SMESHGUI* theModule)
573 : SMESHGUI_SingleEditDlg(theModule)
574 {
575   setWindowTitle(tr("CAPTION"));
576   myHelpFileName = "uniting_two_triangles_page.html";
577 }
578
579 SMESHGUI_UnionOfTwoTrianglesDlg::~SMESHGUI_UnionOfTwoTrianglesDlg()
580 {
581 }
582
583 bool SMESHGUI_UnionOfTwoTrianglesDlg::process (SMESH::SMESH_MeshEditor_ptr theMeshEditor,
584                                                const int theId1, const int theId2)
585 {
586   return theMeshEditor->DeleteDiag(theId1, theId2);
587 }