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