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