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