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