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