Salome HOME
The selections of elements corrected.
[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   int x, y;
347   mySMESHGUI->DefineDlgPosition(this, x, y);
348   this->move(x, y);
349
350   this->show();
351
352   updateMainButtons();
353   isPermissionValid(false);
354 }
355
356 //=======================================================================
357 // name    : SMESHGUI_FilterLibraryDlg::updateControlsVisibility
358 // Purpose : Update visibility of controls in accordance with myMode
359 //=======================================================================
360 void SMESHGUI_FilterLibraryDlg::updateControlsVisibility()
361 {
362   if (myMode == ADD_TO)
363   {
364     myNameGrp->show();
365     myNameGrp->setEnabled(true);
366     myAddBtn->hide();
367     myDeleteBtn->hide();
368     myTable->SetEditable(false);
369   }
370   else if (myMode == COPY_FROM)
371   {
372     myNameGrp->hide();
373     myNameGrp->setEnabled(false);
374     myAddBtn->hide();
375     myDeleteBtn->hide();
376     myTable->SetEditable(false);
377   }
378   else if (myMode == EDIT)
379   {
380     myNameGrp->show();
381     myNameGrp->setEnabled(true);
382     myAddBtn->show();
383     myDeleteBtn->show();
384     myTable->SetEditable(true);
385   }
386
387   qApp->processEvents();
388   updateGeometry();
389   adjustSize();
390 }
391
392 //=======================================================================
393 // name    : SMESHGUI_FilterLibraryDlg::onApply
394 // Purpose : SLOT called when "Apply" button pressed.
395 //=======================================================================
396 bool SMESHGUI_FilterLibraryDlg::onApply()
397 {
398   if (!isValid(true) || !isPermissionValid(false))
399     return false;
400
401   if (myLibrary->_is_nil()) {
402     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
403                              tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
404     return false;
405   }
406
407   const char* aName = myFileName->text().latin1();
408   if (strcmp(myLibrary->GetFileName(), aName) != 0)
409     myLibrary->SetFileName(aName);
410
411   bool aResult = false;
412
413   if (myMode == COPY_FROM || myListBox->count() == 0) {
414     aResult = true;
415   } else if (myMode == EDIT || myMode == ADD_TO) {
416     SMESH::Filter_var aFilter = createFilter();
417     if (!myLibrary->Replace(myCurrFilterName, myName->text(), aFilter.in())) {
418       QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
419                                tr("ERROR_OF_EDITING"), QMessageBox::Ok);
420       aResult = false;
421     }
422     else
423       aResult = true;
424   }
425
426   if (aResult && myMode != COPY_FROM)
427     aResult = myLibrary->Save();
428
429   if (aResult) {
430     char* aFileName = myLibrary->GetFileName();
431     getDefaultLibraryName() = QString(aFileName);
432     delete aFileName;
433   } else if (myMode != COPY_FROM) {
434     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
435                              tr("ERROR_OF_SAVING"), QMessageBox::Ok);
436   } else {
437   }
438
439   return aResult;
440 }
441
442 //=======================================================================
443 // name    : SMESHGUI_FilterLibraryDlg::onOk
444 // Purpose : SLOT called when "Ok" button pressed.
445 //           Assign filters VTK viewer and close dialog
446 //=======================================================================
447 void SMESHGUI_FilterLibraryDlg::onOk()
448 {
449   if (onApply())
450   {
451     disconnect( mySMESHGUI, 0, this, 0);
452     mySMESHGUI->ResetState();
453     accept();
454   }
455 }
456
457 //=======================================================================
458 // name    : SMESHGUI_FilterLibraryDlg::onClose
459 // Purpose : SLOT called when "Close" button pressed. Close dialog
460 //=======================================================================
461 void SMESHGUI_FilterLibraryDlg::onClose()
462 {
463   disconnect( mySMESHGUI, 0, this, 0);
464   mySMESHGUI->ResetState();
465   reject();
466 }
467
468 //=======================================================================
469 // name    : SMESHGUI_FilterLibraryDlg::onDeactivate
470 // Purpose : SLOT called when dialog must be deativated
471 //=======================================================================
472 void SMESHGUI_FilterLibraryDlg::onDeactivate()
473 {
474   setEnabled(false);
475 }
476
477 //=======================================================================
478 // name    : SMESHGUI_FilterLibraryDlg::enterEvent
479 // Purpose : Event filter
480 //=======================================================================
481 void SMESHGUI_FilterLibraryDlg::enterEvent(QEvent*)
482 {
483   setEnabled(true);
484 }
485
486 //=================================================================================
487 // function : closeEvent()
488 // purpose  : Close dialog
489 //=================================================================================
490 void SMESHGUI_FilterLibraryDlg::closeEvent(QCloseEvent* e)
491 {
492   onClose();
493 }
494
495 //=======================================================================
496 // name    : SMESHGUI_FilterLibraryDlg::getFileName
497 // Purpose : Get file name
498 //=======================================================================
499 QString SMESHGUI_FilterLibraryDlg::getFileName() const
500 {
501   return myFileName != 0 ? myFileName->text() : QString("");
502 }
503
504 //================================================================
505 // Function : setFileName
506 // Purpose  : Set file name to line edit
507 //================================================================
508 void SMESHGUI_FilterLibraryDlg::setFileName(const QString& txt, const bool autoExt)
509 {
510   if (myFileName == 0)
511     return;
512   myFileName->setText(autoExt ? autoExtension(txt) : txt);
513 }
514
515 //================================================================
516 // Function : autoExtension
517 // Purpose  : Append extension to the file name
518 //================================================================
519 QString SMESHGUI_FilterLibraryDlg::autoExtension(const QString& theFileName) const
520 {
521   QString anExt = theFileName.section('.', -1);
522   return anExt != "xml" && anExt != "XML" ? theFileName + ".xml" : theFileName;
523 }
524
525 //================================================================
526 // Function : filterWildCards
527 // Purpose  :
528 //================================================================
529 QStringList SMESHGUI_FilterLibraryDlg::filterWildCards(const QString& theFilter) const
530 {
531   QStringList res;
532
533   int b = theFilter.findRev("(");
534   int e = theFilter.findRev(")");
535   if (b != -1 && e != -1)
536   {
537     QString content = theFilter.mid(b + 1, e - b - 1).stripWhiteSpace();
538     QStringList lst = QStringList::split(" ", content);
539     for (QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it)
540       if ((*it).find(".") != -1)
541         res.append((*it).stripWhiteSpace());
542   }
543   return res;
544 }
545
546 //=======================================================================
547 // name    : SMESHGUI_FilterLibraryDlg::prepareFilters
548 // Purpose : Prepare filters for dialog
549 //=======================================================================
550 QStringList SMESHGUI_FilterLibraryDlg::prepareFilters() const
551 {
552   static QStringList aList;
553   if (aList.isEmpty())
554   {
555     aList.append(tr("XML_FILT"));
556     //aList.append(tr("ALL_FILES_FILTER"));
557   }
558
559   return aList;
560 }
561
562 //================================================================
563 // Function : onBrowse
564 // Purpose  : SLOT. Display "Open file" dialog for chosing library name
565 //================================================================
566 void SMESHGUI_FilterLibraryDlg::onBrowse()
567 {
568   Dialog* aDlg = new Dialog(this, true);
569   aDlg->setCaption(tr("OPEN_LIBRARY"));
570
571   //aDlg->setMode(myMode == COPY_FROM ? QFileDialogP::ExistingFile : QFileDialogP::AnyFile);
572   aDlg->setMode(myMode == COPY_FROM ? QFileDialog::ExistingFile : QFileDialog::AnyFile);
573   aDlg->setFilters(prepareFilters());
574   aDlg->setSelection(getFileName());
575
576   QPushButton* anOkBtn = (QPushButton*)aDlg->child("OK", "QPushButton");
577   if (anOkBtn != 0)
578     anOkBtn->setText(tr("SMESH_BUT_OK"));
579
580   if (aDlg->exec() != Accepted)
581     return;
582
583   QString fName = aDlg->selectedFile();
584
585   if (fName.isEmpty())
586     return;
587
588   if (QFileInfo(fName).extension().isEmpty())
589     fName = autoExtension(fName);
590
591   fName = QDir::convertSeparators(fName);
592   QString prev = QDir::convertSeparators(getFileName());
593
594   if (prev == fName)
595     return;
596
597   setFileName(fName);
598
599   QString aName = myListBox->text(myListBox->count() - 1);
600   processNewLibrary();
601
602   if (myMode == ADD_TO)
603   {
604     myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
605     myCurrFilterName = "";
606     myCurrFilter = -1;
607     addFilterToLib(aName);
608   }
609
610   isPermissionValid(false);
611 }
612
613 //=======================================================================
614 // name    : SMESHGUI_FilterLibraryDlg::processNewLibrary
615 // Purpose : SLOT. Calleds when file name changed
616 //=======================================================================
617 void SMESHGUI_FilterLibraryDlg::processNewLibrary()
618 {
619   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
620   if (aFilterMgr->_is_nil())
621     return;
622
623   myLibrary = aFilterMgr->LoadLibrary(autoExtension(getFileName()));
624   if (myLibrary->_is_nil()) {
625     if (myMode == COPY_FROM) {
626       QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
627                                tr("ERROR_LOAD"), QMessageBox::Ok);
628       return;
629     } else {
630       myLibrary = aFilterMgr->CreateLibrary();
631       myLibrary->SetFileName(getFileName().latin1());
632     }
633   }
634
635   updateList();
636 }
637
638 //=======================================================================
639 // name    : SMESHGUI_FilterLibraryDlg::updateList
640 // Purpose : Fill list box with filter names
641 //=======================================================================
642 void SMESHGUI_FilterLibraryDlg::updateList()
643 {
644   QStringList aList;
645   SMESH::string_array_var aNames = myLibrary->GetNames((SMESH::ElementType)myTable->GetType());
646   for (int i = 0, n = aNames->length(); i < n; i++)
647     aList.append(QString(aNames[ i ]));
648   myListBox->clear();
649   myListBox->insertStringList(aList);
650   if (myListBox->count() == 0)
651   {
652     myTable->Clear(myTable->GetType());
653     myName->clear();
654     myName->setEnabled(false);
655     myTable->SetEnabled(false);
656   }
657   else
658   {
659     myName->setEnabled(true);
660     myTable->SetEnabled(true);
661     if (myListBox->count())
662     {
663       myCurrFilterName = "";
664       myListBox->setCurrentItem(0);
665     }
666   }
667 }
668
669 //=======================================================================
670 // name    : SMESHGUI_FilterLibraryDlg::isNameValid
671 // Purpose : Verify validity if entered data
672 //=======================================================================
673 bool SMESHGUI_FilterLibraryDlg::isNameValid(const bool theMess) const
674 {
675   // verify validity of filter name
676   if (myName->isEnabled() && !myCurrFilterName.isEmpty()) {
677     QString aCurrName = myName->text();
678     if (aCurrName.isEmpty()) {
679       if (theMess)
680         QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
681                                  tr("EMPTY_FILTER_NAME"), QMessageBox::Ok);
682       return false;
683     }
684
685     SMESH::string_array_var aNames = myLibrary->GetAllNames();
686     for (int f = 0, n = aNames->length(); f < n; f++) {
687       if (aNames[ f ] == aCurrName && aNames[ f ] != myCurrFilterName) {
688         if (theMess)
689           QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
690                                    tr("ERROR_FILTER_NAME"), QMessageBox::Ok);
691         return false;
692       }
693     }
694   }
695
696   return true;
697 }
698
699 //=======================================================================
700 // name    : SMESHGUI_FilterLibraryDlg::isPermissionValid
701 // Purpose : Verify write permission on file
702 //=======================================================================
703 bool SMESHGUI_FilterLibraryDlg::isPermissionValid(const bool theIsExistingOnly)
704 {
705   if (myMode == COPY_FROM)
706     return true;
707
708   // Verify write permission
709   bool isWritable = false;
710
711   QString fName(myFileName->text());
712   if (QFileInfo(fName).extension().isEmpty())
713     fName = autoExtension(fName);
714
715   fName = QDir::convertSeparators(fName);
716
717   if (QFileInfo(fName).exists()) {
718     isWritable = QFileInfo(fName).isWritable();
719   } else if (!theIsExistingOnly) {
720     QFileInfo aDirInfo(QFileInfo(fName).dirPath(true));
721     isWritable = aDirInfo.isWritable();
722     /*if (QDir(QFileInfo(fName).dirPath(true)).exists() ||
723          QDir().mkdir(QFileInfo(fName).dirPath(true)))
724     {
725       QFile aFile(fName);
726       if (aFile.open(IO_WriteOnly))
727         isWritable = true;
728       else
729         aFile.close();
730     }
731     */
732   } else {
733     isWritable = true;
734   }
735
736   if (!isWritable) {
737     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
738                              tr("NO_PERMISSION"), QMessageBox::Ok);
739     return false;
740   }
741
742   return true;
743 }
744
745 //=======================================================================
746 // name    : SMESHGUI_FilterLibraryDlg::isValid
747 // Purpose : Verify validity if entered data
748 //=======================================================================
749 bool SMESHGUI_FilterLibraryDlg::isValid(const bool theMess) const
750 {
751   // verify validity of table
752   if (!myTable->IsValid(theMess) || !isNameValid(theMess))
753     return false;
754   else
755     return true;
756 }
757
758 //=======================================================================
759 // name    : SMESHGUI_FilterLibraryDlg::onFilterChanged
760 // Purpose : SLOT. Called when selected filter of library  changed
761 //=======================================================================
762 void SMESHGUI_FilterLibraryDlg::onFilterChanged(const QString& theName)
763 {
764   if (myLibrary->_is_nil())
765     return;
766
767   // Save parameters of filter if it was changed
768
769   if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
770   {
771     if (!isValid(true))
772     {
773       myListBox->blockSignals(true);
774       myListBox->setCurrentItem(myCurrFilter);
775       myListBox->blockSignals(false);
776       return;
777     }
778
779     SMESH::Filter_var aFilter = createFilter();
780     myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
781   }
782
783   // Fill table with filter parameters
784
785   SMESH::Filter_var aFilter = myLibrary->Copy(theName);
786   myCurrFilterName = theName;
787   myCurrFilter = myListBox->currentItem();
788   myName->setText(theName);
789
790
791   SMESH::Filter::Criteria_var aCriteria;
792
793   myTable->Clear(myTable->GetType());
794
795   if (!aFilter->GetCriteria(aCriteria))
796     return;
797
798   for (int i = 0, n = aCriteria->length(); i < n; i++)
799     myTable->AddCriterion(aCriteria[ i ], myTable->GetType());
800
801   myTable->Update();
802 }
803
804 //=======================================================================
805 // name    : SMESHGUI_FilterLibraryDlg::onReturnPressed
806 // Purpose : SLOT. Called when enter button is pressed in library name field
807 //           Reload library
808 //=======================================================================
809 void SMESHGUI_FilterLibraryDlg::onReturnPressed()
810 {
811   QString aName = myListBox->text(myListBox->count() - 1);
812
813   processNewLibrary();
814
815   if (myMode == ADD_TO)
816   {
817     myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
818     myCurrFilterName = "";
819     myCurrFilter = -1;
820     addFilterToLib(aName);
821   }
822
823   isPermissionValid(false);
824 }
825
826 //=======================================================================
827 // name    : SMESHGUI_FilterLibraryDlg::enableMainButtons
828 // Purpose : Update state of "OK", "Cancel" buttons
829 //=======================================================================
830 void SMESHGUI_FilterLibraryDlg::enableMainButtons()
831 {
832   /*bool isEnabled = isValid(false);
833   if (myButtons.contains(BTN_OK))
834     myButtons[ BTN_OK ]->setEnabled(isEnabled);
835   else if (myButtons.contains(BTN_Apply))
836     myButtons[ BTN_OK ]->setEnabled(isEnabled);
837   if (myButtons.contains(BTN_Cancel))
838     myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
839   else if (myButtons.contains(BTN_Close))
840     myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
841     */
842 }
843
844 //=======================================================================
845 // name    : SMESHGUI_FilterLibraryDlg::createFilter
846 // Purpose : Cerate filter in accordance with library
847 //=======================================================================
848 SMESH::Filter_ptr SMESHGUI_FilterLibraryDlg::createFilter(const int theType)
849 {
850   int n = myTable->NumRows(theType);
851
852   SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
853   aCriteria->length(n);
854
855   for (int i = 0; i < n; i++)
856   {
857     SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
858     myTable->GetCriterion(i, aCriterion);
859     aCriteria[ i ] = aCriterion;
860   }
861
862   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
863   SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
864   aFilter->SetCriteria(aCriteria.inout());
865
866   return aFilter._retn();
867 }
868
869 //=======================================================================
870 // name    : SMESHGUI_FilterLibraryDlg::onAddBtnPressed
871 // Purpose : SLOT. Called when "Add" button pressed
872 //           Add new filter to the end of library
873 //=======================================================================
874 void SMESHGUI_FilterLibraryDlg::onAddBtnPressed()
875 {
876   // Save parameters of filter if it was changed
877   if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
878   {
879     if (!isValid(true))
880       return;
881
882     SMESH::Filter_var aFilter = createFilter();
883     myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
884   }
885
886   addFilterToLib(getDefaultFilterName());
887 }
888
889 //=======================================================================
890 // name    : onAddBtnPressed()
891 // Purpose : SLOT. Called when "Add" button pressed
892 //           Add new filter to the end of library
893 //=======================================================================
894 void SMESHGUI_FilterLibraryDlg::addFilterToLib (const QString& theName)
895 {
896   if (myLibrary->_is_nil()) {
897     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
898                              tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
899     return;
900   }
901
902   // create filter
903   SMESH::Filter_var aFilter = createFilter();
904
905   // if name of filter already exist in the library assign default name for the filter
906   QString aName(theName);
907   SMESH::string_array_var aNames = myLibrary->GetAllNames();
908   for (int i = 0, n = aNames->length(); i < n; i++)
909     if (aName == QString(aNames[ i ]))
910     {
911       aName = getDefaultFilterName();
912       break;
913     }
914
915   // add new filter in library
916   bool aResult = !aFilter->GetPredicate()->_is_nil()
917     ? myLibrary->Add(aName.latin1(), aFilter)
918     : myLibrary->AddEmpty(aName.latin1(), (SMESH::ElementType)myTable->GetType());
919
920   if (!aResult) {
921     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
922                              tr("ERROR_OF_ADDING"), QMessageBox::Ok);
923   }
924
925   updateList();
926   myCurrFilterName = "";
927   myCurrFilter = -1;
928   setSelected(aName);
929
930   if (theName != aName)
931     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
932                              tr("ASSIGN_NEW_NAME").arg(theName).arg(aName), QMessageBox::Ok);
933 }
934
935 //=======================================================================
936 // name    : SMESHGUI_FilterLibraryDlg::getDefaultLibraryName
937 // Purpose : Get default library name
938 //=======================================================================
939 QString& SMESHGUI_FilterLibraryDlg::getDefaultLibraryName() const
940 {
941   static QString aName;
942   if (aName.isEmpty())
943   {
944     QString aHomeDir = QDir(QDir::home()).absPath();
945     aName = aHomeDir + "/" + tr ("LIB_NAME");
946   }
947   return aName;
948 }
949
950 //=======================================================================
951 // name    : SMESHGUI_FilterLibraryDlg::getDefaultFilterName
952 // Purpose : Create default filter name
953 //=======================================================================
954 QString SMESHGUI_FilterLibraryDlg::getDefaultFilterName() const
955 {
956   QString aName;
957
958   if (myTable->GetType() == SMESH::NODE)
959     aName = tr("NODE");
960   else if (myTable->GetType() == SMESH::EDGE)
961     aName = tr("EDGE");
962   else if (myTable->GetType() == SMESH::FACE)
963     aName = tr("FACE");
964   else if (myTable->GetType() == SMESH::VOLUME)
965     aName = tr("VOLUME");
966   else if (myTable->GetType() == SMESH::ALL)
967     aName = tr("ELEMENT");
968   else
969     aName = tr("SELECTION");
970
971   aName += tr("FILTER");
972
973
974   QMap< QString, int > anAllNames;
975   SMESH::string_array_var aNames = myLibrary->GetAllNames();
976   for(int i = 0, n = aNames->length(); i < n; i++)
977     anAllNames[ QString(aNames[ i ]) ] = -1;
978
979   bool isNotValid = true;
980   int k = 1;
981   QString aNewName;
982   while (isNotValid)
983   {
984     isNotValid = false;
985     aNewName = aName + "_" + QString("%1").arg(k);
986     if (anAllNames.contains(aNewName))
987     {
988       isNotValid = true;
989       k++;
990     }
991   }
992
993   return aNewName;
994 }
995
996 //=======================================================================
997 // name    : SMESHGUI_FilterLibraryDlg::setSelected
998 // Purpose : set selected item in list box containing filters
999 //=======================================================================
1000 bool SMESHGUI_FilterLibraryDlg::setSelected(const QString& theName)
1001 {
1002   int anIndex = getIndex(theName);
1003   if (anIndex != -1)
1004   {
1005     myListBox->setCurrentItem(anIndex);
1006     myCurrFilterName = theName;
1007     myCurrFilter = anIndex;
1008   }
1009   return anIndex != -1;
1010 }
1011
1012 //=======================================================================
1013 // name    : SMESHGUI_FilterLibraryDlg::getIndex
1014 // Purpose : Get index of the filter in list box
1015 //=======================================================================
1016 int SMESHGUI_FilterLibraryDlg::getIndex(const QString& theName) const
1017 {
1018   for (int i = 0, n = myListBox->count(); i < n; i++)
1019     if (myListBox->text(i) == theName)
1020       return i;
1021   return -1;
1022 }
1023
1024 //=======================================================================
1025 // name    : SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed
1026 // Purpose : SLOT. Called when "Delete" button pressed
1027 //=======================================================================
1028 void SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed()
1029 {
1030   if (myLibrary->_is_nil()) {
1031     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
1032                              tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
1033     return;
1034   }
1035
1036   int anIndex = getIndex(myCurrFilterName);
1037
1038   if (anIndex == -1 || !myLibrary->Delete(myCurrFilterName.latin1())) {
1039     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
1040                              tr("ERROR_OF_DELETING"), QMessageBox::Ok);
1041   } else {
1042     myCurrFilterName = "";
1043     myCurrFilterName = -1;
1044     myListBox->removeItem(anIndex);
1045
1046     if (anIndex >= 1)
1047       myListBox->setSelected(anIndex - 1, true);
1048     else if (anIndex == 0 && myListBox->count() > 0)
1049       myListBox->setSelected(0, true);
1050     else
1051       myTable->Clear();
1052   }
1053
1054   myTable->SetEnabled(myListBox->count() > 0);
1055   if (myListBox->count() == 0) {
1056     myName->setText("");
1057     myName->setEnabled(false);
1058   }
1059 }
1060
1061 //=======================================================================
1062 // name    : onFilterNameChanged()
1063 // Purpose : SLOT. Called when name of filter changed
1064 //           Change filter name in list box
1065 //=======================================================================
1066 void SMESHGUI_FilterLibraryDlg::onFilterNameChanged (const QString& theName)
1067 {
1068   int aCurrItem = myListBox->currentItem();
1069   if (aCurrItem == -1)
1070     return;
1071
1072   myListBox->blockSignals(true);
1073   myListBox->changeItem(theName, aCurrItem);
1074   myListBox->blockSignals(false);
1075 }
1076
1077 //=======================================================================
1078 // name    : SMESHGUI_FilterLibraryDlg::SetTable
1079 // Purpose : Set table
1080 //=======================================================================
1081 void SMESHGUI_FilterLibraryDlg::SetTable(const SMESHGUI_FilterTable* theTable)
1082 {
1083   myTable->Copy(theTable);
1084   myName->setText(getDefaultFilterName());
1085   addFilterToLib(myName->text());
1086   myTable->Update();
1087 }
1088
1089 //=======================================================================
1090 // name    : SMESHGUI_FilterLibraryDlg::GetTable
1091 // Purpose : Get table
1092 //=======================================================================
1093 const SMESHGUI_FilterTable* SMESHGUI_FilterLibraryDlg::GetTable() const
1094 {
1095   return myTable;
1096 }
1097
1098
1099 //=======================================================================
1100 // name    : SMESHGUI_FilterLibraryDlg::onEntityTypeChanged
1101 // Purpose : SLOT. Called when entiyt type changed
1102 //=======================================================================
1103 void SMESHGUI_FilterLibraryDlg::onEntityTypeChanged(const int theType)
1104 {
1105   if (myLibrary->_is_nil())
1106     return;
1107
1108   myName->clear();
1109   myCurrFilterName = "";
1110   myCurrFilter = -1;
1111   updateList();
1112   if (myListBox->count())
1113     myListBox->setCurrentItem(0);
1114 }
1115
1116 //=======================================================================
1117 // name    : SMESHGUI_FilterLibraryDlg::onNeedValidation
1118 // Purpose :
1119 //=======================================================================
1120 void SMESHGUI_FilterLibraryDlg::onNeedValidation()
1121 {
1122   if (!myCurrFilterName.isEmpty())
1123   {
1124     bool valid = isValid(true);
1125     myTable->SetValidity(valid);
1126
1127     if (valid)
1128     {
1129       SMESH::Filter_var aFilter = createFilter(myTable->GetType());
1130       myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
1131     }
1132   }
1133 }