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