Salome HOME
0019296: EDF 681 SMESH - Pre-evaluation of the number of elements before mesh
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshInfosDlg.cxx
1 //  Copyright (C) 2007-2008  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 // SMESH SMESHGUI : GUI for SMESH component
23 // File   : SMESHGUI_MeshInfosDlg.cxx
24 // Author : Nicolas BARBEROU
25 // SMESH includes
26 //
27 #include "SMESHGUI_MeshInfosDlg.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_MeshInfosBox.h"
32
33 // SALOME GUI includes
34 #include <SUIT_Desktop.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_OverrideCursor.h>
37 #include <SUIT_Session.h>
38 #include <SUIT_MessageBox.h>
39
40 #include <LightApp_SelectionMgr.h>
41 #include <LightApp_Application.h>
42 #include <SALOME_ListIO.hxx>
43
44 // SALOME KERNEL includes
45 #include <SALOMEDSClient_Study.hxx>
46
47 // Qt includes
48 #include <QGroupBox>
49 #include <QLabel>
50 #include <QFrame>
51 #include <QStackedWidget>
52 #include <QVBoxLayout>
53 #include <QHBoxLayout>
54 #include <QGridLayout>
55 #include <QPushButton>
56 #include <QKeyEvent>
57
58 // IDL includes
59 #include <SALOMEconfig.h>
60 #include CORBA_SERVER_HEADER(SMESH_Mesh)
61 #include CORBA_SERVER_HEADER(SMESH_Group)
62
63 #define COLONIZE(str)   (QString(str).contains(":") > 0 ? QString(str) : QString(str) + " :" )
64 #define SPACING 6
65 #define MARGIN  11
66
67 //=================================================================================
68 // function : SMESHGUI_MeshInfosDlg()
69 // purpose  : Constructor
70 //=================================================================================
71 SMESHGUI_MeshInfosDlg::SMESHGUI_MeshInfosDlg(SMESHGUI* theModule): 
72   QDialog(SMESH::GetDesktop(theModule)),
73   mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
74   mySMESHGUI(theModule)
75 {
76   setModal( false );
77   setAttribute( Qt::WA_DeleteOnClose, true );
78   setWindowTitle(tr("SMESH_MESHINFO_TITLE"));
79   setSizeGripEnabled(true);
80
81   myStartSelection = true;
82   myIsActiveWindow = true;
83
84   QVBoxLayout* aTopLayout = new QVBoxLayout(this);
85   aTopLayout->setSpacing(SPACING);  aTopLayout->setMargin(MARGIN);
86
87   // select button & label
88   QPixmap image0(SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH",tr("ICON_SELECT")));
89   mySelectBtn = new QPushButton(this);
90   mySelectBtn->setIcon(image0);
91   mySelectBtn->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
92
93   mySelectLab = new QLabel(this);
94   mySelectLab->setAlignment(Qt::AlignCenter);
95   QFont fnt = mySelectLab->font(); fnt.setBold(true);
96   mySelectLab->setFont(fnt);
97
98   QHBoxLayout* aSelectLayout = new QHBoxLayout;
99   aSelectLayout->setMargin(0); aSelectLayout->setSpacing(0);
100   aSelectLayout->addWidget(mySelectBtn);
101   aSelectLayout->addWidget(mySelectLab);
102
103   // top widget stack
104   myWGStack = new QStackedWidget(this);
105
106   // no valid selection
107   QWidget* myBadWidget = new QWidget(myWGStack);
108   QVBoxLayout* aBadLayout = new QVBoxLayout(myBadWidget);
109   QLabel* myBadLab = new QLabel(tr("SMESH_BAD_SELECTION"), myBadWidget);
110   myBadLab->setAlignment(Qt::AlignCenter);
111   myBadLab->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
112   aBadLayout->addWidget(myBadLab);
113   myWGStack->addWidget(myBadWidget);
114
115   // mesh
116   myMeshWidget = new QWidget(myWGStack);
117   QGridLayout* aMeshLayout = new QGridLayout(myMeshWidget);
118   aMeshLayout->setSpacing(SPACING);  aMeshLayout->setMargin(0);
119   myWGStack->addWidget(myMeshWidget);
120
121   // --> name
122   QLabel* myMeshNameLab = new QLabel(COLONIZE(tr("SMESH_MESHINFO_NAME")), myMeshWidget);
123   myMeshName    = new QLabel(myMeshWidget);
124   myMeshName->setMinimumWidth(100);
125   QFrame* line1 = new QFrame(myMeshWidget);
126   line1->setFrameStyle(QFrame::HLine | QFrame::Sunken);
127
128   myMeshInfoBox = new SMESHGUI_MeshInfosBox(true, myMeshWidget);
129
130   aMeshLayout->addWidget(myMeshNameLab,      0, 0);
131   aMeshLayout->addWidget(myMeshName,         0, 1);
132   aMeshLayout->addWidget(line1,              1, 0, 1, 2);
133   aMeshLayout->addWidget(myMeshInfoBox,      2, 0, 1, 2);
134   aMeshLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), 3, 0);
135
136   // buttons
137   myButtonsGroup = new QGroupBox(this);
138   QHBoxLayout* myButtonsGroupLayout = new QHBoxLayout(myButtonsGroup);
139   myButtonsGroupLayout->setSpacing(SPACING); myButtonsGroupLayout->setMargin(MARGIN);
140
141   // buttons --> OK and Help buttons
142   myOkBtn = new QPushButton(tr("SMESH_BUT_OK" ), myButtonsGroup);
143   myOkBtn->setAutoDefault(true); myOkBtn->setDefault(true);
144   myHelpBtn = new QPushButton(tr("SMESH_BUT_HELP" ), myButtonsGroup);
145   myHelpBtn->setAutoDefault(true);
146
147   myButtonsGroupLayout->addWidget(myOkBtn);
148   myButtonsGroupLayout->addSpacing(10);
149   myButtonsGroupLayout->addStretch();
150   myButtonsGroupLayout->addWidget(myHelpBtn);
151
152   aTopLayout->addLayout(aSelectLayout);
153   aTopLayout->addWidget(myWGStack);
154   aTopLayout->addWidget(myButtonsGroup);
155
156   mySMESHGUI->SetActiveDialogBox(this);
157
158   // connect signals
159   connect(myOkBtn,                 SIGNAL(clicked()),                      this, SLOT(close()));
160   connect( myHelpBtn,              SIGNAL(clicked()),                      this, SLOT(onHelp()));
161   connect(mySelectBtn,             SIGNAL(clicked()),                      this, SLOT(onStartSelection()));
162   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()),        this, SLOT(close()));
163   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
164   connect(mySelectionMgr,          SIGNAL(currentSelectionChanged()),      this, SLOT(onSelectionChanged()));
165
166   // init dialog with current selection
167   onSelectionChanged();
168
169   myHelpFileName = "mesh_infos_page.html#advanced_mesh_infos_anchor";
170 }
171
172 //=================================================================================
173 // function : ~SMESHGUI_MeshInfosDlg()
174 // purpose  : Destructor
175 //=================================================================================
176 SMESHGUI_MeshInfosDlg::~SMESHGUI_MeshInfosDlg()
177 {
178 }
179
180 //=================================================================================
181 // function : DumpMeshInfos()
182 // purpose  : 
183 //=================================================================================
184 void SMESHGUI_MeshInfosDlg::DumpMeshInfos()
185 {
186   SUIT_OverrideCursor wc;
187
188   SALOME_ListIO aList;
189   mySelectionMgr->selectedObjects(aList);
190
191   int nbSel = aList.Extent();
192   if (nbSel == 1) {
193     myStartSelection = false;
194     mySelectLab->setText("");
195     Handle(SALOME_InteractiveObject) IObject = aList.First();
196     _PTR(SObject) aSO = SMESH::GetActiveStudyDocument()->FindObjectID(IObject->getEntry());
197     if (aSO) {
198       //CORBA::Object_var anObject = aSO->GetObject();
199       CORBA::Object_var anObject = SMESH::SObjectToObject(aSO);
200       if (!CORBA::is_nil(anObject)) {
201         SMESH::SMESH_IDSource_var anIDSource = SMESH::SMESH_IDSource::_narrow(anObject);
202         if (!anIDSource->_is_nil()) {
203           myWGStack->setCurrentWidget(myMeshWidget);
204           setWindowTitle(tr("SMESH_MESHINFO_TITLE") + " [" + tr("SMESH_OBJECT_MESH") + "]");
205           myMeshName->setText(aSO->GetName().c_str());
206
207           SMESH::long_array_var aMeshInfo = anIDSource->GetMeshInfo();
208           myMeshInfoBox->SetMeshInfo( aMeshInfo );
209
210           return;
211         }
212       }
213     }
214   }
215   myWGStack->setCurrentIndex(0);
216   setWindowTitle(tr("SMESH_MESHINFO_TITLE"));
217 }
218
219 //=================================================================================
220 // function : SelectionIntoArgument()
221 // purpose  : Called when selection has changed
222 //=================================================================================
223 void SMESHGUI_MeshInfosDlg::onSelectionChanged()
224 {
225   if (myStartSelection)
226     DumpMeshInfos();
227 }
228
229 //=================================================================================
230 // function : closeEvent()
231 // purpose  :
232 //=================================================================================
233 void SMESHGUI_MeshInfosDlg::closeEvent(QCloseEvent* e)
234 {
235   mySMESHGUI->ResetState();
236   QDialog::closeEvent(e);
237 }
238
239 //=================================================================================
240 // function : windowActivationChange()
241 // purpose  : called when window is activated/deactivated
242 //=================================================================================
243 void SMESHGUI_MeshInfosDlg::windowActivationChange(bool oldActive)
244 {
245   QDialog::windowActivationChange(oldActive);
246   if (isActiveWindow() && myIsActiveWindow != isActiveWindow())
247     ActivateThisDialog();
248   myIsActiveWindow = isActiveWindow();
249 }
250
251 //=================================================================================
252 // function : DeactivateActiveDialog()
253 // purpose  :
254 //=================================================================================
255 void SMESHGUI_MeshInfosDlg::DeactivateActiveDialog()
256 {
257   disconnect(mySelectionMgr, 0, this, 0);
258 }
259
260 //=================================================================================
261 // function : ActivateThisDialog()
262 // purpose  :
263 //=================================================================================
264 void SMESHGUI_MeshInfosDlg::ActivateThisDialog()
265 {
266   /* Emit a signal to deactivate any active dialog */
267   mySMESHGUI->EmitSignalDeactivateDialog();
268   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(onSelectionChanged()));
269 }
270
271 //=================================================================================
272 // function : onStartSelection()
273 // purpose  : starts selection
274 //=================================================================================
275 void SMESHGUI_MeshInfosDlg::onStartSelection()
276 {
277   myStartSelection = true;
278   onSelectionChanged();
279   myStartSelection = true;
280   mySelectLab->setText(tr("INF_SELECT_OBJECT"));
281 }
282
283 //=================================================================================
284 // function : onHelp()
285 // purpose  :
286 //=================================================================================
287 void SMESHGUI_MeshInfosDlg::onHelp()
288 {
289   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
290   if (app) 
291     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
292   else {
293     QString platform;
294 #ifdef WIN32
295     platform = "winapplication";
296 #else
297     platform = "application";
298 #endif
299     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
300                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
301                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
302                                                                  platform)).
303                              arg(myHelpFileName));
304   }
305 }
306
307 //=================================================================================
308 // function : keyPressEvent()
309 // purpose  :
310 //=================================================================================
311 void SMESHGUI_MeshInfosDlg::keyPressEvent( QKeyEvent* e )
312 {
313   QDialog::keyPressEvent( e );
314   if ( e->isAccepted() )
315     return;
316
317   if ( e->key() == Qt::Key_F1 ) {
318     e->accept();
319     onHelp();
320   }
321 }