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