Salome HOME
Merge from V6_5_BR 05/06/2012
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Measurements.cxx
1 // Copyright (C) 2007-2012  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 //  File   : SMESHGUI_Measurements.cxx
23 //  Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24
25 #include "SMESHGUI_Measurements.h"
26
27 #include "SMESH_Actor.h"
28 #include "SMESHGUI.h"
29 #include "SMESHGUI_IdValidator.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_VTKUtils.h"
32 #include <SMESH_TypeFilter.hxx>
33 #include <SMESH_LogicalFilter.hxx>
34
35 #include <LightApp_SelectionMgr.h>
36 #include <SUIT_OverrideCursor.h>
37 #include <SUIT_ResourceMgr.h>
38 #include <SVTK_ViewWindow.h>
39 #include <SALOME_ListIteratorOfListIO.hxx>
40
41 #include <QButtonGroup>
42 #include <QGridLayout>
43 #include <QGroupBox>
44 #include <QHBoxLayout>
45 #include <QKeyEvent>
46 #include <QLabel>
47 #include <QLineEdit>
48 #include <QPushButton>
49 #include <QRadioButton>
50 #include <QTabWidget>
51 #include <QVBoxLayout>
52
53 #include <vtkPoints.h>
54 #include <vtkUnstructuredGrid.h>
55 #include <vtkIdList.h>
56 #include <vtkCellArray.h>
57 #include <vtkUnsignedCharArray.h>
58 #include <vtkDataSetMapper.h>
59 #include <VTKViewer_CellLocationsArray.h>
60 #include <vtkProperty.h>
61
62 #include <SALOMEconfig.h>
63 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
64 #include CORBA_SERVER_HEADER(SMESH_Measurements)
65
66 const int SPACING = 6;            // layout spacing
67 const int MARGIN  = 9;            // layout margin
68 const int MAX_NB_FOR_EDITOR = 40; // max nb of items in the ID list editor field
69
70 // Uncomment as soon as elements are supported by Min Distance operation
71 //#define MINDIST_ENABLE_ELEMENT
72
73 // Uncomment as soon as objects are supported by Min Distance operation
74 //#define MINDIST_ENABLE_OBJECT
75
76 /*!
77   \class SMESHGUI_MinDistance
78   \brief Minimum distance measurement widget.
79   
80   Widget to calculate minimum distance between two objects.
81 */
82
83 /*!
84   \brief Constructor.
85   \param parent parent widget
86 */
87 SMESHGUI_MinDistance::SMESHGUI_MinDistance( QWidget* parent )
88 : QWidget( parent ), myCurrentTgt( FirstTgt ), myFirstActor( 0 ), mySecondActor( 0 ), myPreview( 0 )
89 {
90   QGroupBox*    aFirstTgtGrp = new QGroupBox( tr( "FIRST_TARGET" ), this );
91   QRadioButton* aFNode       = new QRadioButton( tr( "NODE" ),    aFirstTgtGrp );
92   QRadioButton* aFElem       = new QRadioButton( tr( "ELEMENT" ), aFirstTgtGrp );
93   QRadioButton* aFObject     = new QRadioButton( tr( "OBJECT" ),  aFirstTgtGrp );
94   myFirstTgt                 = new QLineEdit( aFirstTgtGrp );
95
96   QGridLayout* fl = new QGridLayout( aFirstTgtGrp );
97   fl->setMargin( MARGIN );
98   fl->setSpacing( SPACING );
99   fl->addWidget( aFNode,     0, 0 );
100   fl->addWidget( aFElem,     0, 1 );
101   fl->addWidget( aFObject,   0, 2 );
102   fl->addWidget( myFirstTgt, 1, 0, 1, 3 );
103
104   myFirst = new QButtonGroup( this );
105   myFirst->addButton( aFNode,   NodeTgt );
106   myFirst->addButton( aFElem,   ElementTgt );
107   myFirst->addButton( aFObject, ObjectTgt );
108
109   QGroupBox*    aSecondTgtGrp = new QGroupBox( tr( "SECOND_TARGET" ), this );
110   QRadioButton* aSOrigin      = new QRadioButton( tr( "ORIGIN" ),  aSecondTgtGrp );
111   QRadioButton* aSNode        = new QRadioButton( tr( "NODE" ),    aSecondTgtGrp );
112   QRadioButton* aSElem        = new QRadioButton( tr( "ELEMENT" ), aSecondTgtGrp );
113   QRadioButton* aSObject      = new QRadioButton( tr( "OBJECT" ),  aSecondTgtGrp );
114   mySecondTgt                 = new QLineEdit( aSecondTgtGrp );
115
116   QGridLayout* sl = new QGridLayout( aSecondTgtGrp );
117   sl->setMargin( MARGIN );
118   sl->setSpacing( SPACING );
119   sl->addWidget( aSOrigin,   0, 0 );
120   sl->addWidget( aSNode,     0, 1 );
121   sl->addWidget( aSElem,     0, 2 );
122   sl->addWidget( aSObject,   0, 3 );
123   sl->addWidget( mySecondTgt, 1, 0, 1, 4 );
124
125   mySecond = new QButtonGroup( this );
126   mySecond->addButton( aSOrigin, OriginTgt );
127   mySecond->addButton( aSNode,   NodeTgt );
128   mySecond->addButton( aSElem,   ElementTgt );
129   mySecond->addButton( aSObject, ObjectTgt );
130
131   QPushButton* aCompute = new QPushButton( tr( "COMPUTE" ), this );
132   
133   QGroupBox* aResults = new QGroupBox( tr( "RESULT" ), this );
134   QLabel* aDxLab   = new QLabel( "dX", aResults );
135   myDX             = new QLineEdit( aResults );
136   QLabel* aDyLab   = new QLabel( "dY", aResults );
137   myDY             = new QLineEdit( aResults );
138   QLabel* aDzLab   = new QLabel( "dZ", aResults );
139   myDZ             = new QLineEdit( aResults );
140   QLabel* aDistLab = new QLabel( tr( "DISTANCE" ), aResults );
141   myDistance       = new QLineEdit( aResults );
142
143   QGridLayout* rl = new QGridLayout( aResults );
144   rl->setMargin( MARGIN );
145   rl->setSpacing( SPACING );
146   rl->addWidget( aDxLab,     0, 0 );
147   rl->addWidget( myDX,       0, 1 );
148   rl->addWidget( aDyLab,     1, 0 );
149   rl->addWidget( myDY,       1, 1 );
150   rl->addWidget( aDzLab,     2, 0 );
151   rl->addWidget( myDZ,       2, 1 );
152   rl->addWidget( aDistLab,   0, 2 );
153   rl->addWidget( myDistance, 0, 3 );
154
155   QGridLayout* l = new QGridLayout( this );
156   l->setMargin( MARGIN );
157   l->setSpacing( SPACING );
158
159   l->addWidget( aFirstTgtGrp,  0, 0, 1, 2 );
160   l->addWidget( aSecondTgtGrp, 1, 0, 1, 2 );
161   l->addWidget( aCompute,      2, 0 );
162   l->addWidget( aResults,      3, 0, 1, 2 );
163   l->setColumnStretch( 1, 5 );
164   l->setRowStretch( 4, 5 );
165
166   aFNode->setChecked( true );
167   aSOrigin->setChecked( true );
168 #ifndef MINDIST_ENABLE_ELEMENT
169   aFElem->setEnabled( false );   // NOT AVAILABLE YET
170   aSElem->setEnabled( false );   // NOT AVAILABLE YET
171 #endif
172 #ifndef MINDIST_ENABLE_OBJECT
173   aFObject->setEnabled( false ); // NOT AVAILABLE YET
174   aSObject->setEnabled( false ); // NOT AVAILABLE YET
175 #endif
176   myDX->setReadOnly( true );
177   myDY->setReadOnly( true );
178   myDZ->setReadOnly( true );
179   myDistance->setReadOnly( true );
180
181   myValidator = new SMESHGUI_IdValidator( this, 1 );
182
183   myFirstTgt->installEventFilter( this );
184   mySecondTgt->installEventFilter( this );
185
186   connect( myFirst,     SIGNAL( buttonClicked( int ) ),  this, SLOT( firstChanged() ) );
187   connect( mySecond,    SIGNAL( buttonClicked( int ) ),  this, SLOT( secondChanged() ) );
188   connect( aCompute,    SIGNAL( clicked() ),             this, SLOT( compute() ) );
189   connect( myFirstTgt,  SIGNAL( textEdited( QString ) ), this, SLOT( firstEdited() ) );
190   connect( mySecondTgt, SIGNAL( textEdited( QString ) ), this, SLOT( secondEdited() ) );
191
192   QList<SUIT_SelectionFilter*> filters;
193   filters.append( new SMESH_TypeFilter( MESHorSUBMESH ) );
194   filters.append( new SMESH_TypeFilter( GROUP ) );
195   myFilter = new SMESH_LogicalFilter( filters, SMESH_LogicalFilter::LO_OR );
196
197   mySecondTgt->setEnabled( mySecond->checkedId() != OriginTgt );
198   clear();
199
200   //setTarget( FirstTgt );
201 }
202
203 /*!
204   \brief Destructor
205 */
206 SMESHGUI_MinDistance::~SMESHGUI_MinDistance()
207 {
208   erasePreview();
209   if ( myPreview )
210     myPreview->Delete();
211 }
212
213 /*!
214   \brief Event filter
215   \param o object
216   \param o event
217   \return \c true if event is filtered or \c false otherwise
218 */
219 bool SMESHGUI_MinDistance::eventFilter( QObject* o, QEvent* e )
220 {
221   if ( e->type() == QEvent::FocusIn ) {
222     if ( o == myFirstTgt )
223       setTarget( FirstTgt );
224     else if ( o == mySecondTgt )
225       setTarget( SecondTgt );
226   }
227   return QWidget::eventFilter( o, e );
228 }
229
230 /*!
231   \brief Setup selection mode depending on the current widget state
232 */
233 void SMESHGUI_MinDistance::updateSelection()
234 {
235   LightApp_SelectionMgr* selMgr = SMESHGUI::selectionMgr();
236
237   disconnect( selMgr, 0, this, 0 );
238   selMgr->clearFilters();
239   
240   bool nodeMode = ( myCurrentTgt == FirstTgt  && myFirst->checkedId() == NodeTgt ) ||
241                   ( myCurrentTgt == SecondTgt && mySecond->checkedId() == NodeTgt );
242   bool elemMode = ( myCurrentTgt == FirstTgt  && myFirst->checkedId() == ElementTgt ) ||
243                   ( myCurrentTgt == SecondTgt && mySecond->checkedId() == ElementTgt );
244   bool objMode  = ( myCurrentTgt == FirstTgt  && myFirst->checkedId() == ObjectTgt ) ||
245                   ( myCurrentTgt == SecondTgt && mySecond->checkedId() == ObjectTgt ) ||
246                   ( myCurrentTgt == NoTgt );
247
248   if ( nodeMode ) {
249     SMESH::SetPointRepresentation( true );
250     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
251       aViewWindow->SetSelectionMode( NodeSelection );
252   }
253   else if ( elemMode ) {
254     SMESH::SetPointRepresentation( false );
255     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
256       aViewWindow->SetSelectionMode( CellSelection );
257   }
258   else if ( objMode ) {
259     SMESH::SetPointRepresentation( false );
260     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
261       aViewWindow->SetSelectionMode( ActorSelection );
262     selMgr->installFilter( myFilter );
263   }
264
265   connect( selMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( selectionChanged() ) );
266
267   if ( myCurrentTgt == FirstTgt )
268     firstEdited();
269   else if ( myCurrentTgt == SecondTgt )
270     secondEdited();
271
272   //selectionChanged();
273 }
274
275 /*!
276   \brief Deactivate widget
277 */
278 void SMESHGUI_MinDistance::deactivate()
279 {
280   disconnect( SMESHGUI::selectionMgr(), 0, this, 0 );
281 }
282
283 /*!
284   \brief Set current target for selection
285   \param target new target ID
286 */
287 void SMESHGUI_MinDistance::setTarget( int target )
288 {
289   if ( myCurrentTgt != target ) {
290     myCurrentTgt = target;
291     updateSelection();
292   }
293 }
294
295 /*!
296   \brief Erase preview actor
297 */
298 void SMESHGUI_MinDistance::erasePreview()
299 {
300   SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow();
301   if ( aViewWindow && myPreview ) {
302     aViewWindow->RemoveActor( myPreview );
303     aViewWindow->Repaint();
304   }
305 }
306
307 /*!
308   \brief Display preview actor
309 */
310 void SMESHGUI_MinDistance::displayPreview()
311 {
312   SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow();
313   if ( aViewWindow && myPreview ) {
314     aViewWindow->AddActor( myPreview );
315     aViewWindow->Repaint();
316   }
317 }
318
319 /*!
320   \brief Create preview actor
321   \param x1 X coordinate of first point
322   \param y1 X coordinate of first point
323   \param z1 Y coordinate of first point
324   \param x2 Y coordinate of second point
325   \param y2 Z coordinate of second point
326   \param z2 Z coordinate of second point
327 */
328 void SMESHGUI_MinDistance::createPreview( double x1, double y1, double z1, double x2, double y2, double z2 )
329 {
330   if ( myPreview )
331     myPreview->Delete();
332
333   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
334   // create points
335   vtkPoints* aPoints = vtkPoints::New();
336   aPoints->SetNumberOfPoints( 2 );
337   aPoints->SetPoint( 0, x1, y1, z1 );
338   aPoints->SetPoint( 1, x2, y2, z2 );
339   aGrid->SetPoints( aPoints );
340   aPoints->Delete();
341   // create cells
342   vtkIdList* anIdList = vtkIdList::New();
343   anIdList->SetNumberOfIds( 2 );
344   vtkCellArray* aCells = vtkCellArray::New();
345   aCells->Allocate( 2, 0);
346   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
347   aCellTypesArray->SetNumberOfComponents( 1 );
348   aCellTypesArray->Allocate( 1 );
349   anIdList->SetId( 0, 0 ); anIdList->SetId( 1, 1 );
350   aCells->InsertNextCell( anIdList );
351   aCellTypesArray->InsertNextValue( VTK_LINE );
352   anIdList->Delete();
353   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
354   aCellLocationsArray->SetNumberOfComponents( 1 );
355   aCellLocationsArray->SetNumberOfTuples( 1 );
356   aCells->InitTraversal();
357   for( vtkIdType idType = 0, *pts, npts; aCells->GetNextCell( npts, pts ); idType++ )
358     aCellLocationsArray->SetValue( idType, aCells->GetTraversalLocation( npts ) );
359   aGrid->SetCells( aCellTypesArray, aCellLocationsArray, aCells );
360   aCellLocationsArray->Delete();
361   aCellTypesArray->Delete();
362   aCells->Delete();
363   // create actor
364   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
365   aMapper->SetInput( aGrid );
366   aGrid->Delete();
367   myPreview = SALOME_Actor::New();
368   myPreview->PickableOff();
369   myPreview->SetMapper( aMapper );
370   aMapper->Delete();
371   vtkProperty* aProp = vtkProperty::New();
372   aProp->SetRepresentationToWireframe();
373   aProp->SetColor( 250, 0, 250 );
374   aProp->SetPointSize( 5 );
375   aProp->SetLineWidth( 3 );
376   myPreview->SetProperty( aProp );
377   aProp->Delete();
378 }
379
380 /*!
381   \brief Called when selection is changed
382 */
383 void SMESHGUI_MinDistance::selectionChanged()
384 {
385   SUIT_OverrideCursor wc;
386
387   SALOME_ListIO selected;
388   SMESHGUI::selectionMgr()->selectedObjects( selected );
389
390   if ( selected.Extent() == 1 ) {
391     Handle(SALOME_InteractiveObject) IO = selected.First();
392     SMESH::SMESH_IDSource_var obj = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
393     if ( !CORBA::is_nil( obj ) ) {
394       if ( myCurrentTgt == FirstTgt ) {
395         myFirstSrc = obj;
396         myFirstActor = SMESH::FindActorByEntry( IO->getEntry() );
397         if ( myFirst->checkedId() == ObjectTgt ) {
398           QString aName;
399           SMESH::GetNameOfSelectedIObjects( SMESHGUI::selectionMgr(), aName );
400           myFirstTgt->setText( aName );
401         }
402         else {
403           SVTK_Selector* selector = SMESH::GetViewWindow()->GetSelector();
404           QString ID;
405           int nb = 0;
406           if ( myFirstActor && selector ) {
407             nb = myFirst->checkedId() == NodeTgt ? 
408               SMESH::GetNameOfSelectedElements( selector, IO, ID ) :
409               SMESH::GetNameOfSelectedNodes( selector, IO, ID );
410           }
411           if ( nb == 1 )
412             myFirstTgt->setText( ID.trimmed() );
413           else
414             myFirstTgt->clear();
415         }
416       }
417       else if ( myCurrentTgt == SecondTgt ) {
418         mySecondSrc = obj;
419         mySecondActor = SMESH::FindActorByEntry( IO->getEntry() );
420         if ( mySecond->checkedId() == ObjectTgt ) {
421           QString aName;
422           SMESH::GetNameOfSelectedIObjects( SMESHGUI::selectionMgr(), aName );
423           mySecondTgt->setText( aName );
424         }
425         else {
426           SVTK_Selector* selector = SMESH::GetViewWindow()->GetSelector();
427           QString ID;
428           int nb = 0;
429           if ( mySecondActor && selector ) {
430             nb = mySecond->checkedId() == NodeTgt ? 
431               SMESH::GetNameOfSelectedElements( selector, IO, ID ) :
432               SMESH::GetNameOfSelectedNodes( selector, IO, ID );
433           }
434           if ( nb == 1 )
435             mySecondTgt->setText( ID.trimmed() );
436           else
437             mySecondTgt->clear();
438         }
439       }
440     }
441   }
442   clear();
443 }
444
445 /*!
446   \brief Called when first target mode is changed by the user
447 */
448 void SMESHGUI_MinDistance::firstChanged()
449 {
450   myFirstSrc = SMESH::SMESH_IDSource::_nil();
451   myFirstTgt->clear();
452   myFirstTgt->setReadOnly( myFirst->checkedId() == ObjectTgt );
453   myFirstTgt->setValidator( myFirst->checkedId() == ObjectTgt ? 0 : myValidator );
454   setTarget( FirstTgt );
455   updateSelection();
456   clear();
457 }
458
459 /*!
460   \brief Called when second target mode is changed by the user
461 */
462 void SMESHGUI_MinDistance::secondChanged()
463 {
464   mySecondSrc = SMESH::SMESH_IDSource::_nil();
465   mySecondTgt->setEnabled( mySecond->checkedId() != OriginTgt );
466   mySecondTgt->setReadOnly( mySecond->checkedId() == ObjectTgt );
467   mySecondTgt->setValidator( mySecond->checkedId() == ObjectTgt ? 0 : myValidator );
468   mySecondTgt->clear();
469   setTarget( mySecond->checkedId() != OriginTgt ? SecondTgt : NoTgt );
470   updateSelection();
471   clear();
472 }
473
474 /*!
475   \brief Called when first target is edited by the user
476 */
477 void SMESHGUI_MinDistance::firstEdited()
478 {
479   setTarget( FirstTgt );
480   if ( sender() == myFirstTgt )
481     clear();
482   SVTK_Selector* selector = SMESH::GetViewWindow()->GetSelector();
483   if ( myFirstActor && selector ) {
484     Handle(SALOME_InteractiveObject) IO = myFirstActor->getIO();
485     if ( myFirst->checkedId() == NodeTgt || myFirst->checkedId() == ElementTgt ) {
486       TColStd_MapOfInteger ID;
487       ID.Add( myFirstTgt->text().toLong() );
488       selector->AddOrRemoveIndex( IO, ID, false );
489     }
490     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
491       aViewWindow->highlight( IO, true, true );
492   }
493 }
494
495 /*!
496   \brief Called when second target is edited by the user
497 */
498 void SMESHGUI_MinDistance::secondEdited()
499 {
500   setTarget( SecondTgt );
501   if ( sender() == mySecondTgt )
502     clear();
503   SVTK_Selector* selector = SMESH::GetViewWindow()->GetSelector();
504   if ( mySecondActor && selector ) {
505     Handle(SALOME_InteractiveObject) IO = mySecondActor->getIO();
506     if ( mySecond->checkedId() == NodeTgt || mySecond->checkedId() == ElementTgt ) {
507       TColStd_MapOfInteger ID;
508       ID.Add( mySecondTgt->text().toLong() );
509       selector->AddOrRemoveIndex( IO, ID, false );
510     }
511     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
512       aViewWindow->highlight( IO, true, true );
513   }
514 }
515
516 /*!
517   \brief Compute the minimum distance between targets
518 */
519 void SMESHGUI_MinDistance::compute()
520 {
521   SUIT_OverrideCursor wc;
522   SMESH::SMESH_IDSource_var s1;
523   SMESH::SMESH_IDSource_var s2;
524   bool isOrigin = mySecond->checkedId() == OriginTgt;
525
526   // process first target
527   if ( !CORBA::is_nil( myFirstSrc ) ) {
528     if ( myFirst->checkedId() == NodeTgt || myFirst->checkedId() == ElementTgt ) {
529       SMESH::SMESH_Mesh_var m = myFirstSrc->GetMesh();
530       long id = myFirstTgt->text().toLong();
531       if ( !CORBA::is_nil( m ) && id ) {
532         SMESH::long_array_var ids = new SMESH::long_array();
533         ids->length( 1 );
534         ids[0] = id;
535         SMESH::SMESH_MeshEditor_var me = m->GetMeshEditor();
536         s1 = me->MakeIDSource( ids.in(), myFirst->checkedId() == NodeTgt ? SMESH::NODE : SMESH::FACE );
537       }
538     }
539     else {
540       s1 = myFirstSrc;
541     }
542   }
543
544   // process second target
545   if ( !CORBA::is_nil( mySecondSrc ) ) {
546     if ( mySecond->checkedId() == NodeTgt || mySecond->checkedId() == ElementTgt ) {
547       SMESH::SMESH_Mesh_var m = mySecondSrc->GetMesh();
548       long id = mySecondTgt->text().toLong();
549       if ( !CORBA::is_nil( m ) && id ) {
550         SMESH::long_array_var ids = new SMESH::long_array();
551         ids->length( 1 );
552         ids[0] = id;
553         SMESH::SMESH_MeshEditor_var me = m->GetMeshEditor();
554         s2 = me->MakeIDSource( ids.in(), mySecond->checkedId() == NodeTgt ? SMESH::NODE : SMESH::FACE );
555       }
556     }
557     else {
558       s2 = mySecondSrc;
559     }
560   }
561
562   if ( !CORBA::is_nil( s1 ) && ( !CORBA::is_nil( s2 ) || isOrigin ) ) {
563     // compute min distance
564     int precision = SMESHGUI::resourceMgr()->integerValue( "SMESH", "length_precision", 6 );
565     SMESH::Measurements_var measure = SMESHGUI::GetSMESHGen()->CreateMeasurements();
566     SMESH::Measure result = measure->MinDistance( s1.in(), s2.in() );
567     measure->UnRegister();
568     myDX->setText( QString::number( result.minX, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
569     myDY->setText( QString::number( result.minY, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
570     myDZ->setText( QString::number( result.minZ, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
571     myDistance->setText( QString::number( result.value, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
572     // update preview actor
573     erasePreview();
574     double x1, y1, z1, x2, y2, z2;
575     SMESH::double_array_var coord = s1->GetMesh()->GetNodeXYZ( result.node1 );
576     x1 = coord[0]; y1 = coord[1]; z1 = coord[2];
577     if ( isOrigin ) {
578       x2 = y2 = z2 = 0.;
579     }
580     else {
581       coord = s2->GetMesh()->GetNodeXYZ( result.node2 );
582       x2 = coord[0]; y2 = coord[1]; z2 = coord[2];
583     }
584     createPreview( x1, y1, z1, x2, y2, z2 );
585     displayPreview();
586   }
587   else {
588     clear();
589   }
590 }
591
592 /*!
593   \brief Reset the widget to the initial state (nullify result fields)
594 */
595 void SMESHGUI_MinDistance::clear()
596 {
597   myDX->clear();
598   myDY->clear();
599   myDZ->clear();
600   myDistance->clear();
601   erasePreview();
602 }
603
604 /*!
605   \class SMESHGUI_BoundingBox
606   \brief Bounding box measurement widget.
607   
608   Widget to calculate bounding box of the selected object(s).
609 */
610
611 /*!
612   \brief Constructor.
613   \param parent parent widget
614 */
615 SMESHGUI_BoundingBox::SMESHGUI_BoundingBox( QWidget* parent )
616 : QWidget( parent ), myActor( 0 ), myPreview( 0 )
617 {
618   QGroupBox* aSourceGrp = new QGroupBox( tr( "SOURCE" ), this );
619   QRadioButton* aObjects  = new QRadioButton( tr( "OBJECTS" ),  aSourceGrp );
620   QRadioButton* aNodes    = new QRadioButton( tr( "NODES" ),    aSourceGrp );
621   QRadioButton* aElements = new QRadioButton( tr( "ELEMENTS" ), aSourceGrp );
622   mySource = new QLineEdit( aSourceGrp );
623   
624   QGridLayout* fl = new QGridLayout( aSourceGrp );
625   fl->setMargin( MARGIN );
626   fl->setSpacing( SPACING );
627   fl->addWidget( aObjects,   0, 0 );
628   fl->addWidget( aNodes,     0, 1 );
629   fl->addWidget( aElements,  0, 2 );
630   fl->addWidget( mySource,   1, 0, 1, 3 );
631   
632   mySourceMode = new QButtonGroup( this );
633   mySourceMode->addButton( aObjects,  ObjectsSrc );
634   mySourceMode->addButton( aNodes,    NodesSrc );
635   mySourceMode->addButton( aElements, ElementsSrc );
636
637   QPushButton* aCompute = new QPushButton( tr( "COMPUTE" ), this );
638
639   QGroupBox* aResults = new QGroupBox( tr( "RESULT" ), this );
640   QLabel* aXminLab = new QLabel( "Xmin", aResults );
641   myXmin           = new QLineEdit( aResults );
642   QLabel* aXmaxLab = new QLabel( "Xmax", aResults );
643   myXmax           = new QLineEdit( aResults );
644   QLabel* aDxLab   = new QLabel( "dX", aResults );
645   myDX             = new QLineEdit( aResults );
646   QLabel* aYminLab = new QLabel( "Ymin", aResults );
647   myYmin           = new QLineEdit( aResults );
648   QLabel* aYmaxLab = new QLabel( "Ymax", aResults );
649   myYmax           = new QLineEdit( aResults );
650   QLabel* aDyLab   = new QLabel( "dY", aResults );
651   myDY             = new QLineEdit( aResults );
652   QLabel* aZminLab = new QLabel( "Zmin", aResults );
653   myZmin           = new QLineEdit( aResults );
654   QLabel* aZmaxLab = new QLabel( "Zmax", aResults );
655   myZmax           = new QLineEdit( aResults );
656   QLabel* aDzLab   = new QLabel( "dZ", aResults );
657   myDZ             = new QLineEdit( aResults );
658
659   QGridLayout* rl  = new QGridLayout( aResults );
660   rl->setMargin( MARGIN );
661   rl->setSpacing( SPACING );
662   rl->addWidget( aXminLab,   0, 0 );
663   rl->addWidget( myXmin,     0, 1 );
664   rl->addWidget( aXmaxLab,   0, 2 );
665   rl->addWidget( myXmax,     0, 3 );
666   rl->addWidget( aDxLab,     0, 4 );
667   rl->addWidget( myDX,       0, 5 );
668   rl->addWidget( aYminLab,   1, 0 );
669   rl->addWidget( myYmin,     1, 1 );
670   rl->addWidget( aYmaxLab,   1, 2 );
671   rl->addWidget( myYmax,     1, 3 );
672   rl->addWidget( aDyLab,     1, 4 );
673   rl->addWidget( myDY,       1, 5 );
674   rl->addWidget( aZminLab,   2, 0 );
675   rl->addWidget( myZmin,     2, 1 );
676   rl->addWidget( aZmaxLab,   2, 2 );
677   rl->addWidget( myZmax,     2, 3 );
678   rl->addWidget( aDzLab,     2, 4 );
679   rl->addWidget( myDZ,       2, 5 );
680
681   QGridLayout* l = new QGridLayout( this );
682   l->setMargin( MARGIN );
683   l->setSpacing( SPACING );
684
685   l->addWidget( aSourceGrp, 0, 0, 1, 2 );
686   l->addWidget( aCompute,   1, 0 );
687   l->addWidget( aResults,   2, 0, 1, 2 );
688   l->setColumnStretch( 1, 5 );
689   l->setRowStretch( 3, 5 );
690
691   aObjects->setChecked( true );
692   myXmin->setReadOnly( true );
693   myXmax->setReadOnly( true );
694   myDX->setReadOnly( true );
695   myYmin->setReadOnly( true );
696   myYmax->setReadOnly( true );
697   myDY->setReadOnly( true );
698   myZmin->setReadOnly( true );
699   myZmax->setReadOnly( true );
700   myDZ->setReadOnly( true );
701
702   myValidator = new SMESHGUI_IdValidator( this );
703
704   connect( mySourceMode, SIGNAL( buttonClicked( int ) ),  this, SLOT( sourceChanged() ) );
705   connect( aCompute,     SIGNAL( clicked() ),             this, SLOT( compute() ) );
706   connect( mySource,     SIGNAL( textEdited( QString ) ), this, SLOT( sourceEdited() ) );
707
708   QList<SUIT_SelectionFilter*> filters;
709   filters.append( new SMESH_TypeFilter( MESHorSUBMESH ) );
710   filters.append( new SMESH_TypeFilter( GROUP ) );
711   myFilter = new SMESH_LogicalFilter( filters, SMESH_LogicalFilter::LO_OR );
712
713   clear();
714 }
715
716 /*!
717   \brief Destructor
718 */
719 SMESHGUI_BoundingBox::~SMESHGUI_BoundingBox()
720 {
721   erasePreview();
722   if ( myPreview )
723     myPreview->Delete();
724 }
725
726 /*!
727   \brief Setup selection mode depending on the current widget state
728 */
729 void SMESHGUI_BoundingBox::updateSelection()
730 {
731   LightApp_SelectionMgr* selMgr = SMESHGUI::selectionMgr();
732
733   disconnect( selMgr, 0, this, 0 );
734   selMgr->clearFilters();
735   
736   bool nodeMode = mySourceMode->checkedId() == NodesSrc;
737   bool elemMode = mySourceMode->checkedId() == ElementsSrc;
738   bool objMode  = mySourceMode->checkedId() == ObjectsSrc;
739
740   if ( nodeMode ) {
741     SMESH::SetPointRepresentation( true );
742     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
743       aViewWindow->SetSelectionMode( NodeSelection );
744   }
745   else if ( elemMode ) {
746     SMESH::SetPointRepresentation( false );
747     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
748       aViewWindow->SetSelectionMode( CellSelection );
749   }
750   else if ( objMode ) {
751     SMESH::SetPointRepresentation( false );
752     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
753       aViewWindow->SetSelectionMode( ActorSelection );
754     selMgr->installFilter( myFilter );
755   }
756
757   connect( selMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( selectionChanged() ) );
758
759   sourceEdited();
760
761   //selectionChanged();
762 }
763
764 /*!
765   \brief Deactivate widget
766 */
767 void SMESHGUI_BoundingBox::deactivate()
768 {
769   disconnect( SMESHGUI::selectionMgr(), 0, this, 0 );
770 }
771
772 /*!
773   \brief Erase preview actor
774 */
775 void SMESHGUI_BoundingBox::erasePreview()
776 {
777   SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow();
778   if ( aViewWindow && myPreview ) {
779     aViewWindow->RemoveActor( myPreview );
780     aViewWindow->Repaint();
781   }
782 }
783
784 /*!
785   \brief Display preview actor
786 */
787 void SMESHGUI_BoundingBox::displayPreview()
788 {
789   SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow();
790   if ( aViewWindow && myPreview ) {
791     aViewWindow->AddActor( myPreview );
792     aViewWindow->Repaint();
793   }
794 }
795
796 /*!
797   \brief Create preview actor
798   \param minX min X coordinate of bounding box
799   \param maxX max X coordinate of bounding box
800   \param minY min Y coordinate of bounding box
801   \param maxY max Y coordinate of bounding box
802   \param minZ min Z coordinate of bounding box
803   \param maxZ max Z coordinate of bounding box
804 */
805 void SMESHGUI_BoundingBox::createPreview( double minX, double maxX, double minY, double maxY, double minZ, double maxZ )
806 {
807   if ( myPreview )
808     myPreview->Delete();
809
810   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
811   // create points
812   vtkPoints* aPoints = vtkPoints::New();
813   aPoints->SetNumberOfPoints( 8 );
814   aPoints->SetPoint( 0, minX, minY, minZ );
815   aPoints->SetPoint( 1, maxX, minY, minZ );
816   aPoints->SetPoint( 2, minX, maxY, minZ );
817   aPoints->SetPoint( 3, maxX, maxY, minZ );
818   aPoints->SetPoint( 4, minX, minY, maxZ );
819   aPoints->SetPoint( 5, maxX, minY, maxZ );
820   aPoints->SetPoint( 6, minX, maxY, maxZ );
821   aPoints->SetPoint( 7, maxX, maxY, maxZ );
822   aGrid->SetPoints( aPoints );
823   aPoints->Delete();
824   // create cells
825   // connectivity: 0-1 0-4 0-2 1-5 1-3 2-6 2-3 3-7 4-6 4-5 5-7 6-7
826   vtkIdList* anIdList = vtkIdList::New();
827   anIdList->SetNumberOfIds( 2 );
828   vtkCellArray* aCells = vtkCellArray::New();
829   aCells->Allocate( 2*12, 0);
830   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
831   aCellTypesArray->SetNumberOfComponents( 1 );
832   aCellTypesArray->Allocate( 12 );
833   anIdList->SetId( 0, 0 ); anIdList->SetId( 1, 1 );
834   aCells->InsertNextCell( anIdList );
835   aCellTypesArray->InsertNextValue( VTK_LINE );
836   anIdList->SetId( 0, 0 ); anIdList->SetId( 1, 4 );
837   aCells->InsertNextCell( anIdList );
838   aCellTypesArray->InsertNextValue( VTK_LINE );
839   anIdList->SetId( 0, 0 ); anIdList->SetId( 1, 2 );
840   aCells->InsertNextCell( anIdList );
841   aCellTypesArray->InsertNextValue( VTK_LINE );
842   anIdList->SetId( 0, 1 ); anIdList->SetId( 1, 5 );
843   aCells->InsertNextCell( anIdList );
844   aCellTypesArray->InsertNextValue( VTK_LINE );
845   anIdList->SetId( 0, 1 ); anIdList->SetId( 1, 3 );
846   aCells->InsertNextCell( anIdList );
847   aCellTypesArray->InsertNextValue( VTK_LINE );
848   anIdList->SetId( 0, 2 ); anIdList->SetId( 1, 6 );
849   aCells->InsertNextCell( anIdList );
850   aCellTypesArray->InsertNextValue( VTK_LINE );
851   anIdList->SetId( 0, 2 ); anIdList->SetId( 1, 3 );
852   aCells->InsertNextCell( anIdList );
853   aCellTypesArray->InsertNextValue( VTK_LINE );
854   anIdList->SetId( 0, 3 ); anIdList->SetId( 1, 7 );
855   aCells->InsertNextCell( anIdList );
856   aCellTypesArray->InsertNextValue( VTK_LINE );
857   anIdList->SetId( 0, 4 ); anIdList->SetId( 1, 6 );
858   aCells->InsertNextCell( anIdList );
859   aCellTypesArray->InsertNextValue( VTK_LINE );
860   anIdList->SetId( 0, 4 ); anIdList->SetId( 1, 5 );
861   aCells->InsertNextCell( anIdList );
862   aCellTypesArray->InsertNextValue( VTK_LINE );
863   anIdList->SetId( 0, 5 ); anIdList->SetId( 1, 7 );
864   aCells->InsertNextCell( anIdList );
865   aCellTypesArray->InsertNextValue( VTK_LINE );
866   anIdList->SetId( 0, 6 ); anIdList->SetId( 1, 7 );
867   aCells->InsertNextCell( anIdList );
868   aCellTypesArray->InsertNextValue( VTK_LINE );
869   anIdList->Delete();
870   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
871   aCellLocationsArray->SetNumberOfComponents( 1 );
872   aCellLocationsArray->SetNumberOfTuples( 12 );
873   aCells->InitTraversal();
874   for( vtkIdType idType = 0, *pts, npts; aCells->GetNextCell( npts, pts ); idType++ )
875     aCellLocationsArray->SetValue( idType, aCells->GetTraversalLocation( npts ) );
876   aGrid->SetCells( aCellTypesArray, aCellLocationsArray, aCells );
877   aCellLocationsArray->Delete();
878   aCellTypesArray->Delete();
879   aCells->Delete();
880   // create actor
881   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
882   aMapper->SetInput( aGrid );
883   aGrid->Delete();
884   myPreview = SALOME_Actor::New();
885   myPreview->PickableOff();
886   myPreview->SetMapper( aMapper );
887   aMapper->Delete();
888   vtkProperty* aProp = vtkProperty::New();
889   aProp->SetRepresentationToWireframe();
890   aProp->SetColor( 250, 0, 250 );
891   aProp->SetPointSize( 5 );
892   aProp->SetLineWidth( 3 );
893   myPreview->SetProperty( aProp );
894   aProp->Delete();
895 }
896
897 /*!
898   \brief Called when selection is changed
899 */
900 void SMESHGUI_BoundingBox::selectionChanged()
901 {
902   SUIT_OverrideCursor wc;
903
904   SALOME_ListIO selected;
905   SMESHGUI::selectionMgr()->selectedObjects( selected );
906
907   if ( selected.Extent() == 1 ) {
908     Handle(SALOME_InteractiveObject) IO = selected.First();
909     SMESH::SMESH_IDSource_var obj = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
910     if ( !CORBA::is_nil( obj ) ) {
911       mySrc.clear();
912       mySrc.append( obj );
913       myActor = SMESH::FindActorByEntry( IO->getEntry() );
914       if ( mySourceMode->checkedId() == ObjectsSrc ) {
915         QString aName;
916         SMESH::GetNameOfSelectedIObjects( SMESHGUI::selectionMgr(), aName );
917         mySource->setText( aName );
918       }
919       else {
920         SVTK_Selector* selector = SMESH::GetViewWindow()->GetSelector();
921         QString ID;
922         int nb = 0;
923         if ( myActor && selector ) {
924           nb = mySourceMode->checkedId() == NodesSrc ? 
925             SMESH::GetNameOfSelectedElements( selector, IO, ID ) :
926             SMESH::GetNameOfSelectedNodes( selector, IO, ID );
927         }
928         if ( nb > 0 ) {
929           myIDs = ID.trimmed();
930           if ( nb < MAX_NB_FOR_EDITOR ) {
931             mySource->setReadOnly( false );
932             if ( mySource->validator() != myValidator )
933               mySource->setValidator( myValidator );
934             mySource->setText( ID.trimmed() );
935           }
936           else {
937             mySource->setReadOnly( true );
938             mySource->setValidator( 0 );
939             mySource->setText( tr( "SELECTED_NB_OBJ" ).arg( nb )
940                                .arg( mySourceMode->checkedId() == NodesSrc ? tr( "NB_NODES" ) : tr( "NB_ELEMENTS") ) );
941           }
942         }
943         else {
944           myIDs = "";
945           mySource->clear();
946           mySource->setReadOnly( false );
947           mySource->setValidator( myValidator );
948         }
949       }
950     }
951   }
952   else if ( selected.Extent() > 1 ) {
953     myIDs = "";
954     SALOME_ListIteratorOfListIO It( selected );
955     mySrc.clear();
956     myActor = 0;
957     if ( mySourceMode->checkedId() == ObjectsSrc ) {
958       for( ; It.More(); It.Next()){
959         Handle(SALOME_InteractiveObject) IO = It.Value();
960         SMESH::SMESH_IDSource_var obj = SMESH::IObjectToInterface<SMESH::SMESH_IDSource>( IO );
961         if ( !CORBA::is_nil( obj ) ) {
962           mySrc.append( obj );
963         }
964       }
965       QString aName;
966       SMESH::GetNameOfSelectedIObjects( SMESHGUI::selectionMgr(), aName );
967       mySource->setText( aName );
968     }
969     else {
970       mySource->clear();
971     }
972   }
973   clear();
974 }
975
976 /*!
977   \brief Called when source mode is changed by the user
978 */
979 void SMESHGUI_BoundingBox::sourceChanged()
980 {
981   myIDs = "";
982   mySource->clear();
983   mySource->setReadOnly( mySourceMode->checkedId() == ObjectsSrc );
984   mySource->setValidator( mySourceMode->checkedId() == ObjectsSrc ? 0 : myValidator );
985   updateSelection();
986   clear();
987 }
988
989 /*!
990   \brief Called when source mode is edited by the user
991 */
992 void SMESHGUI_BoundingBox::sourceEdited()
993 {
994   if ( sender() == mySource )
995     clear();
996   SVTK_Selector* selector = SMESH::GetViewWindow()->GetSelector();
997   if ( myActor && selector ) {
998     Handle(SALOME_InteractiveObject) IO = myActor->getIO();
999     if ( mySourceMode->checkedId() == NodesSrc || mySourceMode->checkedId() == ElementsSrc ) {
1000       TColStd_MapOfInteger ID;
1001       if ( !mySource->isReadOnly() )
1002         myIDs = mySource->text();
1003       QStringList ids = myIDs.split( " ", QString::SkipEmptyParts );
1004       foreach ( QString id, ids )
1005         ID.Add( id.trimmed().toLong() );
1006       selector->AddOrRemoveIndex( IO, ID, false );
1007     }
1008     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
1009       aViewWindow->highlight( IO, true, true );
1010   }
1011 }
1012
1013 /*!
1014   \brief Calculate bounding box of the selected object(s)
1015 */
1016 void SMESHGUI_BoundingBox::compute()
1017 {
1018   SUIT_OverrideCursor wc;
1019   SMESH::ListOfIDSources_var srcList = new SMESH::ListOfIDSources();
1020   if ( mySourceMode->checkedId() == NodesSrc || mySourceMode->checkedId() == ElementsSrc ) {
1021     if ( mySrc.count() > 0 && !CORBA::is_nil( mySrc[0] ) ) {
1022       SMESH::SMESH_Mesh_var m = mySrc[0]->GetMesh();
1023       QStringList ids = myIDs.split( " ", QString::SkipEmptyParts );
1024       if ( !CORBA::is_nil( m ) && ids.count() > 0 ) {
1025         SMESH::long_array_var ids_in = new SMESH::long_array();
1026         ids_in->length( ids.count() );
1027         for( int i = 0; i < ids.count(); i++ )
1028           ids_in[i] = ids[i].trimmed().toLong();
1029         SMESH::SMESH_MeshEditor_var me = m->GetMeshEditor();
1030         SMESH::SMESH_IDSource_var s = me->MakeIDSource( ids_in.in(), mySourceMode->checkedId() == NodesSrc ? SMESH::NODE : SMESH::FACE ); 
1031         srcList->length( 1 );
1032         srcList[0] = s;
1033       }
1034     }
1035   }
1036   else {
1037     srcList->length( mySrc.count() );
1038     for( int i = 0; i < mySrc.count(); i++ )
1039       srcList[i] = mySrc[i];
1040   }
1041   if ( srcList->length() > 0 ) {
1042     // compute bounding box
1043     int precision = SMESHGUI::resourceMgr()->integerValue( "SMESH", "length_precision", 6 );
1044     SMESH::Measurements_var measure = SMESHGUI::GetSMESHGen()->CreateMeasurements();
1045     SMESH::Measure result = measure->BoundingBox( srcList.in() );
1046     measure->UnRegister();
1047     myXmin->setText( QString::number( result.minX, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
1048     myXmax->setText( QString::number( result.maxX, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
1049     myDX->setText( QString::number( result.maxX-result.minX, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
1050     myYmin->setText( QString::number( result.minY, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
1051     myYmax->setText( QString::number( result.maxY, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
1052     myDY->setText( QString::number( result.maxY-result.minY, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
1053     myZmin->setText( QString::number( result.minZ, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
1054     myZmax->setText( QString::number( result.maxZ, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
1055     myDZ->setText( QString::number( result.maxZ-result.minZ, precision > 0 ? 'f' : 'g', qAbs( precision ) ) );
1056     // update preview actor
1057     erasePreview();
1058     createPreview( result.minX, result.maxX, result.minY, result.maxY, result.minZ, result.maxZ );
1059     displayPreview();
1060   }
1061   else {
1062     clear();
1063   }
1064 }
1065
1066 /*!
1067   \brief Reset the widget to the initial state (nullify result fields)
1068 */
1069 void SMESHGUI_BoundingBox::clear()
1070 {
1071   myXmin->clear();
1072   myXmax->clear();
1073   myDX->clear();
1074   myYmin->clear();
1075   myYmax->clear();
1076   myDY->clear();
1077   myZmin->clear();
1078   myZmax->clear();
1079   myDZ->clear();
1080   erasePreview();
1081 }
1082
1083 /*!
1084   \class SMESHGUI_MeshInfoDlg
1085   \brief Centralized dialog box for the measurements
1086 */
1087
1088 /*!
1089   \brief Constructor
1090   \param parent parent widget
1091   \param page specifies the dialog page to be shown at the start-up
1092 */
1093 SMESHGUI_MeasureDlg::SMESHGUI_MeasureDlg( QWidget* parent, int page )
1094 : QDialog( parent )
1095 {
1096   setModal( false );
1097   setAttribute( Qt::WA_DeleteOnClose, true );
1098   setWindowTitle( tr( "MEASUREMENTS" ) );
1099   setSizeGripEnabled( true );
1100
1101   SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
1102
1103   myTabWidget = new QTabWidget( this );
1104
1105   // min distance
1106
1107   myMinDist = new SMESHGUI_MinDistance( myTabWidget );
1108   myTabWidget->addTab( myMinDist, resMgr->loadPixmap( "SMESH", tr( "ICON_MEASURE_MIN_DIST" ) ), tr( "MIN_DIST" ) );
1109
1110   // bounding box
1111   
1112   myBndBox = new SMESHGUI_BoundingBox( myTabWidget );
1113   myTabWidget->addTab( myBndBox, resMgr->loadPixmap( "SMESH", tr( "ICON_MEASURE_BND_BOX" ) ), tr( "BND_BOX" ) );
1114
1115   // buttons
1116   QPushButton* okBtn = new QPushButton( tr( "SMESH_BUT_OK" ), this );
1117   okBtn->setAutoDefault( true );
1118   okBtn->setDefault( true );
1119   okBtn->setFocus();
1120   QPushButton* helpBtn = new QPushButton( tr( "SMESH_BUT_HELP" ), this );
1121   helpBtn->setAutoDefault( true );
1122
1123   QHBoxLayout* btnLayout = new QHBoxLayout;
1124   btnLayout->setSpacing( SPACING );
1125   btnLayout->setMargin( 0 );
1126   btnLayout->addWidget( okBtn );
1127   btnLayout->addStretch( 10 );
1128   btnLayout->addWidget( helpBtn );
1129
1130   QVBoxLayout* l = new QVBoxLayout ( this );
1131   l->setMargin( MARGIN );
1132   l->setSpacing( SPACING );
1133   l->addWidget( myTabWidget );
1134   l->addStretch();
1135   l->addLayout( btnLayout );
1136
1137   myTabWidget->setCurrentIndex( qMax( (int)MinDistance, qMin( (int)BoundingBox, page ) ) );
1138
1139   connect( okBtn,       SIGNAL( clicked() ),              this, SLOT( reject() ) );
1140   connect( helpBtn,     SIGNAL( clicked() ),              this, SLOT( help() ) );
1141   connect( myTabWidget, SIGNAL( currentChanged( int  ) ), this, SLOT( updateSelection() ) );
1142   connect( SMESHGUI::GetSMESHGUI(),  SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( deactivate() ) );
1143   connect( SMESHGUI::GetSMESHGUI(),  SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( reject() ) );
1144
1145   updateSelection();
1146 }
1147
1148 /*!
1149   \brief Destructor
1150 */
1151 SMESHGUI_MeasureDlg::~SMESHGUI_MeasureDlg()
1152 {
1153 }
1154
1155 /*!
1156   \brief Perform clean-up actions on the dialog box closing.
1157 */
1158 void SMESHGUI_MeasureDlg::reject()
1159 {
1160   LightApp_SelectionMgr* selMgr = SMESHGUI::selectionMgr();
1161   selMgr->clearFilters();
1162   SMESH::SetPointRepresentation( false );
1163   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow() )
1164     aViewWindow->SetSelectionMode( ActorSelection );
1165   QDialog::reject();
1166 }
1167
1168 /*!
1169   \brief Process keyboard event
1170   \param e key press event
1171 */
1172 void SMESHGUI_MeasureDlg::keyPressEvent( QKeyEvent* e )
1173 {
1174   QDialog::keyPressEvent( e );
1175   if ( !e->isAccepted() && e->key() == Qt::Key_F1 ) {
1176     e->accept();
1177     help();
1178   }
1179 }
1180
1181 /*!
1182   \brief Reactivate dialog box, when mouse pointer goes into it.
1183 */
1184 void SMESHGUI_MeasureDlg::enterEvent( QEvent* )
1185 {
1186   activate();
1187 }
1188
1189 /*!
1190   \brief Setup selection mode depending on the current dialog box state.
1191 */
1192 void SMESHGUI_MeasureDlg::updateSelection()
1193 {
1194   if ( myTabWidget->currentIndex() == MinDistance )
1195     myMinDist->updateSelection();
1196   else if ( myTabWidget->currentIndex() == BoundingBox )
1197     myBndBox->updateSelection();
1198     
1199 }
1200
1201 /*!
1202   \brief Show help page
1203 */
1204 void SMESHGUI_MeasureDlg::help()
1205 {
1206   SMESH::ShowHelpFile( myTabWidget->currentIndex() == MinDistance ?
1207                        "measurements_page.html#min_distance_anchor" : 
1208                        "measurements_page.html#bounding_box_anchor" );
1209 }
1210
1211 /*!
1212   \brief Activate dialog box
1213 */
1214 void SMESHGUI_MeasureDlg::activate()
1215 {
1216   SMESHGUI::GetSMESHGUI()->EmitSignalDeactivateDialog();
1217   SMESHGUI::GetSMESHGUI()->SetActiveDialogBox( this );
1218   myTabWidget->setEnabled( true );
1219   updateSelection();
1220 }
1221
1222 /*!
1223   \brief Deactivate dialog box
1224 */
1225 void SMESHGUI_MeasureDlg::deactivate()
1226 {
1227   myMinDist->deactivate();
1228   myBndBox->deactivate();
1229   myTabWidget->setEnabled( false );
1230   disconnect( SMESHGUI::selectionMgr(), SIGNAL( currentSelectionChanged() ), this, SLOT( updateInfo() ) );
1231 }