Salome HOME
Improve dialog box's layout
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_FindElemByPointDlg.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File   : SMESHGUI_FindElemByPointDlg.cxx
21 // Author : Edward AGAPOV, Open CASCADE S.A.S.
22 // SMESH includes
23 //
24 #include "SMESHGUI_FindElemByPointDlg.h"
25
26 #include "SMESHGUI.h"
27 #include "SMESHGUI_MeshUtils.h"
28 #include "SMESHGUI_VTKUtils.h"
29 #include "SMESHGUI_SpinBox.h"
30 #include "SMESHGUI_MeshEditPreview.h"
31
32 #include <SMESH_Actor.h>
33 #include <SMESH_ActorUtils.h>
34 #include <SMESH_TypeFilter.hxx>
35
36 // SALOME GUI includes
37 #include <LightApp_SelectionMgr.h>
38 #include <QtxComboBox.h>
39 #include <SALOME_ListIO.hxx>
40 #include <SUIT_Desktop.h>
41 #include <SUIT_MessageBox.h>
42 #include <SUIT_OverrideCursor.h>
43 #include <SUIT_ResourceMgr.h>
44 #include <SVTK_ViewModel.h>
45 #include <SVTK_ViewWindow.h>
46 #include <SalomeApp_Tools.h>
47 #include <SalomeApp_TypeFilter.h>
48
49 // Qt includes
50 #include <QAbstractButton>
51 #include <QGridLayout>
52 #include <QGroupBox>
53 #include <QHBoxLayout>
54 #include <QLabel>
55 #include <QLineEdit>
56 #include <QListWidget>
57 #include <QPushButton>
58 #include <QVBoxLayout>
59
60 // VTK includes
61 #include <vtkProperty.h>
62
63 // IDL includes
64 #include <SALOMEconfig.h>
65 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
66
67 #define SPACING 6
68 #define MARGIN  11
69
70 //=======================================================================
71 /*!
72  * \brief Dialog to find elements by a point coordinates
73  */
74 //=======================================================================
75
76 SMESHGUI_FindElemByPointDlg::SMESHGUI_FindElemByPointDlg()
77   : SMESHGUI_Dialog( 0, false, true, OK | Help )
78 {
79   setWindowTitle(tr("CAPTION"));
80
81   QVBoxLayout* aDlgLay = new QVBoxLayout (mainFrame());
82   aDlgLay->setMargin(0);
83   aDlgLay->setSpacing(SPACING);
84
85   QWidget* aMainFrame = createMainFrame  (mainFrame());
86
87   aDlgLay->addWidget(aMainFrame);
88   aDlgLay->setStretchFactor(aMainFrame, 1);
89 }
90
91 //=======================================================================
92 // function : createMainFrame()
93 // purpose  : Create frame containing dialog's input fields
94 //=======================================================================
95 QWidget* SMESHGUI_FindElemByPointDlg::createMainFrame (QWidget* theParent)
96 {
97   QWidget* aFrame = new QWidget(theParent);
98
99   //mesh name
100
101   QGroupBox* aMeshGrp = new QGroupBox(tr("SMESH_MESH"), aFrame);
102   QHBoxLayout* aMeshGrpLayout = new QHBoxLayout(aMeshGrp);
103   aMeshGrpLayout->setMargin(MARGIN);
104   aMeshGrpLayout->setSpacing(SPACING);
105
106   myMeshName = new QLineEdit(aMeshGrp);
107   aMeshGrpLayout->addWidget(myMeshName);
108
109   // coordinates
110
111   QGroupBox* aCoordGrp = new QGroupBox(tr("SMESH_COORDINATES"), aFrame);
112   QHBoxLayout* aCoordGrpLayout = new QHBoxLayout(aCoordGrp);
113   aCoordGrpLayout->setMargin(MARGIN);
114   aCoordGrpLayout->setSpacing(SPACING);
115
116   QLabel* aXLabel = new QLabel(tr("SMESH_X"), aCoordGrp);
117   myX = new SMESHGUI_SpinBox(aCoordGrp);
118
119   QLabel* aYLabel = new QLabel(tr("SMESH_Y"), aCoordGrp);
120   myY = new SMESHGUI_SpinBox(aCoordGrp);
121
122   QLabel* aZLabel = new QLabel(tr("SMESH_Z"), aCoordGrp);
123   myZ = new SMESHGUI_SpinBox(aCoordGrp);
124
125   myX->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
126   myY->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
127   myZ->RangeStepAndValidator(COORD_MIN, COORD_MAX, 10.0, "length_precision");
128   myX->SetValue(0);
129   myY->SetValue(0);
130   myZ->SetValue(0);
131
132   aCoordGrpLayout->addWidget(aXLabel);
133   aCoordGrpLayout->addWidget(myX);
134   aCoordGrpLayout->addWidget(aYLabel);
135   aCoordGrpLayout->addWidget(myY);
136   aCoordGrpLayout->addWidget(aZLabel);
137   aCoordGrpLayout->addWidget(myZ);
138
139   // Elements
140
141   QGroupBox* elementGrp = new QGroupBox(tr("Elements"), aFrame);
142   QGridLayout* elementGrpLayout = new QGridLayout(elementGrp);
143   elementGrpLayout->setSpacing(SPACING);
144   elementGrpLayout->setMargin(MARGIN);
145
146
147   myFindBtn = new QPushButton(elementGrp);
148   myFindBtn->setText(tr("Find"));
149   //myFindBtn->setCheckable(false);
150
151   myElemTypeCombo = new QtxComboBox(elementGrp);
152   myElemTypeCombo->addItem( tr( "MEN_ALL" ));    myElemTypeCombo->setId( 0, int( SMESH::ALL   ));
153   myElemTypeCombo->addItem( tr( "MEN_NODE" ));   myElemTypeCombo->setId( 1, int( SMESH::NODE  ));
154   myElemTypeCombo->addItem( tr( "MEN_EDGE" ));   myElemTypeCombo->setId( 2, int( SMESH::EDGE  ));
155   myElemTypeCombo->addItem( tr( "MEN_FACE" ));   myElemTypeCombo->setId( 3, int( SMESH::FACE  ));
156   myElemTypeCombo->addItem( tr("MEN_VOLUME_3D"));myElemTypeCombo->setId( 4, int( SMESH::VOLUME));
157   myElemTypeCombo->addItem( tr( "MEN_ELEM0D" )); myElemTypeCombo->setId( 5, int( SMESH::ELEM0D));
158
159   myFoundList = new QListWidget(elementGrp);
160
161   elementGrpLayout->addWidget( myFindBtn,       0, 0 );
162   elementGrpLayout->addWidget( myElemTypeCombo, 0, 1 );
163   elementGrpLayout->addWidget( myFoundList,     1, 0, 2, 2 );
164
165
166   QVBoxLayout* aLay = new QVBoxLayout(aFrame);
167   aLay->setMargin( 0 );
168   aLay->setSpacing( SPACING );
169   aLay->addWidget(aMeshGrp);
170   aLay->addWidget(aCoordGrp);
171   aLay->addWidget(elementGrp);
172
173   // OK instead of "Apply and Close"
174   if ( QAbstractButton* but = button(OK) )
175     but->setText( tr("SMESH_BUT_OK"));
176
177   return aFrame;
178 }
179
180 //================================================================================
181 /*!
182  * \brief Constructor
183 */
184 //================================================================================
185
186 SMESHGUI_FindElemByPointOp::SMESHGUI_FindElemByPointOp()
187   :SMESHGUI_SelectionOp()
188 {
189   mySimulation = 0;
190   myDlg = new SMESHGUI_FindElemByPointDlg;
191   myHelpFileName = "find_element_by_point_page.html";
192   myFilter = new SMESH_TypeFilter( MESH );
193
194   myPreview = new SMESH::MeshPreviewStruct();
195
196   myPreview->nodesXYZ.length(1);
197   myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
198   myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
199   myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
200
201   myPreview->elementTypes.length(1);
202   myPreview->elementTypes[0].SMDS_ElementType = SMESH::NODE;
203   myPreview->elementTypes[0].isPoly = false;
204   myPreview->elementTypes[0].nbNodesInElement = 1;
205
206   myPreview->elementConnectivities.length(1);
207   myPreview->elementConnectivities[0] = 0;
208
209   // connect signals and slots
210   connect(myDlg->myFindBtn,      SIGNAL(clicked()),               this, SLOT(onFind()));
211   connect(myDlg->myX,            SIGNAL(valueChanged(double)),    this, SLOT(redisplayPreview()));
212   connect(myDlg->myY,            SIGNAL(valueChanged(double)),    this, SLOT(redisplayPreview()));
213   connect(myDlg->myZ,            SIGNAL(valueChanged(double)),    this, SLOT(redisplayPreview()));
214   connect(myDlg->myFoundList,    SIGNAL(itemSelectionChanged()),  this, SLOT(onElemSelected()));
215   connect(myDlg->myElemTypeCombo,SIGNAL(currentIndexChanged(int)),this, SLOT(onElemTypeChange(int)));
216 }
217
218 //=======================================================================
219 // function : startOperation()
220 // purpose  : Init dialog fields, connect signals and slots, show dialog
221 //=======================================================================
222 void SMESHGUI_FindElemByPointOp::startOperation()
223 {
224   // init simulation with a current View
225   if ( mySimulation ) delete mySimulation;
226   mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
227   vtkProperty* aProp = vtkProperty::New();
228   aProp->SetRepresentationToWireframe();
229   aProp->SetColor(250, 0, 250);
230   aProp->SetPointSize(5);
231   aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
232   mySimulation->GetActor()->SetProperty(aProp);
233   aProp->Delete();
234
235   myDlg->myElemTypeCombo->setCurrentId( int(SMESH::ALL));
236
237   SMESHGUI_SelectionOp::startOperation();
238   myDlg->show();
239   redisplayPreview();
240
241   onSelectionDone(); // init myMesh
242 }
243
244 //================================================================================
245 /*!
246  * \brief Stops operation
247  */
248 //================================================================================
249
250 void SMESHGUI_FindElemByPointOp::stopOperation()
251 {
252   if ( mySimulation )
253   {
254     mySimulation->SetVisibility(false);
255     delete mySimulation;
256     mySimulation = 0;
257   }
258   selectionMgr()->removeFilter( myFilter );
259   SMESHGUI_SelectionOp::stopOperation();
260 }
261
262 //================================================================================
263 /*!
264  * \brief hilight found selected elements
265  */
266 //================================================================================
267
268 void SMESHGUI_FindElemByPointOp::onElemSelected()
269 {
270   if ( !myMeshIO.IsNull() )
271   {
272     Selection_Mode selMode =
273       myDlg->myElemTypeCombo->currentId() == int(SMESH::NODE) ? NodeSelection : CellSelection;
274     if ( selectionMode() != selMode )
275       setSelectionMode( selMode );
276
277     QList<QListWidgetItem *> ids = myDlg->myFoundList->selectedItems();
278     QList<QListWidgetItem*>::iterator id = ids.begin();
279     TColStd_MapOfInteger idMap;
280     for ( ; id != ids.end(); ++id )
281       idMap.Add( (*id)->text().toInt() );
282
283     addOrRemoveIndex( myMeshIO, idMap, false );
284
285     SALOME_ListIO aList;
286     aList.Append(myMeshIO);
287     selectionMgr()->setSelectedObjects(aList,false);
288   }
289 }
290
291 //================================================================================
292 /*!
293  * \brief Set selection mode according to element type
294  */
295 //================================================================================
296
297 void SMESHGUI_FindElemByPointOp::onElemTypeChange(int index)
298 {
299   Selection_Mode newMode = (index == 1) ? NodeSelection : CellSelection;
300   if ( selectionMode() != newMode )
301   {
302     selectionMgr()->clearFilters();
303     setSelectionMode( newMode );
304   }
305   myDlg->myFoundList->clear();
306 }
307
308 //================================================================================
309 /*!
310  * \brief perform it's intention action: find elements
311  */
312 //================================================================================
313
314 void SMESHGUI_FindElemByPointOp::onFind()
315 {
316   if ( myMesh->_is_nil() ) {
317     return;
318   }
319
320   try {
321     SUIT_OverrideCursor wc;
322
323     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
324     if (aMeshEditor->_is_nil())
325       return;
326
327     SMESH::long_array_var foundIds =
328       aMeshEditor->FindElementsByPoint( myDlg->myX->GetValue(),
329                                         myDlg->myY->GetValue(),
330                                         myDlg->myZ->GetValue(),
331                                         SMESH::ElementType( myDlg->myElemTypeCombo->currentId() ));
332     myDlg->myFoundList->clear();
333     for ( int i = 0; i < foundIds->length(); ++i )
334       myDlg->myFoundList->addItem( QString::number( foundIds[i] ));
335
336     if ( foundIds->length() > 0 )
337       myDlg->myFoundList->setCurrentRow(0);
338   }
339   catch (const SALOME::SALOME_Exception& S_ex) {
340     SalomeApp_Tools::QtCatchCorbaException(S_ex);
341   }
342   catch (...) {
343   }
344 }
345
346 //================================================================================
347 /*!
348  * \brief Method needed for internal cuisine
349  */
350 //================================================================================
351
352 bool SMESHGUI_FindElemByPointOp::onApply()
353 {
354   return true;
355 }
356
357 //================================================================================
358 /*!
359  * \brief SLOT called when selection changed
360  */
361 //================================================================================
362
363 void SMESHGUI_FindElemByPointOp::onSelectionDone()
364 {
365   if ( !myDlg->isVisible() || !myDlg->isEnabled() )
366     return;
367
368   QString oldMeshEntry, newMeshEntry;
369   if ( !myMeshIO.IsNull() && myMeshIO->hasEntry() )
370     oldMeshEntry = myMeshIO->getEntry();
371
372   myDlg->myMeshName->setText("");
373   myMeshIO.Nullify();
374
375   try {
376     SALOME_ListIO aList;
377     selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
378     if (aList.Extent() == 1 && aList.First()->hasEntry())
379     {
380       Handle(SALOME_InteractiveObject) anIO = aList.First();
381       _PTR(SObject) pObj = studyDS()->FindObjectID(anIO->getEntry());
382       myMesh = SMESH::GetMeshByIO( anIO );
383       if ( pObj && !myMesh->_is_nil() )
384       {
385         myMeshIO = anIO;
386         myDlg->myMeshName->setText( pObj->GetName().c_str() );
387         newMeshEntry = anIO->getEntry();
388       }
389     }
390   }
391   catch (...) {
392   }
393
394   if ( oldMeshEntry != newMeshEntry || newMeshEntry.isEmpty() )
395     myDlg->myFoundList->clear();
396
397   myDlg->myFindBtn->setEnabled( !myMeshIO.IsNull() );
398 }
399
400 //================================================================================
401 /*!
402  * \brief show point by coordinates
403  */
404 //================================================================================
405
406 void SMESHGUI_FindElemByPointOp::redisplayPreview()
407 {
408   myDlg->myFoundList->clear();
409
410   myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
411   myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
412   myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
413
414   mySimulation->SetData(&myPreview.in());
415 }
416
417 //================================================================================
418 /*!
419  * \brief install filter on meshes
420  */
421 //================================================================================
422
423 void SMESHGUI_FindElemByPointOp::activateSelection()
424 {
425   selectionMgr()->clearFilters();
426   selectionMgr()->installFilter( myFilter );
427 }
428
429 //================================================================================
430 /*!
431  * \brief Destructor
432 */
433 //================================================================================
434
435 SMESHGUI_FindElemByPointOp::~SMESHGUI_FindElemByPointOp()
436 {
437   if ( myDlg )         { delete myDlg;        myDlg = 0; }
438   if ( mySimulation )  { delete mySimulation; mySimulation = 0; }
439   if ( myFilter )      { delete myFilter;     myFilter = 0; }
440 }
441
442 //================================================================================
443 /*!
444  * \brief Gets dialog of this operation
445  * \retval LightApp_Dialog* - pointer to dialog of this operation
446  */
447 //================================================================================
448
449 LightApp_Dialog* SMESHGUI_FindElemByPointOp::dlg() const
450 {
451   return myDlg;
452 }
453