Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_StandardMeshInfosDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SMESHGUI_StandardMeshInfosDlg.cxx
25 //  Author : Michael ZORIN
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_StandardMeshInfosDlg.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_MeshUtils.h"
34
35 #include "SMESH.hxx"
36 #include "SMESH_TypeFilter.hxx"
37
38 #include "SALOMEDSClient_Study.hxx"
39 #include "SALOMEDSClient_SObject.hxx"
40
41 #include "SUIT_Desktop.h"
42 #include "SUIT_Session.h"
43 #include "SUIT_OverrideCursor.h"
44
45 #include "LightApp_SelectionMgr.h"
46 #include "SALOME_ListIO.hxx"
47
48 #include "utilities.h"
49
50 // QT Includes
51 #include <qgroupbox.h>
52 #include <qlabel.h>
53 #include <qlayout.h>
54 #include <qlineedit.h>
55 #include <qtextbrowser.h>
56 #include <qmap.h>
57 #include <qpushbutton.h>
58
59 // IDL Headers
60 #include "SALOMEconfig.h"
61 #include CORBA_SERVER_HEADER(SMESH_Mesh)
62 #include CORBA_SERVER_HEADER(SMESH_Group)
63 #include CORBA_SERVER_HEADER(GEOM_Gen)
64
65 using namespace std;
66
67
68 //=================================================================================
69 /*!
70  *  SMESHGUI_StandardMeshInfosDlg::SMESHGUI_StandardMeshInfosDlg
71  *
72  *  Constructor
73  */
74 //=================================================================================
75 SMESHGUI_StandardMeshInfosDlg::SMESHGUI_StandardMeshInfosDlg( SMESHGUI* theModule, const char* name,
76                                                               bool modal, WFlags fl)
77      : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder |
78                 WStyle_Title | WStyle_SysMenu | WDestructiveClose),
79      mySMESHGUI( theModule ),
80      mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
81 {
82   if (!name)
83       setName("SMESHGUI_StandardMeshInfosDlg");
84   setCaption(tr("SMESH_STANDARD_MESHINFO_TITLE" ));
85   setSizeGripEnabled(TRUE);
86
87   myStartSelection = true;
88   myIsActiveWindow = true;
89
90   // dialog layout
91   QGridLayout* aDlgLayout = new QGridLayout(this);
92   aDlgLayout->setSpacing(6);
93   aDlgLayout->setMargin(11);
94
95   // mesh group box
96   myMeshGroup = new QGroupBox(this, "myMeshGroup");
97   myMeshGroup->setTitle(tr("SMESH_MESH"));
98   myMeshGroup->setColumnLayout(0, Qt::Vertical);
99   myMeshGroup->layout()->setSpacing(0);
100   myMeshGroup->layout()->setMargin(0);
101   QGridLayout* myMeshGroupLayout = new QGridLayout(myMeshGroup->layout());
102   myMeshGroupLayout->setAlignment(Qt::AlignTop);
103   myMeshGroupLayout->setSpacing(6);
104   myMeshGroupLayout->setMargin(11);
105
106   // select button, label and line edit with mesh name
107   myNameLab = new QLabel(myMeshGroup, "myNameLab");
108   myNameLab->setText(tr("SMESH_NAME" ));
109   myMeshGroupLayout->addWidget(myNameLab, 0, 0);
110
111   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("SMESH",tr("ICON_SELECT")));
112   mySelectBtn = new QPushButton(myMeshGroup, "mySelectBtn");
113   mySelectBtn->setPixmap(image0);
114   mySelectBtn->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
115   myMeshGroupLayout->addWidget(mySelectBtn, 0, 1);
116
117   myMeshLine = new QLineEdit(myMeshGroup, "myMeshLine");
118   myMeshGroupLayout->addWidget(myMeshLine, 0, 2);
119
120   aDlgLayout->addWidget(myMeshGroup, 0, 0);
121
122   // information group box
123   myInfoGroup  = new QGroupBox(this, "myInfoGroup");
124   myInfoGroup->setTitle(tr("SMESH_INFORMATION"));
125   myInfoGroup->setColumnLayout(0, Qt::Vertical);
126   myInfoGroup->layout()->setSpacing(0);
127   myInfoGroup->layout()->setMargin(0);
128   QGridLayout* myInfoGroupLayout = new QGridLayout(myInfoGroup->layout());
129   myInfoGroupLayout->setAlignment(Qt::AlignTop);
130   myInfoGroupLayout->setSpacing(6);
131   myInfoGroupLayout->setMargin(11);
132
133   // information text browser
134   myInfo = new QTextBrowser(myInfoGroup, "myInfo");
135   myInfoGroupLayout->addWidget(myInfo, 0, 0);
136
137   aDlgLayout->addWidget(myInfoGroup, 1, 0);
138
139   // buttons group
140   myButtonsGroup = new QGroupBox(this, "myButtonsGroup");
141   myButtonsGroup->setColumnLayout(0, Qt::Vertical);
142   myButtonsGroup->layout()->setSpacing(0);  myButtonsGroup->layout()->setMargin(0);
143   QHBoxLayout* myButtonsGroupLayout = new QHBoxLayout(myButtonsGroup->layout());
144   myButtonsGroupLayout->setAlignment(Qt::AlignTop);
145   myButtonsGroupLayout->setSpacing(6); myButtonsGroupLayout->setMargin(11);
146
147   // buttons --> OK button
148   myOkBtn = new QPushButton(tr("SMESH_BUT_OK" ), myButtonsGroup, "myOkBtn");
149   myOkBtn->setAutoDefault(TRUE); myOkBtn->setDefault(TRUE);
150   myButtonsGroupLayout->addStretch();
151   myButtonsGroupLayout->addWidget(myOkBtn);
152   myButtonsGroupLayout->addStretch();
153
154   aDlgLayout->addWidget(myButtonsGroup, 2, 0);
155
156   mySMESHGUI->SetActiveDialogBox(this);
157
158   // connect signals
159   connect( myOkBtn,         SIGNAL(clicked()),                      this, SLOT(close()));
160   connect( mySelectBtn,     SIGNAL(clicked()),                      this, SLOT(onStartSelection()));
161   connect( mySMESHGUI,      SIGNAL(SignalCloseAllDialogs()),        this, SLOT(close()));
162   connect( mySMESHGUI,      SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
163   connect( mySelectionMgr,  SIGNAL(currentSelectionChanged()),      this, SLOT(onSelectionChanged()));
164
165   // resize and move dialog, then show
166   this->setMinimumSize(270, 428);
167   this->show();
168
169   // init dialog with current selection
170   myMeshFilter = new SMESH_TypeFilter (MESH);
171   mySelectionMgr->installFilter(myMeshFilter);
172   onSelectionChanged();
173 }
174
175 //=================================================================================
176 /*!
177  *  SMESHGUI_StandardMeshInfosDlg::~SMESHGUI_StandardMeshInfosDlg
178  *
179  *  Destructor
180  */
181 //=================================================================================
182 SMESHGUI_StandardMeshInfosDlg::~SMESHGUI_StandardMeshInfosDlg()
183 {
184 }
185
186 //=================================================================================
187 /*!
188  *  SMESHGUI_StandardMeshInfosDlg::DumpMeshInfos
189  */
190 //=================================================================================
191 void SMESHGUI_StandardMeshInfosDlg::DumpMeshInfos()
192 {
193   SUIT_OverrideCursor wc;
194
195   SALOME_ListIO aList;
196   mySelectionMgr->selectedObjects(aList);
197
198   int nbSel = aList.Extent();
199   myInfo->clear();
200   if (nbSel == 1) {
201     myStartSelection = false;
202     myMeshLine->setText("");
203     SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(aList.First());
204
205     if (!aMesh->_is_nil()) {
206       QString aName, anInfo;
207       SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aName);
208       myMeshLine->setText(aName);
209       int aNbNodes =   (int)aMesh->NbNodes();
210       int aNbEdges =   (int)aMesh->NbEdges();
211       int aNbFaces =   (int)aMesh->NbFaces();
212       int aNbVolumes = (int)aMesh->NbVolumes();
213
214       int aDimension = 0;
215       double aNbDimElements = 0;
216       if (aNbVolumes > 0) {
217         aNbDimElements = aNbVolumes;
218         aDimension = 3;
219       }
220       else if(aNbFaces > 0) {
221         aNbDimElements = aNbFaces;
222         aDimension = 2;
223       }
224       else if(aNbEdges > 0) {
225         aNbDimElements = aNbEdges;
226         aDimension = 1;
227       }
228       else if(aNbNodes > 0) {
229         aNbDimElements = aNbNodes;
230         aDimension = 0;
231       }
232
233       // information about the mesh
234       anInfo.append(QString("Nb of element of dimension %1:<b> %2</b><br>").arg(aDimension).arg(aNbDimElements));
235       anInfo.append(QString("Nb of nodes: <b>%1</b><br><br>").arg(aNbNodes));
236
237       // information about the groups of the mesh
238       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
239       _PTR(SObject) aMeshSO = SMESH::FindSObject(aMesh);
240       _PTR(SObject) anObj;
241
242       bool hasGroup = false;
243
244       // info about groups on nodes
245       aMeshSO->FindSubObject(Tag_NodeGroups, anObj);
246       if (anObj) {
247         _PTR(ChildIterator) it = aStudy->NewChildIterator(anObj);
248         if (it->More()) {
249           anInfo.append(QString("Groups:<br><br>"));
250           hasGroup = true;
251         }
252         for (; it->More(); it->Next()) {
253           _PTR(SObject) subObj = it->Value();
254           CORBA::Object_var anObject = SMESH::SObjectToObject(subObj);
255           SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(anObject);
256           if (!aGroup->_is_nil()) {
257             anInfo.append(QString("-   <b>%1</b><br>").arg(aGroup->GetName()));
258             anInfo.append(QString("%1<br>").arg("on nodes"));
259             anInfo.append(QString("%1<br>").arg(aGroup->Size()));
260             // check if the group based on geometry
261             SMESH::SMESH_GroupOnGeom_var aGroupOnGeom = SMESH::SMESH_GroupOnGeom::_narrow(aGroup);
262             if (!aGroupOnGeom->_is_nil()) {
263               GEOM::GEOM_Object_var aGroupMainShape = aGroupOnGeom->GetShape();
264               QString aShapeName = "<unknown>";
265               _PTR(SObject) aGeomObj, aRef;
266               if (subObj->FindSubObject(1, aGeomObj) &&  aGeomObj->ReferencedObject(aRef))
267                 aShapeName = aRef->GetName().c_str();
268               anInfo.append(QString("based on <i>%1</i> geometry object<br><br>").arg(aShapeName));
269             } else {
270               anInfo.append(QString("<br>"));
271             }
272           }
273         }
274       }
275
276       // info about groups on edges
277       anObj.reset();
278       aMeshSO->FindSubObject(Tag_EdgeGroups, anObj);
279       if (anObj) {
280         _PTR(ChildIterator) it = aStudy->NewChildIterator(anObj);
281         if (!hasGroup && it->More()) {
282           anInfo.append(QString("Groups:<br><br>"));
283           hasGroup = true;
284         }
285         for (; it->More(); it->Next()) {
286           _PTR(SObject) subObj = it->Value();
287           CORBA::Object_var anObject = SMESH::SObjectToObject(subObj);
288           SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(anObject);
289           if (!aGroup->_is_nil()) {
290             anInfo.append(QString("-   <b>%1</b><br>").arg(aGroup->GetName()));
291             anInfo.append(QString("%1<br>").arg("on edges"));
292             anInfo.append(QString("%1<br>").arg(aGroup->Size()));
293             // check if the group based on geometry
294             SMESH::SMESH_GroupOnGeom_var aGroupOnGeom = SMESH::SMESH_GroupOnGeom::_narrow(aGroup);
295             if (!aGroupOnGeom->_is_nil()) {
296               GEOM::GEOM_Object_var aGroupMainShape = aGroupOnGeom->GetShape();
297               QString aShapeName = "<unknown>";
298               _PTR(SObject) aGeomObj, aRef;
299               if (subObj->FindSubObject(1, aGeomObj) && aGeomObj->ReferencedObject(aRef))
300                 aShapeName = aRef->GetName().c_str();
301               anInfo.append(QString("based on <i>%1</i> geometry object<br><br>").arg(aShapeName));
302             } else {
303               anInfo.append(QString("<br>"));
304             }
305           }
306         }
307       }
308
309       // info about groups on faces
310       anObj.reset();
311       aMeshSO->FindSubObject(Tag_FaceGroups , anObj);
312       if (anObj) {
313         _PTR(ChildIterator) it = aStudy->NewChildIterator(anObj);
314         if (!hasGroup && it->More()) {
315           anInfo.append(QString("Groups:<br><br>"));
316           hasGroup = true;
317         }
318         for (; it->More(); it->Next()) {
319           _PTR(SObject) subObj = it->Value();
320           CORBA::Object_var anObject = SMESH::SObjectToObject(subObj);
321           SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(anObject);
322           if (!aGroup->_is_nil()) {
323             anInfo.append(QString("-   <b>%1</b><br>").arg(aGroup->GetName()));
324             anInfo.append(QString("%1<br>").arg("on faces"));
325             anInfo.append(QString("%1<br>").arg(aGroup->Size()));
326             // check if the group based on geometry
327             SMESH::SMESH_GroupOnGeom_var aGroupOnGeom = SMESH::SMESH_GroupOnGeom::_narrow(aGroup);
328             if (!aGroupOnGeom->_is_nil()) {
329               GEOM::GEOM_Object_var aGroupMainShape = aGroupOnGeom->GetShape();
330               QString aShapeName = "<unknown>";
331               _PTR(SObject) aGeomObj, aRef;
332               if (subObj->FindSubObject(1, aGeomObj) && aGeomObj->ReferencedObject(aRef))
333                 aShapeName = aRef->GetName().c_str();
334               anInfo.append(QString("based on <i>%1</i> geometry object<br><br>").arg(aShapeName));
335             } else {
336               anInfo.append(QString("<br>"));
337             }
338           }
339         }
340       }
341
342       // info about groups on volumes
343       anObj.reset();
344       aMeshSO->FindSubObject(Tag_VolumeGroups, anObj);
345       if (anObj) {
346         _PTR(ChildIterator) it = aStudy->NewChildIterator(anObj);
347         if (!hasGroup && it->More())
348           anInfo.append(QString("Groups:<br>"));
349         for (; it->More(); it->Next()) {
350           _PTR(SObject) subObj = it->Value();
351           CORBA::Object_var anObject = SMESH::SObjectToObject(subObj);
352           SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(anObject);
353           if (!aGroup->_is_nil()) {
354             anInfo.append(QString("-   <b>%1</b><br>").arg(aGroup->GetName()));
355             anInfo.append(QString("%1<br>").arg("on volumes"));
356             anInfo.append(QString("%1<br>").arg(aGroup->Size()));
357             // check if the group based on geometry
358             SMESH::SMESH_GroupOnGeom_var aGroupOnGeom = SMESH::SMESH_GroupOnGeom::_narrow(aGroup);
359             if (!aGroupOnGeom->_is_nil()) {
360               GEOM::GEOM_Object_var aGroupMainShape = aGroupOnGeom->GetShape();
361               QString aShapeName = "<unknown>";
362               _PTR(SObject) aGeomObj, aRef;
363               if (subObj->FindSubObject(1, aGeomObj) &&  aGeomObj->ReferencedObject(aRef))
364                 aShapeName = aRef->GetName().c_str();
365               anInfo.append(QString("based on <i>%1</i> geometry object<br><br>").arg(aShapeName));
366             } else {
367               anInfo.append(QString("<br>"));
368             }
369           }
370         }
371       }
372
373       myInfo->setText(anInfo);
374       return;
375     }
376   }
377 }
378
379 //=================================================================================
380 // function : SelectionIntoArgument()
381 // purpose  : Called when selection has changed
382 //=================================================================================
383 void SMESHGUI_StandardMeshInfosDlg::onSelectionChanged()
384 {
385   if (myStartSelection)
386     DumpMeshInfos();
387 }
388
389 //=================================================================================
390 // function : closeEvent()
391 // purpose  :
392 //=================================================================================
393 void SMESHGUI_StandardMeshInfosDlg::closeEvent (QCloseEvent* e)
394 {
395   mySelectionMgr->clearFilters();
396   mySMESHGUI->ResetState();
397   QDialog::closeEvent(e);
398 }
399
400 //=================================================================================
401 // function : windowActivationChange()
402 // purpose  : called when window is activated/deactivated
403 //=================================================================================
404 void SMESHGUI_StandardMeshInfosDlg::windowActivationChange (bool oldActive)
405 {
406   QDialog::windowActivationChange(oldActive);
407   if (isActiveWindow() && myIsActiveWindow != isActiveWindow())
408     ActivateThisDialog();
409   myIsActiveWindow = isActiveWindow();
410 }
411
412 //=================================================================================
413 // function : DeactivateActiveDialog()
414 // purpose  :
415 //=================================================================================
416 void SMESHGUI_StandardMeshInfosDlg::DeactivateActiveDialog()
417 {
418   disconnect(mySelectionMgr, 0, this, 0);
419 }
420
421 //=================================================================================
422 // function : ActivateThisDialog()
423 // purpose  :
424 //=================================================================================
425 void SMESHGUI_StandardMeshInfosDlg::ActivateThisDialog()
426 {
427   /* Emit a signal to deactivate any active dialog */
428   mySMESHGUI->EmitSignalDeactivateDialog();
429   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(onSelectionChanged()));
430 }
431
432 //=================================================================================
433 // function : onStartSelection()
434 // purpose  : starts selection
435 //=================================================================================
436 void SMESHGUI_StandardMeshInfosDlg::onStartSelection()
437 {
438   myStartSelection = true;
439   mySelectionMgr->installFilter(myMeshFilter);
440   myMeshLine->setText(tr("Select a mesh"));
441   onSelectionChanged();
442   myStartSelection = true;
443 }