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