Salome HOME
Merge branch V7_3_1_BR
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_FilterLibraryDlg.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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(reject()));
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(reject()));
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     disconnect( mySMESHGUI, 0, this, 0);
471     mySMESHGUI->ResetState();
472     accept();
473   }
474 }
475
476 //=======================================================================
477 // name    : SMESHGUI_FilterLibraryDlg::reject
478 // Purpose : SLOT called when "Close" button pressed. Close dialog
479 //=======================================================================
480 void SMESHGUI_FilterLibraryDlg::reject()
481 {
482   disconnect( mySMESHGUI, 0, this, 0);
483   mySMESHGUI->ResetState();
484   QDialog::reject();
485 }
486
487 //=================================================================================
488 // function : onHelp()
489 // purpose  :
490 //=================================================================================
491 void SMESHGUI_FilterLibraryDlg::onHelp()
492 {
493   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
494   if (app) 
495     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
496   else {
497     QString platform;
498 #ifdef WIN32
499     platform = "winapplication";
500 #else
501     platform = "application";
502 #endif
503     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
504                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
505                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
506                                                                  platform)).
507                              arg(myHelpFileName));
508   }
509 }
510
511 //=======================================================================
512 // name    : SMESHGUI_FilterLibraryDlg::onDeactivate
513 // Purpose : SLOT called when dialog must be deativated
514 //=======================================================================
515 void SMESHGUI_FilterLibraryDlg::onDeactivate()
516 {
517   setEnabled(false);
518 }
519
520 //=======================================================================
521 // name    : SMESHGUI_FilterLibraryDlg::enterEvent
522 // Purpose : Event filter
523 //=======================================================================
524 void SMESHGUI_FilterLibraryDlg::enterEvent(QEvent*)
525 {
526   setEnabled(true);
527 }
528
529 //=======================================================================
530 // name    : SMESHGUI_FilterLibraryDlg::getFileName
531 // Purpose : Get file name
532 //=======================================================================
533 QString SMESHGUI_FilterLibraryDlg::getFileName() const
534 {
535   return myFileName != 0 ? myFileName->text() : QString("");
536 }
537
538 //================================================================
539 // Function : setFileName
540 // Purpose  : Set file name to line edit
541 //================================================================
542 void SMESHGUI_FilterLibraryDlg::setFileName(const QString& txt, const bool autoExt)
543 {
544   if (myFileName == 0)
545     return;
546   myFileName->setText(autoExt ? autoExtension(txt) : txt);
547 }
548
549 //================================================================
550 // Function : autoExtension
551 // Purpose  : Append extension to the file name
552 //================================================================
553 QString SMESHGUI_FilterLibraryDlg::autoExtension(const QString& theFileName) const
554 {
555   QString anExt = theFileName.section('.', -1);
556   return anExt != "xml" && anExt != "XML" ? theFileName + ".xml" : theFileName;
557 }
558
559 //================================================================
560 // Function : filterWildCards
561 // Purpose  :
562 //================================================================
563 QStringList SMESHGUI_FilterLibraryDlg::filterWildCards(const QString& theFilter) const
564 {
565   QStringList res;
566
567   int b = theFilter.lastIndexOf("(");
568   int e = theFilter.lastIndexOf(")");
569   if (b != -1 && e != -1)
570   {
571     QString content = theFilter.mid(b + 1, e - b - 1).trimmed();
572     QStringList lst = content.split(" ", QString::SkipEmptyParts);
573     for (QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it)
574       if ((*it).indexOf(".") != -1)
575         res.append((*it).trimmed());
576   }
577   return res;
578 }
579
580 //=======================================================================
581 // name    : SMESHGUI_FilterLibraryDlg::prepareFilters
582 // Purpose : Prepare filters for dialog
583 //=======================================================================
584 QStringList SMESHGUI_FilterLibraryDlg::prepareFilters() const
585 {
586   static QStringList aList;
587   if (aList.isEmpty())
588   {
589     aList.append(tr("XML_FILT"));
590     //aList.append(tr("ALL_FILES_FILTER"));
591   }
592
593   return aList;
594 }
595
596 //================================================================
597 // Function : onBrowse
598 // Purpose  : SLOT. Display "Open file" dialog for chosing library name
599 //================================================================
600 void SMESHGUI_FilterLibraryDlg::onBrowse()
601 {
602   Dialog* aDlg = new Dialog(this, true);
603   aDlg->setWindowTitle(tr("OPEN_LIBRARY"));
604
605   //aDlg->setMode(myMode == COPY_FROM ? QFileDialogP::ExistingFile : QFileDialogP::AnyFile);
606   aDlg->setFileMode(myMode == COPY_FROM ? QFileDialog::ExistingFile : QFileDialog::AnyFile);
607   aDlg->setFilters(prepareFilters());
608   aDlg->selectFile(getFileName());
609
610   QPushButton* anOkBtn = (QPushButton*)aDlg->findChild<QPushButton*>("OK");
611   if (anOkBtn != 0)
612     anOkBtn->setText(tr("SMESH_BUT_OK"));
613
614   if (aDlg->exec() != Accepted)
615     return;
616
617   QString fName = aDlg->selectedFile();
618
619   if (fName.isEmpty())
620     return;
621
622   if (QFileInfo(fName).suffix().isEmpty())
623     fName = autoExtension(fName);
624
625   fName = QDir::convertSeparators(fName);
626   QString prev = QDir::convertSeparators(getFileName());
627
628   if (prev == fName)
629     return;
630
631   setFileName(fName);
632
633   QListWidgetItem* item = myListBox->item( myListBox->count()-1 );
634   QString aName = item ? item->text() : QString::null;
635   processNewLibrary();
636
637   if (myMode == ADD_TO)
638   {
639     myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
640     myCurrFilterName = "";
641     myCurrFilter = -1;
642     addFilterToLib(aName);
643   }
644
645   isPermissionValid(false);
646 }
647
648 //=======================================================================
649 // name    : SMESHGUI_FilterLibraryDlg::processNewLibrary
650 // Purpose : SLOT. Calleds when file name changed
651 //=======================================================================
652 void SMESHGUI_FilterLibraryDlg::processNewLibrary()
653 {
654   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
655   if (aFilterMgr->_is_nil())
656     return;
657
658   myLibrary = aFilterMgr->LoadLibrary(autoExtension(getFileName()).toLatin1().constData());
659   if (myLibrary->_is_nil()) {
660     if (myMode == COPY_FROM) {
661       SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
662                                    tr("ERROR_LOAD"));
663       return;
664     } else {
665       myLibrary = aFilterMgr->CreateLibrary();
666       myLibrary->SetFileName(getFileName().toLatin1().constData());
667     }
668   }
669
670   updateList();
671 }
672
673 //=======================================================================
674 // name    : SMESHGUI_FilterLibraryDlg::updateList
675 // Purpose : Fill list box with filter names
676 //=======================================================================
677 void SMESHGUI_FilterLibraryDlg::updateList()
678 {
679   QStringList aList;
680   SMESH::string_array_var aNames = myLibrary->GetNames((SMESH::ElementType)myTable->GetType());
681   for (int i = 0, n = aNames->length(); i < n; i++)
682     aList.append(QString(aNames[ i ]));
683   myListBox->blockSignals(true);
684   myListBox->clear();
685   myListBox->blockSignals(false);
686   myListBox->addItems(aList);
687   if (myListBox->count() == 0)
688   {
689     myTable->Clear(myTable->GetType());
690     myName->clear();
691     myName->setEnabled(false);
692     myTable->SetEnabled(false);
693   }
694   else
695   {
696     myName->setEnabled(true);
697     myTable->SetEnabled(true);
698     if (myListBox->count())
699     {
700       myCurrFilterName = "";
701       myListBox->setCurrentItem(0);
702     }
703   }
704 }
705
706 //=======================================================================
707 // name    : SMESHGUI_FilterLibraryDlg::isNameValid
708 // Purpose : Verify validity if entered data
709 //=======================================================================
710 bool SMESHGUI_FilterLibraryDlg::isNameValid(const bool theMess) const
711 {
712   // verify validity of filter name
713   if (myName->isEnabled() && !myCurrFilterName.isEmpty()) {
714     QString aCurrName = myName->text();
715     if (aCurrName.isEmpty()) {
716       if (theMess)
717         SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
718                                      tr("EMPTY_FILTER_NAME"));
719       return false;
720     }
721
722     SMESH::string_array_var aNames = myLibrary->GetAllNames();
723     for (int f = 0, n = aNames->length(); f < n; f++) {
724       if (aNames[ f ] == aCurrName && aNames[ f ] != myCurrFilterName) {
725         if (theMess)
726           SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
727                                        tr("ERROR_FILTER_NAME"));
728         return false;
729       }
730     }
731   }
732
733   return true;
734 }
735
736 //=======================================================================
737 // name    : SMESHGUI_FilterLibraryDlg::isPermissionValid
738 // Purpose : Verify write permission on file
739 //=======================================================================
740 bool SMESHGUI_FilterLibraryDlg::isPermissionValid(const bool theIsExistingOnly)
741 {
742   if (myMode == COPY_FROM)
743     return true;
744
745   // Verify write permission
746   bool isWritable = false;
747
748   QString fName(myFileName->text());
749   if (QFileInfo(fName).suffix().isEmpty())
750     fName = autoExtension(fName);
751
752   fName = QDir::convertSeparators(fName);
753
754   if (QFileInfo(fName).exists()) {
755     isWritable = QFileInfo(fName).isWritable();
756   } else if (!theIsExistingOnly) {
757     QFileInfo aDirInfo(QFileInfo(fName).absolutePath());
758     isWritable = aDirInfo.isWritable();
759     /*if (QDir(QFileInfo(fName).dirPath(true)).exists() ||
760          QDir().mkdir(QFileInfo(fName).dirPath(true)))
761     {
762       QFile aFile(fName);
763       if (aFile.open(IO_WriteOnly))
764         isWritable = true;
765       else
766         aFile.close();
767     }
768     */
769   } else {
770     isWritable = true;
771   }
772
773   if (!isWritable) {
774     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
775                                  tr("NO_PERMISSION"));
776     return false;
777   }
778
779   return true;
780 }
781
782 //=======================================================================
783 // name    : SMESHGUI_FilterLibraryDlg::isValid
784 // Purpose : Verify validity if entered data
785 //=======================================================================
786 bool SMESHGUI_FilterLibraryDlg::isValid(const bool theMess) const
787 {
788   // verify validity of table
789   if (!myTable->IsValid(theMess) || !isNameValid(theMess))
790     return false;
791   else
792     return true;
793 }
794
795 //=======================================================================
796 // name    : SMESHGUI_FilterLibraryDlg::onFilterChanged
797 // Purpose : SLOT. Called when selected filter of library is changed
798 //=======================================================================
799 void SMESHGUI_FilterLibraryDlg::onFilterChanged()
800 {
801   QString theName = myListBox->currentItem() ? myListBox->currentItem()->text() : QString::null;
802   if (myLibrary->_is_nil())
803     return;
804
805   // Save parameters of filter if it was changed
806
807   if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
808   {
809     if (!isValid(true))
810     {
811       myListBox->blockSignals(true);
812       myListBox->setCurrentRow(myCurrFilter);
813       myListBox->blockSignals(false);
814       return;
815     }
816
817     SMESH::Filter_var aFilter = createFilter();
818     myLibrary->Replace(myCurrFilterName.toLatin1().constData(), 
819                        myName->text().toLatin1().constData(), 
820                        aFilter);
821   }
822
823   // Fill table with filter parameters
824
825   SMESH::Filter_var aFilter = myLibrary->Copy(theName.toLatin1().constData());
826   myCurrFilterName = theName;
827   myCurrFilter = myListBox->currentRow();
828   myName->setText(theName);
829
830
831   SMESH::Filter::Criteria_var aCriteria;
832
833   myTable->Clear(myTable->GetType());
834
835   if (CORBA::is_nil( aFilter ) || !aFilter->GetCriteria(aCriteria))
836     return;
837
838   for (int i = 0, n = aCriteria->length(); i < n; i++)
839     myTable->AddCriterion(aCriteria[ i ], myTable->GetType());
840
841   myTable->Update();
842   updateControlsVisibility(); // IPAL19974
843 }
844
845 //=======================================================================
846 // name    : SMESHGUI_FilterLibraryDlg::onReturnPressed
847 // Purpose : SLOT. Called when enter button is pressed in library name field
848 //           Reload library
849 //=======================================================================
850 void SMESHGUI_FilterLibraryDlg::onReturnPressed()
851 {
852   QListWidgetItem* item = myListBox->item( myListBox->count()-1 );
853   QString aName = item ? item->text() : QString::null;
854
855   processNewLibrary();
856
857   if (myMode == ADD_TO)
858   {
859     myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
860     myCurrFilterName = "";
861     myCurrFilter = -1;
862     addFilterToLib(aName);
863   }
864
865   isPermissionValid(false);
866 }
867
868 //=======================================================================
869 // name    : SMESHGUI_FilterLibraryDlg::enableMainButtons
870 // Purpose : Update state of "OK", "Cancel" buttons
871 //=======================================================================
872 void SMESHGUI_FilterLibraryDlg::enableMainButtons()
873 {
874   /*bool isEnabled = isValid(false);
875   if (myButtons.contains(BTN_OK))
876     myButtons[ BTN_OK ]->setEnabled(isEnabled);
877   else if (myButtons.contains(BTN_Apply))
878     myButtons[ BTN_OK ]->setEnabled(isEnabled);
879   if (myButtons.contains(BTN_Cancel))
880     myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
881   else if (myButtons.contains(BTN_Close))
882     myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
883     */
884 }
885
886 //=======================================================================
887 // name    : SMESHGUI_FilterLibraryDlg::createFilter
888 // Purpose : Cerate filter in accordance with library
889 //=======================================================================
890 SMESH::Filter_ptr SMESHGUI_FilterLibraryDlg::createFilter(const int theType)
891 {
892   int n = myTable->NumRows(theType);
893
894   SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
895   aCriteria->length(n);
896
897   for (int i = 0; i < n; i++)
898   {
899     SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
900     myTable->GetCriterion(i, aCriterion);
901     aCriteria[ i ] = aCriterion;
902   }
903
904   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
905   SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
906   aFilter->SetCriteria(aCriteria.in());
907
908   return aFilter._retn();
909 }
910
911 //=======================================================================
912 // name    : SMESHGUI_FilterLibraryDlg::onAddBtnPressed
913 // Purpose : SLOT. Called when "Add" button pressed
914 //           Add new filter to the end of library
915 //=======================================================================
916 void SMESHGUI_FilterLibraryDlg::onAddBtnPressed()
917 {
918   // Save parameters of filter if it was changed
919   if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
920   {
921     if (!isValid(true))
922       return;
923
924     SMESH::Filter_var aFilter = createFilter();
925     myLibrary->Replace(myCurrFilterName.toLatin1().constData(), 
926                        myName->text().toLatin1().constData(), 
927                        aFilter);
928   }
929   myTable->Clear(myTable->GetType());
930
931   addFilterToLib(getDefaultFilterName());
932 }
933
934 //=======================================================================
935 // name    : onAddBtnPressed()
936 // Purpose : SLOT. Called when "Add" button pressed
937 //           Add new filter to the end of library
938 //=======================================================================
939 void SMESHGUI_FilterLibraryDlg::addFilterToLib (const QString& theName)
940 {
941   if (myLibrary->_is_nil()) {
942     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
943                                  tr("LIBRARY_IS_NOT_LOADED"));
944     return;
945   }
946
947   // create filter
948   SMESH::Filter_var aFilter = createFilter();
949
950   // if name of filter already exist in the library assign default name for the filter
951   QString aName(theName);
952   SMESH::string_array_var aNames = myLibrary->GetAllNames();
953   for (int i = 0, n = aNames->length(); i < n; i++)
954     if (aName == QString(aNames[ i ]))
955     {
956       aName = getDefaultFilterName();
957       break;
958     }
959
960   // add new filter in library
961   bool aResult = !aFilter->GetPredicate()->_is_nil()
962     ? myLibrary->Add(aName.toLatin1().constData(), aFilter)
963     : myLibrary->AddEmpty(aName.toLatin1().constData(), (SMESH::ElementType)myTable->GetType());
964
965   if (!aResult) {
966     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
967                                  tr("ERROR_OF_ADDING"));
968   }
969
970   updateList();
971   myCurrFilterName = "";
972   myCurrFilter = -1;
973   setSelected(aName);
974
975   if (theName != aName)
976     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
977                                  tr("ASSIGN_NEW_NAME").arg(theName).arg(aName));
978 }
979
980 //=======================================================================
981 // name    : SMESHGUI_FilterLibraryDlg::getDefaultLibraryName
982 // Purpose : Get default library name
983 //=======================================================================
984 QString& SMESHGUI_FilterLibraryDlg::getDefaultLibraryName() const
985 {
986   static QString aName;
987   if (aName.isEmpty())
988   {
989     QString aHomeDir = QDir(QDir::home()).absolutePath();
990     aName = aHomeDir + "/" + tr ("LIB_NAME");
991   }
992   return aName;
993 }
994
995 //=======================================================================
996 // name    : SMESHGUI_FilterLibraryDlg::getDefaultFilterName
997 // Purpose : Create default filter name
998 //=======================================================================
999 QString SMESHGUI_FilterLibraryDlg::getDefaultFilterName() const
1000 {
1001   QString aName;
1002
1003   if (myTable->GetType() == SMESH::NODE)
1004     aName = tr("NODE");
1005   else if (myTable->GetType() == SMESH::EDGE)
1006     aName = tr("EDGE");
1007   else if (myTable->GetType() == SMESH::FACE)
1008     aName = tr("FACE");
1009   else if (myTable->GetType() == SMESH::VOLUME)
1010     aName = tr("VOLUME");
1011   else if (myTable->GetType() == SMESH::ALL)
1012     aName = tr("ELEMENT");
1013   else
1014     aName = tr("SELECTION");
1015
1016   aName += tr("FILTER");
1017
1018
1019   QMap< QString, int > anAllNames;
1020   SMESH::string_array_var aNames = myLibrary->GetAllNames();
1021   for(int i = 0, n = aNames->length(); i < n; i++)
1022     anAllNames[ QString(aNames[ i ]) ] = -1;
1023
1024   bool isNotValid = true;
1025   int k = 1;
1026   QString aNewName;
1027   while (isNotValid)
1028   {
1029     isNotValid = false;
1030     aNewName = aName + "_" + QString("%1").arg(k);
1031     if (anAllNames.contains(aNewName))
1032     {
1033       isNotValid = true;
1034       k++;
1035     }
1036   }
1037
1038   return aNewName;
1039 }
1040
1041 //=======================================================================
1042 // name    : SMESHGUI_FilterLibraryDlg::setSelected
1043 // Purpose : set selected item in list box containing filters
1044 //=======================================================================
1045 bool SMESHGUI_FilterLibraryDlg::setSelected(const QString& theName)
1046 {
1047   int anIndex = getIndex(theName);
1048   if (anIndex != -1)
1049   {
1050     myListBox->setCurrentRow(anIndex);
1051     myCurrFilterName = theName;
1052     myCurrFilter = anIndex;
1053   }
1054   return anIndex != -1;
1055 }
1056
1057 //=======================================================================
1058 // name    : SMESHGUI_FilterLibraryDlg::getIndex
1059 // Purpose : Get index of the filter in list box
1060 //=======================================================================
1061 int SMESHGUI_FilterLibraryDlg::getIndex(const QString& theName) const
1062 {
1063   for (int i = 0, n = myListBox->count(); i < n; i++)
1064     if (myListBox->item(i)->text() == theName)
1065       return i;
1066   return -1;
1067 }
1068
1069 //=======================================================================
1070 // name    : SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed
1071 // Purpose : SLOT. Called when "Delete" button pressed
1072 //=======================================================================
1073 void SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed()
1074 {
1075   if (myLibrary->_is_nil()) {
1076     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
1077                                  tr("LIBRARY_IS_NOT_LOADED"));
1078     return;
1079   }
1080
1081   int anIndex = getIndex(myCurrFilterName);
1082
1083   if (anIndex == -1 || !myLibrary->Delete(myCurrFilterName.toLatin1().constData())) {
1084     SUIT_MessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
1085                                  tr("ERROR_OF_DELETING"));
1086   } else {
1087     myCurrFilterName = "";
1088     myCurrFilter = -1;
1089     delete myListBox->item(anIndex);
1090
1091     if (anIndex >= 1)
1092       myListBox->item(anIndex - 1)->setSelected(true);
1093     else if (anIndex == 0 && myListBox->count() > 0)
1094       myListBox->item(0)->setSelected(true);
1095     else
1096       myTable->Clear();
1097   }
1098
1099   myTable->SetEnabled(myListBox->count() > 0);
1100   if (myListBox->count() == 0) {
1101     myName->setText("");
1102     myName->setEnabled(false);
1103   }
1104 }
1105
1106 //=======================================================================
1107 // name    : onFilterNameChanged()
1108 // Purpose : SLOT. Called when name of filter changed
1109 //           Change filter name in list box
1110 //=======================================================================
1111 void SMESHGUI_FilterLibraryDlg::onFilterNameChanged (const QString& theName)
1112 {
1113   int aCurrItem = myListBox->currentRow();
1114   if (aCurrItem == -1)
1115     return;
1116
1117   myListBox->blockSignals(true);
1118   myListBox->item(aCurrItem)->setText(theName);
1119   myListBox->blockSignals(false);
1120 }
1121
1122 //=======================================================================
1123 // name    : SMESHGUI_FilterLibraryDlg::SetTable
1124 // Purpose : Set table
1125 //=======================================================================
1126 void SMESHGUI_FilterLibraryDlg::SetTable(const SMESHGUI_FilterTable* theTable)
1127 {
1128   myTable->Copy(theTable);
1129   myName->setText(getDefaultFilterName());
1130   addFilterToLib(myName->text());
1131   myTable->Update();
1132 }
1133
1134 //=======================================================================
1135 // name    : SMESHGUI_FilterLibraryDlg::GetTable
1136 // Purpose : Get table
1137 //=======================================================================
1138 const SMESHGUI_FilterTable* SMESHGUI_FilterLibraryDlg::GetTable() const
1139 {
1140   return myTable;
1141 }
1142
1143
1144 //=======================================================================
1145 // name    : SMESHGUI_FilterLibraryDlg::onEntityTypeChanged
1146 // Purpose : SLOT. Called when entiyt type changed
1147 //=======================================================================
1148 void SMESHGUI_FilterLibraryDlg::onEntityTypeChanged(const int theType)
1149 {
1150   if (myLibrary->_is_nil())
1151     return;
1152
1153   myName->clear();
1154   myCurrFilterName = "";
1155   myCurrFilter = -1;
1156   updateList();
1157   if (myListBox->count())
1158     myListBox->setCurrentItem(0);
1159 }
1160
1161 //=======================================================================
1162 // name    : SMESHGUI_FilterLibraryDlg::onNeedValidation
1163 // Purpose :
1164 //=======================================================================
1165 void SMESHGUI_FilterLibraryDlg::onNeedValidation()
1166 {
1167   if (!myCurrFilterName.isEmpty())
1168   {
1169     bool valid = isValid(true);
1170     myTable->SetValidity(valid);
1171
1172     if (valid)
1173     {
1174       SMESH::Filter_var aFilter = createFilter(myTable->GetType());
1175       myLibrary->Replace(myCurrFilterName.toLatin1().constData(),
1176                          myName->text().toLatin1().constData(),
1177                          aFilter);
1178     }
1179   }
1180 }
1181
1182 //=================================================================================
1183 // function : keyPressEvent()
1184 // purpose  :
1185 //=================================================================================
1186 void SMESHGUI_FilterLibraryDlg::keyPressEvent( QKeyEvent* e )
1187 {
1188   QDialog::keyPressEvent( e );
1189   if ( e->isAccepted() )
1190     return;
1191
1192   if ( e->key() == Qt::Key_F1 ) {
1193     e->accept();
1194     onHelp();
1195   }
1196 }