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