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"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_FilterUtils.h"
34 #include "SUIT_Session.h"
35 #include "SUIT_Desktop.h"
36 #include "SUIT_FileDlg.h"
39 #include <qapplication.h>
40 #include <qmessagebox.h>
43 #include <qlineedit.h>
44 #include <qpushbutton.h>
45 #include <qgroupbox.h>
49 #include <qfileinfo.h>
56 * Class : SMESHGUI_FilterLibraryDlg::Dialog
57 * Description : Dialog for opening filter library
60 class SMESHGUI_FilterLibraryDlg::Dialog : public SUIT_FileDlg
63 Dialog(QWidget* theParent, const bool theToOpen);
67 virtual bool acceptData();
70 SMESHGUI_FilterLibraryDlg::Dialog::Dialog (QWidget* theParent,
72 : SUIT_FileDlg(theParent, theToOpen)
76 SMESHGUI_FilterLibraryDlg::Dialog::~Dialog()
80 bool SMESHGUI_FilterLibraryDlg::Dialog::acceptData()
82 // if (mode() != QFileDialogP::AnyFile)
83 // return SUIT_FileDlg::acceptData();
89 * Class : SMESHGUI_FilterLibraryDlg
90 * Description : Dialog to specify filters for VTK viewer
93 //=======================================================================
94 // name : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
95 // Purpose : Constructor
96 //=======================================================================
97 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg (SMESHGUI* theModule,
99 const QValueList<int>& theTypes,
102 : QDialog( parent, theName, true, WStyle_Customize |
103 WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
104 mySMESHGUI( theModule )
106 construct(theTypes, theMode);
109 //=======================================================================
110 // name : SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg
111 // Purpose : Constructor
112 //=======================================================================
113 SMESHGUI_FilterLibraryDlg::SMESHGUI_FilterLibraryDlg (SMESHGUI* theModule,
118 : QDialog( parent, theName, true, WStyle_Customize |
119 WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
120 mySMESHGUI( theModule )
122 QValueList<int> aTypes;
123 aTypes.append(theType);
124 construct(aTypes, theMode);
127 //=======================================================================
128 // name : SMESHGUI_FilterLibraryDlg::construct
129 // Purpose : Construct dialog (called by constructor)
130 //=======================================================================
131 void SMESHGUI_FilterLibraryDlg::construct (const QValueList<int>& theTypes,
137 QVBoxLayout* aDlgLay = new QVBoxLayout(this, MARGIN, SPACING);
139 myMainFrame = createMainFrame (this);
140 QFrame* aBtnFrame = createButtonFrame(this);
142 aDlgLay->addWidget(myMainFrame);
143 aDlgLay->addWidget(aBtnFrame);
145 aDlgLay->setStretchFactor(myMainFrame, 1);
147 Init(myTypes, myMode);
150 //=======================================================================
151 // name : SMESHGUI_FilterLibraryDlg::createMainFrame
152 // Purpose : Create frame containing dialog's input fields
153 //=======================================================================
154 QFrame* SMESHGUI_FilterLibraryDlg::createMainFrame (QWidget* theParent)
156 QGroupBox* aMainFrame = new QGroupBox(1, Qt::Horizontal, theParent);
157 aMainFrame->setFrameStyle(QFrame::NoFrame);
158 aMainFrame->setInsideMargin(0);
162 QGroupBox* aGrp = new QGroupBox(1, Qt::Vertical, aMainFrame);
163 aGrp->setFrameStyle(QFrame::NoFrame);
164 aGrp->setInsideMargin(0);
166 new QLabel(tr("LIBRARY_FILE"), aGrp);
167 myFileName = new QLineEdit(aGrp);
168 myOpenBtn = new QPushButton(aGrp);
169 myOpenBtn->setPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap(
170 "SUIT", tr("ICON_FILE_OPEN")));
174 aGrp = new QGroupBox(1, Qt::Vertical, tr("FILTER_NAMES"), aMainFrame);
175 QFrame* aFrame = new QFrame(aGrp);
176 myListBox = new QListBox(aFrame);
177 myAddBtn = new QPushButton(tr("ADD"), aFrame);
178 myDeleteBtn = new QPushButton(tr("DELETE"), aFrame);
180 QGridLayout* aLay = new QGridLayout(aFrame, 3, 2, 0, 5);
181 aLay->addMultiCellWidget(myListBox, 0, 2, 0, 0);
182 aLay->addWidget(myAddBtn, 0, 1);
183 aLay->addWidget(myDeleteBtn, 1, 1);
184 QSpacerItem* aVSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
185 aLay->addItem(aVSpacer, 2, 1);
189 myNameGrp = new QGroupBox(1, Qt::Vertical, aMainFrame);
190 myNameGrp->setFrameStyle(QFrame::NoFrame);
191 myNameGrp->setInsideMargin(0);
192 new QLabel(tr("FILTER_NAME"), myNameGrp);
193 myName = new QLineEdit(myNameGrp);
197 myTable = new SMESHGUI_FilterTable( mySMESHGUI, aMainFrame, myTypes);
198 myTable->SetEditable(myMode == EDIT);
199 myTable->SetLibsEnabled(false);
201 myListBox->setMinimumHeight((int)(myTable->sizeHint().height() * 0.5));
202 myListBox->setRowMode(QListBox::FitToWidth);
203 myListBox->setSelectionMode(QListBox::Single);
205 myOpenBtn->setAutoDefault(false);
206 myAddBtn->setAutoDefault(false);
207 myDeleteBtn->setAutoDefault(false);
209 // connect signals and slots
211 connect(myFileName, SIGNAL(returnPressed()), this, SLOT(onReturnPressed()));
212 connect(myOpenBtn , SIGNAL(clicked()), this, SLOT(onBrowse()));
214 connect(myListBox, SIGNAL(highlighted(const QString&)),
215 this, SLOT(onFilterChanged(const QString&)));
217 connect(myAddBtn, SIGNAL(clicked()), this, SLOT(onAddBtnPressed()));
218 connect(myDeleteBtn, SIGNAL(clicked()), this, SLOT(onDeleteBtnPressed()));
220 connect(myName, SIGNAL(textChanged(const QString&)),
221 this, SLOT(onFilterNameChanged(const QString&)));
223 connect(myTable, SIGNAL(EntityTypeChanged(const int)),
224 this, SLOT(onEntityTypeChanged(const int)));
226 connect(myTable, SIGNAL(NeedValidation()), this, SLOT(onNeedValidation()));
231 //=======================================================================
232 // name : SMESHGUI_FilterLibraryDlg::createButtonFrame
233 // Purpose : Create frame containing buttons
234 //=======================================================================
235 QFrame* SMESHGUI_FilterLibraryDlg::createButtonFrame (QWidget* theParent)
237 QGroupBox* aGrp = new QGroupBox(1, Qt::Vertical, theParent);
239 myButtons[ BTN_OK ] = new QPushButton(tr("SMESH_BUT_OK" ), aGrp);
240 myButtons[ BTN_Apply ] = new QPushButton(tr("SMESH_BUT_APPLY"), aGrp);
242 QLabel* aLbl = new QLabel(aGrp);
243 aLbl->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
245 myButtons[ BTN_Cancel ] = new QPushButton(tr("SMESH_BUT_CANCEL"), aGrp);
246 myButtons[ BTN_Close ] = new QPushButton(tr("SMESH_BUT_CLOSE"), aGrp);
248 connect(myButtons[ BTN_OK ], SIGNAL(clicked()), SLOT(onOk()));
249 connect(myButtons[ BTN_Cancel ], SIGNAL(clicked()), SLOT(onClose()));
250 connect(myButtons[ BTN_Close ], SIGNAL(clicked()), SLOT(onClose()));
251 connect(myButtons[ BTN_Apply ], SIGNAL(clicked()), SLOT(onApply()));
253 QMap<int, QPushButton*>::iterator anIter;
254 for (anIter = myButtons.begin(); anIter != myButtons.end(); ++anIter)
255 anIter.data()->setAutoDefault(false);
262 //=======================================================================
263 // name : SMESHGUI_FilterLibraryDlg::updateMainButtons
264 // Purpose : Update visibility of main buttons (OK, Cancel, Close ...)
265 //=======================================================================
266 void SMESHGUI_FilterLibraryDlg::updateMainButtons()
268 if (myTypes.count() == 1) {
269 myButtons[ BTN_Cancel ]->show();
270 myButtons[ BTN_Apply ]->hide();
271 myButtons[ BTN_Close ]->hide();
273 myButtons[ BTN_Cancel ]->hide();
274 myButtons[ BTN_Apply ]->show();
275 myButtons[ BTN_Close ]->show();
279 //=======================================================================
280 // name : SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg
281 // Purpose : Destructor
282 //=======================================================================
283 SMESHGUI_FilterLibraryDlg::~SMESHGUI_FilterLibraryDlg()
287 //=======================================================================
288 // name : SMESHGUI_FilterLibraryDlg::Init
289 // Purpose : Init dialog fields, connect signals and slots, show dialog
290 //=======================================================================
291 void SMESHGUI_FilterLibraryDlg::Init (const int type, const int theMode)
293 QValueList<int> aTypes;
295 Init(aTypes, theMode);
298 //=======================================================================
299 // name : SMESHGUI_FilterLibraryDlg::Init
300 // Purpose : Init dialog fields, connect signals and slots, show dialog
301 //=======================================================================
302 void SMESHGUI_FilterLibraryDlg::Init (const QValueList<int>& theTypes,
307 myTable->Init(theTypes);
308 myCurrFilterName = "";
314 updateControlsVisibility();
317 connect( mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
318 connect( mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
320 if (myMode == ADD_TO)
322 setCaption(tr("ADD_TO_TLT"));
323 if (myFileName->text().isEmpty())
324 myFileName->setText(getDefaultLibraryName());
327 else if (myMode == COPY_FROM)
329 setCaption(tr("COPY_FROM_TLT"));
330 if (myFileName->text().isEmpty())
331 myFileName->setText(getDefaultLibraryName());
333 if (myListBox->count() > 0)
334 myListBox->setCurrentItem(0);
338 setCaption(tr("EDIT_LIB_TLT"));
339 if (myFileName->text().isEmpty())
340 myFileName->setText(getDefaultLibraryName());
342 if (myListBox->count() > 0)
343 myListBox->setCurrentItem(0);
347 mySMESHGUI->DefineDlgPosition(this, x, y);
353 isPermissionValid(false);
356 //=======================================================================
357 // name : SMESHGUI_FilterLibraryDlg::updateControlsVisibility
358 // Purpose : Update visibility of controls in accordance with myMode
359 //=======================================================================
360 void SMESHGUI_FilterLibraryDlg::updateControlsVisibility()
362 if (myMode == ADD_TO)
365 myNameGrp->setEnabled(true);
368 myTable->SetEditable(false);
370 else if (myMode == COPY_FROM)
373 myNameGrp->setEnabled(false);
376 myTable->SetEditable(false);
378 else if (myMode == EDIT)
381 myNameGrp->setEnabled(true);
384 myTable->SetEditable(true);
387 qApp->processEvents();
392 //=======================================================================
393 // name : SMESHGUI_FilterLibraryDlg::onApply
394 // Purpose : SLOT called when "Apply" button pressed.
395 //=======================================================================
396 bool SMESHGUI_FilterLibraryDlg::onApply()
398 if (!isValid(true) || !isPermissionValid(false))
401 if (myLibrary->_is_nil()) {
402 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
403 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
407 const char* aName = myFileName->text().latin1();
408 if (strcmp(myLibrary->GetFileName(), aName) != 0)
409 myLibrary->SetFileName(aName);
411 bool aResult = false;
413 if (myMode == COPY_FROM || myListBox->count() == 0) {
415 } else if (myMode == EDIT || myMode == ADD_TO) {
416 SMESH::Filter_var aFilter = createFilter();
417 if (!myLibrary->Replace(myCurrFilterName, myName->text(), aFilter.in())) {
418 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
419 tr("ERROR_OF_EDITING"), QMessageBox::Ok);
426 if (aResult && myMode != COPY_FROM)
427 aResult = myLibrary->Save();
430 char* aFileName = myLibrary->GetFileName();
431 getDefaultLibraryName() = QString(aFileName);
433 } else if (myMode != COPY_FROM) {
434 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
435 tr("ERROR_OF_SAVING"), QMessageBox::Ok);
442 //=======================================================================
443 // name : SMESHGUI_FilterLibraryDlg::onOk
444 // Purpose : SLOT called when "Ok" button pressed.
445 // Assign filters VTK viewer and close dialog
446 //=======================================================================
447 void SMESHGUI_FilterLibraryDlg::onOk()
451 disconnect( mySMESHGUI, 0, this, 0);
452 mySMESHGUI->ResetState();
457 //=======================================================================
458 // name : SMESHGUI_FilterLibraryDlg::onClose
459 // Purpose : SLOT called when "Close" button pressed. Close dialog
460 //=======================================================================
461 void SMESHGUI_FilterLibraryDlg::onClose()
463 disconnect( mySMESHGUI, 0, this, 0);
464 mySMESHGUI->ResetState();
468 //=======================================================================
469 // name : SMESHGUI_FilterLibraryDlg::onDeactivate
470 // Purpose : SLOT called when dialog must be deativated
471 //=======================================================================
472 void SMESHGUI_FilterLibraryDlg::onDeactivate()
477 //=======================================================================
478 // name : SMESHGUI_FilterLibraryDlg::enterEvent
479 // Purpose : Event filter
480 //=======================================================================
481 void SMESHGUI_FilterLibraryDlg::enterEvent(QEvent*)
486 //=================================================================================
487 // function : closeEvent()
488 // purpose : Close dialog
489 //=================================================================================
490 void SMESHGUI_FilterLibraryDlg::closeEvent(QCloseEvent* e)
495 //=======================================================================
496 // name : SMESHGUI_FilterLibraryDlg::getFileName
497 // Purpose : Get file name
498 //=======================================================================
499 QString SMESHGUI_FilterLibraryDlg::getFileName() const
501 return myFileName != 0 ? myFileName->text() : QString("");
504 //================================================================
505 // Function : setFileName
506 // Purpose : Set file name to line edit
507 //================================================================
508 void SMESHGUI_FilterLibraryDlg::setFileName(const QString& txt, const bool autoExt)
512 myFileName->setText(autoExt ? autoExtension(txt) : txt);
515 //================================================================
516 // Function : autoExtension
517 // Purpose : Append extension to the file name
518 //================================================================
519 QString SMESHGUI_FilterLibraryDlg::autoExtension(const QString& theFileName) const
521 QString anExt = theFileName.section('.', -1);
522 return anExt != "xml" && anExt != "XML" ? theFileName + ".xml" : theFileName;
525 //================================================================
526 // Function : filterWildCards
528 //================================================================
529 QStringList SMESHGUI_FilterLibraryDlg::filterWildCards(const QString& theFilter) const
533 int b = theFilter.findRev("(");
534 int e = theFilter.findRev(")");
535 if (b != -1 && e != -1)
537 QString content = theFilter.mid(b + 1, e - b - 1).stripWhiteSpace();
538 QStringList lst = QStringList::split(" ", content);
539 for (QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it)
540 if ((*it).find(".") != -1)
541 res.append((*it).stripWhiteSpace());
546 //=======================================================================
547 // name : SMESHGUI_FilterLibraryDlg::prepareFilters
548 // Purpose : Prepare filters for dialog
549 //=======================================================================
550 QStringList SMESHGUI_FilterLibraryDlg::prepareFilters() const
552 static QStringList aList;
555 aList.append(tr("XML_FILT"));
556 //aList.append(tr("ALL_FILES_FILTER"));
562 //================================================================
563 // Function : onBrowse
564 // Purpose : SLOT. Display "Open file" dialog for chosing library name
565 //================================================================
566 void SMESHGUI_FilterLibraryDlg::onBrowse()
568 Dialog* aDlg = new Dialog(this, true);
569 aDlg->setCaption(tr("OPEN_LIBRARY"));
571 //aDlg->setMode(myMode == COPY_FROM ? QFileDialogP::ExistingFile : QFileDialogP::AnyFile);
572 aDlg->setMode(myMode == COPY_FROM ? QFileDialog::ExistingFile : QFileDialog::AnyFile);
573 aDlg->setFilters(prepareFilters());
574 aDlg->setSelection(getFileName());
576 QPushButton* anOkBtn = (QPushButton*)aDlg->child("OK", "QPushButton");
578 anOkBtn->setText(tr("SMESH_BUT_OK"));
580 if (aDlg->exec() != Accepted)
583 QString fName = aDlg->selectedFile();
588 if (QFileInfo(fName).extension().isEmpty())
589 fName = autoExtension(fName);
591 fName = QDir::convertSeparators(fName);
592 QString prev = QDir::convertSeparators(getFileName());
599 QString aName = myListBox->text(myListBox->count() - 1);
602 if (myMode == ADD_TO)
604 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
605 myCurrFilterName = "";
607 addFilterToLib(aName);
610 isPermissionValid(false);
613 //=======================================================================
614 // name : SMESHGUI_FilterLibraryDlg::processNewLibrary
615 // Purpose : SLOT. Calleds when file name changed
616 //=======================================================================
617 void SMESHGUI_FilterLibraryDlg::processNewLibrary()
619 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
620 if (aFilterMgr->_is_nil())
623 myLibrary = aFilterMgr->LoadLibrary(autoExtension(getFileName()));
624 if (myLibrary->_is_nil()) {
625 if (myMode == COPY_FROM) {
626 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
627 tr("ERROR_LOAD"), QMessageBox::Ok);
630 myLibrary = aFilterMgr->CreateLibrary();
631 myLibrary->SetFileName(getFileName().latin1());
638 //=======================================================================
639 // name : SMESHGUI_FilterLibraryDlg::updateList
640 // Purpose : Fill list box with filter names
641 //=======================================================================
642 void SMESHGUI_FilterLibraryDlg::updateList()
645 SMESH::string_array_var aNames = myLibrary->GetNames((SMESH::ElementType)myTable->GetType());
646 for (int i = 0, n = aNames->length(); i < n; i++)
647 aList.append(QString(aNames[ i ]));
649 myListBox->insertStringList(aList);
650 if (myListBox->count() == 0)
652 myTable->Clear(myTable->GetType());
654 myName->setEnabled(false);
655 myTable->SetEnabled(false);
659 myName->setEnabled(true);
660 myTable->SetEnabled(true);
661 if (myListBox->count())
663 myCurrFilterName = "";
664 myListBox->setCurrentItem(0);
669 //=======================================================================
670 // name : SMESHGUI_FilterLibraryDlg::isNameValid
671 // Purpose : Verify validity if entered data
672 //=======================================================================
673 bool SMESHGUI_FilterLibraryDlg::isNameValid(const bool theMess) const
675 // verify validity of filter name
676 if (myName->isEnabled() && !myCurrFilterName.isEmpty()) {
677 QString aCurrName = myName->text();
678 if (aCurrName.isEmpty()) {
680 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
681 tr("EMPTY_FILTER_NAME"), QMessageBox::Ok);
685 SMESH::string_array_var aNames = myLibrary->GetAllNames();
686 for (int f = 0, n = aNames->length(); f < n; f++) {
687 if (aNames[ f ] == aCurrName && aNames[ f ] != myCurrFilterName) {
689 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
690 tr("ERROR_FILTER_NAME"), QMessageBox::Ok);
699 //=======================================================================
700 // name : SMESHGUI_FilterLibraryDlg::isPermissionValid
701 // Purpose : Verify write permission on file
702 //=======================================================================
703 bool SMESHGUI_FilterLibraryDlg::isPermissionValid(const bool theIsExistingOnly)
705 if (myMode == COPY_FROM)
708 // Verify write permission
709 bool isWritable = false;
711 QString fName(myFileName->text());
712 if (QFileInfo(fName).extension().isEmpty())
713 fName = autoExtension(fName);
715 fName = QDir::convertSeparators(fName);
717 if (QFileInfo(fName).exists()) {
718 isWritable = QFileInfo(fName).isWritable();
719 } else if (!theIsExistingOnly) {
720 QFileInfo aDirInfo(QFileInfo(fName).dirPath(true));
721 isWritable = aDirInfo.isWritable();
722 /*if (QDir(QFileInfo(fName).dirPath(true)).exists() ||
723 QDir().mkdir(QFileInfo(fName).dirPath(true)))
726 if (aFile.open(IO_WriteOnly))
737 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
738 tr("NO_PERMISSION"), QMessageBox::Ok);
745 //=======================================================================
746 // name : SMESHGUI_FilterLibraryDlg::isValid
747 // Purpose : Verify validity if entered data
748 //=======================================================================
749 bool SMESHGUI_FilterLibraryDlg::isValid(const bool theMess) const
751 // verify validity of table
752 if (!myTable->IsValid(theMess) || !isNameValid(theMess))
758 //=======================================================================
759 // name : SMESHGUI_FilterLibraryDlg::onFilterChanged
760 // Purpose : SLOT. Called when selected filter of library changed
761 //=======================================================================
762 void SMESHGUI_FilterLibraryDlg::onFilterChanged(const QString& theName)
764 if (myLibrary->_is_nil())
767 // Save parameters of filter if it was changed
769 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
773 myListBox->blockSignals(true);
774 myListBox->setCurrentItem(myCurrFilter);
775 myListBox->blockSignals(false);
779 SMESH::Filter_var aFilter = createFilter();
780 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
783 // Fill table with filter parameters
785 SMESH::Filter_var aFilter = myLibrary->Copy(theName);
786 myCurrFilterName = theName;
787 myCurrFilter = myListBox->currentItem();
788 myName->setText(theName);
791 SMESH::Filter::Criteria_var aCriteria;
793 myTable->Clear(myTable->GetType());
795 if (!aFilter->GetCriteria(aCriteria))
798 for (int i = 0, n = aCriteria->length(); i < n; i++)
799 myTable->AddCriterion(aCriteria[ i ], myTable->GetType());
804 //=======================================================================
805 // name : SMESHGUI_FilterLibraryDlg::onReturnPressed
806 // Purpose : SLOT. Called when enter button is pressed in library name field
808 //=======================================================================
809 void SMESHGUI_FilterLibraryDlg::onReturnPressed()
811 QString aName = myListBox->text(myListBox->count() - 1);
815 if (myMode == ADD_TO)
817 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
818 myCurrFilterName = "";
820 addFilterToLib(aName);
823 isPermissionValid(false);
826 //=======================================================================
827 // name : SMESHGUI_FilterLibraryDlg::enableMainButtons
828 // Purpose : Update state of "OK", "Cancel" buttons
829 //=======================================================================
830 void SMESHGUI_FilterLibraryDlg::enableMainButtons()
832 /*bool isEnabled = isValid(false);
833 if (myButtons.contains(BTN_OK))
834 myButtons[ BTN_OK ]->setEnabled(isEnabled);
835 else if (myButtons.contains(BTN_Apply))
836 myButtons[ BTN_OK ]->setEnabled(isEnabled);
837 if (myButtons.contains(BTN_Cancel))
838 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
839 else if (myButtons.contains(BTN_Close))
840 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
844 //=======================================================================
845 // name : SMESHGUI_FilterLibraryDlg::createFilter
846 // Purpose : Cerate filter in accordance with library
847 //=======================================================================
848 SMESH::Filter_ptr SMESHGUI_FilterLibraryDlg::createFilter(const int theType)
850 int n = myTable->NumRows(theType);
852 SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
853 aCriteria->length(n);
855 for (int i = 0; i < n; i++)
857 SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
858 myTable->GetCriterion(i, aCriterion);
859 aCriteria[ i ] = aCriterion;
862 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
863 SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
864 aFilter->SetCriteria(aCriteria.in());
866 return aFilter._retn();
869 //=======================================================================
870 // name : SMESHGUI_FilterLibraryDlg::onAddBtnPressed
871 // Purpose : SLOT. Called when "Add" button pressed
872 // Add new filter to the end of library
873 //=======================================================================
874 void SMESHGUI_FilterLibraryDlg::onAddBtnPressed()
876 // Save parameters of filter if it was changed
877 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
882 SMESH::Filter_var aFilter = createFilter();
883 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
886 addFilterToLib(getDefaultFilterName());
889 //=======================================================================
890 // name : onAddBtnPressed()
891 // Purpose : SLOT. Called when "Add" button pressed
892 // Add new filter to the end of library
893 //=======================================================================
894 void SMESHGUI_FilterLibraryDlg::addFilterToLib (const QString& theName)
896 if (myLibrary->_is_nil()) {
897 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
898 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
903 SMESH::Filter_var aFilter = createFilter();
905 // if name of filter already exist in the library assign default name for the filter
906 QString aName(theName);
907 SMESH::string_array_var aNames = myLibrary->GetAllNames();
908 for (int i = 0, n = aNames->length(); i < n; i++)
909 if (aName == QString(aNames[ i ]))
911 aName = getDefaultFilterName();
915 // add new filter in library
916 bool aResult = !aFilter->GetPredicate()->_is_nil()
917 ? myLibrary->Add(aName.latin1(), aFilter)
918 : myLibrary->AddEmpty(aName.latin1(), (SMESH::ElementType)myTable->GetType());
921 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
922 tr("ERROR_OF_ADDING"), QMessageBox::Ok);
926 myCurrFilterName = "";
930 if (theName != aName)
931 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
932 tr("ASSIGN_NEW_NAME").arg(theName).arg(aName), QMessageBox::Ok);
935 //=======================================================================
936 // name : SMESHGUI_FilterLibraryDlg::getDefaultLibraryName
937 // Purpose : Get default library name
938 //=======================================================================
939 QString& SMESHGUI_FilterLibraryDlg::getDefaultLibraryName() const
941 static QString aName;
944 QString aHomeDir = QDir(QDir::home()).absPath();
945 aName = aHomeDir + "/" + tr ("LIB_NAME");
950 //=======================================================================
951 // name : SMESHGUI_FilterLibraryDlg::getDefaultFilterName
952 // Purpose : Create default filter name
953 //=======================================================================
954 QString SMESHGUI_FilterLibraryDlg::getDefaultFilterName() const
958 if (myTable->GetType() == SMESH::NODE)
960 else if (myTable->GetType() == SMESH::EDGE)
962 else if (myTable->GetType() == SMESH::FACE)
964 else if (myTable->GetType() == SMESH::VOLUME)
965 aName = tr("VOLUME");
966 else if (myTable->GetType() == SMESH::ALL)
967 aName = tr("ELEMENT");
969 aName = tr("SELECTION");
971 aName += tr("FILTER");
974 QMap< QString, int > anAllNames;
975 SMESH::string_array_var aNames = myLibrary->GetAllNames();
976 for(int i = 0, n = aNames->length(); i < n; i++)
977 anAllNames[ QString(aNames[ i ]) ] = -1;
979 bool isNotValid = true;
985 aNewName = aName + "_" + QString("%1").arg(k);
986 if (anAllNames.contains(aNewName))
996 //=======================================================================
997 // name : SMESHGUI_FilterLibraryDlg::setSelected
998 // Purpose : set selected item in list box containing filters
999 //=======================================================================
1000 bool SMESHGUI_FilterLibraryDlg::setSelected(const QString& theName)
1002 int anIndex = getIndex(theName);
1005 myListBox->setCurrentItem(anIndex);
1006 myCurrFilterName = theName;
1007 myCurrFilter = anIndex;
1009 return anIndex != -1;
1012 //=======================================================================
1013 // name : SMESHGUI_FilterLibraryDlg::getIndex
1014 // Purpose : Get index of the filter in list box
1015 //=======================================================================
1016 int SMESHGUI_FilterLibraryDlg::getIndex(const QString& theName) const
1018 for (int i = 0, n = myListBox->count(); i < n; i++)
1019 if (myListBox->text(i) == theName)
1024 //=======================================================================
1025 // name : SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed
1026 // Purpose : SLOT. Called when "Delete" button pressed
1027 //=======================================================================
1028 void SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed()
1030 if (myLibrary->_is_nil()) {
1031 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
1032 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
1036 int anIndex = getIndex(myCurrFilterName);
1038 if (anIndex == -1 || !myLibrary->Delete(myCurrFilterName.latin1())) {
1039 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
1040 tr("ERROR_OF_DELETING"), QMessageBox::Ok);
1042 myCurrFilterName = "";
1044 myListBox->removeItem(anIndex);
1047 myListBox->setSelected(anIndex - 1, true);
1048 else if (anIndex == 0 && myListBox->count() > 0)
1049 myListBox->setSelected(0, true);
1054 myTable->SetEnabled(myListBox->count() > 0);
1055 if (myListBox->count() == 0) {
1056 myName->setText("");
1057 myName->setEnabled(false);
1061 //=======================================================================
1062 // name : onFilterNameChanged()
1063 // Purpose : SLOT. Called when name of filter changed
1064 // Change filter name in list box
1065 //=======================================================================
1066 void SMESHGUI_FilterLibraryDlg::onFilterNameChanged (const QString& theName)
1068 int aCurrItem = myListBox->currentItem();
1069 if (aCurrItem == -1)
1072 myListBox->blockSignals(true);
1073 myListBox->changeItem(theName, aCurrItem);
1074 myListBox->blockSignals(false);
1077 //=======================================================================
1078 // name : SMESHGUI_FilterLibraryDlg::SetTable
1079 // Purpose : Set table
1080 //=======================================================================
1081 void SMESHGUI_FilterLibraryDlg::SetTable(const SMESHGUI_FilterTable* theTable)
1083 myTable->Copy(theTable);
1084 myName->setText(getDefaultFilterName());
1085 addFilterToLib(myName->text());
1089 //=======================================================================
1090 // name : SMESHGUI_FilterLibraryDlg::GetTable
1091 // Purpose : Get table
1092 //=======================================================================
1093 const SMESHGUI_FilterTable* SMESHGUI_FilterLibraryDlg::GetTable() const
1099 //=======================================================================
1100 // name : SMESHGUI_FilterLibraryDlg::onEntityTypeChanged
1101 // Purpose : SLOT. Called when entiyt type changed
1102 //=======================================================================
1103 void SMESHGUI_FilterLibraryDlg::onEntityTypeChanged(const int theType)
1105 if (myLibrary->_is_nil())
1109 myCurrFilterName = "";
1112 if (myListBox->count())
1113 myListBox->setCurrentItem(0);
1116 //=======================================================================
1117 // name : SMESHGUI_FilterLibraryDlg::onNeedValidation
1119 //=======================================================================
1120 void SMESHGUI_FilterLibraryDlg::onNeedValidation()
1122 if (!myCurrFilterName.isEmpty())
1124 bool valid = isValid(true);
1125 myTable->SetValidity(valid);
1129 SMESH::Filter_var aFilter = createFilter(myTable->GetType());
1130 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);