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