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