Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_FilterLibraryDlg.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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SMESHGUI_FilterLibraryDlg.cxx
25 //  Author : Sergey LITONIN
26 //  Module : SMESH
27
28 #include "SMESHGUI_FilterLibraryDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_FilterUtils.h"
33
34 #include "SUIT_Session.h"
35 #include "SUIT_Desktop.h"
36 #include "SUIT_FileDlg.h"
37
38 // QT Includes
39 #include <qapplication.h>
40 #include <qmessagebox.h>
41 #include <qframe.h>
42 #include <qlayout.h>
43 #include <qlineedit.h>
44 #include <qpushbutton.h>
45 #include <qgroupbox.h>
46 #include <qlabel.h>
47 #include <qlistbox.h>
48 #include <qfile.h>
49 #include <qfileinfo.h>
50 #include <qdir.h>
51
52 #define SPACING 5
53 #define MARGIN  10
54
55 /*!
56  *  Class       : SMESHGUI_FilterLibraryDlg::Dialog
57  *  Description : Dialog for opening filter library
58  */
59
60 class SMESHGUI_FilterLibraryDlg::Dialog : public SUIT_FileDlg
61 {
62  public:
63   Dialog(QWidget* theParent, const bool theToOpen);
64   virtual ~Dialog();
65
66  protected:
67   virtual bool acceptData();
68 };
69
70 SMESHGUI_FilterLibraryDlg::Dialog::Dialog (QWidget*   theParent,
71                                            const bool theToOpen)
72      : SUIT_FileDlg(theParent, theToOpen)
73 {
74 }
75
76 SMESHGUI_FilterLibraryDlg::Dialog::~Dialog()
77 {
78 }
79
80 bool SMESHGUI_FilterLibraryDlg::Dialog::acceptData()
81 {
82 //  if (mode() != QFileDialogP::AnyFile)
83 //    return SUIT_FileDlg::acceptData();
84
85   return true;
86 }
87
88 /*!
89  *  Class       : SMESHGUI_FilterLibraryDlg
90  *  Description : Dialog to specify filters for VTK viewer
91  */
92
93 //=======================================================================
94 // name    : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
95 // Purpose : Constructor
96 //=======================================================================
97 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg (SMESHGUI* theModule,
98                                                       QWidget* parent,
99                                                       const QValueList<int>& theTypes,
100                                                       const int              theMode,
101                                                       const char*            theName)
102      : QDialog( parent, theName, true, WStyle_Customize |
103                 WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
104      mySMESHGUI( theModule )
105 {
106   construct(theTypes, theMode);
107 }
108
109 //=======================================================================
110 // name    : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
111 // Purpose : Constructor
112 //=======================================================================
113 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg (SMESHGUI* theModule,
114                                                       QWidget* parent,
115                                                       const int   theType,
116                                                       const int   theMode,
117                                                       const char* theName)
118      : QDialog( parent, theName, true, WStyle_Customize |
119                 WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
120      mySMESHGUI( theModule )
121 {
122   QValueList<int> aTypes;
123   aTypes.append(theType);
124   construct(aTypes, theMode);
125 }
126
127 //=======================================================================
128 // name    : SMESHGUI_FilterLibraryDlg::construct
129 // Purpose : Construct dialog (called by constructor)
130 //=======================================================================
131 void SMESHGUI_FilterLibraryDlg::construct (const QValueList<int>& theTypes,
132                                            const int theMode)
133 {
134   myTypes = theTypes;
135   myMode  = theMode;
136
137   QVBoxLayout* aDlgLay = new QVBoxLayout(this, MARGIN, SPACING);
138
139   myMainFrame        = createMainFrame  (this);
140   QFrame* aBtnFrame  = createButtonFrame(this);
141
142   aDlgLay->addWidget(myMainFrame);
143   aDlgLay->addWidget(aBtnFrame);
144
145   aDlgLay->setStretchFactor(myMainFrame, 1);
146
147   Init(myTypes, myMode);
148 }
149
150 //=======================================================================
151 // name    : SMESHGUI_FilterLibraryDlg::createMainFrame
152 // Purpose : Create frame containing dialog's input fields
153 //=======================================================================
154 QFrame* SMESHGUI_FilterLibraryDlg::createMainFrame (QWidget* theParent)
155 {
156   QGroupBox* aMainFrame = new QGroupBox(1, Qt::Horizontal, theParent);
157   aMainFrame->setFrameStyle(QFrame::NoFrame);
158   aMainFrame->setInsideMargin(0);
159
160   // library name
161
162   QGroupBox* aGrp = new QGroupBox(1, Qt::Vertical, aMainFrame);
163   aGrp->setFrameStyle(QFrame::NoFrame);
164   aGrp->setInsideMargin(0);
165
166   new QLabel(tr("LIBRARY_FILE"), aGrp);
167   myFileName = new QLineEdit(aGrp);
168   myOpenBtn = new QPushButton(aGrp);
169   myOpenBtn->setPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap(
170     "SUIT", tr("ICON_FILE_OPEN")));
171
172   // filters list box
173
174   aGrp = new QGroupBox(1, Qt::Vertical, tr("FILTER_NAMES"), aMainFrame);
175   QFrame* aFrame = new QFrame(aGrp);
176   myListBox = new QListBox(aFrame);
177   myAddBtn = new QPushButton(tr("ADD"), aFrame);
178   myDeleteBtn = new QPushButton(tr("DELETE"), aFrame);
179
180   QGridLayout* aLay = new QGridLayout(aFrame, 3, 2, 0, 5);
181   aLay->addMultiCellWidget(myListBox, 0, 2, 0, 0);
182   aLay->addWidget(myAddBtn, 0, 1);
183   aLay->addWidget(myDeleteBtn, 1, 1);
184   QSpacerItem* aVSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
185   aLay->addItem(aVSpacer, 2, 1);
186
187   // filter name
188
189   myNameGrp = new QGroupBox(1, Qt::Vertical, aMainFrame);
190   myNameGrp->setFrameStyle(QFrame::NoFrame);
191   myNameGrp->setInsideMargin(0);
192   new QLabel(tr("FILTER_NAME"), myNameGrp);
193   myName = new QLineEdit(myNameGrp);
194
195   // table
196
197   myTable = new SMESHGUI_FilterTable( mySMESHGUI, aMainFrame, myTypes);
198   myTable->SetEditable(myMode == EDIT);
199   myTable->SetLibsEnabled(false);
200
201   myListBox->setMinimumHeight((int)(myTable->sizeHint().height() * 0.5));
202   myListBox->setRowMode(QListBox::FitToWidth);
203   myListBox->setSelectionMode(QListBox::Single);
204
205   myOpenBtn->setAutoDefault(false);
206   myAddBtn->setAutoDefault(false);
207   myDeleteBtn->setAutoDefault(false);
208
209   // connect signals and slots
210
211   connect(myFileName, SIGNAL(returnPressed()), this, SLOT(onReturnPressed()));
212   connect(myOpenBtn , SIGNAL(clicked()), this, SLOT(onBrowse()));
213
214   connect(myListBox, SIGNAL(highlighted(const QString&)),
215            this, SLOT(onFilterChanged(const QString&)));
216
217   connect(myAddBtn, SIGNAL(clicked()), this, SLOT(onAddBtnPressed()));
218   connect(myDeleteBtn, SIGNAL(clicked()), this, SLOT(onDeleteBtnPressed()));
219
220   connect(myName, SIGNAL(textChanged(const QString&)),
221            this, SLOT(onFilterNameChanged(const QString&)));
222
223   connect(myTable, SIGNAL(EntityTypeChanged(const int)),
224            this, SLOT(onEntityTypeChanged(const int)));
225
226   connect(myTable, SIGNAL(NeedValidation()), this, SLOT(onNeedValidation()));
227
228   return aMainFrame;
229 }
230
231 //=======================================================================
232 // name    : SMESHGUI_FilterLibraryDlg::createButtonFrame
233 // Purpose : Create frame containing buttons
234 //=======================================================================
235 QFrame* SMESHGUI_FilterLibraryDlg::createButtonFrame (QWidget* theParent)
236 {
237   QGroupBox* aGrp = new QGroupBox(1, Qt::Vertical, theParent);
238
239   myButtons[ BTN_OK    ] = new QPushButton(tr("SMESH_BUT_OK"   ), aGrp);
240   myButtons[ BTN_Apply ] = new QPushButton(tr("SMESH_BUT_APPLY"), aGrp);
241
242   QLabel* aLbl = new QLabel(aGrp);
243   aLbl->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
244
245   myButtons[ BTN_Cancel ] = new QPushButton(tr("SMESH_BUT_CANCEL"), aGrp);
246   myButtons[ BTN_Close  ] = new QPushButton(tr("SMESH_BUT_CLOSE"), aGrp);
247
248   connect(myButtons[ BTN_OK     ], SIGNAL(clicked()), SLOT(onOk()));
249   connect(myButtons[ BTN_Cancel ], SIGNAL(clicked()), SLOT(onClose()));
250   connect(myButtons[ BTN_Close  ], SIGNAL(clicked()), SLOT(onClose()));
251   connect(myButtons[ BTN_Apply  ], SIGNAL(clicked()), SLOT(onApply()));
252
253   QMap<int, QPushButton*>::iterator anIter;
254   for (anIter = myButtons.begin(); anIter != myButtons.end(); ++anIter)
255     anIter.data()->setAutoDefault(false);
256
257   updateMainButtons();
258
259   return aGrp;
260 }
261
262 //=======================================================================
263 // name    : SMESHGUI_FilterLibraryDlg::updateMainButtons
264 // Purpose : Update visibility of main buttons (OK, Cancel, Close ...)
265 //=======================================================================
266 void SMESHGUI_FilterLibraryDlg::updateMainButtons()
267 {
268   if (myTypes.count() == 1) {
269     myButtons[ BTN_Cancel ]->show();
270     myButtons[ BTN_Apply  ]->hide();
271     myButtons[ BTN_Close  ]->hide();
272   } else {
273     myButtons[ BTN_Cancel ]->hide();
274     myButtons[ BTN_Apply  ]->show();
275     myButtons[ BTN_Close  ]->show();
276   }
277 }
278
279 //=======================================================================
280 // name    : SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg
281 // Purpose : Destructor
282 //=======================================================================
283 SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg()
284 {
285 }
286
287 //=======================================================================
288 // name    : SMESHGUI_FilterLibraryDlg::Init
289 // Purpose : Init dialog fields, connect signals and slots, show dialog
290 //=======================================================================
291 void SMESHGUI_FilterLibraryDlg::Init (const int type, const int theMode)
292 {
293   QValueList<int> aTypes;
294   aTypes.append(type);
295   Init(aTypes, theMode);
296 }
297
298 //=======================================================================
299 // name    : SMESHGUI_FilterLibraryDlg::Init
300 // Purpose : Init dialog fields, connect signals and slots, show dialog
301 //=======================================================================
302 void SMESHGUI_FilterLibraryDlg::Init (const QValueList<int>& theTypes,
303                                       const int theMode)
304 {
305   myMode = theMode;
306   myTypes = theTypes;
307   myTable->Init(theTypes);
308   myCurrFilterName = "";
309   myCurrFilter = -1;
310   myListBox->clear();
311   myName->clear();
312   myTable->Clear();
313
314   updateControlsVisibility();
315   setEnabled(true);
316
317   connect( mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
318   connect( mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
319
320   if (myMode == ADD_TO)
321   {
322     setCaption(tr("ADD_TO_TLT"));
323     if (myFileName->text().isEmpty())
324       myFileName->setText(getDefaultLibraryName());
325     processNewLibrary();
326   }
327   else if (myMode == COPY_FROM)
328   {
329     setCaption(tr("COPY_FROM_TLT"));
330     if (myFileName->text().isEmpty())
331       myFileName->setText(getDefaultLibraryName());
332     processNewLibrary();
333     if (myListBox->count() > 0)
334       myListBox->setCurrentItem(0);
335   }
336   else
337   {
338     setCaption(tr("EDIT_LIB_TLT"));
339     if (myFileName->text().isEmpty())
340       myFileName->setText(getDefaultLibraryName());
341     processNewLibrary();
342     if (myListBox->count() > 0)
343       myListBox->setCurrentItem(0);
344   }
345
346   this->show();
347
348   updateMainButtons();
349   isPermissionValid(false);
350 }
351
352 //=======================================================================
353 // name    : SMESHGUI_FilterLibraryDlg::updateControlsVisibility
354 // Purpose : Update visibility of controls in accordance with myMode
355 //=======================================================================
356 void SMESHGUI_FilterLibraryDlg::updateControlsVisibility()
357 {
358   if (myMode == ADD_TO)
359   {
360     myNameGrp->show();
361     myNameGrp->setEnabled(true);
362     myAddBtn->hide();
363     myDeleteBtn->hide();
364     myTable->SetEditable(false);
365   }
366   else if (myMode == COPY_FROM)
367   {
368     myNameGrp->hide();
369     myNameGrp->setEnabled(false);
370     myAddBtn->hide();
371     myDeleteBtn->hide();
372     myTable->SetEditable(false);
373   }
374   else if (myMode == EDIT)
375   {
376     myNameGrp->show();
377     myNameGrp->setEnabled(true);
378     myAddBtn->show();
379     myDeleteBtn->show();
380     myTable->SetEditable(true);
381   }
382
383   qApp->processEvents();
384   updateGeometry();
385   adjustSize();
386 }
387
388 //=======================================================================
389 // name    : SMESHGUI_FilterLibraryDlg::onApply
390 // Purpose : SLOT called when "Apply" button pressed.
391 //=======================================================================
392 bool SMESHGUI_FilterLibraryDlg::onApply()
393 {
394   if (!isValid(true) || !isPermissionValid(false))
395     return false;
396
397   if (myLibrary->_is_nil()) {
398     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
399                              tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
400     return false;
401   }
402
403   const char* aName = myFileName->text().latin1();
404   if (strcmp(myLibrary->GetFileName(), aName) != 0)
405     myLibrary->SetFileName(aName);
406
407   bool aResult = false;
408
409   if (myMode == COPY_FROM || myListBox->count() == 0) {
410     aResult = true;
411   } else if (myMode == EDIT || myMode == ADD_TO) {
412     SMESH::Filter_var aFilter = createFilter();
413     if (!myLibrary->Replace(myCurrFilterName, myName->text(), aFilter.in())) {
414       QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
415                                tr("ERROR_OF_EDITING"), QMessageBox::Ok);
416       aResult = false;
417     }
418     else
419       aResult = true;
420   }
421
422   if (aResult && myMode != COPY_FROM)
423     aResult = myLibrary->Save();
424
425   if (aResult) {
426     char* aFileName = myLibrary->GetFileName();
427     getDefaultLibraryName() = QString(aFileName);
428     delete aFileName;
429   } else if (myMode != COPY_FROM) {
430     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
431                              tr("ERROR_OF_SAVING"), QMessageBox::Ok);
432   } else {
433   }
434
435   return aResult;
436 }
437
438 //=======================================================================
439 // name    : SMESHGUI_FilterLibraryDlg::onOk
440 // Purpose : SLOT called when "Ok" button pressed.
441 //           Assign filters VTK viewer and close dialog
442 //=======================================================================
443 void SMESHGUI_FilterLibraryDlg::onOk()
444 {
445   if (onApply())
446   {
447     disconnect( mySMESHGUI, 0, this, 0);
448     mySMESHGUI->ResetState();
449     accept();
450   }
451 }
452
453 //=======================================================================
454 // name    : SMESHGUI_FilterLibraryDlg::onClose
455 // Purpose : SLOT called when "Close" button pressed. Close dialog
456 //=======================================================================
457 void SMESHGUI_FilterLibraryDlg::onClose()
458 {
459   disconnect( mySMESHGUI, 0, this, 0);
460   mySMESHGUI->ResetState();
461   reject();
462 }
463
464 //=======================================================================
465 // name    : SMESHGUI_FilterLibraryDlg::onDeactivate
466 // Purpose : SLOT called when dialog must be deativated
467 //=======================================================================
468 void SMESHGUI_FilterLibraryDlg::onDeactivate()
469 {
470   setEnabled(false);
471 }
472
473 //=======================================================================
474 // name    : SMESHGUI_FilterLibraryDlg::enterEvent
475 // Purpose : Event filter
476 //=======================================================================
477 void SMESHGUI_FilterLibraryDlg::enterEvent(QEvent*)
478 {
479   setEnabled(true);
480 }
481
482 //=================================================================================
483 // function : closeEvent()
484 // purpose  : Close dialog
485 //=================================================================================
486 void SMESHGUI_FilterLibraryDlg::closeEvent(QCloseEvent* e)
487 {
488   onClose();
489 }
490
491 //=======================================================================
492 // name    : SMESHGUI_FilterLibraryDlg::getFileName
493 // Purpose : Get file name
494 //=======================================================================
495 QString SMESHGUI_FilterLibraryDlg::getFileName() const
496 {
497   return myFileName != 0 ? myFileName->text() : QString("");
498 }
499
500 //================================================================
501 // Function : setFileName
502 // Purpose  : Set file name to line edit
503 //================================================================
504 void SMESHGUI_FilterLibraryDlg::setFileName(const QString& txt, const bool autoExt)
505 {
506   if (myFileName == 0)
507     return;
508   myFileName->setText(autoExt ? autoExtension(txt) : txt);
509 }
510
511 //================================================================
512 // Function : autoExtension
513 // Purpose  : Append extension to the file name
514 //================================================================
515 QString SMESHGUI_FilterLibraryDlg::autoExtension(const QString& theFileName) const
516 {
517   QString anExt = theFileName.section('.', -1);
518   return anExt != "xml" && anExt != "XML" ? theFileName + ".xml" : theFileName;
519 }
520
521 //================================================================
522 // Function : filterWildCards
523 // Purpose  :
524 //================================================================
525 QStringList SMESHGUI_FilterLibraryDlg::filterWildCards(const QString& theFilter) const
526 {
527   QStringList res;
528
529   int b = theFilter.findRev("(");
530   int e = theFilter.findRev(")");
531   if (b != -1 && e != -1)
532   {
533     QString content = theFilter.mid(b + 1, e - b - 1).stripWhiteSpace();
534     QStringList lst = QStringList::split(" ", content);
535     for (QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it)
536       if ((*it).find(".") != -1)
537         res.append((*it).stripWhiteSpace());
538   }
539   return res;
540 }
541
542 //=======================================================================
543 // name    : SMESHGUI_FilterLibraryDlg::prepareFilters
544 // Purpose : Prepare filters for dialog
545 //=======================================================================
546 QStringList SMESHGUI_FilterLibraryDlg::prepareFilters() const
547 {
548   static QStringList aList;
549   if (aList.isEmpty())
550   {
551     aList.append(tr("XML_FILT"));
552     //aList.append(tr("ALL_FILES_FILTER"));
553   }
554
555   return aList;
556 }
557
558 //================================================================
559 // Function : onBrowse
560 // Purpose  : SLOT. Display "Open file" dialog for chosing library name
561 //================================================================
562 void SMESHGUI_FilterLibraryDlg::onBrowse()
563 {
564   Dialog* aDlg = new Dialog(this, true);
565   aDlg->setCaption(tr("OPEN_LIBRARY"));
566
567   //aDlg->setMode(myMode == COPY_FROM ? QFileDialogP::ExistingFile : QFileDialogP::AnyFile);
568   aDlg->setMode(myMode == COPY_FROM ? QFileDialog::ExistingFile : QFileDialog::AnyFile);
569   aDlg->setFilters(prepareFilters());
570   aDlg->setSelection(getFileName());
571
572   QPushButton* anOkBtn = (QPushButton*)aDlg->child("OK", "QPushButton");
573   if (anOkBtn != 0)
574     anOkBtn->setText(tr("SMESH_BUT_OK"));
575
576   if (aDlg->exec() != Accepted)
577     return;
578
579   QString fName = aDlg->selectedFile();
580
581   if (fName.isEmpty())
582     return;
583
584   if (QFileInfo(fName).extension().isEmpty())
585     fName = autoExtension(fName);
586
587   fName = QDir::convertSeparators(fName);
588   QString prev = QDir::convertSeparators(getFileName());
589
590   if (prev == fName)
591     return;
592
593   setFileName(fName);
594
595   QString aName = myListBox->text(myListBox->count() - 1);
596   processNewLibrary();
597
598   if (myMode == ADD_TO)
599   {
600     myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
601     myCurrFilterName = "";
602     myCurrFilter = -1;
603     addFilterToLib(aName);
604   }
605
606   isPermissionValid(false);
607 }
608
609 //=======================================================================
610 // name    : SMESHGUI_FilterLibraryDlg::processNewLibrary
611 // Purpose : SLOT. Calleds when file name changed
612 //=======================================================================
613 void SMESHGUI_FilterLibraryDlg::processNewLibrary()
614 {
615   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
616   if (aFilterMgr->_is_nil())
617     return;
618
619   myLibrary = aFilterMgr->LoadLibrary(autoExtension(getFileName()));
620   if (myLibrary->_is_nil()) {
621     if (myMode == COPY_FROM) {
622       QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
623                                tr("ERROR_LOAD"), QMessageBox::Ok);
624       return;
625     } else {
626       myLibrary = aFilterMgr->CreateLibrary();
627       myLibrary->SetFileName(getFileName().latin1());
628     }
629   }
630
631   updateList();
632 }
633
634 //=======================================================================
635 // name    : SMESHGUI_FilterLibraryDlg::updateList
636 // Purpose : Fill list box with filter names
637 //=======================================================================
638 void SMESHGUI_FilterLibraryDlg::updateList()
639 {
640   QStringList aList;
641   SMESH::string_array_var aNames = myLibrary->GetNames((SMESH::ElementType)myTable->GetType());
642   for (int i = 0, n = aNames->length(); i < n; i++)
643     aList.append(QString(aNames[ i ]));
644   myListBox->clear();
645   myListBox->insertStringList(aList);
646   if (myListBox->count() == 0)
647   {
648     myTable->Clear(myTable->GetType());
649     myName->clear();
650     myName->setEnabled(false);
651     myTable->SetEnabled(false);
652   }
653   else
654   {
655     myName->setEnabled(true);
656     myTable->SetEnabled(true);
657     if (myListBox->count())
658     {
659       myCurrFilterName = "";
660       myListBox->setCurrentItem(0);
661     }
662   }
663 }
664
665 //=======================================================================
666 // name    : SMESHGUI_FilterLibraryDlg::isNameValid
667 // Purpose : Verify validity if entered data
668 //=======================================================================
669 bool SMESHGUI_FilterLibraryDlg::isNameValid(const bool theMess) const
670 {
671   // verify validity of filter name
672   if (myName->isEnabled() && !myCurrFilterName.isEmpty()) {
673     QString aCurrName = myName->text();
674     if (aCurrName.isEmpty()) {
675       if (theMess)
676         QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
677                                  tr("EMPTY_FILTER_NAME"), QMessageBox::Ok);
678       return false;
679     }
680
681     SMESH::string_array_var aNames = myLibrary->GetAllNames();
682     for (int f = 0, n = aNames->length(); f < n; f++) {
683       if (aNames[ f ] == aCurrName && aNames[ f ] != myCurrFilterName) {
684         if (theMess)
685           QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
686                                    tr("ERROR_FILTER_NAME"), QMessageBox::Ok);
687         return false;
688       }
689     }
690   }
691
692   return true;
693 }
694
695 //=======================================================================
696 // name    : SMESHGUI_FilterLibraryDlg::isPermissionValid
697 // Purpose : Verify write permission on file
698 //=======================================================================
699 bool SMESHGUI_FilterLibraryDlg::isPermissionValid(const bool theIsExistingOnly)
700 {
701   if (myMode == COPY_FROM)
702     return true;
703
704   // Verify write permission
705   bool isWritable = false;
706
707   QString fName(myFileName->text());
708   if (QFileInfo(fName).extension().isEmpty())
709     fName = autoExtension(fName);
710
711   fName = QDir::convertSeparators(fName);
712
713   if (QFileInfo(fName).exists()) {
714     isWritable = QFileInfo(fName).isWritable();
715   } else if (!theIsExistingOnly) {
716     QFileInfo aDirInfo(QFileInfo(fName).dirPath(true));
717     isWritable = aDirInfo.isWritable();
718     /*if (QDir(QFileInfo(fName).dirPath(true)).exists() ||
719          QDir().mkdir(QFileInfo(fName).dirPath(true)))
720     {
721       QFile aFile(fName);
722       if (aFile.open(IO_WriteOnly))
723         isWritable = true;
724       else
725         aFile.close();
726     }
727     */
728   } else {
729     isWritable = true;
730   }
731
732   if (!isWritable) {
733     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
734                              tr("NO_PERMISSION"), QMessageBox::Ok);
735     return false;
736   }
737
738   return true;
739 }
740
741 //=======================================================================
742 // name    : SMESHGUI_FilterLibraryDlg::isValid
743 // Purpose : Verify validity if entered data
744 //=======================================================================
745 bool SMESHGUI_FilterLibraryDlg::isValid(const bool theMess) const
746 {
747   // verify validity of table
748   if (!myTable->IsValid(theMess) || !isNameValid(theMess))
749     return false;
750   else
751     return true;
752 }
753
754 //=======================================================================
755 // name    : SMESHGUI_FilterLibraryDlg::onFilterChanged
756 // Purpose : SLOT. Called when selected filter of library  changed
757 //=======================================================================
758 void SMESHGUI_FilterLibraryDlg::onFilterChanged(const QString& theName)
759 {
760   if (myLibrary->_is_nil())
761     return;
762
763   // Save parameters of filter if it was changed
764
765   if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
766   {
767     if (!isValid(true))
768     {
769       myListBox->blockSignals(true);
770       myListBox->setCurrentItem(myCurrFilter);
771       myListBox->blockSignals(false);
772       return;
773     }
774
775     SMESH::Filter_var aFilter = createFilter();
776     myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
777   }
778
779   // Fill table with filter parameters
780
781   SMESH::Filter_var aFilter = myLibrary->Copy(theName);
782   myCurrFilterName = theName;
783   myCurrFilter = myListBox->currentItem();
784   myName->setText(theName);
785
786
787   SMESH::Filter::Criteria_var aCriteria;
788
789   myTable->Clear(myTable->GetType());
790
791   if (!aFilter->GetCriteria(aCriteria))
792     return;
793
794   for (int i = 0, n = aCriteria->length(); i < n; i++)
795     myTable->AddCriterion(aCriteria[ i ], myTable->GetType());
796
797   myTable->Update();
798 }
799
800 //=======================================================================
801 // name    : SMESHGUI_FilterLibraryDlg::onReturnPressed
802 // Purpose : SLOT. Called when enter button is pressed in library name field
803 //           Reload library
804 //=======================================================================
805 void SMESHGUI_FilterLibraryDlg::onReturnPressed()
806 {
807   QString aName = myListBox->text(myListBox->count() - 1);
808
809   processNewLibrary();
810
811   if (myMode == ADD_TO)
812   {
813     myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
814     myCurrFilterName = "";
815     myCurrFilter = -1;
816     addFilterToLib(aName);
817   }
818
819   isPermissionValid(false);
820 }
821
822 //=======================================================================
823 // name    : SMESHGUI_FilterLibraryDlg::enableMainButtons
824 // Purpose : Update state of "OK", "Cancel" buttons
825 //=======================================================================
826 void SMESHGUI_FilterLibraryDlg::enableMainButtons()
827 {
828   /*bool isEnabled = isValid(false);
829   if (myButtons.contains(BTN_OK))
830     myButtons[ BTN_OK ]->setEnabled(isEnabled);
831   else if (myButtons.contains(BTN_Apply))
832     myButtons[ BTN_OK ]->setEnabled(isEnabled);
833   if (myButtons.contains(BTN_Cancel))
834     myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
835   else if (myButtons.contains(BTN_Close))
836     myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
837     */
838 }
839
840 //=======================================================================
841 // name    : SMESHGUI_FilterLibraryDlg::createFilter
842 // Purpose : Cerate filter in accordance with library
843 //=======================================================================
844 SMESH::Filter_ptr SMESHGUI_FilterLibraryDlg::createFilter(const int theType)
845 {
846   int n = myTable->NumRows(theType);
847
848   SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
849   aCriteria->length(n);
850
851   for (int i = 0; i < n; i++)
852   {
853     SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
854     myTable->GetCriterion(i, aCriterion);
855     aCriteria[ i ] = aCriterion;
856   }
857
858   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
859   SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
860   aFilter->SetCriteria(aCriteria.in());
861
862   return aFilter._retn();
863 }
864
865 //=======================================================================
866 // name    : SMESHGUI_FilterLibraryDlg::onAddBtnPressed
867 // Purpose : SLOT. Called when "Add" button pressed
868 //           Add new filter to the end of library
869 //=======================================================================
870 void SMESHGUI_FilterLibraryDlg::onAddBtnPressed()
871 {
872   // Save parameters of filter if it was changed
873   if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
874   {
875     if (!isValid(true))
876       return;
877
878     SMESH::Filter_var aFilter = createFilter();
879     myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
880   }
881
882   addFilterToLib(getDefaultFilterName());
883 }
884
885 //=======================================================================
886 // name    : onAddBtnPressed()
887 // Purpose : SLOT. Called when "Add" button pressed
888 //           Add new filter to the end of library
889 //=======================================================================
890 void SMESHGUI_FilterLibraryDlg::addFilterToLib (const QString& theName)
891 {
892   if (myLibrary->_is_nil()) {
893     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
894                              tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
895     return;
896   }
897
898   // create filter
899   SMESH::Filter_var aFilter = createFilter();
900
901   // if name of filter already exist in the library assign default name for the filter
902   QString aName(theName);
903   SMESH::string_array_var aNames = myLibrary->GetAllNames();
904   for (int i = 0, n = aNames->length(); i < n; i++)
905     if (aName == QString(aNames[ i ]))
906     {
907       aName = getDefaultFilterName();
908       break;
909     }
910
911   // add new filter in library
912   bool aResult = !aFilter->GetPredicate()->_is_nil()
913     ? myLibrary->Add(aName.latin1(), aFilter)
914     : myLibrary->AddEmpty(aName.latin1(), (SMESH::ElementType)myTable->GetType());
915
916   if (!aResult) {
917     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
918                              tr("ERROR_OF_ADDING"), QMessageBox::Ok);
919   }
920
921   updateList();
922   myCurrFilterName = "";
923   myCurrFilter = -1;
924   setSelected(aName);
925
926   if (theName != aName)
927     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
928                              tr("ASSIGN_NEW_NAME").arg(theName).arg(aName), QMessageBox::Ok);
929 }
930
931 //=======================================================================
932 // name    : SMESHGUI_FilterLibraryDlg::getDefaultLibraryName
933 // Purpose : Get default library name
934 //=======================================================================
935 QString& SMESHGUI_FilterLibraryDlg::getDefaultLibraryName() const
936 {
937   static QString aName;
938   if (aName.isEmpty())
939   {
940     QString aHomeDir = QDir(QDir::home()).absPath();
941     aName = aHomeDir + "/" + tr ("LIB_NAME");
942   }
943   return aName;
944 }
945
946 //=======================================================================
947 // name    : SMESHGUI_FilterLibraryDlg::getDefaultFilterName
948 // Purpose : Create default filter name
949 //=======================================================================
950 QString SMESHGUI_FilterLibraryDlg::getDefaultFilterName() const
951 {
952   QString aName;
953
954   if (myTable->GetType() == SMESH::NODE)
955     aName = tr("NODE");
956   else if (myTable->GetType() == SMESH::EDGE)
957     aName = tr("EDGE");
958   else if (myTable->GetType() == SMESH::FACE)
959     aName = tr("FACE");
960   else if (myTable->GetType() == SMESH::VOLUME)
961     aName = tr("VOLUME");
962   else if (myTable->GetType() == SMESH::ALL)
963     aName = tr("ELEMENT");
964   else
965     aName = tr("SELECTION");
966
967   aName += tr("FILTER");
968
969
970   QMap< QString, int > anAllNames;
971   SMESH::string_array_var aNames = myLibrary->GetAllNames();
972   for(int i = 0, n = aNames->length(); i < n; i++)
973     anAllNames[ QString(aNames[ i ]) ] = -1;
974
975   bool isNotValid = true;
976   int k = 1;
977   QString aNewName;
978   while (isNotValid)
979   {
980     isNotValid = false;
981     aNewName = aName + "_" + QString("%1").arg(k);
982     if (anAllNames.contains(aNewName))
983     {
984       isNotValid = true;
985       k++;
986     }
987   }
988
989   return aNewName;
990 }
991
992 //=======================================================================
993 // name    : SMESHGUI_FilterLibraryDlg::setSelected
994 // Purpose : set selected item in list box containing filters
995 //=======================================================================
996 bool SMESHGUI_FilterLibraryDlg::setSelected(const QString& theName)
997 {
998   int anIndex = getIndex(theName);
999   if (anIndex != -1)
1000   {
1001     myListBox->setCurrentItem(anIndex);
1002     myCurrFilterName = theName;
1003     myCurrFilter = anIndex;
1004   }
1005   return anIndex != -1;
1006 }
1007
1008 //=======================================================================
1009 // name    : SMESHGUI_FilterLibraryDlg::getIndex
1010 // Purpose : Get index of the filter in list box
1011 //=======================================================================
1012 int SMESHGUI_FilterLibraryDlg::getIndex(const QString& theName) const
1013 {
1014   for (int i = 0, n = myListBox->count(); i < n; i++)
1015     if (myListBox->text(i) == theName)
1016       return i;
1017   return -1;
1018 }
1019
1020 //=======================================================================
1021 // name    : SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed
1022 // Purpose : SLOT. Called when "Delete" button pressed
1023 //=======================================================================
1024 void SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed()
1025 {
1026   if (myLibrary->_is_nil()) {
1027     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
1028                              tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
1029     return;
1030   }
1031
1032   int anIndex = getIndex(myCurrFilterName);
1033
1034   if (anIndex == -1 || !myLibrary->Delete(myCurrFilterName.latin1())) {
1035     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
1036                              tr("ERROR_OF_DELETING"), QMessageBox::Ok);
1037   } else {
1038     myCurrFilterName = "";
1039     myCurrFilter = -1;
1040     myListBox->removeItem(anIndex);
1041
1042     if (anIndex >= 1)
1043       myListBox->setSelected(anIndex - 1, true);
1044     else if (anIndex == 0 && myListBox->count() > 0)
1045       myListBox->setSelected(0, true);
1046     else
1047       myTable->Clear();
1048   }
1049
1050   myTable->SetEnabled(myListBox->count() > 0);
1051   if (myListBox->count() == 0) {
1052     myName->setText("");
1053     myName->setEnabled(false);
1054   }
1055 }
1056
1057 //=======================================================================
1058 // name    : onFilterNameChanged()
1059 // Purpose : SLOT. Called when name of filter changed
1060 //           Change filter name in list box
1061 //=======================================================================
1062 void SMESHGUI_FilterLibraryDlg::onFilterNameChanged (const QString& theName)
1063 {
1064   int aCurrItem = myListBox->currentItem();
1065   if (aCurrItem == -1)
1066     return;
1067
1068   myListBox->blockSignals(true);
1069   myListBox->changeItem(theName, aCurrItem);
1070   myListBox->blockSignals(false);
1071 }
1072
1073 //=======================================================================
1074 // name    : SMESHGUI_FilterLibraryDlg::SetTable
1075 // Purpose : Set table
1076 //=======================================================================
1077 void SMESHGUI_FilterLibraryDlg::SetTable(const SMESHGUI_FilterTable* theTable)
1078 {
1079   myTable->Copy(theTable);
1080   myName->setText(getDefaultFilterName());
1081   addFilterToLib(myName->text());
1082   myTable->Update();
1083 }
1084
1085 //=======================================================================
1086 // name    : SMESHGUI_FilterLibraryDlg::GetTable
1087 // Purpose : Get table
1088 //=======================================================================
1089 const SMESHGUI_FilterTable* SMESHGUI_FilterLibraryDlg::GetTable() const
1090 {
1091   return myTable;
1092 }
1093
1094
1095 //=======================================================================
1096 // name    : SMESHGUI_FilterLibraryDlg::onEntityTypeChanged
1097 // Purpose : SLOT. Called when entiyt type changed
1098 //=======================================================================
1099 void SMESHGUI_FilterLibraryDlg::onEntityTypeChanged(const int theType)
1100 {
1101   if (myLibrary->_is_nil())
1102     return;
1103
1104   myName->clear();
1105   myCurrFilterName = "";
1106   myCurrFilter = -1;
1107   updateList();
1108   if (myListBox->count())
1109     myListBox->setCurrentItem(0);
1110 }
1111
1112 //=======================================================================
1113 // name    : SMESHGUI_FilterLibraryDlg::onNeedValidation
1114 // Purpose :
1115 //=======================================================================
1116 void SMESHGUI_FilterLibraryDlg::onNeedValidation()
1117 {
1118   if (!myCurrFilterName.isEmpty())
1119   {
1120     bool valid = isValid(true);
1121     myTable->SetValidity(valid);
1122
1123     if (valid)
1124     {
1125       SMESH::Filter_var aFilter = createFilter(myTable->GetType());
1126       myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
1127     }
1128   }
1129 }