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