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