1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESHGUI_FilterLibraryDlg.cxx
25 // Author : Sergey LITONIN
28 #include "SMESHGUI_FilterLibraryDlg.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_FilterUtils.h"
33 #include <QAD_Desktop.h>
34 #include <QAD_FileDlg.h>
36 #include <qapplication.h>
37 #include <qmessagebox.h>
40 #include <qlineedit.h>
41 #include <qpushbutton.h>
42 #include <qgroupbox.h>
46 #include <qfileinfo.h>
53 Class : SMESHGUI_FilterLibraryDlg::Dialog
54 Description : Dialog for opening filter library
57 class SMESHGUI_FilterLibraryDlg::Dialog : public QAD_FileDlg
60 Dialog( QWidget* theParent, const bool theToOpen );
64 virtual bool acceptData();
67 SMESHGUI_FilterLibraryDlg::Dialog::Dialog( QWidget* theParent,
68 const bool theToOpen )
69 : QAD_FileDlg( theParent, theToOpen )
73 SMESHGUI_FilterLibraryDlg::Dialog::~Dialog()
77 bool SMESHGUI_FilterLibraryDlg::Dialog::acceptData()
79 if ( mode() != QFileDialogP::AnyFile )
80 return QAD_FileDlg::acceptData();
87 Class : SMESHGUI_FilterLibraryDlg
88 Description : Dialog to specify filters for VTK viewer
91 //=======================================================================
92 // name : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
93 // Purpose : Constructor
94 //=======================================================================
95 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg( QWidget* theParent,
96 const QValueList<int>& theTypes,
99 : QDialog( theParent, theName, true,
100 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
102 construct( theTypes, theMode );
105 //=======================================================================
106 // name : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
107 // Purpose : Constructor
108 //=======================================================================
109 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg( QWidget* theParent,
112 const char* theName )
113 : QDialog( theParent, theName, true,
114 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
116 QValueList<int> aTypes;
117 aTypes.append( theType );
118 construct( aTypes, theMode );
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 )
130 QVBoxLayout* aDlgLay = new QVBoxLayout( this, MARGIN, SPACING );
132 myMainFrame = createMainFrame ( this );
133 QFrame* aBtnFrame = createButtonFrame( this );
135 aDlgLay->addWidget( myMainFrame );
136 aDlgLay->addWidget( aBtnFrame );
138 aDlgLay->setStretchFactor( myMainFrame, 1 );
140 Init( myTypes, myMode );
143 //=======================================================================
144 // name : SMESHGUI_FilterLibraryDlg::createMainFrame
145 // Purpose : Create frame containing dialog's input fields
146 //=======================================================================
147 QFrame* SMESHGUI_FilterLibraryDlg::createMainFrame( QWidget* theParent )
149 QGroupBox* aMainFrame = new QGroupBox( 1, Qt::Horizontal, theParent );
150 aMainFrame->setFrameStyle( QFrame::NoFrame );
151 aMainFrame->setInsideMargin( 0 );
155 QGroupBox* aGrp = new QGroupBox( 1, Qt::Vertical, aMainFrame );
156 aGrp->setFrameStyle( QFrame::NoFrame );
157 aGrp->setInsideMargin( 0 );
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") ) );
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 );
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 );
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 );
190 myTable = new SMESHGUI_FilterTable( aMainFrame, myTypes );
191 myTable->SetEditable( myMode == EDIT );
192 myTable->SetLibsEnabled( false );
194 myListBox->setMinimumHeight( ( int )( myTable->sizeHint().height() * 0.5 ) );
195 myListBox->setRowMode( QListBox::FitToWidth );
196 myListBox->setSelectionMode( QListBox::Single );
198 myOpenBtn->setAutoDefault( false );
199 myAddBtn->setAutoDefault( false );
200 myDeleteBtn->setAutoDefault( false );
202 // connect signals and slots
204 connect( myFileName, SIGNAL( returnPressed() ), this, SLOT( onReturnPressed() ) );
205 connect( myOpenBtn , SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
207 connect( myListBox, SIGNAL( highlighted( const QString& ) ),
208 this, SLOT( onFilterChanged( const QString& ) ) );
210 connect( myAddBtn, SIGNAL( clicked() ), this, SLOT( onAddBtnPressed() ) );
211 connect( myDeleteBtn, SIGNAL( clicked() ), this, SLOT( onDeleteBtnPressed() ) );
213 connect( myName, SIGNAL( textChanged( const QString& ) ),
214 this, SLOT( onFilterNameChanged( const QString& ) ) );
216 connect( myTable, SIGNAL( EntityTypeChanged( const int ) ),
217 this, SLOT( onEntityTypeChanged( const int ) ) );
219 connect( myTable, SIGNAL( NeedValidation() ), this, SLOT( onNeedValidation() ) );
224 //=======================================================================
225 // name : SMESHGUI_FilterLibraryDlg::createButtonFrame
226 // Purpose : Create frame containing buttons
227 //=======================================================================
228 QFrame* SMESHGUI_FilterLibraryDlg::createButtonFrame( QWidget* theParent )
230 QGroupBox* aGrp = new QGroupBox( 1, Qt::Vertical, theParent );
232 myButtons[ BTN_OK ] = new QPushButton( tr( "SMESH_BUT_OK" ), aGrp );
233 myButtons[ BTN_Apply ] = new QPushButton( tr( "SMESH_BUT_APPLY" ), aGrp );
235 QLabel* aLbl = new QLabel( aGrp );
236 aLbl->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
238 myButtons[ BTN_Cancel ] = new QPushButton( tr( "SMESH_BUT_CANCEL" ), aGrp );
239 myButtons[ BTN_Close ] = new QPushButton( tr( "SMESH_BUT_CLOSE" ), aGrp );
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() ) );
246 QMap<int, QPushButton*>::iterator anIter;
247 for ( anIter = myButtons.begin(); anIter != myButtons.end(); ++anIter )
248 anIter.data()->setAutoDefault( false );
255 //=======================================================================
256 // name : SMESHGUI_FilterLibraryDlg::updateMainButtons
257 // Purpose : Update visibility of main buttons ( OK, Cancel, Close ... )
258 //=======================================================================
259 void SMESHGUI_FilterLibraryDlg::updateMainButtons()
261 if ( myTypes.count() == 1 )
263 myButtons[ BTN_Cancel ]->show();
264 myButtons[ BTN_Apply ]->hide();
265 myButtons[ BTN_Close ]->hide();
269 myButtons[ BTN_Cancel ]->hide();
270 myButtons[ BTN_Apply ]->show();
271 myButtons[ BTN_Close ]->show();
275 //=======================================================================
276 // name : SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg
277 // Purpose : Destructor
278 //=======================================================================
279 SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg()
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 )
289 QValueList<int> aTypes;
290 aTypes.append( type );
291 Init( aTypes, theMode );
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 )
302 myTable->Init( theTypes );
303 myCurrFilterName = "";
309 updateControlsVisibility();
312 SMESHGUI* aModeler = SMESHGUI::GetSMESHGUI();
314 connect( aModeler, SIGNAL( SignalDeactivateActiveDialog() ), SLOT( onDeactivate() ) );
315 connect( aModeler, SIGNAL( SignalCloseAllDialogs() ), SLOT( onClose() ) );
317 if ( myMode == ADD_TO )
319 setCaption( tr( "ADD_TO_TLT" ) );
320 if ( myFileName->text().isEmpty() )
321 myFileName->setText( getDefaultLibraryName() );
324 else if ( myMode == COPY_FROM )
326 setCaption( tr( "COPY_FROM_TLT" ) );
327 if ( myFileName->text().isEmpty() )
328 myFileName->setText( getDefaultLibraryName() );
330 if ( myListBox->count() > 0 )
331 myListBox->setCurrentItem( 0 );
335 setCaption( tr( "EDIT_LIB_TLT" ) );
336 if ( myFileName->text().isEmpty() )
337 myFileName->setText( getDefaultLibraryName() );
339 if ( myListBox->count() > 0 )
340 myListBox->setCurrentItem( 0 );
344 aModeler->DefineDlgPosition( this, x, y );
350 isPermissionValid( false );
353 //=======================================================================
354 // name : SMESHGUI_FilterLibraryDlg::updateControlsVisibility
355 // Purpose : Update visibility of controls in accordance with myMode
356 //=======================================================================
357 void SMESHGUI_FilterLibraryDlg::updateControlsVisibility()
359 if ( myMode == ADD_TO )
362 myNameGrp->setEnabled( true );
365 myTable->SetEditable( false );
367 else if ( myMode == COPY_FROM )
370 myNameGrp->setEnabled( false );
373 myTable->SetEditable( false );
375 else if ( myMode == EDIT )
378 myNameGrp->setEnabled( true );
381 myTable->SetEditable( true );
384 qApp->processEvents();
389 //=======================================================================
390 // name : SMESHGUI_FilterLibraryDlg::onApply
391 // Purpose : SLOT called when "Apply" button pressed.
392 //=======================================================================
393 bool SMESHGUI_FilterLibraryDlg::onApply()
395 if ( !isValid( true ) || !isPermissionValid( false ) )
398 if ( myLibrary->_is_nil() )
400 QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),
401 tr( "SMESH_WRN_WARNING" ), tr( "LIBRARY_IS_NOT_LOADED" ), QMessageBox::Ok );
405 const char* aName = myFileName->text().latin1();
406 if ( strcmp( myLibrary->GetFileName(), aName ) != 0 )
407 myLibrary->SetFileName( aName );
409 bool aResult = false;
411 if ( myMode == COPY_FROM || myListBox->count() == 0 )
415 else if ( myMode == EDIT || myMode == ADD_TO )
417 SMESH::Filter_var aFilter = createFilter();
418 if ( !myLibrary->Replace( myCurrFilterName, myName->text(), aFilter.in() ) )
420 QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),
421 tr( "SMESH_ERROR" ), tr( "ERROR_OF_EDITING" ), QMessageBox::Ok );
428 if ( aResult && myMode != COPY_FROM )
429 aResult = myLibrary->Save();
433 char* aFileName = myLibrary->GetFileName();
434 getDefaultLibraryName() = QString( aFileName );
437 else if ( myMode != COPY_FROM )
438 QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),
439 tr( "SMESH_ERROR" ), tr( "ERROR_OF_SAVING" ), QMessageBox::Ok );
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()
454 disconnect( SMESHGUI::GetSMESHGUI(), 0, this, 0 );
455 SMESHGUI::GetSMESHGUI()->ResetState() ;
460 //=======================================================================
461 // name : SMESHGUI_FilterLibraryDlg::onClose
462 // Purpose : SLOT called when "Close" button pressed. Close dialog
463 //=======================================================================
464 void SMESHGUI_FilterLibraryDlg::onClose()
466 disconnect( SMESHGUI::GetSMESHGUI(), 0, this, 0 );
467 SMESHGUI::GetSMESHGUI()->ResetState() ;
471 //=======================================================================
472 // name : SMESHGUI_FilterLibraryDlg::onDeactivate
473 // Purpose : SLOT called when dialog must be deativated
474 //=======================================================================
475 void SMESHGUI_FilterLibraryDlg::onDeactivate()
480 //=======================================================================
481 // name : SMESHGUI_FilterLibraryDlg::enterEvent
482 // Purpose : Event filter
483 //=======================================================================
484 void SMESHGUI_FilterLibraryDlg::enterEvent( QEvent* )
489 //=================================================================================
490 // function : closeEvent()
491 // purpose : Close dialog
492 //=================================================================================
493 void SMESHGUI_FilterLibraryDlg::closeEvent( QCloseEvent* e )
498 //=======================================================================
499 // name : SMESHGUI_FilterLibraryDlg::getFileName
500 // Purpose : Get file name
501 //=======================================================================
502 QString SMESHGUI_FilterLibraryDlg::getFileName() const
504 return myFileName != 0 ? myFileName->text() : QString("");
507 //================================================================
508 // Function : setFileName
509 // Purpose : Set file name to line edit
510 //================================================================
511 void SMESHGUI_FilterLibraryDlg::setFileName( const QString& txt, const bool autoExt )
513 if ( myFileName == 0 )
515 myFileName->setText( autoExt ? autoExtension( txt ) : txt );
518 //================================================================
519 // Function : autoExtension
520 // Purpose : Append extension to the file name
521 //================================================================
522 QString SMESHGUI_FilterLibraryDlg::autoExtension( const QString& theFileName ) const
524 QString anExt = theFileName.section( '.', -1 );
525 return anExt != "xml" && anExt != "XML" ? theFileName + ".xml" : theFileName;
528 //================================================================
529 // Function : filterWildCards
531 //================================================================
532 QStringList SMESHGUI_FilterLibraryDlg::filterWildCards( const QString& theFilter ) const
536 int b = theFilter.findRev( "(" );
537 int e = theFilter.findRev( ")" );
538 if ( b != -1 && e != -1 )
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() );
549 //=======================================================================
550 // name : SMESHGUI_FilterLibraryDlg::prepareFilters
551 // Purpose : Prepare filters for dialog
552 //=======================================================================
553 QStringList SMESHGUI_FilterLibraryDlg::prepareFilters() const
555 static QStringList aList;
556 if ( aList.isEmpty() )
558 aList.append( tr( "XML_FILT" ) );
559 //aList.append( tr( "ALL_FILES_FILTER" ) );
565 //================================================================
566 // Function : onBrowse
567 // Purpose : SLOT. Display "Open file" dialog for chosing library name
568 //================================================================
569 void SMESHGUI_FilterLibraryDlg::onBrowse()
571 Dialog* aDlg = new Dialog( this, true );
572 aDlg->setCaption( tr( "OPEN_LIBRARY" ) );
574 aDlg->setMode( myMode == COPY_FROM ? QFileDialogP::ExistingFile : QFileDialogP::AnyFile );
575 aDlg->setFilters( prepareFilters() );
576 aDlg->setSelection( getFileName() );
578 QPushButton* anOkBtn = ( QPushButton* )aDlg->child( "OK", "QPushButton" );
580 anOkBtn->setText( tr( "SMESH_BUT_OK" ) );
582 if ( aDlg->exec() != Accepted )
585 QString fName = aDlg->selectedFile();
587 if ( fName.isEmpty() )
590 if ( QFileInfo( fName ).extension().isEmpty() )
591 fName = autoExtension( fName );
593 fName = QDir::convertSeparators( fName );
594 QString prev = QDir::convertSeparators( getFileName() );
599 setFileName( fName );
601 QString aName = myListBox->text( myListBox->count() - 1 );
604 if ( myMode == ADD_TO )
606 myTable->Copy( (SMESHGUI_FilterTable*)parentWidget() );
607 myCurrFilterName = "";
609 addFilterToLib( aName );
612 isPermissionValid( false );
615 //=======================================================================
616 // name : SMESHGUI_FilterLibraryDlg::processNewLibrary
617 // Purpose : SLOT. Calleds when file name changed
618 //=======================================================================
619 void SMESHGUI_FilterLibraryDlg::processNewLibrary()
621 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
622 if ( aFilterMgr->_is_nil() )
625 myLibrary = aFilterMgr->LoadLibrary( autoExtension( getFileName() ) );
626 if ( myLibrary->_is_nil() )
628 if ( myMode == COPY_FROM )
630 QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),
631 tr( "SMESH_ERROR" ), tr( "ERROR_LOAD" ), QMessageBox::Ok );
636 myLibrary = aFilterMgr->CreateLibrary();
637 myLibrary->SetFileName( getFileName().latin1() );
644 //=======================================================================
645 // name : SMESHGUI_FilterLibraryDlg::updateList
646 // Purpose : Fill list box with filter names
647 //=======================================================================
648 void SMESHGUI_FilterLibraryDlg::updateList()
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 ] ) );
655 myListBox->insertStringList( aList );
656 if ( myListBox->count() == 0 )
658 myTable->Clear( myTable->GetType() );
660 myName->setEnabled( false );
661 myTable->SetEnabled( false );
665 myName->setEnabled( true );
666 myTable->SetEnabled( true );
667 if ( myListBox->count() )
669 myCurrFilterName = "";
670 myListBox->setCurrentItem( 0 );
675 //=======================================================================
676 // name : SMESHGUI_FilterLibraryDlg::isNameValid
677 // Purpose : Verify validity if entered data
678 //=======================================================================
679 bool SMESHGUI_FilterLibraryDlg::isNameValid( const bool theMess ) const
681 // verify validity of filter name
682 if ( myName->isEnabled() && !myCurrFilterName.isEmpty() )
684 QString aCurrName = myName->text();
685 if ( aCurrName.isEmpty() )
688 QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),
689 tr( "SMESH_INSUFFICIENT_DATA" ), tr( "EMPTY_FILTER_NAME" ), QMessageBox::Ok );
693 SMESH::string_array_var aNames = myLibrary->GetAllNames();
694 for ( int f = 0, n = aNames->length(); f < n; f++ )
696 if ( aNames[ f ] == aCurrName && aNames[ f ] != myCurrFilterName )
699 QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),
700 tr( "SMESH_INSUFFICIENT_DATA" ), tr( "ERROR_FILTER_NAME" ), QMessageBox::Ok );
709 //=======================================================================
710 // name : SMESHGUI_FilterLibraryDlg::isPermissionValid
711 // Purpose : Verify write permission on file
712 //=======================================================================
713 bool SMESHGUI_FilterLibraryDlg::isPermissionValid( const bool theIsExistingOnly )
715 if ( myMode == COPY_FROM )
718 // Verify write permission
719 bool isWritable = false;
721 QString fName( myFileName->text() );
722 if ( QFileInfo( fName ).extension().isEmpty() )
723 fName = autoExtension( fName );
725 fName = QDir::convertSeparators( fName );
727 if ( QFileInfo( fName ).exists() )
728 isWritable = QFileInfo( fName ).isWritable();
729 else if ( !theIsExistingOnly )
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 ) ) )
736 QFile aFile( fName );
737 if ( aFile.open( IO_WriteOnly ) )
749 QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),
750 tr( "SMESH_WRN_WARNING" ), tr( "NO_PERMISSION" ), QMessageBox::Ok );
757 //=======================================================================
758 // name : SMESHGUI_FilterLibraryDlg::isValid
759 // Purpose : Verify validity if entered data
760 //=======================================================================
761 bool SMESHGUI_FilterLibraryDlg::isValid( const bool theMess ) const
763 // verify validity of table
764 if ( !myTable->IsValid( theMess ) || !isNameValid( theMess ) )
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 )
776 if ( myLibrary->_is_nil() )
779 // Save parameters of filter if it was changed
781 if ( !myCurrFilterName.isEmpty() && myTable->IsEditable() )
783 if ( !isValid( true ) )
785 myListBox->blockSignals( true );
786 myListBox->setCurrentItem( myCurrFilter );
787 myListBox->blockSignals( false );
791 SMESH::Filter_var aFilter = createFilter();
792 myLibrary->Replace( myCurrFilterName.latin1(), myName->text().latin1(), aFilter );
795 // Fill table with filter parameters
797 SMESH::Filter_var aFilter = myLibrary->Copy( theName );
798 myCurrFilterName = theName;
799 myCurrFilter = myListBox->currentItem();
800 myName->setText( theName );
803 SMESH::Filter::Criteria_var aCriteria;
805 myTable->Clear( myTable->GetType() );
807 if ( !aFilter->GetCriteria( aCriteria ) )
810 for ( int i = 0, n = aCriteria->length(); i < n; i++ )
811 myTable->AddCriterion( aCriteria[ i ], myTable->GetType() );
816 //=======================================================================
817 // name : SMESHGUI_FilterLibraryDlg::onReturnPressed
818 // Purpose : SLOT. Called when enter button is pressed in library name field
820 //=======================================================================
821 void SMESHGUI_FilterLibraryDlg::onReturnPressed()
823 QString aName = myListBox->text( myListBox->count() - 1 );
827 if ( myMode == ADD_TO )
829 myTable->Copy( (SMESHGUI_FilterTable*)parentWidget() );
830 myCurrFilterName = "";
832 addFilterToLib( aName );
835 isPermissionValid( false );
838 //=======================================================================
839 // name : SMESHGUI_FilterLibraryDlg::enableMainButtons
840 // Purpose : Update state of "OK", "Cancel" buttons
841 //=======================================================================
842 void SMESHGUI_FilterLibraryDlg::enableMainButtons()
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 );
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 )
862 int n = myTable->NumRows( theType );
864 SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
865 aCriteria->length( n );
867 for ( int i = 0; i < n; i++ )
869 SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
870 myTable->GetCriterion( i, aCriterion );
871 aCriteria[ i ] = aCriterion;
874 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
875 SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
876 aFilter->SetCriteria( aCriteria.inout() );
878 return aFilter._retn();
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()
888 // Save parameters of filter if it was changed
889 if ( !myCurrFilterName.isEmpty() && myTable->IsEditable() )
891 if ( !isValid( true ) )
894 SMESH::Filter_var aFilter = createFilter();
895 myLibrary->Replace( myCurrFilterName.latin1(), myName->text().latin1(), aFilter );
898 addFilterToLib( getDefaultFilterName() );
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 )
908 if ( myLibrary->_is_nil() )
910 QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),
911 tr( "SMESH_WRN_WARNING" ), tr( "LIBRARY_IS_NOT_LOADED" ), QMessageBox::Ok );
916 SMESH::Filter_var aFilter = createFilter();
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 ] ) )
924 aName = getDefaultFilterName();
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() );
935 QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),
936 tr( "SMESH_ERROR" ), tr( "ERROR_OF_ADDING" ), QMessageBox::Ok );
940 myCurrFilterName = "";
942 setSelected( aName );
944 if ( theName != aName )
945 QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(), tr( "SMESH_WARNING" ),
946 tr( "ASSIGN_NEW_NAME" ).arg( theName ).arg( aName ) , QMessageBox::Ok );
949 //=======================================================================
950 // name : SMESHGUI_FilterLibraryDlg::getDefaultLibraryName
951 // Purpose : Get default library name
952 //=======================================================================
953 QString& SMESHGUI_FilterLibraryDlg::getDefaultLibraryName() const
955 static QString aName;
956 if ( aName.isEmpty() )
958 QString aHomeDir = QDir( QDir::home() ).absPath();
959 aName = aHomeDir + "/" + tr ( "LIB_NAME" );
964 //=======================================================================
965 // name : SMESHGUI_FilterLibraryDlg::getDefaultFilterName
966 // Purpose : Create default filter name
967 //=======================================================================
968 QString SMESHGUI_FilterLibraryDlg::getDefaultFilterName() const
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" );
983 aName = tr( "SELECTION" );
985 aName += tr( "FILTER" );
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;
993 bool isNotValid = true;
999 aNewName = aName + "_" + QString( "%1" ).arg( k );
1000 if ( anAllNames.contains( aNewName ) )
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 )
1016 int anIndex = getIndex( theName );
1017 if ( anIndex != -1 )
1019 myListBox->setCurrentItem( anIndex );
1020 myCurrFilterName = theName;
1021 myCurrFilter = anIndex;
1023 return anIndex != -1;
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
1032 for ( int i = 0, n = myListBox->count(); i < n; i++ )
1033 if ( myListBox->text( i ) == theName)
1038 //=======================================================================
1039 // name : SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed
1040 // Purpose : SLOT. Called when "Delete" button pressed
1041 //=======================================================================
1042 void SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed()
1044 if ( myLibrary->_is_nil() )
1046 QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),
1047 tr( "SMESH_WRN_WARNING" ), tr( "LIBRARY_IS_NOT_LOADED" ), QMessageBox::Ok );
1051 int anIndex = getIndex( myCurrFilterName );
1053 if ( anIndex == -1 || !myLibrary->Delete( myCurrFilterName.latin1() ) )
1055 QMessageBox::information( SMESHGUI::GetSMESHGUI()->GetDesktop(),
1056 tr( "SMESH_ERROR" ), tr( "ERROR_OF_DELETING" ), QMessageBox::Ok );
1060 myCurrFilterName = "";
1061 myCurrFilterName = -1;
1062 myListBox->removeItem( anIndex );
1065 myListBox->setSelected( anIndex - 1, true );
1066 else if ( anIndex == 0 && myListBox->count() > 0 )
1067 myListBox->setSelected( 0, true );
1072 myTable->SetEnabled( myListBox->count() > 0 );
1073 if ( myListBox->count() == 0 )
1075 myName->setText( "" );
1076 myName->setEnabled( false );
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 )
1087 int aCurrItem = myListBox->currentItem();
1088 if ( aCurrItem == -1 )
1091 myListBox->blockSignals( true );
1092 myListBox->changeItem( theName, aCurrItem );
1093 myListBox->blockSignals( false );
1096 //=======================================================================
1097 // name : SMESHGUI_FilterLibraryDlg::SetTable
1098 // Purpose : Set table
1099 //=======================================================================
1100 void SMESHGUI_FilterLibraryDlg::SetTable( const SMESHGUI_FilterTable* theTable )
1102 myTable->Copy( theTable );
1103 myName->setText( getDefaultFilterName() );
1104 addFilterToLib( myName->text() );
1108 //=======================================================================
1109 // name : SMESHGUI_FilterLibraryDlg::GetTable
1110 // Purpose : Get table
1111 //=======================================================================
1112 const SMESHGUI_FilterTable* SMESHGUI_FilterLibraryDlg::GetTable() const
1118 //=======================================================================
1119 // name : SMESHGUI_FilterLibraryDlg::onEntityTypeChanged
1120 // Purpose : SLOT. Called when entiyt type changed
1121 //=======================================================================
1122 void SMESHGUI_FilterLibraryDlg::onEntityTypeChanged( const int theType )
1124 if ( myLibrary->_is_nil() )
1128 myCurrFilterName = "";
1131 if ( myListBox->count() )
1132 myListBox->setCurrentItem( 0 );
1135 //=======================================================================
1136 // name : SMESHGUI_FilterLibraryDlg::onNeedValidation
1138 //=======================================================================
1139 void SMESHGUI_FilterLibraryDlg::onNeedValidation()
1141 if ( !myCurrFilterName.isEmpty() )
1143 bool valid = isValid( true );
1144 myTable->SetValidity( valid );
1148 SMESH::Filter_var aFilter = createFilter( myTable->GetType() );
1149 myLibrary->Replace( myCurrFilterName.latin1(), myName->text().latin1(), aFilter );