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