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