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