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