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);
349 isPermissionValid(false);
352 //=======================================================================
353 // name : SMESHGUI_FilterLibraryDlg::updateControlsVisibility
354 // Purpose : Update visibility of controls in accordance with myMode
355 //=======================================================================
356 void SMESHGUI_FilterLibraryDlg::updateControlsVisibility()
358 if (myMode == ADD_TO)
361 myNameGrp->setEnabled(true);
364 myTable->SetEditable(false);
366 else if (myMode == COPY_FROM)
369 myNameGrp->setEnabled(false);
372 myTable->SetEditable(false);
374 else if (myMode == EDIT)
377 myNameGrp->setEnabled(true);
380 myTable->SetEditable(true);
383 qApp->processEvents();
388 //=======================================================================
389 // name : SMESHGUI_FilterLibraryDlg::onApply
390 // Purpose : SLOT called when "Apply" button pressed.
391 //=======================================================================
392 bool SMESHGUI_FilterLibraryDlg::onApply()
394 if (!isValid(true) || !isPermissionValid(false))
397 if (myLibrary->_is_nil()) {
398 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
399 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
403 const char* aName = myFileName->text().latin1();
404 if (strcmp(myLibrary->GetFileName(), aName) != 0)
405 myLibrary->SetFileName(aName);
407 bool aResult = false;
409 if (myMode == COPY_FROM || myListBox->count() == 0) {
411 } else if (myMode == EDIT || myMode == ADD_TO) {
412 SMESH::Filter_var aFilter = createFilter();
413 if (!myLibrary->Replace(myCurrFilterName, myName->text(), aFilter.in())) {
414 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
415 tr("ERROR_OF_EDITING"), QMessageBox::Ok);
422 if (aResult && myMode != COPY_FROM)
423 aResult = myLibrary->Save();
426 char* aFileName = myLibrary->GetFileName();
427 getDefaultLibraryName() = QString(aFileName);
429 } else if (myMode != COPY_FROM) {
430 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
431 tr("ERROR_OF_SAVING"), QMessageBox::Ok);
438 //=======================================================================
439 // name : SMESHGUI_FilterLibraryDlg::onOk
440 // Purpose : SLOT called when "Ok" button pressed.
441 // Assign filters VTK viewer and close dialog
442 //=======================================================================
443 void SMESHGUI_FilterLibraryDlg::onOk()
447 disconnect( mySMESHGUI, 0, this, 0);
448 mySMESHGUI->ResetState();
453 //=======================================================================
454 // name : SMESHGUI_FilterLibraryDlg::onClose
455 // Purpose : SLOT called when "Close" button pressed. Close dialog
456 //=======================================================================
457 void SMESHGUI_FilterLibraryDlg::onClose()
459 disconnect( mySMESHGUI, 0, this, 0);
460 mySMESHGUI->ResetState();
464 //=======================================================================
465 // name : SMESHGUI_FilterLibraryDlg::onDeactivate
466 // Purpose : SLOT called when dialog must be deativated
467 //=======================================================================
468 void SMESHGUI_FilterLibraryDlg::onDeactivate()
473 //=======================================================================
474 // name : SMESHGUI_FilterLibraryDlg::enterEvent
475 // Purpose : Event filter
476 //=======================================================================
477 void SMESHGUI_FilterLibraryDlg::enterEvent(QEvent*)
482 //=================================================================================
483 // function : closeEvent()
484 // purpose : Close dialog
485 //=================================================================================
486 void SMESHGUI_FilterLibraryDlg::closeEvent(QCloseEvent* e)
491 //=======================================================================
492 // name : SMESHGUI_FilterLibraryDlg::getFileName
493 // Purpose : Get file name
494 //=======================================================================
495 QString SMESHGUI_FilterLibraryDlg::getFileName() const
497 return myFileName != 0 ? myFileName->text() : QString("");
500 //================================================================
501 // Function : setFileName
502 // Purpose : Set file name to line edit
503 //================================================================
504 void SMESHGUI_FilterLibraryDlg::setFileName(const QString& txt, const bool autoExt)
508 myFileName->setText(autoExt ? autoExtension(txt) : txt);
511 //================================================================
512 // Function : autoExtension
513 // Purpose : Append extension to the file name
514 //================================================================
515 QString SMESHGUI_FilterLibraryDlg::autoExtension(const QString& theFileName) const
517 QString anExt = theFileName.section('.', -1);
518 return anExt != "xml" && anExt != "XML" ? theFileName + ".xml" : theFileName;
521 //================================================================
522 // Function : filterWildCards
524 //================================================================
525 QStringList SMESHGUI_FilterLibraryDlg::filterWildCards(const QString& theFilter) const
529 int b = theFilter.findRev("(");
530 int e = theFilter.findRev(")");
531 if (b != -1 && e != -1)
533 QString content = theFilter.mid(b + 1, e - b - 1).stripWhiteSpace();
534 QStringList lst = QStringList::split(" ", content);
535 for (QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it)
536 if ((*it).find(".") != -1)
537 res.append((*it).stripWhiteSpace());
542 //=======================================================================
543 // name : SMESHGUI_FilterLibraryDlg::prepareFilters
544 // Purpose : Prepare filters for dialog
545 //=======================================================================
546 QStringList SMESHGUI_FilterLibraryDlg::prepareFilters() const
548 static QStringList aList;
551 aList.append(tr("XML_FILT"));
552 //aList.append(tr("ALL_FILES_FILTER"));
558 //================================================================
559 // Function : onBrowse
560 // Purpose : SLOT. Display "Open file" dialog for chosing library name
561 //================================================================
562 void SMESHGUI_FilterLibraryDlg::onBrowse()
564 Dialog* aDlg = new Dialog(this, true);
565 aDlg->setCaption(tr("OPEN_LIBRARY"));
567 //aDlg->setMode(myMode == COPY_FROM ? QFileDialogP::ExistingFile : QFileDialogP::AnyFile);
568 aDlg->setMode(myMode == COPY_FROM ? QFileDialog::ExistingFile : QFileDialog::AnyFile);
569 aDlg->setFilters(prepareFilters());
570 aDlg->setSelection(getFileName());
572 QPushButton* anOkBtn = (QPushButton*)aDlg->child("OK", "QPushButton");
574 anOkBtn->setText(tr("SMESH_BUT_OK"));
576 if (aDlg->exec() != Accepted)
579 QString fName = aDlg->selectedFile();
584 if (QFileInfo(fName).extension().isEmpty())
585 fName = autoExtension(fName);
587 fName = QDir::convertSeparators(fName);
588 QString prev = QDir::convertSeparators(getFileName());
595 QString aName = myListBox->text(myListBox->count() - 1);
598 if (myMode == ADD_TO)
600 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
601 myCurrFilterName = "";
603 addFilterToLib(aName);
606 isPermissionValid(false);
609 //=======================================================================
610 // name : SMESHGUI_FilterLibraryDlg::processNewLibrary
611 // Purpose : SLOT. Calleds when file name changed
612 //=======================================================================
613 void SMESHGUI_FilterLibraryDlg::processNewLibrary()
615 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
616 if (aFilterMgr->_is_nil())
619 myLibrary = aFilterMgr->LoadLibrary(autoExtension(getFileName()));
620 if (myLibrary->_is_nil()) {
621 if (myMode == COPY_FROM) {
622 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
623 tr("ERROR_LOAD"), QMessageBox::Ok);
626 myLibrary = aFilterMgr->CreateLibrary();
627 myLibrary->SetFileName(getFileName().latin1());
634 //=======================================================================
635 // name : SMESHGUI_FilterLibraryDlg::updateList
636 // Purpose : Fill list box with filter names
637 //=======================================================================
638 void SMESHGUI_FilterLibraryDlg::updateList()
641 SMESH::string_array_var aNames = myLibrary->GetNames((SMESH::ElementType)myTable->GetType());
642 for (int i = 0, n = aNames->length(); i < n; i++)
643 aList.append(QString(aNames[ i ]));
645 myListBox->insertStringList(aList);
646 if (myListBox->count() == 0)
648 myTable->Clear(myTable->GetType());
650 myName->setEnabled(false);
651 myTable->SetEnabled(false);
655 myName->setEnabled(true);
656 myTable->SetEnabled(true);
657 if (myListBox->count())
659 myCurrFilterName = "";
660 myListBox->setCurrentItem(0);
665 //=======================================================================
666 // name : SMESHGUI_FilterLibraryDlg::isNameValid
667 // Purpose : Verify validity if entered data
668 //=======================================================================
669 bool SMESHGUI_FilterLibraryDlg::isNameValid(const bool theMess) const
671 // verify validity of filter name
672 if (myName->isEnabled() && !myCurrFilterName.isEmpty()) {
673 QString aCurrName = myName->text();
674 if (aCurrName.isEmpty()) {
676 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
677 tr("EMPTY_FILTER_NAME"), QMessageBox::Ok);
681 SMESH::string_array_var aNames = myLibrary->GetAllNames();
682 for (int f = 0, n = aNames->length(); f < n; f++) {
683 if (aNames[ f ] == aCurrName && aNames[ f ] != myCurrFilterName) {
685 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
686 tr("ERROR_FILTER_NAME"), QMessageBox::Ok);
695 //=======================================================================
696 // name : SMESHGUI_FilterLibraryDlg::isPermissionValid
697 // Purpose : Verify write permission on file
698 //=======================================================================
699 bool SMESHGUI_FilterLibraryDlg::isPermissionValid(const bool theIsExistingOnly)
701 if (myMode == COPY_FROM)
704 // Verify write permission
705 bool isWritable = false;
707 QString fName(myFileName->text());
708 if (QFileInfo(fName).extension().isEmpty())
709 fName = autoExtension(fName);
711 fName = QDir::convertSeparators(fName);
713 if (QFileInfo(fName).exists()) {
714 isWritable = QFileInfo(fName).isWritable();
715 } else if (!theIsExistingOnly) {
716 QFileInfo aDirInfo(QFileInfo(fName).dirPath(true));
717 isWritable = aDirInfo.isWritable();
718 /*if (QDir(QFileInfo(fName).dirPath(true)).exists() ||
719 QDir().mkdir(QFileInfo(fName).dirPath(true)))
722 if (aFile.open(IO_WriteOnly))
733 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
734 tr("NO_PERMISSION"), QMessageBox::Ok);
741 //=======================================================================
742 // name : SMESHGUI_FilterLibraryDlg::isValid
743 // Purpose : Verify validity if entered data
744 //=======================================================================
745 bool SMESHGUI_FilterLibraryDlg::isValid(const bool theMess) const
747 // verify validity of table
748 if (!myTable->IsValid(theMess) || !isNameValid(theMess))
754 //=======================================================================
755 // name : SMESHGUI_FilterLibraryDlg::onFilterChanged
756 // Purpose : SLOT. Called when selected filter of library changed
757 //=======================================================================
758 void SMESHGUI_FilterLibraryDlg::onFilterChanged(const QString& theName)
760 if (myLibrary->_is_nil())
763 // Save parameters of filter if it was changed
765 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
769 myListBox->blockSignals(true);
770 myListBox->setCurrentItem(myCurrFilter);
771 myListBox->blockSignals(false);
775 SMESH::Filter_var aFilter = createFilter();
776 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
779 // Fill table with filter parameters
781 SMESH::Filter_var aFilter = myLibrary->Copy(theName);
782 myCurrFilterName = theName;
783 myCurrFilter = myListBox->currentItem();
784 myName->setText(theName);
787 SMESH::Filter::Criteria_var aCriteria;
789 myTable->Clear(myTable->GetType());
791 if (!aFilter->GetCriteria(aCriteria))
794 for (int i = 0, n = aCriteria->length(); i < n; i++)
795 myTable->AddCriterion(aCriteria[ i ], myTable->GetType());
800 //=======================================================================
801 // name : SMESHGUI_FilterLibraryDlg::onReturnPressed
802 // Purpose : SLOT. Called when enter button is pressed in library name field
804 //=======================================================================
805 void SMESHGUI_FilterLibraryDlg::onReturnPressed()
807 QString aName = myListBox->text(myListBox->count() - 1);
811 if (myMode == ADD_TO)
813 myTable->Copy((SMESHGUI_FilterTable*)parentWidget());
814 myCurrFilterName = "";
816 addFilterToLib(aName);
819 isPermissionValid(false);
822 //=======================================================================
823 // name : SMESHGUI_FilterLibraryDlg::enableMainButtons
824 // Purpose : Update state of "OK", "Cancel" buttons
825 //=======================================================================
826 void SMESHGUI_FilterLibraryDlg::enableMainButtons()
828 /*bool isEnabled = isValid(false);
829 if (myButtons.contains(BTN_OK))
830 myButtons[ BTN_OK ]->setEnabled(isEnabled);
831 else if (myButtons.contains(BTN_Apply))
832 myButtons[ BTN_OK ]->setEnabled(isEnabled);
833 if (myButtons.contains(BTN_Cancel))
834 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
835 else if (myButtons.contains(BTN_Close))
836 myButtons[ BTN_Cancel ]->setEnabled(isEnabled);
840 //=======================================================================
841 // name : SMESHGUI_FilterLibraryDlg::createFilter
842 // Purpose : Cerate filter in accordance with library
843 //=======================================================================
844 SMESH::Filter_ptr SMESHGUI_FilterLibraryDlg::createFilter(const int theType)
846 int n = myTable->NumRows(theType);
848 SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
849 aCriteria->length(n);
851 for (int i = 0; i < n; i++)
853 SMESH::Filter::Criterion aCriterion = SMESHGUI_FilterDlg::createCriterion();
854 myTable->GetCriterion(i, aCriterion);
855 aCriteria[ i ] = aCriterion;
858 SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
859 SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
860 aFilter->SetCriteria(aCriteria.in());
862 return aFilter._retn();
865 //=======================================================================
866 // name : SMESHGUI_FilterLibraryDlg::onAddBtnPressed
867 // Purpose : SLOT. Called when "Add" button pressed
868 // Add new filter to the end of library
869 //=======================================================================
870 void SMESHGUI_FilterLibraryDlg::onAddBtnPressed()
872 // Save parameters of filter if it was changed
873 if (!myCurrFilterName.isEmpty() && myTable->IsEditable())
878 SMESH::Filter_var aFilter = createFilter();
879 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);
882 addFilterToLib(getDefaultFilterName());
885 //=======================================================================
886 // name : onAddBtnPressed()
887 // Purpose : SLOT. Called when "Add" button pressed
888 // Add new filter to the end of library
889 //=======================================================================
890 void SMESHGUI_FilterLibraryDlg::addFilterToLib (const QString& theName)
892 if (myLibrary->_is_nil()) {
893 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
894 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
899 SMESH::Filter_var aFilter = createFilter();
901 // if name of filter already exist in the library assign default name for the filter
902 QString aName(theName);
903 SMESH::string_array_var aNames = myLibrary->GetAllNames();
904 for (int i = 0, n = aNames->length(); i < n; i++)
905 if (aName == QString(aNames[ i ]))
907 aName = getDefaultFilterName();
911 // add new filter in library
912 bool aResult = !aFilter->GetPredicate()->_is_nil()
913 ? myLibrary->Add(aName.latin1(), aFilter)
914 : myLibrary->AddEmpty(aName.latin1(), (SMESH::ElementType)myTable->GetType());
917 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
918 tr("ERROR_OF_ADDING"), QMessageBox::Ok);
922 myCurrFilterName = "";
926 if (theName != aName)
927 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WARNING"),
928 tr("ASSIGN_NEW_NAME").arg(theName).arg(aName), QMessageBox::Ok);
931 //=======================================================================
932 // name : SMESHGUI_FilterLibraryDlg::getDefaultLibraryName
933 // Purpose : Get default library name
934 //=======================================================================
935 QString& SMESHGUI_FilterLibraryDlg::getDefaultLibraryName() const
937 static QString aName;
940 QString aHomeDir = QDir(QDir::home()).absPath();
941 aName = aHomeDir + "/" + tr ("LIB_NAME");
946 //=======================================================================
947 // name : SMESHGUI_FilterLibraryDlg::getDefaultFilterName
948 // Purpose : Create default filter name
949 //=======================================================================
950 QString SMESHGUI_FilterLibraryDlg::getDefaultFilterName() const
954 if (myTable->GetType() == SMESH::NODE)
956 else if (myTable->GetType() == SMESH::EDGE)
958 else if (myTable->GetType() == SMESH::FACE)
960 else if (myTable->GetType() == SMESH::VOLUME)
961 aName = tr("VOLUME");
962 else if (myTable->GetType() == SMESH::ALL)
963 aName = tr("ELEMENT");
965 aName = tr("SELECTION");
967 aName += tr("FILTER");
970 QMap< QString, int > anAllNames;
971 SMESH::string_array_var aNames = myLibrary->GetAllNames();
972 for(int i = 0, n = aNames->length(); i < n; i++)
973 anAllNames[ QString(aNames[ i ]) ] = -1;
975 bool isNotValid = true;
981 aNewName = aName + "_" + QString("%1").arg(k);
982 if (anAllNames.contains(aNewName))
992 //=======================================================================
993 // name : SMESHGUI_FilterLibraryDlg::setSelected
994 // Purpose : set selected item in list box containing filters
995 //=======================================================================
996 bool SMESHGUI_FilterLibraryDlg::setSelected(const QString& theName)
998 int anIndex = getIndex(theName);
1001 myListBox->setCurrentItem(anIndex);
1002 myCurrFilterName = theName;
1003 myCurrFilter = anIndex;
1005 return anIndex != -1;
1008 //=======================================================================
1009 // name : SMESHGUI_FilterLibraryDlg::getIndex
1010 // Purpose : Get index of the filter in list box
1011 //=======================================================================
1012 int SMESHGUI_FilterLibraryDlg::getIndex(const QString& theName) const
1014 for (int i = 0, n = myListBox->count(); i < n; i++)
1015 if (myListBox->text(i) == theName)
1020 //=======================================================================
1021 // name : SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed
1022 // Purpose : SLOT. Called when "Delete" button pressed
1023 //=======================================================================
1024 void SMESHGUI_FilterLibraryDlg::onDeleteBtnPressed()
1026 if (myLibrary->_is_nil()) {
1027 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_WRN_WARNING"),
1028 tr("LIBRARY_IS_NOT_LOADED"), QMessageBox::Ok);
1032 int anIndex = getIndex(myCurrFilterName);
1034 if (anIndex == -1 || !myLibrary->Delete(myCurrFilterName.latin1())) {
1035 QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
1036 tr("ERROR_OF_DELETING"), QMessageBox::Ok);
1038 myCurrFilterName = "";
1040 myListBox->removeItem(anIndex);
1043 myListBox->setSelected(anIndex - 1, true);
1044 else if (anIndex == 0 && myListBox->count() > 0)
1045 myListBox->setSelected(0, true);
1050 myTable->SetEnabled(myListBox->count() > 0);
1051 if (myListBox->count() == 0) {
1052 myName->setText("");
1053 myName->setEnabled(false);
1057 //=======================================================================
1058 // name : onFilterNameChanged()
1059 // Purpose : SLOT. Called when name of filter changed
1060 // Change filter name in list box
1061 //=======================================================================
1062 void SMESHGUI_FilterLibraryDlg::onFilterNameChanged (const QString& theName)
1064 int aCurrItem = myListBox->currentItem();
1065 if (aCurrItem == -1)
1068 myListBox->blockSignals(true);
1069 myListBox->changeItem(theName, aCurrItem);
1070 myListBox->blockSignals(false);
1073 //=======================================================================
1074 // name : SMESHGUI_FilterLibraryDlg::SetTable
1075 // Purpose : Set table
1076 //=======================================================================
1077 void SMESHGUI_FilterLibraryDlg::SetTable(const SMESHGUI_FilterTable* theTable)
1079 myTable->Copy(theTable);
1080 myName->setText(getDefaultFilterName());
1081 addFilterToLib(myName->text());
1085 //=======================================================================
1086 // name : SMESHGUI_FilterLibraryDlg::GetTable
1087 // Purpose : Get table
1088 //=======================================================================
1089 const SMESHGUI_FilterTable* SMESHGUI_FilterLibraryDlg::GetTable() const
1095 //=======================================================================
1096 // name : SMESHGUI_FilterLibraryDlg::onEntityTypeChanged
1097 // Purpose : SLOT. Called when entiyt type changed
1098 //=======================================================================
1099 void SMESHGUI_FilterLibraryDlg::onEntityTypeChanged(const int theType)
1101 if (myLibrary->_is_nil())
1105 myCurrFilterName = "";
1108 if (myListBox->count())
1109 myListBox->setCurrentItem(0);
1112 //=======================================================================
1113 // name : SMESHGUI_FilterLibraryDlg::onNeedValidation
1115 //=======================================================================
1116 void SMESHGUI_FilterLibraryDlg::onNeedValidation()
1118 if (!myCurrFilterName.isEmpty())
1120 bool valid = isValid(true);
1121 myTable->SetValidity(valid);
1125 SMESH::Filter_var aFilter = createFilter(myTable->GetType());
1126 myLibrary->Replace(myCurrFilterName.latin1(), myName->text().latin1(), aFilter);