Salome HOME
df5618fc2b223224094f732b318fe3f6af22c96a
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_FilterDlg.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
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 <QtxColorButton.h>
55
56 #include <LightApp_Application.h>
57 #include <LightApp_SelectionMgr.h>
58 #include <SalomeApp_Tools.h>
59 #include <SalomeApp_Study.h>
60 #include <SalomeApp_IntSpinBox.h>
61 #include <SalomeApp_DoubleSpinBox.h>
62
63 #include <SALOME_ListIO.hxx>
64 #include <SALOME_ListIteratorOfListIO.hxx>
65 #include <SALOME_DataMapIteratorOfDataMapOfIOMapOfInteger.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_BelongToGeom ||
1139              aCriterion == SMESH::FT_BelongToPlane ||
1140              aCriterion == SMESH::FT_BelongToCylinder ||
1141              aCriterion == SMESH::FT_BelongToGenSurface ||
1142              aCriterion == SMESH::FT_ElemGeomType ||
1143              aCriterion == SMESH::FT_EntityType ||
1144              aCriterion == SMESH::FT_CoplanarFaces ||
1145              aCriterion == SMESH::FT_LyingOnGeom ||
1146              aCriterion == SMESH::FT_ConnectedElements )
1147     {
1148       if (aTable->text(i, 2).isEmpty())
1149         errMsg = tr( "ERROR" );
1150     }
1151     else
1152     {
1153       bool aRes = false;
1154       bool isSignalsBlocked = aTable->signalsBlocked();
1155       aTable->blockSignals(true);
1156       double  aThreshold = (int)aTable->text(i, 2).toDouble(&aRes);
1157       aTable->blockSignals(isSignalsBlocked);
1158
1159       if (!aRes && aTable->isEditable(i, 2))
1160         errMsg = tr( "ERROR" );
1161       else if (aType == SMESH::EDGE &&
1162                GetCriterionType(i, aType) == SMESH::FT_MultiConnection &&
1163                aThreshold == 1)
1164         errMsg = tr( "MULTIEDGES_ERROR" );
1165     }
1166
1167     if (!errMsg.isEmpty()) {
1168       if (theMess)
1169         SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"), errMsg );
1170       return false;
1171     }
1172
1173     QTableWidgetItem* anItem = aTable->item(i, 0);
1174     if (myAddWidgets.contains(anItem) && !myAddWidgets[ anItem ]->IsValid())
1175       return false;
1176   }
1177
1178   return true;
1179 }
1180
1181 //=======================================================================
1182 // name    : SMESHGUI_FilterTable::SetValidity
1183 // Purpose : Set validity of the table
1184 //=======================================================================
1185 void SMESHGUI_FilterTable::SetValidity (const bool isValid)
1186 {
1187   myIsValid = isValid;
1188 }
1189
1190 //=======================================================================
1191 // name    : SMESHGUI_FilterTable::GetType
1192 // Purpose : Get current entity type
1193 //=======================================================================
1194 int SMESHGUI_FilterTable::GetType() const
1195 {
1196   return myEntityType;
1197 }
1198
1199 //=======================================================================
1200 // name    : SMESHGUI_FilterTable::SetType
1201 // Purpose : Set current entity type
1202 //=======================================================================
1203 void SMESHGUI_FilterTable::SetType (const int type)
1204 {
1205   myEntityTypeGrp->button(type)->setChecked(true);
1206   onEntityType(type);
1207 }
1208
1209 //=======================================================================
1210 // name    : SMESHGUI_FilterTable::RestorePreviousEntityType
1211 // Purpose : Restore previous entity type
1212 //=======================================================================
1213 void SMESHGUI_FilterTable::RestorePreviousEntityType()
1214 {
1215   SetType(myEntityType);
1216 }
1217
1218 //=======================================================================
1219 // name    : SMESHGUI_FilterTable::GetCriterionType
1220 // Purpose : Get type of criterion from specified row (corresponding enums in h-file)
1221 //=======================================================================
1222 int SMESHGUI_FilterTable::GetCriterionType (const int theRow, const int theType) const
1223 {
1224   int aType = theType == -1 ? GetType() : theType;
1225   Table* aTable = myTables[ aType ];
1226   ComboItem* anItem = (ComboItem*)aTable->item(theRow, 0);
1227   return anItem != 0 ? anItem->value() : SMESH::FT_Undefined;
1228 }
1229
1230 //=======================================================================
1231 // name    : SMESHGUI_FilterTable::GetCriterion
1232 // Purpose : Get parameters of criterion from specified row
1233 //=======================================================================
1234 void SMESHGUI_FilterTable::GetCriterion (const int                 theRow,
1235                                          SMESH::Filter::Criterion& theCriterion,
1236                                          const int                 theEntityType) const
1237 {
1238   int aType = theEntityType == -1 ? GetType() : theEntityType;
1239   Table* aTable = myTables[ aType ];
1240
1241   theCriterion.Type = ((ComboItem*)aTable->item(theRow, 0))->value();
1242   theCriterion.UnaryOp = ((CheckItem*)aTable->item(theRow, 3))->checked() ? SMESH::FT_LogicalNOT : SMESH::FT_Undefined;
1243   theCriterion.BinaryOp = theRow != aTable->rowCount() - 1 ?
1244     ((ComboItem*)aTable->item(theRow, 4))->value() : SMESH::FT_Undefined;
1245   theCriterion.TypeOfElement = (SMESH::ElementType)aType;
1246
1247   int aCriterionType = GetCriterionType(theRow, aType);
1248
1249   if ( aCriterionType == SMESH::FT_GroupColor )
1250   {
1251     QtxColorButton* clrBtn = qobject_cast<QtxColorButton*>(aTable->cellWidget(theRow, 2));
1252     if ( clrBtn )
1253     {
1254       Kernel_Utils::Localizer loc;
1255       const QColor qClr = clrBtn->color();
1256       QString clrStr = QString( "%1;%2;%3" ).
1257         arg( qClr.red()/256. ).arg( qClr.green()/256. ).arg( qClr.blue()/256. );
1258       theCriterion.ThresholdStr = clrStr.toLatin1().constData();
1259     }
1260   }
1261   else if ( aCriterionType == SMESH::FT_ElemGeomType ||
1262             aCriterionType == SMESH::FT_EntityType )
1263   {
1264     theCriterion.Threshold = (double)((ComboItem*)aTable->item(theRow, 2))->value();
1265   }
1266   else if ( aCriterionType == SMESH::FT_CoplanarFaces )
1267   {
1268     theCriterion.ThresholdID = aTable->text(theRow, 2).toLatin1().constData();
1269   }
1270   else if ( aCriterionType == SMESH::FT_ConnectedElements )
1271   {
1272     QString id = aTable->text(theRow, 5);
1273     if ( !id.isEmpty() ) // shape ID
1274     {
1275       theCriterion.ThresholdID = id.toLatin1().constData();
1276     }
1277     else
1278     {
1279       QString text = aTable->text(theRow, 2).trimmed();
1280       QString workText = text;
1281       for ( char c = '0'; c <= '9'; ++c )
1282         workText.remove( c );
1283
1284       if ( workText.isEmpty() ) // node ID
1285         theCriterion.Threshold = text.toDouble();
1286       else // point coordinates
1287         theCriterion.ThresholdStr = text.toLatin1().constData();
1288     }
1289   }
1290   else if ( aCriterionType != SMESH::FT_RangeOfIds &&
1291             aCriterionType != SMESH::FT_BelongToGeom &&
1292             aCriterionType != SMESH::FT_BelongToPlane &&
1293             aCriterionType != SMESH::FT_BelongToCylinder &&
1294             aCriterionType != SMESH::FT_BelongToGenSurface &&
1295             aCriterionType != SMESH::FT_LyingOnGeom )
1296   {
1297     theCriterion.Compare = ((ComboItem*)aTable->item(theRow, 1))->value();
1298     theCriterion.Threshold = aTable->item(theRow, 2)->text().toDouble();
1299   }
1300   else
1301   {
1302     theCriterion.ThresholdStr = aTable->text(theRow, 2).toLatin1().constData();
1303     if ( aCriterionType != SMESH::FT_RangeOfIds )
1304       theCriterion.ThresholdID = aTable->text( theRow, 5 ).toLatin1().constData();
1305   }
1306
1307   QTableWidgetItem* anItem = aTable->item(theRow, 0);
1308   if (myAddWidgets.contains(anItem))
1309     theCriterion.Tolerance = myAddWidgets[ anItem ]->GetDouble(AdditionalWidget::Tolerance);
1310 }
1311
1312 //=======================================================================
1313 // name    : SMESHGUI_FilterTable::SetCriterion
1314 // Purpose : Set parameters of criterion of specified row
1315 //=======================================================================
1316 void SMESHGUI_FilterTable::SetCriterion (const int                       theRow,
1317                                          const SMESH::Filter::Criterion& theCriterion,
1318                                          const int                       theEntityType)
1319 {
1320   int aType = theEntityType == -1 ? GetType() : theEntityType;
1321
1322   Table* aTable = myTables[ aType ];
1323
1324   if (theRow > aTable->rowCount() - 1)
1325     return;
1326
1327   ((ComboItem*)aTable->item(theRow, 0))->setValue(theCriterion.Type);
1328   onCriterionChanged(theRow, 0, aType);
1329   if ( theCriterion.Compare == SMESH::FT_Undefined )
1330     ((ComboItem*)aTable->item(theRow, 1))->setValue( SMESH::FT_EqualTo );
1331   else
1332     ((ComboItem*)aTable->item(theRow, 1))->setValue(theCriterion.Compare);
1333   ((CheckItem*)aTable->item(theRow, 3))->setChecked(theCriterion.UnaryOp == SMESH::FT_LogicalNOT);
1334
1335   if (theCriterion.BinaryOp != SMESH::FT_Undefined)
1336   {
1337     if (!aTable->isEditable(theRow, 4))
1338       aTable->setItem(theRow, 4, getBinaryItem());
1339     ((ComboItem*)aTable->item(theRow, 4))->setValue(theCriterion.BinaryOp);
1340   }
1341   else
1342     aTable->setEditable(false, theRow, 4);
1343
1344   if (theCriterion.Type == SMESH::FT_GroupColor )
1345   {
1346     QtxColorButton* clrBtn = qobject_cast<QtxColorButton*>(aTable->cellWidget(theRow, 2));
1347     if ( clrBtn )
1348     {
1349       QColor qClr;
1350       QString clrStr( theCriterion.ThresholdStr );
1351       QStringList clrVals = clrStr.split( ";" );
1352       if ( clrVals.count() > 2 )
1353         qClr.setRgb( (int)256*clrVals[0].toDouble(),
1354                      (int)256*clrVals[1].toDouble(),
1355                      (int)256*clrVals[2].toDouble() );
1356       clrBtn->setColor( qClr );
1357     }
1358   }
1359   else if (theCriterion.Type == SMESH::FT_ElemGeomType || 
1360            theCriterion.Type == SMESH::FT_EntityType )
1361   {
1362     ComboItem* typeBox = (ComboItem*)aTable->item(theRow, 2);
1363     typeBox->setValue( (int)(theCriterion.Threshold + 0.5) );
1364   }
1365   else if (theCriterion.Type == SMESH::FT_CoplanarFaces )
1366   {
1367     aTable->item( theRow, 2 )->setText( QString( theCriterion.ThresholdID ) );
1368   }
1369   else if (theCriterion.Type == SMESH::FT_ConnectedElements )
1370   {
1371     if ( strlen( theCriterion.ThresholdID ) > 0 ) // shape ID -> name
1372     {
1373       _PTR(SObject) sobj =
1374         SMESH::GetActiveStudyDocument()->FindObjectID( theCriterion.ThresholdID.in() );
1375       if ( !sobj )
1376         aTable->item( theRow, 2 )->setText( QString( theCriterion.ThresholdID ) );
1377       else
1378         aTable->item( theRow, 2 )->setText( QString( sobj->GetName().c_str() ));
1379     }
1380     else if ( strlen( theCriterion.ThresholdStr ) > 0 ) // point coords
1381     {
1382       aTable->item( theRow, 2 )->setText( QString( theCriterion.ThresholdStr ));
1383     }
1384     else // node ID
1385     {
1386       aTable->item( theRow, 2 )->setText( QString("%1").arg((int) theCriterion.Threshold ));
1387     }
1388   }
1389   else if (theCriterion.Type != SMESH::FT_RangeOfIds &&
1390            theCriterion.Type != SMESH::FT_BelongToGeom &&
1391            theCriterion.Type != SMESH::FT_BelongToPlane &&
1392            theCriterion.Type != SMESH::FT_BelongToCylinder &&
1393            theCriterion.Type != SMESH::FT_BelongToGenSurface &&
1394            theCriterion.Type != SMESH::FT_LyingOnGeom &&
1395            theCriterion.Type != SMESH::FT_FreeBorders &&
1396            theCriterion.Type != SMESH::FT_FreeEdges &&
1397            theCriterion.Type != SMESH::FT_FreeNodes &&
1398            theCriterion.Type != SMESH::FT_FreeFaces &&
1399            theCriterion.Type != SMESH::FT_BadOrientedVolume &&
1400            theCriterion.Type != SMESH::FT_BareBorderFace &&
1401            theCriterion.Type != SMESH::FT_BareBorderVolume &&
1402            theCriterion.Type != SMESH::FT_OverConstrainedFace &&
1403            theCriterion.Type != SMESH::FT_OverConstrainedVolume &&
1404            theCriterion.Type != SMESH::FT_LinearOrQuadratic)
1405   {
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   
1574   if (!myAddWidgets.contains(anItem))
1575   {
1576     myAddWidgets[ anItem ] = new AdditionalWidget(myWgStack);
1577     myWgStack->addWidget(myAddWidgets[ anItem ]);
1578   }
1579
1580   myWgStack->setCurrentWidget(myAddWidgets[ anItem ]);
1581   myAddWidgets[ anItem ]->SetPrecision( AdditionalWidget::Tolerance, getPrecision( aCriterion ) );
1582   myWgStack->setEnabled(toEnable);
1583 }
1584
1585 const char* SMESHGUI_FilterTable::getPrecision( const int aType )
1586 {
1587   const char* retval = 0;
1588   switch ( aType ) {
1589   case SMESH::FT_AspectRatio:
1590   case SMESH::FT_AspectRatio3D:
1591   case SMESH::FT_Taper:
1592     retval = "parametric_precision"; break;
1593   case SMESH::FT_Warping:
1594   case SMESH::FT_MinimumAngle:
1595   case SMESH::FT_Skew:
1596   case SMESH::FT_CoplanarFaces:
1597     retval = "angle_precision"; break;
1598   case SMESH::FT_Area:
1599     retval = "area_precision"; break;
1600   case SMESH::FT_BelongToGeom:
1601   case SMESH::FT_BelongToPlane:
1602   case SMESH::FT_BelongToCylinder:
1603   case SMESH::FT_BelongToGenSurface:
1604   case SMESH::FT_LyingOnGeom:
1605   case SMESH::FT_EqualNodes:
1606     retval = "len_tol_precision"; break;
1607   case SMESH::FT_Length:
1608   case SMESH::FT_Length2D:
1609   case SMESH::FT_MaxElementLength2D:
1610   case SMESH::FT_MaxElementLength3D:
1611     retval = "length_precision"; break;
1612   case SMESH::FT_Volume3D:
1613     retval = "vol_precision"; break;
1614   default:
1615     break;
1616   }
1617   return retval;
1618 }
1619
1620 //=======================================================================
1621 // name    : SMESHGUI_FilterTable::removeAdditionalWidget
1622 // Purpose : Remove widgets containing additional parameters from widget
1623 //           stack and internal map
1624 //=======================================================================
1625 void SMESHGUI_FilterTable::removeAdditionalWidget (QTableWidget* theTable, const int theRow)
1626 {
1627   QTableWidgetItem* anItem = theTable->item(theRow, 0);
1628   if (myAddWidgets.contains(anItem))
1629   {
1630     myWgStack->removeWidget(myAddWidgets[ anItem ]);
1631     myAddWidgets[ anItem ]->setParent(0);
1632     delete myAddWidgets[ anItem ];
1633     myAddWidgets.remove(anItem);
1634   }
1635 }
1636
1637 //=======================================================================
1638 // name    : SMESHGUI_FilterTable::onClearBtn
1639 // Purpose : SLOT. Called then "Clear" button pressed.
1640 //           Removes all strings from table
1641 //=======================================================================
1642 void SMESHGUI_FilterTable::onClearBtn()
1643 {
1644   Table* aTable = myTables[ GetType() ];
1645
1646   if (aTable->rowCount() == 0)
1647     return;
1648
1649   while (aTable->rowCount() > 0)
1650   {
1651     removeAdditionalWidget(aTable, 0);
1652     aTable->removeRow(0);
1653   }
1654
1655   updateBtnState();
1656 }
1657
1658 //=======================================================================
1659 // name    : SMESHGUI_FilterTable::onCurrentChanged()
1660 // Purpose : SLOT. Called when current cell changed
1661 //=======================================================================
1662 void SMESHGUI_FilterTable::onCurrentChanged (int theRow, int theCol)
1663 {
1664   if( !myIsLocked )
1665     updateAdditionalWidget();
1666   emit CurrentChanged(theRow, theCol);
1667 }
1668
1669 //=======================================================================
1670 // name    : geomTypes
1671 // Purpose : returns available geometry types of elements
1672 //=======================================================================
1673 static QList<int> geomTypes( const int theType )
1674 {
1675   QList<int> typeIds;
1676   if ( theType == SMESH::NODE )
1677     typeIds.append( SMESH::Geom_POINT );
1678   if ( theType == SMESH::ALL || theType == SMESH::EDGE )
1679     typeIds.append( SMESH::Geom_EDGE );
1680   if ( theType == SMESH::ALL || theType == SMESH::FACE )
1681   {
1682     typeIds.append( SMESH::Geom_TRIANGLE );
1683     typeIds.append( SMESH::Geom_QUADRANGLE );
1684     typeIds.append( SMESH::Geom_POLYGON );
1685   }
1686   if ( theType == SMESH::ALL || theType == SMESH::VOLUME )
1687   {
1688     typeIds.append( SMESH::Geom_TETRA );
1689     typeIds.append( SMESH::Geom_PYRAMID );
1690     typeIds.append( SMESH::Geom_HEXA );
1691     typeIds.append( SMESH::Geom_PENTA );
1692     typeIds.append( SMESH::Geom_HEXAGONAL_PRISM );
1693     typeIds.append( SMESH::Geom_POLYHEDRA );
1694   }
1695   if ( theType == SMESH::ALL || theType == SMESH::ELEM0D )
1696   {
1697     typeIds.append( SMESH::Geom_POINT );
1698   }
1699   if ( theType == SMESH::ALL || theType == SMESH::BALL )
1700   {
1701     typeIds.append( SMESH::Geom_BALL );
1702   }
1703   return typeIds;
1704 }
1705
1706 //=======================================================================
1707 // name    : entityTypes
1708 // Purpose : returns available entity types of elements
1709 //=======================================================================
1710
1711 static QList<int> entityTypes( const int theType )
1712 {
1713   QList<int> typeIds;
1714
1715   switch ( theType )
1716   {
1717   case SMESH::NODE:
1718     typeIds.append( SMDSEntity_Node );
1719   case SMESH::EDGE:
1720     typeIds.append( SMDSEntity_Edge );
1721     typeIds.append( SMDSEntity_Quad_Edge );
1722     break;
1723   case SMESH::FACE:
1724     typeIds.append( SMDSEntity_Triangle );
1725     typeIds.append( SMDSEntity_Quad_Triangle );
1726     typeIds.append( SMDSEntity_BiQuad_Triangle );
1727     typeIds.append( SMDSEntity_Quadrangle );
1728     typeIds.append( SMDSEntity_Quad_Quadrangle );
1729     typeIds.append( SMDSEntity_BiQuad_Quadrangle );
1730     typeIds.append( SMDSEntity_Polygon );
1731     //typeIds.append( SMDSEntity_Quad_Polygon );
1732     break;
1733   case SMESH::VOLUME:
1734     typeIds.append( SMDSEntity_Tetra );
1735     typeIds.append( SMDSEntity_Quad_Tetra );
1736     typeIds.append( SMDSEntity_Pyramid );
1737     typeIds.append( SMDSEntity_Quad_Pyramid );
1738     typeIds.append( SMDSEntity_Hexa );
1739     typeIds.append( SMDSEntity_Quad_Hexa );
1740     typeIds.append( SMDSEntity_TriQuad_Hexa );
1741     typeIds.append( SMDSEntity_Penta );
1742     typeIds.append( SMDSEntity_Quad_Penta );
1743     typeIds.append( SMDSEntity_Hexagonal_Prism );
1744     typeIds.append( SMDSEntity_Polyhedra );
1745     //typeIds.append( SMDSEntity_Quad_Polyhedra );
1746     break;
1747   case SMESH::ELEM0D:
1748     typeIds.append( SMDSEntity_0D );
1749     break;
1750   case SMESH::BALL:
1751     typeIds.append( SMDSEntity_Ball );
1752     break;
1753   }
1754   return typeIds;
1755 }
1756
1757 //=======================================================================
1758 // name    : SMESHGUI_FilterTable::onCriterionChanged()
1759 // Purpose : Provides reaction on change of criterion
1760 //=======================================================================
1761
1762 void SMESHGUI_FilterTable::onCriterionChanged (const int row, const int col, const int entityType)
1763 {
1764   int aType = entityType == -1 ? GetType() : entityType;
1765   Table* aTable = myTables[ aType ];
1766   ComboItem* aCompareItem = (ComboItem*)aTable->item(row, 1);
1767
1768   // find out type of a existing threshould table item
1769   QtxColorButton* clrBtn = qobject_cast<QtxColorButton*>(aTable->cellWidget(row, 2));
1770   bool isComboItem       = false;
1771   bool isIntSpinItem     = false;
1772   bool isDoubleSpinItem  = false;
1773   if ( QTableWidgetItem* aTableItem = aTable->item(row, 2) )
1774   {
1775     int aTableType   = aTableItem->type();
1776     isComboItem      = ( aTableType == ComboItem::Type() );
1777     isIntSpinItem    = ( aTableType == IntSpinItem::Type() );
1778     isDoubleSpinItem = ( aTableType == DoubleSpinItem::Type() );
1779   }
1780
1781   // find out a type of item required by a new criterion and other table features
1782   int aCriterionType       = GetCriterionType(row);
1783   bool anIsDoubleCriterion = false;
1784   bool anIsIntCriterion    = false;
1785   bool anIsComboCriterion  = false;
1786   // other features:
1787   QList<int> comboIDs; // values to show in a combo item
1788   int nbCompareSigns = 0; // possible values are 0,1,3
1789   bool isThresholdEditable = false; // actual for "simple" item types
1790   switch ( aCriterionType )
1791   {
1792   case SMESH::FT_AspectRatio:
1793   case SMESH::FT_AspectRatio3D:
1794   case SMESH::FT_Warping:
1795   case SMESH::FT_MinimumAngle:
1796   case SMESH::FT_Taper:
1797   case SMESH::FT_Skew:
1798   case SMESH::FT_Area:
1799   case SMESH::FT_Volume3D:
1800   case SMESH::FT_MaxElementLength2D:
1801   case SMESH::FT_MaxElementLength3D:
1802     anIsDoubleCriterion = true; nbCompareSigns = 3; break;
1803
1804   case SMESH::FT_FreeBorders:
1805   case SMESH::FT_FreeEdges:
1806   case SMESH::FT_FreeNodes:
1807   case SMESH::FT_FreeFaces:
1808   case SMESH::FT_EqualNodes:
1809   case SMESH::FT_EqualEdges:
1810   case SMESH::FT_EqualFaces:
1811   case SMESH::FT_EqualVolumes: break;
1812
1813   case SMESH::FT_MultiConnection:
1814   case SMESH::FT_MultiConnection2D: anIsIntCriterion = true; break;
1815
1816   case SMESH::FT_Length:
1817   case SMESH::FT_Length2D: anIsDoubleCriterion = true; break;
1818
1819   case SMESH::FT_BelongToGeom:
1820   case SMESH::FT_BelongToPlane:
1821   case SMESH::FT_BelongToCylinder:
1822   case SMESH::FT_BelongToGenSurface:
1823   case SMESH::FT_LyingOnGeom: nbCompareSigns = 1; isThresholdEditable = true; break;
1824
1825   case SMESH::FT_RangeOfIds: nbCompareSigns = 1; isThresholdEditable = true; break;
1826
1827   case SMESH::FT_BadOrientedVolume:
1828   case SMESH::FT_BareBorderVolume:
1829   case SMESH::FT_BareBorderFace:
1830   case SMESH::FT_OverConstrainedVolume:
1831   case SMESH::FT_OverConstrainedFace:
1832   case SMESH::FT_LinearOrQuadratic: break;
1833
1834   case SMESH::FT_GroupColor: nbCompareSigns = 1; isThresholdEditable = true; break;
1835
1836   case SMESH::FT_ElemGeomType:
1837     comboIDs = geomTypes( aType ); anIsComboCriterion = true; nbCompareSigns = 1; break;
1838
1839   case SMESH::FT_EntityType:
1840     comboIDs = entityTypes( aType ); anIsComboCriterion = true; nbCompareSigns = 1; break;
1841
1842   case SMESH::FT_CoplanarFaces: isThresholdEditable = true; break;
1843
1844   case SMESH::FT_BallDiameter: anIsDoubleCriterion = true; break;
1845
1846   case SMESH::FT_ConnectedElements: isThresholdEditable = true; break;
1847
1848   case SMESH::FT_LessThan:
1849   case SMESH::FT_MoreThan:
1850   case SMESH::FT_EqualTo:
1851   case SMESH::FT_LogicalNOT:
1852   case SMESH::FT_LogicalAND:
1853   case SMESH::FT_LogicalOR:
1854   case SMESH::FT_Undefined:
1855   default: return;
1856   }
1857
1858   // get a precision of a double criterion
1859   int aPrecision = 0;
1860   if ( anIsDoubleCriterion ) {
1861     const char*     aPrecisionType = getPrecision( aCriterionType );
1862     SUIT_ResourceMgr* aResourceMgr = SMESH::GetResourceMgr( mySMESHGUI );
1863     if( aPrecisionType && aResourceMgr )
1864       aPrecision = aResourceMgr->integerValue( "SMESH", aPrecisionType, aPrecision );
1865   }
1866
1867   // check if the current item type satisfies the criterion
1868   bool itemTypeKO =
1869     ( aCriterionType == SMESH::FT_GroupColor && !clrBtn) ||
1870     ( anIsComboCriterion                     && !isComboItem ) ||
1871     ( anIsIntCriterion                       && !isIntSpinItem ) ||
1872     ( anIsDoubleCriterion                    && !isDoubleSpinItem );
1873
1874   if ( !itemTypeKO )
1875   {
1876     if ( anIsDoubleCriterion )
1877     {
1878       // if the precision is to be changed we should remove the existing
1879       // spin item and create another one with new precision
1880       if ( DoubleSpinItem* aDoubleSpinItem = dynamic_cast<DoubleSpinItem*>( aTable->item( row, 2 )))
1881         itemTypeKO = ( aDoubleSpinItem->precision() != aPrecision );
1882       else
1883         itemTypeKO = true;
1884     }
1885     else if ( anIsComboCriterion )
1886     {
1887       if ( ComboItem* aComboItem = dynamic_cast<ComboItem*>( aTable->item( row, 2 )))
1888         itemTypeKO = ( aComboItem->count() != comboIDs.count() );
1889       else
1890         itemTypeKO = true;
1891     }
1892     else if ( !anIsIntCriterion && aCriterionType != SMESH::FT_GroupColor )
1893     {
1894       itemTypeKO = ( clrBtn || isComboItem || isIntSpinItem || isDoubleSpinItem );
1895     }
1896   }
1897
1898   // update the table row
1899
1900   bool isSignalsBlocked = aTable->signalsBlocked();
1901   aTable->blockSignals( true );
1902
1903   // update threshold table item
1904   if ( itemTypeKO )
1905   {
1906     aTable->removeCellWidget( row, 2 );
1907
1908     if ( aCriterionType == SMESH::FT_GroupColor ) // ---------------------- QtxColorButton
1909     {
1910       aTable->setCellWidget( row, 2, new QtxColorButton( aTable ) );
1911     }
1912     else if ( anIsComboCriterion ) // -------------------------------------------ComboItem
1913     {
1914       QString msgPrefix
1915         ( aCriterionType == SMESH::FT_ElemGeomType  ? "GEOM_TYPE_%1" :  "ENTITY_TYPE_%1" );
1916       QMap<int, QString> names;
1917       QList<int>::const_iterator id = comboIDs.begin();
1918       for ( ; id != comboIDs.end(); ++id )
1919       {
1920         QString name = msgPrefix.arg( *id );
1921         names[ *id ] = tr( name.toLatin1().data() );
1922       }
1923       ComboItem* comboBox = new ComboItem( names );
1924       aTable->setItem( row, 2, comboBox );
1925     }
1926     else if ( anIsIntCriterion ) // ------------------------------------------ IntSpinItem
1927     {
1928       IntSpinItem* intSpin = new IntSpinItem( 0 );
1929       aTable->setItem( row, 2, intSpin );
1930     }
1931     else if ( anIsDoubleCriterion ) // -------------------------------------DoubleSpinItem
1932     {
1933       DoubleSpinItem* dblSpin = new DoubleSpinItem( 0 );
1934       dblSpin->setPrecision( aPrecision );
1935       aTable->setItem( row, 2, dblSpin );
1936     }
1937     else // --------------------------------------------------------------QTableWidgetItem
1938     {
1939       aTable->setItem( row, 2, new QTableWidgetItem() );
1940     }
1941   }
1942
1943   // set Compare
1944   if ( aCompareItem->count() != nbCompareSigns )
1945   {
1946     switch ( nbCompareSigns ) {
1947     case 0: {
1948       aCompareItem->clear();
1949       break;
1950     }
1951     case 1: {
1952       QMap<int, QString> aMap;
1953       aMap[ SMESH::FT_EqualTo ] = tr("EQUAL_TO");
1954       aCompareItem->setItems(aMap);
1955       break;
1956     }
1957     case 3: {
1958       aCompareItem->setItems(getCompare());
1959       break;
1960     }
1961     }
1962   }
1963   aTable->setEditable( nbCompareSigns == 3, row, 1);
1964
1965   // enable/desable Threshold
1966   if ( aCriterionType == SMESH::FT_GroupColor ||
1967        anIsComboCriterion ||
1968        anIsIntCriterion ||
1969        anIsDoubleCriterion )
1970   {
1971     isThresholdEditable = true;
1972   }
1973   aTable->setEditable( isThresholdEditable, row, 2);
1974
1975
1976   aTable->blockSignals( isSignalsBlocked );
1977
1978   updateAdditionalWidget();
1979
1980   emit CriterionChanged(row, entityType);
1981 }
1982
1983 //=======================================================================
1984 // name    : SMESHGUI_FilterTable::onCriterionChanged()
1985 // Purpose : SLOT. Called then contents of table changed
1986 //           Provides reaction on change of criterion
1987 //=======================================================================
1988 void SMESHGUI_FilterTable::onCriterionChanged (int row, int col)
1989 {
1990   if( col == 0 )
1991     onCriterionChanged(row, col, -1);
1992   else if ( col == 2 )
1993     emit ThresholdChanged(row, GetType());
1994 }
1995
1996 //=======================================================================
1997 // name    : SMESHGUI_FilterTable::getFirstSelectedRow
1998 // Purpose : Get first selected row
1999 //=======================================================================
2000 int SMESHGUI_FilterTable::getFirstSelectedRow() const
2001 {
2002   Table* aTable = myTables[ GetType() ];
2003
2004   QList<int> selRows = aTable->selectedRows(); // already sorted
2005   int aRow = selRows.count() > 0 ? selRows[0] : aTable->currentRow();
2006
2007   return aRow >= 0 && aRow < aTable->rowCount() ? aRow : -1;
2008 }
2009
2010 //=======================================================================
2011 // name    : SMESHGUI_FilterTable::addRow
2012 // Purpose : Add row at the end of table
2013 //=======================================================================
2014 void SMESHGUI_FilterTable::addRow (Table* theTable, const int theType, const bool toTheEnd)
2015 {
2016   int aCurrRow = 0;
2017   int aSelectedRow = getFirstSelectedRow();
2018   int aCurrCol = theTable->currentColumn();
2019
2020   myIsLocked = true;
2021   if (toTheEnd || aSelectedRow == -1)
2022   {
2023     theTable->insertRows(theTable->rowCount());
2024     aCurrRow = theTable->rowCount() - 1;
2025   }
2026   else
2027   {
2028     theTable->insertRows(aSelectedRow);
2029     aCurrRow = aSelectedRow;
2030   }
2031   myIsLocked = false;
2032
2033   // IPAL19372 - to prevent calling onCriterionChaged() slot before completion of setItem()
2034   bool isSignalsBlocked = theTable->signalsBlocked();
2035   theTable->blockSignals( true );
2036
2037   // Criteria
2038   theTable->setItem(aCurrRow, 0, getCriterionItem(theType));
2039
2040   // Compare
2041   theTable->setItem(aCurrRow, 1, getCompareItem());
2042
2043   // Threshold
2044   theTable->setItem(aCurrRow, 2, new QTableWidgetItem());
2045
2046   // Logical operation NOT
2047   theTable->setItem(aCurrRow, 3, getUnaryItem());
2048
2049   // Logical operation AND / OR
2050   theTable->setItem(aCurrRow, 4, new QTableWidgetItem());
2051
2052   theTable->setItem(aCurrRow, 5, new QTableWidgetItem());
2053     
2054   theTable->blockSignals( isSignalsBlocked );
2055
2056   // Logical binary operation for previous value
2057   int anAddBinOpStr = -1;
2058   if (aCurrRow == theTable->rowCount() - 1)
2059     anAddBinOpStr = aCurrRow - 1;
2060   else if (aCurrRow >= 0 )
2061     anAddBinOpStr = aCurrRow;
2062
2063   if (theTable->item(aCurrRow, 4) == 0 ||
2064        theTable->item(aCurrRow, 4)->type() != ComboItem::Type())
2065   {
2066     if (anAddBinOpStr >= 0 &&
2067          (theTable->item(anAddBinOpStr, 4) == 0 ||
2068            theTable->item(anAddBinOpStr, 4)->type() != ComboItem::Type()))
2069       theTable->setItem(anAddBinOpStr, 4, getBinaryItem());
2070   }
2071
2072   theTable->setEditable(false, theTable->rowCount() - 1, 4);
2073   
2074   if (aCurrRow >=0 && aCurrRow < theTable->rowCount() &&
2075        aCurrCol >=0 && aCurrCol < theTable->rowCount())
2076   theTable->setCurrentCell(aCurrRow, aCurrCol);
2077
2078   onCriterionChanged(aCurrRow, 0);
2079
2080   updateBtnState();
2081 }
2082
2083 //=======================================================================
2084 // name    : SMESHGUI_FilterTable::getCriterionItem
2085 // Purpose : Get combo table item for criteria of specified type
2086 //=======================================================================
2087 QTableWidgetItem* SMESHGUI_FilterTable::getCriterionItem (const int theType) const
2088 {
2089   return new ComboItem(getCriteria(theType));
2090 }
2091
2092 //=======================================================================
2093 // name    : SMESHGUI_FilterTable::getCompareItem
2094 // Purpose : Get combo table item for operation of comparision
2095 //=======================================================================
2096 QTableWidgetItem* SMESHGUI_FilterTable::getCompareItem () const
2097 {
2098   return new ComboItem(getCompare());
2099 }
2100
2101 //=======================================================================
2102 // name    : SMESHGUI_FilterTable::getBinaryItem
2103 // Purpose :
2104 //=======================================================================
2105 QTableWidgetItem* SMESHGUI_FilterTable::getBinaryItem () const
2106 {
2107   static QMap<int, QString> aMap;
2108   if (aMap.isEmpty())
2109   {
2110     aMap[ SMESH::FT_LogicalAND ] = tr("AND");
2111     aMap[ SMESH::FT_LogicalOR  ] = tr("OR");
2112   }
2113
2114   return new ComboItem(aMap);
2115 }
2116
2117 //=======================================================================
2118 // name    : SMESHGUI_FilterTable::getUnaryItem
2119 // Purpose : Get check table item
2120 //=======================================================================
2121 QTableWidgetItem* SMESHGUI_FilterTable::getUnaryItem () const
2122 {
2123   return new CheckItem(tr("NOT"));
2124 }
2125
2126 //=======================================================================
2127 // name    : SMESHGUI_FilterTable::getSupportedTypes
2128 // Purpose : Get all supported type
2129 //=======================================================================
2130 const QMap<int, QString>& SMESHGUI_FilterTable::getSupportedTypes() const
2131 {
2132   static QMap<int, QString> aTypes;
2133   if (aTypes.isEmpty())
2134   {
2135     aTypes[ SMESH::NODE   ] = tr("NODES");
2136     aTypes[ SMESH::ELEM0D ] = tr("ELEM0D");
2137     aTypes[ SMESH::BALL   ] = tr("BALLS");
2138     aTypes[ SMESH::EDGE   ] = tr("EDGES");
2139     aTypes[ SMESH::FACE   ] = tr("FACES");
2140     aTypes[ SMESH::VOLUME ] = tr("VOLUMES");
2141     aTypes[ SMESH::ALL ]    = tr("ELEMENTS");
2142   }
2143
2144   return aTypes;
2145 }
2146
2147 //=======================================================================
2148 // name    : SMESHGUI_FilterTable::getCriteria
2149 // Purpose : Get criteria for specified type
2150 //=======================================================================
2151 const QMap<int, QString>& SMESHGUI_FilterTable::getCriteria (const int theType) const
2152 {
2153   if (theType == SMESH::NODE)
2154   {
2155     static QMap<int, QString> aCriteria;
2156     if (aCriteria.isEmpty())
2157     {
2158       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2159       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2160       aCriteria[ SMESH::FT_BelongToPlane      ] = tr("BELONG_TO_PLANE");
2161       aCriteria[ SMESH::FT_BelongToCylinder   ] = tr("BELONG_TO_CYLINDER");
2162       aCriteria[ SMESH::FT_BelongToGenSurface ] = tr("BELONG_TO_GENSURFACE");
2163       aCriteria[ SMESH::FT_LyingOnGeom        ] = tr("LYING_ON_GEOM");
2164       aCriteria[ SMESH::FT_FreeNodes          ] = tr("FREE_NODES");
2165       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2166       aCriteria[ SMESH::FT_EqualNodes         ] = tr("EQUAL_NODE");
2167       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2168     }
2169     return aCriteria;
2170   }
2171   else if (theType == SMESH::EDGE)
2172   {
2173     static QMap<int, QString> aCriteria;
2174     if (aCriteria.isEmpty())
2175     {
2176       aCriteria[ SMESH::FT_FreeBorders        ] = tr("FREE_BORDERS");
2177       aCriteria[ SMESH::FT_MultiConnection    ] = tr("MULTI_BORDERS");
2178       aCriteria[ SMESH::FT_Length             ] = tr("LENGTH");
2179       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2180       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2181       aCriteria[ SMESH::FT_BelongToPlane      ] = tr("BELONG_TO_PLANE");
2182       aCriteria[ SMESH::FT_BelongToCylinder   ] = tr("BELONG_TO_CYLINDER");
2183       aCriteria[ SMESH::FT_BelongToGenSurface ] = tr("BELONG_TO_GENSURFACE");
2184       aCriteria[ SMESH::FT_LyingOnGeom        ] = tr("LYING_ON_GEOM");
2185       aCriteria[ SMESH::FT_LinearOrQuadratic  ] = tr("LINEAR");
2186       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2187       aCriteria[ SMESH::FT_ElemGeomType       ] = tr("GEOM_TYPE");
2188       aCriteria[ SMESH::FT_EqualEdges         ] = tr("EQUAL_EDGE");
2189       aCriteria[ SMESH::FT_EntityType         ] = tr("ENTITY_TYPE");
2190       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2191     }
2192     return aCriteria;
2193   }
2194   else if (theType == SMESH::FACE)
2195   {
2196     static QMap<int, QString> aCriteria;
2197     if (aCriteria.isEmpty())
2198     {
2199       aCriteria[ SMESH::FT_AspectRatio        ] = tr("ASPECT_RATIO");
2200       aCriteria[ SMESH::FT_Warping            ] = tr("WARPING");
2201       aCriteria[ SMESH::FT_MinimumAngle       ] = tr("MINIMUM_ANGLE");
2202       aCriteria[ SMESH::FT_Taper              ] = tr("TAPER");
2203       aCriteria[ SMESH::FT_Skew               ] = tr("SKEW");
2204       aCriteria[ SMESH::FT_Area               ] = tr("AREA");
2205       aCriteria[ SMESH::FT_MaxElementLength2D ] = tr("MAX_ELEMENT_LENGTH_2D");
2206       aCriteria[ SMESH::FT_FreeEdges          ] = tr("FREE_EDGES");
2207       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2208       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2209       aCriteria[ SMESH::FT_BelongToPlane      ] = tr("BELONG_TO_PLANE");
2210       aCriteria[ SMESH::FT_BelongToCylinder   ] = tr("BELONG_TO_CYLINDER");
2211       aCriteria[ SMESH::FT_BelongToGenSurface ] = tr("BELONG_TO_GENSURFACE");
2212       aCriteria[ SMESH::FT_LyingOnGeom        ] = tr("LYING_ON_GEOM");
2213       aCriteria[ SMESH::FT_Length2D           ] = tr("LENGTH2D");
2214       aCriteria[ SMESH::FT_MultiConnection2D  ] = tr("MULTI2D_BORDERS");
2215       aCriteria[ SMESH::FT_FreeFaces          ] = tr("FREE_FACES");
2216       aCriteria[ SMESH::FT_BareBorderFace     ] = tr("BARE_BORDER_FACE");
2217       aCriteria[ SMESH::FT_OverConstrainedFace] = tr("OVER_CONSTRAINED_FACE");
2218       aCriteria[ SMESH::FT_LinearOrQuadratic  ] = tr("LINEAR");
2219       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2220       aCriteria[ SMESH::FT_ElemGeomType       ] = tr("GEOM_TYPE");
2221       aCriteria[ SMESH::FT_CoplanarFaces      ] = tr("COPLANAR_FACES");
2222       aCriteria[ SMESH::FT_EqualFaces         ] = tr("EQUAL_FACE");
2223       aCriteria[ SMESH::FT_EntityType         ] = tr("ENTITY_TYPE");
2224       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2225     }
2226     return aCriteria;
2227   }
2228   else if (theType == SMESH::VOLUME)
2229   {
2230     static QMap<int, QString> aCriteria;
2231     if (aCriteria.isEmpty())
2232     {
2233       aCriteria[ SMESH::FT_AspectRatio3D        ] = tr("ASPECT_RATIO_3D");
2234       aCriteria[ SMESH::FT_RangeOfIds           ] = tr("RANGE_OF_IDS");
2235       aCriteria[ SMESH::FT_BelongToGeom         ] = tr("BELONG_TO_GEOM");
2236       aCriteria[ SMESH::FT_LyingOnGeom          ] = tr("LYING_ON_GEOM");
2237       aCriteria[ SMESH::FT_BadOrientedVolume    ] = tr("BAD_ORIENTED_VOLUME");
2238       aCriteria[ SMESH::FT_BareBorderVolume     ] = tr("BARE_BORDER_VOLUME");
2239       aCriteria[ SMESH::FT_OverConstrainedVolume] = tr("OVER_CONSTRAINED_VOLUME");
2240       aCriteria[ SMESH::FT_Volume3D             ] = tr("VOLUME_3D");
2241       aCriteria[ SMESH::FT_MaxElementLength3D   ] = tr("MAX_ELEMENT_LENGTH_3D");
2242       aCriteria[ SMESH::FT_LinearOrQuadratic    ] = tr("LINEAR");
2243       aCriteria[ SMESH::FT_GroupColor           ] = tr("GROUP_COLOR");
2244       aCriteria[ SMESH::FT_ElemGeomType         ] = tr("GEOM_TYPE");
2245       aCriteria[ SMESH::FT_EqualVolumes         ] = tr("EQUAL_VOLUME");
2246       aCriteria[ SMESH::FT_EntityType           ] = tr("ENTITY_TYPE");
2247       aCriteria[ SMESH::FT_ConnectedElements    ] = tr("CONNECTED_ELEMS");
2248     }
2249     return aCriteria;
2250   }
2251   else if (theType == SMESH::ELEM0D)
2252   {
2253     static QMap<int, QString> aCriteria;
2254     if (aCriteria.isEmpty())
2255     {
2256       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2257       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2258       aCriteria[ SMESH::FT_BelongToPlane      ] = tr("BELONG_TO_PLANE");
2259       aCriteria[ SMESH::FT_BelongToCylinder   ] = tr("BELONG_TO_CYLINDER");
2260       aCriteria[ SMESH::FT_BelongToGenSurface ] = tr("BELONG_TO_GENSURFACE");
2261       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2262       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2263     }
2264     return aCriteria;
2265   }
2266   else if (theType == SMESH::BALL)
2267   {
2268     static QMap<int, QString> aCriteria;
2269     if (aCriteria.isEmpty())
2270     {
2271       aCriteria[ SMESH::FT_BallDiameter       ] = tr("BALL_DIAMETER");
2272       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2273       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2274       aCriteria[ SMESH::FT_BelongToPlane      ] = tr("BELONG_TO_PLANE");
2275       aCriteria[ SMESH::FT_BelongToCylinder   ] = tr("BELONG_TO_CYLINDER");
2276       aCriteria[ SMESH::FT_BelongToGenSurface ] = tr("BELONG_TO_GENSURFACE");
2277       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2278       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2279     }
2280     return aCriteria;
2281   }
2282   else if (theType == SMESH::ELEM0D)
2283   {
2284     static QMap<int, QString> aCriteria;
2285     if (aCriteria.isEmpty())
2286     {
2287       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2288       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2289       aCriteria[ SMESH::FT_BelongToPlane      ] = tr("BELONG_TO_PLANE");
2290       aCriteria[ SMESH::FT_BelongToCylinder   ] = tr("BELONG_TO_CYLINDER");
2291       aCriteria[ SMESH::FT_BelongToGenSurface ] = tr("BELONG_TO_GENSURFACE");
2292       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2293       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2294     }
2295     return aCriteria;
2296   }
2297   else // SMESH::ALL
2298   {
2299     static QMap<int, QString> aCriteria;
2300     if (aCriteria.isEmpty())
2301     {
2302       aCriteria[ SMESH::FT_RangeOfIds         ] = tr("RANGE_OF_IDS");
2303       aCriteria[ SMESH::FT_BelongToGeom       ] = tr("BELONG_TO_GEOM");
2304       aCriteria[ SMESH::FT_LyingOnGeom        ] = tr("LYING_ON_GEOM");
2305       aCriteria[ SMESH::FT_LinearOrQuadratic  ] = tr("LINEAR");
2306       aCriteria[ SMESH::FT_GroupColor         ] = tr("GROUP_COLOR");
2307       aCriteria[ SMESH::FT_ElemGeomType       ] = tr("GEOM_TYPE");
2308       aCriteria[ SMESH::FT_ConnectedElements  ] = tr("CONNECTED_ELEMS");
2309     }
2310
2311     return aCriteria;
2312   }
2313 }
2314
2315
2316 //=======================================================================
2317 // name    : SMESHGUI_FilterTable::getCompare
2318 // Purpose : Get operation of comparison
2319 //=======================================================================
2320 const QMap<int, QString>& SMESHGUI_FilterTable::getCompare() const
2321 {
2322   static QMap<int, QString> aMap;
2323
2324   if (aMap.isEmpty())
2325   {
2326     aMap[ SMESH::FT_LessThan ] = tr("LESS_THAN");
2327     aMap[ SMESH::FT_MoreThan ] = tr("MORE_THAN");
2328     aMap[ SMESH::FT_EqualTo  ] = tr("EQUAL_TO" );
2329   }
2330
2331   return aMap;
2332 }
2333
2334 //=======================================================================
2335 // name    : SMESHGUI_FilterTable::createTable
2336 // Purpose : Create table
2337 //=======================================================================
2338 SMESHGUI_FilterTable::Table* SMESHGUI_FilterTable::createTable (QWidget*  theParent,
2339                                                                 const int theType)
2340 {
2341   // create table
2342   Table* aTable= new Table(0, 6, theParent);
2343
2344   QHeaderView* aHeaders = aTable->horizontalHeader();
2345
2346   QFontMetrics aMetrics(aHeaders->font());
2347
2348   // append spaces to the header of criteria in order to
2349   // provide visibility of criterion inside comboboxes
2350   static int aMaxLenCr = 0;
2351
2352   if (aMaxLenCr == 0)
2353   {
2354     const QMap<int, QString>& aSupportedTypes = getSupportedTypes();
2355     QMap<int, QString>::const_iterator anIter;
2356     for (anIter = aSupportedTypes.begin(); anIter != aSupportedTypes.end(); ++anIter)
2357       aMaxLenCr = qMax(maxLength(getCriteria(anIter.key()), aMetrics), aMaxLenCr);
2358   }
2359
2360   static int aLenCr = qAbs( aMaxLenCr -
2361                             aMetrics.width(tr("CRITERION"))) / aMetrics.width(' ') + 5;
2362
2363   QString aCrStr;
2364   aCrStr.fill(' ', aLenCr);
2365   QString aCoStr;
2366   aCoStr.fill(' ', 10);
2367
2368   QStringList aHeaderLabels;
2369   aHeaderLabels.append( tr("CRITERION") + aCrStr );
2370   aHeaderLabels.append( tr("COMPARE")   + aCoStr );
2371   aHeaderLabels.append( tr("THRESHOLD_VALUE") );
2372   aHeaderLabels.append( tr("UNARY") );
2373   aHeaderLabels.append( tr("BINARY") + "  " );
2374   aHeaderLabels.append( tr("ID") );
2375   aTable->setHorizontalHeaderLabels( aHeaderLabels );
2376   
2377   // set geometry of the table
2378   for (int i = 0; i <= 4; i++)
2379     aTable->resizeColumnToContents(i);
2380
2381   // set the ID column invisible
2382   aTable->hideColumn( 5 );
2383
2384   aTable->updateGeometry();
2385   QSize aSize = aTable->sizeHint();
2386   int aWidth = aSize.width();
2387   aTable->setMinimumSize(QSize(aWidth, aWidth / 2));
2388   aTable->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
2389
2390   connect(aTable, SIGNAL(cellChanged(int, int)),
2391           this,   SLOT(onCriterionChanged(int, int)));
2392
2393   connect(aTable, SIGNAL(currentCellChanged(int, int, int, int)),
2394           this,   SLOT(onCurrentChanged(int, int)));
2395   
2396   return aTable;
2397 }
2398
2399 //=======================================================================
2400 // name    : SMESHGUI_FilterTable::updateBtnState
2401 // Purpose : Update button state
2402 //=======================================================================
2403 void SMESHGUI_FilterTable::updateBtnState()
2404 {
2405   myRemoveBtn->setEnabled(myTables[ GetType() ]->rowCount() > 0);
2406   myClearBtn->setEnabled(myTables[ GetType() ]->rowCount() > 0);
2407 }
2408
2409 //=======================================================================
2410 // name    : SMESHGUI_FilterTable::SetEditable
2411 // Purpose : Set read only flag for tables. Show/hide buttons for work with rows
2412 //=======================================================================
2413 void SMESHGUI_FilterTable::SetEditable (const bool isEditable)
2414 {
2415   TableMap::iterator anIter;
2416   for (anIter = myTables.begin(); anIter != myTables.end(); ++anIter)
2417   {
2418     anIter.value()->setReadOnly(!isEditable);
2419
2420     // Set Flags for CheckItems directly IPAL 19974
2421     Table* aTable = anIter.value();
2422     for (int i = 0, n = aTable->rowCount(); i < n; i++)
2423       for (int j = 0, m = aTable->columnCount(); j < m; j++)
2424         {
2425           QTableWidgetItem* anItem = aTable->item(i, j);
2426           if ( dynamic_cast<SMESHGUI_FilterTable::CheckItem*>( anItem ) ) {
2427             Qt::ItemFlags f = anItem->flags();
2428             if (!isEditable) f = f & ~Qt::ItemIsUserCheckable;
2429             else f = f | Qt::ItemIsUserCheckable;
2430             anItem->setFlags( f );
2431           }
2432         }
2433     //end of IPAL19974
2434
2435     if (isEditable)
2436     {
2437       myAddBtn->show();
2438       myInsertBtn->show();
2439       myRemoveBtn->show();
2440       myClearBtn->show();
2441     }
2442     else
2443     {
2444       myAddBtn->hide();
2445       myInsertBtn->hide();
2446       myRemoveBtn->hide();
2447       myClearBtn->hide();
2448     }
2449   }
2450
2451   QMap<QTableWidgetItem*, AdditionalWidget*>::iterator anIter2;
2452   for (anIter2 = myAddWidgets.begin(); anIter2 != myAddWidgets.end(); ++anIter2)
2453     anIter2.value()->SetEditable(isEditable);
2454 }
2455
2456 //=======================================================================
2457 // name    : SMESHGUI_FilterTable::SetEnabled
2458 // Purpose : Enable/Disable table. Switching type of elements already enabled
2459 //=======================================================================
2460 void SMESHGUI_FilterTable::SetEnabled (const bool isEnabled)
2461 {
2462   myAddBtn->setEnabled(isEnabled);
2463   myInsertBtn->setEnabled(isEnabled);
2464   myRemoveBtn->setEnabled(isEnabled);
2465   myClearBtn->setEnabled(isEnabled);
2466
2467   if (isEnabled)
2468     updateBtnState();
2469
2470   QMap<QTableWidgetItem*, AdditionalWidget*>::iterator anIter2;
2471   for (anIter2 = myAddWidgets.begin(); anIter2 != myAddWidgets.end(); ++anIter2)
2472     anIter2.value()->setEnabled(isEnabled);
2473 }
2474
2475 //=======================================================================
2476 // name    : SMESHGUI_FilterTable::IsEditable
2477 // Purpose : Verify whether table is editable
2478 //=======================================================================
2479 bool SMESHGUI_FilterTable::IsEditable() const
2480 {
2481   return !myTables[ GetType() ]->isReadOnly();
2482 }
2483
2484 //=======================================================================
2485 // name    : SMESHGUI_FilterTable::SetLibsEnabled
2486 // Purpose : Show/hide buttons for work with libraries
2487 //=======================================================================
2488 void SMESHGUI_FilterTable::SetLibsEnabled (const bool isEnabled)
2489 {
2490   if (isEnabled)
2491   {
2492     myCopyFromBtn->show();
2493     myAddToBtn->show();
2494   }
2495   else
2496   {
2497     myCopyFromBtn->hide();
2498     myAddToBtn->hide();
2499   }
2500 }
2501
2502 //=======================================================================
2503 // name    : SMESHGUI_FilterTable::onCopyFromBtn
2504 // Purpose : SLOT. Called the "Copy from ..." button clicked
2505 //           Display filter library dialog
2506 //=======================================================================
2507 void SMESHGUI_FilterTable::onCopyFromBtn()
2508 {
2509   if (myLibDlg == 0)
2510     myLibDlg = new SMESHGUI_FilterLibraryDlg(
2511       mySMESHGUI, this, GetType(), SMESHGUI_FilterLibraryDlg::COPY_FROM);
2512   else
2513     myLibDlg->Init(GetType(), SMESHGUI_FilterLibraryDlg::COPY_FROM);
2514
2515   if (myLibDlg->exec() == QDialog::Accepted)
2516   {
2517     Copy(myLibDlg->GetTable());
2518     Update();
2519   }
2520 }
2521
2522 //=======================================================================
2523 // name    : SMESHGUI_FilterTable::onAddToBtn
2524 // Purpose : SLOT. Called the "Add to ..." button clicked
2525 //           Display filter library dialog
2526 //=======================================================================
2527 void SMESHGUI_FilterTable::onAddToBtn()
2528 {
2529   if (!IsValid(true))
2530     return;
2531   if (myLibDlg == 0)
2532     myLibDlg = new SMESHGUI_FilterLibraryDlg(
2533       mySMESHGUI, this, GetType(), SMESHGUI_FilterLibraryDlg::ADD_TO);
2534   else
2535     myLibDlg->Init(GetType(), SMESHGUI_FilterLibraryDlg::ADD_TO);
2536
2537   myLibDlg->SetTable(this);
2538
2539   myLibDlg->exec();
2540 }
2541
2542 //=======================================================================
2543 // name    : SMESHGUI_FilterTable::Copy
2544 // Purpose : Initialise table with values of other table
2545 //=======================================================================
2546 void SMESHGUI_FilterTable::Copy (const SMESHGUI_FilterTable* theTable)
2547 {
2548   Clear();
2549
2550   for (int i = 0, n = theTable->NumRows(); i < n; i++)
2551   {
2552     SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
2553     theTable->GetCriterion(i, aCriterion);
2554     AddCriterion(aCriterion);
2555   }
2556 }
2557
2558 //=======================================================================
2559 // name    : SMESHGUI_FilterTable::CurrentCell
2560 // Purpose : Returns current cell
2561 //=======================================================================
2562 bool SMESHGUI_FilterTable::CurrentCell (int& theRow, int& theCol) const
2563 {
2564   theRow = myTables[ GetType() ]->currentRow();
2565   theCol = myTables[ GetType() ]->currentColumn();
2566   return theRow >= 0 && theCol >= 0;
2567 }
2568
2569 //=======================================================================
2570 // name    : SMESHGUI_FilterTable::SetText
2571 // Purpose : Set text and internal value in cell of threshold value
2572 //=======================================================================
2573 void SMESHGUI_FilterTable::SetThreshold (const int      theRow,
2574                                          const QString& theText,
2575                                          const int      theEntityType)
2576 {
2577   Table* aTable = myTables[ theEntityType == -1 ? GetType() : theEntityType ];
2578
2579   bool isSignalsBlocked = aTable->signalsBlocked();
2580   aTable->blockSignals(true);
2581   
2582   aTable->item( theRow, 2 )->setText(theText);
2583
2584   aTable->blockSignals(isSignalsBlocked);
2585 }
2586
2587 //=======================================================================
2588 // name    : SMESHGUI_FilterTable::SetText
2589 // Purpose : Get text and internal value from cell of threshold value
2590 //=======================================================================
2591 bool SMESHGUI_FilterTable::GetThreshold (const int      theRow,
2592                                          QString&       theText,
2593                                          const int      theEntityType)
2594 {
2595   Table* aTable = myTables[ theEntityType == -1 ? GetType() : theEntityType ];
2596   QTableWidgetItem* anItem = aTable->item(theRow, 2);
2597   if (anItem != 0)
2598   {
2599     theText = anItem->text();
2600     return true;
2601   }
2602   else
2603    return false;
2604 }
2605
2606 //=======================================================================
2607 // name    : SMESHGUI_FilterTable::SetID
2608 // Purpose : Set text and internal value in cell of ID value 
2609 //=======================================================================
2610 void SMESHGUI_FilterTable::SetID( const int      theRow,
2611                                   const QString& theText,
2612                                   const int      theEntityType )
2613 {
2614   Table* aTable = myTables[ theEntityType == -1 ? GetType() : theEntityType ];
2615   aTable->item( theRow, 5 )->setText( theText );
2616 }
2617
2618 //=======================================================================
2619 // name    : SMESHGUI_FilterTable::GetID
2620 // Purpose : Get text and internal value from cell of ID value
2621 //=======================================================================
2622 bool SMESHGUI_FilterTable::GetID( const int      theRow,
2623                                   QString&       theText,
2624                                   const int      theEntityType )
2625 {
2626   Table* aTable = myTables[ theEntityType == -1 ? GetType() : theEntityType ];
2627   QTableWidgetItem* anItem = aTable->item( theRow, 5 );
2628   if ( anItem != 0 )
2629     {
2630       theText = anItem->text();
2631       return true;    
2632     }
2633   else
2634     return false;
2635
2636
2637 /*
2638   Class       : SMESHGUI_FilterDlg
2639   Description : Dialog to specify filters for VTK viewer
2640 */
2641
2642
2643 //=======================================================================
2644 // name    : SMESHGUI_FilterDlg::SMESHGUI_FilterDlg
2645 // Purpose : Constructor
2646 //=======================================================================
2647 SMESHGUI_FilterDlg::SMESHGUI_FilterDlg( SMESHGUI*         theModule,
2648                                         const QList<int>& theTypes )
2649 : QDialog( SMESH::GetDesktop( theModule ) ),
2650   mySMESHGUI( theModule ),
2651   mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
2652   myInitSourceWgOnApply( true )
2653 {
2654   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
2655     mySelector = aViewWindow->GetSelector();
2656
2657   construct(theTypes);
2658 }
2659
2660 //=======================================================================
2661 // name    : SMESHGUI_FilterDlg::SMESHGUI_FilterDlg
2662 // Purpose : Constructor
2663 //=======================================================================
2664 SMESHGUI_FilterDlg::SMESHGUI_FilterDlg( SMESHGUI*   theModule,
2665                                         const int   theType )
2666 : QDialog( SMESH::GetDesktop( theModule ) ),
2667   mySMESHGUI( theModule ),
2668   mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
2669   myInitSourceWgOnApply( true )
2670 {
2671   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
2672     mySelector = aViewWindow->GetSelector();
2673   QList<int> aTypes;
2674   aTypes.append(theType);
2675   construct(aTypes);
2676 }
2677
2678 //=======================================================================
2679 // name    : SMESHGUI_FilterDlg::construct
2680 // Purpose : Construct dialog (called by constructor)
2681 //=======================================================================
2682 void SMESHGUI_FilterDlg::construct (const QList<int>& theTypes)
2683 {
2684   myTypes = theTypes;
2685
2686   setModal(false);
2687   //setAttribute(Qt::WA_DeleteOnClose, true); // VSR ??? is it required?
2688   setWindowTitle(tr("CAPTION"));
2689
2690   QVBoxLayout* aDlgLay = new QVBoxLayout (this);
2691   aDlgLay->setMargin(MARGIN);
2692   aDlgLay->setSpacing(SPACING);
2693
2694   myMainFrame         = createMainFrame  (this);
2695   QWidget* aBtnFrame  = createButtonFrame(this);
2696
2697   aDlgLay->addWidget(myMainFrame);
2698   aDlgLay->addWidget(aBtnFrame);
2699
2700   aDlgLay->setStretchFactor(myMainFrame, 1);
2701
2702   myHelpFileName = "selection_filter_library_page.html";
2703
2704   Init(myTypes);
2705 }
2706
2707 //=======================================================================
2708 // name    : SMESHGUI_FilterDlg::createMainFrame
2709 // Purpose : Create frame containing dialog's input fields
2710 //=======================================================================
2711 QWidget* SMESHGUI_FilterDlg::createMainFrame (QWidget* theParent)
2712 {
2713   QWidget* aMainFrame = new QWidget(theParent);
2714   QVBoxLayout* aMainLay = new QVBoxLayout(aMainFrame);
2715   aMainLay->setMargin(0);
2716   aMainLay->setSpacing(SPACING);
2717
2718   // filter frame
2719
2720   myTable = new SMESHGUI_FilterTable( mySMESHGUI, aMainFrame, myTypes );
2721   myTable->SetLibsEnabled(true);
2722
2723   QGroupBox* aGrp = myTable->GetTableGrp();
2724   QGridLayout* aLay = qobject_cast<QGridLayout*>( aGrp->layout() );
2725   int rows = aLay->rowCount();
2726   int cols = aLay->columnCount();
2727
2728   QFrame* aLine = new QFrame(aGrp);
2729   aLine->setFrameStyle(QFrame::HLine | QFrame::Sunken);
2730   aLay->addWidget(aLine, rows++, 0, 1, cols);
2731
2732   mySetInViewer = new QCheckBox(tr("SET_IN_VIEWER"), aGrp);
2733   mySetInViewer->setChecked(true);
2734   aLay->addWidget(mySetInViewer, rows++, 0, 1, cols);
2735
2736   // other controls
2737   QWidget* aSourceGrp = createSourceGroup(aMainFrame);
2738
2739   connect(myTable, SIGNAL(CriterionChanged(const int, const int)),
2740                     SLOT(onCriterionChanged(const int, const int)));
2741
2742   connect(myTable, SIGNAL(CurrentChanged(int, int)),
2743                     SLOT(onCurrentChanged(int, int)));
2744
2745   aMainLay->addWidget(myTable);
2746   aMainLay->addWidget(aSourceGrp);
2747   
2748   return aMainFrame;
2749 }
2750
2751 //=======================================================================
2752 // name    : SMESHGUI_FilterDlg::createSourceFrame
2753 // Purpose : Create frame containing source radio button
2754 //=======================================================================
2755 QWidget* SMESHGUI_FilterDlg::createSourceGroup (QWidget* theParent)
2756 {
2757   QGroupBox* aBox = new QGroupBox(tr("SOURCE"), theParent);
2758   QHBoxLayout* aLay = new QHBoxLayout(aBox);
2759   aLay->setMargin(MARGIN);
2760   aLay->setSpacing(SPACING);
2761
2762   mySourceGrp = new QButtonGroup(theParent);
2763
2764   QRadioButton* aMeshBtn = new QRadioButton(tr("MESH"),          aBox);
2765   QRadioButton* aSelBtn  = new QRadioButton(tr("SELECTION"),     aBox);
2766   QRadioButton* aDlgBtn  = new QRadioButton(tr("CURRENT_DIALOG"),aBox);
2767
2768   aLay->addWidget(aMeshBtn);
2769   aLay->addWidget(aSelBtn);
2770   aLay->addWidget(aDlgBtn);
2771
2772   mySourceGrp->addButton(aMeshBtn, Mesh);
2773   mySourceGrp->addButton(aSelBtn,  Selection);
2774   mySourceGrp->addButton(aDlgBtn,  Dialog);
2775
2776   aMeshBtn->setChecked(true);
2777
2778   return aBox;
2779 }
2780
2781 //=======================================================================
2782 // name    : SMESHGUI_FilterDlg::updateMainButtons
2783 // Purpose : Update visibility of main buttons (OK, Cancel, Close ...)
2784 //=======================================================================
2785 void SMESHGUI_FilterDlg::updateMainButtons()
2786 {
2787   myButtons[ BTN_Close  ]->show();
2788   if (myTypes.count() == 1)
2789   {
2790     myButtons[ BTN_Apply  ]->hide();
2791   }
2792   else
2793   {
2794     myButtons[ BTN_Apply  ]->show();
2795   }
2796 //  updateGeometry();
2797 }
2798
2799 //=======================================================================
2800 // name    : SMESHGUI_FilterDlg::createButtonFrame
2801 // Purpose : Create frame containing buttons
2802 //=======================================================================
2803 QWidget* SMESHGUI_FilterDlg::createButtonFrame (QWidget* theParent)
2804 {
2805   QGroupBox* aGrp = new QGroupBox(theParent);
2806   QHBoxLayout* aLay = new QHBoxLayout(aGrp);
2807   aLay->setMargin(MARGIN);
2808   aLay->setSpacing(SPACING);
2809
2810   myButtons[ BTN_OK     ] = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), aGrp);
2811   myButtons[ BTN_Apply  ] = new QPushButton(tr("SMESH_BUT_APPLY"),           aGrp);
2812   myButtons[ BTN_Close  ] = new QPushButton(tr("SMESH_BUT_CLOSE"),           aGrp);
2813   myButtons[ BTN_Help   ] = new QPushButton(tr("SMESH_BUT_HELP"),            aGrp);
2814
2815   aLay->addWidget(myButtons[ BTN_OK     ]);
2816   aLay->addSpacing(10);
2817   aLay->addWidget(myButtons[ BTN_Apply  ]);
2818   aLay->addSpacing(10);
2819   aLay->addStretch();
2820   aLay->addWidget(myButtons[ BTN_Close  ]);
2821   aLay->addWidget(myButtons[ BTN_Help   ]);
2822
2823   connect(myButtons[ BTN_OK     ], SIGNAL(clicked()), SLOT(onOk()));
2824   connect(myButtons[ BTN_Close  ], SIGNAL(clicked()), SLOT(reject()));
2825   connect(myButtons[ BTN_Apply  ], SIGNAL(clicked()), SLOT(onApply()));
2826   connect(myButtons[ BTN_Help   ], SIGNAL(clicked()), SLOT(onHelp()));
2827
2828   updateMainButtons();
2829
2830   return aGrp;
2831 }
2832
2833 //=======================================================================
2834 // name    : SMESHGUI_FilterDlg::~SMESHGUI_FilterDlg
2835 // Purpose : Destructor
2836 //=======================================================================
2837 SMESHGUI_FilterDlg::~SMESHGUI_FilterDlg()
2838 {
2839 }
2840
2841 //=======================================================================
2842 // name    : SMESHGUI_FilterDlg::Init
2843 // Purpose : Init dialog fields, connect signals and slots, show dialog
2844 //=======================================================================
2845 void SMESHGUI_FilterDlg::Init (const int type, const bool setInViewer)
2846 {
2847   QList<int> aTypes;
2848   aTypes.append(type);
2849   Init(aTypes,setInViewer);
2850 }
2851
2852 //=======================================================================
2853 // name    : SMESHGUI_FilterDlg::Init
2854 // Purpose : Init dialog fields, connect signals and slots, show dialog
2855 //=======================================================================
2856 void SMESHGUI_FilterDlg::Init (const QList<int>& theTypes, const bool setInViewer)
2857 {
2858   mySourceWg  = 0;
2859   myTypes     = theTypes;
2860   myMesh      = SMESH::SMESH_Mesh::_nil();
2861   myIObjects.Clear();
2862   myIsSelectionChanged = false;
2863   myToRestoreSelMode = false;
2864
2865   myTable->Init(theTypes);
2866
2867   // set caption
2868   if (theTypes.count() == 1)
2869   {
2870     int aType = theTypes.first();
2871     if      (aType == SMESH::NODE  ) setWindowTitle(tr("NODES_TLT"));
2872     else if (aType == SMESH::ELEM0D) setWindowTitle(tr("ELEM0D_TLT"));
2873     else if (aType == SMESH::BALL  ) setWindowTitle(tr("BALL_TLT"));
2874     else if (aType == SMESH::EDGE  ) setWindowTitle(tr("EDGES_TLT"));
2875     else if (aType == SMESH::FACE  ) setWindowTitle(tr("FACES_TLT"));
2876     else if (aType == SMESH::VOLUME) setWindowTitle(tr("VOLUMES_TLT"));
2877     else if (aType == SMESH::ALL)    setWindowTitle(tr("TLT"));
2878   }
2879   else
2880     setWindowTitle(tr("TLT"));
2881
2882   qApp->processEvents();
2883   updateGeometry();
2884   adjustSize();
2885   setEnabled(true);
2886
2887   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
2888
2889   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
2890   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(reject()));
2891   
2892   updateMainButtons();
2893   updateSelection();
2894
2895   // Initialise filter table with values of previous filter
2896   QList<int>::const_iterator anIter;
2897   for (anIter = theTypes.begin(); anIter != theTypes.end(); ++anIter)
2898   {
2899     myTable->Clear(*anIter);
2900     if (!myFilter[ *anIter ]->_is_nil())
2901     {
2902       SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
2903       if (myFilter[ *anIter ]->GetCriteria(aCriteria))
2904       {
2905         for (int i = 0, n = aCriteria->length(); i < n; i++)
2906           myTable->AddCriterion(aCriteria[ i ], *anIter);
2907       }
2908     }
2909   }
2910
2911   if (myInsertState.contains(theTypes.first()))
2912     mySetInViewer->setChecked(myInsertState[ theTypes.first() ]);
2913   else
2914     mySetInViewer->setChecked(setInViewer);
2915
2916   mySourceGrp->button(myApplyToState.contains(theTypes.first()) ? 
2917                       myApplyToState[ theTypes.first() ] :
2918                       Mesh)->setChecked(true);
2919 }
2920
2921 //=======================================================================
2922 // name    : SMESHGUI_FilterDlg::onOk
2923 // Purpose : SLOT called when "Ok" button pressed.
2924 //           Assign filters VTK viewer and close dialog
2925 //=======================================================================
2926 void SMESHGUI_FilterDlg::onOk()
2927 {
2928   if (onApply())
2929   {
2930     restoreSelMode();
2931     mySelectionMgr->clearFilters();
2932     disconnect(mySMESHGUI, 0, this, 0);
2933     disconnect(mySelectionMgr, 0, this, 0);
2934     mySMESHGUI->ResetState();
2935     accept();
2936     emit Accepted();
2937   }
2938 }
2939
2940 //=======================================================================
2941 // name    : SMESHGUI_FilterDlg::reject
2942 // Purpose : SLOT called when "Close" button pressed. Close dialog
2943 //=======================================================================
2944 void SMESHGUI_FilterDlg::reject()
2945 {
2946   restoreSelMode();
2947   // Restore previously selected object
2948   if (mySelectionMgr)
2949   {
2950     SALOME_ListIO aList;
2951     mySelectionMgr->clearFilters();
2952     mySelectionMgr->clearSelected();
2953     SALOME_DataMapIteratorOfDataMapOfIOMapOfInteger anIter (myIObjects);
2954     for ( ; anIter.More(); anIter.Next())
2955     {
2956       aList.Append(anIter.Key());
2957
2958       TColStd_MapOfInteger aResMap;
2959       const TColStd_IndexedMapOfInteger& anIndMap = anIter.Value();
2960       for (int i = 1, n = anIndMap.Extent(); i <= n; i++)
2961         aResMap.Add(anIndMap(i));
2962
2963       mySelector->AddOrRemoveIndex( anIter.Key(), aResMap, false);
2964       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
2965         aViewWindow->highlight( anIter.Key(), true, true );
2966     }
2967     mySelectionMgr->setSelectedObjects(aList, false);
2968   }
2969
2970   disconnect(mySMESHGUI, 0, this, 0);
2971   disconnect(mySelectionMgr, 0, this, 0);
2972   mySMESHGUI->ResetState();
2973   QDialog::reject();
2974 }
2975
2976 //=================================================================================
2977 // function : onHelp()
2978 // purpose  :
2979 //=================================================================================
2980 void SMESHGUI_FilterDlg::onHelp()
2981 {
2982   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
2983   if (app) 
2984     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
2985   else {
2986     QString platform;
2987 #ifdef WIN32
2988     platform = "winapplication";
2989 #else
2990     platform = "application";
2991 #endif
2992     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
2993                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
2994                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
2995                                                                  platform)).
2996                              arg(myHelpFileName));
2997   }
2998 }
2999
3000 //=======================================================================
3001 // name    : SMESHGUI_FilterDlg::onDeactivate
3002 // Purpose : SLOT called when dialog must be deativated
3003 //=======================================================================
3004 void SMESHGUI_FilterDlg::onDeactivate()
3005 {
3006   setEnabled(false);
3007 }
3008
3009 //=======================================================================
3010 // name    : SMESHGUI_FilterDlg::enterEvent
3011 // Purpose : Event filter
3012 //=======================================================================
3013 void SMESHGUI_FilterDlg::enterEvent (QEvent*)
3014 {
3015 //  mySMESHGUI->EmitSignalDeactivateDialog();
3016   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
3017   mySMESHGUI->ResetState();
3018   setEnabled(true);
3019 }
3020
3021 //=======================================================================
3022 // name    : SMESHGUI_FilterDlg::getIdsFromWg
3023 // Purpose : Retrieve list of ids from given widget
3024 //=======================================================================
3025 void SMESHGUI_FilterDlg::getIdsFromWg (const QWidget* theWg, QList<int>& theRes) const
3026 {
3027   theRes.clear();
3028   if (theWg == 0)
3029     return;
3030
3031   if (theWg->inherits("QListWidget"))
3032   {
3033     const QListWidget* aListBox = qobject_cast<const QListWidget*>( theWg );
3034     bool b;
3035     for (int i = 0, n = aListBox->count(); i < n; i++)
3036     {
3037       int anId = aListBox->item(i)->text().toInt(&b);
3038       if (b)
3039         theRes.append(anId);
3040     }
3041   }
3042   else if (theWg->inherits("QLineEdit"))
3043   {
3044     const QLineEdit* aLineEdit = qobject_cast<const QLineEdit*>( theWg );
3045     QString aStr = aLineEdit->text();
3046     QRegExp aRegExp("(\\d+)");
3047     bool b;
3048     int aPos = 0;
3049     while (aPos >= 0)
3050     {
3051       aPos = aRegExp.indexIn(aStr, aPos);
3052       if (aPos > -1)
3053       {
3054         int anId = aRegExp.cap(1).toInt(&b);
3055         if (b)
3056           theRes.append(anId);
3057         aPos += aRegExp.matchedLength();
3058       }
3059     }
3060   }
3061 }
3062
3063 //=======================================================================
3064 // name    : SMESHGUI_FilterDlg::getSelMode
3065 // Purpose : Get selection mode of specified type
3066 //=======================================================================
3067 Selection_Mode SMESHGUI_FilterDlg::getSelMode (const int theType) const
3068 {
3069   switch (theType)
3070   {
3071     case SMESH::NODE   : return NodeSelection;
3072     case SMESH::ELEM0D : return Elem0DSelection;
3073     case SMESH::BALL   : return BallSelection;
3074     case SMESH::EDGE   : return EdgeSelection;
3075     case SMESH::FACE   : return FaceSelection;
3076     case SMESH::VOLUME : return VolumeSelection;
3077     case SMESH::ALL    : return CellSelection;
3078     default            : return ActorSelection;
3079   }
3080
3081 }
3082
3083 //=======================================================================
3084 // name    : SMESHGUI_FilterDlg::setIdsToWg
3085 // Purpose : Insert identifiers in specified widgets
3086 //=======================================================================
3087 void SMESHGUI_FilterDlg::setIdsToWg (QWidget* theWg, const QList<int>& theIds)
3088 {
3089   if (theWg == 0)
3090     return;
3091
3092   QStringList aStrList;
3093   foreach(int id, theIds)
3094     aStrList << QString::number(id);
3095
3096   if (theWg->inherits("QListWidget"))
3097   {
3098     qobject_cast<QListWidget*>(theWg)->clear();
3099     qobject_cast<QListWidget*>(theWg)->addItems(aStrList);
3100   }
3101   else if (theWg->inherits("QLineEdit"))
3102   {
3103     qobject_cast<QLineEdit*>( theWg )->setText(aStrList.join(" "));
3104   }
3105 }
3106
3107 //=======================================================================
3108 // name    : SMESHGUI_FilterDlg::isValid
3109 // Purpose : Verify validity of input data
3110 //=======================================================================
3111 bool SMESHGUI_FilterDlg::isValid() const
3112 {
3113   if (!myTable->IsValid())
3114     return false;
3115
3116   for (int i = 0, n = myTable->NumRows(); i < n; i++)
3117   {
3118     int aType = myTable->GetCriterionType(i);
3119     if (aType == SMESH::FT_BelongToGeom ||
3120         aType == SMESH::FT_BelongToPlane ||
3121         aType == SMESH::FT_BelongToCylinder ||
3122         aType == SMESH::FT_BelongToGenSurface ||
3123         aType == SMESH::FT_LyingOnGeom)
3124     {
3125       QString aName;
3126       myTable->GetThreshold(i, aName);
3127
3128       std::vector<_PTR(SObject)> aList =
3129         SMESH::GetActiveStudyDocument()->FindObjectByName(aName.toLatin1().constData(), "GEOM");
3130       if (aList.size() == 0) {
3131         SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3132                                      tr("BAD_SHAPE_NAME").arg(aName));
3133         return false;
3134       }
3135
3136       if (aType == SMESH::FT_BelongToCylinder ||
3137           aType == SMESH::FT_BelongToPlane    ||
3138           aType == SMESH::FT_BelongToGenSurface ) {
3139         CORBA::Object_var anObject = SMESH::SObjectToObject(aList[ 0 ]);
3140         //GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow(aList[ 0 ]->GetObject());
3141         GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow(anObject);
3142         if (!aGeomObj->_is_nil()) {
3143           TopoDS_Shape aFace;
3144           if (!GEOMBase::GetShape(aGeomObj, aFace) ||
3145                aFace.IsNull() ||
3146                aFace.ShapeType() != TopAbs_FACE) {
3147             SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3148                                          tr("SHAPE_IS_NOT_A_FACE").arg(aName));
3149             return false;
3150           }
3151
3152           Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aFace));
3153           if (aSurf.IsNull()) {
3154             SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3155                                          tr("SHAPE_IS_NOT_A_FACE").arg(aName));
3156             return false;
3157           }
3158
3159           if (aType == SMESH::FT_BelongToPlane && !aSurf->IsKind(STANDARD_TYPE(Geom_Plane))) {
3160             SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3161                                          tr("SHAPE_IS_NOT_A_PLANE").arg(aName));
3162             return false;
3163           }
3164
3165           if (aType == SMESH::FT_BelongToCylinder && !aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
3166             SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3167                                          tr("SHAPE_IS_NOT_A_CYLINDER").arg(aName));
3168             return false;
3169           }
3170         }
3171       }
3172     }
3173     else if (aType == SMESH::FT_CoplanarFaces)
3174     {
3175       QString faceID;
3176       myTable->GetThreshold(i, faceID);
3177       if ( faceID.isEmpty() )
3178       {
3179         SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3180                                      tr("FACE_ID_NOT_SELECTED"));
3181         return false;
3182       }
3183       if ( myMesh->_is_nil() )
3184       {
3185         SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3186                                      tr("MESH_IS_NOT_SELECTED"));
3187         return false;
3188       }
3189       if ( myMesh->GetElementType( faceID.toLong(), /*iselem=*/true) != SMESH::FACE )
3190       {
3191         SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
3192                                      tr("NOT_FACE_ID").arg(faceID));
3193         return false;
3194       }
3195     }
3196   }
3197
3198   return true;
3199 }
3200
3201 //=======================================================================
3202 // name    : SMESHGUI_FilterDlg::SetSourceWg
3203 // Purpose : Set widget of parent dialog containing ids to be filtered if
3204 //           user select corresponding source radio button
3205 //=======================================================================
3206 void SMESHGUI_FilterDlg::SetSourceWg (QWidget* theWg,
3207                                       const bool initOnApply)
3208 {
3209   mySourceWg = theWg;
3210   myInitSourceWgOnApply = initOnApply;
3211 }
3212
3213 //=======================================================================
3214 // name    : SMESHGUI_FilterDlg::SetMesh
3215 // Purpose : Set mesh
3216 //=======================================================================
3217 void SMESHGUI_FilterDlg::SetMesh (SMESH::SMESH_Mesh_var theMesh)
3218 {
3219   if ( !theMesh->_is_nil() ) {
3220     myMesh = theMesh;
3221     if ( !myFilter[ myTable->GetType() ]->_is_nil())
3222       myFilter[ myTable->GetType() ]->SetMesh( theMesh );
3223   }
3224   const bool isEnable = !(myMesh->_is_nil());
3225   myButtons[BTN_OK]->setEnabled(isEnable);
3226   myButtons[BTN_Apply]->setEnabled(isEnable);
3227 }
3228
3229 //=======================================================================
3230 // name    : SMESHGUI_FilterDlg::SetSelection
3231 // Purpose : Get filtered ids
3232 //=======================================================================
3233 void SMESHGUI_FilterDlg::SetSelection()
3234 {
3235   if (mySelectionMgr)
3236     disconnect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(onSelectionDone()));
3237
3238   if (mySelectionMgr) {
3239     myIObjects.Clear();
3240     const SALOME_ListIO& anObjs = mySelector->StoredIObjects(); 
3241     SALOME_ListIteratorOfListIO anIter (anObjs);
3242     for ( ; anIter.More(); anIter.Next()) {
3243       TColStd_IndexedMapOfInteger aMap;
3244       mySelector->GetIndex(anIter.Value(), aMap);
3245       myIObjects.Bind(anIter.Value(), aMap);
3246     }
3247
3248     connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
3249
3250     updateSelection();
3251   }
3252   else
3253     myIObjects.Clear();
3254 }
3255
3256 //=======================================================================
3257 // name    : SMESHGUI_FilterDlg::onApply
3258 // Purpose : SLOT called when "Apply" button pressed.
3259 //           Assign filters to VTK viewer
3260 //=======================================================================
3261 bool SMESHGUI_FilterDlg::onApply()
3262 {
3263   if (!isValid())
3264     return false;
3265
3266   try {
3267     int aCurrType = myTable->GetType();
3268
3269     if (!createFilter(aCurrType))
3270       return false;
3271
3272     insertFilterInViewer();
3273
3274     if (!myFilter[ aCurrType ]->GetPredicate()->_is_nil()) {
3275       QList<int> aResultIds;
3276       filterSource(aCurrType, aResultIds);
3277       // select in viewer
3278       selectInViewer(aCurrType, aResultIds);
3279     }
3280
3281
3282     myInsertState[ aCurrType ] = mySetInViewer->isChecked();
3283     myApplyToState[ aCurrType ] = mySourceGrp->checkedId();
3284   }
3285   catch(const SALOME::SALOME_Exception& S_ex)
3286   {
3287     SalomeApp_Tools::QtCatchCorbaException(S_ex);
3288     return false;
3289   }
3290   catch(...)
3291   {
3292     return false;
3293   }
3294
3295   return true;
3296 }
3297
3298 //=======================================================================
3299 // name    : SMESHGUI_FilterDlg::createFilter
3300 // Purpose : Create predicate for given type
3301 //=======================================================================
3302 bool SMESHGUI_FilterDlg::createFilter (const int theType)
3303 {
3304   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
3305   if (aFilterMgr->_is_nil())
3306     return false;
3307
3308   int n = myTable->NumRows();
3309
3310   SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
3311   aCriteria->length(n);
3312
3313   long aPrecision = -1;
3314   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
3315
3316   if ( mgr && mgr->booleanValue( "SMESH", "use_precision", false ) )
3317     aPrecision = mgr->integerValue( "SMESH", "controls_precision", aPrecision );
3318
3319   for (CORBA::ULong i = 0; i < n; i++) {
3320     SMESH::Filter::Criterion aCriterion = createCriterion();
3321     myTable->GetCriterion(i, aCriterion);
3322     aCriterion.Precision = aPrecision;
3323     aCriteria[ i ] = aCriterion;
3324   }
3325
3326   myFilter[ theType ] = aFilterMgr->CreateFilter();
3327   myFilter[ theType ]->SetCriteria(aCriteria.inout());
3328
3329   return true;
3330 }
3331
3332 //================================================================================
3333 /*!
3334  * \brief Return the current filter
3335  */
3336 //================================================================================
3337
3338 SMESH::Filter_var SMESHGUI_FilterDlg::GetFilter() const
3339 {
3340   SMESH::Filter_var filter;
3341   try {
3342     int aCurrType = myTable->GetType();
3343     filter = myFilter[ aCurrType ];
3344   }
3345   catch(...)
3346   {
3347   }
3348   return filter._retn();
3349 }
3350
3351 //================================================================================
3352 /*!
3353  * \brief Sets a filter to the table
3354  */
3355 //================================================================================
3356
3357 void SMESHGUI_FilterDlg::SetFilter(SMESH::Filter_var filter, int type)
3358 {
3359   myFilter[ type ] = filter;
3360 }
3361
3362 //=======================================================================
3363 // name    : SMESHGUI_FilterDlg::insertFilterInViewer
3364 // Purpose : Insert filter in viewer
3365 //=======================================================================
3366 void SMESHGUI_FilterDlg::insertFilterInViewer()
3367 {
3368   if (SVTK_Selector* aSelector = SMESH::GetSelector()) {
3369     SMESH::ElementType anEntType = (SMESH::ElementType)myTable->GetType();
3370
3371     if (myFilter[ myTable->GetType() ]->_is_nil() ||
3372          myFilter[ myTable->GetType() ]->GetPredicate()->_is_nil() ||
3373          !mySetInViewer->isChecked()) {
3374       SMESH::RemoveFilter(getFilterId(anEntType), aSelector);
3375     }
3376     else {
3377       Handle(SMESHGUI_PredicateFilter) aFilter = new SMESHGUI_PredicateFilter();
3378       aFilter->SetPredicate(myFilter[ myTable->GetType() ]->GetPredicate());
3379       SMESH::RemoveFilter(getFilterId(anEntType), aSelector); //skl for IPAL12631
3380       SMESH::SetFilter(aFilter, aSelector);
3381     }
3382   }
3383 }
3384
3385 //=======================================================================
3386 // name    : SMESHGUI_FilterDlg::filterSource
3387 // Purpose : Filter source ids
3388 //=======================================================================
3389 void SMESHGUI_FilterDlg::filterSource (const int theType,
3390                                        QList<int>& theResIds)
3391 {
3392   theResIds.clear();
3393   if (myFilter[ theType ]->_is_nil())
3394     return;
3395
3396   int aSourceId = mySourceGrp->checkedId();
3397
3398   if (aSourceId == Mesh)
3399   {
3400     if (myMesh->_is_nil())
3401       return;
3402     SMESH::long_array_var anIds = myFilter[ theType ]->GetElementsId(myMesh);
3403     for (int i = 0, n = anIds->length(); i < n; i++)
3404       theResIds.append(anIds[ i ]);
3405   }
3406   else if (aSourceId == Selection)
3407   {
3408     filterSelectionSource(theType, theResIds);
3409   }
3410   else if (aSourceId == Dialog)
3411   {
3412     // retrieve ids from dialog
3413     QList<int> aDialogIds;
3414     getIdsFromWg(mySourceWg, aDialogIds);
3415
3416     if (myMesh->_is_nil())
3417     {
3418       theResIds = aDialogIds;
3419       return;
3420     }
3421
3422     // filter ids
3423     SMESH::Predicate_ptr aPred = myFilter[ theType ]->GetPredicate();
3424     myFilter[ theType ]->SetMesh(myMesh);
3425     QList<int>::const_iterator anIter;
3426     for (anIter = aDialogIds.begin(); anIter != aDialogIds.end(); ++ anIter)
3427       if (aPred->IsSatisfy(*anIter))
3428         theResIds.append(*anIter);
3429   }
3430   // set ids to the dialog
3431   if (myInitSourceWgOnApply || aSourceId == Dialog)
3432     setIdsToWg(mySourceWg, theResIds);
3433 }
3434
3435 //=======================================================================
3436 // name    : SMESHGUI_FilterDlg::filterSelectionSource
3437 // Purpose : Filter source selection
3438 //=======================================================================
3439 void SMESHGUI_FilterDlg::filterSelectionSource (const int theType,
3440                                                 QList<int>& theResIds)
3441 {
3442   theResIds.clear();
3443   if (myMesh->_is_nil() || mySelectionMgr == 0)
3444     return;
3445
3446   // Create map of entities to be filtered
3447   TColStd_MapOfInteger aToBeFiltered;
3448   SALOME_DataMapIteratorOfDataMapOfIOMapOfInteger anIter(myIObjects);
3449
3450   for ( ; anIter.More(); anIter.Next())
3451   {
3452     // process sub mesh
3453     SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIter.Key());
3454     if (!aSubMesh->_is_nil())
3455     {
3456       if (aSubMesh->GetFather()->GetId() == myMesh->GetId())
3457       {
3458         SMESH::long_array_var anIds =
3459           theType == SMESH::NODE ? aSubMesh->GetNodesId() : aSubMesh->GetElementsId();
3460         for (int i = 0, n = anIds->length(); i < n; i++)
3461           aToBeFiltered.Add(anIds[ i ]);
3462       }
3463     }
3464
3465     // process group
3466     SMESH::SMESH_GroupBase_var aGroup =
3467       SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIter.Key());
3468     if (!aGroup->_is_nil())
3469     {
3470       if (aGroup->GetType() == theType && aGroup->GetMesh()->GetId() == myMesh->GetId())
3471       {
3472         SMESH::long_array_var anIds = aGroup->GetListOfID();
3473         for (int i = 0, n = anIds->length(); i < n; i++)
3474           aToBeFiltered.Add(anIds[ i ]);
3475       }
3476     }
3477
3478     // process mesh
3479     SMESH::SMESH_Mesh_var aMeshPtr = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIter.Key());
3480     if (!aMeshPtr->_is_nil() && aMeshPtr->GetId() == myMesh->GetId())
3481     {
3482       const TColStd_IndexedMapOfInteger& aSelMap = anIter.Value();
3483
3484       if (aSelMap.Extent() > 0)
3485       {
3486         if(SMESH::FindActorByEntry(anIter.Key()->getEntry()))
3487         {
3488           for (int i = 1; i <= aSelMap.Extent(); i++)
3489             aToBeFiltered.Add(aSelMap(i));
3490         }
3491       }
3492     }
3493   }
3494
3495   // Filter entities
3496   SMESH::Predicate_ptr aPred = myFilter[ theType ]->GetPredicate();
3497   myFilter[ theType ]->SetMesh(myMesh);
3498   TColStd_MapIteratorOfMapOfInteger aResIter(aToBeFiltered);
3499   for ( ; aResIter.More(); aResIter.Next())
3500     if (aPred->IsSatisfy(aResIter.Key()))
3501       theResIds.append(aResIter.Key());
3502 }
3503
3504 //=======================================================================
3505 // name    : SMESHGUI_FilterDlg::selectInViewer
3506 // Purpose : Select given entities in viewer
3507 //=======================================================================
3508 void SMESHGUI_FilterDlg::selectInViewer (const int theType, const QList<int>& theIds)
3509 {
3510   if (mySelectionMgr == 0 || myMesh->_is_nil())
3511     return;
3512
3513   mySelectionMgr->clearFilters();
3514
3515   // Set new selection mode if necessary
3516   Selection_Mode aSelMode = getSelMode(theType);
3517   SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI );
3518   if ( aViewWindow && aViewWindow->SelectionMode()!=aSelMode) {
3519     mySelectionMgr->clearSelected();
3520     mySelectionMgr->clearFilters();
3521     SMESH::SetPointRepresentation( aSelMode == NodeSelection );
3522     aViewWindow->SetSelectionMode(aSelMode);
3523   }
3524
3525   // Clear selection
3526   SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
3527   if (!anActor || !anActor->hasIO())
3528     return;
3529
3530   Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
3531   //mySelectionMgr->clearSelected();
3532   //mySelectionMgr->AddIObject(anIO, false);
3533   SALOME_ListIO aList;
3534   aList.Append(anIO);
3535   mySelectionMgr->setSelectedObjects(aList, false);
3536
3537   // Remove filter corresponding to the current type from viewer
3538   int aType = myTable->GetType();
3539   int aFilterId = SMESH::UnknownFilter;
3540   if      (aType == SMESH::EDGE  ) aFilterId = SMESH::EdgeFilter;
3541   else if (aType == SMESH::FACE  ) aFilterId = SMESH::FaceFilter;
3542   else if (aType == SMESH::VOLUME) aFilterId = SMESH::VolumeFilter;
3543   Handle(VTKViewer_Filter) aFilter = SMESH::GetFilter(aFilterId);
3544   SMESH::RemoveFilter(aFilterId);
3545
3546   // get vtk ids
3547   TColStd_MapOfInteger aMap;
3548   QList<int>::const_iterator anIter;
3549   for (anIter = theIds.begin(); anIter != theIds.end(); ++anIter) {
3550     aMap.Add(*anIter);
3551   }
3552
3553   // Set new selection
3554   mySelector->AddOrRemoveIndex(anIO, aMap, false);
3555   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
3556     aViewWindow->highlight( anIO, true, true );
3557
3558   // insert previously stored filter in viewer if necessary
3559   if (!aFilter.IsNull())
3560     SMESH::SetFilter(aFilter);
3561 }
3562
3563 //=======================================================================
3564 // name    : SMESHGUI_FilterDlg::createCriterion
3565 // Purpose : Create criterion structure with default values
3566 //=======================================================================
3567 SMESH::Filter::Criterion SMESHGUI_FilterDlg::createCriterion()
3568 {
3569    SMESH::Filter::Criterion aCriterion;
3570
3571   aCriterion.Type          = SMESH::FT_Undefined;
3572   aCriterion.Compare       = SMESH::FT_Undefined;
3573   aCriterion.Threshold     = 0;
3574   aCriterion.UnaryOp       = SMESH::FT_Undefined;
3575   aCriterion.BinaryOp      = SMESH::FT_Undefined;
3576   aCriterion.ThresholdStr  = "";
3577   aCriterion.ThresholdID   = "";
3578   aCriterion.TypeOfElement = SMESH::ALL;
3579
3580   return aCriterion;
3581 }
3582
3583 //=======================================================================
3584 // name    : SMESHGUI_FilterDlg::onSelectionDone
3585 // Purpose : SLOT called when selection changed.
3586 //           If current cell corresponds to the threshold value of
3587 //           BelongToGeom criterion name of selected object is set in this cell
3588 //=======================================================================
3589 void SMESHGUI_FilterDlg::onSelectionDone()
3590 {
3591   const SALOME_ListIO& aList = mySelector->StoredIObjects();
3592   if ( myMesh->_is_nil() && aList.Extent()>0 )
3593   {
3594     myMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(aList.First());
3595     if ( !(myMesh->_is_nil()) ) {
3596       myButtons[BTN_OK]->setEnabled(true);
3597       myButtons[BTN_Apply]->setEnabled(true);
3598     }
3599   }
3600   int aRow, aCol;
3601   if (aList.Extent() != 1 || !myTable->CurrentCell(aRow, aCol))
3602     return;
3603
3604   const int type = myTable->GetCriterionType(aRow);
3605   QList<int> types; 
3606   types << SMESH::FT_BelongToGeom     << SMESH::FT_BelongToPlane 
3607         << SMESH::FT_BelongToCylinder << SMESH::FT_BelongToGenSurface
3608         << SMESH::FT_LyingOnGeom      << SMESH::FT_CoplanarFaces
3609         << SMESH::FT_ConnectedElements;
3610   if ( !types.contains( type ))
3611     return;
3612
3613   Handle(SALOME_InteractiveObject) anIO = aList.First();
3614   switch ( type )
3615   {
3616   case SMESH::FT_CoplanarFaces: // get ID of a selected mesh face
3617     {
3618       QString aString;
3619       int nbElems = SMESH::GetNameOfSelectedElements(mySelector, anIO, aString);
3620       if (nbElems == 1)
3621         myTable->SetThreshold(aRow, aString);
3622       break;
3623     }
3624   case SMESH::FT_ConnectedElements: // get either VERTEX or a node ID
3625     {
3626       GEOM::GEOM_Object_var anObj = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
3627       if (!anObj->_is_nil())
3628       {
3629         myTable->SetThreshold(aRow, GEOMBase::GetName(anObj));
3630         myTable->SetID       (aRow, anIO->getEntry());
3631       }
3632       else
3633       {
3634         QString aString;
3635         int nbElems = SMESH::GetNameOfSelectedElements(mySelector, anIO, aString);
3636         if (nbElems == 1)
3637           myTable->SetThreshold(aRow, aString);
3638       }
3639       break;
3640     }
3641   default: // get a GEOM object
3642     {
3643       GEOM::GEOM_Object_var anObj = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
3644       if (!anObj->_is_nil())
3645       {
3646         myTable->SetThreshold(aRow, GEOMBase::GetName(anObj));
3647         myTable->SetID(aRow, anIO->getEntry());
3648       }
3649     }
3650   }
3651 }
3652
3653
3654 //=======================================================================
3655 // name    : SMESHGUI_FilterDlg::onCriterionChanged
3656 // Purpose : SLOT called when cretarion of current row changed. Update selection
3657 //=======================================================================
3658 void SMESHGUI_FilterDlg::onCriterionChanged (const int, const int)
3659 {
3660   updateSelection();
3661 }
3662
3663 //=======================================================================
3664 // name    : SMESHGUI_FilterDlg::onThresholdChanged
3665 // Purpose : SLOT called when a threshold value is changed by the user and
3666 //           not by myTable->SetThreshold()
3667 //=======================================================================
3668 void SMESHGUI_FilterDlg::onThresholdChanged( const int row, const int type )
3669 {
3670   if ( myTable->GetCriterionType( row, type ) == SMESH::FT_ConnectedElements )
3671   {
3672     // to differ the text entered by the user from that got from selection
3673     myTable->SetID( row, "", type );
3674   }
3675 }
3676
3677 //=======================================================================
3678 // name    : SMESHGUI_FilterDlg::onCurrentChanged
3679 // Purpose : SLOT called when current row changed. Update selection
3680 //=======================================================================
3681 void SMESHGUI_FilterDlg::onCurrentChanged (int, int)
3682 {
3683   updateSelection();
3684 }
3685
3686 //=======================================================================
3687 // name    : SMESHGUI_FilterDlg::updateSelection
3688 // Purpose : UpdateSelection in accordance with current row
3689 //=======================================================================
3690 void SMESHGUI_FilterDlg::updateSelection()
3691 {
3692   if (mySelectionMgr == 0)
3693     return;
3694
3695 //   TColStd_MapOfInteger allTypes;
3696 //   for( int i=0; i<10; i++ )
3697 //     allTypes.Add( i );
3698   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( mySMESHGUI->application()->activeStudy() );
3699   if( !aStudy )
3700     return;
3701
3702   restoreSelMode();
3703
3704   mySelectionMgr->clearFilters();
3705
3706   int aRow, aCol;
3707   
3708   bool isCurrentCell = myTable->CurrentCell(aRow, aCol);
3709   int aCriterionType = myTable->GetCriterionType(aRow);
3710   if ( isCurrentCell &&
3711       (aCriterionType == SMESH::FT_BelongToGeom ||
3712        aCriterionType == SMESH::FT_BelongToPlane ||
3713        aCriterionType == SMESH::FT_BelongToCylinder ||
3714        aCriterionType == SMESH::FT_BelongToGenSurface ||
3715        aCriterionType == SMESH::FT_LyingOnGeom))
3716   {
3717     if (aCriterionType == SMESH::FT_BelongToGeom ||
3718         aCriterionType == SMESH::FT_BelongToGenSurface ||
3719         aCriterionType == SMESH::FT_LyingOnGeom) {
3720
3721       mySelectionMgr->installFilter(new GEOM_SelectionFilter( aStudy, true ));
3722
3723     } else if (aCriterionType == SMESH::FT_BelongToPlane) {
3724       mySelectionMgr->installFilter(new GEOM_FaceFilter( aStudy, StdSelect_Plane ) );
3725
3726     } else if (aCriterionType == SMESH::FT_BelongToCylinder) {
3727       mySelectionMgr->installFilter(new GEOM_FaceFilter( aStudy, StdSelect_Cylinder ) );
3728     }
3729     myIsSelectionChanged = true;
3730
3731   }
3732   else if ( aCriterionType == SMESH::FT_ConnectedElements )
3733   {
3734     QList<SUIT_SelectionFilter*> fList;
3735     fList.push_back( new SMESH_NumberFilter( "GEOM", TopAbs_SHAPE, 1, TopAbs_VERTEX ));
3736     fList.push_back( new SMESH_TypeFilter( SMESH::IDSOURCE ));
3737     mySelectionMgr->installFilter
3738       ( new SMESH_LogicalFilter( fList, SMESH_LogicalFilter::LO_OR,
3739                                  !mySelectionMgr->autoDeleteFilter() ));
3740
3741     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
3742     {
3743       mySelModeToRestore = aViewWindow->SelectionMode();
3744       aViewWindow->SetSelectionMode( NodeSelection );
3745       myToRestoreSelMode = ( mySelModeToRestore != NodeSelection );
3746       if ( myToRestoreSelMode )
3747         SMESH::SetPointRepresentation( true );
3748     }
3749   }
3750   else
3751   {
3752     mySelector->SetSelectionMode( getSelMode( myTable->GetType() ));
3753
3754     if (myIsSelectionChanged) {
3755       // mySelectionMgr->installFilter( new GEOM_TypeFilter( aStudy, -1 ) ); // This filter deactivates selection
3756       // Impossible to select any object in the OB on the second opening of FilterDlg
3757     }
3758   }
3759 }
3760
3761 //=================================================================================
3762 // function : keyPressEvent()
3763 // purpose  :
3764 //=================================================================================
3765 void SMESHGUI_FilterDlg::keyPressEvent( QKeyEvent* e )
3766 {
3767   QDialog::keyPressEvent( e );
3768   if ( e->isAccepted() )
3769     return;
3770
3771   if ( e->key() == Qt::Key_F1 ) {
3772     e->accept();
3773     onHelp();
3774   }
3775 }
3776
3777 //================================================================================
3778 /*!
3779  * \brief Restores a selection mode if it was changed to set up some criterion
3780  */
3781 //================================================================================
3782
3783 void SMESHGUI_FilterDlg::restoreSelMode()
3784 {
3785   if ( myToRestoreSelMode )
3786   {
3787     SMESH::SetPointRepresentation( mySelModeToRestore == NodeSelection );
3788
3789     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
3790       aViewWindow->SetSelectionMode( mySelModeToRestore );
3791
3792     myToRestoreSelMode = false;
3793   }
3794 }