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