Salome HOME
f629fba64cfa93392912cf1d65a96627594f7bc0
[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(MARGIN);;
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->addWidget(aMeshGrp);
168   aLay->addWidget(aCoordGrp);
169   aLay->addWidget(elementGrp);
170
171   // OK instead of "Apply and Close"
172   if ( QAbstractButton* but = button(OK) )
173     but->setText( tr("SMESH_BUT_OK"));
174
175   return aFrame;
176 }
177
178 //================================================================================
179 /*!
180  * \brief Constructor
181 */
182 //================================================================================
183
184 SMESHGUI_FindElemByPointOp::SMESHGUI_FindElemByPointOp()
185   :SMESHGUI_SelectionOp()
186 {
187   mySimulation = 0;
188   myDlg = new SMESHGUI_FindElemByPointDlg;
189   myHelpFileName = "find_element_by_point_page.html";
190   myFilter = new SMESH_TypeFilter( MESH );
191
192   myPreview = new SMESH::MeshPreviewStruct();
193
194   myPreview->nodesXYZ.length(1);
195   myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
196   myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
197   myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
198
199   myPreview->elementTypes.length(1);
200   myPreview->elementTypes[0].SMDS_ElementType = SMESH::NODE;
201   myPreview->elementTypes[0].isPoly = false;
202   myPreview->elementTypes[0].nbNodesInElement = 1;
203
204   myPreview->elementConnectivities.length(1);
205   myPreview->elementConnectivities[0] = 0;
206
207   // connect signals and slots
208   connect(myDlg->myFindBtn,      SIGNAL(clicked()),               this, SLOT(onFind()));
209   connect(myDlg->myX,            SIGNAL(valueChanged(double)),    this, SLOT(redisplayPreview()));
210   connect(myDlg->myY,            SIGNAL(valueChanged(double)),    this, SLOT(redisplayPreview()));
211   connect(myDlg->myZ,            SIGNAL(valueChanged(double)),    this, SLOT(redisplayPreview()));
212   connect(myDlg->myFoundList,    SIGNAL(itemSelectionChanged()),  this, SLOT(onElemSelected()));
213   connect(myDlg->myElemTypeCombo,SIGNAL(currentIndexChanged(int)),this, SLOT(onElemTypeChange(int)));
214 }
215
216 //=======================================================================
217 // function : startOperation()
218 // purpose  : Init dialog fields, connect signals and slots, show dialog
219 //=======================================================================
220 void SMESHGUI_FindElemByPointOp::startOperation()
221 {
222   // init simulation with a current View
223   if ( mySimulation ) delete mySimulation;
224   mySimulation = new SMESHGUI_MeshEditPreview(SMESH::GetViewWindow( getSMESHGUI() ));
225   vtkProperty* aProp = vtkProperty::New();
226   aProp->SetRepresentationToWireframe();
227   aProp->SetColor(250, 0, 250);
228   aProp->SetPointSize(5);
229   aProp->SetLineWidth( SMESH::GetFloat("SMESH:element_width",1) + 1);
230   mySimulation->GetActor()->SetProperty(aProp);
231   aProp->Delete();
232
233   myDlg->myElemTypeCombo->setCurrentId( int(SMESH::ALL));
234
235   SMESHGUI_SelectionOp::startOperation();
236   myDlg->show();
237   redisplayPreview();
238
239   onSelectionDone(); // init myMesh
240 }
241
242 //================================================================================
243 /*!
244  * \brief Stops operation
245  */
246 //================================================================================
247
248 void SMESHGUI_FindElemByPointOp::stopOperation()
249 {
250   if ( mySimulation )
251   {
252     mySimulation->SetVisibility(false);
253     delete mySimulation;
254     mySimulation = 0;
255   }
256   selectionMgr()->removeFilter( myFilter );
257   SMESHGUI_SelectionOp::stopOperation();
258 }
259
260 //================================================================================
261 /*!
262  * \brief hilight found selected elements
263  */
264 //================================================================================
265
266 void SMESHGUI_FindElemByPointOp::onElemSelected()
267 {
268   if ( !myMeshIO.IsNull() )
269   {
270     Selection_Mode selMode =
271       myDlg->myElemTypeCombo->currentId() == int(SMESH::NODE) ? NodeSelection : CellSelection;
272     if ( selectionMode() != selMode )
273       setSelectionMode( selMode );
274
275     QList<QListWidgetItem *> ids = myDlg->myFoundList->selectedItems();
276     QList<QListWidgetItem*>::iterator id = ids.begin();
277     TColStd_MapOfInteger idMap;
278     for ( ; id != ids.end(); ++id )
279       idMap.Add( (*id)->text().toInt() );
280
281     addOrRemoveIndex( myMeshIO, idMap, false );
282
283     SALOME_ListIO aList;
284     aList.Append(myMeshIO);
285     selectionMgr()->setSelectedObjects(aList,false);
286   }
287 }
288
289 //================================================================================
290 /*!
291  * \brief Set selection mode according to element type
292  */
293 //================================================================================
294
295 void SMESHGUI_FindElemByPointOp::onElemTypeChange(int index)
296 {
297   Selection_Mode newMode = (index == 1) ? NodeSelection : CellSelection;
298   if ( selectionMode() != newMode )
299   {
300     selectionMgr()->clearFilters();
301     setSelectionMode( newMode );
302   }
303   myDlg->myFoundList->clear();
304 }
305
306 //================================================================================
307 /*!
308  * \brief perform it's intention action: find elements
309  */
310 //================================================================================
311
312 void SMESHGUI_FindElemByPointOp::onFind()
313 {
314   if ( myMesh->_is_nil() ) {
315     return;
316   }
317
318   try {
319     SUIT_OverrideCursor wc;
320
321     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
322     if (aMeshEditor->_is_nil())
323       return;
324
325     SMESH::long_array_var foundIds =
326       aMeshEditor->FindElementsByPoint( myDlg->myX->GetValue(),
327                                         myDlg->myY->GetValue(),
328                                         myDlg->myZ->GetValue(),
329                                         SMESH::ElementType( myDlg->myElemTypeCombo->currentId() ));
330     myDlg->myFoundList->clear();
331     for ( int i = 0; i < foundIds->length(); ++i )
332       myDlg->myFoundList->addItem( QString::number( foundIds[i] ));
333
334     if ( foundIds->length() > 0 )
335       myDlg->myFoundList->setCurrentRow(0);
336   }
337   catch (const SALOME::SALOME_Exception& S_ex) {
338     SalomeApp_Tools::QtCatchCorbaException(S_ex);
339   }
340   catch (...) {
341   }
342 }
343
344 //================================================================================
345 /*!
346  * \brief Method needed for internal cuisine
347  */
348 //================================================================================
349
350 bool SMESHGUI_FindElemByPointOp::onApply()
351 {
352   return true;
353 }
354
355 //================================================================================
356 /*!
357  * \brief SLOT called when selection changed
358  */
359 //================================================================================
360
361 void SMESHGUI_FindElemByPointOp::onSelectionDone()
362 {
363   if ( !myDlg->isVisible() || !myDlg->isEnabled() )
364     return;
365
366   QString oldMeshEntry, newMeshEntry;
367   if ( !myMeshIO.IsNull() && myMeshIO->hasEntry() )
368     oldMeshEntry = myMeshIO->getEntry();
369
370   myDlg->myMeshName->setText("");
371   myMeshIO.Nullify();
372
373   try {
374     SALOME_ListIO aList;
375     selectionMgr()->selectedObjects(aList, SVTK_Viewer::Type());
376     if (aList.Extent() == 1 && aList.First()->hasEntry())
377     {
378       Handle(SALOME_InteractiveObject) anIO = aList.First();
379       _PTR(SObject) pObj = studyDS()->FindObjectID(anIO->getEntry());
380       myMesh = SMESH::GetMeshByIO( anIO );
381       if ( pObj && !myMesh->_is_nil() )
382       {
383         myMeshIO = anIO;
384         myDlg->myMeshName->setText( pObj->GetName().c_str() );
385         newMeshEntry = anIO->getEntry();
386       }
387     }
388   }
389   catch (...) {
390   }
391
392   if ( oldMeshEntry != newMeshEntry || newMeshEntry.isEmpty() )
393     myDlg->myFoundList->clear();
394
395   myDlg->myFindBtn->setEnabled( !myMeshIO.IsNull() );
396 }
397
398 //================================================================================
399 /*!
400  * \brief show point by coordinates
401  */
402 //================================================================================
403
404 void SMESHGUI_FindElemByPointOp::redisplayPreview()
405 {
406   myDlg->myFoundList->clear();
407
408   myPreview->nodesXYZ[0].x = myDlg->myX->GetValue();
409   myPreview->nodesXYZ[0].y = myDlg->myY->GetValue();
410   myPreview->nodesXYZ[0].z = myDlg->myZ->GetValue();
411
412   mySimulation->SetData(&myPreview.in());
413 }
414
415 //================================================================================
416 /*!
417  * \brief install filter on meshes
418  */
419 //================================================================================
420
421 void SMESHGUI_FindElemByPointOp::activateSelection()
422 {
423   selectionMgr()->clearFilters();
424   selectionMgr()->installFilter( myFilter );
425 }
426
427 //================================================================================
428 /*!
429  * \brief Destructor
430 */
431 //================================================================================
432
433 SMESHGUI_FindElemByPointOp::~SMESHGUI_FindElemByPointOp()
434 {
435   if ( myDlg )         { delete myDlg;        myDlg = 0; }
436   if ( mySimulation )  { delete mySimulation; mySimulation = 0; }
437   if ( myFilter )      { delete myFilter;     myFilter = 0; }
438 }
439
440 //================================================================================
441 /*!
442  * \brief Gets dialog of this operation
443  * \retval LightApp_Dialog* - pointer to dialog of this operation
444  */
445 //================================================================================
446
447 LightApp_Dialog* SMESHGUI_FindElemByPointOp::dlg() const
448 {
449   return myDlg;
450 }
451