Salome HOME
Update copyright
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_FindElemByPointDlg.cxx
1 // Copyright (C) 2007-2011  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   connect(myDlg,                 SIGNAL(rejectedDlg()),           this, SLOT(onRejectedDlg()));
217 }
218
219 //=======================================================================
220 // function : startOperation()
221 // purpose  : Init dialog fields, connect signals and slots, show dialog
222 //=======================================================================
223 void SMESHGUI_FindElemByPointOp::startOperation()
224 {
225   // init simulation with a current View
226   if ( mySimulation ) delete mySimulation;
227   mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
228   vtkProperty* aProp = vtkProperty::New();
229   aProp->SetRepresentationToWireframe();
230   aProp->SetColor(250, 0, 250);
231   aProp->SetPointSize(5);
232   aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
233   mySimulation->GetActor()->SetProperty(aProp);
234   aProp->Delete();
235
236   myDlg->myElemTypeCombo->setCurrentId( int(SMESH::ALL));
237
238   SMESHGUI_SelectionOp::startOperation();
239   myDlg->show();
240   redisplayPreview();
241
242   onSelectionDone(); // init myMesh
243 }
244
245 //================================================================================
246 /*!
247  * \brief Stops operation
248  */
249 //================================================================================
250
251 void SMESHGUI_FindElemByPointOp::stopOperation()
252 {
253   if ( mySimulation )
254   {
255     mySimulation->SetVisibility(false);
256     delete mySimulation;
257     mySimulation = 0;
258   }
259   selectionMgr()->removeFilter( myFilter );
260   SMESHGUI_SelectionOp::stopOperation();
261 }
262
263 //================================================================================
264 /*!
265  * \brief hilight found selected elements
266  */
267 //================================================================================
268
269 void SMESHGUI_FindElemByPointOp::onElemSelected()
270 {
271   if ( !myMeshIO.IsNull() )
272   {
273     Selection_Mode selMode =
274       myDlg->myElemTypeCombo->currentId() == int(SMESH::NODE) ? NodeSelection : CellSelection;
275     if ( selectionMode() != selMode )
276       setSelectionMode( selMode );
277
278     QList<QListWidgetItem *> ids = myDlg->myFoundList->selectedItems();
279     QList<QListWidgetItem*>::iterator id = ids.begin();
280     TColStd_MapOfInteger idMap;
281     for ( ; id != ids.end(); ++id )
282       idMap.Add( (*id)->text().toInt() );
283
284     addOrRemoveIndex( myMeshIO, idMap, false );
285
286     SALOME_ListIO aList;
287     aList.Append(myMeshIO);
288     selectionMgr()->setSelectedObjects(aList,false);
289   }
290 }
291
292 //================================================================================
293 /*!
294  * \brief Set selection mode according to element type
295  */
296 //================================================================================
297
298 void SMESHGUI_FindElemByPointOp::onElemTypeChange(int index)
299 {
300   Selection_Mode newMode = (index == 1) ? NodeSelection : CellSelection;
301   if ( selectionMode() != newMode )
302   {
303     selectionMgr()->clearFilters();
304     setSelectionMode( newMode );
305   }
306   myDlg->myFoundList->clear();
307 }
308
309 //================================================================================
310 /*!
311  * \brief Method needed for internal cuisine
312  */
313 //================================================================================
314
315 void SMESHGUI_FindElemByPointDlg::reject()
316 {
317   emit rejectedDlg();
318   QtxDialog::reject();
319 }
320
321 //================================================================================
322 /*!
323  * \brief Method needed for internal cuisine
324  */
325 //================================================================================
326
327 void SMESHGUI_FindElemByPointOp::onRejectedDlg()
328 {
329   myMeshIO.Nullify(); 
330 }
331
332 //================================================================================
333 /*!
334  * \brief perform it's intention action: find elements
335  */
336 //================================================================================
337
338 void SMESHGUI_FindElemByPointOp::onFind()
339 {
340   if ( myMesh->_is_nil() ) {
341     return;
342   }
343
344   try {
345     SUIT_OverrideCursor wc;
346
347     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
348     if (aMeshEditor->_is_nil())
349       return;
350
351     SMESH::long_array_var foundIds =
352       aMeshEditor->FindElementsByPoint( myDlg->myX->GetValue(),
353                                         myDlg->myY->GetValue(),
354                                         myDlg->myZ->GetValue(),
355                                         SMESH::ElementType( myDlg->myElemTypeCombo->currentId() ));
356     myDlg->myFoundList->clear();
357     for ( int i = 0; i < foundIds->length(); ++i )
358       myDlg->myFoundList->addItem( QString::number( foundIds[i] ));
359
360     if ( foundIds->length() > 0 )
361       myDlg->myFoundList->setCurrentRow(0);
362   }
363   catch (const SALOME::SALOME_Exception& S_ex) {
364     SalomeApp_Tools::QtCatchCorbaException(S_ex);
365   }
366   catch (...) {
367   }
368 }
369
370 //================================================================================
371 /*!
372  * \brief Method needed for internal cuisine
373  */
374 //================================================================================
375
376 bool SMESHGUI_FindElemByPointOp::onApply()
377 {
378   onRejectedDlg();
379   return true;
380 }
381
382 //================================================================================
383 /*!
384  * \brief SLOT called when selection changed
385  */
386 //================================================================================
387
388 void SMESHGUI_FindElemByPointOp::onSelectionDone()
389 {
390   if ( !myDlg->isVisible() || !myDlg->isEnabled() )
391     return;
392
393   QString oldMeshEntry, newMeshEntry;
394   if ( !myMeshIO.IsNull() && myMeshIO->hasEntry() )
395     oldMeshEntry = myMeshIO->getEntry();
396
397   myDlg->myMeshName->setText("");
398   myMeshIO.Nullify();
399
400   try {
401     SALOME_ListIO aList;
402     selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
403     if (aList.Extent() == 1 && aList.First()->hasEntry())
404     {
405       Handle(SALOME_InteractiveObject) anIO = aList.First();
406       _PTR(SObject) pObj = studyDS()->FindObjectID(anIO->getEntry());
407       myMesh = SMESH::GetMeshByIO( anIO );
408       if ( pObj && !myMesh->_is_nil() )
409       {
410         myMeshIO = anIO;
411         myDlg->myMeshName->setText( pObj->GetName().c_str() );
412         newMeshEntry = anIO->getEntry();
413       }
414     }
415   }
416   catch (...) {
417   }
418
419   if ( oldMeshEntry != newMeshEntry || newMeshEntry.isEmpty() )
420     myDlg->myFoundList->clear();
421
422   myDlg->myFindBtn->setEnabled( !myMeshIO.IsNull() );
423 }
424
425 //================================================================================
426 /*!
427  * \brief show point by coordinates
428  */
429 //================================================================================
430
431 void SMESHGUI_FindElemByPointOp::redisplayPreview()
432 {
433   myDlg->myFoundList->clear();
434
435   myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
436   myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
437   myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
438
439   mySimulation->SetData(&myPreview.in());
440 }
441
442 //================================================================================
443 /*!
444  * \brief install filter on meshes
445  */
446 //================================================================================
447
448 void SMESHGUI_FindElemByPointOp::activateSelection()
449 {
450   selectionMgr()->clearFilters();
451   selectionMgr()->installFilter( myFilter );
452 }
453
454 //================================================================================
455 /*!
456  * \brief Destructor
457 */
458 //================================================================================
459
460 SMESHGUI_FindElemByPointOp::~SMESHGUI_FindElemByPointOp()
461 {
462   if ( myDlg )         { delete myDlg;        myDlg = 0; }
463   if ( mySimulation )  { delete mySimulation; mySimulation = 0; }
464   if ( myFilter )      { delete myFilter;     myFilter = 0; }
465 }
466
467 //================================================================================
468 /*!
469  * \brief Gets dialog of this operation
470  * \retval LightApp_Dialog* - pointer to dialog of this operation
471  */
472 //================================================================================
473
474 LightApp_Dialog* SMESHGUI_FindElemByPointOp::dlg() const
475 {
476   return myDlg;
477 }
478