Salome HOME
0288e565641a7e391054131eed72dde7657ca8a7
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_FilterDlg.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_FilterDlg.cxx
25 // Author : Sergey LITONIN, Open CASCADE S.A.S.
26 //
27
28 // SMESH includes
29 #include "SMESHGUI_FilterDlg.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34 #include "SMESHGUI_Filter.h"
35 #include "SMESHGUI_FilterUtils.h"
36 #include "SMESHGUI_FilterLibraryDlg.h"
37 #include "SMESHGUI_SpinBox.h"
38
39 #include "SMESH_Actor.h"
40 #include "SMESH_NumberFilter.hxx"
41 #include "SMESH_TypeFilter.hxx"
42 #include "SMESH_LogicalFilter.hxx"
43
44 // SALOME GEOM includes
45 #include <GEOMBase.h>
46 #include <GEOM_FaceFilter.h>
47 #include <GEOM_TypeFilter.h>
48
49 // SALOME GUI includes
50 #include <SUIT_Desktop.h>
51 #include <SUIT_ResourceMgr.h>
52 #include <SUIT_Session.h>
53 #include <SUIT_MessageBox.h>
54 #include <SUIT_OverrideCursor.h>
55 #include <QtxColorButton.h>
56
57 #include <LightApp_Application.h>
58 #include <LightApp_SelectionMgr.h>
59 #include <SalomeApp_Application.h>
60 #include <SalomeApp_DoubleSpinBox.h>
61 #include <SalomeApp_IntSpinBox.h>
62 #include <SalomeApp_Study.h>
63 #include <SalomeApp_Tools.h>
64
65 #include <SALOME_ListIO.hxx>
66
67 #include <SVTK_ViewWindow.h>
68
69 // SALOME KERNEL includes
70 #include <SALOMEDSClient_Study.hxx>
71 #include <Basics_Utils.hxx>
72
73 // OCCT includes
74 #include <StdSelect_TypeOfFace.hxx>
75 #include <BRep_Tool.hxx>
76 #include <TopoDS.hxx>
77 #include <TopoDS_Shape.hxx>
78 #include <Geom_Plane.hxx>
79 #include <Geom_CylindricalSurface.hxx>
80 #include <Precision.hxx>
81 #include <TColStd_MapOfInteger.hxx>
82 #include <TColStd_IndexedMapOfInteger.hxx>
83 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
84
85 // Qt includes
86 #include <QFrame>
87 #include <QLineEdit>
88 #include <QPushButton>
89 #include <QGroupBox>
90 #include <QTableWidget>
91 #include <QStringList>
92 #include <QHBoxLayout>
93 #include <QVBoxLayout>
94 #include <QGridLayout>
95 #include <QStackedWidget>
96 #include <QApplication>
97 #include <QComboBox>
98 #include <QFontMetrics>
99 #include <QLabel>
100 #include <QButtonGroup>
101 #include <QRadioButton>
102 #include <QRegExp>
103 #include <QListWidget>
104 #include <QCheckBox>
105 #include <QItemDelegate>
106 #include <QDoubleValidator>
107 #include <QKeyEvent>
108 #include <QHeaderView>
109
110 // IDL includes
111 #include <SALOMEconfig.h>
112 #include CORBA_SERVER_HEADER(SMESH_Group)
113
114 #define SPACING 6
115 #define MARGIN  11
116
117 //---------------------------------------
118 // maxLength
119 //---------------------------------------
120 static int maxLength (const QMap<int, QString> theMap, const QFontMetrics& theMetrics)
121 {
122   int aRes = 0;
123   QMap<int, QString>::const_iterator anIter;
124   for (anIter = theMap.begin(); anIter != theMap.end(); ++anIter)
125     aRes = qMax(aRes, theMetrics.width(anIter.value()));
126   return aRes;
127 }
128
129 //---------------------------------------
130 // getFilterId
131 //---------------------------------------
132 static int getFilterId (SMESH::ElementType theType)
133 {
134   switch (theType)
135   {
136     case SMESH::NODE   : return SMESH::NodeFilter;
137     case SMESH::EDGE   : return SMESH::EdgeFilter;
138     case SMESH::FACE   : return SMESH::FaceFilter;
139     case SMESH::VOLUME : return SMESH::VolumeFilter;
140     case SMESH::ALL    : return SMESH::AllElementsFilter;
141     default            : return SMESH::UnknownFilter;
142   }
143 }
144
145 /*
146   Class       : SMESHGUI_FilterTable::AdditionalWidget
147   Description : Class for storing additional parameters of criterion
148 */
149
150 class SMESHGUI_FilterTable::AdditionalWidget : public QWidget
151 {
152 public:
153   enum { Tolerance };
154
155 public:
156   AdditionalWidget(QWidget* theParent);
157   virtual ~AdditionalWidget();
158
159   virtual QList<int>      GetParameters() const;
160   virtual bool            IsValid(const bool = true) const;
161   virtual double          GetDouble(const int) const;
162   virtual int             GetInteger(const int) const;
163   virtual QString         GetString(const int) const;
164   virtual void            SetDouble(const int, const double);
165   virtual void            SetInteger(const int, const int);
166   virtual void            SetString(const int, const QString&);
167   void                    SetEditable(const int, const bool);
168   void                    SetEditable(const bool);
169   void                    SetPrecision(const int, const char* = 0);
170
171 private:
172   QMap< int, QWidget* > myWidgets;
173 };
174
175 SMESHGUI_FilterTable::AdditionalWidget::AdditionalWidget (QWidget* theParent)
176   : QWidget(theParent)
177 {
178   QLabel* aLabel = new QLabel(tr("SMESH_TOLERANCE"), this);
179
180   SMESHGUI_SpinBox* sb = new SMESHGUI_SpinBox(this);
181   sb->setAcceptNames( false ); // No Notebook variables allowed
182   sb->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
183   sb->RangeStepAndValidator( 0., 1.e20, 0.1, "len_tol_precision" );
184   myWidgets[ Tolerance ] = sb;
185
186   QHBoxLayout* aLay = new QHBoxLayout(this);
187   aLay->setSpacing(SPACING);
188   aLay->setMargin(0);
189
190   aLay->addWidget(aLabel);
191   aLay->addWidget(myWidgets[ Tolerance ]);
192   aLay->addStretch();
193
194   SetDouble( Tolerance, Precision::Confusion() );
195 }
196
197 SMESHGUI_FilterTable::AdditionalWidget::~AdditionalWidget()
198 {
199 }
200
201 QList<int> SMESHGUI_FilterTable::AdditionalWidget::GetParameters() const
202 {
203   QList<int> theList;
204   theList.append(Tolerance);
205   return theList;
206 }
207
208 bool SMESHGUI_FilterTable::AdditionalWidget::IsValid (const bool theMsg) const
209 {
210   if (!isEnabled())
211     return true;
212
213   QList<int> aParams = GetParameters();
214   QList<int>::const_iterator anIter;
215   for (anIter = aParams.begin(); anIter != aParams.end(); ++anIter) {
216     if ( !myWidgets.contains( *anIter ) ) continue;
217     bool valid = true;
218     if ( qobject_cast<QLineEdit*>( myWidgets[ *anIter ] ) ) {
219       valid = qobject_cast<QLineEdit*>( myWidgets[ *anIter ] )->hasAcceptableInput();
220     }
221     else if ( qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ *anIter ] ) ) {
222       QString foo;
223       valid = qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ *anIter ] )->isValid( foo, false );
224     }
225     if (!valid && theMsg) {
226       SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
227                                    tr("SMESHGUI_INVALID_PARAMETERS"));
228       return false;
229     }
230   }
231
232   return true;
233 }
234
235 double SMESHGUI_FilterTable::AdditionalWidget::GetDouble (const int theId) const
236 {
237   double retval = 0;
238   if ( myWidgets.contains( theId ) ) {
239     if ( qobject_cast<QLineEdit*>( myWidgets[ theId ] ) )
240       retval = qobject_cast<QLineEdit*>( myWidgets[ theId ] )->text().toDouble();
241     if ( qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] ) )
242       retval = qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] )->GetValue();
243   }
244   return retval;
245 }
246
247 int SMESHGUI_FilterTable::AdditionalWidget::GetInteger (const int theId) const
248 {
249   int retval = 0;
250   if ( myWidgets.contains( theId ) ) {
251     if ( qobject_cast<QLineEdit*>( myWidgets[ theId ] ) )
252       retval = qobject_cast<QLineEdit*>( myWidgets[ theId ] )->text().toInt();
253     if ( qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] ) )
254       retval = (int)( qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] )->GetValue() );
255   }
256   return retval;
257 }
258
259 QString SMESHGUI_FilterTable::AdditionalWidget::GetString (const int theId) const
260 {
261   QString retval = "";
262   if ( myWidgets.contains( theId ) ) {
263     if ( qobject_cast<QLineEdit*>( myWidgets[ theId ] ) )
264       retval = qobject_cast<QLineEdit*>( myWidgets[ theId ] )->text();
265     if ( qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] ) )
266       retval = QString::number( qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] )->GetValue() );
267   }
268   return retval;
269 }
270
271 void SMESHGUI_FilterTable::AdditionalWidget::SetDouble (const int theId, const double theVal)
272 {
273   if ( myWidgets.contains( theId ) ) {
274     if ( qobject_cast<QLineEdit*>( myWidgets[ theId ] ) )
275       qobject_cast<QLineEdit*>( myWidgets[ theId ] )->setText( QString::number( theVal ) );
276     if ( qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] ) )
277       qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] )->SetValue( theVal );
278   }
279 }
280
281 void SMESHGUI_FilterTable::AdditionalWidget::SetInteger (const int theId, const int theVal)
282 {
283   if ( myWidgets.contains( theId ) ) {
284     if ( qobject_cast<QLineEdit*>( myWidgets[ theId ] ) )
285       qobject_cast<QLineEdit*>( myWidgets[ theId ] )->setText( QString::number( theVal ) );
286     if ( qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] ) )
287       qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] )->SetValue( (double)theVal );
288   }
289 }
290
291 void SMESHGUI_FilterTable::AdditionalWidget::SetString (const int theId, const QString& theVal)
292 {
293   if ( myWidgets.contains( theId ) ) {
294     if ( qobject_cast<QLineEdit*>( myWidgets[ theId ] ) )
295       qobject_cast<QLineEdit*>( myWidgets[ theId ] )->setText( theVal );
296     if ( qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] ) )
297       qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] )->SetValue( theVal.toDouble() );
298   }
299 }
300
301 void SMESHGUI_FilterTable::AdditionalWidget::SetEditable (const int theId, const bool isEditable)
302 {
303   if ( myWidgets.contains( theId ) ) {
304     if ( qobject_cast<QLineEdit*>( myWidgets[ theId ] ) )
305       qobject_cast<QLineEdit*>( myWidgets[ theId ] )->setReadOnly( !isEditable );
306     if ( qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] ) )
307       qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] )->setReadOnly( !isEditable );
308   }
309 }
310
311 void SMESHGUI_FilterTable::AdditionalWidget::SetEditable (const bool isEditable)
312 {
313   QList<int> aParams = GetParameters();
314   QList<int>::const_iterator anIter;
315   for (anIter = aParams.begin(); anIter != aParams.end(); ++anIter)
316     SetEditable( *anIter,  isEditable );
317 }
318
319 void SMESHGUI_FilterTable::AdditionalWidget::SetPrecision(const int theId, const char* precision)
320 {
321   if ( myWidgets.contains( theId ) ) {
322     if ( qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] ) ) {
323       SMESHGUI_SpinBox* sb = qobject_cast<SMESHGUI_SpinBox*>( myWidgets[ theId ] );
324       double val = sb->GetValue();
325       double min = pow(10.0, -(sb->decimals()));
326       sb->RangeStepAndValidator( 0., 1.e20, 0.1, precision ? precision : "len_tol_precision" );
327       sb->SetValue( qMax( val, min ) );
328     }
329   }
330 }
331
332 /*
333   Class       : SMESHGUI_FilterTable::ComboItem
334   Description : Combo table item. Identificator corresponding to string may be assigned
335 */
336
337 class SMESHGUI_FilterTable::ComboItem : public QTableWidgetItem
338 {
339 public:
340   static int     Type();
341
342   ComboItem( const QMap<int, QString>& );
343
344   void           setItems( const QMap<int, QString>& );
345   void           clear();
346   int            count() const;
347
348   int            value() const;
349   void           setValue( const int );
350
351 private:
352   int            id( int ) const;
353   int            index( int ) const;
354
355 private:
356   QMap<int, int> myIdToIdx;
357 };
358
359 int SMESHGUI_FilterTable::ComboItem::Type()
360 {
361   return QTableWidgetItem::UserType + 1;
362 }
363
364 SMESHGUI_FilterTable::ComboItem::ComboItem( const QMap<int, QString>& theIds )
365  : QTableWidgetItem( Type() )
366 {
367   setItems( theIds );
368 }
369
370 void SMESHGUI_FilterTable::ComboItem::setItems( const QMap<int, QString>& theIds )
371 {
372   myIdToIdx.clear();
373   QMap<int, QString>::const_iterator it;
374   QStringList items;
375   for ( it = theIds.begin(); it != theIds.end(); ++it ) {
376     myIdToIdx[it.key()] = items.count();
377     items.append( it.value() );
378   }
379   setData( Qt::UserRole, items );
380   setValue( id( 0 ) ); 
381 }
382
383 void SMESHGUI_FilterTable::ComboItem::clear()
384 {
385   QMap<int, QString> empty;
386   setItems( empty );
387 }
388
389 int SMESHGUI_FilterTable::ComboItem::count() const
390 {
391   return myIdToIdx.count();
392 }
393
394 int SMESHGUI_FilterTable::ComboItem::value() const
395 {
396   return( id( data( Qt::UserRole ).toStringList().indexOf( text() ) ) ); 
397 }
398
399 void SMESHGUI_FilterTable::ComboItem::setValue( const int theId )
400 {
401   int idx = index( theId );
402   QStringList items = data( Qt::UserRole ).toStringList();
403   setText( idx >= 0 && idx < items.count() ? items[idx] : "" );
404 }
405
406 int SMESHGUI_FilterTable::ComboItem::id( int idx ) const
407 {
408   QMap<int,int>::const_iterator it;
409   for ( it = myIdToIdx.begin(); it != myIdToIdx.end(); ++it )
410     if ( it.value() == idx ) return it.key();
411   return -1;
412 }
413
414 int SMESHGUI_FilterTable::ComboItem::index( int i ) const
415 {
416   return myIdToIdx.contains( i ) ? myIdToIdx[i] : -1;
417 }
418 /*
419   Class       : SMESHGUI_FilterTable::CheckItem
420   Description : Check table item.
421 */
422
423 class SMESHGUI_FilterTable::CheckItem : public QTableWidgetItem
424 {
425 public:
426   static int     Type();
427
428   CheckItem( bool = false );
429   CheckItem( const QString&, bool = false );
430   ~CheckItem();
431
432   void  setChecked( bool );
433   bool  checked() const;
434 };
435
436 int SMESHGUI_FilterTable::CheckItem::Type()
437 {
438   return QTableWidgetItem::UserType + 2;
439 }
440
441 SMESHGUI_FilterTable::CheckItem::CheckItem( bool value )
442  : QTableWidgetItem( Type() )
443 {
444   Qt::ItemFlags f = flags();
445   f = f | Qt::ItemIsUserCheckable;
446   f = f & ~Qt::ItemIsTristate;
447   f = f & ~Qt::ItemIsEditable;
448   setFlags( f );
449   setChecked(value);
450 }
451
452 SMESHGUI_FilterTable::CheckItem::CheckItem( const QString& text, bool value )
453  : QTableWidgetItem( Type() )
454 {
455   Qt::ItemFlags f = flags();
456   f = f | Qt::ItemIsUserCheckable;
457   f = f & ~Qt::ItemIsTristate;
458   f = f & ~Qt::ItemIsEditable;
459   setFlags( f );
460   setChecked( value );
461   setText( text );
462 }
463
464 SMESHGUI_FilterTable::CheckItem::~CheckItem()
465 {
466 }
467
468 void SMESHGUI_FilterTable::CheckItem::setChecked( bool value )
469 {
470   setCheckState( value ? Qt::Checked : Qt::Unchecked );
471 }
472
473 bool SMESHGUI_FilterTable::CheckItem::checked() const
474 {
475   return checkState() == Qt::Checked;
476 }
477
478 /*
479   Class       : SMESHGUI_FilterTable::IntSpinItem
480   Description : Integer spin table item.
481 */
482
483 class SMESHGUI_FilterTable::IntSpinItem : public QTableWidgetItem
484 {
485 public:
486   static int     Type();
487
488   IntSpinItem( const int theValue );
489
490   int            value() const;
491   void           setValue( const int theValue );
492
493   void           clear();
494 };
495
496 int SMESHGUI_FilterTable::IntSpinItem::Type()
497 {
498   return QTableWidgetItem::UserType + 3;
499 }
500
501 SMESHGUI_FilterTable::IntSpinItem::IntSpinItem( const int theValue )
502  : QTableWidgetItem( Type() )
503 {
504   setValue( theValue );
505 }
506
507 int SMESHGUI_FilterTable::IntSpinItem::value() const
508 {
509   bool ok = false;
510   int value = data( Qt::UserRole ).toInt( &ok );
511   return ok ? value : 0; 
512 }
513
514 void SMESHGUI_FilterTable::IntSpinItem::setValue( const int theValue )
515 {
516   setData( Qt::UserRole, theValue );
517   setText( QString::number( theValue ) ); 
518 }
519
520 void SMESHGUI_FilterTable::IntSpinItem::clear()
521 {
522   setText( "" );
523 }
524
525 /*
526   Class       : SMESHGUI_FilterTable::DoubleSpinItem
527   Description : Double spin table item.
528 */
529
530 class SMESHGUI_FilterTable::DoubleSpinItem : public QTableWidgetItem
531 {
532 public:
533   static int     Type();
534
535   DoubleSpinItem( const double theValue );
536
537   double         value() const;
538   void           setValue( const double theValue );
539
540   int            precision() const;
541   void           setPrecision( const int thePrecision );
542
543   void           clear();
544 };
545
546 int SMESHGUI_FilterTable::DoubleSpinItem::Type()
547 {
548   return QTableWidgetItem::UserType + 4;
549 }
550
551 SMESHGUI_FilterTable::DoubleSpinItem::DoubleSpinItem( const double theValue )
552  : QTableWidgetItem( Type() )
553 {
554   setValue( theValue );
555 }
556
557 double SMESHGUI_FilterTable::DoubleSpinItem::value() const
558 {
559   bool ok = false;
560   double value = data( Qt::UserRole ).toDouble( &ok );
561   return ok ? value : 0; 
562 }
563
564 void SMESHGUI_FilterTable::DoubleSpinItem::setValue( const double theValue )
565 {
566   setData( Qt::UserRole, theValue );
567   setText( QString::number( theValue ) ); 
568 }
569
570 int SMESHGUI_FilterTable::DoubleSpinItem::precision() const
571 {
572   bool ok = false;
573   int precision = data( Qt::UserRole + 1 ).toInt( &ok );
574   return ok ? precision : 0; 
575 }
576
577 void SMESHGUI_FilterTable::DoubleSpinItem::setPrecision( const int thePrecision )
578 {
579   setData( Qt::UserRole + 1, thePrecision );
580 }
581
582 void SMESHGUI_FilterTable::DoubleSpinItem::clear()
583 {
584   setText( "" );
585 }
586
587 /*
588   Class       : SMESHGUI_FilterTable::ComboDelegate
589   Description : Table used by this widget
590 */
591
592 class SMESHGUI_FilterTable::ComboDelegate : public QItemDelegate
593 {
594 public:
595   ComboDelegate( QObject* = 0 );
596   ~ComboDelegate();
597   
598   QWidget*      createEditor( QWidget*, const QStyleOptionViewItem&,
599                               const QModelIndex& ) const;
600   
601   void          setEditorData( QWidget*, const QModelIndex& ) const;
602   void          setModelData( QWidget*, QAbstractItemModel*, const QModelIndex& ) const;
603   
604   void          updateEditorGeometry( QWidget*, const QStyleOptionViewItem&, 
605                                       const QModelIndex& ) const;
606 private:
607   QTableWidget* myTable;
608 };
609
610 SMESHGUI_FilterTable::ComboDelegate::ComboDelegate( QObject* parent )
611   : QItemDelegate( parent ), 
612     myTable( qobject_cast<QTableWidget*>( parent ) )
613 {
614 }
615   
616 SMESHGUI_FilterTable::ComboDelegate::~ComboDelegate()
617 {
618 }
619
620 QWidget* SMESHGUI_FilterTable::ComboDelegate::createEditor( QWidget* parent,
621                                                             const QStyleOptionViewItem& option,
622                                                             const QModelIndex& index ) const
623 {
624   QVariant aData = index.data( Qt::UserRole );
625   QVariant::Type aDataType = aData.type();
626   if( aDataType == QVariant::StringList ) {
627     QStringList l = aData.toStringList();
628     if ( !l.isEmpty() ) {
629       QComboBox* cb = new QComboBox( parent );
630       cb->setFrame( false );
631       cb->addItems( l );
632       return cb;
633     }
634   }
635   else if( aDataType == QVariant::Int ) {
636     bool ok = false;
637     int aValue = aData.toInt( &ok );
638     if ( ok ) {
639       SalomeApp_IntSpinBox* intSpin = new SalomeApp_IntSpinBox( 0, 1000, 1, parent, false, true );
640       intSpin->setFrame( false );
641       intSpin->setValue( aValue );
642       return intSpin;
643     }
644   }
645   else if( aDataType == QVariant::Double ) {
646     bool ok = false;
647     double aValue = aData.toDouble( &ok );
648     if ( ok ) {
649       int aPrecision = index.data( Qt::UserRole + 1 ).toInt( &ok );
650       if ( !ok )
651         aPrecision = 0;
652
653       SalomeApp_DoubleSpinBox* dblSpin = new SalomeApp_DoubleSpinBox( -1.e20, 1.e20, 1, aPrecision, 20, parent, false, true );
654       dblSpin->setFrame( false );
655       dblSpin->setValue( aValue );
656       return dblSpin;
657     }
658   }
659   return QItemDelegate::createEditor( parent, option, index );
660 }
661
662 void SMESHGUI_FilterTable::ComboDelegate::setEditorData( QWidget* editor, 
663                                                          const QModelIndex& index ) const
664 {
665   QVariant data = index.model()->data( index, Qt::DisplayRole );
666   QString value = data.toString();
667   bool bOk = false;
668   if ( QComboBox* cb = dynamic_cast<QComboBox*>( editor ) ) {
669     int i = cb->findText( value );
670     if ( i >= 0 ) {
671       cb->setCurrentIndex( i );
672       bOk = true;
673     }
674   }
675   else if ( SalomeApp_DoubleSpinBox* dblSpin = dynamic_cast<SalomeApp_DoubleSpinBox*>( editor ) ) {
676     if( data.type() == QVariant::Double ) {
677       double valueDouble = data.toDouble( &bOk );
678       if( bOk )
679         dblSpin->setValue( valueDouble );
680     }
681   }
682   if ( !bOk ) QItemDelegate::setEditorData( editor, index );
683 }
684
685 void SMESHGUI_FilterTable::ComboDelegate::setModelData( QWidget* editor,
686                                                         QAbstractItemModel* model,
687                                                         const QModelIndex& index) const
688 {
689   if( QComboBox* cb = dynamic_cast<QComboBox*>( editor ) )
690     model->setData( index, cb->currentText(), Qt::DisplayRole );
691   else if( SalomeApp_IntSpinBox* intSpin = dynamic_cast<SalomeApp_IntSpinBox*>( editor ) )
692     model->setData( index, intSpin->value(), Qt::DisplayRole );
693   else if( SalomeApp_DoubleSpinBox* dblSpin = dynamic_cast<SalomeApp_DoubleSpinBox*>( editor ) )
694     model->setData( index, dblSpin->value(), Qt::DisplayRole );
695   else QItemDelegate::setModelData( editor, model, index );
696 }
697
698 void SMESHGUI_FilterTable::ComboDelegate::updateEditorGeometry( QWidget* editor,
699                                                                 const QStyleOptionViewItem& option, 
700                                                                 const QModelIndex& index ) const
701 {
702   editor->setGeometry( option.rect );
703 }
704
705 /*
706   Class       : SMESHGUI_FilterTable::Table
707   Description : Table used by this widget
708 */
709
710 class SMESHGUI_FilterTable::Table : public QTableWidget
711 {
712 public:
713   Table( QWidget* = 0 );
714   Table( int, int, QWidget* = 0 );
715   virtual ~Table();
716
717   QSize                   minimumSizeHint() const;
718
719   void                    setEditable( bool, int, int );
720   bool                    isEditable( int, int ) const;
721
722   void                    setReadOnly( bool );
723   bool                    isReadOnly() const;
724
725   void                    insertRows( int, int = 1 );
726   QString                 text( int, int );
727
728   QList<int>              selectedRows();
729 };
730
731 //=======================================================================
732 // name    : SMESHGUI_FilterTable::Table::Table
733 // Purpose : Constructor
734 //=======================================================================
735 SMESHGUI_FilterTable::Table::Table (QWidget* parent)
736 : QTableWidget(parent)
737 {
738   // set custom item delegate
739   setItemDelegate( new ComboDelegate(this) );
740   // set edit triggers by default
741   setReadOnly( false );
742 }
743
744 //=======================================================================
745 // name    : SMESHGUI_FilterTable::Table::Table
746 // Purpose : Constructor
747 //=======================================================================
748 SMESHGUI_FilterTable::Table::Table (int numRows, int numCols, QWidget* parent)
749 : QTableWidget(numRows, numCols, parent)
750 {
751   // set custom item delegate
752   setItemDelegate( new ComboDelegate(this) );
753   // set edit triggers by default
754   setReadOnly( false );
755 }
756
757 //=======================================================================
758 // name    : SMESHGUI_FilterTable::Table::~Table
759 // Purpose : Destructor
760 //=======================================================================
761 SMESHGUI_FilterTable::Table::~Table()
762 {
763 }
764
765 //=======================================================================
766 // name    : SMESHGUI_FilterTable::Table::minimumSizeHint
767 // Purpose : Get minimum size for the table
768 //=======================================================================
769 QSize SMESHGUI_FilterTable::Table::minimumSizeHint() const
770 {
771   QSize s = QTableWidget::minimumSizeHint();
772   QHeaderView* hv = horizontalHeader();
773   if ( hv )
774     s.setWidth( qMax( s.width(), hv->length() ) );
775   return s;
776 }
777
778 //=======================================================================
779 // name    : SMESHGUI_FilterTable::Table::setEditable
780 // Purpose : Set editable of specified cell
781 //=======================================================================
782 void SMESHGUI_FilterTable::Table::setEditable (bool isEditable,
783                                                int row, int col)
784 {
785   QTableWidgetItem* anItem = item( row, col );
786   if ( anItem ) {
787     bool isSignalsBlocked = signalsBlocked();
788     blockSignals( true );
789
790     Qt::ItemFlags f = anItem->flags();
791     if ( !isEditable ) f = f & ~Qt::ItemIsEditable;
792     else f = f | Qt::ItemIsEditable;
793     anItem->setFlags( f );
794     
795     blockSignals( isSignalsBlocked );
796   }
797 }
798
799 //=======================================================================
800 // name    : SMESHGUI_FilterTable::Table::isEditable
801 // Purpose : Verify wheter cell is editable
802 //=======================================================================
803 bool SMESHGUI_FilterTable::Table::isEditable (int row, int col) const
804 {
805   QTableWidgetItem* anItem = item( row, col );
806   return anItem == 0 || anItem->flags() & Qt::ItemIsEditable;
807 }
808
809 void SMESHGUI_FilterTable::Table::setReadOnly( bool on )
810 {
811   setEditTriggers( on ? 
812                    QAbstractItemView::NoEditTriggers  :
813                    QAbstractItemView::AllEditTriggers );
814 }
815
816 bool SMESHGUI_FilterTable::Table::isReadOnly() const
817 {
818   return editTriggers() == QAbstractItemView::NoEditTriggers;
819 }
820
821 //=======================================================================
822 // name    : SMESHGUI_FilterTable::Table::insertRows
823 // Purpose : Insert rows (virtual redefined)
824 //=======================================================================
825 void SMESHGUI_FilterTable::Table::insertRows (int row, int count)
826 {
827   closePersistentEditor( currentItem() );
828   while ( count-- ) insertRow( row );
829 }
830
831 //=======================================================================
832 // name    : SMESHGUI_FilterTable::Table::text
833 // Purpose : Get text from cell (virtual redefined)
834 //=======================================================================
835 QString SMESHGUI_FilterTable::Table::text (int row, int col)
836 {
837   closePersistentEditor( currentItem() );
838   QTableWidgetItem* anItem = item( row, col );
839   return anItem ? anItem->text() : QString();
840 }
841
842 QList<int> SMESHGUI_FilterTable::Table::selectedRows()
843 {
844   QList<QTableWidgetItem*> selItems = selectedItems();
845   QTableWidgetItem* anItem;
846   QList<int> rows;
847
848   foreach( anItem, selItems ) {
849     int r = row( anItem );
850     if ( !rows.contains( r ) ) rows.append( r );
851   }
852
853   qSort( rows );
854   return rows;
855 }
856
857 /*
858   Class       : SMESHGUI_FilterTable
859   Description : Frame containig
860                   - Button group for switching entity type
861                   - Table for displaying filter criterions
862                   - Buttons for editing table and filter libraries
863 */
864
865 //=======================================================================
866 // name    : SMESHGUI_FilterTable::SMESHGUI_FilterTable
867 // Purpose : Constructor
868 //=======================================================================
869 SMESHGUI_FilterTable::SMESHGUI_FilterTable( SMESHGUI* theModule,
870                                             QWidget* parent,
871                                             const int type )
872 : QWidget( parent ),
873   myIsLocked( false ),
874   mySMESHGUI( theModule )
875 {
876   myEntityType = -1;
877
878   QList<int> aTypes;
879   aTypes.append(type);
880   Init(aTypes);
881 }
882
883 //=======================================================================
884 // name    : SMESHGUI_FilterTable::SMESHGUI_FilterTable
885 // Purpose : Constructor
886 //=======================================================================
887 SMESHGUI_FilterTable::SMESHGUI_FilterTable( SMESHGUI* theModule,
888                                             QWidget* parent,
889                                             const QList<int>& types )
890 : QWidget( parent ),
891   myIsLocked( false ),
892   mySMESHGUI( theModule )
893 {
894   myEntityType = -1;
895   Init(types);
896 }
897
898 SMESHGUI_FilterTable::~SMESHGUI_FilterTable()
899 {
900 }
901
902 //=======================================================================
903 // name    : SMESHGUI_FilterTable::Init
904 // Purpose : Create table corresponding to the specified type
905 //=======================================================================
906 void SMESHGUI_FilterTable::Init (const QList<int>& theTypes)
907 {
908   if (theTypes.isEmpty())
909     return;
910
911   // Create buttons if necessary
912
913   if (myTables.isEmpty())
914   {
915     // create main layout
916     QVBoxLayout* aMainLay = new QVBoxLayout(this);
917     aMainLay->setMargin( 0 );
918     aMainLay->setSpacing( SPACING );
919
920     // create switch of entity types
921     myEntityTypeBox = new QGroupBox(tr("ENTITY_TYPE"), this);
922     QHBoxLayout* myEntityTypeBoxLayout = new QHBoxLayout(myEntityTypeBox);
923     myEntityTypeBoxLayout->setMargin( MARGIN );
924     myEntityTypeBoxLayout->setSpacing( SPACING );
925     myEntityTypeGrp = new QButtonGroup(this);
926
927     const QMap<int, QString>& aSupportedTypes = getSupportedTypes();
928     QMap<int, QString>::const_iterator anIter;
929     for (anIter = aSupportedTypes.begin(); anIter != aSupportedTypes.end(); ++anIter)
930     {
931       QRadioButton* aBtn = new QRadioButton(anIter.value(), myEntityTypeBox);
932       myEntityTypeGrp->addButton(aBtn, anIter.key());
933       myEntityTypeBoxLayout->addWidget(aBtn);
934     }
935
936     myTableGrp = new QGroupBox(tr("FILTER"), this );
937
938     // create table
939     mySwitchTableGrp = new QWidget(myTableGrp);
940     QVBoxLayout* mySwitchTableGrpLayout = new QVBoxLayout(mySwitchTableGrp);
941     mySwitchTableGrpLayout->setMargin(0);
942     mySwitchTableGrpLayout->setSpacing(0);
943
944     QList<int>::const_iterator typeIt = theTypes.begin();
945     for ( ; typeIt != theTypes.end(); ++typeIt ) {
946       Table* aTable = createTable(mySwitchTableGrp, *typeIt);
947       myTables[ *typeIt ] = aTable;
948       mySwitchTableGrpLayout->addWidget(aTable);
949       if ( typeIt != theTypes.begin() )
950         aTable->hide();
951     }
952
953     // create buttons
954     myAddBtn      = new QPushButton(tr("ADD"),       myTableGrp);
955     myRemoveBtn   = new QPushButton(tr("REMOVE"),    myTableGrp);
956     myClearBtn    = new QPushButton(tr("CLEAR"),     myTableGrp);
957     myInsertBtn   = new QPushButton(tr("INSERT"),    myTableGrp);
958     myCopyFromBtn = new QPushButton(tr("COPY_FROM"), myTableGrp);
959     myAddToBtn    = new QPushButton(tr("ADD_TO"),    myTableGrp);
960
961     myAddBtn->setAutoDefault(false);
962     myRemoveBtn->setAutoDefault(false);
963     myClearBtn->setAutoDefault(false);
964     myInsertBtn->setAutoDefault(false);
965     myCopyFromBtn->setAutoDefault(false);
966     myAddToBtn->setAutoDefault(false);
967
968     myCopyFromBtn->hide();
969     myAddToBtn->hide();
970
971     // layout widgets
972     QGridLayout* aLay = new QGridLayout(myTableGrp);
973     aLay->setMargin(MARGIN);
974     aLay->setSpacing(SPACING);
975
976     aLay->addWidget(mySwitchTableGrp, 0, 0, 7, 1);
977     aLay->addWidget(myAddBtn,         0, 1);
978     aLay->addWidget(myInsertBtn,      1, 1);
979     aLay->addWidget(myRemoveBtn,      2, 1);
980     aLay->addWidget(myClearBtn,       3, 1);
981     aLay->addWidget(myCopyFromBtn,    5, 1);
982     aLay->addWidget(myAddToBtn,       6, 1);
983     aLay->addWidget(createAdditionalFrame(myTableGrp), 7, 0, 1, 2 );
984
985     aLay->setRowMinimumHeight(4, 10);
986     aLay->setRowStretch(4, 1);
987     aLay->setColumnStretch(0, 1);
988     aLay->setColumnStretch(1, 0);
989
990     // layout 
991     aMainLay->addWidget(myEntityTypeBox);
992     aMainLay->addWidget(myTableGrp);
993     
994     // signals and slots
995     connect(myAddBtn,    SIGNAL(clicked()), this, SLOT(onAddBtn()));
996     connect(myInsertBtn, SIGNAL(clicked()), this, SLOT(onInsertBtn()));
997     connect(myRemoveBtn, SIGNAL(clicked()), this, SLOT(onRemoveBtn()));
998     connect(myClearBtn,  SIGNAL(clicked()), this, SLOT(onClearBtn()));
999
1000     connect(myCopyFromBtn, SIGNAL(clicked()), this, SLOT(onCopyFromBtn()));
1001     connect(myAddToBtn,    SIGNAL(clicked()), this, SLOT(onAddToBtn()));
1002
1003     connect(myEntityTypeGrp, SIGNAL(buttonClicked(int)), this, SLOT(onEntityType(int)));
1004
1005     myLibDlg = 0;
1006   }
1007   else
1008   {
1009     QList<int>::const_iterator typeIt = theTypes.begin();
1010     for ( ; typeIt != theTypes.end(); ++typeIt ) {
1011       if ( !myTables[ *typeIt ] ) {
1012         Table* aTable = createTable(mySwitchTableGrp, *typeIt);
1013         myTables[ *typeIt ] = aTable;
1014         ((QVBoxLayout*)mySwitchTableGrp->layout())->addWidget(myTables[ *typeIt ]);
1015       }
1016     }
1017   }
1018
1019   // Hide buttons of entity types if necessary
1020   const QMap<int, QString>& aSupportedTypes = getSupportedTypes();
1021   QMap<int, QString>::const_iterator anIt;
1022   for (anIt = aSupportedTypes.begin(); anIt != aSupportedTypes.end(); ++anIt)
1023   {
1024     QAbstractButton* aBtn = myEntityTypeGrp->button(anIt.key());
1025     if ( aBtn ) aBtn->setVisible( theTypes.contains(anIt.key()) );
1026   }
1027
1028   // select first button if there is no selected buttons or it is hidden
1029   int aBtnId = myEntityTypeGrp->checkedId();
1030   if ( aBtnId == -1 || !theTypes.contains(aBtnId) ) {
1031     QAbstractButton* aBtn = myEntityTypeGrp->button(theTypes.first());
1032     if ( aBtn ) aBtn->setChecked(true);
1033   }
1034
1035   myEntityTypeBox->setVisible(theTypes.count() > 1);
1036
1037   myTableGrp->updateGeometry();
1038   int cType = myEntityTypeGrp->checkedId();
1039   onEntityType(cType);
1040 }
1041
1042 //=======================================================================
1043 // name    : SMESHGUI_FilterTable::createAdditionalFrame
1044 // Purpose : Get group box containing table. May be used for adding new widgets in it
1045 //=======================================================================
1046 QWidget* SMESHGUI_FilterTable::createAdditionalFrame (QWidget* theParent)
1047 {
1048   QWidget* aFrame = new QWidget(theParent);
1049
1050   QFrame* aLine1 = new QFrame(aFrame);
1051   QFrame* aLine2 = new QFrame(aFrame);
1052   aLine1->setFrameStyle(QFrame::HLine | QFrame::Sunken);
1053   aLine2->setFrameStyle(QFrame::HLine | QFrame::Sunken);
1054   aLine1->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
1055   aLine2->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
1056
1057   QLabel* aLabel = new QLabel(tr("ADDITIONAL_PARAMETERS"), aFrame);
1058
1059   myWgStack = new QStackedWidget(aFrame);
1060
1061   QGridLayout* aLay = new QGridLayout(aFrame);
1062   aLay->setMargin(0);
1063   aLay->setSpacing(SPACING);
1064   aLay->addWidget(aLine1,    0, 0);
1065   aLay->addWidget(aLabel,    0, 1);
1066   aLay->addWidget(aLine2,    0, 2);
1067   aLay->addWidget(myWgStack, 1, 0, 1, 3);
1068
1069   return aFrame;
1070 }
1071
1072 //=======================================================================
1073 // name    : SMESHGUI_FilterTable::GetTableGrp
1074 // Purpose : Get group box containing table. May be used for adding new widgets in it
1075 //=======================================================================
1076 QGroupBox* SMESHGUI_FilterTable::GetTableGrp()
1077 {
1078   return myTableGrp;
1079 }
1080
1081 //=======================================================================
1082 // name    : SMESHGUI_FilterTable::onEntityType
1083 // Purpose : SLOT. Called when entity type changed.
1084 //           Display corresponding table
1085 //=======================================================================
1086 void SMESHGUI_FilterTable::onEntityType (int theType)
1087 {
1088   if (myEntityType == theType)
1089     return;
1090
1091   myIsValid = true;
1092   emit NeedValidation();
1093   if (!myIsValid)
1094   {
1095     myEntityTypeGrp->button(myEntityType)->setChecked(true);
1096     return;
1097   }
1098
1099   myEntityType = theType;
1100
1101   if (!myTables.contains(theType)) {
1102     myTables[ theType ] = createTable(mySwitchTableGrp, theType);
1103     ((QVBoxLayout*)mySwitchTableGrp->layout())->addWidget(myTables[ theType ]);
1104   }
1105
1106   TableMap::iterator anIter;
1107   for (anIter = myTables.begin(); anIter != myTables.end(); ++anIter)
1108      anIter.value()->setVisible( myEntityType == anIter.key() );
1109
1110   updateBtnState();
1111   qApp->processEvents();
1112   myTables[ myEntityType ]->updateGeometry();
1113   adjustSize();
1114
1115   emit EntityTypeChanged(theType);
1116 }
1117
1118 //=======================================================================
1119 // name    : SMESHGUI_FilterTable::IsValid
1120 // Purpose : Verify validity of entered data
1121 //=======================================================================
1122 bool SMESHGUI_FilterTable::IsValid (const bool theMess, const int theEntityType) const
1123 {
1124   int aType = theEntityType == -1 ? GetType() : theEntityType;
1125
1126   Table* aTable = myTables[ aType ];
1127   for (int i = 0, n = aTable->rowCount(); i < n; i++)
1128   {
1129     int aCriterion = GetCriterionType(i, aType);
1130     QString errMsg;
1131     if (aCriterion == SMESH::FT_GroupColor )
1132     {
1133       QtxColorButton* clrBtn = qobject_cast<QtxColorButton*>(aTable->cellWidget(i, 2));
1134       if (clrBtn && !clrBtn->color().isValid())
1135         errMsg = tr( "GROUPCOLOR_ERROR" );
1136     }
1137     else if (aCriterion == SMESH::FT_RangeOfIds ||
1138              aCriterion == SMESH::FT_BelongToMeshGroup ||
1139              aCriterion == SMESH::FT_BelongToGeom ||
1140              aCriterion == SMESH::FT_BelongToPlane ||
1141              aCriterion == SMESH::FT_BelongToCylinder ||
1142              aCriterion == SMESH::FT_BelongToGenSurface ||
1143              aCriterion == SMESH::FT_ElemGeomType ||
1144              aCriterion == SMESH::FT_EntityType ||
1145              aCriterion == SMESH::FT_CoplanarFaces ||
1146              aCriterion == SMESH::FT_LyingOnGeom ||
1147              aCriterion == SMESH::FT_ConnectedElements )
1148     {
1149       if (aTable->text(i, 2).isEmpty())
1150         errMsg = tr( "ERROR" );
1151     }
1152     else // check correctness of a numeric value
1153     {
1154       bool aRes = false;
1155       bool isSignalsBlocked = aTable->signalsBlocked();
1156       aTable->blockSignals(true);
1157       double  aThreshold = (int)aTable->text(i, 2).toDouble(&aRes);
1158       aTable->blockSignals(isSignalsBlocked);
1159
1160       if (!aRes && aTable->isEditable(i, 2))
1161         errMsg = tr( "ERROR" );
1162     }
1163
1164     if (!errMsg.isEmpty()) {
1165       if (theMess)
1166         SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"), errMsg );
1167       return false;
1168     }
1169
1170     QTableWidgetItem* anItem = aTable->item(i, 0);
1171     if (myAddWidgets.contains(anItem) && !myAddWidgets[ anItem ]->IsValid())
1172       return false;
1173   }
1174
1175   return true;
1176 }
1177
1178 //=======================================================================
1179 // name    : SMESHGUI_FilterTable::SetValidity
1180 // Purpose : Set validity of the table
1181 //=======================================================================
1182 void SMESHGUI_FilterTable::SetValidity (const bool isValid)
1183 {
1184   myIsValid = isValid;
1185 }
1186
1187 //=======================================================================
1188 // name    : SMESHGUI_FilterTable::GetType
1189 // Purpose : Get current entity type
1190 //=======================================================================
1191 int SMESHGUI_FilterTable::GetType() const
1192 {
1193   return myEntityType;
1194 }
1195
1196 //=======================================================================
1197 // name    : SMESHGUI_FilterTable::SetType
1198 // Purpose : Set current entity type
1199 //=======================================================================
1200 void SMESHGUI_FilterTable::SetType (const int type)
1201 {
1202   myEntityTypeGrp->button(type)->setChecked(true);
1203   onEntityType(type);
1204 }
1205
1206 //=======================================================================
1207 // name    : SMESHGUI_FilterTable::RestorePreviousEntityType
1208 // Purpose : Restore previous entity type
1209 //=======================================================================
1210 void SMESHGUI_FilterTable::RestorePreviousEntityType()
1211 {
1212   SetType(myEntityType);
1213 }
1214
1215 //=======================================================================
1216 // name    : SMESHGUI_FilterTable::GetCriterionType
1217 // Purpose : Get type of criterion from specified row (corresponding enums in h-file)
1218 //=======================================================================
1219 int SMESHGUI_FilterTable::GetCriterionType (const int theRow, const int theType) const
1220 {
1221   int aType = theType == -1 ? GetType() : theType;
1222   Table* aTable = myTables[ aType ];
1223   ComboItem* anItem = (ComboItem*)aTable->item(theRow, 0);
1224   return anItem != 0 ? anItem->value() : SMESH::FT_Undefined;
1225 }
1226
1227 //=======================================================================
1228 // name    : SMESHGUI_FilterTable::GetCriterion
1229 // Purpose : Get parameters of criterion from specified row
1230 //=======================================================================
1231 void SMESHGUI_FilterTable::GetCriterion (const int                 theRow,
1232                                          SMESH::Filter::Criterion& theCriterion,
1233                                          const int                 theEntityType) const
1234 {
1235   int aType = theEntityType == -1 ? GetType() : theEntityType;
1236   Table* aTable = myTables[ aType ];
1237
1238   theCriterion.Type = ((ComboItem*)aTable->item(theRow, 0))->value();
1239   theCriterion.UnaryOp = ((CheckItem*)aTable->item(theRow, 3))->checked() ? SMESH::FT_LogicalNOT : SMESH::FT_Undefined;
1240   theCriterion.BinaryOp = theRow != aTable->rowCount() - 1 ?
1241     ((ComboItem*)aTable->item(theRow, 4))->value() : SMESH::FT_Undefined;
1242   theCriterion.TypeOfElement = (SMESH::ElementType)aType;
1243
1244   int aCriterionType = GetCriterionType(theRow, aType);
1245
1246   if ( aCriterionType == SMESH::FT_GroupColor )
1247   {
1248     QtxColorButton* clrBtn = qobject_cast<QtxColorButton*>(aTable->cellWidget(theRow, 2));
1249     if ( clrBtn )
1250     {
1251       Kernel_Utils::Localizer loc;
1252       const QColor qClr = clrBtn->color();
1253       QString clrStr = QString( "%1;%2;%3" ).
1254         arg( qClr.red()/256. ).arg( qClr.green()/256. ).arg( qClr.blue()/256. );
1255       theCriterion.ThresholdStr = clrStr.toLatin1().constData();
1256     }
1257   }
1258   else if ( aCriterionType == SMESH::FT_ElemGeomType ||
1259             aCriterionType == SMESH::FT_EntityType )
1260   {
1261     theCriterion.Threshold = (double)((ComboItem*)aTable->item(theRow, 2))->value();
1262   }
1263   else if ( aCriterionType == SMESH::FT_CoplanarFaces )
1264   {
1265     theCriterion.ThresholdID = aTable->text(theRow, 2).toLatin1().constData();
1266   }
1267   else if ( aCriterionType == SMESH::FT_ConnectedElements )
1268   {
1269     QString id = aTable->text(theRow, 5);
1270     if ( !id.isEmpty() ) // shape ID
1271     {
1272       theCriterion.ThresholdID = id.toLatin1().constData();
1273     }
1274     else
1275     {
1276       QString text = aTable->text(theRow, 2).trimmed();
1277       QString workText = text;
1278       for ( char c = '0'; c <= '9'; ++c )
1279         workText.remove( c );
1280
1281       if ( workText.isEmpty() ) // node ID
1282         theCriterion.Threshold = text.toDouble();
1283       else // point coordinates
1284         theCriterion.ThresholdStr = text.toLatin1().constData();
1285     }
1286   }
1287   else if ( aCriterionType != SMESH::FT_RangeOfIds &&
1288             aCriterionType != SMESH::FT_BelongToMeshGroup &&
1289             aCriterionType != SMESH::FT_BelongToGeom &&
1290             aCriterionType != SMESH::FT_BelongToPlane &&
1291             aCriterionType != SMESH::FT_BelongToCylinder &&
1292             aCriterionType != SMESH::FT_BelongToGenSurface &&
1293             aCriterionType != SMESH::FT_LyingOnGeom )
1294   {
1295     theCriterion.Compare = ((ComboItem*)aTable->item(theRow, 1))->value();
1296     theCriterion.Threshold = aTable->item(theRow, 2)->text().toDouble();
1297   }
1298   else
1299   {
1300     theCriterion.ThresholdStr = aTable->text(theRow, 2).toLatin1().constData();
1301     if ( aCriterionType != SMESH::FT_RangeOfIds )
1302       theCriterion.ThresholdID = aTable->text( theRow, 5 ).toLatin1().constData();
1303   }
1304
1305   QTableWidgetItem* anItem = aTable->item(theRow, 0);
1306   if (myAddWidgets.contains(anItem))
1307     theCriterion.Tolerance = myAddWidgets[ anItem ]->GetDouble(AdditionalWidget::Tolerance);
1308 }
1309
1310 //=======================================================================
1311 // name    : SMESHGUI_FilterTable::SetCriterion
1312 // Purpose : Set parameters of criterion of specified row
1313 //=======================================================================
1314 void SMESHGUI_FilterTable::SetCriterion (const int                       theRow,
1315                                          const SMESH::Filter::Criterion& theCriterion,
1316                                          const int                       theEntityType)
1317 {
1318   int aType = theEntityType == -1 ? GetType() : theEntityType;
1319
1320   Table* aTable = myTables[ aType ];
1321
1322   if (theRow > aTable->rowCount() - 1)
1323     return;
1324
1325   ((ComboItem*)aTable->item(theRow, 0))->setValue(theCriterion.Type);
1326   onCriterionChanged(theRow, 0, aType);
1327   if ( theCriterion.Compare == SMESH::FT_Undefined )
1328     ((ComboItem*)aTable->item(theRow, 1))->setValue( SMESH::FT_EqualTo );
1329   else
1330     ((ComboItem*)aTable->item(theRow, 1))->setValue(theCriterion.Compare);
1331   ((CheckItem*)aTable->item(theRow, 3))->setChecked(theCriterion.UnaryOp == SMESH::FT_LogicalNOT);
1332
1333   if (theCriterion.BinaryOp != SMESH::FT_Undefined)
1334   {
1335     if (!aTable->isEditable(theRow, 4))
1336       aTable->setItem(theRow, 4, getBinaryItem());
1337     ((ComboItem*)aTable->item(theRow, 4))->setValue(theCriterion.BinaryOp);
1338   }
1339   else
1340     aTable->setEditable(false, theRow, 4);
1341
1342   if (theCriterion.Type == SMESH::FT_GroupColor )
1343   {
1344     QtxColorButton* clrBtn = qobject_cast<QtxColorButton*>(aTable->cellWidget(theRow, 2));
1345     if ( clrBtn )
1346     {
1347       QColor qClr;
1348       QString clrStr( theCriterion.ThresholdStr );
1349       QStringList clrVals = clrStr.split( ";" );
1350       if ( clrVals.count() > 2 )
1351         qClr.setRgb( (int)256*clrVals[0].toDouble(),
1352                      (int)256*clrVals[1].toDouble(),
1353                      (int)256*clrVals[2].toDouble() );
1354       clrBtn->setColor( qClr );
1355     }
1356   }
1357   else if (theCriterion.Type == SMESH::FT_ElemGeomType || 
1358            theCriterion.Type == SMESH::FT_EntityType )
1359   {
1360     ComboItem* typeBox = (ComboItem*)aTable->item(theRow, 2);
1361     typeBox->setValue( (int)(theCriterion.Threshold + 0.5) );
1362   }
1363   else if (theCriterion.Type == SMESH::FT_CoplanarFaces )
1364   {
1365     aTable->item( theRow, 2 )->setText( QString( theCriterion.ThresholdID ) );
1366   }
1367   else if (theCriterion.Type == SMESH::FT_ConnectedElements )
1368   {
1369     if ( strlen( theCriterion.ThresholdID ) > 0 ) // shape ID -> name
1370     {
1371       _PTR(SObject) sobj =
1372         SMESH::GetActiveStudyDocument()->FindObjectID( theCriterion.ThresholdID.in() );
1373       if ( !sobj )
1374         aTable->item( theRow, 2 )->setText( QString( theCriterion.ThresholdID ) );
1375       else
1376         aTable->item( theRow, 2 )->setText( QString( sobj->GetName().c_str() ));
1377     }
1378     else if ( strlen( theCriterion.ThresholdStr ) > 0 ) // point coords
1379     {
1380       aTable->item( theRow, 2 )->setText( QString( theCriterion.ThresholdStr ));
1381     }
1382     else // node ID
1383     {
1384       aTable->item( theRow, 2 )->setText( QString("%1").arg((int) theCriterion.Threshold ));
1385     }
1386   }
1387   else if (theCriterion.Type != SMESH::FT_RangeOfIds &&
1388            theCriterion.Type != SMESH::FT_BelongToMeshGroup &&
1389            theCriterion.Type != SMESH::FT_BelongToGeom &&
1390            theCriterion.Type != SMESH::FT_BelongToPlane &&
1391            theCriterion.Type != SMESH::FT_BelongToCylinder &&
1392            theCriterion.Type != SMESH::FT_BelongToGenSurface &&
1393            theCriterion.Type != SMESH::FT_LyingOnGeom &&
1394            theCriterion.Type != SMESH::FT_FreeBorders &&
1395            theCriterion.Type != SMESH::FT_FreeEdges &&
1396            theCriterion.Type != SMESH::FT_FreeNodes &&
1397            theCriterion.Type != SMESH::FT_FreeFaces &&
1398            theCriterion.Type != SMESH::FT_BadOrientedVolume &&
1399            theCriterion.Type != SMESH::FT_BareBorderFace &&
1400            theCriterion.Type != SMESH::FT_BareBorderVolume &&
1401            theCriterion.Type != SMESH::FT_OverConstrainedFace &&
1402            theCriterion.Type != SMESH::FT_OverConstrainedVolume &&
1403            theCriterion.Type != SMESH::FT_LinearOrQuadratic)
1404   {
1405     // Numberic criterion
1406     aTable->item( theRow, 2 )->setText(QString("%1").arg(theCriterion.Threshold, 0, 'g', 15));
1407   }
1408   else
1409   {
1410     aTable->item( theRow, 2 )->setText(QString(theCriterion.ThresholdStr));
1411     if ( theCriterion.Type != SMESH::FT_RangeOfIds )
1412       aTable->item( theRow, 5 )->setText( QString( theCriterion.ThresholdID ) );
1413   }
1414
1415   if (theCriterion.Compare == SMESH::FT_EqualTo ||
1416       theCriterion.Type    == SMESH::FT_BelongToPlane ||
1417       theCriterion.Type    == SMESH::FT_BelongToCylinder ||
1418       theCriterion.Type    == SMESH::FT_BelongToGenSurface ||
1419       theCriterion.Type    == SMESH::FT_BelongToGeom ||
1420       theCriterion.Type    == SMESH::FT_LyingOnGeom ||
1421       theCriterion.Type    == SMESH::FT_CoplanarFaces ||
1422       theCriterion.Type    == SMESH::FT_EqualNodes)
1423   {
1424     QTableWidgetItem* anItem = aTable->item(theRow, 0);
1425     if (!myAddWidgets.contains(anItem))
1426     {
1427       myAddWidgets[ anItem ] = new AdditionalWidget(myWgStack);
1428       myAddWidgets[ anItem ]->SetPrecision( AdditionalWidget::Tolerance, getPrecision( theCriterion.Type ) );
1429       myWgStack->addWidget(myAddWidgets[ anItem ]);
1430     }
1431     myAddWidgets[ anItem ]->SetDouble(AdditionalWidget::Tolerance, theCriterion.Tolerance);
1432   }
1433
1434   emit CriterionChanged(theRow, aType);
1435 }
1436
1437 //=======================================================================
1438 // name    : SMESHGUI_FilterTable::Update
1439 // Purpose : Update table
1440 //=======================================================================
1441 void SMESHGUI_FilterTable::Update()
1442 {
1443   Table* aTable = myTables[ GetType() ];
1444   int aCurrRow = aTable->currentRow();
1445   int numRows = aTable->rowCount();
1446   if ((aCurrRow < 0 || aCurrRow >= numRows) && numRows > 0)
1447     aTable->setCurrentCell(0, 0);
1448   updateAdditionalWidget();
1449 }
1450
1451 //=======================================================================
1452 // name    : SMESHGUI_FilterTable::AddCriterion
1453 // Purpose : Add criterion with parameters
1454 //=======================================================================
1455 void SMESHGUI_FilterTable::AddCriterion (const SMESH::Filter::Criterion& theCriterion,
1456                                          const int                       theEntityType)
1457 {
1458   int aType = theEntityType == -1 ? GetType() : theEntityType;
1459   Table* aTable = myTables[ aType ];
1460   addRow(aTable, aType);
1461   SetCriterion(aTable->rowCount() - 1, theCriterion);
1462 }
1463
1464 //=======================================================================
1465 // name    : SMESHGUI_FilterTable::NumRows
1466 // Purpose : Get number of criterions of current type
1467 //=======================================================================
1468 int SMESHGUI_FilterTable::NumRows (const int theEntityType) const
1469 {
1470   return myTables[ theEntityType == -1 ? GetType() : theEntityType ]->rowCount();
1471 }
1472
1473 //=======================================================================
1474 // name    : SMESHGUI_FilterTable::Clear
1475 // Purpose : Clear current table
1476 //=======================================================================
1477 void SMESHGUI_FilterTable::Clear (const int theType)
1478 {
1479   int aType = theType == -1 ? GetType() : theType;
1480   Table* aTable = myTables[ aType ];
1481
1482   if (aTable->rowCount() == 0)
1483     return;
1484
1485   while (aTable->rowCount() > 0)
1486   {
1487     removeAdditionalWidget(aTable, 0);
1488     aTable->removeRow(0);
1489   }
1490
1491   updateBtnState();
1492 }
1493
1494 //=======================================================================
1495 // name    : SMESHGUI_FilterTable::onAddBtn
1496 // Purpose : SLOT. Called then "Add" button pressed.
1497 //           Adds new string to table
1498 //=======================================================================
1499 void SMESHGUI_FilterTable::onAddBtn()
1500 {
1501   int aType = GetType();
1502   addRow(myTables[ aType ], aType);
1503
1504   Update();
1505 }
1506
1507 //=======================================================================
1508 // name    : SMESHGUI_FilterTable::onInsertBtn
1509 // Purpose : SLOT. Called then "Insert" button pressed.
1510 //           Inserts new string before current one
1511 //=======================================================================
1512 void SMESHGUI_FilterTable::onInsertBtn()
1513 {
1514   addRow(myTables[ GetType() ], GetType(), false);
1515 }
1516
1517 //=======================================================================
1518 // name    : SMESHGUI_FilterTable::onRemoveBtn
1519 // Purpose : SLOT. Called then "Remove" button pressed.
1520 //           Removes current string from table
1521 //=======================================================================
1522 void SMESHGUI_FilterTable::onRemoveBtn()
1523 {
1524   Table* aTable = myTables[ GetType() ];
1525
1526   if (aTable->rowCount() == 0)
1527     return;
1528
1529   QList<QTableWidgetItem*> items = aTable->selectedItems();
1530   
1531   QList<int> aRows = aTable->selectedRows(); // already sorted
1532   int i;
1533   foreach( i, aRows )
1534   {
1535     removeAdditionalWidget(aTable, i);
1536     aTable->removeRow(i);
1537   }
1538
1539   // remove control of binary logical operation from last row
1540   if (aTable->rowCount() > 0)
1541     aTable->setEditable(false, aTable->rowCount() - 1, 4);
1542
1543   updateBtnState();
1544 }
1545
1546 //=======================================================================
1547 // name    : SMESHGUI_FilterTable::updateAdditionalWidget
1548 // Purpose : Enable/Disable widget with additonal parameters
1549 //=======================================================================
1550 void SMESHGUI_FilterTable::updateAdditionalWidget()
1551 {
1552   Table* aTable = myTables[ GetType() ];
1553   int aRow = aTable->currentRow();
1554   if (aRow < 0 || aRow >= aTable->rowCount())
1555   {
1556     myWgStack->setEnabled(false);
1557     return;
1558   }
1559
1560   ComboItem* anItem = ((ComboItem*)aTable->item(aRow, 0));
1561   int aCriterion = GetCriterionType(aRow);
1562   bool toEnable = ((((ComboItem*)aTable->item(aRow, 1))->value() == SMESH::FT_EqualTo &&
1563                    aCriterion != SMESH::FT_RangeOfIds &&
1564                    aCriterion != SMESH::FT_FreeEdges &&
1565                    aCriterion != SMESH::FT_FreeFaces &&
1566                    aCriterion != SMESH::FT_BadOrientedVolume &&
1567                    aCriterion != SMESH::FT_BareBorderFace &&
1568                    aCriterion != SMESH::FT_BareBorderVolume &&
1569                    aCriterion != SMESH::FT_OverConstrainedFace &&
1570                    aCriterion != SMESH::FT_OverConstrainedVolume)
1571                    ||
1572                    aCriterion == SMESH::FT_CoplanarFaces ||
1573                    aCriterion == SMESH::FT_EqualNodes);
1574   
1575   if (!myAddWidgets.contains(anItem))
1576   {
1577     myAddWidgets[ anItem ] = new AdditionalWidget(myWgStack);
1578     myWgStack->addWidget(myAddWidgets[ anItem ]);
1579   }
1580
1581   myWgStack->setCurrentWidget(myAddWidgets[ anItem ]);
1582   myAddWidgets[ anItem ]->SetPrecision( AdditionalWidget::Tolerance, getPrecision( aCriterion ) );
1583   myWgStack->setEnabled(toEnable);
1584 }
1585
1586 const char* SMESHGUI_FilterTable::getPrecision( const int aType )
1587 {
1588   const char* retval = 0;
1589   switch ( aType ) {
1590   case SMESH::FT_AspectRatio:
1591   case SMESH::FT_AspectRatio3D:
1592   case SMESH::FT_Taper:
1593     retval = "parametric_precision"; break;
1594   case SMESH::FT_Warping:
1595   case SMESH::FT_MinimumAngle:
1596   case SMESH::FT_Skew:
1597   case SMESH::FT_CoplanarFaces:
1598     retval = "angle_precision"; break;
1599   case SMESH::FT_Area:
1600     retval = "area_precision"; break;
1601   case SMESH::FT_BelongToGeom:
1602   case SMESH::FT_BelongToPlane:
1603   case SMESH::FT_BelongToCylinder:
1604   case SMESH::FT_BelongToGenSurface:
1605   case SMESH::FT_LyingOnGeom:
1606   case SMESH::FT_EqualNodes:
1607     retval = "len_tol_precision"; break;
1608   case SMESH::FT_Length:
1609   case SMESH::FT_Length2D:
1610   case SMESH::FT_MaxElementLength2D:
1611   case SMESH::FT_MaxElementLength3D:
1612   case SMESH::FT_BallDiameter:
1613     retval = "length_precision"; break;
1614   case SMESH::FT_Volume3D:
1615     retval = "vol_precision"; break;
1616   default:
1617     break;
1618   }
1619   return retval;
1620 }
1621
1622 //=======================================================================
1623 // name    : SMESHGUI_FilterTable::removeAdditionalWidget
1624 // Purpose : Remove widgets containing additional parameters from widget
1625 //           stack and internal map
1626 //=======================================================================
1627 void SMESHGUI_FilterTable::removeAdditionalWidget (QTableWidget* theTable, const int theRow)
1628 {
1629   QTableWidgetItem* anItem = theTable->item(theRow, 0);
1630   if (myAddWidgets.contains(anItem))
1631   {
1632     myWgStack->removeWidget(myAddWidgets[ anItem ]);
1633     myAddWidgets[ anItem ]->setParent(0);
1634     delete myAddWidgets[ anItem ];
1635     myAddWidgets.remove(anItem);
1636   }
1637 }
1638
1639 //=======================================================================
1640 // name    : SMESHGUI_FilterTable::onClearBtn
1641 // Purpose : SLOT. Called then "Clear" button pressed.
1642 //           Removes all strings from table
1643 //=======================================================================
1644 void SMESHGUI_FilterTable::onClearBtn()
1645 {
1646   Table* aTable = myTables[ GetType() ];
1647
1648   if (aTable->rowCount() == 0)
1649     return;
1650
1651   while (aTable->rowCount() > 0)
1652   {
1653     removeAdditionalWidget(aTable, 0);
1654     aTable->removeRow(0);
1655   }
1656
1657   updateBtnState();
1658 }
1659
1660 //=======================================================================
1661 // name    : SMESHGUI_FilterTable::onCurrentChanged()
1662 // Purpose : SLOT. Called when current cell changed
1663 //=======================================================================
1664 void SMESHGUI_FilterTable::onCurrentChanged (int theRow, int theCol)
1665 {
1666   if( !myIsLocked )
1667     updateAdditionalWidget();
1668   emit CurrentChanged(theRow, theCol);
1669 }
1670
1671 //=======================================================================
1672 // name    : geomTypes
1673 // Purpose : returns available geometry types of elements
1674 //=======================================================================
1675 static QList<int> geomTypes( const int theType )
1676 {
1677   QList<int> typeIds;
1678   if ( theType == SMESH::NODE )
1679     typeIds.append( SMESH::Geom_POINT );
1680   if ( theType == SMESH::ALL || theType == SMESH::EDGE )
1681     typeIds.append( SMESH::Geom_EDGE );
1682   if ( theType == SMESH::ALL || theType == SMESH::FACE )
1683   {
1684     typeIds.append( SMESH::Geom_TRIANGLE );
1685     typeIds.append( SMESH::Geom_QUADRANGLE );
1686     typeIds.append( SMESH::Geom_POLYGON );
1687   }
1688   if ( theType == SMESH::ALL || theType == SMESH::VOLUME )
1689   {
1690     typeIds.append( SMESH::Geom_TETRA );
1691     typeIds.append( SMESH::Geom_PYRAMID );
1692     typeIds.append( SMESH::Geom_HEXA );
1693     typeIds.append( SMESH::Geom_PENTA );
1694     typeIds.append( SMESH::Geom_HEXAGONAL_PRISM );
1695     typeIds.append( SMESH::Geom_POLYHEDRA );
1696   }
1697   if ( theType == SMESH::ALL || theType == SMESH::ELEM0D )
1698   {
1699     typeIds.append( SMESH::Geom_POINT );
1700   }
1701   if ( theType == SMESH::ALL || theType == SMESH::BALL )
1702   {
1703     typeIds.append( SMESH::Geom_BALL );
1704   }
1705   return typeIds;
1706 }
1707
1708 //=======================================================================
1709 // name    : entityTypes
1710 // Purpose : returns available entity types of elements
1711 //=======================================================================
1712
1713 static QList<int> entityTypes( const int theType )
1714 {
1715   QList<int> typeIds;
1716
1717   switch ( theType )
1718   {
1719   case SMESH::NODE:
1720     typeIds.append( SMDSEntity_Node );
1721   case SMESH::EDGE:
1722     typeIds.append( SMDSEntity_Edge );
1723     typeIds.append( SMDSEntity_Quad_Edge );
1724     break;
1725   case SMESH::FACE:
1726     typeIds.append( SMDSEntity_Triangle );
1727     typeIds.append( SMDSEntity_Quad_Triangle );
1728     typeIds.append( SMDSEntity_BiQuad_Triangle );
1729     typeIds.append( SMDSEntity_Quadrangle );
1730     typeIds.append( SMDSEntity_Quad_Quadrangle );
1731     typeIds.append( SMDSEntity_BiQuad_Quadrangle );
1732     typeIds.append( SMDSEntity_Polygon );
1733     //typeIds.append( SMDSEntity_Quad_Polygon );
1734     break;
1735   case SMESH::VOLUME:
1736     typeIds.append( SMDSEntity_Tetra );
1737     typeIds.append( SMDSEntity_Quad_Tetra );
1738     typeIds.append( SMDSEntity_Pyramid );
1739     typeIds.append( SMDSEntity_Quad_Pyramid );
1740     typeIds.append( SMDSEntity_Hexa );
1741     typeIds.append( SMDSEntity_Quad_Hexa );
1742     typeIds.append( SMDSEntity_TriQuad_Hexa );
1743     typeIds.append( SMDSEntity_Penta );
1744     typeIds.append( SMDSEntity_Quad_Penta );
1745     typeIds.append( SMDSEntity_Hexagonal_Prism );
1746     typeIds.append( SMDSEntity_Polyhedra );
1747     //typeIds.append( SMDSEntity_Quad_Polyhedra );
1748     break;
1749   case SMESH::ELEM0D:
1750     typeIds.append( SMDSEntity_0D );
1751     break;
1752   case SMESH::BALL:
1753     typeIds.append( SMDSEntity_Ball );
1754     break;
1755   }
1756   return typeIds;
1757 }
1758
1759 //=======================================================================
1760 // name    : SMESHGUI_FilterTable::onCriterionChanged()
1761 // Purpose : Provides reaction on change of criterion
1762 //=======================================================================
1763
1764 void SMESHGUI_FilterTable::onCriterionChanged (const int row, const int col, const int entityType)
1765 {
1766   int aType = entityType == -1 ? GetType() : entityType;
1767   Table* aTable = myTables[ aType ];
1768   ComboItem* aCompareItem = (ComboItem*)aTable->item(row, 1);
1769
1770   // find out type of a existing threshould table item
1771   QtxColorButton* clrBtn = qobject_cast<QtxColorButton*>(aTable->cellWidget(row, 2));
1772   bool isComboItem       = false;
1773   bool isIntSpinItem     = false;
1774   bool isDoubleSpinItem  = false;
1775   if ( QTableWidgetItem* aTableItem = aTable->item(row, 2) )
1776   {
1777     int aTableType   = aTableItem->type();
1778     isComboItem      = ( aTableType == ComboItem::Type() );
1779     isIntSpinItem    = ( aTableType == IntSpinItem::Type() );
1780     isDoubleSpinItem = ( aTableType == DoubleSpinItem::Type() );
1781   }
1782
1783   // find out a type of item required by a new criterion and other table features
1784   int aCriterionType       = GetCriterionType(row);
1785   bool anIsDoubleCriterion = false;
1786   bool anIsIntCriterion    = false;
1787   bool anIsComboCriterion  = false;
1788   // other features:
1789   QList<int> comboIDs; // values to show in a combo item
1790   int nbCompareSigns = 0; // possible values are 0,1,3
1791   bool isThresholdEditable = false; // actual for "simple" item types
1792   switch ( aCriterionType )
1793   {
1794   case SMESH::FT_AspectRatio:
1795   case SMESH::FT_AspectRatio3D:
1796   case SMESH::FT_Warping:
1797   case SMESH::FT_MinimumAngle:
1798   case SMESH::FT_Taper:
1799   case SMESH::FT_Skew:
1800   case SMESH::FT_Area:
1801   case SMESH::FT_Volume3D:
1802   case SMESH::FT_MaxElementLength2D:
1803   case SMESH::FT_MaxElementLength3D:
1804     anIsDoubleCriterion = true; break;
1805
1806   case SMESH::FT_FreeBorders:
1807   case SMESH::FT_FreeEdges:
1808   case SMESH::FT_FreeNodes:
1809   case SMESH::FT_FreeFaces:
1810   case SMESH::FT_EqualNodes:
1811   case SMESH::FT_EqualEdges:
1812   case SMESH::FT_EqualFaces:
1813   case SMESH::FT_EqualVolumes: break;
1814
1815   case SMESH::FT_MultiConnection:
1816   case SMESH::FT_MultiConnection2D: anIsIntCriterion = true; nbCompareSigns = 3; break;
1817
1818   case SMESH::FT_Length:
1819   case SMESH::FT_Length2D: anIsDoubleCriterion = true; break;
1820
1821   case SMESH::FT_BelongToMeshGroup: break;
1822
1823   case SMESH::FT_BelongToGeom:
1824   case SMESH::FT_BelongToPlane:
1825   case SMESH::FT_BelongToCylinder:
1826   case SMESH::FT_BelongToGenSurface:
1827   case SMESH::FT_LyingOnGeom: nbCompareSigns = 1; isThresholdEditable = true; break;
1828
1829   case SMESH::FT_RangeOfIds: nbCompareSigns = 1; isThresholdEditable = true; break;
1830
1831   case SMESH::FT_BadOrientedVolume:
1832   case SMESH::FT_BareBorderVolume:
1833   case SMESH::FT_BareBorderFace:
1834   case SMESH::FT_OverConstrainedVolume:
1835   case SMESH::FT_OverConstrainedFace:
1836   case SMESH::FT_LinearOrQuadratic: break;
1837
1838   case SMESH::FT_GroupColor: nbCompareSigns = 1; isThresholdEditable = true; break;
1839
1840   case SMESH::FT_ElemGeomType:
1841     comboIDs = geomTypes( aType ); anIsComboCriterion = true; nbCompareSigns = 1; break;
1842
1843   case SMESH::FT_EntityType:
1844     comboIDs = entityTypes( aType ); anIsComboCriterion = true; nbCompareSigns = 1; break;
1845
1846   case SMESH::FT_CoplanarFaces: isThresholdEditable = true; break;
1847
1848   case SMESH::FT_BallDiameter: anIsDoubleCriterion = true; break;
1849
1850   case SMESH::FT_ConnectedElements: isThresholdEditable = true; break;
1851
1852   case SMESH::FT_LessThan:
1853   case SMESH::FT_MoreThan:
1854   case SMESH::FT_EqualTo:
1855   case SMESH::FT_LogicalNOT:
1856   case SMESH::FT_LogicalAND:
1857   case SMESH::FT_LogicalOR:
1858   case SMESH::FT_Undefined:
1859   default: return;
1860   }
1861
1862   // get a precision of a double criterion
1863   int aPrecision = 0;
1864   if ( anIsDoubleCriterion ) {
1865     const char*     aPrecisionType = getPrecision( aCriterionType );
1866     SUIT_ResourceMgr* aResourceMgr = SMESH::GetResourceMgr( mySMESHGUI );
1867     if( aPrecisionType && aResourceMgr )
1868       aPrecision = aResourceMgr->integerValue( "SMESH", aPrecisionType, aPrecision );
1869   }
1870
1871   // check if the current item type satisfies the criterion
1872   bool itemTypeKO =
1873     ( aCriterionType == SMESH::FT_GroupColor && !clrBtn) ||
1874     ( anIsComboCriterion                     && !isComboItem ) ||
1875     ( anIsIntCriterion                       && !isIntSpinItem ) ||
1876     ( anIsDoubleCriterion                    && !isDoubleSpinItem );
1877
1878   if ( !itemTypeKO )
1879   {
1880     if ( anIsDoubleCriterion )
1881     {
1882       // if the precision is to be changed we should remove the existing
1883       // spin item and create another one with new precision
1884       if ( DoubleSpinItem* aDoubleSpinItem = dynamic_cast<DoubleSpinItem*>( aTable->item( row, 2 )))
1885         itemTypeKO = ( aDoubleSpinItem->precision() != aPrecision );
1886       else
1887         itemTypeKO = true;
1888     }
1889     else if ( anIsComboCriterion )
1890     {
1891       if ( ComboItem* aComboItem = dynamic_cast<ComboItem*>( aTable->item( row, 2 )))
1892         itemTypeKO = ( aComboItem->count() != comboIDs.count() );
1893       else
1894         itemTypeKO = true;
1895     }
1896     else if ( !anIsIntCriterion && aCriterionType != SMESH::FT_GroupColor )
1897     {
1898       itemTypeKO = ( clrBtn || isComboItem || isIntSpinItem || isDoubleSpinItem );
1899     }
1900   }
1901
1902   // update the table row
1903
1904   bool isSignalsBlocked = aTable->signalsBlocked();
1905   aTable->blockSignals( true );
1906
1907   // update threshold table item
1908   if ( itemTypeKO )
1909   {
1910     aTable->removeCellWidget( row, 2 );
1911
1912     if ( aCriterionType == SMESH::FT_GroupColor ) // ---------------------- QtxColorButton
1913     {
1914       aTable->setCellWidget( row, 2, new QtxColorButton( aTable ) );
1915     }
1916     else if ( anIsComboCriterion ) // -------------------------------------------ComboItem
1917     {
1918       QString msgPrefix
1919         ( aCriterionType == SMESH::FT_ElemGeomType  ? "GEOM_TYPE_%1" :  "ENTITY_TYPE_%1" );
1920       QMap<int, QString> names;
1921       QList<int>::const_iterator id = comboIDs.begin();
1922       for ( ; id != comboIDs.end(); ++id )
1923       {
1924         QString name = msgPrefix.arg( *id );
1925         names[ *id ] = tr( name.toLatin1().data() );
1926       }
1927       ComboItem* comboBox = new ComboItem( names );
1928       aTable->setItem( row, 2, comboBox );
1929     }
1930     else if ( anIsIntCriterion ) // ------------------------------------------ IntSpinItem
1931     {
1932       IntSpinItem* intSpin = new IntSpinItem( 0 );
1933       aTable->setItem( row, 2, intSpin );
1934     }
1935     else if ( anIsDoubleCriterion ) // -------------------------------------DoubleSpinItem
1936     {
1937       DoubleSpinItem* dblSpin = new DoubleSpinItem( 0 );
1938       dblSpin->setPrecision( aPrecision );
1939       aTable->setItem( row, 2, dblSpin );
1940     }
1941     else // --------------------------------------------------------------QTableWidgetItem
1942     {
1943       aTable->setItem( row, 2, new QTableWidgetItem() );
1944     }
1945   }
1946
1947   // set Compare
1948   if ( anIsDoubleCriterion )
1949     nbCompareSigns = 3;
1950   if ( aCompareItem->count() != nbCompareSigns )
1951   {
1952     switch ( nbCompareSigns ) {
1953     case 0: {
1954       aCompareItem->clear();
1955       break;
1956     }
1957     case 1: {
1958       QMap<int, QString> aMap;
1959       aMap[ SMESH::FT_EqualTo ] = tr("EQUAL_TO");
1960       aCompareItem->setItems(aMap);
1961       break;
1962     }
1963     case 3: {
1964       int oldValue = aCompareItem->value();
1965       aCompareItem->setItems(getCompare());
1966       if ( oldValue >= 0 )
1967         aCompareItem->setValue( oldValue );
1968       break;
1969     }
1970     }
1971   }
1972   aTable->setEditable( nbCompareSigns == 3, row, 1);
1973
1974   // enable/desable Threshold
1975   if ( aCriterionType == SMESH::FT_GroupColor ||
1976        anIsComboCriterion ||
1977        anIsIntCriterion ||
1978        anIsDoubleCriterion )
1979   {
1980     isThresholdEditable = true;
1981   }
1982   if ( !isThresholdEditable )
1983   {
1984     aTable->setItem( row, 2, new QTableWidgetItem() );
1985   }
1986   aTable->setEditable( isThresholdEditable, row, 2);
1987
1988
1989   aTable->blockSignals( isSignalsBlocked );
1990
1991   updateAdditionalWidget();
1992
1993   emit CriterionChanged(row, entityType);
1994 }
1995
1996 //=======================================================================
1997 // name    : SMESHGUI_FilterTable::onCriterionChanged()
1998 // Purpose : SLOT. Called then contents of table changed
1999 //           Provides reaction on change of criterion
2000 //=======================================================================
2001 void SMESHGUI_FilterTable::onCriterionChanged (int row, int col)
2002 {
2003   if( col == 0 )
2004     onCriterionChanged(row, col, -1);
2005   else if ( col == 2 )
2006     emit ThresholdChanged(row, GetType());
2007 }
2008
2009 //=======================================================================
2010 // name    : SMESHGUI_FilterTable::getFirstSelectedRow
2011 // Purpose : Get first selected row
2012 //=======================================================================
2013 int SMESHGUI_FilterTable::getFirstSelectedRow() const
2014 {
2015   Table* aTable = myTables[ GetType() ];
2016
2017   QList<int> selRows = aTable->selectedRows(); // already sorted
2018   int aRow = selRows.count() > 0 ? selRows[0] : aTable->currentRow();
2019
2020   return aRow >= 0 && aRow < aTable->rowCount() ? aRow : -1;
2021 }
2022
2023 //=======================================================================
2024 // name    : SMESHGUI_FilterTable::addRow
2025 // Purpose : Add row at the end of table
2026 //=======================================================================
2027 void SMESHGUI_FilterTable::addRow (Table* theTable, const int theType, const bool toTheEnd)
2028 {
2029   int aCurrRow = 0;
2030   int aSelectedRow = getFirstSelectedRow();
2031   int aCurrCol = theTable->currentColumn();
2032
2033   myIsLocked = true;
2034   if (toTheEnd || aSelectedRow == -1)
2035   {
2036     theTable->insertRows(theTable->rowCount());
2037     aCurrRow = theTable->rowCount() - 1;
2038   }
2039   else
2040   {
2041     theTable->insertRows(aSelectedRow);
2042     aCurrRow = aSelectedRow;
2043   }
2044   myIsLocked = false;
2045
2046   // IPAL19372 - to prevent calling onCriterionChaged() slot before completion of setItem()
2047   bool isSignalsBlocked = theTable->signalsBlocked();
2048   theTable->blockSignals( true );
2049
2050   // Criteria
2051   theTable->setItem(aCurrRow, 0, getCriterionItem(theType));
2052
2053   // Compare
2054   theTable->setItem(aCurrRow, 1, getCompareItem());
2055
2056   // Threshold
2057   theTable->setItem(aCurrRow, 2, new QTableWidgetItem());
2058
2059   // Logical operation NOT
2060   theTable->setItem(aCurrRow, 3, getUnaryItem());
2061
2062   // Logical operation AND / OR
2063   theTable->setItem(aCurrRow, 4, new QTableWidgetItem());
2064
2065   theTable->setItem(aCurrRow, 5, new QTableWidgetItem());
2066     
2067   theTable->blockSignals( isSignalsBlocked );
2068
2069   // Logical binary operation for previous value
2070   int anAddBinOpStr = -1;
2071   if (aCurrRow == theTable->rowCount() - 1)
2072     anAddBinOpStr = aCurrRow - 1;
2073   else if (aCurrRow >= 0 )
2074     anAddBinOpStr = aCurrRow;
2075
2076   if (theTable->item(aCurrRow, 4) == 0 ||
2077        theTable->item(aCurrRow, 4)->type() != ComboItem::Type())
2078   {
2079     if (anAddBinOpStr >= 0 &&
2080          (theTable->item(anAddBinOpStr, 4) == 0 ||
2081            theTable->item(anAddBinOpStr, 4)->type() != ComboItem::Type()))
2082       theTable->setItem(anAddBinOpStr, 4, getBinaryItem());
2083   }
2084
2085   theTable->setEditable(false, theTable->rowCount() - 1, 4);
2086   
2087   if (aCurrRow >=0 && aCurrRow < theTable->rowCount() &&
2088        aCurrCol >=0 && aCurrCol < theTable->rowCount())
2089   theTable->setCurrentCell(aCurrRow, aCurrCol);
2090
2091   onCriterionChanged(aCurrRow, 0);
2092
2093   updateBtnState();
2094 }
2095
2096 //=======================================================================
2097 // name    : SMESHGUI_FilterTable::getCriterionItem
2098 // Purpose : Get combo table item for criteria of specified type
2099 //=======================================================================
2100 QTableWidgetItem* SMESHGUI_FilterTable::getCriterionItem (const int theType) const
2101 {
2102   return new ComboItem(getCriteria(theType));
2103 }
2104
2105 //=======================================================================
2106 // name    : SMESHGUI_FilterTable::getCompareItem
2107 // Purpose : Get combo table item for operation of comparision
2108 //=======================================================================
2109 QTableWidgetItem* SMESHGUI_FilterTable::getCompareItem () const
2110 {
2111   return new ComboItem(getCompare());
2112 }
2113
2114 //=======================================================================
2115 // name    : SMESHGUI_FilterTable::getBinaryItem
2116 // Purpose :
2117 //=======================================================================
2118 QTableWidgetItem* SMESHGUI_FilterTable::getBinaryItem () const
2119 {
2120   static QMap<int, QString> aMap;
2121   if (aMap.isEmpty())
2122   {
2123     aMap[ SMESH::FT_LogicalAND ] = tr("AND");
2124     aMap[ SMESH::FT_LogicalOR  ] = tr("OR");
2125   }
2126
2127   return new ComboItem(aMap);
2128 }
2129
2130 //=======================================================================
2131 // name    : SMESHGUI_FilterTable::getUnaryItem
2132 // Purpose : Get check table item
2133 //=======================================================================
2134 QTableWidgetItem* SMESHGUI_FilterTable::getUnaryItem () const
2135 {
2136   return new CheckItem(tr("NOT"));
2137 }
2138
2139 //=======================================================================
2140 // name    : SMESHGUI_FilterTable::getSupportedTypes
2141 // Purpose : Get all supported type
2142 //=======================================================================
2143 const QMap<int, QString>& SMESHGUI_FilterTable::getSupportedTypes() const
2144 {
2145   static QMap<int, QString> aTypes;
2146   if (aTypes.isEmpty())
2147   {
2148     aTypes[ SMESH::NODE   ] = tr("NODES");
2149     aTypes[ SMESH::ELEM0D ] = tr("ELEM0D");
2150     aTypes[ SMESH::BALL   ] = tr("BALLS");
2151     aTypes[ SMESH::EDGE   ] = tr("EDGES");
2152     aTypes[ SMESH::FACE   ] = tr("FACES");
2153     aTypes[ SMESH::VOLUME ] = tr("VOLUMES");
2154     aTypes[ SMESH::ALL ]    = tr("ELEMENTS");
2155   }
2156
2157   return aTypes;
2158 }
2159
2160 //=======================================================================
2161 // name    : SMESHGUI_FilterTable::getCriteria
2162 // Purpose : Get criteria for specified type
2163 //=======================================================================
2164 const QMap<int, QString>& SMESHGUI_FilterTable::getCriteria (const int theType) const
2165 {
2166   if (theType == SMESH::NODE)
2167   {
2168     static QMap<int, QString> aCriteria;
2169     if (aCriteria.isEmpty())
2170     {
2171       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2172       aCriteria[ SMESH::FT_BelongToMeshGroup  ] = tr("BELONG_TO_MESH_GROUP");
2173       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2174       aCriteria[ SMESH::FT_BelongToPlane      ] = tr("BELONG_TO_PLANE");
2175       aCriteria[ SMESH::FT_BelongToCylinder   ] = tr("BELONG_TO_CYLINDER");
2176       aCriteria[ SMESH::FT_BelongToGenSurface ] = tr("BELONG_TO_GENSURFACE");
2177       aCriteria[ SMESH::FT_LyingOnGeom        ] = tr("LYING_ON_GEOM");
2178       aCriteria[ SMESH::FT_FreeNodes          ] = tr("FREE_NODES");
2179       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2180       aCriteria[ SMESH::FT_EqualNodes         ] = tr("EQUAL_NODE");
2181       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2182     }
2183     return aCriteria;
2184   }
2185   else if (theType == SMESH::EDGE)
2186   {
2187     static QMap<int, QString> aCriteria;
2188     if (aCriteria.isEmpty())
2189     {
2190       aCriteria[ SMESH::FT_FreeBorders        ] = tr("FREE_BORDERS");
2191       aCriteria[ SMESH::FT_MultiConnection    ] = tr("MULTI_BORDERS");
2192       aCriteria[ SMESH::FT_Length             ] = tr("LENGTH");
2193       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2194       aCriteria[ SMESH::FT_BelongToMeshGroup  ] = tr("BELONG_TO_MESH_GROUP");
2195       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2196       aCriteria[ SMESH::FT_BelongToPlane      ] = tr("BELONG_TO_PLANE");
2197       aCriteria[ SMESH::FT_BelongToCylinder   ] = tr("BELONG_TO_CYLINDER");
2198       aCriteria[ SMESH::FT_BelongToGenSurface ] = tr("BELONG_TO_GENSURFACE");
2199       aCriteria[ SMESH::FT_LyingOnGeom        ] = tr("LYING_ON_GEOM");
2200       aCriteria[ SMESH::FT_LinearOrQuadratic  ] = tr("LINEAR");
2201       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2202       aCriteria[ SMESH::FT_ElemGeomType       ] = tr("GEOM_TYPE");
2203       aCriteria[ SMESH::FT_EqualEdges         ] = tr("EQUAL_EDGE");
2204       aCriteria[ SMESH::FT_EntityType         ] = tr("ENTITY_TYPE");
2205       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2206     }
2207     return aCriteria;
2208   }
2209   else if (theType == SMESH::FACE)
2210   {
2211     static QMap<int, QString> aCriteria;
2212     if (aCriteria.isEmpty())
2213     {
2214       aCriteria[ SMESH::FT_AspectRatio        ] = tr("ASPECT_RATIO");
2215       aCriteria[ SMESH::FT_Warping            ] = tr("WARPING");
2216       aCriteria[ SMESH::FT_MinimumAngle       ] = tr("MINIMUM_ANGLE");
2217       aCriteria[ SMESH::FT_Taper              ] = tr("TAPER");
2218       aCriteria[ SMESH::FT_Skew               ] = tr("SKEW");
2219       aCriteria[ SMESH::FT_Area               ] = tr("AREA");
2220       aCriteria[ SMESH::FT_MaxElementLength2D ] = tr("MAX_ELEMENT_LENGTH_2D");
2221       aCriteria[ SMESH::FT_FreeEdges          ] = tr("FREE_EDGES");
2222       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2223       aCriteria[ SMESH::FT_BelongToMeshGroup  ] = tr("BELONG_TO_MESH_GROUP");
2224       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2225       aCriteria[ SMESH::FT_BelongToPlane      ] = tr("BELONG_TO_PLANE");
2226       aCriteria[ SMESH::FT_BelongToCylinder   ] = tr("BELONG_TO_CYLINDER");
2227       aCriteria[ SMESH::FT_BelongToGenSurface ] = tr("BELONG_TO_GENSURFACE");
2228       aCriteria[ SMESH::FT_LyingOnGeom        ] = tr("LYING_ON_GEOM");
2229       aCriteria[ SMESH::FT_Length2D           ] = tr("LENGTH2D");
2230       aCriteria[ SMESH::FT_MultiConnection2D  ] = tr("MULTI2D_BORDERS");
2231       aCriteria[ SMESH::FT_FreeFaces          ] = tr("FREE_FACES");
2232       aCriteria[ SMESH::FT_BareBorderFace     ] = tr("BARE_BORDER_FACE");
2233       aCriteria[ SMESH::FT_OverConstrainedFace] = tr("OVER_CONSTRAINED_FACE");
2234       aCriteria[ SMESH::FT_LinearOrQuadratic  ] = tr("LINEAR");
2235       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2236       aCriteria[ SMESH::FT_ElemGeomType       ] = tr("GEOM_TYPE");
2237       aCriteria[ SMESH::FT_CoplanarFaces      ] = tr("COPLANAR_FACES");
2238       aCriteria[ SMESH::FT_EqualFaces         ] = tr("EQUAL_FACE");
2239       aCriteria[ SMESH::FT_EntityType         ] = tr("ENTITY_TYPE");
2240       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2241     }
2242     return aCriteria;
2243   }
2244   else if (theType == SMESH::VOLUME)
2245   {
2246     static QMap<int, QString> aCriteria;
2247     if (aCriteria.isEmpty())
2248     {
2249       aCriteria[ SMESH::FT_AspectRatio3D        ] = tr("ASPECT_RATIO_3D");
2250       aCriteria[ SMESH::FT_RangeOfIds           ] = tr("RANGE_OF_IDS");
2251       aCriteria[ SMESH::FT_BelongToMeshGroup    ] = tr("BELONG_TO_MESH_GROUP");
2252       aCriteria[ SMESH::FT_BelongToGeom         ] = tr("BELONG_TO_GEOM");
2253       aCriteria[ SMESH::FT_LyingOnGeom          ] = tr("LYING_ON_GEOM");
2254       aCriteria[ SMESH::FT_BadOrientedVolume    ] = tr("BAD_ORIENTED_VOLUME");
2255       aCriteria[ SMESH::FT_BareBorderVolume     ] = tr("BARE_BORDER_VOLUME");
2256       aCriteria[ SMESH::FT_OverConstrainedVolume] = tr("OVER_CONSTRAINED_VOLUME");
2257       aCriteria[ SMESH::FT_Volume3D             ] = tr("VOLUME_3D");
2258       aCriteria[ SMESH::FT_MaxElementLength3D   ] = tr("MAX_ELEMENT_LENGTH_3D");
2259       aCriteria[ SMESH::FT_LinearOrQuadratic    ] = tr("LINEAR");
2260       aCriteria[ SMESH::FT_GroupColor           ] = tr("GROUP_COLOR");
2261       aCriteria[ SMESH::FT_ElemGeomType         ] = tr("GEOM_TYPE");
2262       aCriteria[ SMESH::FT_EqualVolumes         ] = tr("EQUAL_VOLUME");
2263       aCriteria[ SMESH::FT_EntityType           ] = tr("ENTITY_TYPE");
2264       aCriteria[ SMESH::FT_ConnectedElements    ] = tr("CONNECTED_ELEMS");
2265     }
2266     return aCriteria;
2267   }
2268   else if (theType == SMESH::ELEM0D)
2269   {
2270     static QMap<int, QString> aCriteria;
2271     if (aCriteria.isEmpty())
2272     {
2273       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2274       aCriteria[ SMESH::FT_BelongToMeshGroup  ] = tr("BELONG_TO_MESH_GROUP");
2275       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2276       aCriteria[ SMESH::FT_BelongToPlane      ] = tr("BELONG_TO_PLANE");
2277       aCriteria[ SMESH::FT_BelongToCylinder   ] = tr("BELONG_TO_CYLINDER");
2278       aCriteria[ SMESH::FT_BelongToGenSurface ] = tr("BELONG_TO_GENSURFACE");
2279       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2280       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2281     }
2282     return aCriteria;
2283   }
2284   else if (theType == SMESH::BALL)
2285   {
2286     static QMap<int, QString> aCriteria;
2287     if (aCriteria.isEmpty())
2288     {
2289       aCriteria[ SMESH::FT_BallDiameter       ] = tr("BALL_DIAMETER");
2290       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2291       aCriteria[ SMESH::FT_BelongToMeshGroup  ] = tr("BELONG_TO_MESH_GROUP");
2292       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2293       aCriteria[ SMESH::FT_BelongToPlane      ] = tr("BELONG_TO_PLANE");
2294       aCriteria[ SMESH::FT_BelongToCylinder   ] = tr("BELONG_TO_CYLINDER");
2295       aCriteria[ SMESH::FT_BelongToGenSurface ] = tr("BELONG_TO_GENSURFACE");
2296       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2297       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2298     }
2299     return aCriteria;
2300   }
2301   else if (theType == SMESH::ELEM0D)
2302   {
2303     static QMap<int, QString> aCriteria;
2304     if (aCriteria.isEmpty())
2305     {
2306       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2307       aCriteria[ SMESH::FT_BelongToMeshGroup  ] = tr("BELONG_TO_MESH_GROUP");
2308       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2309       aCriteria[ SMESH::FT_BelongToPlane      ] = tr("BELONG_TO_PLANE");
2310       aCriteria[ SMESH::FT_BelongToCylinder   ] = tr("BELONG_TO_CYLINDER");
2311       aCriteria[ SMESH::FT_BelongToGenSurface ] = tr("BELONG_TO_GENSURFACE");
2312       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2313       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2314     }
2315     return aCriteria;
2316   }
2317   else // SMESH::ALL
2318   {
2319     static QMap<int, QString> aCriteria;
2320     if (aCriteria.isEmpty())
2321     {
2322       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2323       aCriteria[ SMESH::FT_BelongToMeshGroup  ] = tr("BELONG_TO_MESH_GROUP");
2324       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2325       aCriteria[ SMESH::FT_LyingOnGeom        ] = tr("LYING_ON_GEOM");
2326       aCriteria[ SMESH::FT_LinearOrQuadratic  ] = tr("LINEAR");
2327       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2328       aCriteria[ SMESH::FT_ElemGeomType       ] = tr("GEOM_TYPE");
2329       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2330     }
2331
2332     return aCriteria;
2333   }
2334 }
2335
2336
2337 //=======================================================================
2338 // name    : SMESHGUI_FilterTable::getCompare
2339 // Purpose : Get operation of comparison
2340 //=======================================================================
2341 const QMap<int, QString>& SMESHGUI_FilterTable::getCompare() const
2342 {
2343   static QMap<int, QString> aMap;
2344
2345   if (aMap.isEmpty())
2346   {
2347     aMap[ SMESH::FT_LessThan ] = tr("LESS_THAN");
2348     aMap[ SMESH::FT_MoreThan ] = tr("MORE_THAN");
2349     aMap[ SMESH::FT_EqualTo  ] = tr("EQUAL_TO" );
2350   }
2351
2352   return aMap;
2353 }
2354
2355 //=======================================================================
2356 // name    : SMESHGUI_FilterTable::createTable
2357 // Purpose : Create table
2358 //=======================================================================
2359 SMESHGUI_FilterTable::Table* SMESHGUI_FilterTable::createTable (QWidget*  theParent,
2360                                                                 const int theType)
2361 {
2362   // create table
2363   Table* aTable= new Table(0, 6, theParent);
2364
2365   QHeaderView* aHeaders = aTable->horizontalHeader();
2366
2367   QFontMetrics aMetrics(aHeaders->font());
2368
2369   // append spaces to the header of criteria in order to
2370   // provide visibility of criterion inside comboboxes
2371   static int aMaxLenCr = 0;
2372
2373   if (aMaxLenCr == 0)
2374   {
2375     const QMap<int, QString>& aSupportedTypes = getSupportedTypes();
2376     QMap<int, QString>::const_iterator anIter;
2377     for (anIter = aSupportedTypes.begin(); anIter != aSupportedTypes.end(); ++anIter)
2378       aMaxLenCr = qMax(maxLength(getCriteria(anIter.key()), aMetrics), aMaxLenCr);
2379   }
2380
2381   static int aLenCr = qAbs( aMaxLenCr -
2382                             aMetrics.width(tr("CRITERION"))) / aMetrics.width(' ') + 5;
2383
2384   QString aCrStr;
2385   aCrStr.fill(' ', aLenCr);
2386   QString aCoStr;
2387   aCoStr.fill(' ', 10);
2388
2389   QStringList aHeaderLabels;
2390   aHeaderLabels.append( tr("CRITERION") + aCrStr );
2391   aHeaderLabels.append( tr("COMPARE")   + aCoStr );
2392   aHeaderLabels.append( tr("THRESHOLD_VALUE") );
2393   aHeaderLabels.append( tr("UNARY") );
2394   aHeaderLabels.append( tr("BINARY") + "  " );
2395   aHeaderLabels.append( tr("ID") );
2396   aTable->setHorizontalHeaderLabels( aHeaderLabels );
2397   
2398   // set geometry of the table
2399   for (int i = 0; i <= 4; i++)
2400     aTable->resizeColumnToContents(i);
2401
2402   // set the ID column invisible
2403   aTable->hideColumn( 5 );
2404
2405   aTable->updateGeometry();
2406   QSize aSize = aTable->sizeHint();
2407   int aWidth = aSize.width();
2408   aTable->setMinimumSize(QSize(aWidth, aWidth / 2));
2409   aTable->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
2410
2411   connect(aTable, SIGNAL(cellChanged(int, int)),
2412           this,   SLOT(onCriterionChanged(int, int)));
2413
2414   connect(aTable, SIGNAL(currentCellChanged(int, int, int, int)),
2415           this,   SLOT(onCurrentChanged(int, int)));
2416   
2417   return aTable;
2418 }
2419
2420 //=======================================================================
2421 // name    : SMESHGUI_FilterTable::updateBtnState
2422 // Purpose : Update button state
2423 //=======================================================================
2424 void SMESHGUI_FilterTable::updateBtnState()
2425 {
2426   myRemoveBtn->setEnabled(myTables[ GetType() ]->rowCount() > 0);
2427   myClearBtn->setEnabled(myTables[ GetType() ]->rowCount() > 0);
2428 }
2429
2430 //=======================================================================
2431 // name    : SMESHGUI_FilterTable::SetEditable
2432 // Purpose : Set read only flag for tables. Show/hide buttons for work with rows
2433 //=======================================================================
2434 void SMESHGUI_FilterTable::SetEditable (const bool isEditable)
2435 {
2436   TableMap::iterator anIter;
2437   for (anIter = myTables.begin(); anIter != myTables.end(); ++anIter)
2438   {
2439     anIter.value()->setReadOnly(!isEditable);
2440
2441     // Set Flags for CheckItems directly IPAL 19974
2442     Table* aTable = anIter.value();
2443     for (int i = 0, n = aTable->rowCount(); i < n; i++)
2444       for (int j = 0, m = aTable->columnCount(); j < m; j++)
2445         {
2446           QTableWidgetItem* anItem = aTable->item(i, j);
2447           if ( dynamic_cast<SMESHGUI_FilterTable::CheckItem*>( anItem ) ) {
2448             Qt::ItemFlags f = anItem->flags();
2449             if (!isEditable) f = f & ~Qt::ItemIsUserCheckable;
2450             else f = f | Qt::ItemIsUserCheckable;
2451             anItem->setFlags( f );
2452           }
2453         }
2454     //end of IPAL19974
2455
2456     if (isEditable)
2457     {
2458       myAddBtn->show();
2459       myInsertBtn->show();
2460       myRemoveBtn->show();
2461       myClearBtn->show();
2462     }
2463     else
2464     {
2465       myAddBtn->hide();
2466       myInsertBtn->hide();
2467       myRemoveBtn->hide();
2468       myClearBtn->hide();
2469     }
2470   }
2471
2472   QMap<QTableWidgetItem*, AdditionalWidget*>::iterator anIter2;
2473   for (anIter2 = myAddWidgets.begin(); anIter2 != myAddWidgets.end(); ++anIter2)
2474     anIter2.value()->SetEditable(isEditable);
2475 }
2476
2477 //=======================================================================
2478 // name    : SMESHGUI_FilterTable::SetEnabled
2479 // Purpose : Enable/Disable table. Switching type of elements already enabled
2480 //=======================================================================
2481 void SMESHGUI_FilterTable::SetEnabled (const bool isEnabled)
2482 {
2483   myAddBtn->setEnabled(isEnabled);
2484   myInsertBtn->setEnabled(isEnabled);
2485   myRemoveBtn->setEnabled(isEnabled);
2486   myClearBtn->setEnabled(isEnabled);
2487
2488   if (isEnabled)
2489     updateBtnState();
2490
2491   QMap<QTableWidgetItem*, AdditionalWidget*>::iterator anIter2;
2492   for (anIter2 = myAddWidgets.begin(); anIter2 != myAddWidgets.end(); ++anIter2)
2493     anIter2.value()->setEnabled(isEnabled);
2494 }
2495
2496 //=======================================================================
2497 // name    : SMESHGUI_FilterTable::IsEditable
2498 // Purpose : Verify whether table is editable
2499 //=======================================================================
2500 bool SMESHGUI_FilterTable::IsEditable() const
2501 {
2502   return !myTables[ GetType() ]->isReadOnly();
2503 }
2504
2505 //=======================================================================
2506 // name    : SMESHGUI_FilterTable::SetLibsEnabled
2507 // Purpose : Show/hide buttons for work with libraries
2508 //=======================================================================
2509 void SMESHGUI_FilterTable::SetLibsEnabled (const bool isEnabled)
2510 {
2511   if (isEnabled)
2512   {
2513     myCopyFromBtn->show();
2514     myAddToBtn->show();
2515   }
2516   else
2517   {
2518     myCopyFromBtn->hide();
2519     myAddToBtn->hide();
2520   }
2521 }
2522
2523 //=======================================================================
2524 // name    : SMESHGUI_FilterTable::onCopyFromBtn
2525 // Purpose : SLOT. Called the "Copy from ..." button clicked
2526 //           Display filter library dialog
2527 //=======================================================================
2528 void SMESHGUI_FilterTable::onCopyFromBtn()
2529 {
2530   if (myLibDlg == 0)
2531     myLibDlg = new SMESHGUI_FilterLibraryDlg(
2532       mySMESHGUI, this, GetType(), SMESHGUI_FilterLibraryDlg::COPY_FROM);
2533   else
2534     myLibDlg->Init(GetType(), SMESHGUI_FilterLibraryDlg::COPY_FROM);
2535
2536   if (myLibDlg->exec() == QDialog::Accepted)
2537   {
2538     Copy(myLibDlg->GetTable());
2539     Update();
2540   }
2541 }
2542
2543 //=======================================================================
2544 // name    : SMESHGUI_FilterTable::onAddToBtn
2545 // Purpose : SLOT. Called the "Add to ..." button clicked
2546 //           Display filter library dialog
2547 //=======================================================================
2548 void SMESHGUI_FilterTable::onAddToBtn()
2549 {
2550   if (!IsValid(true))
2551     return;
2552   if (myLibDlg == 0)
2553     myLibDlg = new SMESHGUI_FilterLibraryDlg(
2554       mySMESHGUI, this, GetType(), SMESHGUI_FilterLibraryDlg::ADD_TO);
2555   else
2556     myLibDlg->Init(GetType(), SMESHGUI_FilterLibraryDlg::ADD_TO);
2557
2558   myLibDlg->SetTable(this);
2559
2560   myLibDlg->exec();
2561 }
2562
2563 //=======================================================================
2564 // name    : SMESHGUI_FilterTable::Copy
2565 // Purpose : Initialise table with values of other table
2566 //=======================================================================
2567 void SMESHGUI_FilterTable::Copy (const SMESHGUI_FilterTable* theTable)
2568 {
2569   Clear();
2570
2571   for (int i = 0, n = theTable->NumRows(); i < n; i++)
2572   {
2573     SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
2574     theTable->GetCriterion(i, aCriterion);
2575     AddCriterion(aCriterion);
2576   }
2577 }
2578
2579 //=======================================================================
2580 // name    : SMESHGUI_FilterTable::CurrentCell
2581 // Purpose : Returns current cell
2582 //=======================================================================
2583 bool SMESHGUI_FilterTable::CurrentCell (int& theRow, int& theCol) const
2584 {
2585   theRow = myTables[ GetType() ]->currentRow();
2586   theCol = myTables[ GetType() ]->currentColumn();
2587   return theRow >= 0 && theCol >= 0;
2588 }
2589
2590 //=======================================================================
2591 // name    : SMESHGUI_FilterTable::SetText
2592 // Purpose : Set text and internal value in cell of threshold value
2593 //=======================================================================
2594 void SMESHGUI_FilterTable::SetThreshold (const int      theRow,
2595                                          const QString& theText,
2596                                          const int      theEntityType)
2597 {
2598   Table* aTable = myTables[ theEntityType == -1 ? GetType() : theEntityType ];
2599
2600   bool isSignalsBlocked = aTable->signalsBlocked();
2601   aTable->blockSignals(true);
2602   
2603   aTable->item( theRow, 2 )->setText(theText);
2604
2605   aTable->blockSignals(isSignalsBlocked);
2606 }
2607
2608 //=======================================================================
2609 // name    : SMESHGUI_FilterTable::SetText
2610 // Purpose : Get text and internal value from cell of threshold value
2611 //=======================================================================
2612 bool SMESHGUI_FilterTable::GetThreshold (const int      theRow,
2613                                          QString&       theText,
2614                                          const int      theEntityType)
2615 {
2616   Table* aTable = myTables[ theEntityType == -1 ? GetType() : theEntityType ];
2617   QTableWidgetItem* anItem = aTable->item(theRow, 2);
2618   if (anItem != 0)
2619   {
2620     theText = anItem->text();
2621     return true;
2622   }
2623   else
2624    return false;
2625 }
2626
2627 //=======================================================================
2628 // name    : SMESHGUI_FilterTable::SetID
2629 // Purpose : Set text and internal value in cell of ID value 
2630 //=======================================================================
2631 void SMESHGUI_FilterTable::SetID( const int      theRow,
2632                                   const QString& theText,
2633                                   const int      theEntityType )
2634 {
2635   Table* aTable = myTables[ theEntityType == -1 ? GetType() : theEntityType ];
2636   aTable->item( theRow, 5 )->setText( theText );
2637 }
2638
2639 //=======================================================================
2640 // name    : SMESHGUI_FilterTable::GetID
2641 // Purpose : Get text and internal value from cell of ID value
2642 //=======================================================================
2643 bool SMESHGUI_FilterTable::GetID( const int      theRow,
2644                                   QString&       theText,
2645                                   const int      theEntityType )
2646 {
2647   Table* aTable = myTables[ theEntityType == -1 ? GetType() : theEntityType ];
2648   QTableWidgetItem* anItem = aTable->item( theRow, 5 );
2649   if ( anItem != 0 )
2650     {
2651       theText = anItem->text();
2652       return true;    
2653     }
2654   else
2655     return false;
2656
2657
2658 /*
2659   Class       : SMESHGUI_FilterDlg
2660   Description : Dialog to specify filters for VTK viewer
2661 */
2662
2663
2664 //=======================================================================
2665 // name    : SMESHGUI_FilterDlg::SMESHGUI_FilterDlg
2666 // Purpose : Constructor
2667 //=======================================================================
2668 SMESHGUI_FilterDlg::SMESHGUI_FilterDlg( SMESHGUI*         theModule,
2669                                         const QList<int>& theTypes )
2670 : QDialog( SMESH::GetDesktop( theModule ) ),
2671   mySMESHGUI( theModule ),
2672   mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
2673   myInitSourceWgOnApply( true ),
2674   myInsertEnabled( true ),
2675   myDiffSourcesEnabled( true )
2676 {
2677   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
2678     mySelector = aViewWindow->GetSelector();
2679
2680   construct(theTypes);
2681 }
2682
2683 //=======================================================================
2684 // name    : SMESHGUI_FilterDlg::SMESHGUI_FilterDlg
2685 // Purpose : Constructor
2686 //=======================================================================
2687 SMESHGUI_FilterDlg::SMESHGUI_FilterDlg( SMESHGUI*   theModule,
2688                                         const int   theType )
2689 : QDialog( SMESH::GetDesktop( theModule ) ),
2690   mySMESHGUI( theModule ),
2691   mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
2692   myInitSourceWgOnApply( true ),
2693   myInsertEnabled( true ),
2694   myDiffSourcesEnabled( true )
2695 {
2696   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
2697     mySelector = aViewWindow->GetSelector();
2698   QList<int> aTypes;
2699   aTypes.append(theType);
2700   construct(aTypes);
2701 }
2702
2703 //=======================================================================
2704 // name    : SMESHGUI_FilterDlg::construct
2705 // Purpose : Construct dialog (called by constructor)
2706 //=======================================================================
2707 void SMESHGUI_FilterDlg::construct (const QList<int>& theTypes)
2708 {
2709   myTypes = theTypes;
2710
2711   setModal(false);
2712   //setAttribute(Qt::WA_DeleteOnClose, true); // VSR ??? is it required?
2713   setWindowTitle(tr("CAPTION"));
2714
2715   QVBoxLayout* aDlgLay = new QVBoxLayout (this);
2716   aDlgLay->setMargin(MARGIN);
2717   aDlgLay->setSpacing(SPACING);
2718
2719   myMainFrame         = createMainFrame  (this);
2720   QWidget* aBtnFrame  = createButtonFrame(this);
2721
2722   aDlgLay->addWidget(myMainFrame);
2723   aDlgLay->addWidget(aBtnFrame);
2724
2725   aDlgLay->setStretchFactor(myMainFrame, 1);
2726
2727   myHelpFileName = "selection_filter_library_page.html";
2728
2729   Init(myTypes);
2730 }
2731
2732 //=======================================================================
2733 // name    : SMESHGUI_FilterDlg::createMainFrame
2734 // Purpose : Create frame containing dialog's input fields
2735 //=======================================================================
2736 QWidget* SMESHGUI_FilterDlg::createMainFrame (QWidget* theParent)
2737 {
2738   QWidget* aMainFrame = new QWidget(theParent);
2739   QVBoxLayout* aMainLay = new QVBoxLayout(aMainFrame);
2740   aMainLay->setMargin(0);
2741   aMainLay->setSpacing(SPACING);
2742
2743   // filter frame
2744
2745   myTable = new SMESHGUI_FilterTable( mySMESHGUI, aMainFrame, myTypes );
2746   myTable->SetLibsEnabled(true);
2747
2748   QGroupBox* aGrp = myTable->GetTableGrp();
2749   QGridLayout* aLay = qobject_cast<QGridLayout*>( aGrp->layout() );
2750   int rows = aLay->rowCount();
2751   int cols = aLay->columnCount();
2752
2753   // This line looks strange when all additional parameters and mySetInViewer are hidden
2754   QFrame* aLine = new QFrame(aGrp);
2755   aLine->setFrameStyle(QFrame::HLine | QFrame::Sunken);
2756   aLay->addWidget(aLine, rows++, 0, 1, cols);
2757
2758   mySetInViewer = new QCheckBox(tr("SET_IN_VIEWER"), aGrp);
2759   mySetInViewer->setChecked(true);
2760   aLay->addWidget(mySetInViewer, rows++, 0, 1, cols);
2761
2762   // other controls
2763   QWidget* aSourceGrp = createSourceGroup(aMainFrame);
2764
2765   connect(myTable, SIGNAL(CriterionChanged(const int, const int)),
2766                     SLOT(onCriterionChanged(const int, const int)));
2767
2768   connect(myTable, SIGNAL(CurrentChanged(int, int)),
2769                     SLOT(onCurrentChanged(int, int)));
2770
2771   aMainLay->addWidget(myTable);
2772   aMainLay->addWidget(aSourceGrp);
2773   
2774   return aMainFrame;
2775 }
2776
2777 //=======================================================================
2778 // name    : SMESHGUI_FilterDlg::createSourceFrame
2779 // Purpose : Create frame containing source radio button
2780 //=======================================================================
2781 QWidget* SMESHGUI_FilterDlg::createSourceGroup (QWidget* theParent)
2782 {
2783   QGroupBox* aBox = new QGroupBox(tr("SOURCE"), theParent);
2784   QHBoxLayout* aLay = new QHBoxLayout(aBox);
2785   aLay->setMargin(MARGIN);
2786   aLay->setSpacing(SPACING);
2787
2788   mySourceGrp = new QButtonGroup(theParent);
2789
2790   QRadioButton* aMeshBtn = new QRadioButton(tr("MESH"),          aBox);
2791   QRadioButton* aSelBtn  = new QRadioButton(tr("SELECTION"),     aBox);
2792   QRadioButton* aDlgBtn  = new QRadioButton(tr("CURRENT_DIALOG"),aBox);
2793
2794   aLay->addWidget(aMeshBtn);
2795   aLay->addWidget(aSelBtn);
2796   aLay->addWidget(aDlgBtn);
2797
2798   mySourceGrp->addButton(aMeshBtn, Mesh);
2799   mySourceGrp->addButton(aSelBtn,  Selection);
2800   mySourceGrp->addButton(aDlgBtn,  Dialog);
2801
2802   aMeshBtn->setChecked(true);
2803
2804   return aBox;
2805 }
2806
2807 //=======================================================================
2808 // name    : SMESHGUI_FilterDlg::updateMainButtons
2809 // Purpose : Update visibility of main buttons (OK, Cancel, Close ...)
2810 //=======================================================================
2811 void SMESHGUI_FilterDlg::updateMainButtons()
2812 {
2813   myButtons[ BTN_Close  ]->show();
2814   if (myTypes.count() == 1)
2815   {
2816     myButtons[ BTN_Apply  ]->hide();
2817   }
2818   else
2819   {
2820     myButtons[ BTN_Apply  ]->show();
2821   }
2822 //  updateGeometry();
2823 }
2824
2825 //=======================================================================
2826 // name    : SMESHGUI_FilterDlg::createButtonFrame
2827 // Purpose : Create frame containing buttons
2828 //=======================================================================
2829 QWidget* SMESHGUI_FilterDlg::createButtonFrame (QWidget* theParent)
2830 {
2831   QGroupBox* aGrp = new QGroupBox(theParent);
2832   QHBoxLayout* aLay = new QHBoxLayout(aGrp);
2833   aLay->setMargin(MARGIN);
2834   aLay->setSpacing(SPACING);
2835
2836   myButtons[ BTN_OK     ] = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aGrp);
2837   myButtons[ BTN_Apply  ] = new QPushButton(tr("SMESH_BUT_APPLY"),           aGrp);
2838   myButtons[ BTN_Close  ] = new QPushButton(tr("SMESH_BUT_CLOSE"),           aGrp);
2839   myButtons[ BTN_Help   ] = new QPushButton(tr("SMESH_BUT_HELP"),            aGrp);
2840
2841   aLay->addWidget(myButtons[ BTN_OK     ]);
2842   aLay->addSpacing(10);
2843   aLay->addWidget(myButtons[ BTN_Apply  ]);
2844   aLay->addSpacing(10);
2845   aLay->addStretch();
2846   aLay->addWidget(myButtons[ BTN_Close  ]);
2847   aLay->addWidget(myButtons[ BTN_Help   ]);
2848
2849   connect(myButtons[ BTN_OK     ], SIGNAL(clicked()), SLOT(onOk()));
2850   connect(myButtons[ BTN_Close  ], SIGNAL(clicked()), SLOT(reject()));
2851   connect(myButtons[ BTN_Apply  ], SIGNAL(clicked()), SLOT(onApply()));
2852   connect(myButtons[ BTN_Help   ], SIGNAL(clicked()), SLOT(onHelp()));
2853
2854   updateMainButtons();
2855
2856   return aGrp;
2857 }
2858
2859 //=======================================================================
2860 // name    : SMESHGUI_FilterDlg::~SMESHGUI_FilterDlg
2861 // Purpose : Destructor
2862 //=======================================================================
2863 SMESHGUI_FilterDlg::~SMESHGUI_FilterDlg()
2864 {
2865 }
2866
2867 //=======================================================================
2868 // name    : SMESHGUI_FilterDlg::Init
2869 // Purpose : Init dialog fields, connect signals and slots, show dialog
2870 //=======================================================================
2871 void SMESHGUI_FilterDlg::Init (const int type, const bool setInViewer)
2872 {
2873   QList<int> aTypes;
2874   aTypes.append(type);
2875   Init(aTypes,setInViewer);
2876 }
2877
2878 //=======================================================================
2879 // name    : SMESHGUI_FilterDlg::Init
2880 // Purpose : Init dialog fields, connect signals and slots, show dialog
2881 //=======================================================================
2882 void SMESHGUI_FilterDlg::Init (const QList<int>& theTypes, const bool setInViewer)
2883 {
2884   mySourceWg  = 0;
2885   myTypes     = theTypes;
2886   myMesh      = SMESH::SMESH_Mesh::_nil();
2887   myGroup     = SMESH::SMESH_GroupOnFilter::_nil();
2888   myIObjects.Clear();
2889   myIsSelectionChanged = false;
2890   myToRestoreSelMode = false;
2891
2892   myTable->Init(theTypes);
2893
2894   // set caption
2895   if (theTypes.count() == 1)
2896   {
2897     int aType = theTypes.first();
2898     if      (aType == SMESH::NODE  ) setWindowTitle(tr("NODES_TLT"));
2899     else if (aType == SMESH::ELEM0D) setWindowTitle(tr("ELEM0D_TLT"));
2900     else if (aType == SMESH::BALL  ) setWindowTitle(tr("BALL_TLT"));
2901     else if (aType == SMESH::EDGE  ) setWindowTitle(tr("EDGES_TLT"));
2902     else if (aType == SMESH::FACE  ) setWindowTitle(tr("FACES_TLT"));
2903     else if (aType == SMESH::VOLUME) setWindowTitle(tr("VOLUMES_TLT"));
2904     else if (aType == SMESH::ALL)    setWindowTitle(tr("TLT"));
2905   }
2906   else
2907     setWindowTitle(tr("TLT"));
2908
2909   qApp->processEvents();
2910   updateGeometry();
2911   adjustSize();
2912   setEnabled(true);
2913
2914   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
2915
2916   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
2917   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(reject()));
2918   
2919   updateMainButtons();
2920   updateSelection();
2921
2922   // Initialise filter table with values of previous filter
2923   QList<int>::const_iterator anIter;
2924   for (anIter = theTypes.begin(); anIter != theTypes.end(); ++anIter)
2925   {
2926     myTable->Clear(*anIter);
2927     if (!myFilter[ *anIter ]->_is_nil())
2928     {
2929       SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
2930       if (myFilter[ *anIter ]->GetCriteria(aCriteria))
2931       {
2932         for (int i = 0, n = aCriteria->length(); i < n; i++)
2933           myTable->AddCriterion(aCriteria[ i ], *anIter);
2934       }
2935     }
2936   }
2937
2938   if (myInsertState.contains(theTypes.first()))
2939     mySetInViewer->setChecked(myInsertState[ theTypes.first() ]);
2940   else
2941     mySetInViewer->setChecked(setInViewer);
2942
2943   mySourceGrp->button(myApplyToState.contains(theTypes.first()) ? 
2944                       myApplyToState[ theTypes.first() ] :
2945                       Mesh)->setChecked(true);
2946 }
2947
2948 //=======================================================================
2949 // name    : SMESHGUI_FilterDlg::onOk
2950 // Purpose : SLOT called when "Ok" button pressed.
2951 //           Assign filters VTK viewer and close dialog
2952 //=======================================================================
2953 void SMESHGUI_FilterDlg::onOk()
2954 {
2955   if (onApply())
2956   {
2957     restoreSelMode();
2958     mySelectionMgr->clearFilters();
2959     disconnect(mySMESHGUI, 0, this, 0);
2960     disconnect(mySelectionMgr, 0, this, 0);
2961     mySMESHGUI->ResetState();
2962     accept();
2963     emit Accepted();
2964   }
2965 }
2966
2967 //=======================================================================
2968 // name    : SMESHGUI_FilterDlg::reject
2969 // Purpose : SLOT called when "Close" button pressed. Close dialog
2970 //=======================================================================
2971 void SMESHGUI_FilterDlg::reject()
2972 {
2973   restoreSelMode();
2974   // Restore previously selected object
2975   if (mySelectionMgr)
2976   {
2977     SALOME_ListIO aList;
2978     mySelectionMgr->clearFilters();
2979     mySelectionMgr->clearSelected();
2980     SALOME_DataMapIteratorOfDataMapOfIOMapOfInteger anIter (myIObjects);
2981     for ( ; anIter.More(); anIter.Next())
2982     {
2983       aList.Append(anIter.Key());
2984
2985       TColStd_MapOfInteger aResMap;
2986       const TColStd_IndexedMapOfInteger& anIndMap = anIter.Value();
2987       for (int i = 1, n = anIndMap.Extent(); i <= n; i++)
2988         aResMap.Add(anIndMap(i));
2989
2990       mySelector->AddOrRemoveIndex( anIter.Key(), aResMap, false);
2991       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
2992         aViewWindow->highlight( anIter.Key(), true, true );
2993     }
2994     mySelectionMgr->setSelectedObjects(aList, false);
2995   }
2996
2997   disconnect(mySMESHGUI, 0, this, 0);
2998   disconnect(mySelectionMgr, 0, this, 0);
2999   mySMESHGUI->ResetState();
3000   QDialog::reject();
3001 }
3002
3003 //=================================================================================
3004 // function : onHelp()
3005 // purpose  :
3006 //=================================================================================
3007 void SMESHGUI_FilterDlg::onHelp()
3008 {
3009   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
3010   if (app) 
3011     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
3012   else {
3013     QString platform;
3014 #ifdef WIN32
3015     platform = "winapplication";
3016 #else
3017     platform = "application";
3018 #endif
3019     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
3020                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
3021                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
3022                                                                  platform)).
3023                              arg(myHelpFileName));
3024   }
3025 }
3026
3027 //=======================================================================
3028 // name    : SMESHGUI_FilterDlg::onDeactivate
3029 // Purpose : SLOT called when dialog must be deativated
3030 //=======================================================================
3031 void SMESHGUI_FilterDlg::onDeactivate()
3032 {
3033   setEnabled(false);
3034 }
3035
3036 //=======================================================================
3037 // name    : SMESHGUI_FilterDlg::enterEvent
3038 // Purpose : Event filter
3039 //=======================================================================
3040 void SMESHGUI_FilterDlg::enterEvent (QEvent*)
3041 {
3042 //  mySMESHGUI->EmitSignalDeactivateDialog();
3043   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
3044   mySMESHGUI->ResetState();
3045   setEnabled(true);
3046 }
3047
3048 //=======================================================================
3049 // name    : SMESHGUI_FilterDlg::getIdsFromWg
3050 // Purpose : Retrieve list of ids from given widget
3051 //=======================================================================
3052 void SMESHGUI_FilterDlg::getIdsFromWg (const QWidget* theWg, QList<int>& theRes) const
3053 {
3054   theRes.clear();
3055   if (theWg == 0)
3056     return;
3057
3058   if (theWg->inherits("QListWidget"))
3059   {
3060     const QListWidget* aListBox = qobject_cast<const QListWidget*>( theWg );
3061     bool b;
3062     for (int i = 0, n = aListBox->count(); i < n; i++)
3063     {
3064       int anId = aListBox->item(i)->text().toInt(&b);
3065       if (b)
3066         theRes.append(anId);
3067     }
3068   }
3069   else if (theWg->inherits("QLineEdit"))
3070   {
3071     const QLineEdit* aLineEdit = qobject_cast<const QLineEdit*>( theWg );
3072     QString aStr = aLineEdit->text();
3073     QRegExp aRegExp("(\\d+)");
3074     bool b;
3075     int aPos = 0;
3076     while (aPos >= 0)
3077     {
3078       aPos = aRegExp.indexIn(aStr, aPos);
3079       if (aPos > -1)
3080       {
3081         int anId = aRegExp.cap(1).toInt(&b);
3082         if (b)
3083           theRes.append(anId);
3084         aPos += aRegExp.matchedLength();
3085       }
3086     }
3087   }
3088 }
3089
3090 //=======================================================================
3091 // name    : SMESHGUI_FilterDlg::getSelMode
3092 // Purpose : Get selection mode of specified type
3093 //=======================================================================
3094 Selection_Mode SMESHGUI_FilterDlg::getSelMode (const int theType) const
3095 {
3096   switch (theType)
3097   {
3098     case SMESH::NODE   : return NodeSelection;
3099     case SMESH::ELEM0D : return Elem0DSelection;
3100     case SMESH::BALL   : return BallSelection;
3101     case SMESH::EDGE   : return EdgeSelection;
3102     case SMESH::FACE   : return FaceSelection;
3103     case SMESH::VOLUME : return VolumeSelection;
3104     case SMESH::ALL    : return CellSelection;
3105     default            : return ActorSelection;
3106   }
3107
3108 }
3109
3110 //=======================================================================
3111 // name    : SMESHGUI_FilterDlg::setIdsToWg
3112 // Purpose : Insert identifiers in specified widgets
3113 //=======================================================================
3114 void SMESHGUI_FilterDlg::setIdsToWg (QWidget* theWg, const QList<int>& theIds)
3115 {
3116   if (theWg == 0)
3117     return;
3118
3119   QStringList aStrList;
3120   foreach(int id, theIds)
3121     aStrList << QString::number(id);
3122
3123   if (theWg->inherits("QListWidget"))
3124   {
3125     qobject_cast<QListWidget*>(theWg)->clear();
3126     qobject_cast<QListWidget*>(theWg)->addItems(aStrList);
3127   }
3128   else if (theWg->inherits("QLineEdit"))
3129   {
3130     qobject_cast<QLineEdit*>( theWg )->setText(aStrList.join(" "));
3131   }
3132 }
3133
3134 //=======================================================================
3135 // name    : SMESHGUI_FilterDlg::isValid
3136 // Purpose : Verify validity of input data
3137 //=======================================================================
3138 bool SMESHGUI_FilterDlg::isValid() const
3139 {
3140   if (!myTable->IsValid())
3141     return false;
3142
3143   for (int i = 0, n = myTable->NumRows(); i < n; i++)
3144   {
3145     int aType = myTable->GetCriterionType(i);
3146     if (aType == SMESH::FT_BelongToGeom ||
3147         aType == SMESH::FT_BelongToPlane ||
3148         aType == SMESH::FT_BelongToCylinder ||
3149         aType == SMESH::FT_BelongToGenSurface ||
3150         aType == SMESH::FT_LyingOnGeom)
3151     {
3152       QString aName;
3153       myTable->GetThreshold(i, aName);
3154
3155       std::vector<_PTR(SObject)> aList =
3156         SMESH::GetActiveStudyDocument()->FindObjectByName(aName.toLatin1().constData(), "GEOM");
3157       if (aList.size() == 0) {
3158         SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3159                                      tr("BAD_SHAPE_NAME").arg(aName));
3160         return false;
3161       }
3162
3163       if (aType == SMESH::FT_BelongToCylinder ||
3164           aType == SMESH::FT_BelongToPlane    ||
3165           aType == SMESH::FT_BelongToGenSurface ) {
3166         CORBA::Object_var     anObject = SMESH::SObjectToObject(aList[ 0 ]);
3167         GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow(anObject);
3168         if (!aGeomObj->_is_nil()) {
3169           TopoDS_Shape aFace;
3170           if (!GEOMBase::GetShape(aGeomObj, aFace) ||
3171                aFace.IsNull() ||
3172                aFace.ShapeType() != TopAbs_FACE) {
3173             SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3174                                          tr("SHAPE_IS_NOT_A_FACE").arg(aName));
3175             return false;
3176           }
3177
3178           Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aFace));
3179           if (aSurf.IsNull()) {
3180             SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3181                                          tr("SHAPE_IS_NOT_A_FACE").arg(aName));
3182             return false;
3183           }
3184
3185           if (aType == SMESH::FT_BelongToPlane && !aSurf->IsKind(STANDARD_TYPE(Geom_Plane))) {
3186             SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3187                                          tr("SHAPE_IS_NOT_A_PLANE").arg(aName));
3188             return false;
3189           }
3190
3191           if (aType == SMESH::FT_BelongToCylinder && !aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
3192             SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3193                                          tr("SHAPE_IS_NOT_A_CYLINDER").arg(aName));
3194             return false;
3195           }
3196         }
3197       }
3198     }
3199     else if (aType == SMESH::FT_CoplanarFaces)
3200     {
3201       QString faceID;
3202       myTable->GetThreshold(i, faceID);
3203       if ( faceID.isEmpty() )
3204       {
3205         SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3206                                      tr("FACE_ID_NOT_SELECTED"));
3207         return false;
3208       }
3209       if ( myMesh->_is_nil() )
3210       {
3211         SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3212                                      tr("MESH_IS_NOT_SELECTED"));
3213         return false;
3214       }
3215       if ( myMesh->GetElementType( faceID.toLong(), /*iselem=*/true) != SMESH::FACE )
3216       {
3217         SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3218                                      tr("NOT_FACE_ID").arg(faceID));
3219         return false;
3220       }
3221     }
3222   }
3223
3224   return true;
3225 }
3226
3227 //=======================================================================
3228 // name    : SMESHGUI_FilterDlg::SetSourceWg
3229 // Purpose : Set widget of parent dialog containing ids to be filtered if
3230 //           user select corresponding source radio button
3231 //=======================================================================
3232 void SMESHGUI_FilterDlg::SetSourceWg (QWidget* theWg,
3233                                       const bool initOnApply)
3234 {
3235   mySourceWg = theWg;
3236   myInitSourceWgOnApply = initOnApply;
3237 }
3238
3239 //=======================================================================
3240 //function : EnableFiltering
3241 //purpose  : Enables "Insert filter in the viewer"
3242 //           and different "Source"s (Mesh, Initial Selection, Current Group)
3243 //=======================================================================
3244
3245 void SMESHGUI_FilterDlg::SetEnabled( bool setInViewer, bool diffSources )
3246 {
3247   myInsertEnabled = setInViewer;
3248   myDiffSourcesEnabled = diffSources;
3249
3250   mySetInViewer->setVisible( myInsertEnabled );
3251   mySourceGrp->button(0)->parentWidget()->setVisible( myDiffSourcesEnabled );
3252 }
3253
3254 //=======================================================================
3255 // name    : SMESHGUI_FilterDlg::SetMesh
3256 // Purpose : Set mesh
3257 //=======================================================================
3258 void SMESHGUI_FilterDlg::SetMesh (SMESH::SMESH_Mesh_var theMesh)
3259 {
3260   if ( !theMesh->_is_nil() ) {
3261     myMesh = theMesh;
3262     if ( !myFilter[ myTable->GetType() ]->_is_nil())
3263       myFilter[ myTable->GetType() ]->SetMesh( theMesh );
3264   }
3265   const bool isEnable = !(myMesh->_is_nil());
3266   myButtons[BTN_OK]->setEnabled(isEnable);
3267   myButtons[BTN_Apply]->setEnabled(isEnable);
3268 }
3269
3270 //=======================================================================
3271 // name    : SMESHGUI_FilterDlg::SetGroup
3272 // Purpose : Set a group being edited
3273 //=======================================================================
3274 void SMESHGUI_FilterDlg::SetGroup(SMESH::SMESH_GroupOnFilter_var group)
3275 {
3276   myGroup = group;
3277 }
3278
3279 //=======================================================================
3280 // name    : SMESHGUI_FilterDlg::SetSelection
3281 // Purpose : Get filtered ids
3282 //=======================================================================
3283 void SMESHGUI_FilterDlg::SetSelection()
3284 {
3285   if (mySelectionMgr)
3286     disconnect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(onSelectionDone()));
3287
3288   if (mySelectionMgr) {
3289     myIObjects.Clear();
3290     const SALOME_ListIO& anObjs = mySelector->StoredIObjects(); 
3291     SALOME_ListIteratorOfListIO anIter (anObjs);
3292     for ( ; anIter.More(); anIter.Next()) {
3293       TColStd_IndexedMapOfInteger aMap;
3294       mySelector->GetIndex(anIter.Value(), aMap);
3295       myIObjects.Bind(anIter.Value(), aMap);
3296     }
3297
3298     connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
3299
3300     updateSelection();
3301   }
3302   else
3303     myIObjects.Clear();
3304 }
3305
3306 //=======================================================================
3307 // name    : SMESHGUI_FilterDlg::onApply
3308 // Purpose : SLOT called when "Apply" button pressed.
3309 //           Assign filters to VTK viewer
3310 //=======================================================================
3311 bool SMESHGUI_FilterDlg::onApply()
3312 {
3313   if (!isValid())
3314     return false;
3315
3316   SUIT_OverrideCursor wc;
3317   try {
3318     int aCurrType = myTable->GetType();
3319
3320     if (!createFilter(aCurrType))
3321       return false;
3322
3323     insertFilterInViewer();
3324
3325     if (!myFilter[ aCurrType ]->GetPredicate()->_is_nil()) {
3326       // 
3327       bool toFilter = (( SMESH::FindActorByObject( myMesh )) ||
3328                        ( myInitSourceWgOnApply && mySourceWg ) ||
3329                        ( mySourceGrp->checkedId() == Dialog && mySourceWg ));
3330       if ( toFilter ) {
3331         QList<int> aResultIds;
3332         filterSource(aCurrType, aResultIds);
3333         // select in viewer
3334         selectInViewer(aCurrType, aResultIds);
3335       }
3336     }
3337
3338     myInsertState[ aCurrType ] = mySetInViewer->isChecked();
3339     myApplyToState[ aCurrType ] = mySourceGrp->checkedId();
3340   }
3341   catch(const SALOME::SALOME_Exception& S_ex)
3342   {
3343     SalomeApp_Tools::QtCatchCorbaException(S_ex);
3344     return false;
3345   }
3346   catch(...)
3347   {
3348     return false;
3349   }
3350
3351   return true;
3352 }
3353
3354 //=======================================================================
3355 // name    : SMESHGUI_FilterDlg::createFilter
3356 // Purpose : Create predicate for given type
3357 //=======================================================================
3358 bool SMESHGUI_FilterDlg::createFilter (const int theType)
3359 {
3360   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
3361   if (aFilterMgr->_is_nil())
3362     return false;
3363
3364   int n = myTable->NumRows();
3365
3366   SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
3367   aCriteria->length(n);
3368
3369   long aPrecision = -1;
3370   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
3371
3372   if ( mgr && mgr->booleanValue( "SMESH", "use_precision", false ) )
3373     aPrecision = mgr->integerValue( "SMESH", "controls_precision", aPrecision );
3374
3375   for (CORBA::ULong i = 0; i < n; i++) {
3376     SMESH::Filter::Criterion aCriterion = createCriterion();
3377     myTable->GetCriterion(i, aCriterion);
3378     aCriterion.Precision = aPrecision;
3379     aCriteria[ i ] = aCriterion;
3380   }
3381
3382   if ( !myFilter[ theType ]->_is_nil() )
3383     myFilter[ theType ]->UnRegister();
3384   myFilter[ theType ] = aFilterMgr->CreateFilter();
3385   myFilter[ theType ]->SetCriteria(aCriteria.inout());
3386
3387   return true;
3388 }
3389
3390 //================================================================================
3391 /*!
3392  * \brief Return the current filter
3393  */
3394 //================================================================================
3395
3396 SMESH::Filter_var SMESHGUI_FilterDlg::GetFilter() const
3397 {
3398   SMESH::Filter_var filter;
3399   try {
3400     int aCurrType = myTable->GetType();
3401     filter = myFilter[ aCurrType ];
3402   }
3403   catch(...)
3404   {
3405   }
3406   return filter._retn();
3407 }
3408
3409 //================================================================================
3410 /*!
3411  * \brief Sets a filter to the table
3412  */
3413 //================================================================================
3414
3415 void SMESHGUI_FilterDlg::SetFilter(SMESH::Filter_var filter, int type)
3416 {
3417   if ( !filter->_is_nil() )
3418     filter->Register();
3419   if ( !myFilter[ type ]->_is_nil() )
3420     myFilter[ type ]->UnRegister();
3421
3422   myFilter[ type ] = filter;
3423 }
3424
3425 //================================================================================
3426 /*!
3427  * \brief call UnRegister() for myFilter's
3428  */
3429 //================================================================================
3430
3431 void SMESHGUI_FilterDlg::UnRegisterFilters()
3432 {
3433   QMap< int, SMESH::Filter_var >::iterator i_f = myFilter.begin();
3434   for ( ; i_f != myFilter.end(); ++i_f )
3435     if ( !i_f.value()->_is_nil() )
3436       i_f.value()->UnRegister();
3437   myFilter.clear();
3438 }
3439
3440 //=======================================================================
3441 // name    : SMESHGUI_FilterDlg::insertFilterInViewer
3442 // Purpose : Insert filter in viewer
3443 //=======================================================================
3444 void SMESHGUI_FilterDlg::insertFilterInViewer()
3445 {
3446   if (SVTK_Selector* aSelector = SMESH::GetSelector()) {
3447     SMESH::ElementType anEntType = (SMESH::ElementType)myTable->GetType();
3448
3449     if (myFilter[ myTable->GetType() ]->_is_nil() ||
3450         myFilter[ myTable->GetType() ]->GetPredicate()->_is_nil() ||
3451         !mySetInViewer->isChecked() ||
3452         !myInsertEnabled )
3453     {
3454       SMESH::RemoveFilter(getFilterId(anEntType), aSelector);
3455     }
3456     else {
3457       Handle(SMESHGUI_PredicateFilter) aFilter = new SMESHGUI_PredicateFilter();
3458       aFilter->SetPredicate(myFilter[ myTable->GetType() ]->GetPredicate());
3459       SMESH::RemoveFilter(getFilterId(anEntType), aSelector); //skl for IPAL12631
3460       SMESH::SetFilter(aFilter, aSelector);
3461     }
3462   }
3463 }
3464
3465 //=======================================================================
3466 // name    : SMESHGUI_FilterDlg::filterSource
3467 // Purpose : Filter source ids
3468 //=======================================================================
3469 void SMESHGUI_FilterDlg::filterSource (const int theType,
3470                                        QList<int>& theResIds)
3471 {
3472   theResIds.clear();
3473   if (myFilter[ theType ]->_is_nil())
3474     return;
3475
3476   int aSourceId = mySourceGrp->checkedId();
3477
3478   if (aSourceId == Mesh || !myDiffSourcesEnabled )
3479   {
3480     if (myMesh->_is_nil())
3481       return;
3482     SMESH::long_array_var anIds = myFilter[ theType ]->GetElementsId(myMesh);
3483     for (int i = 0, n = anIds->length(); i < n; i++)
3484       theResIds.append(anIds[ i ]);
3485   }
3486   else if (aSourceId == Selection)
3487   {
3488     filterSelectionSource(theType, theResIds);
3489   }
3490   else if (aSourceId == Dialog)
3491   {
3492     // retrieve ids from dialog
3493     QList<int> aDialogIds;
3494     getIdsFromWg(mySourceWg, aDialogIds);
3495
3496     if (myMesh->_is_nil())
3497     {
3498       theResIds = aDialogIds;
3499       return;
3500     }
3501
3502     // filter ids
3503     SMESH::Predicate_ptr aPred = myFilter[ theType ]->GetPredicate();
3504     myFilter[ theType ]->SetMesh(myMesh);
3505     QList<int>::const_iterator anIter;
3506     for (anIter = aDialogIds.begin(); anIter != aDialogIds.end(); ++ anIter)
3507       if (aPred->IsSatisfy(*anIter))
3508         theResIds.append(*anIter);
3509   }
3510   // set ids to the dialog
3511   if (myInitSourceWgOnApply || aSourceId == Dialog)
3512     setIdsToWg(mySourceWg, theResIds);
3513 }
3514
3515 //=======================================================================
3516 // name    : SMESHGUI_FilterDlg::filterSelectionSource
3517 // Purpose : Filter source selection
3518 //=======================================================================
3519 void SMESHGUI_FilterDlg::filterSelectionSource (const int theType,
3520                                                 QList<int>& theResIds)
3521 {
3522   theResIds.clear();
3523   if (myMesh->_is_nil() || mySelectionMgr == 0)
3524     return;
3525
3526   // Create map of entities to be filtered
3527   TColStd_MapOfInteger aToBeFiltered;
3528   SALOME_DataMapIteratorOfDataMapOfIOMapOfInteger anIter(myIObjects);
3529
3530   for ( ; anIter.More(); anIter.Next())
3531   {
3532     // process sub mesh
3533     SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIter.Key());
3534     if (!aSubMesh->_is_nil())
3535     {
3536       if (aSubMesh->GetFather()->GetId() == myMesh->GetId())
3537       {
3538         SMESH::long_array_var anIds =
3539           theType == SMESH::NODE ? aSubMesh->GetNodesId() : aSubMesh->GetElementsId();
3540         for (int i = 0, n = anIds->length(); i < n; i++)
3541           aToBeFiltered.Add(anIds[ i ]);
3542       }
3543     }
3544
3545     // process group
3546     SMESH::SMESH_GroupBase_var aGroup =
3547       SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIter.Key());
3548     if (!aGroup->_is_nil())
3549     {
3550       if (aGroup->GetType() == theType && aGroup->GetMesh()->GetId() == myMesh->GetId())
3551       {
3552         SMESH::long_array_var anIds = aGroup->GetListOfID();
3553         for (int i = 0, n = anIds->length(); i < n; i++)
3554           aToBeFiltered.Add(anIds[ i ]);
3555       }
3556     }
3557
3558     // process mesh
3559     SMESH::SMESH_Mesh_var aMeshPtr = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIter.Key());
3560     if (!aMeshPtr->_is_nil() && aMeshPtr->GetId() == myMesh->GetId())
3561     {
3562       const TColStd_IndexedMapOfInteger& aSelMap = anIter.Value();
3563
3564       if (aSelMap.Extent() > 0)
3565       {
3566         if(SMESH::FindActorByEntry(anIter.Key()->getEntry()))
3567         {
3568           for (int i = 1; i <= aSelMap.Extent(); i++)
3569             aToBeFiltered.Add(aSelMap(i));
3570         }
3571       }
3572     }
3573   }
3574
3575   // Filter entities
3576   SMESH::Predicate_ptr aPred = myFilter[ theType ]->GetPredicate();
3577   myFilter[ theType ]->SetMesh(myMesh);
3578   TColStd_MapIteratorOfMapOfInteger aResIter(aToBeFiltered);
3579   for ( ; aResIter.More(); aResIter.Next())
3580     if (aPred->IsSatisfy(aResIter.Key()))
3581       theResIds.append(aResIter.Key());
3582 }
3583
3584 //=======================================================================
3585 // name    : SMESHGUI_FilterDlg::selectInViewer
3586 // Purpose : Select given entities in viewer
3587 //=======================================================================
3588 void SMESHGUI_FilterDlg::selectInViewer (const int theType, const QList<int>& theIds)
3589 {
3590   if (mySelectionMgr == 0 || myMesh->_is_nil())
3591     return;
3592
3593   mySelectionMgr->clearFilters();
3594
3595   // Set new selection mode if necessary
3596   Selection_Mode aSelMode = getSelMode(theType);
3597   SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
3598   if ( aViewWindow && aViewWindow->SelectionMode()!=aSelMode) {
3599     mySelectionMgr->clearSelected();
3600     mySelectionMgr->clearFilters();
3601     SMESH::SetPointRepresentation( aSelMode == NodeSelection );
3602     aViewWindow->SetSelectionMode(aSelMode);
3603   }
3604
3605   // Clear selection
3606   SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
3607   if (!anActor || !anActor->hasIO())
3608     return;
3609
3610   Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
3611   //mySelectionMgr->clearSelected();
3612   //mySelectionMgr->AddIObject(anIO, false);
3613   SALOME_ListIO aList;
3614   aList.Append(anIO);
3615   mySelectionMgr->setSelectedObjects(aList, false);
3616
3617   // Remove filter corresponding to the current type from viewer
3618   int aType = myTable->GetType();
3619   int aFilterId = SMESH::UnknownFilter;
3620   if      (aType == SMESH::EDGE  ) aFilterId = SMESH::EdgeFilter;
3621   else if (aType == SMESH::FACE  ) aFilterId = SMESH::FaceFilter;
3622   else if (aType == SMESH::VOLUME) aFilterId = SMESH::VolumeFilter;
3623   Handle(VTKViewer_Filter) aFilter = SMESH::GetFilter(aFilterId);
3624   SMESH::RemoveFilter(aFilterId);
3625
3626   // get vtk ids
3627   TColStd_MapOfInteger aMap;
3628   QList<int>::const_iterator anIter;
3629   for (anIter = theIds.begin(); anIter != theIds.end(); ++anIter) {
3630     aMap.Add(*anIter);
3631   }
3632
3633   // Set new selection
3634   mySelector->AddOrRemoveIndex(anIO, aMap, false);
3635   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
3636     aViewWindow->highlight( anIO, true, true );
3637
3638   // insert previously stored filter in viewer if necessary
3639   if (!aFilter.IsNull())
3640     SMESH::SetFilter(aFilter);
3641 }
3642
3643 //=======================================================================
3644 // name    : SMESHGUI_FilterDlg::createCriterion
3645 // Purpose : Create criterion structure with default values
3646 //=======================================================================
3647 SMESH::Filter::Criterion SMESHGUI_FilterDlg::createCriterion()
3648 {
3649    SMESH::Filter::Criterion aCriterion;
3650
3651   aCriterion.Type          = SMESH::FT_Undefined;
3652   aCriterion.Compare       = SMESH::FT_Undefined;
3653   aCriterion.Threshold     = 0;
3654   aCriterion.UnaryOp       = SMESH::FT_Undefined;
3655   aCriterion.BinaryOp      = SMESH::FT_Undefined;
3656   aCriterion.ThresholdStr  = "";
3657   aCriterion.ThresholdID   = "";
3658   aCriterion.TypeOfElement = SMESH::ALL;
3659
3660   return aCriterion;
3661 }
3662
3663 //=======================================================================
3664 // name    : SMESHGUI_FilterDlg::onSelectionDone
3665 // Purpose : SLOT called when selection changed.
3666 //           If current cell corresponds to the threshold value of
3667 //           BelongToGeom criterion name of selected object is set in this cell
3668 //=======================================================================
3669 void SMESHGUI_FilterDlg::onSelectionDone()
3670 {
3671   const SALOME_ListIO& aList = mySelector->StoredIObjects();
3672   if ( myMesh->_is_nil() && aList.Extent()>0 )
3673   {
3674     myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(aList.First());
3675     if ( !(myMesh->_is_nil()) ) {
3676       myButtons[BTN_OK]->setEnabled(true);
3677       myButtons[BTN_Apply]->setEnabled(true);
3678     }
3679   }
3680   int aRow, aCol;
3681   if (aList.Extent() != 1 || !myTable->CurrentCell(aRow, aCol))
3682     return;
3683
3684   const int type = myTable->GetCriterionType(aRow);
3685   QList<int> types; 
3686   types << SMESH::FT_BelongToGeom      << SMESH::FT_BelongToPlane 
3687         << SMESH::FT_BelongToCylinder  << SMESH::FT_BelongToGenSurface
3688         << SMESH::FT_LyingOnGeom       << SMESH::FT_CoplanarFaces
3689         << SMESH::FT_ConnectedElements << SMESH::FT_BelongToMeshGroup;
3690   if ( !types.contains( type ))
3691     return;
3692
3693   Handle(SALOME_InteractiveObject) anIO = aList.First();
3694   switch ( type )
3695   {
3696   case SMESH::FT_CoplanarFaces: // get ID of a selected mesh face
3697     {
3698       QString aString;
3699       int nbElems = SMESH::GetNameOfSelectedElements(mySelector, anIO, aString);
3700       if (nbElems == 1)
3701         myTable->SetThreshold(aRow, aString);
3702       break;
3703     }
3704   case SMESH::FT_ConnectedElements: // get either VERTEX or a node ID
3705     {
3706       GEOM::GEOM_Object_var anObj = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
3707       if (!anObj->_is_nil())
3708       {
3709         myTable->SetThreshold(aRow, GEOMBase::GetName(anObj));
3710         myTable->SetID       (aRow, anIO->getEntry());
3711       }
3712       else
3713       {
3714         QString aString;
3715         int nbElems = SMESH::GetNameOfSelectedElements(mySelector, anIO, aString);
3716         if (nbElems == 1)
3717           myTable->SetThreshold(aRow, aString);
3718       }
3719       break;
3720     }
3721   case SMESH::FT_BelongToMeshGroup: // get a group Name and Entry
3722     {
3723       SMESH::SMESH_GroupBase_var grp = SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIO);
3724       if ( !grp->_is_nil() )
3725       {
3726         if ( !myMesh->_is_nil() )
3727         {
3728           SMESH::SMESH_Mesh_var mesh = grp->GetMesh();
3729           if ( ! myMesh->_is_equivalent( mesh ))
3730             return;
3731         }
3732         if ( !myGroup->_is_nil() && myGroup->IsInDependency( grp ))
3733           return; // avoid cyclic dependencies between Groups on Filter
3734
3735         myTable->SetThreshold(aRow, SMESH::toQStr( grp->GetName() ));
3736         myTable->SetID       (aRow, anIO->getEntry() );
3737       }
3738     }
3739   default: // get a GEOM object
3740     {
3741       GEOM::GEOM_Object_var anObj = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
3742       if (!anObj->_is_nil())
3743       {
3744         myTable->SetThreshold(aRow, GEOMBase::GetName(anObj));
3745         myTable->SetID       (aRow, anIO->getEntry());
3746       }
3747     }
3748   }
3749 }
3750
3751
3752 //=======================================================================
3753 // name    : SMESHGUI_FilterDlg::onCriterionChanged
3754 // Purpose : SLOT called when cretarion of current row changed. Update selection
3755 //=======================================================================
3756 void SMESHGUI_FilterDlg::onCriterionChanged (const int, const int)
3757 {
3758   updateSelection();
3759 }
3760
3761 //=======================================================================
3762 // name    : SMESHGUI_FilterDlg::onThresholdChanged
3763 // Purpose : SLOT called when a threshold value is changed by the user and
3764 //           not by myTable->SetThreshold()
3765 //=======================================================================
3766 void SMESHGUI_FilterDlg::onThresholdChanged( const int row, const int type )
3767 {
3768   if ( myTable->GetCriterionType( row, type ) == SMESH::FT_ConnectedElements )
3769   {
3770     // to differ the text entered by the user from that got from selection
3771     myTable->SetID( row, "", type );
3772   }
3773 }
3774
3775 //=======================================================================
3776 // name    : SMESHGUI_FilterDlg::onCurrentChanged
3777 // Purpose : SLOT called when current row changed. Update selection
3778 //=======================================================================
3779 void SMESHGUI_FilterDlg::onCurrentChanged (int, int)
3780 {
3781   updateSelection();
3782 }
3783
3784 //=======================================================================
3785 // name    : SMESHGUI_FilterDlg::updateSelection
3786 // Purpose : UpdateSelection in accordance with current row
3787 //=======================================================================
3788 void SMESHGUI_FilterDlg::updateSelection()
3789 {
3790   if (mySelectionMgr == 0)
3791     return;
3792
3793 //   TColStd_MapOfInteger allTypes;
3794 //   for( int i=0; i<10; i++ )
3795 //     allTypes.Add( i );
3796   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( mySMESHGUI->application()->activeStudy() );
3797   if( !aStudy )
3798     return;
3799
3800   restoreSelMode();
3801
3802   mySelectionMgr->clearFilters();
3803
3804   int aRow, aCol;
3805   
3806   bool isCurrentCell = myTable->CurrentCell(aRow, aCol);
3807   int aCriterionType = myTable->GetCriterionType(aRow);
3808   if ( isCurrentCell &&
3809       (aCriterionType == SMESH::FT_BelongToGeom ||
3810        aCriterionType == SMESH::FT_BelongToPlane ||
3811        aCriterionType == SMESH::FT_BelongToCylinder ||
3812        aCriterionType == SMESH::FT_BelongToGenSurface ||
3813        aCriterionType == SMESH::FT_LyingOnGeom))
3814   {
3815     if (aCriterionType == SMESH::FT_BelongToGeom ||
3816         aCriterionType == SMESH::FT_BelongToGenSurface ||
3817         aCriterionType == SMESH::FT_LyingOnGeom) {
3818
3819       mySelectionMgr->installFilter(new GEOM_SelectionFilter( aStudy, true ));
3820
3821     } else if (aCriterionType == SMESH::FT_BelongToPlane) {
3822       mySelectionMgr->installFilter(new GEOM_FaceFilter( aStudy, StdSelect_Plane ) );
3823
3824     } else if (aCriterionType == SMESH::FT_BelongToCylinder) {
3825       mySelectionMgr->installFilter(new GEOM_FaceFilter( aStudy, StdSelect_Cylinder ) );
3826     }
3827     myIsSelectionChanged = true;
3828
3829   }
3830   else if ( aCriterionType == SMESH::FT_BelongToMeshGroup )
3831   {
3832     SMESH_TypeFilter* typeFilter = 0;
3833     switch ( myTable->GetType() )
3834     {
3835     case SMESH::NODE   : typeFilter = new SMESH_TypeFilter( SMESH::GROUP_NODE   ); break;
3836     case SMESH::ELEM0D : typeFilter = new SMESH_TypeFilter( SMESH::GROUP_0D     ); break;
3837     case SMESH::BALL   : typeFilter = new SMESH_TypeFilter( SMESH::GROUP_BALL   ); break;
3838     case SMESH::EDGE   : typeFilter = new SMESH_TypeFilter( SMESH::GROUP_EDGE   ); break;
3839     case SMESH::FACE   : typeFilter = new SMESH_TypeFilter( SMESH::GROUP_FACE   ); break;
3840     case SMESH::VOLUME : typeFilter = new SMESH_TypeFilter( SMESH::GROUP_VOLUME ); break;
3841     case SMESH::ALL    : typeFilter = new SMESH_TypeFilter( SMESH::GROUP        ); break;
3842     default            : typeFilter = 0;
3843     }
3844     if ( typeFilter )
3845       mySelectionMgr->installFilter( typeFilter );
3846   }
3847   else if ( aCriterionType == SMESH::FT_ConnectedElements )
3848   {
3849     QList<SUIT_SelectionFilter*> fList;
3850     fList.push_back( new SMESH_NumberFilter( "GEOM", TopAbs_SHAPE, 1, TopAbs_VERTEX ));
3851     fList.push_back( new SMESH_TypeFilter( SMESH::IDSOURCE ));
3852     mySelectionMgr->installFilter
3853       ( new SMESH_LogicalFilter( fList, SMESH_LogicalFilter::LO_OR,
3854                                  !mySelectionMgr->autoDeleteFilter() ));
3855
3856     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
3857     {
3858       mySelModeToRestore = aViewWindow->SelectionMode();
3859       aViewWindow->SetSelectionMode( NodeSelection );
3860       myToRestoreSelMode = ( mySelModeToRestore != NodeSelection );
3861       if ( myToRestoreSelMode )
3862         SMESH::SetPointRepresentation( true );
3863     }
3864   }
3865   else
3866   {
3867     mySelector->SetSelectionMode( getSelMode( myTable->GetType() ));
3868
3869     if (myIsSelectionChanged) {
3870       // mySelectionMgr->installFilter( new GEOM_TypeFilter( aStudy, -1 ) ); // This filter deactivates selection
3871       // Impossible to select any object in the OB on the second opening of FilterDlg
3872     }
3873   }
3874 }
3875
3876 //=================================================================================
3877 // function : keyPressEvent()
3878 // purpose  :
3879 //=================================================================================
3880 void SMESHGUI_FilterDlg::keyPressEvent( QKeyEvent* e )
3881 {
3882   QDialog::keyPressEvent( e );
3883   if ( e->isAccepted() )
3884     return;
3885
3886   if ( e->key() == Qt::Key_F1 ) {
3887     e->accept();
3888     onHelp();
3889   }
3890 }
3891
3892 //================================================================================
3893 /*!
3894  * \brief Restores a selection mode if it was changed to set up some criterion
3895  */
3896 //================================================================================
3897
3898 void SMESHGUI_FilterDlg::restoreSelMode()
3899 {
3900   if ( myToRestoreSelMode )
3901   {
3902     SMESH::SetPointRepresentation( mySelModeToRestore == NodeSelection );
3903
3904     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
3905       aViewWindow->SetSelectionMode( mySelModeToRestore );
3906
3907     myToRestoreSelMode = false;
3908   }
3909 }